-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_linux_ntp_time
85 lines (73 loc) · 1.46 KB
/
check_linux_ntp_time
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/bash
Usage()
{
echo "Usage: $0 -H host -w warning -c critical (in seconds)"
}
while getopts ":H:w:c:" opt
do
case ${opt} in
H )
host=${OPTARG}
;;
w )
warning=${OPTARG}
;;
c )
critical=${OPTARG}
;;
* )
Usage
exit 2
;;
esac
done
shift $((OPTIND-1))
if [ -z "${host}" ] || [ -z "${warning}" ] || [ -z "${critical}" ]
then
Usage
exit 2
fi
if (( $(echo "$warning > $critical" | bc -l) ))
then
echo "Warning threshold cannot be equal or bigger than critical"
exit
fi
ntp_config=`ssh nagios@$host "grep -w ^server /etc/ntp.conf 2> /dev/null"`
if [[ -z $ntp_config ]]
then
ntp_config=`ssh nagios@$host "grep -w ^server /etc/chrony.conf 2>/dev/null"`
if [[ -z $ntp_config ]]
then
echo "SSH/ntp.conf/chrony.conf error"
exit 3
fi
fi
ntp_servers=`echo $ntp_config | sed -r 's/\b(iburst|server)\b//g'`
for server in $ntp_servers
do
remote=`ssh nagios@$host "/usr/sbin/ntpdate -q $server"`
if [ $? -eq 0 ]
then
diff=`echo $remote| awk '{print $6}' | cut -d , -f1`
ntp_server=$server
break
fi
done
if [ -z "$remote" ]
then
echo "Check failed. Issue with ntpdate"
exit 3
fi
diff=${diff/-/}
if [[ "$diff" > "$critical" ]]
then
echo "CRITICAL: Offset is $diff seconds, to: $ntp_server | Status=2"
exit 2
fi
if [[ "$diff" > "$warning" ]]
then
echo "WARNING: Offset is $diff seconds, to: $ntp_server | Status=1"
exit 1
fi
echo "OK: In sync with: $ntp_server | Status=0"
exit 0