• mysql 慢查询统计分析脚本


    1. #!/usr/bin/perl
    2. use List::Util qw(first);
    3. use Time::Local;
    4. $minutes = @ARGV ? shift @ARGV : 2;
    5. open MYSQL_CONF, "</etc/my.cnf" or die "cant find my.conf";
    6. $_ = first { /^\s*log-slow-queries\s*=\s*([\w.\/]+)/ } <MYSQL_CONF>
    7.                 or die "cant find log-slow-qureis";
    8. /=\s*([\w.\/]+)/;
    9. my $slow_log = $1;
    10. my $from_time = time() - $minutes * 60;
    11. my $mtime = (stat("$slow_log"))[9];
    12. print "$mtime vs from_time=$from_time\n";
    13. if ($mtime < $from_time)
    14. {
    15.  print "no slow log in recent $minutes minutes\n";
    16.  exit;
    17. }
    18. `tail -5000 $slow_log > /tmp/slow_log.txt`;
    19. open SELECT_LOG, ">/tmp/.last.txt" or die "cant open last log txt";
    20. open TMP_LOG, "</tmp/slow_log.txt" or die "open tmp log failed";
    21. select SELECT_LOG;
    22. my $new_log = 0;
    23. while (<TMP_LOG>)
    24. {
    25.  if ($new_log) {
    26.         print;
    27.         next;
    28.  }
    29.  if (/^# Time: (\d\d)(\d\d)(\d\d) (\d\d):(\d\d):(\d\d)$/) {
    30.         $time_s = timelocal($6, $5, $4, $3, $2-1, 2000+$1);
    31.         if ($time_s >= $from_time) {
    32.                 $new_log = 1;
    33.                 print;
    34.         }
    35.  }
    36. }
    37. exit if $new_log == 0;
    38. select STDOUT;
    39. `/usr/local/mysql/bin/mysqldumpslow -s t /tmp/.last.txt > /tmp/.sloww`;
    40. open SELECT_LOG, "</tmp/.sloww" or die "cant open slow log";
    41. undef$/;
    42. $content = <SELECT_LOG>;
    43. print $content;
    44. &send_mail("mysql slow log monitor", $content);
    45. sub send_mail {
    46.         my ($subject, $message) = @_;
    47. }
    使用:
    加入crontab 即可:
    */2 * * * * /usr/local/bin/mysql_slow_monitor.perl 2 > /tmp/mysql_slow_monitor.log 2>&1 &
     
    转载:http://blog.chinaunix.net/uid-26443921-id-3219655.html
  • 相关阅读:
    AD设置PCB等比例打印
    leetcode------Word Search
    leetcode------Subsets II
    leetcode------Subsets
    leetcode------Palindrome Partitioning
    leetcode------Combinations
    leetcode------Binary Tree Zigzag Level Order Traversal
    leetcode------Populating Next Right Pointers in Each Node II
    leetcode------Populating Next Right Pointers in Each Node
    leetcode------Remove Duplicates from Sorted Array II
  • 原文地址:https://www.cnblogs.com/samurail/p/2881360.html
Copyright © 2020-2023  润新知