[Git][NTPsec/ntpsec][master] 3 commits: Split off MIBControll base class.
Ian Bruene
gitlab at mg.gitlab.com
Mon Feb 19 20:49:55 UTC 2018
Ian Bruene pushed to branch master at NTPsec / ntpsec
Commits:
be60f941 by Ian Bruene at 2018-02-19T14:27:33-06:00
Split off MIBControll base class.
- - - - -
c8af43be by Ian Bruene at 2018-02-19T14:36:44-06:00
Added tree init arguments to MIBControl.
- - - - -
f3c7978b by Ian Bruene at 2018-02-19T14:40:31-06:00
Moved oidTree assignment to MIBControl.
- - - - -
2 changed files:
- ntpclients/ntpsnmpd.py
- pylib/agentx.py
Changes:
=====================================
ntpclients/ntpsnmpd.py
=====================================
--- a/ntpclients/ntpsnmpd.py
+++ b/ntpclients/ntpsnmpd.py
@@ -43,12 +43,12 @@ snmpSysUptime = (1, 3, 6, 1, 2, 1, 1, 3, 0)
DEFHOST = "localhost"
-class DataSource: # This will be broken up in future to be less NTP-specific
+class DataSource(ntp.agentx.MIBControl):
def __init__(self, hostname=DEFHOST):
# This is defined as a dict tree because it is simpler, and avoids
# certain edge cases
# OIDs are relative from ntp root
- self.oidTree = {
+ oidTree = {
# ntpEntNotifications
0: {"subids":
# ntpEntNotifModeChange
@@ -226,6 +226,7 @@ class DataSource: # This will be broken up in future to be less NTP-specific
}}
}
}
+ ntp.agentx.MIBControl.__init__(self, oidTree, mibRoot=ntpRootOID)
self.session = ntp.packet.ControlSession()
self.hostname = hostname if hostname else DEFHOST
self.session.openhost(self.hostname)
@@ -235,11 +236,6 @@ class DataSource: # This will be broken up in future to be less NTP-specific
self.cache = ntp.util.Cache(1)
self.oldValues = {} # Used by notifications to detect changes
self.lastHeartbeat = 0 # Timestamp used for heartbeat notifications
- # The undo system is only for the last operation
- self.inSetP = False # Are we currently in the set procedure?
- self.setVarbinds = [] # Varbind of the current set operation
- self.setHandlers = [] # Handlers for commit/undo/cleanup of set
- self.setUndoData = [] # Previous values for undoing
self.heartbeatInterval = 0 # should save to disk
self.sentNotifications = 0
# Notify bits, they control whether the daemon sends notifications.
@@ -253,91 +249,6 @@ class DataSource: # This will be broken up in future to be less NTP-specific
self.notifyLeapSecondAnnounced = False # 7
self.notifyHeartbeat = False # 8
- def rootOID(self):
- return ntpRootOID
-
- def rangeSubid(self):
- return 0
-
- def upperBound(self):
- return None
-
- def context(self):
- return None
-
- def getOID_core(self, nextP, searchoid, returnGenerator=False):
- gen = ax.walkMIBTree(self.oidTree, ntpRootOID)
- while True:
- try:
- oid, reader, writer = gen.next()
- if nextP is True: # GetNext
- # For getnext any OID greater than the start qualifies
- oidhit = (oid > searchoid)
- else: # Get
- # For get we need a *specific* OID
- oidhit = (oid.subids == searchoid.subids)
- if oidhit and (reader is not None):
- # We only return OIDs that have a minimal implementation
- # walkMIBTree handles the generation of dynamic trees
- if returnGenerator is True:
- return oid, reader, writer, gen
- else:
- return oid, reader, writer
- except StopIteration: # Couldn't find anything in the tree
- if returnGenerator is True:
- return None, None, None, None
- else:
- return None, None, None
-
- # These exist instead of just using getOID_core so semantics are clearer
- def getOID(self, searchoid, returnGenerator=False):
- "Get the requested OID"
- return self.getOID_core(False, searchoid, returnGenerator)
-
- def getNextOID(self, searchoid, returnGenerator=False):
- "Get the next lexicographical OID"
- return self.getOID_core(True, searchoid, returnGenerator)
-
- def getOIDsInRange(self, oidrange, firstOnly=False):
- "Get a list of every (optionally the first) OID in a range"
- oids = []
- gen = ax.walkMIBTree(self.oidTree, ntpRootOID)
- # Find the first OID
- while True:
- try:
- oid, reader, writer = gen.next()
- if reader is None:
- continue # skip unimplemented OIDs
- elif oid.subids == oidrange.start.subids:
- # ok, found the start, do we need to skip it?
- if oidrange.start.include is True:
- oids.append((oid, reader, writer))
- break
- else:
- continue
- elif oid > oidrange.start:
- # If we are here it means we hit the start but skipped
- oids.append((oid, reader, writer))
- break
- except StopIteration:
- # Couldn't find *anything*
- return []
- if firstOnly is True:
- return oids
- # Start filling in the rest of the range
- while True:
- try:
- oid, reader = gen.next()
- if reader is None:
- continue # skip unimplemented OIDs
- elif (oidrange.end is not None) and (oid >= oidrange.end):
- break # past the end of a bounded range
- else:
- oids.append((oid, reader, writer))
- except StopIteration:
- break # We have run off the end of the MIB
- return oids
-
# =============================================================
# Data read callbacks start here
# comment divider lines represent not yet implemented callbacks
=====================================
pylib/agentx.py
=====================================
--- a/pylib/agentx.py
+++ b/pylib/agentx.py
@@ -20,6 +20,106 @@ except ImportError as e:
defaultTimeout = 30
+class MIBControl:
+ def __init__(self, oidTree, mibRoot=(), rangeSubid=0, upperBound=None,
+ mibContext=None):
+ self.oidTree = oidTree # contains callbacks for the MIB
+ # The undo system is only for the last operation
+ self.inSetP = False # Are we currently in the set procedure?
+ self.setVarbinds = [] # Varbind of the current set operation
+ self.setHandlers = [] # Handlers for commit/undo/cleanup of set
+ self.setUndoData = [] # Previous values for undoing
+ self.mibRoot = mibRoot
+ self.rangeSubid = rangeSubid
+ self.upperBound = upperBound
+ self.context = mibContext
+
+ def mib_rootOID(self):
+ return self.mibRoot
+
+ def mib_rangeSubid(self):
+ return self.rangeSubid
+
+ def mib_upperBound(self):
+ return self.upperBound
+
+ def mib_context(self):
+ return self.context
+
+ def getOID_core(self, nextP, searchoid, returnGenerator=False):
+ gen = ax.walkMIBTree(self.oidTree, self.mibRoot)
+ while True:
+ try:
+ oid, reader, writer = gen.next()
+ if nextP is True: # GetNext
+ # For getnext any OID greater than the start qualifies
+ oidhit = (oid > searchoid)
+ else: # Get
+ # For get we need a *specific* OID
+ oidhit = (oid.subids == searchoid.subids)
+ if oidhit and (reader is not None):
+ # We only return OIDs that have a minimal implementation
+ # walkMIBTree handles the generation of dynamic trees
+ if returnGenerator is True:
+ return oid, reader, writer, gen
+ else:
+ return oid, reader, writer
+ except StopIteration: # Couldn't find anything in the tree
+ if returnGenerator is True:
+ return None, None, None, None
+ else:
+ return None, None, None
+
+ # These exist instead of just using getOID_core so semantics are clearer
+ def getOID(self, searchoid, returnGenerator=False):
+ "Get the requested OID"
+ return self.getOID_core(False, searchoid, returnGenerator)
+
+ def getNextOID(self, searchoid, returnGenerator=False):
+ "Get the next lexicographical OID"
+ return self.getOID_core(True, searchoid, returnGenerator)
+
+ def getOIDsInRange(self, oidrange, firstOnly=False):
+ "Get a list of every (optionally the first) OID in a range"
+ oids = []
+ gen = ax.walkMIBTree(self.oidTree, self.mibRoot)
+ # Find the first OID
+ while True:
+ try:
+ oid, reader, writer = gen.next()
+ if reader is None:
+ continue # skip unimplemented OIDs
+ elif oid.subids == oidrange.start.subids:
+ # ok, found the start, do we need to skip it?
+ if oidrange.start.include is True:
+ oids.append((oid, reader, writer))
+ break
+ else:
+ continue
+ elif oid > oidrange.start:
+ # If we are here it means we hit the start but skipped
+ oids.append((oid, reader, writer))
+ break
+ except StopIteration:
+ # Couldn't find *anything*
+ return []
+ if firstOnly is True:
+ return oids
+ # Start filling in the rest of the range
+ while True:
+ try:
+ oid, reader = gen.next()
+ if reader is None:
+ continue # skip unimplemented OIDs
+ elif (oidrange.end is not None) and (oid >= oidrange.end):
+ break # past the end of a bounded range
+ else:
+ oids.append((oid, reader, writer))
+ except StopIteration:
+ break # We have run off the end of the MIB
+ return oids
+
+
class PacketControl:
def __init__(self, sock, dbase, spinGap=0.001, timeout=defaultTimeout,
logfp=None, debugThreshold=0):
@@ -89,10 +189,10 @@ class PacketControl:
self.sessionID = response.sessionID
# Register the tree
register = ax.RegisterPDU(True, self.sessionID, 1, 1, self.timeout, 1,
- self.database.rootOID(),
- self.database.rangeSubid(),
- self.database.upperBound(),
- self.database.context())
+ self.database.mib_rootOID(),
+ self.database.mib_rangeSubid(),
+ self.database.mib_upperBound(),
+ self.database.mib_context())
self.sendPacket(register, False)
self.log("Sent registration\n", 1)
response = self.waitForResponse(register, True)
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/82f681a02112c578766282b0d07bc542897fd825...f3c7978b932249665d35f7d909dbc4f6decf3962
---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/82f681a02112c578766282b0d07bc542897fd825...f3c7978b932249665d35f7d909dbc4f6decf3962
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20180219/ef61be4f/attachment.html>
More information about the vc
mailing list