Home > Database > Mysql Tutorial > mysql_history_monitor

mysql_history_monitor

WBOY
Release: 2016-06-07 16:33:07
Original
1239 people have browsed it

上篇加了bash_history的监控,这篇说mysql_history的监控。不像bash4,mysql自始至终没有提供过syslog的代码,只能自己通过守护进程去实时获取~/.mysql_history的记录了。一个小脚本如下: #!/usr/bin/perl -wuse POE qw(Wheel::FollowTail);use Log::Syslog

上篇加了bash_history的监控,这篇说mysql_history的监控。不像bash4,mysql自始至终没有提供过syslog的代码,只能自己通过守护进程去实时获取~/.mysql_history的记录了。一个小脚本如下:

#!/usr/bin/perl -w
use POE qw(Wheel::FollowTail);
use Log::Syslog::Fast qw(:all);
defined(my $pid = fork) or die "Cant fork:$!";
unless($pid){  
}else{
         exit 0;
}
POE::Session->create(
    inline_states => {
      _start => sub {
        $_[HEAP]{tailor} = POE::Wheel::FollowTail->new(
          Filename => "/root/.mysql_history",
          InputEvent => "got_log_line",
          ResetEvent => "got_log_rollover",
        );
      },
      got_log_line => sub {
#通过Data::Dumper看到实际是$_[10],不过在POE::Session里定义了sub ARG0 () { 10 };这样写起来简单了
        to_rsyslog($_[ARG0]);
      },
      got_log_rollover => sub {
        to_rsyslog('roll');
      },
    }
);
POE::Kernel->run();
exit;
sub to_rsyslog {
  $message = join' ',@_;
#rsyslog开的是UDP的514端口;而LOG_LOCAL0和LOG_INFO都是syslog定义的,乱写的话会自动归入kernel | alert
  my $logger = Log::Syslog::Fast->new(LOG_UDP, "10.0.0.123", 514, LOG_LOCAL0, LOG_INFO, "mysql_231", "mysql_monitor");
  $logger->send($message ,time);
};
Copy after login

当然,mysql的history其实不止一个位置,需要判断~

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template