]> git.openstreetmap.org Git - chef.git/blob - cookbooks/munin/files/default/plugins/chrony
Switch to using chrony for time synchronisation
[chef.git] / cookbooks / munin / files / default / plugins / chrony
1 #!/bin/sh
2
3 : <<=cut
4
5 =head1 NAME
6
7 parse Chrony Tracking output for timeserver status information
8
9 =head1 APPLICABLE SYSTEMS
10
11 Any system with a local chronyd service.
12
13 =head1 CONFIGURATION
14
15 No configuration.
16
17 =head1 MAGIC MARKERS
18
19   #%# family=auto
20   #%# capabilities=autoconf
21
22 =head1 VERSION
23
24 Revision 0.1 2008/08/23 13:06:00 joti
25
26   First version only chronyc tracking, autodetection included.
27
28 Revision 0.2 2008/10/11 16:09:00 joti
29
30   Added scaling of other values to match with frequency, added more description to fields
31
32 Revision 0.3 2014/02/16 zjttoefs
33
34   reduce forking by using awk
35   do not limit output precision
36   add stratum monitoring
37   detect slow/fast time or freqency and adjust sign of value accordingly
38   remove commented out code
39
40 Revision 0.4 2016/11/10 Lars Kruse
41
42   rewrite field handling
43   use "which" for "chronyc" location
44   switch from "bash" to "sh"
45   fix exit code of failing "autoconf"
46
47 =head1 AUTHOR
48
49   joti
50   zjttoefs
51   Lars Kruse <devel@sumpfralle.de>
52
53 =cut
54
55 CHRONYC="$(which chronyc | head -1)"
56
57 # Frequency has extremely higher values than other. Therefore they are fitted by scaling via suitable factors.
58 # field definitions:
59 #   - munin fieldname
60 #   - factor for graph visualization (all values are supposed to reach a similar dimension)
61 #   - regular expression of the chrony output line (may not contain whitespace, case insensitive)
62 #   - label (may include "%d" for including the factor; may contain whitespace)
63 fields="stratum         1       ^Stratum                Stratum
64         systime         1000    ^System.time            System Time (x%d)
65         frequency       1       ^Frequency              Frequency (ppm)
66         residualfreq    100     ^Residual.freq          Residual Freq (ppm, x%d)
67         skew            100     ^Skew                   Skew (ppm, x%d)
68         rootdelay       1000    ^Root.delay             Root delay (seconds, x%d)
69         rootdispersion  1000    ^Root.dispersion        Root dispersion (seconds, x%d)"
70
71 # chrony example output (v2.4.1):
72 #   Reference ID    : 131.188.3.221 (ntp1.rrze.uni-erlangen.de)
73 #   Stratum         : 2
74 #   Ref time (UTC)  : Thu Nov 10 22:39:50 2016
75 #   System time     : 0.000503798 seconds slow of NTP time
76 #   Last offset     : +0.000254355 seconds
77 #   RMS offset      : 0.002186779 seconds
78 #   Frequency       : 17.716 ppm slow
79 #   Residual freq   : +0.066 ppm
80 #   Skew            : 4.035 ppm
81 #   Root delay      : 0.042980 seconds
82 #   Root dispersion : 0.005391 seconds
83 #   Update interval : 258.4 seconds
84 #   Leap status     : Normal
85
86
87 if [ "$1" = "autoconf" ]; then
88         if [ -n "$CHRONYC" ] && [ -x "$CHRONYC" ]; then
89                 echo yes
90         else
91                 echo "no (missing 'chronyc' executable)"
92         fi
93         exit 0
94 fi
95
96 if [ "$1" = "config" ]; then
97         echo 'graph_title Chrony Tracking Stats'
98         echo 'graph_args --base 1000 -l 0'
99         echo 'graph_vlabel (seconds,ppm)'
100         echo 'graph_category time'
101         echo "$fields" | while read fieldname factor regex label; do
102                 # insert the factor, if "%d" is part of the label
103                 printf "${fieldname}.label $label\n" "$factor"
104                 echo "${fieldname}.type GAUGE"
105         done
106         exit 0
107 fi
108
109 chrony_status="$("$CHRONYC" tracking)"
110 echo "$fields" | while read fieldname factor regex label; do
111         status_line="$(echo "$chrony_status" | grep -i -- "$regex " | cut -d ":" -f 2-)"
112         if [ -z "$status_line" ]; then
113                 value="U"
114         else
115                 # the keyword "slow" indicates negative values
116                 value="$(echo "$status_line" | awk '{ /slow/ ? SIGN=-1 : SIGN=1; print $1 * SIGN * '"$factor"' }')"
117         fi
118         echo "${fieldname}.value $value"
119 done