Perl 根据w3C日志画出流量折线图

公司计费采集不是基于访问日志的。但是原始的访问日志是最被信赖的资源,

常常被用来检验各种参数,比如流量。

日志格式:

222.85.90.158 - - [01/Sep/2012:00:00:00 +0800] "GET /download/apks/ggg-market-1/gggmarket2.0.3zhidian201.apk? HTTP/1.1" 206 126410 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)"

主要 时间 和  字节大小


#!/usr/bin/perl

#use warnings;  
#use strict;  
use Data::Dumper;
use Time::Local;
my $hash;
my $text = $ARGV[0];  
my $num = 0;
my $interval = $ARGV[1];
my $flag = 0;

open FILE ,"<$text" or die "Can't open myfile: $!";
        while ($line = <FILE>) {
        #       print $line;
        my @a = split (/\s+/,$line);
        $time = $a[3];
        $time =~/\[(.*)/;
        $time = $1;
        $num =  $a[9];

        my $string = $time;
        %month = (
                        'Jan', '01', 
                        'Feb', '02', 
                        'Mar', '03', 
                        'Apr', '04', 
                        'May', '05', 
                        'Jun', '06', 
                        'Jul', '07', 
                        'Aug', '08', 
                        'Sep', '09', 
                        'Oct', '10', 
                        'Nov', '11', 
                        'Dec', '12', 
                 );
        my ($year,$month,$date,$hour,$minute,$second);
        if($string=~ m/(\d+)\/(\w+)\/(\d+):(\d+):(\d+):(\d+)/){
                $year = int $3;
                $month = int $month{$2} - 1 ;
                $date = int $1;
                $hour = int $4;
                $minute = int $5;
                $second = int $6;
        }
        else
        {
                next;
        }
        $string = timelocal( $second,$minute,$hour,$date,$month,$year);

        if( $flag == 0 )
        {
                $begin_time = $string;
                $flag = 1;
        }
        if($string - $begin_time < $interval)
        {
                $hash_time = $begin_time;
        }
        else
        {
                $begin_time = $string;
                $hash_time = $begin_time;
        }

        if(exists $hash->{$hash_time}){
                $hash->{$hash_time} += $num;
        }
        else
        {
                $hash->{$hash_time} = $num;
        }
}
#print Dumper($hash);
foreach $time (sort keys %{$hash} ) 
{
         print  $time."\t";
        print $hash->{$time}."\n" ;
}

 perl check_for_billing.pl /tmp/in1 300

根据 /tmp/in 300s计算出一组数据。


check_for_billing.pl in1 60

1346428800      1624072102
1346428860      1819704465
1346428920      1652706944
1346428980      1796033459
1346429040      1771569864
1346429100      1744929423
1346429160      1922832195
1346429220      1660383579
1346429280      1714866844
1346429340      1721153695
1346429400      1642454102
1346429460      1544478258
1346429520      1310100814
1346429580      1581676557
1346429640      1518062958
1346429700      1833178378
1346429760      1580890704
1346429820      1733835211
1346429880      1549757477
1346429940      1460066927
1346430000      1642321720
1346430060      1520984661
1346430120      1598766051
1346430180      1436598072
1346430240      1501967340


然后excel可以简单的画出折线图:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用Perl编写分析大量流量数据结果图的示例代码: ``` #!/usr/bin/perl use strict; use warnings; use File::Find; use GD::Graph::bars; my $dir = "/path/to/traffic/data"; # 流量数据所在目录 my $output_file = "/path/to/output.png"; # 输出文件路径 my %data; # 存储分析结果的哈希表 # 遍历流量数据目录,处理每个数据文件 find(\&process_file, $dir); # 生成柱状图 my @data; my @ips = sort { $data{$b}{requests} <=> $data{$a}{requests} } keys %data; foreach my $ip (@ips) { push @data, [$ip, $data{$ip}{requests}]; } my $graph = GD::Graph::bars->new(800, 600); $graph->set( x_label => 'IP Address', y_label => 'Requests', title => 'Requests by IP Address', y_max_value => $data{$ips[0]}{requests}, y_tick_number => 10, y_label_skip => 1, bar_spacing => 10, bar_width => 20, show_values => 1, values_format => '%d', transparent => 0, dclrs => [ qw(lred) ], ) or die $graph->error; my $image = $graph->plot(\@data); # 将结果图输出到文件 open(my $fh, '>', $output_file) or die "Cannot open file $output_file: $!"; binmode $fh; print $fh $image->png; close($fh); # 处理每个流量数据文件 sub process_file { my $file = $_; return unless ($file =~ /\.log$/); # 只处理.log文件 open(my $fh, '<', $file) or die "Cannot open file $file: $!"; while (my $line = <$fh>) { chomp $line; my ($ip, $date, $time, $method, $url, $protocol, $status, $bytes) = split(/\s+/, $line); next unless ($method eq 'GET'); # 只分析GET请求 $data{$ip}{requests}++; $data{$ip}{bytes} += $bytes; } close($fh); } ``` 上面的代码实现了分析流量数据目录中所有.log文件,并生成柱状图的功能。它会遍历目录下的所有.log文件,对每个文件进行处理。处理的过程中,它会按照每个IP地址统计请求次数,并将分析结果存储在一个哈希表中。最终,它使用GD::Graph::bars模块生成柱状图,并将结果图输出到一个PNG文件中。需要注意的是,上面的示例代码只是一个简单的例子,实际使用时需要根据具体需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值