From 5da7d3462af0d3ac0e693d7b901e9de6133f9ec0 Mon Sep 17 00:00:00 2001 From: gau1991 Date: Tue, 20 Jan 2015 12:23:31 +0530 Subject: [PATCH] ee import-slow-log done --- config/plugins.d/import_slow_log.conf | 8 ++++ ee/cli/bootstrap.py | 2 - ee/cli/controllers/isl.py | 26 ------------ ee/cli/plugins/import_slow_log.py | 61 +++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 28 deletions(-) create mode 100644 config/plugins.d/import_slow_log.conf delete mode 100644 ee/cli/controllers/isl.py create mode 100644 ee/cli/plugins/import_slow_log.py diff --git a/config/plugins.d/import_slow_log.conf b/config/plugins.d/import_slow_log.conf new file mode 100644 index 00000000..d7841dbe --- /dev/null +++ b/config/plugins.d/import_slow_log.conf @@ -0,0 +1,8 @@ +### Example Plugin Configuration for EasyEngine + +[import_slow_log] + +### If enabled, load a plugin named `example` either from the Python module +### `ee.cli.plugins.example` or from the file path +### `/var/lib/ee/plugins/example.py` +enable_plugin = true diff --git a/ee/cli/bootstrap.py b/ee/cli/bootstrap.py index a14aacd2..a5ce6d75 100644 --- a/ee/cli/bootstrap.py +++ b/ee/cli/bootstrap.py @@ -5,9 +5,7 @@ from cement.core import handler from ee.cli.controllers.base import EEBaseController -from ee.cli.controllers.isl import EEImportslowlogController def load(app): handler.register(EEBaseController) - handler.register(EEImportslowlogController) diff --git a/ee/cli/controllers/isl.py b/ee/cli/controllers/isl.py deleted file mode 100644 index 7ac8d40f..00000000 --- a/ee/cli/controllers/isl.py +++ /dev/null @@ -1,26 +0,0 @@ -from cement.core.controller import CementBaseController, expose - - -class EEImportslowlogController(CementBaseController): - class Meta: - label = 'import_slow_log' - stacked_on = 'base' - stacked_type = 'nested' - description = 'info command used for debugging issued with stack or \ - site specific configuration' - arguments = [ - (['--mysql'], - dict(help='get mysql configuration information', - action='store_true')), - (['--php'], - dict(help='get php configuration information', - action='store_true')), - (['--nginx'], - dict(help='get nginx configuration information', - action='store_true')), - ] - - @expose(hide=True) - def default(self): - # TODO Default action for ee debug command - print("Inside EEImportslowlogController.default().") diff --git a/ee/cli/plugins/import_slow_log.py b/ee/cli/plugins/import_slow_log.py new file mode 100644 index 00000000..a3cda476 --- /dev/null +++ b/ee/cli/plugins/import_slow_log.py @@ -0,0 +1,61 @@ +from cement.core.controller import CementBaseController, expose +from cement.core import handler, hook +from ee.core.shellexec import EEShellExec +from ee.core.logging import Log +import os + + +def import_slow_log_plugin_hook(app): + pass + + +class EEImportslowlogController(CementBaseController): + class Meta: + label = 'import_slow_log' + stacked_on = 'base' + stacked_type = 'nested' + description = 'Import MySQL slow log to Anemometer database' + + @expose(hide=True) + def default(self): + if os.path.isdir("/var/www/22222/htdocs/db/anemometer"): + if os.path.isfile("/var/log/mysql/mysql-slow.log"): + # Get Anemometer user name and password + host = os.popen("grep -e \"\'host\'\" /var/www/22222/htdocs/" + "db/anemometer/conf/config.inc.php " + "| head -1 | cut -d\\\' -f4 | " + "tr -d '\n'").read() + user = os.popen("grep -e \"\'user\'\" /var/www/22222/htdocs/" + "db/anemometer/conf/config.inc.php " + "| head -1 | cut -d\\\' -f4 | " + "tr -d '\n'").read() + password = os.popen("grep -e \"\'password\'\" /var/www/22222/" + "htdocs/db/anemometer/conf/config.inc.php " + "| head -1 | cut -d\\\' -f4 | " + "tr -d '\n'").read() + # Import slow log Anemometer using pt-query-digest + 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)) + else: + Log.error(self, "Unable to find MySQL slow log file") + else: + Log.error(self, "Anemometer is not installed") + + +def load(app): + # register the plugin class.. this only happens if the plugin is enabled + handler.register(EEImportslowlogController) + + # register a hook (function) to run after arguments are parsed. + hook.register('post_argument_parsing', import_slow_log_plugin_hook)