f37 -> f39
[infrastructure.git] / scripts / check_sfa_peer.py
1 #!/usr/bin/env python
2 #! -*- coding: utf-8 -*-
3
4 KEY_FILE = "/root/nrpe-scripts/admins.pkey"
5 CERT_FILE = "/root/nrpe-scripts/admins.cert"
6
7 ENTRY='http://www.planet-lab.eu:12347'
8 ALL = {
9     'ppk': 'http://www.planet-lab.kr:12346',
10     'elc': 'http://www.emanicslab.org:12346',
11     'ple': 'http://www.planet-lab.eu:12346',
12     'plc': 'http://www.planet-lab.org:12347',
13     'plj': 'http://www.planet-lab.jp:12346'
14 }
15
16 import signal,sys
17 from sfa.client.sfaserverproxy import SfaServerProxy
18 from sfa.client.return_value import ReturnValue
19
20 class TimeOutException(Exception):
21     pass
22
23 error = 0
24
25 def timeout(signum, frame):
26     raise TimeOutException, "Command ran for too long"
27
28 def get_version(url):
29     signal.signal(signal.SIGALRM, timeout)
30     signal.alarm(10)
31     server=SfaServerProxy(url, KEY_FILE, CERT_FILE)
32     try:
33         version = server.GetVersion()
34     except Exception, why:
35         raise
36     finally:
37         signal.alarm(0)
38     return version
39
40 def try_peer(url):
41     global error
42
43     try:
44         version = get_version(url)
45     except Exception, why:
46         print "\t[ERROR] (%s): %s" % (url, str(why)),
47         error += 1
48         return
49     try:
50         iface = ReturnValue.get_value(version)['interface']
51     except:
52         iface = '(unknown)'
53     print "\t[ OK  ] %s (%s)" % (url,iface),
54
55 def try_all_peers():
56     for hrn, url in ALL.items():
57         try_peer(url) 
58
59 def try_peers():
60     try:
61         version = get_version(ENTRY)
62     except Exception, why:
63         print "EXCEPTION ", why
64         sys.exit(1)
65     for hrn, url in version['peers'].items():
66         try_peer(url) 
67          
68
69 if __name__ == '__main__':
70     url = sys.argv[1]
71
72     if (url == 'all'):
73         try_all_peers()
74     else:
75         try_peer(url)
76
77     if (error > 0):
78         sys.exit(1)
79     else:
80         sys.exit(0)