22
33from cement .core .controller import CementBaseController , expose
44from cement .core import handler , hook
5- from ee .core .shellexec import EEShellExec
5+ from ee .core .shellexec import *
66from ee .core .mysql import EEMysql
77from ee .core .services import EEService
88from ee .core .logging import Log
@@ -32,6 +32,9 @@ class Meta:
3232 dict (help = 'Stop debug' , action = 'store_true' )),
3333 (['--start' ],
3434 dict (help = 'Start debug' , action = 'store_true' )),
35+ (['--import-slow-log' ],
36+ dict (help = 'Import MySQL slow log to Anemometer database' ,
37+ action = 'store_true' )),
3538 (['--nginx' ],
3639 dict (help = 'start/stop debugging nginx server '
3740 'configuration for site' ,
@@ -268,21 +271,6 @@ def debug_mysql(self):
268271 EEMysql .execute (self , "set global long_query_time = 2;" )
269272 EEMysql .execute (self , "set global log_queries_not_using"
270273 "_indexes = \' ON\' ;" )
271- if self .app .pargs .interval :
272- try :
273- cron_time = int (self .app .pargs .interval )
274- except Exception as e :
275- cron_time = 5
276-
277- EEShellExec .cmd_exec (self , "/bin/bash -c \" crontab -l "
278- "2> /dev/null | {{ cat; echo -e"
279- " \\ \" #EasyEngine start MySQL "
280- "slow log \\ n*/{0} * * * * "
281- "/usr/local/bin/ee "
282- "import-slow-log\\ n"
283- "#EasyEngine end MySQL slow log"
284- "\\ \" ; }} | crontab -\" "
285- .format (cron_time ))
286274 else :
287275 Log .info (self , "MySQL slow log is already enabled" )
288276
@@ -313,6 +301,11 @@ def debug_wp(self):
313301 self .app .pargs .site_name ))
314302 webroot = "{0}{1}" .format (EEVariables .ee_webroot ,
315303 self .app .pargs .site_name )
304+ # Check wp-config.php file into htdocs folder
305+ if not os .path .isfile (wp_config ):
306+ wp_config = ("{0}/{1}/htdocs/wp-config.php"
307+ .format (EEVariables .ee_webroot ,
308+ self .app .pargs .site_name ))
316309 if os .path .isfile (wp_config ):
317310 if not EEShellExec .cmd_exec (self , "grep \" \' WP_DEBUG\' \" {0} |"
318311 " grep true" .format (wp_config )):
@@ -344,7 +337,7 @@ def debug_wp(self):
344337 self .app .pargs .site_name )]
345338
346339 else :
347- Log .info (self , "{0} domain not valid "
340+ Log .info (self , "Unable to find wp-config.php for site: {0} "
348341 .format (self .app .pargs .site_name ))
349342
350343 elif (self .app .pargs .wp == 'off' and self .app .pargs .site_name ):
@@ -353,6 +346,11 @@ def debug_wp(self):
353346 self .app .pargs .site_name ))
354347 webroot = "{0}{1}" .format (EEVariables .ee_webroot ,
355348 self .app .pargs .site_name )
349+ # Check wp-config.php file into htdocs folder
350+ if not os .path .isfile (wp_config ):
351+ wp_config = ("{0}/{1}/htdocs/wp-config.php"
352+ .format (EEVariables .ee_webroot ,
353+ self .app .pargs .site_name ))
356354 if os .path .isfile (wp_config ):
357355 if EEShellExec .cmd_exec (self , "grep \" \' WP_DEBUG\' \" {0} | "
358356 "grep true" .format (wp_config )):
@@ -484,13 +482,69 @@ def default(self):
484482 and (not self .app .pargs .fpm ) and (not self .app .pargs .mysql )
485483 and (not self .app .pargs .wp ) and (not self .app .pargs .rewrite )
486484 and (not self .app .pargs .all )
487- and (not self .app .pargs .site_name )):
485+ and (not self .app .pargs .site_name )
486+ and (not self .app .pargs .import_slow_log )
487+ and (not self .app .pargs .interval )):
488488 if self .app .pargs .stop or self .app .pargs .start :
489489 print ("--start/stop option is deprecated since ee3.0.5" )
490490 self .app .args .print_help ()
491491 else :
492492 self .app .args .print_help ()
493493
494+ if self .app .pargs .import_slow_log :
495+ self .import_slow_log ()
496+
497+ if self .app .pargs .interval :
498+ try :
499+ cron_time = int (self .app .pargs .interval )
500+ except Exception as e :
501+ cron_time = 5
502+
503+ try :
504+ if not EEShellExec .cmd_exec (self , "crontab -l | grep "
505+ "'ee debug --import-slow-log'" ):
506+ if not cron_time == 0 :
507+ Log .info (self , "setting up crontab entry,"
508+ " please wait ..." )
509+ EEShellExec .cmd_exec (self , "/bin/bash -c \" crontab -l "
510+ "2> /dev/null | {{ cat; echo -e"
511+ " \\ \" #EasyEngine start MySQL "
512+ "slow log \\ n*/{0} * * * * "
513+ "/usr/local/bin/ee debug"
514+ " --import-slow-log\\ n"
515+ "#EasyEngine end MySQL slow log"
516+ "\\ \" ; }} | crontab -\" "
517+ .format (cron_time ))
518+ else :
519+ if not cron_time == 0 :
520+ Log .info (self , "updating crontab entry,"
521+ " please wait ..." )
522+ if not EEShellExec .cmd_exec (self , "/bin/bash -c "
523+ "\" crontab "
524+ "-l | sed '/EasyEngine "
525+ "start MySQL slow "
526+ "log/!b;n;c\*\/{0} "
527+ "\* \* \* "
528+ "\* \/usr"
529+ "\/local\/bin\/ee debug "
530+ "--import\-slow\-log' "
531+ "| crontab -\" "
532+ .format (cron_time )):
533+ Log .error (self , "failed to update crontab entry" )
534+ else :
535+ Log .info (self , "removing crontab entry,"
536+ " please wait ..." )
537+ if not EEShellExec .cmd_exec (self , "/bin/bash -c "
538+ "\" crontab "
539+ "-l | sed '/EasyEngine "
540+ "start MySQL slow "
541+ "log/,+2d'"
542+ "| crontab -\" "
543+ .format (cron_time )):
544+ Log .error (self , "failed to remove crontab entry" )
545+ except CommandExecutionError as e :
546+ Log .debug (self , str (e ))
547+
494548 if self .app .pargs .all == 'on' :
495549 if self .app .pargs .site_name :
496550 self .app .pargs .wp = 'on'
@@ -559,6 +613,59 @@ def default(self):
559613
560614 logwatch (self , watch_list )
561615
616+ @expose (hide = True )
617+ def import_slow_log (self ):
618+ """Default function for import slow log"""
619+ if os .path .isdir ("{0}22222/htdocs/db/anemometer"
620+ .format (EEVariables .ee_webroot )):
621+ if os .path .isfile ("/var/log/mysql/mysql-slow.log" ):
622+ # Get Anemometer user name and password
623+ Log .info (self , "Importing MySQL slow log to Anemometer" )
624+ host = os .popen ("grep -e \" \' host\' \" {0}22222/htdocs/"
625+ .format (EEVariables .ee_webroot )
626+ + "db/anemometer/conf/config.inc.php "
627+ "| head -1 | cut -d\\ \' -f4 | "
628+ "tr -d '\n '" ).read ()
629+ user = os .popen ("grep -e \" \' user\' \" {0}22222/htdocs/"
630+ .format (EEVariables .ee_webroot )
631+ + "db/anemometer/conf/config.inc.php "
632+ "| head -1 | cut -d\\ \' -f4 | "
633+ "tr -d '\n '" ).read ()
634+ password = os .popen ("grep -e \" \' password\' \" {0}22222/"
635+ .format (EEVariables .ee_webroot )
636+ + "htdocs/db/anemometer/conf"
637+ "/config.inc.php "
638+ "| head -1 | cut -d\\ \' -f4 | "
639+ "tr -d '\n '" ).read ()
640+
641+ # Import slow log Anemometer using pt-query-digest
642+ try :
643+ EEShellExec .cmd_exec (self , "pt-query-digest --user={0} "
644+ "--password={1} "
645+ "--review D=slow_query_log,"
646+ "t=global_query_review "
647+ "--history D=slow_query_log,t="
648+ "global_query_review_history "
649+ "--no-report --limit=0% "
650+ "--filter=\" \\ $event->{{Bytes}} = "
651+ "length(\\ $event->{{arg}}) "
652+ "and \\ $event->{{hostname}}=\\ \" "
653+ "{2}\\ \" \" "
654+ "/var/log/mysql/mysql-slow.log"
655+ .format (user , password , host ))
656+ except CommandExecutionError as e :
657+ Log .debug (self , str (e ))
658+ Log .error (self , "MySQL slow log import failed." )
659+ else :
660+ Log .error (self , "MySQL slow log file not found,"
661+ " so not imported slow logs" )
662+ else :
663+ Log .error (self , "Anemometer is not installed." +
664+ Log .ENDC + "\n You can install Anemometer with "
665+ "this command "
666+ + Log .BOLD + "\n `ee stack install --utils`"
667+ + Log .ENDC )
668+
562669
563670def load (app ):
564671 # register the plugin class.. this only happens if the plugin is enabled
0 commit comments