icinga2-checks/check_uptime.sh

22 lines
822 B
Bash
Raw Normal View History

2023-06-08 00:35:27 -06:00
#!/bin/bash
2023-06-02 17:52:30 -06:00
2023-06-08 00:35:27 -06:00
# Check if the system is Linux or BSD
if uname | grep -q "BSD"; then
# Get uptime in seconds for BSD
boot_time_seconds=$(sysctl -n kern.boottime | awk -F'[ ,]' '{print $4}')
total_uptime_seconds=$(($(date +%s) - boot_time_seconds))
else
total_uptime_seconds=$(cat /proc/uptime | awk '{print $1}')
2023-06-08 22:48:55 -06:00
total_uptime_seconds=$(printf "%.0f" "${total_uptime_seconds}")
2023-06-02 17:52:30 -06:00
fi
2023-06-08 00:35:27 -06:00
# Calculate uptime in days, hours, minutes, and seconds
uptime_days=$((total_uptime_seconds / 86400))
2023-06-08 22:48:55 -06:00
uptime_hours=$(((total_uptime_seconds % 86400) / 3600))
uptime_minutes=$(((total_uptime_seconds % 3600) / 60))
2023-06-08 00:35:27 -06:00
uptime_seconds=$((total_uptime_seconds % 60))
2023-06-02 17:52:30 -06:00
2023-06-08 00:35:27 -06:00
# Print the result and perfdata
2024-08-05 14:06:03 -06:00
echo "OK: Uptime is ${uptime_days}d ${uptime_hours}h ${uptime_minutes}m ${uptime_seconds}s | uptime_seconds=${total_uptime_seconds}s;"
2023-06-02 17:52:30 -06:00
exit 0