-
Notifications
You must be signed in to change notification settings - Fork 0
/
fault_checker.py
65 lines (57 loc) · 1.94 KB
/
fault_checker.py
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
'''
fault checker class and dic
'''
import json
import urllib2
from base64 import encodestring
import httplib
import urllib
def check_reachability(network):
result = network.pingAll()
if result > 0:
print ("find the reachability fault, the loss rate is %d%%" % result)
return True
else:
print "no reachability fault"
return False
def check_node_number(network):
url = 'http://127.0.0.1:8181/restconf/operational/network-topology:network-topology/topology/flow:1'
username = 'admin'
password = 'admin'
req = urllib2.Request(url)
auth = encodestring('%s:%s' % (username, password))[:-1]
req.add_header('Authorization', 'Basic %s' % auth)
try:
heml = urllib2.urlopen(req)
except IOError, e:
# here we shouldn't fail if the username/password is right
print "It looks like the username or password is wrong."
return None
json_hash = json.loads(heml.read())
node = json_hash['topology'][0]['node']
return node.__len__()
def check_host_number_for_helium(network):
url = 'http://127.0.0.1:8181/restconf/operational/network-topology:network-topology'
username = 'admin'
password = 'admin'
req = urllib2.Request(url)
auth = encodestring('%s:%s' % (username, password))[:-1]
req.add_header('Authorization', 'Basic %s' % auth)
try:
heml = urllib2.urlopen(req)
except IOError, e:
# here we shouldn't fail if the username/password is right
print "It looks like the username or password is wrong."
return None
json_hash = json.loads(heml.read())
nodes = json_hash['network-topology']['topology'][0]['node']
count = 0
for node in nodes:
if node['node-id'][:4] == 'host':
count += 1
return count
name_to_checker={
"check_reachability" : check_reachability,
"check_node_number" : check_node_number,
"check_host_number_for_helium" : check_host_number_for_helium
}