-3

Am running this perl script ever day on my server and am getting the below output for the script. I am trying to modify the script to also include the current hour as part of the output. How can I go about doing this?

This is my current script:

#!/usr/bin/perl
#Prism Performance log Parser

use strict;

my $cbal_total;
my $cbal_count =0;
my $stck_total = 0;
my $stck_count =0;
my $chg_total = 0;
my $chg_count =0;
my $rmac_total = 0;
my $rmac_count =0;
my $rmd_total = 0;
my $rmd_count =0;
my $cbalT;

my $stckT;
my $rmacT;
my $rmdT;
my $chgT;
my $total;
my $count;
my $cbal;
my $stck;
my $hour;


my $chg;
my $rmac;
my $rmd;

my $lesThresh=0;
my $gtThresh=0;
my $stck_lesThresh=0;
my $stck_gtThresh=0;
my $rmd_lesThresh=0;
my $rmd_gtThresh=0;
my $chg_lesThresh=0;
my $chg_gtThresh=0;
my $rmac_lesThresh=0;
my $rmac_gtThresh=0;

my %CheckBal;
my %SubTypeCheck;
my %charging;
my %remoteAct;
my %remoteDct;

my $chgkey;
my $cbalkey;
my $stckkey;
my $rmackey;
my $rmdkey;

my @value;
my $ct;
my $component;
my $component2;

while (my $line =<>) {
    chomp;
    s/\r//g;
    my @f = split(/\|/, $line);
    my $i;
    $hour = substr($f[0],11,2);

    for ($i==0;$i<=100; $i++) {
        if (($f[$i]=~m/CBAL/) && ($f[$i]!~m/CBAL,100/)) {
            $component="CBAL";
            $cbal=$f[$i];
            $cbalkey="$hour,$component";
            if (!exists($CheckBal{$cbalkey})) {
                $cbal_count=0;
                $cbal_total=0;
                $lesThresh=0;
                $gtThresh=0;
            }
            $cbalT = substr($cbal,index($cbal,",T=")+3,index($cbal,"\n"));
            if ($cbalT <= 300) {
                $lesThresh++;
            }else{
                $gtThresh++
            }
            $cbal_total +=$cbalT;
            $cbal_count ++;
            #$CheckBal{$cbalkey} =($cbal_total).",".($cbal_count).",".($lesThresh).",".$gtThresh.",".($cbal_total/$cbal_count);
            $CheckBal{$cbalkey} =($cbal_count).",".($cbal_total).",".($cbal_total/$cbal_count).",".($lesThresh).",".$gtThresh;

        }elsif($f[$i]=~m/STCK/){
            $component="STCK";
            $stck=$f[$i];
            $stckkey="$hour,$component";
            if (!exists($SubTypeCheck{$stckkey})) {
                $stck_count=0;
                $stck_total=0;
                $stck_lesThresh=0;
                $stck_gtThresh=0
            }
            $stckT = substr($stck,index($stck,",T=")+3,index($stck,"\n"));
            if ($stckT <= 300) {
                $stck_lesThresh++;
            }else{
                $stck_gtThresh++
            }
            $stck_total +=$stckT;
            $stck_count ++;
#           $SubTypeCheck{$stckkey} =($stck_total).",".($stck_count).",".($stck_lesThresh).",".$stck_gtThresh.",".($stck_total/$stck_count);
            $SubTypeCheck{$stckkey} =($stck_count).",".($stck_total).",".($stck_total/$stck_count).",".($stck_lesThresh).",".$stck_gtThresh;

        }elsif($f[$i]=~m/CHG/){
            $component="CHG";
            $chg=$f[$i];
            $chgkey="$hour,$component";
            if (!exists($charging{$chgkey})) {
                $chg_count=0;
                $chg_total=0;
                $chg_lesThresh=0;
                $chg_gtThresh=0
            }
            $chgT = substr($chg,index($chg,",T=")+3,index($chg,"\n"));
            if ($chgT <= 300) {
                $chg_lesThresh++;
            }else{
                $chg_gtThresh++
            }
            $chg_total +=$chgT;
            $chg_count ++;
#           $charging{$chgkey} =($chg_total).",".($chg_count).",".($chg_lesThresh).",".$chg_gtThresh.",".($chg_total/$chg_count);
            $charging{$chgkey} =($chg_count).",".($chg_total).",".($chg_total/$chg_count).",".($chg_lesThresh).",".$chg_gtThresh;
        }elsif(($f[$i]=~m/RMAC/) && ($f[$i]!~m/RMAC,96/)){
            $component="RMAC";
            $rmac=$f[$i];
            $rmackey="$hour,$component";
            if (!exists($remoteAct{$rmackey})) {
                $rmac_count=0;
                $rmac_total=0;
                $rmac_lesThresh=0;
                $rmac_gtThresh=0
            }
            $rmacT = substr($rmac,index($rmac,",T=")+3,index($rmac,"\n"));
            if ($rmacT <= 300) {
                $rmac_lesThresh++;
            }else{
                $rmac_gtThresh++
            }
            $rmac_total +=$rmacT;
            $rmac_count ++;
#           $remoteAct{$rmackey} =($rmac_total).",".($rmac_count).",".($rmac_lesThresh).",".$rmac_gtThresh.",".($rmac_total/$rmac_count);
            $remoteAct{$rmackey} =($rmac_count).",".($rmac_total).",".($rmac_total/$rmac_count).",".($rmac_lesThresh).",".$rmac_gtThresh;

        }elsif(($f[$i]=~m/RMD/) && ($f[$i]!~m/RMD,96/)){
            $component="RMD";
            $rmd=$f[$i];
            $rmdkey="$hour,$component";
            if (!exists($remoteDct{$rmdkey})) {
                $rmd_count=0;
                $rmd_total=0;
                $rmd_lesThresh=0;
                $rmd_gtThresh=0
            }
            $rmdT = substr($rmd,index($rmd,",T=")+3,index($rmd,"\n"));
            if ($rmdT <= 300) {
                $rmd_lesThresh++;
            }else{
                $rmd_gtThresh++
            }
            $rmd_total +=$rmdT;
            $rmd_count ++;
#           $remoteDct{$rmdkey} =($rmd_total).",".($rmd_count).",".($rmd_lesThresh).",".$rmd_gtThresh.",".($rmd_total/$rmd_count);
            $remoteDct{$rmdkey} =($rmd_count).",".($rmd_total).",".($rmd_total/$rmd_count).",".($rmd_lesThresh).",".$rmd_gtThresh;

        }


    }

}


print "Balance Check\n";
print "Hour,Task,Total Transactions,Total Processing Time/ms,Average TPS/ms,<300 ms,>300 ms\n";
unless(%CheckBal){
    print "No record found for STCK\n";
}
foreach (sort keys %CheckBal){
    print $_.",".$CheckBal{$_}."\n";
}

Current Output:

Balance Check
Hour,Task,Total Transactions,Total Processing Time/ms,Average TPS/ms,<300 ms,>300 ms
06,CBAL,17987,13131831,730.073441930283,4295,13692
07,CBAL,17911,13579801,758.182178549495,3970,13941
08,CBAL,228,98643,432.644736842105,100,128

Desired Output:

Balance Check
date,Hour,Task,Total Transactions,Total Processing Time/ms,Average TPS/ms,<300 ms,>300 ms
20140528,06,CBAL,17987,13131831,730.073441930283,4295,13692
20140528,07,CBAL,17911,13579801,758.182178549495,3970,13941
20140528,08,CBAL,228,98643,432.644736842105,100,128
David W.
  • 105,218
  • 39
  • 216
  • 337
Aju
  • 31
  • 3
  • I think you forgot to actually include the output. If you want to include time information in your perl script, you should look into the [localtime() function](http://perldoc.perl.org/functions/localtime.html). – Mr. Llama May 29 '14 at 16:02
  • hi added all informations – Aju May 29 '14 at 16:11
  • 2
    If all that you want is for someone to fix your script for you, have you considered hiring a Perl programmer? – scozy May 29 '14 at 16:17
  • 2
    I can see why you didn't do any work. This script is pretty awful. Poor implementation, poor variable names, and no comments. I've at least formatted the indentations to help highlight the logic. It appears that `%CheckBal` contains the entire formatted line to print, so this variable has to be modified itself. It looks like the date is part of the data, but without the data, I can't say how to even begin. Add `use warnings;` because I see two errors in the program itself. The `use warnings;` will point them out. – David W. May 29 '14 at 18:38

1 Answers1

0

To get the current hour, use Time::Piece

 use strict;
 use warnings;

 use Time::Piece;

 print localtime->strftime("%H"), "\n";

How to insert that functionality into your script is an exercise for you.

Miller
  • 34,962
  • 4
  • 39
  • 60