Created
October 28, 2010 21:22
-
-
Save griggheo/652355 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import os | |
import tornado.escape | |
import tornado.httpclient | |
import tornado.httpserver | |
import tornado.ioloop | |
import tornado.options | |
import tornado.web | |
from tornado.options import define, options | |
import MySQLdb | |
from mysqlchk.config import get_config, set_config_file | |
log = logging.getLogger("mysqlchk") | |
define("config", default=None, help="config file", type=str) | |
define("port", default=31337, help="run on the given port", type=int) | |
class Application(tornado.web.Application): | |
def __init__(self): | |
handlers = [ | |
(r"/mysqlchk/?.*", MysqlchkHandler), | |
] | |
tornado.web.Application.__init__(self, handlers) | |
class MysqlchkHandler(tornado.web.RequestHandler): | |
def get(self): | |
self.mysql_username = get_config('MYSQL_USERNAME') | |
self.mysql_password = get_config('MYSQL_PASSWORD') | |
port = self.get_argument('port', None) | |
server = "127.0.0.1" | |
if not port: | |
raise tornado.web.HTTPError(400) | |
try: | |
port = int(port) | |
conn = MySQLdb.connect(user=self.mysql_username, | |
passwd=self.mysql_password, | |
host=server, | |
port=port) | |
cursor=conn.cursor() | |
cursor.execute("show databases") | |
rows = cursor.fetchall() | |
except Exception, e: | |
raise tornado.web.HTTPError(500) | |
if rows[0][0] != 'information schema' and rows[1][0] != 'mysql': | |
raise tornado.web.HTTPError(500) | |
self.write("Connection to server %s on port %d OK" % (server, port)) | |
def main(): | |
tornado.options.parse_command_line() | |
if options.config: | |
log.info("Using configuration file: %s" % options.config) | |
set_config_file(options.config) | |
http_server = tornado.httpserver.HTTPServer(Application()) | |
http_server.listen(options.port) | |
print "Running Mysqlchk on %d" % options.port | |
tornado.ioloop.IOLoop.instance().start() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment