-
Notifications
You must be signed in to change notification settings - Fork 6
/
ntpd.pl
executable file
·208 lines (184 loc) · 6.17 KB
/
ntpd.pl
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
#!/usr/bin/perl
#
# RRD script to display ntpd statistics using ntpq
#
# Copyright (C) 2017 Christian Garbs <[email protected]>
# Licensed under GNU GPL v3 or later.
#
# This file is part of my rrd scripts (https://github.com/mmitch/rrd).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
# This script should be run every 5 minutes.
#
use strict;
use warnings;
use RRDs;
# parse configuration file
my %conf;
eval(`cat ~/.rrd-conf.pl`);
die $@ if $@;
# set variables
my $datafile = "$conf{DBPATH}/ntpd.rrd";
my $picbase = "$conf{OUTPATH}/ntpd-peers-";
my $picbase2 = "$conf{OUTPATH}/ntpd-stats-";
# global error variable
my $ERR;
# get graph minimum time ($DETAIL_TIME in rrd_runall.sh)
my $MINTIME = 1;
if (defined $ARGV[0])
{
$MINTIME = shift @ARGV;
}
# whoami?
my $hostname = `/bin/hostname`;
chomp $hostname;
# generate database if absent
if (! -e $datafile ) {
RRDs::create($datafile,
'DS:outlyers:GAUGE:600:0:32', # max 32 peers
'DS:candidates:GAUGE:600:0:32', # max 32 peers
'DS:selected:GAUGE:600:0:32', # max 32 peers
'DS:sel_delay:GAUGE:600:-1024000:1024000', # max ~15m delay
'DS:sel_offset:GAUGE:600:-1024000:1024000', # max ~15m offset
'DS:sel_jitter:GAUGE:600:-1000:1000', # max 1s jitter
'DS:sys_offset:GAUGE:600:-1024000:1024000', # max ~15m offset
'DS:sys_jitter:GAUGE:600:-1000:1000', # max 1s sys jitter
'DS:clk_jitter:GAUGE:600:-1000:1000', # max 1s clk jitter
'DS:clk_wander:GAUGE:600:-1000:1000', # max 1s clock wander
'RRA:AVERAGE:0.5:1:600',
'RRA:AVERAGE:0.5:6:700',
'RRA:AVERAGE:0.5:24:775',
'RRA:AVERAGE:0.5:288:797',
'RRA:MAX:0.5:1:600',
'RRA:MAX:0.5:6:700',
'RRA:MAX:0.5:24:775',
'RRA:MAX:0.5:288:797',
'RRA:MIN:0.5:1:600',
'RRA:MIN:0.5:6:700',
'RRA:MIN:0.5:24:775',
'RRA:MIN:0.5:288:797'
);
$ERR=RRDs::error;
die "ERROR while creating $datafile: $ERR\n" if $ERR;
print "created $datafile\n";
}
# gather data
my ($candidates, $selected, $outlyers) = (0, 0, 0);
my ($sel_delay, $sel_offset, $sel_jitter) = ('U', 'U', 'U');
open NTPQ, '-|', 'ntpq -p' or die $!;
while (my $line = <NTPQ>) {
if ($line =~ /^\+/) {
$candidates++;
}
elsif ($line =~ /^-/) {
$outlyers++;
}
elsif ($line =~ /^\*/) {
$selected++;
if ($line =~ /\s(-?\d+\.\d{3})\s+(-?\d+\.\d{3})\s+(-?\d+\.\d{3})$/) {
($sel_delay, $sel_offset, $sel_jitter) = ($1, $2, $3);
}
}
}
close NTPQ or die $!;
my ($sys_jitter, $sys_offset, $clk_jitter, $clk_wander) = (0, 0, 0, 0);
open NTPQ, '-|', 'ntpq -c rv' or die $!;
while (my $line = <NTPQ>) {
if ($line =~ /sys_jitter=(-?\d+\.\d+)/) {
$sys_jitter = $1;
}
if ($line =~ /offset=(-?\d+\.\d+)/) {
$sys_offset = $1;
}
if ($line =~ /clk_jitter=(-?\d+\.\d+)/) {
$clk_jitter = $1;
}
if ($line =~ /clk_wander=(-?\d+\.\d+)/) {
$clk_wander = $1;
}
}
close NTPQ or die $!;
# update database
RRDs::update($datafile,
"N:$outlyers:$candidates:$selected:$sel_delay:$sel_offset:$sel_jitter:$sys_jitter:$sys_offset:$clk_jitter:$clk_wander"
);
die "ERROR while adding $datafile: $ERR\n" if $ERR;
# draw pictures (peer counts)
foreach ( [3600, "hour"], [86400, "day"], [604800, "week"], [31536000, "year"] ) {
my ($time, $scale) = @{$_};
next if $time < $MINTIME;
RRDs::graph($picbase . $scale . ".png",
"--start=-${time}",
'--lazy',
'--imgformat=PNG',
"--title=${hostname} NTP peer count (last $scale)",
"--width=$conf{GRAPH_WIDTH}",
"--height=$conf{GRAPH_HEIGHT}",
'--color=BACK#f3f3f3f3',
'--color=SHADEA#f3f3f3f3',
'--color=SHADEB#f3f3f3f3',
'--lower-limit=0',
"DEF:outlyers=${datafile}:outlyers:AVERAGE",
"DEF:candidates=${datafile}:candidates:AVERAGE",
"DEF:selected=${datafile}:selected:AVERAGE",
'AREA:selected#00D000:selected peer',
'STACK:candidates#D0D000:candidates',
'STACK:outlyers#D00000:outlyers',
'COMMENT:\n',
'COMMENT:\n',
);
$ERR=RRDs::error;
die "ERROR while drawing $datafile $time: $ERR\n" if $ERR;
}
# draw pictures (timings)
foreach ( [3600, "hour"], [86400, "day"], [604800, "week"], [31536000, "year"] ) {
my ($time, $scale) = @{$_};
next if $time < $MINTIME;
RRDs::graph($picbase2 . $scale . ".png",
"--start=-${time}",
'--lazy',
'--imgformat=PNG',
"--title=${hostname} NTP timings (last $scale)",
"--width=$conf{GRAPH_WIDTH}",
"--height=$conf{GRAPH_HEIGHT}",
'--color=BACK#f3f3f3f3',
'--color=SHADEA#f3f3f3f3',
'--color=SHADEB#f3f3f3f3',
"DEF:sys_offset_min=${datafile}:sys_offset:MIN",
"DEF:sys_offset_max=${datafile}:sys_offset:MAX",
"DEF:sel_delay=${datafile}:sel_delay:AVERAGE",
"DEF:sel_offset=${datafile}:sel_offset:AVERAGE",
"DEF:sel_jitter=${datafile}:sel_jitter:AVERAGE",
"DEF:sys_jitter=${datafile}:sys_jitter:AVERAGE",
"DEF:clk_jitter=${datafile}:clk_jitter:AVERAGE",
"DEF:clk_wander=${datafile}:clk_wander:AVERAGE",
"DEF:sys_offset=${datafile}:sys_offset:AVERAGE",
'CDEF:sys_offset_stack=sys_offset_max,sys_offset_min,-',
'HRULE:0#80808080',
'LINE:sys_offset_min',
'AREA:sys_offset_stack#F0808080::STACK',
# 'LINE1:sel_delay#F0F040:sel_delay [ms]',
'LINE1:sel_offset#00F0F0:sel_offset [ms]',
'LINE1:sel_jitter#F000F0:sel_jitter [ms]',
'LINE1:sys_jitter#0000F0:sys_jitter [ms]',
'LINE1:clk_jitter#000000:clk_jitter [ms]',
'LINE1:clk_wander#AAAAAA:clk_wander [ms]',
'LINE2:sys_offset#F00000:sys_offset [ms]',
'COMMENT:\n',
);
$ERR=RRDs::error;
die "ERROR while drawing $datafile $time: $ERR\n" if $ERR;
}