import psycopg2 as dbapi HOST = "localhost" PORT = "5432" DB = "db_monitor" USER = "db_monitor" PASS = "pass" def check_1(con): check_ok = True return check_ok CHECKS = [check_1] def connect(): con = None con = dbapi.connect( database = DB, host = HOST, port = PORT, user = USER, password = PASS) return con def disconnect(con): con.close() def application(environ, start_response): status = '200 OK' out = [] ok = True try: out.append('Starting Tests...') con = connect() out.append('Connect OK') try: for check in CHECKS: check(con) out.append('Check "%s" OK' % check.__name__) except: out.append('Check "%s" FAILED' % check.__name__) ok = False out.append('Finished Tests...') disconnect(con) out.append('Disconnect OK') except: out.append('Connect/Disconnect ERROR') ok = False # Final output. Is all ok with the database? if ok: out.append('DATABASE OK') else: out.append('DATABASE ERROR') output = '\n'.join(out) response_headers = [ ('Content-type', 'text/plain'), ('Content-Length', str(len(output)))] start_response(status, response_headers) return [output] if __name__ == '__main__': print application(None, None)