Skip to content

Commit 91065fb

Browse files
committed
feat(ntp-*): add --stratum parameter and modernize code
1 parent 128c217 commit 91065fb

File tree

15 files changed

+472
-146
lines changed

15 files changed

+472
-146
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
1111

1212
Monitoring Plugins:
1313

14+
* ntp-\*: add `--stratum` parameter and modernize code
1415
* sprint-boot-actuator-health: derived from [PR #940](https://github.com/Linuxfabrik/monitoring-plugins/pull/940), thanks to [Dominik Riva](https://github.com/slalomsk8er) - a monitoring plugin for the Spring Boot Actuator `/health` endpoint
1516
* virustotal-scan-url: analyses URLs to detect malware and other breaches using VirusTotal
1617

check-plugins/ntp-chronyd/README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,24 @@ The stratum of the NTP time source determines its quality. The stratum is equal
3434
## Help
3535

3636
```text
37-
usage: ntp-chronyd [-h] [-V] [-c CRIT] [--test TEST] [-w WARN]
37+
usage: ntp-chronyd [-h] [-V] [-c CRIT] [--stratum STRATUM] [--test TEST]
38+
[-w WARN]
3839
39-
This plugin checks the clock offset of chronyd in milliseconds compared to ntp
40-
servers.
40+
This plugin compares the chronyd clock offset in milliseconds to that of the
41+
NTP servers.
4142
4243
options:
4344
-h, --help show this help message and exit
4445
-V, --version show program's version number and exit
4546
-c, --critical CRIT Set the critical threshold for the ntp time offset, in
4647
ms. Default: 86400000ms
48+
--stratum STRATUM Warns if the determined stratum of the time server is
49+
greater than or equal to this value. Stratum 1
50+
indicates a computer with a locally attached reference
51+
clock. A computer that is synchronised to a stratum 1
52+
computer is at stratum 2. A computer that is
53+
synchronised to a stratum 2 computer is at stratum 3,
54+
and so on. Default: 6
4755
--test TEST For unit tests. Needs "path-to-stdout-file,path-to-
4856
stderr-file,expected-retc".
4957
-w, --warning WARN Set the warning threshold for the ntp time offset, in
@@ -54,7 +62,7 @@ options:
5462
## Usage Examples
5563

5664
```bash
57-
./ntp-chronyd --warning 500 --critical 10000
65+
./ntp-chronyd --warning=500 --critical=10000 --stratum=6
5866
```
5967

6068
Output:
@@ -91,7 +99,7 @@ MS Name/IP address Stratum Poll Reach LastRx Last sample
9199
## States
92100

93101
* WARN or CRIT if ntp offset is below or above a given threshold.
94-
* WARN if stratum is \>= 9.
102+
* WARN if stratum is \>= `--stratum`.
95103
* WARN if no NTP server is used.
96104
* WARN if no NTP server is found.
97105

check-plugins/ntp-chronyd/icingaweb2-module-director/ntp-chronyd.json

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
"--critical": {
66
"value": "$ntp_chronyd_critical$"
77
},
8+
"--stratum": {
9+
"value": "$ntp_chronyd_stratum$"
10+
},
811
"--warning": {
912
"value": "$ntp_chronyd_warning$"
1013
}
@@ -21,6 +24,11 @@
2124
"datafield_id": 2,
2225
"is_required": "n",
2326
"var_filter": null
27+
},
28+
{
29+
"datafield_id": 3,
30+
"is_required": "n",
31+
"var_filter": null
2432
}
2533
],
2634
"imports": [],
@@ -64,7 +72,7 @@
6472
"tpl-service-generic"
6573
],
6674
"max_check_attempts": 5,
67-
"notes": "This plugin checks the clock offset of chronyd in milliseconds compared to ntp servers.",
75+
"notes": "This plugin compares the chronyd clock offset in milliseconds to that of the NTP servers.",
6876
"notes_url": "https://github.com/Linuxfabrik/monitoring-plugins/tree/main/check-plugins/ntp-chronyd",
6977
"object_name": "tpl-service-ntp-chronyd",
7078
"object_type": "template",
@@ -76,6 +84,7 @@
7684
"vars": {
7785
"criticality": "C",
7886
"ntp_chronyd_critical": 86400000,
87+
"ntp_chronyd_stratum": 6,
7988
"ntp_chronyd_warning": 800
8089
},
8190
"volatile": null,
@@ -96,6 +105,17 @@
96105
"uuid": "47e8b431-eaae-4136-8a70-350ce0ca1804"
97106
},
98107
"2": {
108+
"varname": "ntp_chronyd_stratum",
109+
"caption": "NTP Chronyd: Stratum",
110+
"description": "Warns if the determined stratum of the time server is greater than or equal to this value. Stratum 1 indicates a computer with a locally attached reference clock. A computer that is synchronised to a stratum 1 computer is at stratum 2. A computer that is synchronised to a stratum 2 computer is at stratum 3, and so on.",
111+
"datatype": "Icinga\\Module\\Director\\DataType\\DataTypeString",
112+
"format": null,
113+
"settings": {
114+
"visibility": "visible"
115+
},
116+
"uuid": "61cac0f3-c41b-42f8-a11d-f255d74ece12"
117+
},
118+
"3": {
99119
"varname": "ntp_chronyd_warning",
100120
"caption": "NTP Chronyd: Warning",
101121
"description": "Set the warning threshold for the ntp time offset, in ms.",

check-plugins/ntp-chronyd/ntp-chronyd

Lines changed: 120 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,25 @@
1111
"""See the check's README for more details.
1212
"""
1313

14-
import argparse # pylint: disable=C0413
15-
import sys # pylint: disable=C0413
14+
import argparse
15+
import sys
1616

17-
import lib.args # pylint: disable=C0413
18-
import lib.base # pylint: disable=C0413
19-
import lib.human # pylint: disable=C0413
20-
import lib.shell # pylint: disable=C0413
21-
import lib.lftest # pylint: disable=C0413
22-
from lib.globals import (STATE_CRIT, STATE_OK, # pylint: disable=C0413
23-
STATE_UNKNOWN, STATE_WARN)
17+
import lib.args
18+
import lib.base
19+
import lib.human
20+
import lib.lftest
21+
import lib.shell
22+
from lib.globals import (STATE_OK, STATE_UNKNOWN, STATE_WARN)
2423

2524
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
26-
__version__ = '2025021501'
25+
__version__ = '2025091501'
2726

28-
DESCRIPTION = '''This plugin checks the clock offset of chronyd in milliseconds
29-
compared to ntp servers.'''
27+
DESCRIPTION = '''This plugin compares the chronyd clock offset in milliseconds to that of
28+
the NTP servers.'''
3029

31-
DEFAULT_WARN = 800 # ms offset
3230
DEFAULT_CRIT = 86400000 # 24h in ms offset
31+
DEFAULT_STRATUM = 6
32+
DEFAULT_WARN = 800 # ms offset
3333

3434

3535
def parse_args():
@@ -40,17 +40,27 @@ def parse_args():
4040
parser.add_argument(
4141
'-V', '--version',
4242
action='version',
43-
version='{0}: v{1} by {2}'.format('%(prog)s', __version__, __author__)
43+
version=f'%(prog)s: v{__version__} by {__author__}'
4444
)
4545

4646
parser.add_argument(
4747
'-c', '--critical',
48-
help='Set the critical threshold for the ntp time offset, in ms. Default: %(default)sms',
48+
help='Set the critical threshold for the ntp time offset, in ms. '
49+
'Default: %(default)sms',
4950
dest='CRIT',
5051
type=int,
5152
default=DEFAULT_CRIT,
5253
)
5354

55+
parser.add_argument(
56+
'--stratum',
57+
help=lib.args.help('--stratum') + ' '
58+
+ 'Default: %(default)s',
59+
dest='STRATUM',
60+
type=int,
61+
default=DEFAULT_STRATUM,
62+
)
63+
5464
parser.add_argument(
5565
'--test',
5666
help='For unit tests. Needs "path-to-stdout-file,path-to-stderr-file,expected-retc".',
@@ -60,7 +70,8 @@ def parse_args():
6070

6171
parser.add_argument(
6272
'-w', '--warning',
63-
help='Set the warning threshold for the ntp time offset, in ms. Default: %(default)sms',
73+
help='Set the warning threshold for the ntp time offset, in ms. '
74+
'Default: %(default)sms',
6475
dest='WARN',
6576
type=int,
6677
default=DEFAULT_WARN,
@@ -81,18 +92,27 @@ def main():
8192

8293
if args.TEST is None:
8394
cmd = 'chronyc tracking'
84-
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(cmd)) # pylint: disable=W0612
85-
if stderr:
86-
lib.base.cu(stderr)
95+
stdout, stderr, _ = lib.base.coe(lib.shell.shell_exec(cmd))
8796
else:
8897
# do not call the command, put in test data
89-
stdout, stderr, retc = lib.lftest.test(args.TEST)
98+
stdout, stderr, _ = lib.lftest.test(args.TEST)
99+
if stderr:
100+
lib.base.cu(stderr)
90101

91102
# init some vars
92103
msg = ''
93104
state = STATE_OK
94105
perfdata = ''
106+
107+
frequency = 0
108+
last_offset = 0
109+
leap_status = ''
95110
peer_used = False
111+
residual_freq = 0
112+
rms_offset = 0
113+
root_delay = 0
114+
root_dispersion = 0
115+
skew = 0
96116

97117
# analyze data
98118
for line in stdout.splitlines():
@@ -130,31 +150,93 @@ def main():
130150
msg = 'NTP server not reachable. No NTP server is used.'
131151
success, result = lib.shell.shell_exec('chronyc sources')
132152
if success:
133-
msg += '\n\n{}'.format(result[0])
153+
msg += '\n\n' + result[0]
134154
lib.base.oao(msg, STATE_WARN)
135155

136156
# build the message
137157
offset_state = lib.base.get_state(abs(last_offset), args.WARN, args.CRIT)
138158
state = lib.base.get_worst(state, offset_state)
139-
msg += 'NTP offset is {}ms{}, '.format(
140-
round(last_offset, 3),
141-
lib.base.state2str(offset_state, prefix=' '),
142-
)
143-
msg += 'Stratum is {}'.format(stratum)
144-
if stratum >= 9:
159+
msg += f'NTP offset is {round(last_offset, 3)}ms' \
160+
f'{lib.base.state2str(offset_state, prefix=' ')}, ' \
161+
f'Stratum is {stratum}'
162+
if stratum >= args.STRATUM:
145163
stratum_state = STATE_WARN
146164
state = lib.base.get_worst(state, stratum_state)
147-
msg += ' is >= 9{}'.format(lib.base.state2str(stratum_state, prefix=' '))
148-
msg += ', Leap status is {}'.format(leap_status)
149-
150-
perfdata += lib.base.get_perfdata('frequency', frequency, None, None, None, None, None)
151-
perfdata += lib.base.get_perfdata('last_offset', last_offset, 'ms', args.WARN, args.CRIT, None, None)
152-
perfdata += lib.base.get_perfdata('residual_freq', residual_freq, None, None, None, None, None)
153-
perfdata += lib.base.get_perfdata('rms_offset', rms_offset, 'ms', None, None, None, None)
154-
perfdata += lib.base.get_perfdata('root_delay', root_delay, 'ms', None, None, None, None)
155-
perfdata += lib.base.get_perfdata('root_dispersion', root_dispersion, 'ms', None, None, None, None)
156-
perfdata += lib.base.get_perfdata('skew', skew, None, None, None, None, None)
157-
perfdata += lib.base.get_perfdata('stratum', stratum, None, 9, None, 0, None)
165+
msg += f' (is >= {args.STRATUM} {lib.base.state2str(stratum_state)})'
166+
msg += f', Leap status is {leap_status}'
167+
168+
perfdata += lib.base.get_perfdata(
169+
'frequency',
170+
frequency,
171+
uom=None,
172+
warn=None,
173+
crit=None,
174+
_min=None,
175+
_max=None,
176+
)
177+
perfdata += lib.base.get_perfdata(
178+
'last_offset',
179+
last_offset,
180+
uom='ms',
181+
warn=args.WARN,
182+
crit=args.CRIT,
183+
_min=None,
184+
_max=None,
185+
)
186+
perfdata += lib.base.get_perfdata(
187+
'residual_freq',
188+
residual_freq,
189+
uom=None,
190+
warn=None,
191+
crit=None,
192+
_min=None,
193+
_max=None,
194+
)
195+
perfdata += lib.base.get_perfdata(
196+
'rms_offset',
197+
rms_offset,
198+
uom='ms',
199+
warn=None,
200+
crit=None,
201+
_min=None,
202+
_max=None,
203+
)
204+
perfdata += lib.base.get_perfdata(
205+
'root_delay',
206+
root_delay,
207+
uom='ms',
208+
warn=None,
209+
crit=None,
210+
_min=None,
211+
_max=None,
212+
)
213+
perfdata += lib.base.get_perfdata(
214+
'root_dispersion',
215+
root_dispersion,
216+
uom='ms',
217+
warn=None,
218+
crit=None,
219+
_min=None,
220+
_max=None,
221+
)
222+
perfdata += lib.base.get_perfdata(
223+
'skew',
224+
skew,
225+
uom=None,
226+
warn=None,
227+
crit=None,
228+
_min=None,
229+
_max=None,
230+
)
231+
perfdata += lib.base.get_perfdata(
232+
'stratum',
233+
stratum,
234+
uom=None,
235+
warn=args.STRATUM,
236+
crit=None,
237+
_min=0,
238+
_max=16,
239+
)
158240

159241
# over and out
160242
lib.base.oao(msg + '\n\n' + stdout, state, perfdata)

check-plugins/ntp-ntpd/README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ The stratum of the NTP time source determines its quality. The stratum is equal
3333
## Help
3434

3535
```text
36-
usage: ntp-ntpd [-h] [-V] [-c CRIT] [--test TEST] [-w WARN]
36+
usage: ntp-ntpd [-h] [-V] [-c CRIT] [--stratum STRATUM] [--test TEST]
37+
[-w WARN]
3738
3839
This plugin checks the clock offset of ntpd in milliseconds compared to ntp
3940
servers.
@@ -43,6 +44,13 @@ options:
4344
-V, --version show program's version number and exit
4445
-c, --critical CRIT Set the critical threshold for the ntp time offset, in
4546
ms. Default: 86400000ms
47+
--stratum STRATUM Warns if the determined stratum of the time server is
48+
greater than or equal to this value. Stratum 1
49+
indicates a computer with a locally attached reference
50+
clock. A computer that is synchronised to a stratum 1
51+
computer is at stratum 2. A computer that is
52+
synchronised to a stratum 2 computer is at stratum 3,
53+
and so on. Default: 6
4654
--test TEST For unit tests. Needs "path-to-stdout-file,path-to-
4755
stderr-file,expected-retc".
4856
-w, --warning WARN Set the warning threshold for the ntp time offset, in
@@ -53,7 +61,7 @@ options:
5361
## Usage Examples
5462

5563
```bash
56-
./ntp-ntpd --warning 500 --critical 10000
64+
./ntp-ntpd --warning=500 --critical=10000 --stratum=6
5765
```
5866

5967
Output:
@@ -73,7 +81,7 @@ NTP offset is -3.005ms, Stratum is 2
7381
## States
7482

7583
* WARN or CRIT if ntp offset is below or above a given threshold.
76-
* WARN if stratum is \>= 9.
84+
* WARN if stratum is \>= `--stratum`.
7785
* WARN if no NTP server is used.
7886
* WARN if no NTP server is found.
7987
* WARN if only LOCAL clock is used.

0 commit comments

Comments
 (0)