timecheck 3.49 KB
Newer Older
Dan Reading's avatar
Dan Reading committed
1
#!/bin/bash
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#
# Copyright (c) 2013 University of Utah and the Flux Group.
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
Dan Reading's avatar
Dan Reading committed
24
25
26
27

echo -n ' Starting timecheck.. '

ntpserver="ntp1"
28
maxdrift="0.005"
Dan Reading's avatar
Dan Reading committed
29

30
source checkutils.sh
Dan Reading's avatar
Dan Reading committed
31

32
initialize $@
Dan Reading's avatar
Dan Reading committed
33

34
35
36
# are we called from somewhere or are we standalone
x=$(caller)
[[ "${x/NULL}" = "$x" ]] && declare -ri timecheck_standalone=0 || declare -ri timecheck_standalone=1
Dan Reading's avatar
Dan Reading committed
37
38
39
40
41

# check that external need program are installed
s=""
case $os in
    Linux | FreeBSD )
42
	progs="tr ntpdate host"
Dan Reading's avatar
Dan Reading committed
43
44
45
46
47
	;;
    * )
	failed="Unknown OS :$os: FAILED"
	echo "$failed" >> ${logfile}
	echo "$failed"
48
	(( $timecheck_standalone )) && exit 0 || return 0
Dan Reading's avatar
Dan Reading committed
49
50
51
52
53
54
55
56
57
	;;
esac
for i in $progs ; do
    type $i &>/dev/null && continue  || s="$s $i "
done
if [ -n "$s" ] ; then
    failed=" Unable to run need missing command(s) $s FAILED"
    echo "$failed" >> ${logfile}
    echo "$failed"
58
    (( $timecheck_standalone )) && exit 0 || return 0
Dan Reading's avatar
Dan Reading committed
59
60
61
62
fi

{
unset -v d ; declare -a d=()
63

64
65
save_e
set +e
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
dnsok=$(host $ntpserver) 2>/dev/null
if [ "${dnsok/NXDOMAIN}" != "$dnsok" ] ; then
    echo -n "WARNING ntpserver $ntpserver unknown. " >> ${logfile}
    x=$(/usr/local/etc/emulab/tmcc ntpinfo)
    if [ -n "$x" ] ; then
	ntpserver=${x/SERVER=}
    else
	echo -n "tmcc ntpinfo also failed. Last try from resolv.conf" >> ${logfile}
	x=$(grep search /etc/resolv.conf)
	if [ -n "$x" ] ; then
	    ntpserver="${ntpserver}.${x/search }"
	fi
    fi
    echo -n "Trying ntpserver $ntpserver " >> ${logfile}
    dnsok=$(host $ntpserver) 2>/dev/null
    if [ "${dnsok/NXDOMAIN}" != "$dnsok" ] ; then
	echo "WARNING $ntpserver also unknown. " >> ${logfile}
	echo "Giving up">> ${logfile}
	(( $timecheck_standalone )) && exit 0 || return 0
    fi
fi

Dan Reading's avatar
Dan Reading committed
88
89
90
91
92
93
94
95
96
97
98
case $os in
    Linux | FreeBSD )
	d=($(ntpdate -q $ntpserver))
	(( $? )) && failed="No Response from ntpserver:$ntpserver"

	for ((idx=0; idx<${#d[*]}; idx++)) ; do
	    [[ "${d[$idx]}" == "offset" ]] && break
	done
	((++idx))
	z=${d[$idx]}
	offset=$(echo ${z} | tr -d ,) # remove comma
99
	[[ $offset < 0 ]] && a=$(echo $offset | tr -d '-') || a=$offset # make $a abs()
Dan Reading's avatar
Dan Reading committed
100
101
102
	[[ $a > $maxdrift ]] && failed="maxdrift"
	if  [ -z "${failed}" ]; then
	    echo "Time check passed offset $offset allowed $maxdrift"
103
	elif [ "${failed}" = "maxdrift" ] ; then
Dan Reading's avatar
Dan Reading committed
104
105
106
107
108
109
110
111
112
113
114
	    echo "Time check failed offset $offset greater then $maxdrift second "
	else
	    echo "${failed}"
	fi
	;;
    * )
	echo "os $os unknown"
	offset="-1"
	failed="os $os unknown"
	;;
esac
115
restore_e
Dan Reading's avatar
Dan Reading committed
116
117
} >> ${tmplog} 2>&1

118
echo -n "Timecheck `date`: " >> ${logfile}
Dan Reading's avatar
Dan Reading committed
119
120
121
122
123
124
125
126
127
128
cat ${tmplog} >> ${logfile} 

if [ -z "${failed}" ] ; then 
    echo "offset $offset < $maxdrift OK" 
elif [ "${failed}" == "maxdrift" ] ; then
    echo "offset $offset > $maxdrift FAILED"
else
    echo "${failed} FAILED"
fi

129
130
131
132
# Check to if called standalone or source from another shell
(( $timecheck_standalone )) && exit 0 || return 0

# return 0