[Git][NTPsec/ntpsec][master] Put ntpmon in a more usable state.

Eric S. Raymond gitlab at mg.gitlab.com
Thu Dec 8 19:45:48 UTC 2016


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


Commits:
034881d5 by Eric S. Raymond at 2016-12-08T14:44:49-05:00
Put ntpmon in a more usable state.

- - - - -


3 changed files:

- docs/includes/ntpmon-body.txt
- ntpclients/ntpmon
- pylib/util.py


Changes:

=====================================
docs/includes/ntpmon-body.txt
=====================================
--- a/docs/includes/ntpmon-body.txt
+++ b/docs/includes/ntpmon-body.txt
@@ -4,7 +4,7 @@
 
 == Synopsis ==
 
-+ntpmon+
++ntpmon+ [-a] [-n] [-w] [hostname]
 
 == Description ==
 
@@ -18,4 +18,23 @@ soon as they occur.
 adjusts itself to poll at twice the frequency of the shortest
 polling interval reported in the last peers response.)
 
+== Options ==
+
+Command line options are described following. Specifying a command line
+option other than +-i+ or +-n+ will cause the specified query (queries)
+to be sent to the indicated host(s) immediately. Otherwise, +ntpq+ will
+attempt to read interactive format commands from the standard input.
+
++-a+, +--all+::
+  Show all hosts, not just reachable ones.
++-n+, +--numeric+::
+  Output all host addresses in numeric format rather than
+  converting to the canonical host names.
++-w+, +--wide+::
+  Wide mode: if the host name or IP Address doesn't fit, write the
+  full name/address and indent the next line so columns line up.
+  The default truncates the name or address.
+
+If no hostname is specified on the command line, localhost is monitored.
+
 // end


=====================================
ntpclients/ntpmon
=====================================
--- a/ntpclients/ntpmon
+++ b/ntpclients/ntpmon
@@ -5,7 +5,7 @@
 # SPDX-License-Identifier: BSD-2-clause
 from __future__ import print_function, division
 
-import os, sys, curses, time
+import os, sys, curses, time, getopt
 
 try:
     import ntp.packet
@@ -24,31 +24,53 @@ except ImportError:
     sys.stderr.write("ntpmon: can't find Python curses library -- check PYTHONPATH.\n")
     sys.exit(1)
 
-pktversion = ntp.magic.NTP_VERSION
-showhostnames = True
-wideremote = False
-showall = False
-
 stdscr = None
 
+def statline(_peerlist, _mrulist):
+    "Generate a status line"
+    return ntp.util.stdversion()
+
 class Fatal(Exception):
     "Unrecoverable error."
     def __init__(self, msg):
         Exception.__init__(self)
         self.msg = msg
+    def __str__(self):
+        return self.msg
 
 class OutputContext:
     def __enter__(self):
         "Begin critical region."
         global stdscr
         stdscr = curses.initscr()
+        curses.curs_set(0)
     def __exit__(self, extype_unused, value_unused, traceback_unused):
         curses.endwin()
 
 if __name__ == '__main__':
+    try:
+        (options, arguments) = getopt.getopt(sys.argv[1:],
+                                             "anw", ["all", "numeric", "wide"])
+    except getopt.GetoptError as e:
+        sys.stderr.write("%s\n" % e)
+        sys.stderr.write(usage)
+        raise SystemExit(1)
+    progname = sys.argv[0]
+
+    showhostnames = True
+    wideremote = False
+    showall = False
+    for (switch, val) in options:
+        if switch in ("-a", "--all"):
+            showall = False
+        elif switch in ("-n", "--numeric"):
+            showhostnames = False
+        elif switch in ("-w", "--wide"):
+            wideremote = True
+
     poll_interval = 1
     peer_report = ntp.util.PeerSummary(displaymode="peers",
-                                       pktversion=pktversion,
+                                       pktversion=ntp.magic.NTP_VERSION,
                                        showhostnames=showhostnames,
                                        wideremote=wideremote,
                                        termwidth=80,
@@ -56,7 +78,7 @@ if __name__ == '__main__':
     mru_report = ntp.util.MRUSummary(showhostnames)
     try:
         session = ntp.packet.ControlSession()
-        session.openhost("localhost")
+        session.openhost(arguments[0] if arguments else "localhost")
         with OutputContext() as ctx:
             while True:
                 try:
@@ -70,41 +92,48 @@ if __name__ == '__main__':
                 stdscr.addstr(peer_report.header() + "\n", curses.A_BOLD)
                 if len(peers) == 0:
                     raise Fatal("no peers reported")
-                hpolls = []
-                for peer in peers:
-                    if not showall and \
-                            not (ntp.control.CTL_PEER_STATVAL(peer.status)
-                              & (ntp.control.CTL_PST_CONFIG|ntp.control.CTL_PST_REACH)):
-                        continue
-                    try:
-                        variables = session.readvar(peer.associd)
-                        hpolls.append(variables['hpoll'])
-                    except ntp.packet.ControlException as e:
-                        raise Fatal(e.message + "\n")
-                    except IOError as e:
-                        raise Fatal(e.strerror)
-                    except IndexError:
-                        raise Fatal("no 'hpoll' variable in peer response")
-                    if not variables:
-                        continue
-                    stdscr.addstr(peer_report.summary(session.rstatus,
-                                            variables, peer.associd))
-                # The status line
-                stdscr.addstr(ntp.util.stdversion() + "\n",
-                              curses.A_REVERSE|curses.A_DIM)
-                # Now the MRU report
-                span = session.mrulist()
-                if span.entries:
-                    stdscr.addstr(ntp.util.MRUSummary.header + "\n",
-                                  curses.A_BOLD)
-                    for entry in span.entries:
-                        stdscr.addstr(mru_report.summary(entry) + "\n")
+                try:
+                    hpolls = []
+                    for peer in peers:
+                        if not showall and \
+                                not (ntp.control.CTL_PEER_STATVAL(peer.status)
+                                  & (ntp.control.CTL_PST_CONFIG|ntp.control.CTL_PST_REACH)):
+                            continue
+                        try:
+                            variables = session.readvar(peer.associd)
+                            hpolls.append(variables['hpoll'])
+                        except ntp.packet.ControlException as e:
+                            raise Fatal(e.message + "\n")
+                        except IOError as e:
+                            raise Fatal(e.strerror)
+                        except IndexError:
+                            raise Fatal("no 'hpoll' variable in peer response")
+                        if not variables:
+                            continue
+                        stdscr.addstr(peer_report.summary(session.rstatus,
+                                                variables, peer.associd))
+                    # The status line
+                    sl = statline(peer_report, mru_report)
+                    stdscr.addstr(sl + ((peer_report.termwidth - len(sl)) * " ") + "\n",
+                                  curses.A_REVERSE|curses.A_DIM)
+                    # Now the MRU report
+                    span = session.mrulist()
+                    if span.entries:
+                        stdscr.addstr(ntp.util.MRUSummary.header + "\n",
+                                      curses.A_BOLD)
+                        for entry in span.entries:
+                            stdscr.addstr(mru_report.summary(entry) + "\n")
+                except curses.error:
+                    # An addstr overran the screen, no worries
+                    pass
                 # Display all
                 stdscr.refresh()
                 # Nyquist-interval sampling
                 time.sleep(min(hpolls) / 2)
     except KeyboardInterrupt:
         print("")
+    except Fatal as e:
+        print(e)
     except IOError:
         print("Bailing out...")
 


=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -98,6 +98,7 @@ class PeerSummary:
         self.showhostnames = showhostnames	# If false, display numeric IPs
         self.wideremote = wideremote		# show wide remote names?
         self.debug = debug
+        self.termwidth = termwidth
         # By default, the peer spreadsheet layout is designed so lines just
         # fit in 80 characters. This tells us how much extra horizontal space
         # we have available on a wider terminal emulator.



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/034881d56ef89e37024cf95ca82606305e6a80e6
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20161208/2aa65111/attachment.html>


More information about the vc mailing list