From 0c8f6756760831b36146a81fcd31cad6e54aa33a Mon Sep 17 00:00:00 2001 From: Cyberes Date: Thu, 8 Jun 2023 00:35:27 -0600 Subject: [PATCH] check_uptime.sh: fix for bsd --- check_uptime.sh | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/check_uptime.sh b/check_uptime.sh index 62382ee..b5ae57e 100755 --- a/check_uptime.sh +++ b/check_uptime.sh @@ -1,26 +1,25 @@ -#!/usr/bin/env bash +#!/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 -1 +# Check if the system is Linux or BSD +# if [[ "$(uname)" == "Linux" ]]; then +# # Get uptime in seconds for Linux +# total_uptime_seconds=$(cat /proc/uptime | awk '{print $1}') +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}') + # echo "UNKNOWN: Unsupported system" + # 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)) +# Calculate uptime in days, hours, minutes, and seconds +uptime_days=$((total_uptime_seconds / 86400)) +uptime_hours=$(( (total_uptime_seconds % 86400) / 3600 )) +uptime_minutes=$(( (total_uptime_seconds % 3600) / 60 )) +uptime_seconds=$((total_uptime_seconds % 60)) -# Convert the uptime seconds to hours -UPTIME_HOURS=$(echo "scale=2; $UPTIME_SECONDS / 3600" | bc) - -# 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" +# Print the result and perfdata +echo "OK: Uptime is ${uptime_days}d ${uptime_hours}h ${uptime_minutes}m ${uptime_seconds}s | total_uptime_seconds=${total_uptime_seconds}s;" exit 0