diff --git a/config/bash_completion.d/ee_auto.rc b/config/bash_completion.d/ee_auto.rc index fa9b0d80..1f1b0703 100644 --- a/config/bash_completion.d/ee_auto.rc +++ b/config/bash_completion.d/ee_auto.rc @@ -29,7 +29,7 @@ _ee_complete() # IF YOU HAD ANOTHER CONTROLLER, YOU'D HANDLE THAT HERE "debug") COMPREPLY=( $(compgen \ - -W "$(command find /etc/nginx/sites-enabled/ -type l -printf "%P " 2> /dev/null) --nginx --php --fpm --mysql -i --interactive --all --nginx=off --php=off --fpm=off --mysql=off --all=off " \ + -W "$(command find /etc/nginx/sites-enabled/ -type l -printf "%P " 2> /dev/null) --nginx --php --fpm --mysql -i --interactive --all --import-slow-log --import-slow-log-interval= --nginx=off --php=off --fpm=off --mysql=off --all=off " \ -- $cur) ) ;; @@ -137,8 +137,14 @@ _ee_complete() ;; esac - if [ ${COMP_WORDS[1]} == "debug" ] && ([ "$prev" != "--start" ] || [ "$prev" != "--nginx" ] || [ "$prev" != "--php" ] || [ "$prev" != "--fpm" ] || [ "$prev" != "--mysql" ] || [ "$prev" != "-i" ] || ["$prev" != "--interactive" ] || ["$prev" != "--stop" ]); then - retlist="--all --wp --rewrite -i --all=off --wp=off --rewrite=off" + if [ ${COMP_WORDS[1]} == "debug" ] && ([ "$prev" != "--start" ] && [ "$prev" != "--nginx" ] && [ "$prev" != "--php" ] && [ "$prev" != "--fpm" ] && [ "$prev" != "--mysql" ] && [ "$prev" != "-i" ] && [ "$prev" != "--interactive" ] && [ "$prev" != "--import-slow-log" ] && [ "$prev" != "--stop" ]); then + retlist="--all --wp --rewrite -i --all=off --wp=off --rewrite=off" + ret="${retlist[@]/$prev}" + COMPREPLY=( $(compgen \ + -W "$(echo $ret)" \ + -- $cur) ) + else + retlist="--start --nginx --php --fpm --mysql -i --interactive --stop --import-slow-log --import-slow-log-interval=" ret="${retlist[@]/$prev}" COMPREPLY=( $(compgen \ -W "$(echo $ret)" \ @@ -228,7 +234,7 @@ _ee_complete() elif [[ ${COMP_WORDS[2]} == "start" || ${COMP_WORDS[2]} == "reload" || ${COMP_WORDS[2]} == "restart" || ${COMP_WORDS[2]} == "stop" ]]; then retlist="--nginx --php --mysql --postfix --memcache --dovecot" elif [[ ${COMP_WORDS[1]} == "debug" ]]; then - retlist="--start --nginx --php --fpm --mysql -i --interactive --stop" + retlist="--start --nginx --php --fpm --mysql -i --interactive -stop --import-slow-log --import-slow-log-interval= -" if [[ $prev == '--mysql' ]]; then retlist="--start --nginx --php --fpm --mysql -i --interactive --stop --import-slow-log" fi diff --git a/ee/cli/plugins/debug.py b/ee/cli/plugins/debug.py index 5869728d..4fbc1ede 100644 --- a/ee/cli/plugins/debug.py +++ b/ee/cli/plugins/debug.py @@ -2,7 +2,7 @@ from cement.core.controller import CementBaseController, expose from cement.core import handler, hook -from ee.core.shellexec import EEShellExec +from ee.core.shellexec import * from ee.core.mysql import EEMysql from ee.core.services import EEService from ee.core.logging import Log @@ -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', @@ -268,21 +271,6 @@ class EEDebugController(CementBaseController): EEMysql.execute(self, "set global long_query_time = 2;") EEMysql.execute(self, "set global log_queries_not_using" "_indexes = \'ON\';") - if self.app.pargs.interval: - try: - cron_time = int(self.app.pargs.interval) - except Exception as e: - cron_time = 5 - - EEShellExec.cmd_exec(self, "/bin/bash -c \"crontab -l " - "2> /dev/null | {{ cat; echo -e" - " \\\"#EasyEngine start MySQL " - "slow log \\n*/{0} * * * * " - "/usr/local/bin/ee " - "import-slow-log\\n" - "#EasyEngine end MySQL slow log" - "\\\"; }} | crontab -\"" - .format(cron_time)) else: Log.info(self, "MySQL slow log is already enabled") @@ -494,13 +482,69 @@ 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) + and (not self.app.pargs.interval)): 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.interval: + try: + cron_time = int(self.app.pargs.interval) + except Exception as e: + cron_time = 5 + + try: + if not EEShellExec.cmd_exec(self, "crontab -l | grep " + "'ee debug --import-slow-log'"): + if not cron_time == 0: + Log.info(self, "setting up crontab entry," + " please wait ...") + EEShellExec.cmd_exec(self, "/bin/bash -c \"crontab -l " + "2> /dev/null | {{ cat; echo -e" + " \\\"#EasyEngine start MySQL " + "slow log \\n*/{0} * * * * " + "/usr/local/bin/ee debug" + " --import-slow-log\\n" + "#EasyEngine end MySQL slow log" + "\\\"; }} | crontab -\"" + .format(cron_time)) + else: + if not cron_time == 0: + Log.info(self, "updating crontab entry," + " please wait ...") + if not EEShellExec.cmd_exec(self, "/bin/bash -c " + "\"crontab " + "-l | sed '/EasyEngine " + "start MySQL slow " + "log/!b;n;c\*\/{0} " + "\* \* \* " + "\* \/usr" + "\/local\/bin\/ee debug " + "--import\-slow\-log' " + "| crontab -\"" + .format(cron_time)): + Log.error(self, "failed to update crontab entry") + else: + Log.info(self, "removing crontab entry," + " please wait ...") + if not EEShellExec.cmd_exec(self, "/bin/bash -c " + "\"crontab " + "-l | sed '/EasyEngine " + "start MySQL slow " + "log/,+2d'" + "| crontab -\"" + .format(cron_time)): + Log.error(self, "failed to remove crontab entry") + except CommandExecutionError as e: + Log.debug(self, str(e)) + if self.app.pargs.all == 'on': if self.app.pargs.site_name: self.app.pargs.wp = 'on' @@ -569,6 +613,59 @@ 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." + + Log.ENDC + " You can install Anemometer with " + "this command " + + Log.BOLD + "\n `ee stack install --utils`" + + Log.ENDC) + def load(app): # register the plugin class.. this only happens if the plugin is enabled diff --git a/ee/cli/plugins/import_slow_log.py b/ee/cli/plugins/import_slow_log.py index 4022d0e2..ea035aee 100644 --- a/ee/cli/plugins/import_slow_log.py +++ b/ee/cli/plugins/import_slow_log.py @@ -20,47 +20,10 @@ class EEImportslowlogController(CementBaseController): @expose(hide=True) def default(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 - 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") + Log.info(self, "This command is deprecated." + " You can use this command instead, " + + Log.ENDC + Log.BOLD + "\n`ee debug --import-slow-log`" + + Log.ENDC) def load(app): diff --git a/ee/cli/plugins/site_functions.py b/ee/cli/plugins/site_functions.py index f014cee1..7c860662 100644 --- a/ee/cli/plugins/site_functions.py +++ b/ee/cli/plugins/site_functions.py @@ -697,9 +697,7 @@ def updatewpuserpassword(self, ee_domain, ee_site_webroot): except CommandExecutionError as e: raise SiteError("wp user password update command failed") Log.info(self, "Password updated successfully") - if len(ee_wp_pass) < 8: - Log.warn(self, "Warning: You have provided a " - "weak password") + else: Log.error(self, "Invalid WordPress user {0} for {1}." .format(ee_wp_user, ee_domain))