add to git - this initial version is the one that ran before sfa-2.0 was rolled out
authorThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Mon, 9 Jan 2012 12:20:14 +0000 (13:20 +0100)
committerThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Mon, 9 Jan 2012 12:20:14 +0000 (13:20 +0100)
scripts/check_sfa_peer.py [new file with mode: 0755]

diff --git a/scripts/check_sfa_peer.py b/scripts/check_sfa_peer.py
new file mode 100755 (executable)
index 0000000..3e22197
--- /dev/null
@@ -0,0 +1,78 @@
+#!/usr/bin/env python
+#! -*- coding: utf-8 -*-
+
+KEY_FILE = "/root/nrpe-scripts/admins.pkey"
+CERT_FILE = "/root/nrpe-scripts/admins.cert"
+
+ENTRY='http://www.planet-lab.eu:12347'
+ALL = {
+    'ppk': 'http://www.planet-lab.kr:12346',
+    'elc': 'http://www.emanicslab.org:12346',
+    'ple': 'http://www.planet-lab.eu:12346',
+    'plc': 'http://www.planet-lab.org:12347',
+    'plj': 'http://www.planet-lab.jp:12346'
+}
+
+import signal,sys
+import sfa.util.xmlrpcprotocol as xmlrpcprotocol
+
+class TimeOutException(Exception):
+    pass
+
+error = 0
+
+def timeout(signum, frame):
+    raise TimeOutException, "Command ran for too long"
+
+def get_version(url):
+    signal.signal(signal.SIGALRM, timeout)
+    signal.alarm(10)
+    server=xmlrpcprotocol.get_server(url, KEY_FILE, CERT_FILE)
+    try:
+        version = server.GetVersion()
+    except Exception, why:
+        raise
+    finally:
+        signal.alarm(0)
+    return version
+
+def try_peer(url):
+    global error
+
+    try:
+        version = get_version(url)
+    except Exception, why:
+        print "\t[ERROR] (%s): %s" % (url, str(why)),
+        error += 1
+        return
+    try:
+        iface = version['interface']
+    except:
+        iface = '(unknown)'
+    print "\t[ OK  ] %s (%s)" % (url,iface),
+
+def try_all_peers():
+    for hrn, url in ALL.items():
+        try_peer(url) 
+
+def try_peers():
+    try:
+        version = get_version(ENTRY)
+    except Exception, why:
+        print "EXCEPTION ", why
+        sys.exit(1)
+    for hrn, url in version['peers'].items():
+        try_peer(url) 
+         
+
+if __name__ == '__main__':
+    url = sys.argv[1]
+
+    if (url == 'all'):
+        try_all_peers()
+    else:
+        try_peer(url)
+
+    if (error > 0):
+        sys.exit(1)
+