[Git][NTPsec/ntpsec][master] 4 commits: Added explanitory comments to doLog().

Ian Bruene gitlab at mg.gitlab.com
Tue Feb 27 16:57:32 UTC 2018


Ian Bruene pushed to branch master at NTPsec / ntpsec


Commits:
f3b10fb8 by Ian Bruene at 2018-02-27T10:57:02-06:00
Added explanitory comments to doLog().

- - - - -
2f8329e5 by Ian Bruene at 2018-02-27T10:57:02-06:00
Fixed backwards log() lambda.

- - - - -
95271b66 by Ian Bruene at 2018-02-27T10:57:02-06:00
Save/Restore of dynamic settings now works.

- - - - -
712d89ba by Ian Bruene at 2018-02-27T10:57:02-06:00
Fixed missing spacer in cbr_entNotifBits().

- - - - -


3 changed files:

- ntpclients/ntpsnmpd.py
- pylib/agentx.py
- pylib/util.py


Changes:

=====================================
ntpclients/ntpsnmpd.py
=====================================
--- a/ntpclients/ntpsnmpd.py
+++ b/ntpclients/ntpsnmpd.py
@@ -43,7 +43,7 @@ DEFLOG = "ntpsnmpd.log"
 
 
 class DataSource(ntp.agentx.MIBControl):
-    def __init__(self, hostname=DEFHOST):
+    def __init__(self, hostname=DEFHOST, settingsFile=None):
         # This is defined as a dict tree because it is simpler, and avoids
         # certain edge cases
         # OIDs are relative from ntp root
@@ -229,6 +229,7 @@ class DataSource(ntp.agentx.MIBControl):
         self.session = ntp.packet.ControlSession()
         self.hostname = hostname if hostname else DEFHOST
         self.session.openhost(self.hostname)
+        self.settingsFilename = settingsFile
         # Cache so we don't hammer ntpd, default 1 second timeout
         # Timeout default pulled from a hat: we don't want it to last for
         # long, just not flood ntpd when we don't need to.
@@ -238,7 +239,7 @@ class DataSource(ntp.agentx.MIBControl):
         self.heartbeatInterval = 0  # should save to disk
         self.sentNotifications = 0
         # Notify bits, they control whether the daemon sends notifications.
-        # these should be saved to disk
+        # these are saved to disk
         self.notifyModeChange = False  # 1
         self.notifyStratumChange = False  # 2
         self.notifySyspeerChange = False  # 3
@@ -247,6 +248,7 @@ class DataSource(ntp.agentx.MIBControl):
         self.notifyConfigChange = False  # 6 [This is not implemented]
         self.notifyLeapSecondAnnounced = False  # 7
         self.notifyHeartbeat = False  # 8
+        self.misc_loadDynamicSettings()
 
     # =============================================================
     # Data read callbacks start here
@@ -458,7 +460,8 @@ class DataSource(ntp.agentx.MIBControl):
 
     def cbr_entNotifBits(self, oid):
         # BITS
-        data = ax.bools2Bits((self.notifyModeChange,
+        data = ax.bools2Bits((False, # notUsed(0)
+                              self.notifyModeChange,
                               self.notifyStratumChange,
                               self.notifySyspeerChange,
                               self.notifyAddAssociation,
@@ -489,9 +492,11 @@ class DataSource(ntp.agentx.MIBControl):
             return ax.ERR_NOERROR
         elif action == "commit":
             self.heartbeatInterval = varbind.payload
+            self.misc_storeDynamicSettings()
             return ax.ERR_NOERROR
         elif action == "undo":
             self.heartbeatInterval = oldData
+            self.misc_storeDynamicSettings()
             return ax.ERR_NOERROR
         elif action == "cleanup":
             pass
@@ -508,6 +513,7 @@ class DataSource(ntp.agentx.MIBControl):
              self.notifyConfigChange,
              self.notifyLeapSecondAnnounced,
              self.notifyHeartbeat) = ax.bits2Bools(varbind.payload, 8)
+            self.misc_storeDynamicSettings()
             return ax.ERR_NOERROR
         elif action == "undo":
             (self.notifyModeChange,
@@ -518,6 +524,7 @@ class DataSource(ntp.agentx.MIBControl):
              self.notifyConfigChange,
              self.notifyLeapSecondAnnounced,
              self.notifyHeartbeat) = ax.bits2Bools(oldData, 8)
+            self.misc_storeDynamicSettings()
             return ax.ERR_NOERROR
         elif action == "cleanup":
             pass
@@ -836,7 +843,7 @@ class DataSource(ntp.agentx.MIBControl):
         pass
 
     def doNotifyLeapSecondAnnounced(self, control):
-        oldLeap = self.oldValues["leap"]
+        oldLeap = self.oldValues.get("leap")
         newLeap = self.safeReadvar(0, ["leap"])
         if newLeap is None:
             return
@@ -885,9 +892,13 @@ class DataSource(ntp.agentx.MIBControl):
     def misc_loadDynamicSettings(self):
         if self.settingsFilename is None:
             return
-        def booify(d, k):
+        def boolify(d, k):
             return True if d[k] == "True" else False
-        settings = loadSettings(self.settingsFilename)
+        optionList = ("notify-mode-change", "notify-stratum-change",
+                      "notify-syspeer-change", "notify-add-association",
+                      "notify-rm-association", "notify-leap-announced",
+                      "notify-heartbeat", "heartbeat-interval")
+        settings = loadSettings(self.settingsFilename, optionList)
         if settings is None:
             return
         for key in settings.keys():
@@ -1091,9 +1102,9 @@ def connect(address):
 
 def mainloop(snmpSocket, reconnectionAddr, host=None):
     dolog("initing loop\n", 1)
-    dbase = DataSource(host)
+    dbase = DataSource(host, "/var/ntpsntpd/notify.conf")
     while True:  # Loop reconnection attempts
-        control = PacketControl(snmpSocket, dbase, logfp=logfp)
+        control = PacketControl(snmpSocket, dbase, logfp=logfp, debug=debug)
         control.loopCallback = dbase.checkNotifications
         control.initNewSession()
         if control.mainloop(True) is False:  # disconnected
@@ -1125,8 +1136,6 @@ def daemonize(runfunc, *runArgs):
     runfunc(*runArgs)
 
 
-optionList = ["master-address", "logfile", "ntp-address"]
-
 def loadSettings(filename, optionList):
     if os.path.isfile(filename) is False:
         return None
@@ -1134,6 +1143,7 @@ def loadSettings(filename, optionList):
     with open(filename) as f:
         data = f.read()
         parser = shlex.shlex(data)
+        parser.wordchars += "-"
         data = [x for x in parser]
         i = 0
         dataLen = len(data)
@@ -1145,10 +1155,13 @@ def loadSettings(filename, optionList):
     return options
 
 
-def storeSettings(filename, options):
+def storeSettings(filename, settings):
+    dirname = os.path.dirname(filename)
+    if os.path.exists(dirname) is False:
+        os.makedirs(dirname)
     data = []
-    for key in options.keys():
-        data.append("%s %s\n" % (key, options[key]))
+    for key in settings.keys():
+        data.append("%s %s\n" % (key, settings[key]))
     data = "".join(data)
     with open(filename, "w") as f:
         f.write(data)


=====================================
pylib/agentx.py
=====================================
--- a/pylib/agentx.py
+++ b/pylib/agentx.py
@@ -123,9 +123,8 @@ class MIBControl:
 
 class PacketControl:
     def __init__(self, sock, dbase, spinGap=0.001, timeout=defaultTimeout,
-                 logfp=None, debugThreshold=0):
-        self.log = (lambda txt, dbg : ntp.util.dolog(logfp, txt, dbg,
-                                                     debugThreshold))
+                 logfp=None, debug=10000):
+        self.log = (lambda txt, dbg : ntp.util.dolog(logfp, txt, debug, dbg))
         # take a pre-made socket instead of making our own so that
         # PacketControl doesn't have to know or care about implementation
         self.socket = sock


=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -82,6 +82,8 @@ PPM_VARS = ("frequency", "clk_wander")
 
 
 def dolog(logfp, text, debug, threshold):
+    # debug is the current debug value
+    # threshold is the trigger for the current log
     if logfp is None:
         return  # can turn off logging by supplying a None file descriptior
     if debug >= threshold:



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/8c34d988b8889ea5209f42e44776136164f5893b...712d89ba252ced287717baf54df6f8e701728cb2

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/8c34d988b8889ea5209f42e44776136164f5893b...712d89ba252ced287717baf54df6f8e701728cb2
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/20180227/57fda216/attachment.html>


More information about the vc mailing list