|
2 | 2 |
|
3 | 3 | from cement.core.controller import CementBaseController, expose |
4 | 4 | from cement.core import handler, hook |
5 | | -from ee.core.shellexec import EEShellExec |
| 5 | +from ee.core.shellexec import * |
6 | 6 | from ee.core.mysql import EEMysql |
7 | 7 | from ee.core.services import EEService |
8 | 8 | from ee.core.logging import Log |
@@ -32,6 +32,9 @@ class Meta: |
32 | 32 | dict(help='Stop debug', action='store_true')), |
33 | 33 | (['--start'], |
34 | 34 | 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')), |
35 | 38 | (['--nginx'], |
36 | 39 | dict(help='start/stop debugging nginx server ' |
37 | 40 | 'configuration for site', |
@@ -268,21 +271,6 @@ def debug_mysql(self): |
268 | 271 | EEMysql.execute(self, "set global long_query_time = 2;") |
269 | 272 | EEMysql.execute(self, "set global log_queries_not_using" |
270 | 273 | "_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)) |
286 | 274 | else: |
287 | 275 | Log.info(self, "MySQL slow log is already enabled") |
288 | 276 |
|
@@ -494,13 +482,69 @@ def default(self): |
494 | 482 | and (not self.app.pargs.fpm) and (not self.app.pargs.mysql) |
495 | 483 | and (not self.app.pargs.wp) and (not self.app.pargs.rewrite) |
496 | 484 | and (not self.app.pargs.all) |
497 | | - 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)): |
498 | 488 | if self.app.pargs.stop or self.app.pargs.start: |
499 | 489 | print("--start/stop option is deprecated since ee3.0.5") |
500 | 490 | self.app.args.print_help() |
501 | 491 | else: |
502 | 492 | self.app.args.print_help() |
503 | 493 |
|
| 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 | + |
504 | 548 | if self.app.pargs.all == 'on': |
505 | 549 | if self.app.pargs.site_name: |
506 | 550 | self.app.pargs.wp = 'on' |
@@ -569,6 +613,59 @@ def default(self): |
569 | 613 |
|
570 | 614 | logwatch(self, watch_list) |
571 | 615 |
|
| 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 + " You can install Anemometer with " |
| 665 | + "this command " |
| 666 | + + Log.BOLD + "\n `ee stack install --utils`" |
| 667 | + + Log.ENDC) |
| 668 | + |
572 | 669 |
|
573 | 670 | def load(app): |
574 | 671 | # register the plugin class.. this only happens if the plugin is enabled |
|
0 commit comments