From f9bf7e5d13d6f14f4ee71cbf2a0511e1896a7466 Mon Sep 17 00:00:00 2001 From: harshadyeola Date: Wed, 15 Apr 2015 16:17:12 +0530 Subject: [PATCH] added parameter --import-slow-log optional arg to debug command --- ee/cli/plugins/debug.py | 58 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/ee/cli/plugins/debug.py b/ee/cli/plugins/debug.py index c6f370a1..87e87d24 100644 --- a/ee/cli/plugins/debug.py +++ b/ee/cli/plugins/debug.py @@ -32,6 +32,9 @@ class EEDebugController(CementBaseController): dict(help='Stop debug', action='store_true')), (['--start'], dict(help='Start debug', action='store_true')), + (['--import-slow-log'], + dict(help='Import MySQL slow log to Anemometer database', + action='store_true')), (['--nginx'], dict(help='start/stop debugging nginx server ' 'configuration for site', @@ -484,13 +487,17 @@ class EEDebugController(CementBaseController): and (not self.app.pargs.fpm) and (not self.app.pargs.mysql) and (not self.app.pargs.wp) and (not self.app.pargs.rewrite) and (not self.app.pargs.all) - and (not self.app.pargs.site_name)): + and (not self.app.pargs.site_name) + and (not self.app.pargs.import_slow_log)): if self.app.pargs.stop or self.app.pargs.start: print("--start/stop option is deprecated since ee3.0.5") self.app.args.print_help() else: self.app.args.print_help() + if self.app.pargs.import_slow_log: + self.import_slow_log() + if self.app.pargs.all == 'on': if self.app.pargs.site_name: self.app.pargs.wp = 'on' @@ -559,6 +566,55 @@ class EEDebugController(CementBaseController): logwatch(self, watch_list) + @expose(hide=True) + def import_slow_log(self): + """Default function for import slow log""" + if os.path.isdir("{0}22222/htdocs/db/anemometer" + .format(EEVariables.ee_webroot)): + if os.path.isfile("/var/log/mysql/mysql-slow.log"): + # Get Anemometer user name and password + Log.info(self, "Importing MySQL slow log to Anemometer") + host = os.popen("grep -e \"\'host\'\" {0}22222/htdocs/" + .format(EEVariables.ee_webroot) + + "db/anemometer/conf/config.inc.php " + "| head -1 | cut -d\\\' -f4 | " + "tr -d '\n'").read() + user = os.popen("grep -e \"\'user\'\" {0}22222/htdocs/" + .format(EEVariables.ee_webroot) + + "db/anemometer/conf/config.inc.php " + "| head -1 | cut -d\\\' -f4 | " + "tr -d '\n'").read() + password = os.popen("grep -e \"\'password\'\" {0}22222/" + .format(EEVariables.ee_webroot) + + "htdocs/db/anemometer/conf" + "/config.inc.php " + "| head -1 | cut -d\\\' -f4 | " + "tr -d '\n'").read() + + # Import slow log Anemometer using pt-query-digest + try: + EEShellExec.cmd_exec(self, "pt-query-digest --user={0} " + "--password={1} " + "--review D=slow_query_log," + "t=global_query_review " + "--history D=slow_query_log,t=" + "global_query_review_history " + "--no-report --limit=0% " + "--filter=\" \\$event->{{Bytes}} = " + "length(\\$event->{{arg}}) " + "and \\$event->{{hostname}}=\\\"" + "{2}\\\"\" " + "/var/log/mysql/mysql-slow.log" + .format(user, password, host)) + except CommandExecutionError as e: + Log.debug(self, str(e)) + Log.error(self, "MySQL slow log import failed.") + else: + Log.error(self, "MySQL slow log file not found," + " so not imported slow logs") + else: + Log.error(self, "Anemometer is not installed") + def load(app): # register the plugin class.. this only happens if the plugin is enabled