Skip to content

Commit

Permalink
ntp-ntpd: Fixed unpacking of ntpq -p values (fix #758)
Browse files Browse the repository at this point in the history
  • Loading branch information
markuslf committed May 3, 2024
1 parent 675f4ba commit b1e541f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ Monitoring Plugins:
* about-me: Throws exception for openvas ([#749](https://github.com/Linuxfabrik/monitoring-plugins/issues/749))
* infomaniak-events: Fix `UnboundLocalError: local variable 'keys' referenced before assignment`
* nextcloud-stats: KeyError: apps ([#731](https://github.com/Linuxfabrik/monitoring-plugins/issues/731))
* ntp-ntpd: Fixed unpacking of ntpq -p values ([PR #758](https://github.com/Linuxfabrik/monitoring-plugins/pull/758), thanks to [Leo Pempera](https://github.com/leo-pempera))
* ntp-w32tm: Fix `UnboundLocalError: local variable 'clock_rate' referenced before assignment`


Expand Down
10 changes: 7 additions & 3 deletions check-plugins/ntp-ntpd/ntp-ntpd
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ from lib.globals import (STATE_CRIT, STATE_OK, # pylint: disable=C0413
STATE_UNKNOWN, STATE_WARN)

__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2023112901'
__version__ = '2024050301'

DESCRIPTION = '''This plugin checks the clock offset of ntpd in milliseconds
compared to ntp servers.'''
Expand Down Expand Up @@ -102,9 +102,9 @@ def main():
peer_used = True
try:
# line of peer in the form [tally]remote refid st t when pool reach delay offset jitter
split_line = line[1:].split()
split_line = line[1:].split() # remove '*' and split

# Get numeric values via negativ index as there might be multiple whitespaces in remote
# get numeric values via negative index as there might be multiple whitespaces in remote
peer = split_line[0]
stratum = int(split_line[-8])
delay = split_line[-3]
Expand All @@ -113,6 +113,10 @@ def main():
except:
pass

try:
offset
except:
lib.base.oao('No NTP server used.', STATE_WARN)
if not peer_used:
lib.base.oao('No NTP server used.', STATE_WARN)
if peer == '':
Expand Down
11 changes: 10 additions & 1 deletion check-plugins/ntp-ntpd/unit-test/run
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class TestCheck(unittest.TestCase):

def test_if_check_runs_EXAMPLE03(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --test=stdout/EXAMPLE03,,0'))
self.assertIn('No NTP server found.', stdout)
self.assertIn('No NTP server used.', stdout)
self.assertEqual(stderr, '')
self.assertEqual(retc, STATE_WARN)

Expand All @@ -55,6 +55,15 @@ class TestCheck(unittest.TestCase):
self.assertEqual(stderr, '')
self.assertEqual(retc, STATE_WARN)

def test_if_check_runs_EXAMPLE05(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --test=stdout/EXAMPLE05,,0'))
self.assertIn('NTP offset is -0.182ms, Stratum is 2', stdout)
self.assertIn('remote refid st t when poll reach delay offset jitter', stdout)
self.assertIn('==============================================================================', stdout)
self.assertIn('*185.232.69.65 ( 79.133.44.146 2 u 232 256 377 0.398 -0.182 0.170', stdout)
self.assertEqual(stderr, '')
self.assertEqual(retc, STATE_OK)


if __name__ == '__main__':
unittest.main()
12 changes: 12 additions & 0 deletions check-plugins/ntp-ntpd/unit-test/stdout/EXAMPLE05
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
remote refid st t when poll reach delay offset jitter
==============================================================================
0.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000
1.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000
2.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000
3.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000
ntp.ubuntu.com .POOL. 16 p - 64 0 0.000 0.000 0.000
+mail.gunnarhofm 192.53.103.103 2 u 110 256 377 2.793 -0.228 0.157
-gps.s-buettgen. 131.188.3.222 2 u 164 256 377 3.311 0.129 0.534
+ntp1.kashra-ser 192.168.100.15 2 u 174 256 377 13.709 -0.834 0.278
-47.ip-51-75-67. 17.253.14.251 2 u 156 256 217 4.930 -0.049 0.111
*185.232.69.65 ( 79.133.44.146 2 u 232 256 377 0.398 -0.182 0.170

0 comments on commit b1e541f

Please sign in to comment.