David Blume commited on 2019-07-07 18:09:31
Showing 1 changed files, with 17 additions and 3 deletions.
| ... | ... |
@@ -10,8 +10,22 @@ process thread pings them one at a time, and prints any changes. |
| 10 | 10 |
|
| 11 | 11 |
 |
| 12 | 12 |
|
| 13 |
-It's so simple that I'm not going to provide a sample source file, |
|
| 14 |
-but for the others below, I will. |
|
| 13 |
+The code for such a loop might look like this: |
|
| 14 |
+ |
|
| 15 |
+ # results is a dict of ip_address -> last ping result |
|
| 16 |
+ for address in results.keys(): |
|
| 17 |
+ result = ping(address) |
|
| 18 |
+ if result != results[address]: |
|
| 19 |
+ log(f'{time.strftime("%Y-%m-%d %H:%M:%S", now)} {address} {result}')
|
|
| 20 |
+ results[address] = result |
|
| 21 |
+ time.sleep(delay) |
|
| 22 |
+ |
|
| 23 |
+Logs go to a logfile, and example output looks like this: |
|
| 24 |
+ |
|
| 25 |
+ 2019-07-06 11:23:20 192.168.1.1 UP |
|
| 26 |
+ 2019-07-06 11:23:20 192.168.1.12 UP |
|
| 27 |
+ 2019-07-06 11:23:29 192.168.1.12 DOWN |
|
| 28 |
+ 2019-07-06 11:23:39 192.168.1.12 UP |
|
| 15 | 29 |
|
| 16 | 30 |
### Upsides |
| 17 | 31 |
|
| ... | ... |
@@ -22,7 +36,7 @@ Really simple code. |
| 22 | 36 |
Since the pings are serialized, one long timeout from one host could |
| 23 | 37 |
affect detecting a problem at another host. |
| 24 | 38 |
|
| 25 |
-## Long Lived Workers Consuming from Queue |
|
| 39 |
+## Long Lived Workers Consuming from a Queue |
|
| 26 | 40 |
|
| 27 | 41 |
Raymond Hettinger's [PyBay 2018 Keynote](https://pybay.com/site_media/slides/raymond2017-keynote/threading.html) |
| 28 | 42 |
uses the queue module to send data between threads, so I thought I'd make a |
| 29 | 43 |