Merge branch 'more_variables_and_other_approach'
This commit is contained in:
commit
a327aed72e
14
snippets/ntp/test02.py
Normal file
14
snippets/ntp/test02.py
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import ntp.packet
|
||||||
|
import json
|
||||||
|
|
||||||
|
session = ntp.packet.ControlSession()
|
||||||
|
session.openhost('localhost')
|
||||||
|
|
||||||
|
peers = session.readstat()
|
||||||
|
|
||||||
|
|
||||||
|
for p in peers:
|
||||||
|
vars = session.readvar(p.associd, ['srchost', 'srcadr', 'refid', 'stratum', 'hmode', 'rec', 'reach', 'hpoll', 'ppoll', 'delay', 'offset', 'jitter'])
|
||||||
|
peerSelectStatus = " x.-+#*o"[(session.rstatus >>8) & 0x07]
|
||||||
|
print(f"{p.associd}, {peerSelectStatus}, {session.rstatus:04x}: {dict(vars)}")
|
||||||
|
|
@ -17,7 +17,8 @@ hottis MODULE-IDENTITY
|
|||||||
|
|
||||||
hottisNtpsec OBJECT IDENTIFIER ::= { hottis 123 }
|
hottisNtpsec OBJECT IDENTIFIER ::= { hottis 123 }
|
||||||
local OBJECT IDENTIFIER ::= { hottisNtpsec 1 }
|
local OBJECT IDENTIFIER ::= { hottisNtpsec 1 }
|
||||||
peers OBJECT IDENTIFIER ::= { hottisNtpsec 2 }
|
system OBJECT IDENTIFIER ::= { hottisNtpsec 2 }
|
||||||
|
peers OBJECT IDENTIFIER ::= { hottisNtpsec 3 }
|
||||||
|
|
||||||
|
|
||||||
ntpsecLocalLeap OBJECT-TYPE
|
ntpsecLocalLeap OBJECT-TYPE
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import ntp.packet
|
import ntp.packet
|
||||||
|
import ntp.ntpc
|
||||||
import threading
|
import threading
|
||||||
from contextlib import AbstractContextManager
|
from contextlib import AbstractContextManager
|
||||||
import time
|
import time
|
||||||
@ -10,6 +11,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,9 +19,9 @@ 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
|
||||||
@ -38,63 +40,52 @@ def int_scale1M(x):
|
|||||||
def pass_value(x):
|
def pass_value(x):
|
||||||
return x
|
return x
|
||||||
|
|
||||||
LOCAL_SERVER_KEYS = [
|
def string_ntp_seconds(x):
|
||||||
|
return ntp.ntpc.prettydate(x).split(' ')[1]
|
||||||
|
|
||||||
|
|
||||||
|
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 +120,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 +160,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}"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user