This commit is contained in:
Wolfgang Hottgenroth 2025-03-10 20:25:38 +01:00
parent e3b9e11533
commit 57c04685a1

View File

@ -10,6 +10,7 @@ import grp
import logging
import logging.handlers
import pyagentx
import datetime
BASE_OID_ENTERPRISE = '1.3.6.1.4.1'
@ -17,16 +18,16 @@ BASE_OID_HOTTIS = BASE_OID_ENTERPRISE + '.9676'
BASE_OID_HOTTIS_NTPSEC = BASE_OID_HOTTIS + '.123'
# just the prefix where the objects are below
LOCAL_PREFIX = '1'
PEERS_PREFIX = '2'
SYSINFO_PREFIX = '1'
SYSSTATS_PREFIX = '2'
PEERS_PREFIX = '3'
NUMBER_OF_PEERS_PREFIX = PEERS_PREFIX + '.1'
# this is for a table
# 2 is the prefix
# the first 1 is for the table in the mib
# the second 1 is for the entries in the table in the mib
TABLE_OF_PEERS_PREFIX = PEERS_PREFIX + '.3.1'
TABLE_OF_PEERS_PREFIX = PEERS_PREFIX + '.2.1'
def int_scale1k(x):
@ -38,107 +39,59 @@ def int_scale1M(x):
def pass_value(x):
return x
# sysinfo
# sysinfo = (
# ("peeradr", "system peer: ", NTP_ADP),
# ("peermode", "system peer mode: ", NTP_MODE),
# ("leap", "leap indicator: ", NTP_2BIT),
# ("stratum", "stratum: ", NTP_INT),
# ("precision", "log2 precision: ", NTP_INT),
# ("rootdelay", "root delay: ", NTP_FLOAT),
# ("rootdisp", "root dispersion: ", NTP_FLOAT),
# ("rootdist", "root distance ", NTP_FLOAT),
# ("refid", "reference ID: ", NTP_STR),
# ("reftime", "reference time: ", NTP_LFP),
# ("sys_jitter", "system jitter: ", NTP_FLOAT),
# ("clk_jitter", "clock jitter: ", NTP_FLOAT),
# ("clk_wander", "clock wander: ", NTP_FLOAT),
# ("authdelay", "symm. auth. delay:", NTP_FLOAT),
# )
def string_ntp_seconds(x):
h1, h2 = x.split('.')
seconds = int(h1, 16)
fraction = int(h2, 16)
ntp_epoch = datetime.datetime(1900, 1, 1)
timestamp = ntp_epoch + datetime.timedelta(seconds=seconds)
microseconds = fraction // 1000
timestamp = timestamp.replace(microsecond=microseconds)
return f"{timestamp} UTC"
LOCAL_SERVER_KEYS = [
SYSINFO_KEYS = [
['peeradr', pyagentx.TYPE_OCTETSTRING, pass_value],
['peermode', pyagentx.TYPE_INTEGER, pass_value],
['leap', pyagentx.TYPE_INTEGER, pass_value],
['stratum', pyagentx.TYPE_INTEGER, pass_value],
['precision', pyagentx.TYPE_INTEGER, pass_value],
['rootdelay', pyagentx.TYPE_INTEGER, int_scale1M],
['rootdisp', pyagentx.TYPE_INTEGER, int_scale1k],
['rootdisp', pyagentx.TYPE_INTEGER, int_scale1M],
['rootdist', pyagentx.TYPE_INTEGER, int_scale1M],
['refid', pyagentx.TYPE_OCTETSTRING, pass_value],
['reftime', pyagentx.TYPE_OCTETSTRING, pass_value],
['tc', pyagentx.TYPE_INTEGER, pass_value],
['peer', pyagentx.TYPE_INTEGER, pass_value],
['offset', pyagentx.TYPE_INTEGER, int_scale1M],
['frequency', pyagentx.TYPE_INTEGER, int_scale1k],
['reftime', pyagentx.TYPE_OCTETSTRING, string_ntp_seconds],
['sys_jitter', pyagentx.TYPE_INTEGER, int_scale1M],
['clk_jitter', pyagentx.TYPE_INTEGER, int_scale1M],
['clock', pyagentx.TYPE_OCTETSTRING, pass_value],
['processor', pyagentx.TYPE_OCTETSTRING, pass_value],
['system', pyagentx.TYPE_OCTETSTRING, pass_value],
['version', pyagentx.TYPE_OCTETSTRING, pass_value],
['clk_wander', pyagentx.TYPE_INTEGER, int_scale1M],
['tai', pyagentx.TYPE_INTEGER, pass_value],
['leapsec', pyagentx.TYPE_OCTETSTRING, pass_value],
['expire', pyagentx.TYPE_OCTETSTRING, pass_value],
['mintc', pyagentx.TYPE_INTEGER, pass_value]
]
# sysstats
# sysstats = (
# ("ss_uptime", "uptime: ", NTP_UPTIME),
# ("ss_numctlreq", "control requests: ", NTP_INT),
# )
# sysstats2 = (
# ("ss_reset", "sysstats reset: ", NTP_UPTIME),
# ("ss_received", "packets received: ", NTP_PACKETS),
# ("ss_thisver", "current version: ", NTP_PACKETS),
# ("ss_oldver", "older version: ", NTP_PACKETS),
# ("ss_ver1", "NTPv1 total: ", NTP_PACKETS),
# ("ss_ver1client","NTPv1 clients: ", NTP_PACKETS),
# ("ss_ver1zero", "NTPv1 mode0: ", NTP_PACKETS),
# ("ss_ver1symm", "NTPv1 symm act: ", NTP_PACKETS),
# ("ss_badformat", "bad length or format: ", NTP_PACKETS),
# ("ss_badauth", "authentication failed:", NTP_PACKETS),
# ("ss_declined", "declined: ", NTP_PACKETS),
# ("ss_restricted","restricted: ", NTP_PACKETS),
# ("ss_limited", "rate limited: ", NTP_PACKETS),
# ("ss_kodsent", "KoD responses: ", NTP_PACKETS),
# ("ss_processed", "processed for time: ", NTP_PACKETS),
# )
#
SYSSTATS_KEYS = [
['ss_uptime', pyagentx.TYPE_INTEGER, pass_value ],
['ss_numctlreq', pyagentx.TYPE_INTEGER, pass_value ],
['ss_reset', pyagentx.TYPE_INTEGER, pass_value ],
['ss_received', pyagentx.TYPE_COUNTER64, pass_value ],
['ss_badformat', pyagentx.TYPE_COUNTER64, pass_value ],
['ss_declined', pyagentx.TYPE_COUNTER64, pass_value ],
['ss_restricted', pyagentx.TYPE_COUNTER64, pass_value ],
['ss_limited', pyagentx.TYPE_COUNTER64, pass_value ],
['ss_kodsent', pyagentx.TYPE_COUNTER64, pass_value ],
['ss_processed', pyagentx.TYPE_COUNTER64, pass_value ]
]
PEER_KEYS = [
['srchost', pyagentx.TYPE_OCTETSTRING, pass_value],
['srcadr', pyagentx.TYPE_OCTETSTRING, pass_value],
['srcport', pyagentx.TYPE_INTEGER, pass_value],
['dstadr', pyagentx.TYPE_OCTETSTRING, pass_value],
['dstport', pyagentx.TYPE_INTEGER, pass_value],
['leap', pyagentx.TYPE_INTEGER, pass_value],
['hmode', pyagentx.TYPE_INTEGER, pass_value],
['refid', pyagentx.TYPE_OCTETSTRING, pass_value],
['stratum', pyagentx.TYPE_INTEGER, pass_value],
['hmode', pyagentx.TYPE_INTEGER, pass_value],
['ppoll', pyagentx.TYPE_INTEGER, pass_value],
['hpoll', pyagentx.TYPE_INTEGER, pass_value],
['precision', pyagentx.TYPE_INTEGER, pass_value],
['rootdelay', pyagentx.TYPE_INTEGER, int_scale1k],
['rootdisp', pyagentx.TYPE_INTEGER, int_scale1k],
['refid', pyagentx.TYPE_OCTETSTRING, pass_value],
['reftime', pyagentx.TYPE_OCTETSTRING, pass_value],
['rec', pyagentx.TYPE_OCTETSTRING, pass_value],
['xmt', pyagentx.TYPE_OCTETSTRING, pass_value],
['rec', pyagentx.TYPE_OCTETSTRING, string_ntp_seconds],
['reach', pyagentx.TYPE_INTEGER, pass_value],
['unreach', pyagentx.TYPE_INTEGER, pass_value],
['delay-s', pyagentx.TYPE_OCTETSTRING, pass_value],
['delay', pyagentx.TYPE_INTEGER, int_scale1k],
['offset', pyagentx.TYPE_INTEGER, int_scale1M],
['jitter', pyagentx.TYPE_INTEGER, int_scale1M],
['dispersion', pyagentx.TYPE_INTEGER, int_scale1k],
['keyid', pyagentx.TYPE_INTEGER, pass_value],
['filtdelay', pyagentx.TYPE_OCTETSTRING, pass_value],
['filtoffset', pyagentx.TYPE_OCTETSTRING, pass_value],
['pmode', pyagentx.TYPE_INTEGER, pass_value],
['filtdisp', pyagentx.TYPE_OCTETSTRING, pass_value],
['flash', pyagentx.TYPE_INTEGER, pass_value],
['headway', pyagentx.TYPE_INTEGER, pass_value],
['ntscookies', pyagentx.TYPE_INTEGER, pass_value]
]
@ -173,20 +126,23 @@ class NtpDataCollector(threading.Thread):
while not self.stop_event.is_set():
try:
logger.debug('Query ntp server')
tmp_data_store = {}
session = ntp.packet.ControlSession()
session.openhost(self.ntpserver)
ntpserver_vars = session.readvar(0)
logger.debug(f"{ntpserver_vars=}")
sysinfo_vars = session.readvar(0, [ x[0] for x in SYSINFO_KEYS ])
logger.debug(f"{sysinfo_vars=}")
tmp_data_store['sysinfo'] = dict(sysinfo_vars)
tmp_data_store = {}
tmp_data_store['local'] = dict(ntpserver_vars)
sysstats_vars = session.readvar(0, [ x[0] for x in SYSSTATS_KEYS ])
logger.debug(f"{sysstats_vars=}")
tmp_data_store['sysstats'] = dict(sysstats_vars)
peers = session.readstat()
tmp_data_store['peers'] = {}
for peer in peers:
peer_vars = session.readvar(peer.associd)
peer_vars = session.readvar(peer.associd, [ x[0] for x in PEER_KEYS ])
tmp_data_store['peers'][peer.associd] = dict(peer_vars)
logger.debug(f"{peer.associd=}, {peer_vars=}")
@ -210,17 +166,30 @@ class NtpsecDataUpdater(pyagentx.Updater):
if ds.data:
try:
logger.debug("Updating data store")
for index, data_spec in enumerate(LOCAL_SERVER_KEYS, start=1):
for index, data_spec in enumerate(SYSINFO_KEYS, start=1):
# logger.debug(f"local: {index=} {data_spec=}")
try:
oid_prefix = f"{LOCAL_PREFIX}.{index}"
oid_prefix = f"{SYSINFO_PREFIX}.{index}"
self._data[oid_prefix] = {
'name': oid_prefix,
'type': data_spec[1],
'value': data_spec[2](ds.data['local'][data_spec[0]])
'value': data_spec[2](ds.data['sysinfo'][data_spec[0]])
}
except KeyError as e:
logger.error(f"key {data_spec[0]} missing in local, skip it: {str(e)}")
for index, data_spec in enumerate(SYSSTATS_KEYS, start=1):
# logger.debug(f"local: {index=} {data_spec=}")
try:
oid_prefix = f"{SYSSTATS_PREFIX}.{index}"
self._data[oid_prefix] = {
'name': oid_prefix,
'type': data_spec[1],
'value': data_spec[2](ds.data['sysstats'][data_spec[0]])
}
except KeyError as e:
logger.error(f"key {data_spec[0]} missing in local, skip it: {str(e)}")
number_of_peers = len(ds.data['peers'])
# logger.debug(f"number of peers: {number_of_peers}")
number_of_peers_oid_prefix = f"{NUMBER_OF_PEERS_PREFIX}"