[Git][NTPsec/ntpsec][master] First peers display in Python. Not quite right yet...
Eric S. Raymond
gitlab at mg.gitlab.com
Sat Oct 22 19:43:48 UTC 2016
Eric S. Raymond pushed to branch master at NTPsec / ntpsec
Commits:
3338c723 by Eric S. Raymond at 2016-10-22T15:43:31-04:00
First peers display in Python. Not quite right yet...
...but major progress.
- - - - -
1 changed file:
- ntpq/pyntpq
Changes:
=====================================
ntpq/pyntpq
=====================================
--- a/ntpq/pyntpq
+++ b/ntpq/pyntpq
@@ -12,7 +12,7 @@
from __future__ import print_function, division
import os, sys, getopt, cmd, errno, curses, curses.ascii
-import socket, select, struct, shlex
+import socket, select, struct, shlex, time
from ntp.packet import *
from ntp.util import *
@@ -337,7 +337,7 @@ usage: help [ command ]
CTL_PST_SEL_SYSPEER: "sys.peer",
CTL_PST_SEL_PPS: "pps.peer",
}
- condition = seldict[statval & 0x7]
+ condition = seldict[statval & 0x7]
else:
if (statval & 0x3) == OLD_CTL_PST_SEL_REJECT:
if (statval & OLD_CTL_PST_SANE) == 0:
@@ -373,9 +373,169 @@ usage: help [ command ]
condition, last_event, event_count)
sys.stdout.write(display.strip() + "\n")
+ @staticmethod
+ def prettyinterval(diff):
+ "Print an interval in natural time units."
+ if diff <= 0:
+ return "-"
+ if diff <= 2048:
+ return str(diff)
+ diff = (diff + 29) / 60
+ if diff <= 300:
+ return "%dm" % diff
+ diff = (diff + 29) / 60
+ if diff <= 96:
+ return "%dh" % diff
+ diff = (diff + 11) / 24
+ return "%dd" % diff
+
def __doprintpeers(self, variables, varlist, associd, af):
- print(variables)
- return True
+ hmode = 0
+ srcport = 0
+ srcaddr = None
+ dstadr_refid = "0.0.0.0"
+ ppoll = 0
+ hpoll = 0
+ reach = 0
+ ptype = '?'
+ displayname = None
+ have_srchost = False
+ have_dstadr = False
+ have_da_rid = False
+ have_jitter = False
+ clock_name = ''
+
+ now = time.time()
+
+ for (name, value) in variables.items():
+ if name in ("srcadr", "peeradr"):
+ srcaddr = value
+ elif name == "srchost":
+ if varlist == Ntpq.__peervarlist or varlist == Ntpq.__apeervarlist:
+ clockname = value
+ have_srchost = True
+ elif name == "dstadr":
+ have_dstadr = True
+ # The C code tried to get a fallback pytpe from this in case
+ # the hmode field was not included
+ if varlist == Ntpq.__opeervarlist:
+ have_da_rid = True
+ dstadr_refid = value[15:]
+ elif name == "hmode":
+ hmode = value
+ elif name == "refid":
+ # The C code for this looked crazily overelaborate. Best
+ # guess is that it was designed to deal with formats that
+ # no longer occur in this field.
+ if varlist == Ntpq.__peervarlist or varlist == Ntpq.__apeervarlist:
+ have_da_rid = True
+ dstaddr_refid = value
+ elif name == "hpoll":
+ hpoll = value
+ if hpoll < 0:
+ hpoll = NTP_MINPOLL
+ elif name == "ppoll":
+ ppoll = value
+ if ppoll < 0:
+ ppoll = NTP_MINPOLL
+ elif name == "reach":
+ # Shipped as hex, displayed in octal
+ reach = value
+ elif name == "delay":
+ estdelay = value
+ elif name == "offset":
+ estoffset = value
+ elif name == "jitter":
+ if varlist == Ntpq.__peervarlist or varlist == Ntpq.__apeervarlist:
+ estjitter = value
+ have_jitter = True
+ elif name == "rootdisp" or name == "dispersion":
+ estdisp = value
+ elif name == "rec":
+ rec = value # l_fp timestamp
+ elif name == "srcport" or name == "peerport":
+ srcport = value
+ elif name == "reftime":
+ reftime = value # l_fp timestamp
+ elif name == "displayname":
+ displayname = value
+ if hmode == MODE_BCLIENT:
+ # broadcastclient or multicastclient
+ ptype = 'b'
+ elif hmode == MODE_BROADCAST:
+ # broadcast or multicast server
+ if srcaddr.startswith("224."): # IANA multicast address prefix
+ ptype = 'M'
+ else:
+ ptype = 'B'
+ elif hmode == MODE_CLIENT:
+ if displayname != None:
+ ptype = 'l' # local refclock
+ elif have_da_rid and dstaddr_refid == "POOL":
+ ptype = 'p' # pool
+ elif srcaddr.startswith("224."):
+ ptype = 'a' # manycastclient
+ else:
+ ptype = 'u' # unicast
+ elif hmode == MODE_ACTIVE:
+ ptype = 's' # symmetric active
+ elif hmode == MODE_PASSIVE:
+ ptype = 'S' # symmetric passive
+
+ #
+ # Got everything, format the line
+ #
+ poll_sec = 1 << min(ppoll, hpoll)
+ if self.pktversion > NTP_OLDVERSION:
+ c = " x.-+#*o"[CTL_PEER_STATVAL(self.session.rstatus) & 0x7]
+ else:
+ c = " .+*"[CTL_PEER_STATVAL(self.session.rstatus) & 0x3]
+ #if len(self.chosts) > 1:
+ # if (varlist == peervarlist or varlist == apeervarlist) and have_dstadr:
+ # serverlocal = nntohost_col(dstadr, True)
+ # else:
+ # if currenthostisnum:
+ # serverlocal = currenthost[:maxhostlen]
+ # else:
+ # serverlocal = currenthost
+ # sys.stdout.write("%-*s " % (maxhostlen, serverlocal))
+ def is_ipv6(addr): return ":" in addr and "." not in addr
+ if socket.AF_UNSPEC == af or af == (socket.AF_INET6 if is_ipv6(srcaddr) else socket.AF_INET):
+ # Source host or clockname
+ namewidth = 15
+ if displayname != None and showhostnames:
+ clock_name = displayname
+ elif not have_srchost:
+ clock_name = canonicalize_dns(srcaddr)
+ if interpreter.wideremote and len(clock_name) > namewidth:
+ sys.stdout.write("%c%s\n " % (c, clock_name))
+ else:
+ sys.stdout.write("%c%-15.15s " % (c, clock_name))
+ # Destination address, assoc ID or refid.
+ if not have_da_rid:
+ drlen = 0
+ else:
+ drlen = len(dstadr_refid)
+ sys.stdout.write(repr(dstaddr_refid)[:drlen])
+ if varlist == Ntpq.__apeervarlist:
+ sys.stdout.write(" " * (namewidth - drlen - 6))
+ sys.stdout.write("%-6d" % (associd))
+ else:
+ sys.stdout.write(" " * (namewidth - drlen))
+ # The rest of the story
+ #prettyinterval(when(&ts, &rec, &reftime)),
+ sys.stdout.write(
+ " %2ld %c %4.4s %4.4s %3lo %7.7s %8.7s %7.7s\n" % \
+ (variables.get("stratum", 0),
+ ptype,
+ Ntpq.prettyinterval(0),
+ Ntpq.prettyinterval(poll_sec),
+ reach, estdelay, estoffset,
+ estjitter if have_jitter else estdisp))
+ return True
+ else:
+ return True
+
def __dogetpeers(self, varlist, associd, af):
try:
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/3338c723b2118615e9a69762fa10665e9d94f716
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20161022/574baa58/attachment.html>
More information about the vc
mailing list