473,395 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Perl code changes

I have a Perl script that I run and the out come as showing below:
Expand|Select|Wrap|Line Numbers
  1. Log chain 1:
  2.   Reading: server 2\08120700.mls
  3.   Reading: server 2\08120900.mls
  4.   Reading: server 1\08121100.mls
  5. Log chain 2:
  6.   Reading: server 2\08120700.mls
  7.   Reading: server 2\08120900.mls
  8.   Reading: server 2\08121100.mls
  9. Log chain 3:
  10.   Reading: server 3\08120700.mls
  11.   Reading: server 3\08120900.mls
  12.   Reading: server 3\08121100.mls
  13. ================================
  14. # Successful Syncs:    2535
  15. # Failed Syncs:          34
  16. ================================
  17. Total:                2569
  18.  
  19. Average Sync Time:    22.05 sec
  20.  
However, it’s only on print screen and I would like to do the
following:
1. Save it as an excel file
2. Look only for error with 1000 to 1012
Expand|Select|Wrap|Line Numbers
  1. ================================
  2. I would like the out come in the new saved spreadsheet as following:
  3. ================================
  4. # Successful Syncs:    2535
  5. ================================
  6. # Event ID 1000:          30
  7. # Event ID 1006:          4
  8. Total # Failed Syncs:          34
  9. ================================
  10. Total:                2569
  11.  
  12. Average Sync Time:    22.05 sec
  13.  
I’m very new on this Perl scripting please help on it
Below is the code:
-------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. # summarize_ml_syncs.pl - Read all MobiLink logs from the current directory and/or all
  2. #                         immediate subdirectores, and provide a simple summary report.
  3. #
  4. #
  5.  
  6. use strict;
  7. use Time::Local;
  8.  
  9. my $Debug = 0;        # Set to 1 to see verbose debug information
  10. my $ReadFiles = 1;  # Set to 0 to show how files will be read, without actually reading.
  11. my $LOG;
  12.  
  13. my $NO_MORE = 'NO_MORE';
  14.  
  15. my $TSPat = /(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/;
  16.  
  17. #---------------------------------------------------------------------
  18. { package MLLog;
  19. #---------------------------------------------------------------------
  20. #    @MLLog::ISA = qw( Obj );
  21.  
  22.     sub new {
  23.     my $proto  = shift();
  24.     my $path   = shift();
  25.  
  26.     my $class = ref($proto) || $proto;
  27.  
  28.     my $self = {};
  29.     bless( $self, $class );
  30.  
  31.     $self->{MLLOG_FIRST_FILE} = undef;
  32.     $self->{MLLOG_PREV_FILES} = {};
  33.     $self->{MLLOG_NEXT_FILES} = {};
  34.     $self->{MLLOG_CURR_FILE}  = undef;
  35.     $self->{MLLOG_PREV_TS}    = undef;
  36.     $self->{MLLOG_CHAINS}     = [];
  37.     $self->{MLLOG_CURR_CHAIN} = undef;
  38.     $self->{MLLOG_IS_DONE}    = 0;
  39.  
  40.     $self->{MLLOG_SYNCS}        = {};
  41.     $self->{MLLOG_NUM_SYNCS}    = 0;
  42.     $self->{MLLOG_NUM_OK_SYNCS}    = 0;
  43.     $self->{MLLOG_NUM_FAILED_SYNCS} = 0;
  44.     $self->{MLLOG_TOTAL_SYNC_TIME}    = 0;
  45.  
  46.     if( -d $path ) {
  47.         $self->AddFiles( $path, 2 );    # 2 is the maximum depth supported.
  48.         $self->{MLLOG_DIR} = $path;
  49.     } else {
  50.         $self->AddFile( undef, $path, undef );
  51.         $self->{MLLOG_DIR} = undef;
  52.     }
  53.  
  54.     return( $self );
  55.     };
  56.  
  57.     sub debug {
  58.     my $self = shift();
  59.  
  60.     if( $Debug ) {
  61.         printf( @_ );
  62.     }
  63.     }
  64.  
  65.     sub Dir {
  66.     my $self = shift();
  67.  
  68.     return( $self->{MLLOG_DIR} );
  69.     }
  70.  
  71.     sub CurrFile {
  72.     my $self = shift();
  73.     my $value = shift();
  74.  
  75.     if( defined( $value ) ) {
  76.         $self->{MLLOG_CURR_FILE} = $value;
  77.     }
  78.     return( $self->{MLLOG_CURR_FILE} );
  79.     }
  80.  
  81.     sub Chains {
  82.     my $self = shift();
  83.  
  84.     return( $self->{MLLOG_CHAINS} );
  85.     }
  86.  
  87.     sub CurrChain {
  88.     my $self  = shift();
  89.     my $value = shift();
  90.  
  91.     if( defined( $value ) ) {
  92.         $self->{MLLOG_CURR_CHAIN} = $value;
  93.     }
  94.     return( $self->{MLLOG_CURR_CHAIN} );
  95.     }
  96.  
  97.     sub IsDone {
  98.     my $self  = shift();
  99.     my $value = shift();
  100.  
  101.     if( defined( $value ) ) {
  102.         $self->{MLLOG_IS_DONE} = $value;
  103.     }
  104.     return( $self->{MLLOG_IS_DONE} );
  105.     }
  106.  
  107.     sub Syncs {
  108.     my $self = shift();
  109.  
  110.     return( $self->{MLLOG_SYNCS} );
  111.     }
  112.  
  113.     sub TotalSyncTime {
  114.     my $self = shift();
  115.  
  116.     return( $self->{MLLOG_TOTAL_SYNC_TIME} );
  117.     }
  118.  
  119.     sub NumSyncs {
  120.     my $self  = shift();
  121.  
  122.     return( $self->{MLLOG_NUM_SYNCS} );
  123.     }
  124.  
  125.     sub NumOKSyncs {
  126.     my $self  = shift();
  127.  
  128.     return( $self->{MLLOG_NUM_OK_SYNCS} );
  129.     }
  130.  
  131.     sub NumFailedSyncs {
  132.     my $self  = shift();
  133.  
  134.     return( $self->{MLLOG_NUM_FAILED_SYNCS} );
  135.     }
  136.  
  137.     sub PrevFiles {
  138.     my $self = shift();
  139.  
  140.     return( $self->{MLLOG_PREV_FILES} );
  141.     }
  142.  
  143.     sub NextFiles {
  144.     my $self = shift();
  145.  
  146.     return( $self->{MLLOG_NEXT_FILES} );
  147.     }
  148.  
  149.     sub PrevFile {
  150.     my $self = shift();
  151.     my $file = shift();
  152.     my $value = shift();
  153.  
  154.     if( defined( $value ) ) {
  155.         $self->debug( "Prev file for %s is %s\n", $file, $value );
  156.         ${ $self->PrevFiles }{ lc( $file ) } = $value;
  157.     }
  158.     return( ${ $self->PrevFiles }{ lc( $file ) } );
  159.     }
  160.  
  161.     sub NextFile {
  162.     my $self = shift();
  163.     my $file = shift();
  164.     my $value = shift();
  165.  
  166.     if( defined( $value ) ) {
  167.         if( !defined( ${ $self->NextFiles }{ lc( $file ) } ) ) {
  168.         $self->debug( "Next file for %s is %s\n", $file, $value );
  169.         }
  170.         ${ $self->NextFiles }{ lc( $file ) } = $value;
  171.     }
  172.     return( ${ $self->NextFiles }{ lc( $file ) } );
  173.     }
  174.  
  175.     sub AddFile {
  176.     my $self      = shift();
  177.     my $dir       = shift();
  178.     my $file      = shift();
  179.     my $prev_file = shift();
  180.  
  181.     if( defined( $dir ) && !( -d $dir ) ) {
  182.         die( "$dir is not a directory" );
  183.     }
  184.  
  185.     $file = "$dir\\$file";
  186.     $prev_file = "$dir\\$prev_file";
  187.  
  188.     if( !( -f $prev_file ) ) {
  189.         $prev_file = undef;
  190.     }
  191.  
  192.     if( defined( $prev_file ) ) {
  193.         $self->debug( "Adding file %s prev is %s\n", $file, $prev_file );
  194.     } else {
  195.         $self->debug( "Adding file %s\n", $file );
  196.  
  197.         # Track the root files of chains.
  198.         push( @{ $self->Chains }, lc( $file ) );
  199.         $self->debug( "Chain %d starts at %s\n", scalar( @{ $self->Chains } ), $file );
  200.     }
  201.  
  202.     # Link files by pointing to the previous file.
  203.     $self->PrevFile( $file, $prev_file );
  204.  
  205.     # Determine the next file(s).
  206.     # Not efficient, but thorough.
  207.     $self->NextFile( $file, undef );
  208.  
  209.     my $f;
  210.     foreach $f (keys( %{ $self->PrevFiles } )) {
  211.         $prev_file = $self->PrevFile( $f );
  212.         if( defined( $prev_file ) ) {
  213.         $self->NextFile( $prev_file, $f );
  214.         }
  215.     }
  216.     }
  217.  
  218.     sub AddFiles {
  219.     my $self  = shift();
  220.     my $dir   = shift();
  221.     my $depth = shift();
  222.     my $e;
  223.     my @dirs;
  224.  
  225.     opendir( D, $dir ) || die( "Unable to open directory $dir for read" );
  226.     while( $e = readdir( D ) ) {
  227.         my $path = $dir . '\\' . $e;
  228.  
  229.         if( -d $path ) {
  230.         if( $e ne '.' && $e ne '..' ) {
  231.             $self->debug( "DIR: %s\n", $e );
  232.             push( @dirs, $e );
  233.         }
  234.         } else {
  235.         my $F;
  236.  
  237.         $self->debug( "FILE: %s\n", $path );
  238.         open( $F, "<$path" ) || die( "Unable to open $path for read" );
  239.         my $first_line = readline( $F );
  240.         close( $F );
  241.         $self->debug( "First line: %s\n", $first_line );
  242.  
  243.         if( $first_line =~ m/SQL Anywhere MobiLink Server Version/ ) {
  244.             $self->AddFile( $dir, $e, undef );
  245.             $self->debug( "Found file: $e, no prev\n" );
  246.         } elsif( $first_line =~ m/^[IWE]\. \d\d\d\d-.*/ ) {
  247.             if( $first_line =~ m/old output file "[^"]+" has been renamed to file "([^"]+)"/i ) {
  248.             my $prev = lc( $1 );
  249.             # Strip the directory, since it is redundant and prevents hash matches.
  250.             $prev =~ s/.*\\(.*)/$1/;
  251.             $self->debug( "Prev: %s\n", $prev );
  252.             # Add the file.
  253.             $self->AddFile( $dir, $e, $prev );
  254.             $self->debug( "Found file: $path, prev is $prev\n" );
  255.             } else {
  256.             $self->debug( "Ignoring %s\n", $path );
  257.             }
  258.         } else {
  259.             $self->debug( "Ignoring %s\n", $path );
  260.         }
  261.         }
  262.     }
  263.     closedir( D );
  264.  
  265.     foreach $e (@dirs) {
  266.         if( $depth > 1 ) {
  267.         $self->AddFiles( $e, $depth - 1 );
  268.         }
  269.     }
  270.     }
  271.  
  272.     sub GetNextFile {
  273.     my $self = shift();
  274.     my $prev_chain = $self->CurrChain;
  275.  
  276.     if( $self->IsDone ) {
  277.         return( undef );
  278.     }
  279.  
  280.     if( !defined( $self->CurrFile ) ) {
  281.         $self->debug( "Next file is first in chain 0\n" );
  282.         $self->CurrChain( 0 );
  283.         $self->CurrFile( ${ $self->Chains }[ $self->CurrChain ] );
  284.     } else {
  285.         $self->{MLLOG_CURR_FILE} = $self->NextFile( $self->CurrFile );
  286.         if( !defined( $self->CurrFile ) ) {
  287.         $self->{MLLOG_CURR_CHAIN} = $self->CurrChain + 1;
  288.         if( $self->CurrChain < scalar( @{ $self->Chains } ) ) {
  289.             $self->CurrFile( ${ $self->Chains }[ $self->CurrChain ] );
  290.             $self->debug( "Starting new chain %d\n", $self->CurrChain );
  291.         } else {
  292.             $self->CurrFile( undef );
  293.             $self->debug( "Last file in all chains\n" );
  294.         }
  295.         } else {
  296.         $self->debug( "In the middle of chain %s\n", $self->CurrChain );
  297.         }
  298.     }
  299.  
  300.     if( $prev_chain ne $self->CurrChain && $self->CurrChain < scalar( @{ $self->Chains } ) ) {
  301.         printf( "Log chain %d\:\n", ( 1 + $self->CurrChain ) );
  302.     }
  303.  
  304.     if( defined( $self->CurrFile ) ) {
  305.         my $f;
  306.         if( $self->Dir ne '' ) {
  307.         $f = $self->Dir . '\\' . $self->CurrFile;
  308.         } else {
  309.         $f = $self->CurrFile;
  310.         }
  311.     } else {
  312.         $self->debug( "No more files to open.\n", $self->CurrFile );
  313.         $self->IsDone( 1 );
  314.     }
  315.     return( $self->CurrFile );
  316.     }
  317.  
  318.     sub TimeStamp {
  319.     my $self = shift();
  320.     my ( $y, $m, $d, $h, $min, $s ) = @_;
  321.     my $ts = Time::Local::timegm( $s, $min, $h, $d, $m - 1, $y - 1900 );
  322.     return( $ts );
  323.     }
  324.  
  325.     sub StartSync {
  326.     my $self = shift();
  327.     my $sid  = shift();
  328.     my $ts   = shift();
  329.  
  330.     ${ $self->Syncs }{ $sid } = $ts;
  331.     }
  332.  
  333.     sub EndOKSync {
  334.     my $self = shift();
  335.     my $sid  = shift();
  336.     my $ts   = shift();
  337.     my $start_ts = ${ $self->Syncs }{ $sid };
  338.  
  339.     if( defined( $start_ts ) ) {
  340.         my $duration = ( $ts - $start_ts );
  341.         $self->debug( "Sync %s has duration %d seconds\n", $sid, $duration );
  342.         # Only count complete syncs.
  343.         $self->{MLLOG_NUM_SYNCS} += 1;
  344.         $self->{MLLOG_TOTAL_SYNC_TIME} += $duration;
  345.         $self->{MLLOG_NUM_OK_SYNCS} += 1;
  346.  
  347.         ${ $self->Syncs }{ $sid } = undef;
  348.     } else {
  349.         $self->debug( "Sync %s has an unknown start\n", $sid );
  350.     }
  351.     }
  352.  
  353.     sub EndFailedSync {
  354.     my $self = shift();
  355.     my $sid  = shift();
  356.     my $ts   = shift();
  357.     my $start_ts = ${ $self->Syncs }{ $sid };
  358.  
  359.     if( defined( $start_ts ) ) {
  360.         my $duration = ( $ts - $start_ts );
  361.         $self->debug( "Failed sync %s has duration %d seconds\n", $sid, $duration );
  362.         # Only count complete syncs.
  363.         $self->{MLLOG_NUM_SYNCS} += 1;
  364.         $self->{MLLOG_TOTAL_SYNC_TIME} += $duration;
  365.         $self->{MLLOG_NUM_FAILED_SYNCS} += 1;
  366.  
  367.         ${ $self->Syncs }{ $sid } = undef;
  368.     } else {
  369.         $self->debug( "Failed sync %s has an unknown start\n", $sid );
  370.     }
  371.     }
  372.  
  373.     sub ReadFiles {
  374.     my $self = shift();
  375.     my $file;
  376.     my $line;
  377.     my $ts;
  378.  
  379.     while( $file = $self->GetNextFile ) {
  380.         printf( "  Reading: $file\n" );
  381.         if( $ReadFiles ) {
  382.         open( LOG, "<$file" ) || die( "Unable to open $file for read" );
  383.         while( $line = readline( LOG ) ) {
  384.             if( $line =~ m/(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)\. <(\d+)> Request from/ ) {
  385.             my ( $y, $m, $d, $h, $min, $s, $sid ) = ( $1, $2, $3, $4, $5, $6, $7 );
  386.             $ts = $self->TimeStamp( $y, $m, $d, $h, $min, $s );
  387.             $self->StartSync( $sid, $ts );
  388.             } elsif( $line =~ m/(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)\. <(\d+)> Synchronization complete/ ) {
  389.             my ( $y, $m, $d, $h, $min, $s, $sid ) = ( $1, $2, $3, $4, $5, $6, $7 );
  390.             $ts = $self->TimeStamp( $y, $m, $d, $h, $min, $s );
  391.             $self->EndOKSync( $sid, $ts );
  392.             } elsif( $line =~ m/(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)\. <(\d+)> Synchronization failed/ ) {
  393.             my ( $y, $m, $d, $h, $min, $s, $sid ) = ( $1, $2, $3, $4, $5, $6, $7 );
  394.             $ts = $self->TimeStamp( $y, $m, $d, $h, $min, $s );
  395.             $self->EndFailedSync( $sid, $ts );
  396.             }
  397.         }
  398.         }
  399.         close( LOG );
  400.     }
  401.     }
  402.  
  403.     sub Report {
  404.     my $self = shift();
  405.     my $average_sync_time;
  406.  
  407.     if( $self->NumSyncs > 0 ) {
  408.         $average_sync_time = ( 0.0 + $self->TotalSyncTime ) / $self->NumSyncs;
  409.     } else {
  410.         $average_sync_time = 0.0;
  411.     }
  412.  
  413.     printf( "================================\n" );
  414.     printf( "# Successful Syncs: %7d\n", $self->NumOKSyncs );
  415.     printf( "# Failed Syncs:     %7d\n", $self->NumFailedSyncs );
  416.     printf( "---------------------------\n" );
  417.     printf( "Total:              %7d\n", $self->NumSyncs );
  418.     printf( "\n" );
  419.     printf( "Average Sync Time: %8.2f sec\n", $average_sync_time );
  420.     printf( "================================\n" );
  421.     }
  422. }
  423.  
  424. my $Logs = new MLLog( '.' );
  425.  
  426. $Logs->ReadFiles();
  427. $Logs->Report();
  428. print( "Done.\n" );
  429.  
----------------------------------------------------<<< end of the code
Thanks,
Mark
Jan 22 '09 #1
3 2159
KevinADC
4,059 Expert 2GB
Look into one of the Excel modules for creating the Excel file. As far as finding the error maybe you can point out in all that code where the script reads/searches for those errors.
Jan 22 '09 #2
The file I’m pulling the data from is mls. Excel isn’t very important to me as long as I can read the output and be able to see the account for all the errors 1000 thru 1012. There is no where in the code it shows where it searches for any error. I have posted the code on my question, if it’s not clear please let me know and I’ll email it to you to review it if you would like.

Thanks
Jan 22 '09 #3
KevinADC
4,059 Expert 2GB
Sorry, I can't help you. Maybe someone else will.
Jan 22 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

42
by: Fred Ma | last post by:
Hello, This is not a troll posting, and I've refrained from asking because I've seen similar threads get all nitter-nattery. But I really want to make a decision on how best to invest my time....
31
by: surfunbear | last post by:
I've read some posts on Perl versus Python and studied a bit of my Python book. I'm a software engineer, familiar with C++ objected oriented development, but have been using Perl because it is...
2
by: Kai Thorsrud | last post by:
Hi I'm currently into converting a perl linux app into a .Net windows service. The application monitors our syslog log files to capture i.p adress changes on some of our routers having dynamic...
0
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
2
by: kelly | last post by:
Hi, I don't have a code to show you, what I need are references or algorithms so that I'm in a right track. By the way, thanks for introducing me the array or arrays. Now I can continue my...
82
by: Edward Elliott | last post by:
This is just anecdotal, but I still find it interesting. Take it for what it's worth. I'm interested in hearing others' perspectives, just please don't turn this into a pissing contest. I'm in...
6
by: surfivor | last post by:
I may be involved in a data migration project involving databases and creating XML feeds. Our site is PHP based, so I imagine the team might suggest PHP, but I had a look at the PHP documentation...
9
by: peter | last post by:
Hi, this is not stinky bait. If you take it that way, please dont respond.. I have been away from UNIX software for quite awhile and want to get back into it. I liked "C" but Java seems like...
2
by: stirfries | last post by:
Hello Perl Gurus, I am new to Perl programming but not new to programming in general. Perl really fascinates me with its cryptic syntax. I have been analyzing a Perl system to understand and...
5
by: Bill H | last post by:
Hi I have some perl code that lets me request different things, images, audio, etc and it will output the correct mime type header and "stream" the file to a browser (instead of a hard link to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.