diff options
| author | schneefux <schneefux+commit@schneefux.xyz> | 2014-09-10 10:03:27 +0100 |
|---|---|---|
| committer | schneefux <schneefux+commit@schneefux.xyz> | 2014-09-11 06:35:08 +0100 |
| commit | 111f64fdcd2bb3c4d85684bf33207726b4aa3920 (patch) | |
| tree | 420f3f6d2c86fef4acc9a3ee36c9629d7a0893dd | |
| parent | fa926e876485dfe81b288ac3ee8025c2c7f59895 (diff) | |
| download | jasper-client-111f64fdcd2bb3c4d85684bf33207726b4aa3920.tar.gz jasper-client-111f64fdcd2bb3c4d85684bf33207726b4aa3920.zip | |
Simple diagnostic tool for Jasper
| -rwxr-xr-x | client/diagnose.py | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/client/diagnose.py b/client/diagnose.py new file mode 100755 index 0000000..935abbe --- /dev/null +++ b/client/diagnose.py @@ -0,0 +1,105 @@ +#!/usr/bin/env python2 +import time +import re +import socket +import os +from subprocess import check_output, call + +class Diagnostics: + """ + Set of diagnostics to be run for determining the health of the + host running Jasper + + To add new checks, add a boolean returning method with a name that starts + with `check_` + """ + @classmethod + def jasper_modules_path(cls): + return os.path.abspath('../../') + + @classmethod + def check_network_connection(cls): + try: + # see if we can resolve the host name -- tells us if there is + # a DNS listening + host = socket.gethostbyname("www.google.com") + # connect to the host -- tells us if the host is actually + # reachable + s = socket.create_connection((host, 80), 2) + except Exception as e: + return False + else: + return True + + @classmethod + def check_phonetisaurus_dictionary_file(cls): + return os.path.isfile(os.path.join(cls.jasper_modules_path(), "phonetisaurus/g014b2b.fst")) + + @classmethod + def check_phonetisaurus_program(cls): + return call(['which', 'phonetisaurus-g2p']) == 0 + + @classmethod + def info_git_revision(cls): + return check_output(['git','rev-parse','HEAD']) + +class DiagnosticRunner: + """ + Performs a series of checks against the system, printing the results to the + console and also saving them to diagnostic.log + """ + def __init__(self, diagnostics): + self.diagnostics = diagnostics + + def run(self): + self.initialize_log() + self.perform_checks() + + def perform_checks(self): + self.failed_checks = 0 + for check in self.select_methods('check'): + self.do_check(check) + for info in self.select_methods('info'): + self.get_info(info) + if self.failed_checks == 0: + self.log("All checks passed\n") + else: + self.log("%d checks failed\n" % self.failed_checks) + + + def select_methods(self, prefix): + def is_match(method_name): + return callable(getattr(self.diagnostics, method_name)) and re.match(r"\A" + prefix +"_", method_name) + + return [method_name for method_name in dir(self.diagnostics) if is_match(method_name)] + + def initialize_log(self): + self.output = open('diagnostic.log', 'w') + self.log("Starting jasper diagnostic\n") + self.log(time.strftime("%c")+"\n") + + def log(self, msg): + print msg, + self.output.write(msg) + + def get_info(self, info_name): + message = info_name.replace("info_", "").replace("_", " ") + info_method = getattr(self.diagnostics, info_name) + info = info_method() + self.log("%s: %s" % (message, info)) + + def do_check(self, check_name): + message = check_name.replace("check_", "").replace("_", " ") + check = getattr(self.diagnostics, check_name) + self.log("Checking %s... " % message) + if check(): + self.log("OK") + else: + self.failed_checks += 1 + self.log("FAILED") + self.log("\n") + + +if __name__ == '__main__': + DiagnosticRunner(Diagnostics).run() + |
