Collection of CheckMK checks (see https://checkmk.com/). All checks and plugins are provided as is. Absolutely no warranty. Send any comments to thl-cmk[at]outlook[dot]com

Skip to content
Snippets Groups Projects
Commit 54569a17 authored by thl-cmk's avatar thl-cmk :flag_na:
Browse files

update project

parent e8c57b51
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -43,6 +43,9 @@ def check_ntp_arguments(params):
if 'delay_levels' in params:
args.append(f'--delay {params["dispersion_levels"][0]},{params["dispersion_levels"][1]}')
if 'state_no_response' in params:
args.append(f'--state_no_response {params["state_no_response"]}')
return args
......
......@@ -11,15 +11,19 @@
#
# Active check to monitor NTP servers.
#
#
# 2022-10-13: added exception handling for ntp request
# 2022-11-14: made state on no response configurable
#
from typing import Optional, Sequence, Tuple
from ipaddress import IPv4Address
import sys
import argparse
import socket
from time import ctime, gmtime, strftime
import ntplib
no_ntplib=False
try:
from ntplib import NTPClient, NTPStats
......@@ -156,6 +160,9 @@ def parse_arguments(argv: Sequence[str]) -> argparse.Namespace:
parser.add_argument(
'--state_not_synchronized', type=int, default=2, choices=[0, 1, 2, 3],
help='Monitoring state if not synchronized.')
parser.add_argument(
'--state_no_response', type=int, default=2, choices=[0, 1, 2, 3],
help='Monitoring state if response (timeout) received.')
parser.add_argument(
'--stratum', type=_warn_crit, default=(10, 15),
help='WARN,CRIT levels for stratum. Use values > 16 to disable.')
......@@ -175,14 +182,18 @@ def parse_arguments(argv: Sequence[str]) -> argparse.Namespace:
return args
def get_ntp_time(server: str, port: int, timeout: int, version: int): # -> Optional[NTPStats] # is not available if ntplib is not installed
def get_ntp_time(server: str, port: int, timeout: int, version: int, state_no_response: int): # -> Optional[NTPStats] # is not available if ntplib is not installed
c = NTPClient()
response = c.request(
host=server,
port=port,
timeout=timeout,
version=version
)
try:
response = c.request(
host=server,
port=port,
timeout=timeout,
version=version
)
except (ntplib.NTPException, socket.gaierror) as e:
sys.stdout.write(f'{e}\n')
sys.exit(state_no_response)
return response
......@@ -193,10 +204,13 @@ def main(args=None):
args = parse_arguments(args)
if no_ntplib:
sys.stdout.write(f'To use this check plugin you need to install the python ntplib in your CMK python environment.')
sys.stdout.write(
f'To use this check plugin you need to install the python ntplib in your CMK python environment.'
)
sys.exit(3)
ntp_time = get_ntp_time(args.server, args.port, args.timeout, args.version)
ntp_time = get_ntp_time(args.server, args.port, args.timeout, args.version, args.state_no_response)
server_time = ctime(ntp_time.tx_time)
stratum = int(ntp_time.stratum)
......
......@@ -60,15 +60,6 @@ def _valuespec_active_checks_ntp():
minvalue=1,
maxvalue=65535,
)),
('timeout',
Integer(
title=_('Request timeout'),
help=_('Timeoute for the request in seconds. Min: 1s, Max: 20, Default is 2 seconds.'),
# size=3,
default_value=2,
minvalue=1,
maxvalue=20,
)),
('version',
Integer(
title=_('NTP version'),
......@@ -78,12 +69,27 @@ def _valuespec_active_checks_ntp():
minvalue=1,
maxvalue=4,
)),
('timeout',
Integer(
title=_('Request timeout'),
help=_('Timeoute for the request in seconds. Min: 1s, Max: 20, Default is 2 seconds.'),
# size=3,
default_value=2,
minvalue=1,
maxvalue=20,
)),
('state_not_synchronized',
MonitoringState(
title=_('Monitoring state if server is not synchronized'),
help=_('Monitoring state if server is not synchronized. Default is warning.'),
default_value=2,
)),
('state_no_response',
MonitoringState(
default_value=2,
title=_('Monitoring state if server on timeout (no response).'),
help=_('Monitoring state if the server doesnt respond. Default is "WARN"')
)),
('stratum_levels',
Tuple(
title=_('max. stratum'),
......@@ -164,18 +170,6 @@ def _valuespec_active_checks_ntp():
],
)),
],
optional_keys=[
'description',
'server',
'port',
'timeout',
'version',
'state_not_synchronized',
'offset_levels',
'stratum_levels',
'dispersion_levels',
'delay_levels',
],
),
)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment