Files
WPIQ/wo/cli/plugins/debug.py

460 lines
22 KiB
Python
Raw Permalink Normal View History

2018-11-13 21:55:59 +01:00
"""Debug Plugin for WordOps"""
2019-09-04 20:36:15 +02:00
import glob
import os
import signal
from cement.core.controller import CementBaseController, expose
from wo.cli.plugins.site_functions import logwatch
from wo.core.fileutils import WOFileUtils
from wo.core.logging import Log
2018-11-13 21:55:59 +01:00
from wo.core.mysql import WOMysql
from wo.core.services import WOService
2019-10-28 09:22:04 +01:00
from wo.core.shellexec import WOShellExec, CommandExecutionError
2019-10-02 13:13:32 +02:00
from wo.core.variables import WOVar
2018-11-13 21:55:59 +01:00
def wo_debug_hook(app):
pass
class WODebugController(CementBaseController):
class Meta:
label = 'debug'
description = 'Used for server level debugging'
stacked_on = 'base'
stacked_type = 'nested'
arguments = [
(['--stop'],
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 OpenLiteSpeed server '
'configuration',
2018-11-13 21:55:59 +01:00
action='store' or 'store_const',
choices=('on', 'off'), const='on', nargs='?')),
(['--mysql'],
dict(help='start/stop debugging MySQL server',
action='store' or 'store_const',
choices=('on', 'off'), const='on', nargs='?')),
(['--wp'],
dict(help='start/stop wordpress debugging for site',
action='store' or 'store_const', choices=('on', 'off'),
const='on', nargs='?')),
(['--all'],
dict(help='start/stop debugging all server parameters',
action='store' or 'store_const', choices=('on', 'off'),
const='on', nargs='?')),
(['-i', '--interactive'],
dict(help='Interactive debug', action='store_true')),
(['--import-slow-log-interval'],
dict(help='Import MySQL slow log to Anemometer',
action='store', dest='interval')),
(['site_name'],
dict(help='Website Name', nargs='?', default=None))
]
2018-11-13 21:55:59 +01:00
usage = "wo debug [<site_name>] [options] "
@expose(hide=True)
def debug_ols(self):
"""Start/Stop OpenLiteSpeed debug"""
ols_conf = "{0}/httpd_config.conf".format(WOVar.wo_ols_conf_dir)
2018-11-13 21:55:59 +01:00
# start global debug
if (self.app.pargs.nginx == 'on' and not self.app.pargs.site_name):
if not WOFileUtils.grepcheck(self, ols_conf, 'logLevel DEBUG'):
Log.info(self, "Setting up OpenLiteSpeed debug log level")
WOFileUtils.searchreplace(
self, ols_conf,
'logLevel NOTICE', 'logLevel DEBUG')
self.trigger_ols = True
else:
Log.info(self, "OpenLiteSpeed debug already enabled")
2018-11-13 21:55:59 +01:00
self.msg = self.msg + [
'/usr/local/lsws/logs/error.log']
2018-11-13 21:55:59 +01:00
# stop global debug
elif (self.app.pargs.nginx == 'off' and
not self.app.pargs.site_name):
if WOFileUtils.grepcheck(self, ols_conf, 'logLevel DEBUG'):
Log.info(self, "Disabling OpenLiteSpeed debug log level")
WOFileUtils.searchreplace(
self, ols_conf,
'logLevel DEBUG', 'logLevel NOTICE')
self.trigger_ols = True
2018-11-13 21:55:59 +01:00
else:
Log.info(self, "OpenLiteSpeed debug already disabled")
2018-11-13 21:55:59 +01:00
# start site-specific debug
2018-11-13 21:55:59 +01:00
elif (self.app.pargs.nginx == 'on' and self.app.pargs.site_name):
vhconf = "{0}/{1}/vhconf.conf".format(
WOVar.wo_ols_vhost_dir, self.app.pargs.site_name)
if os.path.isfile(vhconf):
if not WOFileUtils.grepcheck(
self, vhconf, 'logLevel DEBUG'):
Log.info(self, "Starting OpenLiteSpeed debug for "
2018-11-13 21:55:59 +01:00
"{0}".format(self.app.pargs.site_name))
WOFileUtils.searchreplace(
self, vhconf,
'logLevel NOTICE', 'logLevel DEBUG')
self.trigger_ols = True
2018-11-13 21:55:59 +01:00
else:
Log.info(self, "OpenLiteSpeed debug for site "
"already enabled")
2018-11-13 21:55:59 +01:00
self.msg = self.msg + ['{0}{1}/logs/error.log'
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_webroot,
2018-11-13 21:55:59 +01:00
self.app.pargs.site_name)]
else:
Log.info(self, "{0} domain not valid"
.format(self.app.pargs.site_name))
# stop site-specific debug
2018-11-13 21:55:59 +01:00
elif (self.app.pargs.nginx == 'off' and self.app.pargs.site_name):
vhconf = "{0}/{1}/vhconf.conf".format(
WOVar.wo_ols_vhost_dir, self.app.pargs.site_name)
if os.path.isfile(vhconf):
if WOFileUtils.grepcheck(
self, vhconf, 'logLevel DEBUG'):
Log.info(self, "Stopping OpenLiteSpeed debug for "
"{0}".format(self.app.pargs.site_name))
WOFileUtils.searchreplace(
self, vhconf,
'logLevel DEBUG', 'logLevel NOTICE')
self.trigger_ols = True
2018-11-13 21:55:59 +01:00
else:
Log.info(self, "OpenLiteSpeed debug for site "
"already disabled")
2018-11-13 21:55:59 +01:00
else:
Log.info(self, "{0} domain not valid"
.format(self.app.pargs.site_name))
@expose(hide=True)
def debug_mysql(self):
"""Start/Stop MySQL debug"""
# MySQL start global debug
if (self.app.pargs.mysql == 'on' and not self.app.pargs.site_name):
if not WOShellExec.cmd_exec(self, "mysql -e \"show variables like"
" \'slow_query_log\';\" | "
"grep ON"):
Log.info(self, "Setting up MySQL slow log")
WOMysql.execute(self, "set global slow_query_log = "
"\'ON\';")
WOMysql.execute(self, "set global slow_query_log_file = "
"\'/var/log/mysql/mysql-slow.log\';")
WOMysql.execute(self, "set global long_query_time = 2;")
WOMysql.execute(self, "set global log_queries_not_using"
"_indexes = \'ON\';")
else:
Log.info(self, "MySQL slow log is already enabled")
self.msg = self.msg + ['/var/log/mysql/mysql-slow.log']
# MySQL stop global debug
elif (self.app.pargs.mysql == 'off' and not self.app.pargs.site_name):
if WOShellExec.cmd_exec(self, "mysql -e \"show variables like \'"
"slow_query_log\';\" | grep ON"):
Log.info(self, "Disabling MySQL slow log")
WOMysql.execute(self, "set global slow_query_log = \'OFF\';")
WOMysql.execute(self, "set global slow_query_log_file = \'"
"/var/log/mysql/mysql-slow.log\';")
WOMysql.execute(self, "set global long_query_time = 10;")
WOMysql.execute(self, "set global log_queries_not_using_index"
"es = \'OFF\';")
WOShellExec.cmd_exec(self, "crontab -l | sed \'/#WordOps "
"start/,/#WordOps end/d\' | crontab -")
else:
Log.info(self, "MySQL slow log already disabled")
@expose(hide=True)
def debug_wp(self):
"""Start/Stop WordPress debug"""
if (self.app.pargs.wp == 'on' and self.app.pargs.site_name):
wp_config = ("{0}/{1}/wp-config.php"
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_webroot,
2018-11-13 21:55:59 +01:00
self.app.pargs.site_name))
2019-10-02 13:13:32 +02:00
webroot = "{0}{1}".format(WOVar.wo_webroot,
2018-11-13 21:55:59 +01:00
self.app.pargs.site_name)
# Check wp-config.php file into htdocs folder
if not os.path.isfile(wp_config):
wp_config = ("{0}/{1}/htdocs/wp-config.php"
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_webroot,
2018-11-13 21:55:59 +01:00
self.app.pargs.site_name))
if os.path.isfile(wp_config):
if not WOShellExec.cmd_exec(self, "grep \"\'WP_DEBUG\'\" {0} |"
" grep true".format(wp_config)):
Log.info(self, "Starting WordPress debug")
open("{0}/htdocs/wp-content/debug.log".format(webroot),
encoding='utf-8', mode='a').close()
WOShellExec.cmd_exec(self, "chown {1}: {0}/htdocs/wp-"
"content/debug.log"
"".format(webroot,
2019-10-02 13:13:32 +02:00
WOVar.wo_php_user))
2018-11-13 21:55:59 +01:00
WOShellExec.cmd_exec(self, "sed -i \"s/define(\'WP_DEBUG\'"
".*/define(\'WP_DEBUG\', true);\\n"
"define(\'WP_DEBUG_DISPLAY\', false);"
"\\ndefine(\'WP_DEBUG_LOG\', true);"
"\\ndefine(\'SAVEQUERIES\', true);/\""
" {0}".format(wp_config))
WOShellExec.cmd_exec(self, "cd {0}/htdocs/ && wp"
" plugin --allow-root install "
"developer query-monitor"
.format(webroot))
WOShellExec.cmd_exec(self, "chown -R {1}: {0}/htdocs/"
"wp-content/plugins"
.format(webroot,
2019-10-02 13:13:32 +02:00
WOVar.wo_php_user))
2018-11-13 21:55:59 +01:00
self.msg = self.msg + ['{0}{1}/htdocs/wp-content'
'/debug.log'
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_webroot,
2018-11-13 21:55:59 +01:00
self.app.pargs.site_name)]
else:
Log.info(self, "Unable to find wp-config.php for site: {0}"
.format(self.app.pargs.site_name))
elif (self.app.pargs.wp == 'off' and self.app.pargs.site_name):
wp_config = ("{0}{1}/wp-config.php"
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_webroot,
2018-11-13 21:55:59 +01:00
self.app.pargs.site_name))
2019-10-02 13:13:32 +02:00
webroot = "{0}{1}".format(WOVar.wo_webroot,
2018-11-13 21:55:59 +01:00
self.app.pargs.site_name)
# Check wp-config.php file into htdocs folder
if not os.path.isfile(wp_config):
wp_config = ("{0}/{1}/htdocs/wp-config.php"
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_webroot,
2018-11-13 21:55:59 +01:00
self.app.pargs.site_name))
if os.path.isfile(wp_config):
if WOShellExec.cmd_exec(self, "grep \"\'WP_DEBUG\'\" {0} | "
"grep true".format(wp_config)):
Log.info(self, "Disabling WordPress debug")
WOShellExec.cmd_exec(self, "sed -i \"s/define(\'WP_DEBUG\'"
", true);/define(\'WP_DEBUG\', "
"false);/\" {0}".format(wp_config))
WOShellExec.cmd_exec(self, "sed -i \"/define(\'"
"WP_DEBUG_DISPLAY\', false);/d\" {0}"
.format(wp_config))
WOShellExec.cmd_exec(self, "sed -i \"/define(\'"
"WP_DEBUG_LOG\', true);/d\" {0}"
.format(wp_config))
WOShellExec.cmd_exec(self, "sed -i \"/define(\'"
"SAVEQUERIES\', "
"true);/d\" {0}".format(wp_config))
else:
Log.info(self, "WordPress debug all already disabled")
else:
Log.error(self, "Missing argument site name")
@expose(hide=True)
2019-09-23 15:43:23 +02:00
def signal_handler(self, app, signal, frame):
"""Handle Ctrl+c event for -i option of debug"""
2018-11-13 21:55:59 +01:00
self.start = False
if self.app.pargs.nginx:
self.app.pargs.nginx = 'off'
self.debug_ols()
2018-11-13 21:55:59 +01:00
if self.app.pargs.mysql:
# MySQL debug will not work for remote MySQL
2022-01-23 15:14:24 -03:00
if WOVar.wo_mysql_host == "localhost":
2018-11-13 21:55:59 +01:00
self.app.pargs.mysql = 'off'
self.debug_mysql()
else:
Log.warn(self, "Remote MySQL found, WordOps does not support "
"debugging remote servers")
if self.app.pargs.wp:
self.app.pargs.wp = 'off'
self.debug_wp()
# Reload OpenLiteSpeed
if self.trigger_ols:
WOService.reload_service(self, 'lsws')
2018-11-13 21:55:59 +01:00
self.app.close(0)
@expose(hide=True)
def default(self):
"""Default function of debug"""
# self.start = True
self.interactive = False
self.msg = []
self.trigger_ols = False
2018-11-13 21:55:59 +01:00
if ((not self.app.pargs.nginx) and (not self.app.pargs.mysql) and
(not self.app.pargs.wp) and
2019-03-15 23:12:47 +01:00
(not self.app.pargs.all) and (not self.app.pargs.site_name) and
2019-04-29 02:06:32 +02:00
(not self.app.pargs.import_slow_log) and
(not self.app.pargs.interval)):
2018-11-13 21:55:59 +01:00
if self.app.pargs.stop or self.app.pargs.start:
2019-04-29 02:06:32 +02:00
print("--start/stop option is deprecated since wo v3.0.5")
2018-11-13 21:55:59 +01:00
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:
2019-08-01 10:39:15 +02:00
Log.debug(self, "{0}".format(e))
2018-11-13 21:55:59 +01:00
cron_time = 5
try:
if not WOShellExec.cmd_exec(self, "crontab -l | grep "
"'wo debug --import-slow-log'"):
if not cron_time == 0:
Log.info(self, "setting up crontab entry,"
" please wait...")
WOShellExec.cmd_exec(self, "/bin/bash -c \"crontab -l "
"2> /dev/null | {{ cat; echo -e"
" \\\"#WordOps start MySQL "
"slow log \\n*/{0} * * * * "
"/usr/local/bin/wo debug"
" --import-slow-log\\n"
"#WordOps end MySQL slow log"
"\\\"; }} | crontab -\""
.format(cron_time))
else:
if not cron_time == 0:
Log.info(self, "updating crontab entry,"
" please wait...")
if not WOShellExec.cmd_exec(self, "/bin/bash -c "
"\"crontab "
"-l | sed '/WordOps "
"start MySQL slow "
"log/!b;n;c\*\/{0} "
"\* \* \* "
"\* \/usr"
"\/local\/bin\/wo 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 WOShellExec.cmd_exec(self, "/bin/bash -c "
"\"crontab "
"-l | sed '/WordOps "
"start MySQL slow "
"log/,+2d'"
2019-10-28 09:22:04 +01:00
"| crontab -\""):
2018-11-13 21:55:59 +01:00
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'
self.app.pargs.nginx = 'on'
self.app.pargs.mysql = 'on'
if self.app.pargs.all == 'off':
if self.app.pargs.site_name:
self.app.pargs.wp = 'off'
self.app.pargs.nginx = 'off'
self.app.pargs.mysql = 'off'
if ((not self.app.pargs.nginx) and (not self.app.pargs.mysql) and
(not self.app.pargs.wp) and
2019-03-15 23:12:47 +01:00
self.app.pargs.site_name):
2018-11-13 21:55:59 +01:00
self.app.args.print_help()
if self.app.pargs.nginx:
self.debug_ols()
2018-11-13 21:55:59 +01:00
if self.app.pargs.mysql:
# MySQL debug will not work for remote MySQL
2019-10-02 13:13:32 +02:00
if WOVar.wo_mysql_host == "localhost":
2018-11-13 21:55:59 +01:00
self.debug_mysql()
else:
Log.warn(self, "Remote MySQL found, WordOps does not support "
"debugging remote servers")
if self.app.pargs.wp:
self.debug_wp()
if self.app.pargs.interactive:
self.interactive = True
# Reload OpenLiteSpeed
if self.trigger_ols:
WOService.reload_service(self, 'lsws')
2018-11-13 21:55:59 +01:00
if len(self.msg) > 0:
if not self.app.pargs.interactive:
disp_msg = ' '.join(self.msg)
2019-04-29 02:06:32 +02:00
Log.info(self, "Use following command to check debug logs:\n" +
Log.ENDC + "tail -f {0}".format(disp_msg))
2018-11-13 21:55:59 +01:00
else:
signal.signal(signal.SIGINT, self.signal_handler)
watch_list = []
for w_list in self.msg:
watch_list = watch_list + glob.glob(w_list)
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"
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_webroot)):
2018-11-13 21:55:59 +01:00
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/"
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_webroot) +
2019-04-29 02:06:32 +02:00
"db/anemometer/conf/config.inc.php "
2018-11-13 21:55:59 +01:00
"| head -1 | cut -d\\\' -f4 | "
"tr -d '\n'").read()
user = os.popen("grep -e \"\'user\'\" {0}22222/htdocs/"
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_webroot) +
2019-04-29 02:06:32 +02:00
"db/anemometer/conf/config.inc.php "
2018-11-13 21:55:59 +01:00
"| head -1 | cut -d\\\' -f4 | "
"tr -d '\n'").read()
password = os.popen("grep -e \"\'password\'\" {0}22222/"
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_webroot) +
2019-04-29 02:06:32 +02:00
"htdocs/db/anemometer/conf"
2018-11-13 21:55:59 +01:00
"/config.inc.php "
"| head -1 | cut -d\\\' -f4 | "
"tr -d '\n'").read()
# Import slow log Anemometer using pt-query-digest
try:
WOShellExec.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." +
2019-04-29 02:06:32 +02:00
Log.ENDC + "\n Install Anemometer with:" +
Log.BOLD + "\n `wo stack install --utils`" +
Log.ENDC)
2018-11-13 21:55:59 +01:00
def load(app):
# register the plugin class.. this only happens if the plugin is enabled
2019-09-24 00:01:20 +02:00
app.handler.register(WODebugController)
2018-11-13 21:55:59 +01:00
# register a hook (function) to run after arguments are parsed.
2019-09-24 00:04:32 +02:00
app.hook.register('post_argument_parsing', wo_debug_hook)