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可以简单的画出折线图:



猜你喜欢

转载自blog.csdn.net/cccAllen/article/details/7954979