[Git][NTPsec/ntpsec][master] In pyntpq, three more commands - the association listers - are mow working.

Eric S. Raymond gitlab at mg.gitlab.com
Tue Oct 18 22:46:00 UTC 2016


Eric S. Raymond pushed to branch master at NTPsec / ntpsec


Commits:
d69e85d8 by Eric S. Raymond at 2016-10-18T18:45:39-04:00
In pyntpq, three more commands - the association listers - are mow working.

- - - - -


2 changed files:

- ntpq/pyntpq
- pylib/packet.py


Changes:

=====================================
ntpq/pyntpq
=====================================
--- a/ntpq/pyntpq
+++ b/ntpq/pyntpq
@@ -284,6 +284,7 @@ usage: help [ command ]
     def __dogetassoc(self):
         try:
             self.peers = self.session.readstat()
+            self.peers.sort(key=lambda a: a.associd)
         except Mode6Exception as e:
             print(e.message)
             return False
@@ -299,6 +300,86 @@ usage: help [ command ]
 	#sortassoc()
 	return True
 
+    def __printassoc(self, showall):
+	condition = "";
+        if not self.peers:
+            sys.stdout.write("No association ID's in list\n");
+            return;
+	sys.stdout.write("\nind assid status  conf reach auth condition  last_event cnt\n");
+        sys.stdout.write("===========================================================\n");
+        for (i, peer) in enumerate(self.peers):
+            statval = CTL_PEER_STATVAL(peer.status)
+            if not showall and (statval & (CTL_PST_CONFIG|CTL_PST_REACH)) == 0:
+                continue
+            event = CTL_PEER_EVENT(peer.status)
+            event_count = CTL_PEER_NEVNT(peer.status)
+            if statval & CTL_PST_CONFIG:
+                conf = "yes"
+            else:
+                conf = "no"
+            if statval & CTL_PST_BCAST:
+                    reach = "none"
+                    if statval & CTL_PST_AUTHENABLE:
+                            auth = "yes"
+                    else:
+                            auth = "none"
+            elif statval & CTL_PST_REACH:
+                reach = "yes"
+            else:
+                reach = "no"
+            if (statval & CTL_PST_AUTHENABLE) == 0:
+                auth = "none"
+            elif statval & CTL_PST_AUTHENTIC:
+                auth = "ok "
+            else:
+                auth = "bad"
+            if self.pktversion > NTP_OLDVERSION:
+                seldict = {
+                    CTL_PST_SEL_REJECT: "reject",
+                    CTL_PST_SEL_SANE: "falsetick",
+                    CTL_PST_SEL_CORRECT: "excess",
+                    CTL_PST_SEL_SELCAND: "outlier",
+                    CTL_PST_SEL_SYNCCAND: "candidate",
+                    CTL_PST_SEL_EXCESS: "backup",
+                    CTL_PST_SEL_SYSPEER: "sys.peer",
+                    CTL_PST_SEL_PPS: "pps.peer",
+                    }
+                condition = seldict[statval & 0x7] 
+            else:
+                if (statval & 0x3) == OLD_CTL_PST_SEL_REJECT:
+                    if (statval & OLD_CTL_PST_SANE) == 0:
+                        condition = "insane"
+                    elif (statval & OLD_CTL_PST_DISP) == 0:
+                        condition = "hi_disp"
+                    else:
+                        condition = ""
+                elif (statval & 0x3) ==  OLD_CTL_PST_SEL_SELCAND:
+                        condition = "sel_cand"
+                elif (statval & 0x3) ==  OLD_CTL_PST_SEL_SYNCCAND:
+                    condition = "sync_cand"
+                elif (statval & 0x3) ==  OLD_CTL_PST_SEL_SYSPEER:
+                    condition = "sys_peer"
+            event_dict = {
+                PEVNT_MOBIL: "mobilize",
+                PEVNT_DEMOBIL: "demobilize",
+                PEVNT_REACH: "reachable",
+                PEVNT_UNREACH: "unreachable",
+                PEVNT_RESTART: "restart",
+                PEVNT_REPLY: "no_reply",
+                PEVNT_RATE: "rate_exceeded",
+                PEVNT_DENY: "access_denied",
+                PEVNT_ARMED: "leap_armed",
+                PEVNT_NEWPEER: "sys_peer",
+                PEVNT_CLOCK: "clock_alarm",
+                }
+            last_event = event_dict.get(PEER_EVENT|event, "")
+            display = \
+                     "%-3s %5u  %04x   %3.3s  %4s  %4.4s %9.9s %11s %2lu" % \
+                     (i + 1, peer.associd,
+                     peer.status, conf, reach, auth,
+                     condition, last_event, event_count)
+            sys.stdout.write(display.strip() + "\n")
+
     def __doprintpeers(self, variables, varlist, associd, af):
         print(variables)
         return True
@@ -633,7 +714,8 @@ usage: keytype [ digest-name ]
 
     def do_associations(self, line):
         "print list of association IDs and statuses for the server's peers"
-        pass
+        if self.__dogetassoc():
+            self.__printassoc(showall=False)
 
     def help_associations(self):
         sys.stdout.write("""\
@@ -643,7 +725,7 @@ usage: associations
 
     def do_passociations(self, line):
         "print list of associations returned by last associations command"
-        pass
+        self.__printassoc(showall=False)
 
     def help_passociations(self):
         sys.stdout.write("""\
@@ -653,7 +735,8 @@ usage: passociations
 
     def do_lassociations(self, line):
         "print list of associations including all client information"
-        pass
+        if self.__dogetassoc():
+            self.__printassoc(showall=True)
 
     def help_lassociations(self):
         sys.stdout.write("""\
@@ -663,7 +746,7 @@ usage: lassociations
 
     def do_lpassociations(self, line):
         "print last obtained list of associations, including client information"
-        pass
+        self.__printassoc(showall=True)
 
     def help_lpassociations(self):
         sys.stdout.write("""\


=====================================
pylib/packet.py
=====================================
--- a/pylib/packet.py
+++ b/pylib/packet.py
@@ -23,6 +23,43 @@ NTP_VERSION     = 4     # Current version
 
 MODE_CONTROL = 6
 
+# Event codes. Used for reporting errors/events to the control module
+PEER_EVENT	= 0x080	# this is a peer event */
+CRPT_EVENT	= 0x100	# this is a crypto event */
+
+# System event codes
+EVNT_UNSPEC	= 0	# unspecified
+EVNT_NSET	= 1	# freq not set
+EVNT_FSET	= 2	# freq set
+EVNT_SPIK	= 3	# spike detect
+EVNT_FREQ	= 4	# freq mode
+EVNT_SYNC	= 5	# clock sync
+EVNT_SYSRESTART	= 6	# restart
+EVNT_SYSFAULT	= 7	# panic stop
+EVNT_NOPEER	= 8	# no sys peer
+EVNT_ARMED	= 9	# leap armed
+EVNT_DISARMED	= 10	# leap disarmed
+EVNT_LEAP	= 11	# leap event
+EVNT_CLOCKRESET	= 12	# clock step
+EVNT_KERN	= 13	# kernel event
+EVNT_TAI	= 14	# TAI
+EVNT_LEAPVAL	= 15	# stale leapsecond values
+
+# Peer event codes
+PEVNT_MOBIL	= (1 | PEER_EVENT) # mobilize
+PEVNT_DEMOBIL	= (2 | PEER_EVENT) # demobilize
+PEVNT_UNREACH	= (3 | PEER_EVENT) # unreachable
+PEVNT_REACH	= (4 | PEER_EVENT) # reachable
+PEVNT_RESTART	= (5 | PEER_EVENT) # restart
+PEVNT_REPLY	= (6 | PEER_EVENT) # no reply
+PEVNT_RATE	= (7 | PEER_EVENT) # rate exceeded
+PEVNT_DENY	= (8 | PEER_EVENT) # access denied
+PEVNT_ARMED	= (9 | PEER_EVENT) # leap armed
+PEVNT_NEWPEER	= (10 | PEER_EVENT) # sys peer
+PEVNT_CLOCK	= (11 | PEER_EVENT) # clock event
+PEVNT_AUTH	= (12 | PEER_EVENT) # bad auth
+PEVNT_POPCORN	= (13 | PEER_EVENT) # popcorn
+
 # From ntpq.h:
 
 # Limit on packets in a single response.  Increasing this value to



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/d69e85d80d0eabd56d1f7c6aadb975a020ea8f60
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20161018/9c48ee1b/attachment.html>


More information about the vc mailing list