#!/bin/bash


defaultvalue=10000000
args=$(getopt -l "warning:,critical:" -o "w:,c:" -a -- "$@")
eval set -- "$args"

while true
do
        case "$1" in
        -w|--warning)
                shift
                upwarn="$1"
                ;;
        -c|--critical)
                shift
                upcrit="$1"
                ;;
        --)
                shift
                break
                ;;
        esac
        shift
done

uptime -s >/dev/null 2>/dev/null
exit_code=$?

case $exit_code in

        0)

                # Get the start timestamp
                start_timestamp=$(date -d "$(uptime -s)" +"%s")
                uptime_pretty="$(uptime -p)"
                ;;
        1)

                # Get the start timestamp
                start_timestamp=$(date -d "`cut -f1 -d. /proc/uptime` seconds ago" +"%s")
                current_timestamp=$(date +"%s")
                time_diff=$((current_timestamp - start_timestamp))
                uptime_pretty="$(($time_diff / 86400)) days"
                ;;
esac

# Get the current timestamp

current_timestamp=$(date +"%s")

# Calculate the difference in seconds
time_diff=$((current_timestamp - start_timestamp))

# Convert seconds to minutes
minutes_diff=$((time_diff / 60))

time=$minutes_diff

if [ -z "$upwarn" ]; then upwarn=$defaultvalue; fi
if [ -z "$upcrit" ]; then upcrit=$defaultvalue; fi

if [ "$time" -gt "$upcrit" ]; then
        echo "[CRITICAL] uptime: $uptime_pretty |uptime=$time"
        exit 2;
fi
if [ "$time" -gt "$upwarn" ]; then
        echo "[WARNING] uptime: $uptime_pretty |uptime=$time"
        exit 1;
else
        echo "[OK] uptime: $uptime_pretty |uptime=$time"
        exit 0;
fi