/
check-linux-uptime
55 lines (42 loc) · 1.15 KB
/
check-linux-uptime
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash
# Checks uptime
# Requires a platform supporting /proc/uptime and date tool
# Exit codes
OK=0
WARNING=1
CRITICAL=2
UNKNOWN=3
exitcode=$UNKNOWN
codetext=("Ok" "Warning" "Critical" "Unknown")
# Usage help
usage() {
text="
USAGE: ${0} -c <seconds> -w <seconds>
-c: return CRITICAL if uptime is shorter than the value
-w: return WARNING if uptime is shorter than the value
"
echo "$text"
exit $exitcode
}
# check for command line arguments
while getopts "w:c:h" option
do
case "$option" in
c) critical=$OPTARG;;
w) warning=$OPTARG;;
h) usage;;
*) usage;;
esac
done
[[ ! -f "/proc/uptime" ]] && echo "/proc/uptime does not exist." && exit $exitcode
# Extract the first value and strip off decimal part
uptime=$(cat /proc/uptime | cut -d' ' -f1)
uptime=${uptime%.*}
exitcode=$OK
# date magic
message=$(echo $(($uptime/86400))" days" $(date -d "1970-01-01 + $uptime seconds" "+%H hours %M minutes %S seconds"))
# threshold checks
[[ (! -z $warning) && ($uptime -lt $warning) ]] && exitcode=$WARNING
[[ (! -z $critical) && ($uptime -lt $critical) ]] && exitcode=$CRITICAL
echo "Uptime ${codetext[$exitcode]} - $message"
exit $exitcode