mysql 慢查询统计分析脚本
2013-01-29 14:17
369 查看
#!/usr/bin/perl
use List::Util qw(first);
use Time::Local;
$minutes = @ARGV ? shift @ARGV : 2;
open MYSQL_CONF, "</etc/my.cnf" or die "cant find my.conf";
$_ = first { /^\s*log-slow-queries\s*=\s*([\w.\/]+)/ } <MYSQL_CONF>
or die "cant find log-slow-qureis";
/=\s*([\w.\/]+)/;
my $slow_log = $1;
my $from_time = time() - $minutes * 60;
my $mtime = (stat("$slow_log"))[9];
print "$mtime vs from_time=$from_time\n";
if ($mtime < $from_time)
{
print "no slow log in recent $minutes minutes\n";
exit;
}
`tail -5000 $slow_log > /tmp/slow_log.txt`;
open SELECT_LOG, ">/tmp/.last.txt" or die "cant open last log txt";
open TMP_LOG, "</tmp/slow_log.txt" or die "open tmp log failed";
select SELECT_LOG;
my $new_log = 0;
while (<TMP_LOG>)
{
if ($new_log) {
print;
next;
}
if (/^# Time: (\d\d)(\d\d)(\d\d) (\d\d):(\d\d):(\d\d)$/) {
$time_s = timelocal($6, $5, $4, $3, $2-1, 2000+$1);
if ($time_s >= $from_time) {
$new_log = 1;
print;
}
}
}
exit if $new_log == 0;
select STDOUT;
`/usr/local/mysql/bin/mysqldumpslow -s t /tmp/.last.txt > /tmp/.sloww`;
open SELECT_LOG, "</tmp/.sloww" or die "cant open slow log";
undef$/;
$content = <SELECT_LOG>;
print $content;
&send_mail("mysql slow log monitor", $content);
sub send_mail {
my ($subject, $message) = @_;
}
使用:
加入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
use List::Util qw(first);
use Time::Local;
$minutes = @ARGV ? shift @ARGV : 2;
open MYSQL_CONF, "</etc/my.cnf" or die "cant find my.conf";
$_ = first { /^\s*log-slow-queries\s*=\s*([\w.\/]+)/ } <MYSQL_CONF>
or die "cant find log-slow-qureis";
/=\s*([\w.\/]+)/;
my $slow_log = $1;
my $from_time = time() - $minutes * 60;
my $mtime = (stat("$slow_log"))[9];
print "$mtime vs from_time=$from_time\n";
if ($mtime < $from_time)
{
print "no slow log in recent $minutes minutes\n";
exit;
}
`tail -5000 $slow_log > /tmp/slow_log.txt`;
open SELECT_LOG, ">/tmp/.last.txt" or die "cant open last log txt";
open TMP_LOG, "</tmp/slow_log.txt" or die "open tmp log failed";
select SELECT_LOG;
my $new_log = 0;
while (<TMP_LOG>)
{
if ($new_log) {
print;
next;
}
if (/^# Time: (\d\d)(\d\d)(\d\d) (\d\d):(\d\d):(\d\d)$/) {
$time_s = timelocal($6, $5, $4, $3, $2-1, 2000+$1);
if ($time_s >= $from_time) {
$new_log = 1;
print;
}
}
}
exit if $new_log == 0;
select STDOUT;
`/usr/local/mysql/bin/mysqldumpslow -s t /tmp/.last.txt > /tmp/.sloww`;
open SELECT_LOG, "</tmp/.sloww" or die "cant open slow log";
undef$/;
$content = <SELECT_LOG>;
print $content;
&send_mail("mysql slow log monitor", $content);
sub send_mail {
my ($subject, $message) = @_;
}
使用:
加入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
相关文章推荐
- 高性能可扩展mysql(执行计划,索引分析优化改写,删除重复数据,区间统计,满查询日志)
- shell脚本分析mysql慢查询日志(slow log)
- MySQL zabbix 流量统计分析脚本(内部业务需求)
- 统计分析类目下mysql快速查询应用
- mysql高级----查询截取分析(二):批量插入数据脚本、用show profile分析sql、全局查询日志
- shell脚本分析mysql慢查询日志(slow log)
- mysql-qps统计python脚本(每秒查询处理量)
- Mysql 列转行统计查询 、行转列统计查询
- Mysql 分组查询统计一个属性出现各个值的次数
- 记录一个mysql按日期分组统计的查询
- mysql查询更新时的锁表机制分析
- MySQL 查询截取分析
- MySQL 多表查询实现分析
- MySQL 慢查询日志分析及可视化结果
- Mysql之count(*)统计查询数量为0的数据
- php日志分析小脚本,分析出每行包含相同字符串的统计数量(从大到小进行排序)
- Mysql 多表联合查询效率分析及优化
- 关于MySQL 通用查询日志和慢查询日志分析
- mysql查询性能分析(show status,show profile,explain)
- 利用Shell脚本实现远程MySQL自动查询