Compare commits

..

2 Commits

Author SHA1 Message Date
Wolfgang Hottgenroth
57c04685a1 changes 2025-03-10 20:25:38 +01:00
Wolfgang Hottgenroth
e3b9e11533 peer variables 2025-03-10 14:48:00 +01:00
2 changed files with 67 additions and 61 deletions

View File

@ -6,16 +6,9 @@ session.openhost('localhost')
peers = session.readstat() peers = session.readstat()
k = []
l = session.readvar(0)
print(f"{l=}")
k.append(list(dict(l).keys()))
for p in peers: for p in peers:
l = session.readvar(p.associd) vars = session.readvar(p.associd, ['srchost', 'srcadr', 'refid', 'stratum', 'hmode', 'rec', 'reach', 'hpoll', 'ppoll', 'delay', 'offset', 'jitter'])
print(f"{p.associd}: {dict(l)}") peerSelectStatus = " x.-+#*o"[(session.rstatus >>8) & 0x07]
k.append(list(dict(l).keys())) print(f"{p.associd}, {peerSelectStatus}, {session.rstatus:04x}: {dict(vars)}")
print(json.dumps(k, indent=4))

View File

@ -10,6 +10,7 @@ import grp
import logging import logging
import logging.handlers import logging.handlers
import pyagentx import pyagentx
import datetime
BASE_OID_ENTERPRISE = '1.3.6.1.4.1' 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' BASE_OID_HOTTIS_NTPSEC = BASE_OID_HOTTIS + '.123'
# just the prefix where the objects are below # just the prefix where the objects are below
LOCAL_PREFIX = '1' SYSINFO_PREFIX = '1'
SYSSTATS_PREFIX = '2'
PEERS_PREFIX = '2' PEERS_PREFIX = '3'
NUMBER_OF_PEERS_PREFIX = PEERS_PREFIX + '.1' NUMBER_OF_PEERS_PREFIX = PEERS_PREFIX + '.1'
# this is for a table # this is for a table
# 2 is the prefix # 2 is the prefix
# the first 1 is for the table in the mib # the first 1 is for the table in the mib
# the second 1 is for the entries in 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): def int_scale1k(x):
@ -38,63 +39,59 @@ def int_scale1M(x):
def pass_value(x): def pass_value(x):
return x return x
LOCAL_SERVER_KEYS = [ 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"
SYSINFO_KEYS = [
['peeradr', pyagentx.TYPE_OCTETSTRING, pass_value],
['peermode', pyagentx.TYPE_INTEGER, pass_value],
['leap', pyagentx.TYPE_INTEGER, pass_value], ['leap', pyagentx.TYPE_INTEGER, pass_value],
['stratum', pyagentx.TYPE_INTEGER, pass_value], ['stratum', pyagentx.TYPE_INTEGER, pass_value],
['precision', pyagentx.TYPE_INTEGER, pass_value], ['precision', pyagentx.TYPE_INTEGER, pass_value],
['rootdelay', pyagentx.TYPE_INTEGER, int_scale1M], ['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], ['refid', pyagentx.TYPE_OCTETSTRING, pass_value],
['reftime', pyagentx.TYPE_OCTETSTRING, pass_value], ['reftime', pyagentx.TYPE_OCTETSTRING, string_ntp_seconds],
['tc', pyagentx.TYPE_INTEGER, pass_value],
['peer', pyagentx.TYPE_INTEGER, pass_value],
['offset', pyagentx.TYPE_INTEGER, int_scale1M],
['frequency', pyagentx.TYPE_INTEGER, int_scale1k],
['sys_jitter', pyagentx.TYPE_INTEGER, int_scale1M], ['sys_jitter', pyagentx.TYPE_INTEGER, int_scale1M],
['clk_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], ['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], SYSSTATS_KEYS = [
['mintc', pyagentx.TYPE_INTEGER, pass_value] ['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 = [ PEER_KEYS = [
['srchost', pyagentx.TYPE_OCTETSTRING, pass_value],
['srcadr', pyagentx.TYPE_OCTETSTRING, pass_value], ['srcadr', pyagentx.TYPE_OCTETSTRING, pass_value],
['srcport', pyagentx.TYPE_INTEGER, pass_value], ['refid', pyagentx.TYPE_OCTETSTRING, 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],
['stratum', pyagentx.TYPE_INTEGER, pass_value], ['stratum', pyagentx.TYPE_INTEGER, pass_value],
['hmode', pyagentx.TYPE_INTEGER, pass_value],
['ppoll', pyagentx.TYPE_INTEGER, pass_value], ['ppoll', pyagentx.TYPE_INTEGER, pass_value],
['hpoll', pyagentx.TYPE_INTEGER, pass_value], ['hpoll', pyagentx.TYPE_INTEGER, pass_value],
['precision', pyagentx.TYPE_INTEGER, pass_value], ['rec', pyagentx.TYPE_OCTETSTRING, string_ntp_seconds],
['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],
['reach', pyagentx.TYPE_INTEGER, pass_value], ['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], ['delay', pyagentx.TYPE_INTEGER, int_scale1k],
['offset', pyagentx.TYPE_INTEGER, int_scale1M], ['offset', pyagentx.TYPE_INTEGER, int_scale1M],
['jitter', 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]
] ]
@ -129,20 +126,23 @@ class NtpDataCollector(threading.Thread):
while not self.stop_event.is_set(): while not self.stop_event.is_set():
try: try:
logger.debug('Query ntp server') logger.debug('Query ntp server')
tmp_data_store = {}
session = ntp.packet.ControlSession() session = ntp.packet.ControlSession()
session.openhost(self.ntpserver) session.openhost(self.ntpserver)
ntpserver_vars = session.readvar(0) sysinfo_vars = session.readvar(0, [ x[0] for x in SYSINFO_KEYS ])
logger.debug(f"{ntpserver_vars=}") logger.debug(f"{sysinfo_vars=}")
tmp_data_store['sysinfo'] = dict(sysinfo_vars)
tmp_data_store = {} sysstats_vars = session.readvar(0, [ x[0] for x in SYSSTATS_KEYS ])
tmp_data_store['local'] = dict(ntpserver_vars) logger.debug(f"{sysstats_vars=}")
tmp_data_store['sysstats'] = dict(sysstats_vars)
peers = session.readstat() peers = session.readstat()
tmp_data_store['peers'] = {} tmp_data_store['peers'] = {}
for peer in 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) tmp_data_store['peers'][peer.associd] = dict(peer_vars)
logger.debug(f"{peer.associd=}, {peer_vars=}") logger.debug(f"{peer.associd=}, {peer_vars=}")
@ -166,17 +166,30 @@ class NtpsecDataUpdater(pyagentx.Updater):
if ds.data: if ds.data:
try: try:
logger.debug("Updating data store") 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=}") # logger.debug(f"local: {index=} {data_spec=}")
try: try:
oid_prefix = f"{LOCAL_PREFIX}.{index}" oid_prefix = f"{SYSINFO_PREFIX}.{index}"
self._data[oid_prefix] = { self._data[oid_prefix] = {
'name': oid_prefix, 'name': oid_prefix,
'type': data_spec[1], '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: except KeyError as e:
logger.error(f"key {data_spec[0]} missing in local, skip it: {str(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']) number_of_peers = len(ds.data['peers'])
# logger.debug(f"number of peers: {number_of_peers}") # logger.debug(f"number of peers: {number_of_peers}")
number_of_peers_oid_prefix = f"{NUMBER_OF_PEERS_PREFIX}" number_of_peers_oid_prefix = f"{NUMBER_OF_PEERS_PREFIX}"