check_uptime.sh: fix for bsd

This commit is contained in:
Cyberes 2023-06-08 00:35:27 -06:00
parent cbc7e8e9b4
commit 0c8f675676
1 changed files with 20 additions and 21 deletions

View File

@ -1,26 +1,25 @@
#!/usr/bin/env bash #!/bin/bash
# Get the uptime start time # Check if the system is Linux or BSD
UPTIME_OUTPUT=$(uptime -s) # if [[ "$(uname)" == "Linux" ]]; then
UPTIME_EXIT_CODE=$? # # Get uptime in seconds for Linux
# total_uptime_seconds=$(cat /proc/uptime | awk '{print $1}')
# Check if the uptime command was successful if uname | grep -q "BSD"; then
if [ $UPTIME_EXIT_CODE -ne 0 ]; then # Get uptime in seconds for BSD
echo "UNKNOWN - Unable to get uptime" boot_time_seconds=$(sysctl -n kern.boottime | awk -F'[ ,]' '{print $4}')
exit -1 total_uptime_seconds=$(($(date +%s) - boot_time_seconds))
else
total_uptime_seconds=$(cat /proc/uptime | awk '{print $1}')
# echo "UNKNOWN: Unsupported system"
# exit 3
fi fi
# Calculate the uptime in seconds # Calculate uptime in days, hours, minutes, and seconds
UPTIME_START=$(date -u -d "$UPTIME_OUTPUT" +%s) uptime_days=$((total_uptime_seconds / 86400))
CURRENT_TIME=$(date -u +%s) uptime_hours=$(( (total_uptime_seconds % 86400) / 3600 ))
UPTIME_SECONDS=$((CURRENT_TIME - UPTIME_START)) uptime_minutes=$(( (total_uptime_seconds % 3600) / 60 ))
uptime_seconds=$((total_uptime_seconds % 60))
# Convert the uptime seconds to hours # Print the result and perfdata
UPTIME_HOURS=$(echo "scale=2; $UPTIME_SECONDS / 3600" | bc) echo "OK: Uptime is ${uptime_days}d ${uptime_hours}h ${uptime_minutes}m ${uptime_seconds}s | total_uptime_seconds=${total_uptime_seconds}s;"
# Convert the uptime seconds to days
UPTIME_DAYS=$(echo "scale=2; $UPTIME_SECONDS / 86400" | bc)
# Print the result and performance data
echo "OK - Uptime: $UPTIME_DAYS days | uptime_hours=$UPTIME_HOURS"
exit 0 exit 0