- 论坛徽章:
- 0
|
我的程序请看, 还请告诉我有什么需要改善的..
程序有点长....
- #!/usr/bin/perl
- use POSIX;
- use strict;
- use warnings;
- use Template;
- # define variable
- my $dir = $ARGV[0]; # input var of home
- my $home_dir = "/home/md";
- my $bin_dir = "$home_dir/bin";
- #my $tmp_dir = "$home_dir/tmp";
- my $tmp_dir = "/smc/tmp/sharon";
- #my $error_dir = "$home_dir/error";
- my $error_dir = "/smc/tmp/sharon";
- my $log_dir = "$home_dir/log";
- my $infile= "";
- my $Count = 0;
- my %recordhash = ();
- my %confighash = ();
- my @durtab = ();
- my %statab =();
- my @notab = ();
- my $ysdate = strftime("%Y%m%d",localtime(time-86400));
- if($dir=~/miepcdr/i or $dir =~/httpcdr/i) { # Make sure confirm the Directory, it must be Case-insensitive miepcdr or httpcdr;
- &ReadFolder($dir,$tmp_dir,$bin_dir,\@durtab,\%statab,\@notab,$error_dir); # It pass the specified global variable to subroutine.
- &printhtmlreporttable(\@durtab,\%statab,\@notab,$dir); # It pass the specified global variable to subroutine.
- @durtab = ();
- %statab = ();
- @notab = ();
- } else {
- print "Error: Is not the implementation of the directory!\n";
- exit;
- }
- sub ReadFolder() {
- my($dir,$tmp_dir,$bin_dir,$durtab,$statab,$notab,$error_dir)=@_; # Reference the Global Variable, Please look for line 30.
-
- if ( !-d $dir ) {
- print "Error: No exists directory, Please check!";
- exit;
- } else {
-
- if( !opendir (DH,$dir) ) {
- die ( "Can't open $dir:$!" );
- }
-
- chdir( $dir );
- }
-
- # ---------------Start config file -----------------------
- if ( !open(CONF,"$bin_dir/config.txt") ) {
- die( "Can not open the file" );
- }
-
- while(<CONF>) {
- chomp;
-
- if ( /(\d{1,3}\.)(\d{1,3}\.\d{1,3}\.\d{1,3}) / ) {
- $confighash{$1}{$2} = 1;
- } else {
-
- if ( /(\w+\.)(\w.*) / ) {
- $confighash{$1}{$2} = 1;
- }
- }
- }
- close(CONF);
- # ---------------End config file -----------------------
- &defaultarray($durtab,$notab); # Iinitialize the Global Array @durtab and @notab;
- foreach $infile(readdir DH) {
-
- if( $infile =~/(.+)\-$ysdate\.DAT\.Z$/ ) {
-
- &checkfilecontent($infile, $dir, \%confighash,$durtab,$statab,$notab,$error_dir);# It pass the specified variable to the subroutine.
- }
- }
- %confighash = ();
- closedir DH;
- }
- sub defaultarray() {
- my ($newdur,$newnon ) = @_; # Reference the Global Array, Please look for line 74.
- for my $i ( 0 .. 3 ){
- for my $j ( 0, 1 ) {
- for my $k ( 0 .. 4 ) {
- $newdur->[$i]->[$j]->[$k]=0;
- $newnon->[$i]->[$j]=0;
- }
- }
- }
- }
- sub checkfilecontent() { # read each line
-
- my ($in, $dir ,$conf,$durtab,$statab,$notab,$error_dir)= @_; # Reference the Global Variable, Please look for line 80.
- my $status;
- my $code;
- my $start;
- my $end;
- my $url;
- my $noneurl;
- my $dur;
- my $chksta;
- my $staer4;
- my $chkdur;
- my $classsta;
- my $type;
- my $noname = "";
- my $date = strftime("%Y%m%d%H%M%S",localtime(time));
-
- if( !open( IN,"zcat $in|" )) {
- die( "Could not open file $infile:$!" );
- }
- if ( !open( LOG,">>$log_dir/Report.out" )) {
- die( "Could not open file $log_dir/Report.out:$!" );
- }
- if ( $dir=~/miepcdr/i ) {
- print LOG "$date: MIEP::$in is reading.\n";
- } elsif ( $dir =~/httpcdr/i ) {
- print LOG "$date: HTTP::$in is reading.\n";
- } # Record reading file # Please look for /home/md/log/Report.out;
-
- while( <IN> ) {
- chomp;
- next if /^Record.*\(.*\).*\"MiepPush.+\"/ .. /End/; # ignore the part.
- if ( /^Record.*\(\d+\).*\"MiepPull.+"/ ) {
- $start=$_;
-
- } elsif ( /\s+\"(.*)\"\s\=\s\"(.*)\"/ ) {
- $status = $1;
- $code = $2;
- $recordhash{$status} = $code;
-
- } elsif ( /(^End.*Record.*\(\d+\))/ ) {
- $end = $_;
-
- if ( exists $recordhash{"url"} ) {
-
- $url = $recordhash{"url"};
- $dur = $recordhash{"urlDurationTime"};
-
- $type = &checktypeofurl($url,$conf); # It return 0--SmartIN, 1--MMS, 2--CP, 3--IOM.
- $chksta = &checkStatus($type,\%recordhash); # It return the full Status, e.g. 200 OK.
-
- $staer4 = (split / +/,$chksta)[0];
- $chkdur = &checkDuration($dur); # It return urlDuration 0 -- 0s - 2s, 1 -- 2s - 5s, 2 -- 5s - 10s, 3 -- 10s - 20s, 4 -- 20+s.
- $classsta = &duratonclassbystatus($staer4,$durtab,\%recordhash,$dir,$start,$end,$type,$ysdate,$error_dir); # It return 0 -- less then 400, 1 -- greater than or equal to 400
-
- $durtab->[$type]->[$classsta]->[$chkdur] = $durtab->[$type]->[$classsta]->[$chkdur] + 1; # Counting
- if ( $staer4 >= 400 && $type != 1 ) {
-
- if ( $url =~/favicon.ico/i ){
- $noneurl = 0;
- } elsif ($url =~/Microsoft\-Server\-ActiveSync/i ) {
- $noneurl = 1;
- } else {
- $noneurl = 2;
- }
- if (exists $notab->[$type]->[$noneurl] ) {
- $notab->[$type]->[$noneurl] = $notab->[$type]->[$noneurl] + 1;
- } # Counting
- next if $url =~/favicon.ico/i or $url =~/Microsoft\-Server\-ActiveSync/i; # ignore a Report while if match the specified url.
- }
- }
- undef %recordhash; # Empty for A report.
- if ( exists $statab->{$type}->{$chksta} ){
- $statab->{$type}->{$chksta} = $statab->{$type}->{$chksta} + 1;
- } else {
- $statab->{$type}->{$chksta} = 1;
- } # Counting
- }
- }
- close(IN);
- }
- sub checktypeofurl(){
- my ( $url, $conf ) = @_; # Peference the variable, Please look for line 154.
- my $ret = 3; # return 3 while the url can not be match
- if ( $url =~/mms\.smartone.*|mms2\.smartone.*|mms4\.smartone.*|mms9\.smartone.*|disc1\.smartone\-vodafone\.com|202\.140\.96\.(?=92|208|46|97|227|47).+|10\.16\.96\.(?=76|79|93|94|187|188|180|137|167).+|202\.140\.106\.64/i ) {
- $ret = 1;
- } elsif ( $url =~/.*\.smartone\.com\.hk|.*\.smartone\-vodafone\.com\.hk|.*\.smartone\-vodafone\.com|202\.140\.(?=86\.+|96\.+|106\.+|93\.+|83\.+|76\.+|74\.+|100\.+|104\.+)/i ) {
- $ret = 0;
- } else {
-
- foreach my $key (keys %{$conf}) { # return 2 while if find url in config file. Please look for line 55.
- my $hash = $conf->{$key};
-
- if ( $url =~/http\:\/\/$key/ ) {
-
- for my $key2 ( keys %{$hash} ) {
-
- if ( $url =~/$key2/ ) {
- $ret = 2;
-
- }
- }
- }
- }
- }
- return $ret;
- }
- sub checkDuration() {
- my $duration = shift; # Peference the variable, Please look for line 160.
- my $durms;
- my $ret;
- if ( $duration=~/(\d+)/ ) { # $1 Units is millisecond;
- $durms = sprintf("%f", $1 / 1000); # change units to second, the question is it will rounding
- $durms =~s/(\d+)\.\d+/$1/; # Retaining only the integer
- if ( $durms < 2 ) {
- $ret = 0;
- } elsif ( $durms < 5 ) {
- $ret = 1;
- } elsif ( $durms < 10 ) {
- $ret = 2;
- } elsif ( $durms < 20 ) {
- $ret = 3;
- } elsif ( $durms >= 20 ) {
- $ret = 4;
- }
- }
- return $ret;
- }
- sub checkStatus() {
- my ($ty,$hash) = @_; # Peference the variable, Please look for line 157.
- my $code;
- my $letter;
- my $codeinfo;
- my $sta = $hash->{"returnCode"} || $hash->{"STATUS"}; # $sta can equal $hash->{"returnCode"} or $hash->{"STATUS"}
- my $stainfo = $hash->{"STATUSCODE"};
- my $size = $hash->{"contentSizeFromTerminal"};
- if ( $sta =~/^(\d{3})\s(.*)/) {
- $code = $1;
- $codeinfo =$2;
- if ( $ty == 1 ) { # if type equal MMS, Please look for line 156.
- if ($code == 500) {
- if ( hex($size) < 54 ) {
- $codeinfo = "read reply to null recipient";
- }
- }
- }
- if ($codeinfo=~/^\W.*/) {
- $codeinfo = $code." url link";
- } else {
- $letter = lc($codeinfo);
- $letter =~s/\s+$//g;
- $letter=~s/(?<=\b|^)(\w)/\u$&/g; # the first letters change Capital.
- $codeinfo = $code." ".$letter;
- }
- } elsif ( $sta =~/^(\d{3})/ ) {
- $code = $1;
- if ( $stainfo =~/(.*)/i ) {
- $letter = lc($1);
-
- if ($letter =~/^\W.*/) {
- $letter = "url link";
- }
-
- $letter =~s/\s+$//g;
- $letter=~s/(?<=\b|^)(\w)/\u$&/g; # the first letters change Capital.
- $codeinfo = $code." ".$letter;
- }
- }
- return $codeinfo;
- }
- sub duratonclassbystatus(){
-
- my ($sta, $tab, $hash,$dir,$start,$end,$type,$ysdate,$error_dir) = @_; # Peference the variable, Please look for line 161.
- my $ret; # return var
-
- if ( $sta < 400 ) {
- $ret = 0;
-
- } else {
- $ret = 1;
- &exporterrorcase($hash,$dir,$type,$ysdate,$start,$end,$error_dir); # generate the error report.
- }
- return $ret;
- }
- sub exporterrorcase(){
- my ($hash,$dir,$ty,$ysdate,$start,$end,$error_dir) = @_; # Peference the variable, Please look for line 321.
- my $sta = $hash->{"returncode"} || $hash->{"STATUS"};
- my $typecdr=(split(/\//, $dir))[-1];
- my $errname = "";
- if ( $ty == 0 ) {
- $errname = "error_".$typecdr."_smtin".$ysdate;
- } elsif ( $ty == 1 ) {
- $errname = "error_".$typecdr."_mms".$ysdate;
- } elsif ( $ty == 2 ) {
- $errname = "error_".$typecdr."_cp".$ysdate;
- } elsif ( $ty == 3 ) {
- $errname = "error_".$typecdr."_iom".$ysdate;
- }
-
- if ( !open (OUT,">>$error_dir/$errname" ) ) { die("Can not write the error file $errname:$!"); }
- print OUT $start."\n";
-
- foreach (keys %{$hash}) {
- print OUT "\"".$_."\" = \"".$hash->{$_}."\"\n";
- }
-
- print OUT $end."\n";
-
- close(OUT);
- }
- sub printhtmlreporttable(){
-
- my ($durtab,$statab,$notab,$dir) = @_; # Peference the variable, Please look for line 31.
-
- my $smnto = 0; # number of smtin total
- my $mmsto = 0; # number of mms total
- my $cpsto = 0; # number of cp total
- my $iomto = 0; # number of iom total
- my $percg = 0; # Calculating the percentage
- my $typne = ""; # type of the name
- my @perto = (0 .. 3); # array of total
- my @type = sort keys %{$statab};
-
- for my $t (@type) {
- for my $c ( 0 , 1 ) {
-
- for my $d ( 0 .. 4 ) {
-
- if ( $t == 0 ) {
- $smnto = $durtab->[$t]->[$c]->[$d] + $smnto;
- } elsif ( $t == 1 ) {
- $mmsto = $durtab->[$t]->[$c]->[$d] + $mmsto;
- } elsif ( $t == 2 ) {
- $cpsto = $durtab->[$t]->[$c]->[$d] + $cpsto;
- } elsif ( $t == 3 ) {
- $iomto = $durtab->[$t]->[$c]->[$d] + $iomto;
- }
-
- }
- }
- }
- my @total = ($smnto,$mmsto,$cpsto,$iomto); # the diff total
-
- for my $n (@perto){
- $perto[$n] = 0;
- } # initiatize the $perto[$n] = 0;
-
- # ------------------------------- html ------------------------------
- print "<html>";
-
- # ------- head ---------
- print "<head>";
- print "<title>Percentage Report </title>";
- print "<style type=text/css>";
- print 'table {background-color: #000000;}';
- print 'td {background-color: #FFFFFF;text-align:center; font size=10pt ;font fac=Arial}';
- print 'th {background-color: #FFFFFF;text-align:center; font size=10pt ;font fac=Arial}';
- print "</style>";
- print "</head>";
- # ------- head ---------
- # ------- body ---------
- print "<body>";
- print "<center>";
- print "<table border='0' cellspacing='1' width='65%'>";
-
- for my $t (@type) {
-
- $typne = "SmarToneiN" if $t == 0;
- $typne = "MMS" if $t == 1;
- $typne = "CP" if $t == 2;
- $typne = "IOM" if $t == 3;
-
- my $distp = 1; # as a distinction between success or failure of the temporary
- my $sucto = 0; # number of sucessfull total
- my $faito = 0; # number of failure total
-
- print "<tr><th colspan='3'><p align='center'><b>$typne</b></th></tr>";
- print "<tr><th colspan='3'><p align='left'><b>Successful Status</b></th></tr>";
- foreach my $status (sort keys %{$statab->{$t}} ) {
- $percg = sprintf("%.2f",$statab->{$t}->{$status} / $total[$t] * 100).'%';
- my $cutst = (split / +/,$status)[0]; # split of the first part as a judge to of success or failure;
-
- if ( $cutst < 400 ) {
- $sucto = $statab->{$t}->{$status} + $sucto;
- print "<tr><td width='50%'><p align='left'>$status</td><td width='30%'><p align='center'>$statab->{$t}->{$status}</td><td width='20%'><p align='center'>$percg</td>";
-
- } else {
-
- if ( $t != 1 ) {
- print "<tr><th colspan='3'><p align='left'> </th></tr><tr><th colspan='3'><p align='left'><b>Failed Status (Not including the fail case of favicon.ico and Microsoft-Server-ActiveSync)</b></th></tr>" if $distp == 1;
- } else {
- print "<tr><th colspan='3'><p align='left'> </th></tr><tr><th colspan='3'><p align='left'><b>Failed Status</b></th></tr>" if $distp == 1;
- }
- $faito = $statab->{$t}->{$status} + $faito;
- print "<tr><td width='50%'><p align='left'>$status</td><td width='30%'><p align='center'>$statab->{$t}->{$status}</td><td width='20%'><p align='center'>$percg</td></tr>";
- $distp = 2;
- }
-
- }
-
- for my $c ( 0 , 1 ) {
-
- print "<tr><th colspan='3'><p align='left'> </th></tr><tr><th colspan='3'><p align='left'><b>Duration (Success)</b></th></tr>" if $c ==0;
- print "<tr><th colspan='3'><p align='left'> </th></tr><tr><th colspan='3'><p align='left'><b>Duration (Fail)</b></th></tr>" if $c ==1;
-
- for my $d ( 0 .. 4 ) {
- print "<tr><td width='50%'><p align='left'>Duration 0s - 2s</td>" if $d == 0;
- print "<tr><td width='50%'><p align='left'>Duration 2s - 5s</td>" if $d == 1;
- print "<tr><td width='50%'><p align='left'>Duration 5s - 10s</td>" if $d == 2;
- print "<tr><td width='50%'><p align='left'>Duration 10s - 20s</td>" if $d == 3;
- print "<tr><td width='50%'><p align='left'>Duration 20 +s</td>" if $d == 4;
- $percg = sprintf("%.2f",$durtab->[$t]->[$c]->[$d] / $total[$t] * 100).'%';
- print "<td width='30%'><p align='center'>$durtab->[$t]->[$c]->[$d]</td><td width='20%'><p align='center'>$percg</td></tr>";
- }
- }
- $perto[0] = sprintf("%.2f",$sucto / $total[$t] * 100).'%';
- $perto[1] = sprintf("%.2f",$faito / $total[$t] * 100).'%';
- $perto[2] = sprintf("%.2f",$notab->[$t]->[0] / $total[$t] * 100).'%';
- $perto[3] = sprintf("%.2f",$notab->[$t]->[1] / $total[$t] * 100).'%';
-
- print "<tr><th colspan='3'><p align='left'> </th></tr><tr><td width='50%'><p align='left'><b>$typne Total Successful</b></td><td width='30%'><p align='center'>$sucto</td><td width='20%'><p align='center'>$perto[0]</td></tr>";
- print "<tr><td width='50%'><p align='left'><b>$typne Total Failed</b></td><td width='30%'><p align='center'>$faito</td><td width='20%'><p align='center'>$perto[1]</td></tr>";
- if ( $t != 1 ) {
- print "<tr><td width='50%'><p align='left'><b>$typne Total favicon.ico</b></td><td width='30%'><p align='center'>$notab->[$t]->[0]</td><td width='20%'><p align='center'>$perto[2]</td></tr>";
- print "<tr><td width='50%'><p align='left'><b>$typne Total Microsoft-Server-ActiveSync</b></td><td width='30%'><p align='center'>$notab->[$t]->[1]</td><td width='20%'><p align='center'>$perto[3]</td></tr>";
- }
- print "<tr><td width='50%'><p align='left'><b>$typne Total</b></td><td width='30%'><p align='center'>$total[$t]</td><td width='20%'><p align='center'> </td></tr>";
-
- }
- print "</center></body>";
- # ------- body ---------
-
- print "</html>";
- # ------------------------------- html ------------------------------
-
- @type = ();
- @perto = ();
- }
复制代码
[ 本帖最后由 hyoryeo 于 2009-10-28 17:59 编辑 ] |
|