-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpyp.py
More file actions
56 lines (37 loc) · 886 Bytes
/
pyp.py
File metadata and controls
56 lines (37 loc) · 886 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from time import sleep
from pyping.model import Monitor
DEFAULT_TIMEOUT = 5
urls = ['http://google.com', 'http://localhost:8000']
class Hub(object):
observers = []
def __init__(self, urls):
self.monitors = [Monitor(url) for url in urls]
def one(self):
responses = [(m.url, m.ping()) for m in self.monitors]
for response in responses:
for observer in self.observers:
observer(*response)
def run(self):
while True:
self.one()
sleep(10)
#
# basic observers
#
def all_printer(url, response):
print url, response.response_code
def air_horn(url, response):
if response.response_code >= 300:
print '\n', '*' * 80
print 'AwOOOOOOGA!!! {0} is down!'.format(url)
print '\n', '*' * 80
#
# entry point
#
def main():
hub = Hub(urls)
hub.observers.append(all_printer)
hub.observers.append(air_horn)
hub.run()
if __name__ == '__main__':
main()