add to git - this initial version is the one that ran before sfa-2.0 was rolled out
[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 import sfa.util.xmlrpcprotocol as xmlrpcprotocol
18
19 class TimeOutException(Exception):
20     pass
21
22 error = 0
23
24 def timeout(signum, frame):
25     raise TimeOutException, "Command ran for too long"
26
27 def get_version(url):
28     signal.signal(signal.SIGALRM, timeout)
29     signal.alarm(10)
30     server=xmlrpcprotocol.get_server(url, KEY_FILE, CERT_FILE)
31     try:
32         version = server.GetVersion()
33     except Exception, why:
34         raise
35     finally:
36         signal.alarm(0)
37     return version
38
39 def try_peer(url):
40     global error
41
42     try:
43         version = get_version(url)
44     except Exception, why:
45         print "\t[ERROR] (%s): %s" % (url, str(why)),
46         error += 1
47         return
48     try:
49         iface = version['interface']
50     except:
51         iface = '(unknown)'
52     print "\t[ OK  ] %s (%s)" % (url,iface),
53
54 def try_all_peers():
55     for hrn, url in ALL.items():
56         try_peer(url) 
57
58 def try_peers():
59     try:
60         version = get_version(ENTRY)
61     except Exception, why:
62         print "EXCEPTION ", why
63         sys.exit(1)
64     for hrn, url in version['peers'].items():
65         try_peer(url) 
66          
67
68 if __name__ == '__main__':
69     url = sys.argv[1]
70
71     if (url == 'all'):
72         try_all_peers()
73     else:
74         try_peer(url)
75
76     if (error > 0):
77         sys.exit(1)
78