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