[Git][NTPsec/ntpsec][master] Add ? command to ntpmon.

Eric S. Raymond gitlab at mg.gitlab.com
Tue Dec 13 19:12:04 UTC 2016


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


Commits:
930942f8 by Eric S. Raymond at 2016-12-13T14:09:54-05:00
Add ? command to ntpmon.

- - - - -


1 changed file:

- ntpclients/ntpmon


Changes:

=====================================
ntpclients/ntpmon
=====================================
--- a/ntpclients/ntpmon
+++ b/ntpclients/ntpmon
@@ -1,8 +1,25 @@
 #!/usr/bin/env python
-#
-# ntpmon - real-time peerstats/MRU monitor
-#
 # SPDX-License-Identifier: BSD-2-clause
+"""
+Keystroke commands:
+
+a: Change peer display to apeers mode, showing association IDs.
+
+n: Toggle display of hostnames (vs. IP addresses).
+
+o: Change peer display to opeers mode, showing destination address.
+
+p: Change peer display to default mode, showing refid
+
+q: Cleanly terminate the program.
+
+s: Show all hosts, not just reachable ones.
+
+w: Toggle wide mode.
+
+x: Cleanly terminate the program.
+"""
+
 from __future__ import print_function, division
 
 import sys, time, getopt
@@ -58,6 +75,7 @@ class OutputContext:
         stdscr = curses.initscr()
         curses.curs_set(0)
         curses.cbreak()
+        curses.noecho()
     def __exit__(self, extype_unused, value_unused, traceback_unused):
         curses.endwin()
 
@@ -70,6 +88,7 @@ if __name__ == '__main__':
     wideremote = False
     showall = False
     poll_interval = 1
+    helpmode = False
     peer_report = ntp.util.PeerSummary(displaymode="peers",
                                        pktversion=ntp.magic.NTP_VERSION,
                                        showhostnames=showhostnames,
@@ -83,64 +102,73 @@ if __name__ == '__main__':
         sysvars = session.readvar()
         with OutputContext() as ctx:
             while True:
-                try:
-                    peers = session.readstat()
-                except ntp.packet.ControlException as e:
-                    raise Fatal(e.message)
-                except IOError as e:
-                    raise Fatal(e.strerror)
                 stdscr.clear()
                 stdscr.addstr(0, 0, "")
-                stdscr.addstr(peer_report.header() + "\n", curses.A_BOLD)
-                if len(peers) == 0:
-                    raise Fatal("no peers reported")
-                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))
-
-                    # Now the MRU report
-                    span = session.mrulist()
-                    mru_report.now = time.time()
-
-                    # The status line
-                    sl = statline(peer_report, mru_report)
-                    stdscr.addstr(sl + "\n", curses.A_REVERSE|curses.A_DIM)
-                    if span.entries:
-                        stdscr.addstr(ntp.util.MRUSummary.header + "\n",
-                                      curses.A_BOLD)
-                        for entry in reversed(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 - half the smallest poll interval
-                # seen in the last cycle, rounded up to 1 second.
-                stdscr.timeout(int((min(hpolls) / 2) + 0.5))
+                if helpmode:
+                    stdscr.addstr(__doc__)
+                    stdscr.addstr("\nPress any key to resume monitoring")
+                    stdscr.refresh()
+                    stdscr.timeout(-1)
+                else:
+                    try:
+                        peers = session.readstat()
+                    except ntp.packet.ControlException as e:
+                        raise Fatal(e.message)
+                    except IOError as e:
+                        raise Fatal(e.strerror)
+                    stdscr.addstr(peer_report.header() + "\n", curses.A_BOLD)
+                    if len(peers) == 0:
+                        raise Fatal("no peers reported")
+                    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))
+
+                        # Now the MRU report
+                        span = session.mrulist()
+                        mru_report.now = time.time()
+
+                        # The status line
+                        sl = statline(peer_report, mru_report)
+                        stdscr.addstr(sl + "\n", curses.A_REVERSE|curses.A_DIM)
+                        if span.entries:
+                            stdscr.addstr(ntp.util.MRUSummary.header + "\n",
+                                          curses.A_BOLD)
+                            for entry in reversed(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 - half the smallest poll interval
+                    # seen in the last cycle, rounded up to 1 second.
+                    stdscr.timeout(int((min(hpolls) / 2) + 0.5))
                 try:
+                    helpmode = False
                     key = stdscr.getkey()
                     if key == 'q' or key == 'x':
                         raise SystemExit(0)
                     elif key == 'a':
                         peer_report.displaymode = 'apeers'
+                    elif key == '?':
+                        helpmode = True
                     elif key == 'n':
                         peer_report.showhostnames = not peer_report.showhostnames
                         mru_report.showhostnames = not mru_report.showhostnames
@@ -152,6 +180,8 @@ if __name__ == '__main__':
                         showall = not showall
                     elif key == 'w':
                         peer_report.wideremote = not peer_report.wideremote
+                    elif key == '?':
+                        helpmode = True
                 except curses.error:
                     pass
     except KeyboardInterrupt:



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/930942f8eb08787c9ce684fc39fca96908590352
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20161213/8ddc5085/attachment.html>


More information about the vc mailing list