Belle II Software  release-05-01-25
b2hlt_monitor.py
1 #!/usr/bin/env python3
2 
3 # ************************************************************************#
4 # BASF2 (Belle Analysis Framework 2) #
5 # Copyright(C) 2019 - Belle II Collaboration #
6 # #
7 # Author: The Belle II Collaboration #
8 # Contributors: Nils Braun #
9 # #
10 # This software is provided "as is" without any warranty. #
11 # ************************************************************************#
12 
13 import zmq
14 from argparse import ArgumentParser, FileType
15 from zmq_daq.utils import get_monitor_table, normalize_addresses, write_monitoring, show_monitoring
16 
17 from time import sleep
18 
19 if __name__ == '__main__':
20  parser = ArgumentParser(description="Monitor running ZMQ HLT applications with the given addresses.")
21  parser.add_argument(
22  "addresses",
23  nargs='+',
24  help="Monitor the given addresses. " +
25  "Valid input formats are 'tcp://<host>:<port>', '<host>:<port>' or just '<port>' in which case localhost is assumed.")
26  parser.add_argument(
27  "--start",
28  action="store_true",
29  help="Additional to monitoring, also send out a START signal to all monitored processes.")
30  parser.add_argument(
31  "--stop",
32  action="store_true",
33  help="Additional to monitoring, also send out a STOP signal to all monitored processes.")
34  parser.add_argument(
35  "--abort",
36  action="store_true",
37  help="Send out a TERMINATE signal to all monitored processes and do not monitor afterwards.")
38 
39  parser.add_argument(
40  "--show-detail",
41  action="store_true",
42  help="By default, some information are omitted. With this flag, all details are shown (can be very long output).")
43  parser.add_argument("--watch", action="store_true", help="Enter watch mode, where the script is called every 1 second.")
44  parser.add_argument("--dat", help="Write out the results of the monitoring periodically into a dat file. " +
45  "If not combined with --watch, the output will not be shown.")
46 
47  args = parser.parse_args()
48 
49  addresses = normalize_addresses(args.addresses)
50 
51  # Create and connect all needed sockets
52  ctx = zmq.Context()
53  ctx.setsockopt(zmq.LINGER, 0)
54  sockets = {address: ctx.socket(zmq.DEALER) for address in addresses}
55 
56  for address, socket in sockets.items():
57  socket.connect(address)
58 
59  # Now send out signals if requested
60  for socket in sockets.values():
61  if args.start:
62  socket.send_multipart([b"n", b"", b""])
63  if args.stop:
64  socket.send_multipart([b"l", b"", b""])
65  if args.abort:
66  socket.send_multipart([b"x", b"", b""])
67  exit()
68 
69  try:
70  # When no additional things are requested, just show the table once and exit
71  if not args.watch and not args.dat:
72  df = get_monitor_table(sockets, show_detail=args.show_detail)
73  show_monitoring(df)
74  exit()
75 
76  # Else we go into a main loop
77  if args.dat:
78  f = open(args.dat, "wb")
79 
80  while True:
81  df = get_monitor_table(sockets, show_detail=args.show_detail)
82  if args.watch:
83  show_monitoring(df, clear=True)
84 
85  if args.dat:
86  write_monitoring(df, f)
87 
88  sleep(0.5)
89  except KeyboardInterrupt:
90  pass
91  finally:
92  if args.dat:
93  f.close()
zmq_daq.utils
Definition: utils.py:1