perl anyevent socket监控web日志client

perl anyevent socket监控web日志client,第1张

概述此脚本为client端的code,主要实现读取日志中的一些关键词进行过滤,以下介绍几个模块的用途: File::Tail  用于读取日志,功能类似于linux中tail ,只读取最新的日志 AnyEvent  处理异步事件 AnyEvent::Socket  建立socket连接 AnyEvent::Handle  处理Socket中的句柄 Config::Tiny  读取ini配置文件 #!/u 此脚本为clIEnt端的code,主要实现读取日志中的一些关键词进行过滤,以下介绍几个模块的用途: file::Tail  用于读取日志,功能类似于linux中tail,只读取最新的日志 AnyEvent  处理异步事件 AnyEvent::Socket  建立socket连接 AnyEvent::Handle  处理Socket中的句柄 Config::Tiny  读取ini配置文件
#!/usr/bin/perl#use warnings;#use strict;use file::Tail;use AnyEvent;use AnyEvent::Socket;use AnyEvent::Handle;use Config::Tiny;use FindBin;use lib "$FindBin::Bin/../module";use log_grep;main();sub main {    my $clIEnt_config_file = "$FindBin::Bin/../etc/config.ini";    my $config             = Config::Tiny->new;    my $clIEnt_config      = $config->read($clIEnt_config_file);    my $clIEnt_log_info    = $clIEnt_config->{'clIEnt_config_info'};    my $log_type           = $clIEnt_log_info->{'log_type'};    my $user               = $clIEnt_log_info->{'user'};    my $port               = $clIEnt_log_info->{'port'};    my $log_path           = $clIEnt_log_info->{'log_path'};    my $remove_ip          = $clIEnt_log_info->{'remove_ip'};    my $local_ip           = $clIEnt_log_info->{'local_ip'};    my $apache_reglar =qr/^((?:\d{1,3}\.){3}\d{1,3})[^[]+\[([^]]+)\]\s+\"([^"]+)\"\s+(\d+).*\"([^"]+)\"$/;    my $Nginx_reglar =qr/^((?:\d{1,3})[^[]+\[([^]]+)\]\s+\"([^"]+)\"\s+(\d+).*\"([^"]+)\"$/;#这块根据具体情况来编写,my $log_grep = log_grep->new();    my $cv       = AnyEvent->condvar;                  {        tcp_connect $remove_ip,$port,sub {            my ($fh) = @_              or dIE "tcp_connect: $!";            my $hdl = new AnyEvent::Handle fh => $fh;                                   my $file = file::Tail->new(                name        => $log_path,maxinterval => 1,adjustafter => 3            );            while ( defined( my $log_line = $file->read ) ) {                if ( $log_line =~ $apache_reglar ) {                    my $date    = $log_grep->date_grep();                    my @actions = $log_grep->default_grep();                    my @clIEnts = $log_grep->default_grep();                    my $base ="t:$log_type|me:$actions[1]|so:|lo:$local_ip|date:$date|opt:$actions[0]|of:|u:$user\n";                    $hdl->push_write($base);                }            }                                                        }    }    $cv->recv;}

以下是log_grep.pm模块

package log_grep;use strict;use warnings;sub new {    my $self  = {};    my $class = shift;    bless $self;    return $self;}#date formatsub date_grep {    my $self  = shift;    my $str   = shift;    my %month = (        "Jan" => 1,"Feb" => 2,"Mar" => 3,"Apr" => 4,"May" => 5,"Jun" => 6,"Jul" => 7,"Aug" => 8,"Sep" => 9,"Oct" => 10,"Nov" => 11,"Dec" => 12    );    my ( $day,$mon,$year,$hour,$minute,$sec ) =      ( split /\/|:|\s+/,$str )[ 0,1,2,3,4,5 ];    my $format_date = "$year-$month{$mon}-$day $hour:$minute:$sec";    return $format_date;}#default formatsub default_grep{    my $self =shift;    my $str = shift;    return  split" ",$str;           }1;

配置文件如下:

[clIEnt_config_info]remove_ip=192.168.6.2port=9981local_ip=192.168.6.2log_path=/var/log/httpd/access_loglog_type=Apacheuser=客户端01[server_config_info]username=rootpassword=*******host=localhostdatabase=w3a_systemserver_ip=192.168.6.2server_port=9981
总结

以上是内存溢出为你收集整理的perl anyevent socket监控web日志client全部内容,希望文章能够帮你解决perl anyevent socket监控web日志client所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1278165.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-09
下一篇2022-06-09

发表评论

登录后才能评论

评论列表(0条)

    保存