[Git][NTPsec/ntpsec][master] 3 commits: Converted unsafe handlers to use safeReadvar().

Ian Bruene gitlab at mg.gitlab.com
Sun Jan 7 13:27:50 UTC 2018


Ian Bruene pushed to branch master at NTPsec / ntpsec


Commits:
ae0c683e by Ian Bruene at 2018-01-07T06:45:48-06:00
Converted unsafe handlers to use safeReadvar().

- - - - -
501986fc by Ian Bruene at 2018-01-07T06:49:06-06:00
Added explanation comment to doNotifyChangeAssociation().

- - - - -
e4bae0f0 by Ian Bruene at 2018-01-07T07:07:52-06:00
Added leap second announcement notification.

- - - - -


1 changed file:

- ntpclients/ntpsnmpd.py


Changes:

=====================================
ntpclients/ntpsnmpd.py
=====================================
--- a/ntpclients/ntpsnmpd.py
+++ b/ntpclients/ntpsnmpd.py
@@ -400,7 +400,9 @@ class DataSource:  # This will be broken up in future to be less NTP-specific
 
     def cbr_statusActiveOffset(self, oid):
         # DisplayString
-        data = self.session.readvar(0, ["koffset"], raw=True)
+        data = self.safeReadvar(0, ["koffset"], raw=True)
+        if data is None:
+            return ax.Varbind(ax.VALUE_NULL, oid)
         data = ntp.util.unitifyvar(data["koffset"][1], "koffset",
                                    width=None, unitSpace=True)
         return ax.Varbind(ax.VALUE_OCTET_STR, oid, data)
@@ -412,7 +414,9 @@ class DataSource:  # This will be broken up in future to be less NTP-specific
 
     def cbr_statusDispersion(self, oid):
         # DisplayString
-        data = self.session.readvar(0, ["rootdisp"], raw=True)
+        data = self.safeReadvar(0, ["rootdisp"], raw=True)
+        if data is None:
+            return ax.Varbind(ax.VALUE_NULL, oid)
         return ax.Varbind(ax.VALUE_OCTET_STR, oid, data["rootdisp"][1])
 
     def cbr_statusEntityUptime(self, oid):
@@ -429,7 +433,9 @@ class DataSource:  # This will be broken up in future to be less NTP-specific
         #  That is the opposite of what the spec claims.
         #
         # I am abandoning the spec, and going with what makes a lick of sense
-        uptime = self.session.readvar(0, ["ss_reset"])["ss_reset"] * 100
+        uptime = self.safeReadvar(0, ["ss_reset"])["ss_reset"] * 100
+        if uptime is None:
+            return ax.Varbind(ax.VALUE_NULL, oid)
         return ax.Varbind(ax.VALUE_TIME_TICKS, oid, uptime)
 
     def cbr_statusDateTime(self, oid):
@@ -463,7 +469,9 @@ class DataSource:  # This will be broken up in future to be less NTP-specific
 
     def cbr_statusLeapSecDirection(self, oid):
         # range of int32
-        leap = self.session.readvar(0, ["leap"])["leap"]
+        leap = self.safeReadvar(0, ["leap"])["leap"]
+        if leap is None:
+            return ax.Varbind(ax.VALUE_NULL, oid)
         if leap == 1:
             pass  # leap 1 == forward
         elif leap == 2:
@@ -485,7 +493,9 @@ class DataSource:  # This will be broken up in future to be less NTP-specific
                                                ax.VALUE_COUNTER32)
 
     def cbr_statusProtocolError(self, oid):
-        data = self.session.readvar(0, ["ss_badformat", "ss_badauth"])
+        data = self.safeReadvar(0, ["ss_badformat", "ss_badauth"])
+        if data is None:
+            return ax.Varbind(ax.VALUE_NULL, oid)
         protoerr = 0
         for key in data.keys():
             protoerr += data[key]
@@ -794,6 +804,7 @@ class DataSource:  # This will be broken up in future to be less NTP-specific
                   ax.Varbind(ax.VALUE_INTEGER, ntpRootOID + (1, 2, 1),
                              newMode)]
             control.sendNotify(vl)
+            self.sentNotifications += 1
 
     def doNotifyStratumChange(self, control):
         oldStratum = self.oldValues.get("stratum")
@@ -819,6 +830,7 @@ class DataSource:  # This will be broken up in future to be less NTP-specific
                   ax.Varbind(ax.VALUE_OCTET_STR, ntpRootOID + (1, 5, 1),
                              "Stratum changed")]  # Uh... what goes here?
             control.sendNotify(vl)
+            self.sentNotifications += 1
 
     def doNotifySyspeerChange(self, control):
         oldSyspeer = self.oldValues.get("syspeer")
@@ -845,8 +857,11 @@ class DataSource:  # This will be broken up in future to be less NTP-specific
                   ax.Varbind(ax.VALUE_OCTET_STR, ntpRootOID + (1, 5, 1),
                              "SysPeer changed")]  # Uh... what goes here?
             control.sendNotify(vl)
+            self.sentNotifications += 1
 
     def doNotifyChangeAssociation(self, control, which):
+        # Add and remove are combined because they use the same data source
+        # and it would be easy to have them stepping on each other.
         changes = self.misc_getAssocListChanges()
         if changes is None:
             return
@@ -870,6 +885,7 @@ class DataSource:  # This will be broken up in future to be less NTP-specific
                       ax.Varbind(ax.VALUE_OCTET_STR, ntpRootOID + (1, 5, 1),
                                  "Association added")]  # Uh... what goes here?
                 control.sendNotify(vl)
+                self.sentNotifications += 1
         if which in ("rm", "both"):
             print("removing", rms)
             for name in rms:
@@ -883,12 +899,36 @@ class DataSource:  # This will be broken up in future to be less NTP-specific
                       ax.Varbind(ax.VALUE_OCTET_STR, ntpRootOID + (1, 5, 1),
                                  "Association removed")]  # Uh... what goes here?
                 control.sendNotify(vl)
+                self.sentNotifications += 1
 
     def doNotifyConfigChange(self, control):
         pass  # DUMMY
 
     def doNotifyLeapSecondAnnounced(self, control):
-        pass  # DUMMY
+        oldLeap = self.oldValues["leap"]
+        newLeap = self.safeReadvar(0, ["leap"])["leap"]
+        if newLeap is None:
+            return
+        if oldLeap is None:
+            self.oldValues["leap"] = newLeap
+            return
+        if oldLeap != newLeap:
+            self.oldValues["leap"] = newLeap
+            if (oldLeap in (0, 3)) and (newLeap in (1, 2)):
+                # changed noleap or unsync to a leap announcement
+                datetime = self.safeReadvar(0, ["reftime"])["reftime"]
+                if datetime is None:
+                    datetime = ""
+                else:
+                    datetime = ntp.util.deformatNTPTime(datetime)
+                vl = [ax.Varbind(ax.VALUE_OID, snmpTrapOID,
+                                 ax.OID(ntpRootOID + (0, 7))),
+                      ax.Varbind(ax.VALUE_OCTET_STR, ntpRootOID + (1, 2, 9),
+                                 datetime),
+                      ax.Varbind(ax.VALUE_OCTET_STR, ntpRootOID + (1, 5, 1),
+                                 "Leap second announced")]
+                control.sendNotify(vl)
+                self.sentNotifications += 1
 
     def doNotifyHeartbeat(self, control):  # TODO: check if ntpd running?
         vl = [ax.Varbind(ax.VALUE_OID, snmpTrapOID,
@@ -898,12 +938,13 @@ class DataSource:  # This will be broken up in future to be less NTP-specific
         if self.heartbeatInterval == 0:  # interval == 0 means send once
             self.notifyHeartbeat = False
             control.sendNotify(vl)
+            self.sentNotifications += 1
         else:
             current = ntp.util.monoclock()
             if (current - self.lastHeartbeat) > self.heartbeatInterval:
                 self.lastHeartbeat = current
                 control.sendNotify(vl)
-        self.sentNotifications += 1
+                self.sentNotifications += 1
 
     # =====================================
     # Misc data helpers (not part of the MIB proper)



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/480e5b4146ae5d7ac72aac6b59de2625696a78b1...e4bae0f0e6292c59a4a65adc6cf358ce27b1e946

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/480e5b4146ae5d7ac72aac6b59de2625696a78b1...e4bae0f0e6292c59a4a65adc6cf358ce27b1e946
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/20180107/a78478d9/attachment.html>


More information about the vc mailing list