David Blume commited on 2019-08-26 22:23:39
Showing 4 changed files, with 52 additions and 9 deletions.
... | ... |
@@ -11,10 +11,16 @@ import subprocess |
11 | 11 |
import time |
12 | 12 |
from argparse import ArgumentParser |
13 | 13 |
import threading |
14 |
-import queue |
|
14 |
+import signal |
|
15 | 15 |
|
16 | 16 |
printer_lock = threading.Lock() |
17 | 17 |
|
18 |
+def log_exit(_signal, frame): |
|
19 |
+ log(f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}' |
|
20 |
+ f' PID={os.getpid()} signal={signal.Signals(_signal).name} Exiting.') |
|
21 |
+ sys.exit(0) |
|
22 |
+ |
|
23 |
+ |
|
18 | 24 |
def pinger(host, delay): |
19 | 25 |
"""Independent worker thread: repeatedly ping, check, print and sleep.""" |
20 | 26 |
last_results = None |
... | ... |
@@ -43,7 +49,7 @@ def log(*args, **kwargs): |
43 | 49 |
|
44 | 50 |
if __name__ == '__main__': |
45 | 51 |
parser = ArgumentParser(description="Repeatedly ping sites. Ex: %(prog)s -d 3 192.168.1.1 192.168.1.12") |
46 |
- parser.add_argument('-d', '--delay', type=float, default=1.0, help='Delay between pings') |
|
52 |
+ parser.add_argument('-d', '--delay', type=float, default=3.0, help='Delay between pings') |
|
47 | 53 |
parser.add_argument('addresses', nargs='+', help='Addresses to ping') |
48 | 54 |
parser_args = parser.parse_args() |
49 | 55 |
|
... | ... |
@@ -56,7 +62,10 @@ if __name__ == '__main__': |
56 | 62 |
_ping_cmd = ('ping', '-c', '1', '-W', '2', '-q') |
57 | 63 |
|
58 | 64 |
log(f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}' |
59 |
- f' PID={os.getpid()} repeat={parser_args.delay}s Starting') |
|
65 |
+ f' PID={os.getpid()} repeat={parser_args.delay}s addresses={",".join(parser_args.addresses)} Starting') |
|
66 |
+ |
|
67 |
+ signal.signal(signal.SIGINT, log_exit) |
|
68 |
+ signal.signal(signal.SIGTERM, log_exit) |
|
60 | 69 |
|
61 | 70 |
# Create worker threads for all but one host address. (Saving one address for this thread.) |
62 | 71 |
for address in parser_args.addresses[:-1]: |
... | ... |
@@ -12,6 +12,14 @@ import time |
12 | 12 |
from argparse import ArgumentParser |
13 | 13 |
import threading |
14 | 14 |
import queue |
15 |
+import signal |
|
16 |
+ |
|
17 |
+ |
|
18 |
+def log_exit(_signal, frame): |
|
19 |
+ _print_queue.put([f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}' |
|
20 |
+ f' PID={os.getpid()} signal={signal.Signals(_signal).name} Exiting.']) |
|
21 |
+ _print_queue.join() |
|
22 |
+ sys.exit(0) |
|
15 | 23 |
|
16 | 24 |
|
17 | 25 |
def pinger(host, delay): |
... | ... |
@@ -50,7 +58,7 @@ def log(*args, **kwargs): |
50 | 58 |
|
51 | 59 |
if __name__ == '__main__': |
52 | 60 |
parser = ArgumentParser(description="Repeatedly ping sites. Ex: %(prog)s -d 3 192.168.1.1 192.168.1.12") |
53 |
- parser.add_argument('-d', '--delay', type=float, default=1.0, help='Delay between pings') |
|
61 |
+ parser.add_argument('-d', '--delay', type=float, default=3.0, help='Delay between pings') |
|
54 | 62 |
parser.add_argument('addresses', nargs='+', help='Addresses to ping') |
55 | 63 |
parser_args = parser.parse_args() |
56 | 64 |
|
... | ... |
@@ -66,8 +74,11 @@ if __name__ == '__main__': |
66 | 74 |
print_thread = threading.Thread(target=print_manager, daemon=True) |
67 | 75 |
print_thread.start() |
68 | 76 |
|
69 |
- _print_queue.put([f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}' |
|
70 |
- f' PID={os.getpid()} repeat={parser_args.delay}s Starting']) |
|
77 |
+ _print_queue.put([f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())} PID={os.getpid()}' |
|
78 |
+ f' repeat={parser_args.delay}s addresses={",".join(parser_args.addresses)} Starting']) |
|
79 |
+ |
|
80 |
+ signal.signal(signal.SIGINT, log_exit) |
|
81 |
+ signal.signal(signal.SIGTERM, log_exit) |
|
71 | 82 |
|
72 | 83 |
# Create worker threads for all but one host address. (Saving one address for this thread.) |
73 | 84 |
for address in parser_args.addresses[:-1]: |
... | ... |
@@ -12,10 +12,18 @@ import time |
12 | 12 |
from argparse import ArgumentParser |
13 | 13 |
import threading |
14 | 14 |
import queue |
15 |
+import signal |
|
15 | 16 |
|
16 | 17 |
pinger_lock = threading.Lock() |
17 | 18 |
|
18 | 19 |
|
20 |
+def log_exit(signal_, frame): |
|
21 |
+ _print_queue.put([f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}' |
|
22 |
+ f' PID={os.getpid()} signal={signal.Signals(signal_).name} Exiting.']) |
|
23 |
+ _print_queue.join() |
|
24 |
+ sys.exit(0) |
|
25 |
+ |
|
26 |
+ |
|
19 | 27 |
def pinger(): |
20 | 28 |
"""Each queue item is a new address to ping. Ping and compare and print. |
21 | 29 |
Issue: Each of these threads reads from and writes to the global _results dict. |
... | ... |
@@ -89,7 +97,11 @@ if __name__ == '__main__': |
89 | 97 |
del t |
90 | 98 |
|
91 | 99 |
_print_queue.put([f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}' |
92 |
- f' PID={os.getpid()} repeat={delay}s Starting']) |
|
100 |
+ f' PID={os.getpid()} repeat={delay}s addresses={",".join(parser_args.addresses)} Starting']) |
|
101 |
+ |
|
102 |
+ signal.signal(signal.SIGINT, log_exit) |
|
103 |
+ signal.signal(signal.SIGTERM, log_exit) |
|
104 |
+ |
|
93 | 105 |
while True: |
94 | 106 |
for address in _results.keys(): |
95 | 107 |
_ping_queue.put(address) |
... | ... |
@@ -12,10 +12,18 @@ import time |
12 | 12 |
from argparse import ArgumentParser |
13 | 13 |
import threading |
14 | 14 |
import queue |
15 |
+import signal |
|
15 | 16 |
|
16 | 17 |
pinger_lock = threading.Lock() |
17 | 18 |
|
18 | 19 |
|
20 |
+def log_exit(_signal, frame): |
|
21 |
+ _print_queue.put([f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}' |
|
22 |
+ f' PID={os.getpid()} signal={signal.Signals(_signal).name} Exiting.']) |
|
23 |
+ _print_queue.join() |
|
24 |
+ sys.exit(0) |
|
25 |
+ |
|
26 |
+ |
|
19 | 27 |
def pinger(host): |
20 | 28 |
"""Executes one ping, prints if there was a change, exits thread.""" |
21 | 29 |
now = time.localtime() |
... | ... |
@@ -53,7 +61,7 @@ def log(*args, **kwargs): |
53 | 61 |
|
54 | 62 |
if __name__ == '__main__': |
55 | 63 |
parser = ArgumentParser(description="Repeatedly ping sites. Ex: %(prog)s -d 3 192.168.1.1 192.168.1.12") |
56 |
- parser.add_argument('-d', '--delay', type=float, default=1.0, help='Delay between pings') |
|
64 |
+ parser.add_argument('-d', '--delay', type=float, default=3.0, help='Delay between pings') |
|
57 | 65 |
parser.add_argument('addresses', nargs='+', help='Addresses to ping') |
58 | 66 |
parser_args = parser.parse_args() |
59 | 67 |
|
... | ... |
@@ -74,8 +82,11 @@ if __name__ == '__main__': |
74 | 82 |
t.start() |
75 | 83 |
del t |
76 | 84 |
|
85 |
+ signal.signal(signal.SIGINT, log_exit) |
|
86 |
+ signal.signal(signal.SIGTERM, log_exit) |
|
87 |
+ |
|
77 | 88 |
_print_queue.put([f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}' |
78 |
- f' PID={os.getpid()} repeat={delay}s Starting']) |
|
89 |
+ f' PID={os.getpid()} repeat={delay}s addresses={",".join(parser_args.addresses)} Starting']) |
|
79 | 90 |
while True: |
80 | 91 |
for address in _results.keys(): |
81 | 92 |
t = threading.Thread(target=pinger, args=(address,), daemon=True) |
82 | 93 |