From eb4e6f690e8cc5beeeb215eea5d934df65da88c3 Mon Sep 17 00:00:00 2001 From: Cyberes Date: Fri, 2 Jun 2023 17:52:30 -0600 Subject: [PATCH] add check_uptime --- check_uptime.sh | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100755 check_uptime.sh diff --git a/check_uptime.sh b/check_uptime.sh new file mode 100755 index 0000000..8464c2c --- /dev/null +++ b/check_uptime.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# Get the uptime start time +UPTIME_OUTPUT=$(uptime -s) +UPTIME_EXIT_CODE=$? + +# Check if the uptime command was successful +if [ $UPTIME_EXIT_CODE -ne 0 ]; then + echo "UNKNOWN - Unable to get uptime" + exit 3 +fi + +# Calculate the uptime in seconds +UPTIME_START=$(date -u -d "$UPTIME_OUTPUT" +%s) +CURRENT_TIME=$(date -u +%s) +UPTIME_SECONDS=$((CURRENT_TIME - UPTIME_START)) + +# Convert the uptime seconds to hours +UPTIME_HOURS=$(echo "scale=2; $UPTIME_SECONDS / 3600" | bc) + +# Get the human-readable uptime +UPTIME_HUMAN=$(uptime -p) + +# Print the result and performance data +echo "OK - Uptime: $UPTIME_HUMAN | uptime_hours=$UPTIME_HOURS" +exit 0