-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinternetpulse.py
More file actions
73 lines (61 loc) · 2.01 KB
/
internetpulse.py
File metadata and controls
73 lines (61 loc) · 2.01 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/python
#
# Internet Pulse Scraper
# by Tony Perriello (tony.perriello@gmail.com)
#
import sys
import urllib
import re
import datetime
def display_latencies(matches, min_lat, max_lat):
print '--------------------------------------------'
if min_lat == 0:
sys.stdout.write('Normal')
elif min_lat == 90:
sys.stdout.write('Warning')
elif min_lat == 180:
sys.stdout.write('Critical')
elif (min_lat == 0) & (max_lat == 9999):
sys.stdout.write('All')
print ' latencies as of', datetime.datetime.now()
for match in matches:
if (int(match[2]) > min_lat) & (int(match[2]) < max_lat):
print 'Origin: ', match[0], '\nDestination: ', match[1], '\nLatency: ', match[2], '\n\n'
print '--------------------------------------------'
def print_usage():
print 'usage: internetpulse.py [LATENCY TYPE]'
print '-h: Healthy latencies (< 90 ms)\n-w: Warning latencies (< 180 ms)\n-c: Critical latencies (> 180 ms)\n-a: All latencies'
def check_args():
# Set the min and max latencies to look for based on the arguments.
if len(sys.argv) == 1:
print_usage()
exit(1)
elif sys.argv[1] == '-h':
min_lat = 0
max_lat = 90
elif sys.argv[1] == '-w':
min_lat = 90
max_lat = 180
elif sys.argv[1] == '-c':
min_lat = 180
max_lat = 9999
elif sys.argv[1] == '-a':
min_lat = 0
max_lat = 9999
else:
print 'invalid argument.\n'
print_usage()
exit(1)
return min_lat, max_lat
def main():
# Validate args and set min/max lat range based on arg
min_lat, max_lat = check_args()
# Get the main internetpulse page
ufile = urllib.urlopen('http://www.internetpulse.com')
page = ufile.read()
# Parse the html for the Origin/Destination/Latency pattern
matches = re.findall('Origin=(\S+)&Destination=(\S+)&.+>(\d+)</a>', page)
# Show the appropriate latencies based on the args
display_latencies(matches, min_lat, max_lat)
if __name__ == '__main__':
main()