-
Notifications
You must be signed in to change notification settings - Fork 1
/
ntp_shdiff
executable file
·432 lines (398 loc) · 13.1 KB
/
ntp_shdiff
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/usr/bin/env ksh
# #!/usr/bin/ksh
# #!/usr/bin/bash
# Sat Feb 28 21:21:50 CET 2015 - mayer
# Sun Mar 29 22:22:12 CEST 2015 - mayer - optimized arg parser , $YEAR
# Wed Apr 1 21:46:47 CEST 2015 - mayer - ip check
# Wednesday, July 15, 2015 10:52:49 PM MEST - mayer
# Sun Aug 16 14:44:04 CEST 2015 - mayer - bugfix $AWK
# Sun Aug 23 20:03:01 CEST 2015 - mayer - bugfix endless loop
# Sat Jan 2 19:10:33 CET 2016 - mayer - y-label
# Sun Jan 10 09:38:23 CET 2016 - mayer - x-range
# Fri Feb 5 22:47:37 CET 2016 - mayer - fit ohne prevalue
# Sat Feb 6 19:43:12 CET 2016 - mayer - bugfix for label function
# Mon Feb 8 20:51:40 CET 2016 - mayer - polynomfunktion ersten u zweiten grades
# Tue Feb 9 21:37:19 CET 2016 - mayer - y-axis format
# Fri Feb 12 21:53:56 CET 2016 - mayer - fit function quiet, print results
# Sat Feb 13 22:37:35 CET 2016 - mayer - show options
# Sun Feb 14 18:04:52 CET 2016 - mayer - array of values with 8 decimal places
# Wed Feb 24 21:19:12 CET 2016 - mayer - option w
# Mon May 9 21:14:42 CEST 2016 - mayer - option c - range for calculation
# 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
# Sun Jan 22 13:16:12 CET 2017 - mayer - git merge from pull request github.com/velotraveler/ntpgraph
# Mon Jan 23 20:45:29 CET 2017 - mayer - set fit quiet only for gnuplot above 4.6
# Sat Jan 28 21:26:21 CET 2017 - mayer - debug option Z with argument 1 or 9
# Sun Jun 4 13:46:50 CEST 2017 - mayer - check XRANGE
# Fri Jul 7 21:45:31 CEST 2017 - mayer - calculation only for the given XRANGE
# Tue Aug 15 20:25:07 CEST 2017 - mayer - bugfix $do_once
export DISPLAY DAYS TIMESPERH IP1 IP2 AWK STATSDIR PEERSTATS LIST YRANGE FILE XFIT YFIT FFIT UNWFIT LOGFIT FIT_LOG
export TERM YEAR LINES AVG AVGLINE SELDATE LOOPCNT XRANGE LBLPOS LINEX SETLBL POLYN PRRESULT SHOWOPT
export LINEWIDTH TIMEDIFFLOW TIMEDIFFHIGH FITQUIET DEBUGOPT XLOW XHIGH
AWK=gawk
# default one point per hour
TIMESPERH=1
# a temporary file deletede at the end
LIST=/tmp/erg.dat.$$
# default output is to screen - could be file with option -f
TERM="set term x11 persist"
# default this year
YEAR=`TZ=GMT date '+%Y'`
# connect the point with curves - changed with option -l
LINES="smooth csplines"
# miximum time difference, values below will be taken for calculation
TIMEDIFFLOW=-1.1
TIMEDIFFHIGH=1.1
LINEWIDTH=2
XTICS=1
XRANGE='[-0.5:24.8]'
LBLPOS=0.95
thisusage(){
set +x
echo
echo "show time difference for 2 NTP server as graph - v 2017 08 15 "
echo " author: ntpgraph@ma.yer.at "
echo " source: https://github.com/hans-mayer/ntpgraph "
echo
echo "usage: $0 [ -a ] [ -f ] [ -l ] [ -m value ] [ -c low high ] [ -t value ] [ -w value ] [ -y range ] [ -F n ] [ -L ] [ -D 1|9 ] IP1 IP2 DATESPEC "
echo " DATESPEC is MMDD in year $YEAR or YYYYMMDD or . (today) or - (yesterday) "
echo " DATESPEC can also be path to peerstats file"
echo " -x range - low:high, example 1:10 , default autorange -0.5:24.8 , time in hours "
echo " -y range - low:high, example -0.1:0.1 , default autorange "
echo " -a - print average line "
echo " -l - straight line instead of smooth csplines "
echo " -f IMG - output to file in current working directory - IMG can be jpeg, png, ... "
echo " -t number - values per hour for average calculation, default is 1 "
echo " -m value - maximum time difference - default 1.1 second "
echo " -c low high - lower and upper limit to calculate - default 1.1 second "
echo " -F n - fit function, n polynomial ( 1 or 2 ) "
echo " -L - label at bottom - only for fit function "
echo " -w value - line width "
echo " -D 1|9 - debug 1 ... less, 9 ... more "
echo
exit 1
}
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
# no limitation for the y-range , can be overwritten with -y
YRANGE='[*:*]'
# check the arguments
LOOPCNT=0
: echo argc: $#
do_once=1
while test $# -gt 3 -o $do_once -eq 1
do
: echo argc: $#
do_once=0
case $1 in
-t ) TIMESPERH=$2 ; shift ; shift ; SHOWOPT="$SHOWOPT -t $TIMESPERH" ;;
-x ) XRANGE="[$2]" ; SHOWOPT="$SHOWOPT -x $2 h" ; shift ; shift ;;
-y ) YRANGE="[$2]" ; SHOWOPT="$SHOWOPT -y $2" ; shift ; shift ;;
-a ) AVGLINE=", \"$LIST\" using 1:3 title 'average' $LINES ls 2 " ; shift ;;
-f ) FILE="set output \"plot_$$.$2\" "
TERM="set terminal $2"
LINEWIDTH=1
XTICS=2
echo $FILE ; shift ; shift ;;
-l ) LINES="with lines" ; shift ;;
-D ) DEBUG=y ; shift ; DEBUGOPT=$1 ; shift ;;
-m ) TIMEDIFFLOW=-$2 ; TIMEDIFFHIGH=$2 ; shift ; shift ; SHOWOPT="$SHOWOPT -m $TIMEDIFFHIGH" ;;
-c ) TIMEDIFFLOW=$2 ; TIMEDIFFHIGH=$3 ; shift ; shift ; shift ; SHOWOPT="$SHOWOPT -c $TIMEDIFFLOW $TIMEDIFFHIGH" ;;
-F ) POLYN=$2 ; shift ; shift ; FFIT=', line(x) ' ; SHOWOPT="$SHOWOPT -F $POLYN" ;;
-L ) LBLPOS=0.1 ; shift ;;
-w ) LINEWIDTH=$2 ; shift ; shift ;;
-* ) thisusage ; exit 1 ;;
esac
if test $# -lt 3
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
: echo "$AVGLINE$ "
# now we should have 2 IP addresses and 1 date field
if test $# -eq 3
then
IP1=$1
IP2=$2
MDATE=$3
else
thisusage
exit 1
fi
if test -z "$FILE"
then
# check if DISPLAY is set ?
case "$DISPLAY" in
localhost:* ) ;;
"" )
if test -z "$DISPLAY"
then
echo $0: \$DISPLAY not set
exit 1
fi
;;
esac
fi
# if date field is a valid filename, use it as datasource
# otherwise perform rudimentary date check
TITLEHOST=$(uname -n)
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` ;;
* ) if [ -f "$MDATE" ]
then
STATSFILEPATH=$MDATE
TITLEHOST=$(basename $STATSFILEPATH)
else
echo "$0: wrong date format - must be one of {MMDD | YYYYMMDD | . | - } or a filename" 1>&2
thisusage
exit 1
fi
;;
esac
# very rudimentary check of IP addresses - V4 and V6
case "$IP1" in
[1-9]*\.[0-9]*\.[0-9]*\.[0-9]* ) ;;
[1-9]*:* ) ;;
* ) thisusage
exit 1 ;;
esac
case "$IP2" in
[1-9]*\.[0-9]*\.[0-9]*\.[0-9]* ) ;;
[1-9]*:* ) ;;
* ) thisusage
exit 1 ;;
esac
# check XRANGE low value smaller than high value
echo $XRANGE | tr '[:]' ' ' | $AWK '{ if ( $1 >= $2 ) { exit 1 } else exit 0 }'
if test $? -ne 0
then
echo
echo "xrange low < high, example 1:10.75 "
thisusage
exit 1
fi
# split XRANGE into 2 parts
XLOW=`echo $XRANGE | tr '[:]' ' ' | $AWK '{ print $1 }'`
XHIGH=`echo $XRANGE | tr '[:]' ' ' | $AWK '{ print $2 }'`
: echo XLOW $XLOW XHIGH $XHIGH
# 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
# what is statsdir directory and peerstats file ?
# 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 no filegen keyword in ntp.conf
: echo STATSDIR $STATSDIR PEERSTATS $PEERSTATS
STATSFILEPATH=${STATSFILEPATH:-$STATSDIR/$PEERSTATS.$SELDATE}
# does at least one file exist ?
ls $STATSFILEPATH > /dev/null 2>&1
if test $? -ne 0
then
echo $0: $STATSFILEPATH not found
exit 1
fi
DAYS=`ls $STATSFILEPATH | wc -l | $AWK '{ print $1 }'`
: echo
# peerstats column 2 are seconds after midnight
# myarray enhaelt anzahl gefundenet zeilen pro stunde
: echo $AWK -v DAYS=$DAYS -v TIMESPERH=$TIMESPERH -v IP1=$IP1 -v IP2=$IP2
cat $STATSFILEPATH | \
$AWK -v XLOW=$XLOW -v XHIGH=$XHIGH '{ if ( ( XLOW * 3600 <= $2 ) && ( $2 <= XHIGH * 3600 ) ) { print $0 } }' | \
$AWK -v DAYS=$DAYS -v TIMESPERH=$TIMESPERH -v IP1=$IP1 -v IP2=$IP2 -v TIMEDIFFLOW=$TIMEDIFFLOW -v TIMEDIFFHIGH=$TIMEDIFFHIGH '
BEGIN {
# print ( "awk begin " ) ;
IP1LAST = -1 ;
IP2LAST = -1 ;
AVERAGE = 0 ;
CNT4AVG = 0 ;
}
{ TSLOT = int($2/3600*TIMESPERH) ;
# print ( IP1 , IP1LAST, IP2, IP2LAST, $3, $5 ) ;
# initialisierung
if ( ( IP1 == $3 ) && ( IP1LAST == -1 ) ) {
# in diese bedingung faellt man nur einmal
# print ( "init IP1 " ) ;
IP1LAST = 1 ;
TIME1 = $5 ;
} ;
if ( ( IP2 == $3 ) && ( IP2LAST == -1 ) ) {
# in diese bedingung faellt man nur einmal
# print ( "init IP2 " ) ;
IP2LAST = 1 ;
TIME2 = $5 ;
} ;
# wenn die letzte IP die IP1 war und jetzt IP2 gelesen wird
if ( ( IP1LAST == 1 ) && ( IP2 == $3 ) ) {
DIFFTIME = TIME1 - $5 ;
if ( ( DIFFTIME < TIMEDIFFHIGH ) && ( DIFFTIME > TIMEDIFFLOW ) ) {
# we are within the range where calculation takes place
# in myarray wird mitgezaehlt und aufsummiert
myarrayC[TSLOT]++ ;
myarrayT[TSLOT] += DIFFTIME ;
IP1LAST = 0 ;
IP2LAST = 1 ;
AVERAGE += DIFFTIME ;
CNT4AVG++ ;
}
else
printf ( "%1.10f %s %02d:%02d:%02d %s \n" , DIFFTIME , $2, $2/3600, $2/60%60, $2%60, IP2 ) | "cat 1>&2"
} ;
if ( IP2 == $3 ) TIME2 = $5 ;
if ( ( IP2LAST == 1 ) && ( IP1 == $3 ) ) {
DIFFTIME = $5 - TIME2 ;
if ( ( DIFFTIME < TIMEDIFFHIGH ) && ( DIFFTIME > TIMEDIFFLOW ) ) {
myarrayC[TSLOT]++ ;
myarrayT[TSLOT] += DIFFTIME ;
IP1LAST = 1 ;
IP2LAST = 0 ;
AVERAGE += DIFFTIME ;
CNT4AVG++ ;
}
else
printf ( "%1.10f %s %02d:%02d:%02d %s \n" , DIFFTIME , $2, $2/3600, $2/60%60, $2%60, IP1 ) | "cat 1>&2"
} ;
if ( IP1 == $3 ) TIME1 = $5 ;
myarray[int($2/3600*TIMESPERH)]++ }
END {
AVG = AVERAGE / CNT4AVG ;
for (i = 0 ; i <= 24*TIMESPERH ; i++ ) {
# printf ( "%.2f %.6f %d %d \n", i/TIMESPERH, myarrayT[i], myarrayC[i], myarray[i] ) | "cat 1>&2" ;
if ( myarrayC[i] > 0 )
printf ( "%.2f %.8f %.8f \n", i/TIMESPERH, myarrayT[i]/myarrayC[i], AVG ) ;
else
printf ( "# %.3f %.8f \n", 0, 0 ) ;
} ;
print ( "\n" ) ;
# print ( "average: " AVG "\n" ) | "cat 1>&2" ;
}' > $LIST
: exit 0
# if getent does not exist $HOST1 and $HOST2 is empty
HOST1=`getent hosts $IP1 2> /dev/null | $AWK '{ print $2 }'`
HOST2=`getent hosts $IP2 2> /dev/null | $AWK '{ print $2 }'`
if test -n "$AVGLINE"
then
AVG="- average: `grep -v '^#' $LIST | head -1 | $AWK '{ print $3 }'`"
fi
# if reverse lookup does not work take the IP
if test -z "$HOST1"
then
HOST1=$IP1
fi
if test -z "$HOST2"
then
HOST2=$IP2
fi
# result file could be empty - avoid additional error messages
if test ! -s "$LIST"
then
echo "no data found "
thisusage
exit 1
fi
# only gnuplot version 4.6 and higher supports "set fit quiet "
gnuplot -V | $AWK '{ if ( $2 >= 4.6 ) { exit 1 } else exit 0 }'
if test $? -eq 1
then
FITQUIET='set fit quiet'
else
FITQUIET=''
fi
FIT_LOG=/tmp/fit.log.$$
y0=0.0
if test -n "$FFIT"
then
LOGFIT="set fit logfile \"$FIT_LOG\""
YFIT=`head -1 "$LIST" | $AWK '{ print ( "y0 = " , $2 ) }'`
case "$POLYN" in
1 )
UNWFIT="fit line(x) \"$LIST\" via y0, m"
XFIT='m = 0.01'
LINEX='line(x) = y0 + m*x'
SETLBL="set label 2 sprintf(\"y0 = %2.7e \n m = %2.7e\",y0,m) left at graph 0.05, graph $LBLPOS"
PRRESULT="print \"y0 = \" , y0 ,\"\n m = \" , m "
;;
2 )
UNWFIT="fit line(x) \"$LIST\" via y0, m, n"
XFIT='m = 0.01 ; n = 0.01 '
LINEX='line(x) = y0 + m*x + n*x*x'
SETLBL="set label 2 sprintf(\"y0 = %2.7e \n m = %2.7e \n n = %2.7e \",y0,m,n) left at graph 0.05, graph $LBLPOS"
PRRESULT="print \"y0 = \" , y0 ,\"\n m = \" , m ,\"\n n = \" , n "
;;
* )
thisusage
exit 1
;;
esac
fi
gnuplot << EOF
$TERM
$FILE
set title "$TITLEHOST: $IP1 - $IP2 - $SELDATE \n $HOST1 - $HOST2 $AVG\n$SHOWOPT"
set xlabel "hour"
set ylabel "offset (sec)"
set xtics $XTICS
# set ytics 10
set xrange $XRANGE
set yrange $YRANGE
set style line 1 linewidth $LINEWIDTH
set style line 2 linewidth 1
set format y "%9.6f"
set dummy x, y
$LINEX
$LOGFIT
$FITQUIET
$XFIT
$YFIT
$UNWFIT
$SETLBL
plot "$LIST" using 1:2 title 'offset' $LINES ls 1 $AVGLINE $FFIT
$PRRESULT
EOF
if test "$DEBUG" == y
then
if test -f $FIT_LOG
then
ls -l $FIT_LOG
fi
echo STATSDIR $STATSDIR PEERSTATS $STATSFILEPATH
echo plotfile: $LIST
echo "$AVG "
else
rm -f $LIST $FIT_LOG
fi
exit 0