-
Notifications
You must be signed in to change notification settings - Fork 1
/
ntp_shavail
executable file
·267 lines (233 loc) · 7.26 KB
/
ntp_shavail
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env ksh
# Sat Aug 15 13:33:59 CEST 2015
# Wednesday, August 19, 2015 03:42:24 PM CEST - mayer
# Sun Aug 23 20:03:01 CEST 2015 - mayer - bugfix endless loop
# Sat Feb 6 20:13:51 CET 2016 - mayer - bugfix for date eq - ( yesterday )
# Thu Sep 22 15:00:47 CEST 2016 - mayer - using /usr/bin/env ksh
# Thu Sep 22 16:44:24 CEST 2016 - mayer - getent not availavle on OS X
# Sat Jan 28 21:26:21 CET 2017 - mayer - debug option Z with argument 1 or 9
# Sun Apr 16 12:12:11 CEST 2017 - mayer - option n / noselect
# Friday, June 30, 2017 10:22:31 AM CEST - mayer - rm NOTSELECTED
# Tue Aug 15 20:25:07 CEST 2017 - mayer - bugfix $do_once
export DISPLAY AWK STATSDIR PEERSTATS SELDATE CONF YEAR MINARGS TMPIPADDR SEDARG LIST STERM
export TMP1 TITLE1 TITLE2 YLABEL HOSTN MYADDR LOOPCNT DEBUGOPT NOSELECT NOTSELECTED H SRV NOSELHOST
AWK=gawk
YEAR=`TZ=GMT date '+%Y'`
DEBUG=n
MINARGS=1
TMPIPADDR=/tmp/...tmpipaddr.$$
LIST=/tmp/erg.dat.$$
STERM="set term x11 persist"
NOTSELECTED=/tmp/...ntp_shav_notselected
NOSELECT=n
thisusage(){
echo
echo "show NTP available peers as graph - v 2017 08 15 "
echo " author: ntpgraph@ma.yer.at "
echo " source: https://github.com/hans-mayer/ntpgraph "
echo
echo "usage: $0 [ -n ] [ -D 1|9 ] [ -f IMG ] DATE "
echo " date is MMDD in year $YEAR or YYYYMMDD or . or - ( . is today, - is yesterday ) "
echo " -f IMG - output to file - IMG can be jpeg, png, ... "
echo " -n - don't take server marked as noselect "
echo " -D 1|9 - debug 1 ... less, 9 ... more "
echo
exit 1
}
# check the arguments
LOOPCNT=0
# go at least 1 time into the loop / there is no do until
do_once=1
while test $# -gt $MINARGS -o $do_once -eq 1
do
do_once=0
case $1 in
-D ) DEBUG=y ; shift ; DEBUGOPT=$1 ; shift ;;
-f ) FILE="set output \"plot_$$.$2\" "
STERM="set terminal $2 size 900,500"
LINEWIDTH=1
echo $FILE ; shift ; shift ;;
-n ) NOSELECT=y ; shift ;;
- ) break ;; # yesterday
-* ) thisusage ; exit 1 ;;
esac
if test $# -lt $MINARGS
then
thisusage
exit 1
fi
LOOPCNT=$((LOOPCNT+1))
# check if we are not running into an endless loop
if test "$LOOPCNT" -gt 30
then
thisusage
exit 1
fi
done
if test "$DEBUGOPT" -eq 9
then
set -x
fi
if test $# -eq 1
then
MDATE=$1
else
thisusage
exit 1
fi
# very rudimentary check of date
case "$MDATE" in
[0-9\?][0-9\?][0-9\?][0-9\?] )
SELDATE=$YEAR$MDATE ;;
[12][90][0-9\?][0-9\?][0-9\?][0-9\?][0-9\?][0-9\?] )
SELDATE=$MDATE ;;
\. ) SELDATE=`TZ=GMT date +%Y%m%d` ;;
- ) SELDATE=`TZ=GMT+24 date +%Y%m%d` ;;
* ) echo $0: wrong date format - must be MMDD or . 1>&2
thisusage
exit 1
;;
esac
if test -z "$FILE"
then
# check if DISPLAY is set ?
case "$DISPLAY" in
localhost:* ) ;;
"" ) echo '$DISPLAY not set' ; exit 1 ;;
* ) ;;
esac
fi
# check if gawk is available
type $AWK > /dev/null
if test $? -ne 0
then
echo try to install $AWK
echo if not available try a symlink to awk or change this script
exit 1
fi
# check where the conf file is located
CONF=N
if test -f /etc/ntp.conf
then
CONF=/etc/ntp.conf
fi
if test -f /etc/inet/ntp.conf
then
CONF=/etc/inet/ntp.conf
fi
if test $CONF = N
then
echo $0: ntp.conf not found
exit 1
fi
# look what is statsdir directory and peerstats file defined
# awk is needed for solaris to strip off the leading blanks
STATSDIR=`grep statsdir $CONF | grep -v '^#' | tr '""' ' ' | head -1 | $AWK '{ print ( $2 ) }'`
PEERSTATS=`grep filegen $CONF | grep peerstats | grep -v '^#' | head -1 | $AWK '{ print ( $4 ) }'`
PEERSTATS=${PEERSTATS:-peerstats} # in case filegen keyword not found
if test "$DEBUG" == y
then
echo CONF $CONF
echo STATSDIR $STATSDIR PEERSTATS $PEERSTATS.$SELDATE
echo TMPIPADDR $TMPIPADDR
echo NOTSELECTED $NOTSELECTED
fi
if test ! -f $STATSDIR/${PEERSTATS}.$SELDATE
then
echo $0: file $STATSDIR/${PEERSTATS}.$SELDATE not found
exit 1
fi
# OS X doesn't have getent
type getent > /dev/null 2>&1
if test $? -ne 0 -a $NOSELECT = y
then
echo option noselect -n disabled as program getent is not available
echo if this is macOS from Apple try to get an emulated script for example from
echo https://github.com/petere/getent-osx.git
NOSELECT=n
fi
rm -f $NOTSELECTED
if test $NOSELECT = y
then
# create a list if IP which we do not show
NOSELHOST=`egrep '^server .* noselect' $CONF | $AWK '{ print $2 }'`
if test -n "$NOSELHOST"
then
echo "$NOSELHOST" | while read SRV
do
# mac os x doesn't have getent - so option -n will not work
# H is empty if it's an ip and no reverse lookup available
H=`getent hosts $SRV 2> /dev/null`
if test -z "$H"
then
echo $SRV >> $NOTSELECTED
else
echo $H | $AWK '{ print $1 }' >> $NOTSELECTED
fi
done
else
echo $0: there are no noselected hosts found
# any string which could not be a server name for grep -v
echo we_take_all_servers > $NOTSELECTED
fi
else
echo we_take_all_servers > $NOTSELECTED
fi
cat $STATSDIR/${PEERSTATS}.$SELDATE | fgrep -v -f $NOTSELECTED | \
$AWK '{ print ( $3 ) }' | sort -u | $AWK '{ print ( $1 , NR ) }' | \
while read IP IND
do
# if getent does not exist $HOST1 is empty
echo $IP $IND `getent hosts $IP 2> /dev/null | $AWK '{ print ( $2 ) }'`
done > $TMPIPADDR
# max ip found + 1
YRANGE=`wc -l $TMPIPADDR | $AWK '{ print ( "[0.8:" $1 + 1 "]" ) }'`
TITLE1=`cat $TMPIPADDR | $AWK '{ print ( $2 ":" , $1 ) }' | tr "\n" " "`
# find the DNS name for each host
TMP1=`cat $TMPIPADDR | \
while read IP INDEX NAM
do
if test -z "$NAM"
then
case "$IP" in
127.127.22.* ) echo "$INDEX: PPS " ;;
127.127.28.* ) echo "$INDEX: GPS " ;;
esac
else
getent hosts $IP | $AWK -v INDEX=$INDEX '{ print ( INDEX ":" , $2 , " " ) }'
fi
done `
# now remove the line breaks
TITLE2=`echo $TMP1`
MYADDR=`ifconfig -a | grep inet | egrep -v '::1/128|127.0.0.1|inet6' | head -1 | $AWK '{ print $2 }'`
HOSTN=`hostname`
# replace IP address by index number
SEDARG=`cat $TMPIPADDR | $AWK '{ print ( "-e s/" $1 "/" $2 "/" ) }'`
# first host has number 1
# for peer status word see https://www.eecis.udel.edu/~mills/ntp/html/decode.html#peer
# if host 1 is system peer then it gets value 1.6
sed $SEDARG < $STATSDIR/${PEERSTATS}.$SELDATE | $AWK '{ print ( $2, $3, $4 ) }' | \
$AWK '{ print ( $1 , $2 , substr ( $3, 2, 1 ) ) }' | \
$AWK '{ printf ( "%.4f %.2f \n", $1/3600, $2 + $3/10 ) }' > $LIST
YLABEL='.0 reject / .1 x falsetick / .3 - outlyer \n.4 + candidate / .5 # backup / .6 * peer / .7 o pps '
gnuplot << EOF
$STERM
$FILE
set title "$SELDATE\n$TITLE1\n$TITLE2"
set xlabel "hour"
set ylabel "$YLABEL"
set yrange $YRANGE
set xtics 1
set ytics 0.2
set xrange [-0.5:24.8]
plot "$LIST" using 1:2 title "$HOSTN - $MYADDR"
EOF
rm -f $NOTSELECTED
if test "$DEBUG" == y
then
echo plotfile: $LIST
cat $TMPIPADDR
else
rm -f $LIST $TMPIPADDR
fi
exit 0