diff options
-rw-r--r-- | fqdn | 1 | ||||
-rw-r--r-- | last_address | 0 | ||||
-rw-r--r-- | last_response | 1 | ||||
-rw-r--r-- | last_state | 1 | ||||
-rw-r--r-- | maintenance | 0 | ||||
-rw-r--r-- | pythonweb.zip | bin | 0 -> 1313 bytes | |||
-rw-r--r-- | webserver.py | 166 |
7 files changed, 169 insertions, 0 deletions
@@ -0,0 +1 @@ +perftest-global.linaro.org. diff --git a/last_address b/last_address new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/last_address diff --git a/last_response b/last_response new file mode 100644 index 0000000..0316458 --- /dev/null +++ b/last_response @@ -0,0 +1 @@ +503
\ No newline at end of file diff --git a/last_state b/last_state new file mode 100644 index 0000000..bf0d87a --- /dev/null +++ b/last_state @@ -0,0 +1 @@ +4
\ No newline at end of file diff --git a/maintenance b/maintenance new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/maintenance diff --git a/pythonweb.zip b/pythonweb.zip Binary files differnew file mode 100644 index 0000000..3273849 --- /dev/null +++ b/pythonweb.zip diff --git a/webserver.py b/webserver.py new file mode 100644 index 0000000..26fe284 --- /dev/null +++ b/webserver.py @@ -0,0 +1,166 @@ +# Original webserver code copyright Jon Berg , turtlemeat.com +# Extended by Philip Colmer, Linaro, to provide health check service. + +import string,cgi,time +import os +import socket +from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer + +class States: + Passive, Active, Failed, StartingUp, Maintenance, Frozen = range(6) + +class MyHandler(BaseHTTPRequestHandler): + + def do_GET(self): + # Only process requests for "/". For anything else, just return 404. + if (self.path != "/"): + self.send_response(404) + self.send_header('Content-type', 'text/html') + self.end_headers() + return + + service_ip = "" + new_state = last_state = States.Passive + new_response = last_response = 202 + # See if we've got a recorded last state + try: + with open(script_directory + "/last_state") as fp: + line = int(fp.readline()) + if ((line < States.Passive) or (line > States.Frozen)): + line = States.Passive + last_state = line + except Exception,e: + print "Got exception trying to read last state: ", str(e) + + # and see if we've got a recorded last response code + try: + with open(script_directory + "/last_response") as fp: + last_response = int(fp.readline()) + except Exception,e: + print "Got exception trying to read last response: ", str(e) + last_response = 202 + + # and a last IP address + try: + with open(script_directory + "/last_address") as fp: + last_address = fp.readline() + except Exception,e: + print "Got exception trying to read last address: ", str(e) + last_address = "" + + if (os.path.isfile(script_directory + "/frozen")): + print "Frozen file exists" + new_state = States.Frozen + new_response = last_response + elif (os.path.isfile(script_directory + "/maintenance")): + print "Maintenance file exists" + new_state = States.Maintenance + new_response = 503 + elif (last_state == States.Failed): + new_state = States.Failed + new_response = 500 + else: + failed = False + fqdn = "" + # Get the IP address from Route 53 + try: + with open(script_directory + "/fqdn") as fp: + fqdn = str(fp.readline()).rstrip() + except Exception,e: + print "Got exception trying to get fqdn: ", str(e) + failed = True + + if (not failed): + try: + service_ip = socket.gethostbyname(fqdn) + except Exception,e: + print "Got exception trying to get IP address of '%s': %s" % (fqdn, str(e)) + failed = True + + if (not failed): + # Try to get this system's IP address + try: + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + s.connect(("8.8.8.8", 53)) + system_ip = s.getsockname()[0] + s.close + except Exception,e: + print "Got exception trying to get system's IP address: ", str(e) + failed = True + + if (not failed): + print "Service IP = %s and this IP = %s" % (service_ip, system_ip) + # The following logic ONLY works if: + # a) there are two nodes + # b) the IP addresses returned by Route 53 map onto those nodes + if (service_ip != last_address): + # Active node has changed + if (last_state == States.Passive): + # We've become the new active node - switch to starting up + new_state = States.StartingUp + new_response = 203 + if (last_state == States.Active): + # We were the active node - we must have failed + new_state = States.Failed + new_response = 500 + else: + if (service_ip == system_ip): + # We're the active node - report on the results of the + # external health tests + new_state = States.Active + new_response = 200 + else: + # We're the passive node + new_state = States.Passive + new_response = 202 + else: + new_state = States.Failed + new_response = 500 + + print "Returning new response of %s" % str(new_response) + self.send_response(new_response) + self.send_header('Content-type', 'text/html') + self.end_headers() + self.wfile.write("<p>This is the health check service. State is %s and response code is %s</P>" % (str(new_state), str(new_response))) + + # Save away the various bits of information + try: + fp = open(script_directory + "/last_state", "w") + fp.write(str(new_state)) + fp.close + except Exception,e: + print "Got exception trying to save reported state: ", str(e) + + try: + fp = open(script_directory + "/last_response", "w") + fp.write(str(new_response)) + fp.close + except Exception,e: + print "Got exception trying to save reported response: ", str(e) + + try: + fp = open(script_directory + "/last_address", "w") + fp.write(str(service_ip)) + fp.close + except Exception,e: + print "Got exception trying to save service IP: ", str(e) + + return + + def log_message(self, format, *args): + return + +def main(): + global script_directory + try: + script_directory = os.path.dirname(os.path.realpath(__file__)) + server = HTTPServer(('', 1234), MyHandler) + print 'started httpserver...' + server.serve_forever() + except KeyboardInterrupt: + print '^C received, shutting down server' + server.socket.close() + +if __name__ == '__main__': + main() + |