#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Monitoring Log Files


    Hi,

    I'm new to Perl and trying to write the following script:

    • it will monitor a log file called stats.log

    • if a number reaches a certain limit, a message gets written to a temp file


    the log file has data in this format:

    servername|2013-02-23 6:06:12|alloccount=150|used=11
    servername|2013-02-23 6:06:12|alloccount=150|used=19

    so i'm trying to do it with this code:

    Code:
    #!/usr/bin/perl -w
    
    $countlimit = 75; #this is the limit that if reached, the temp file is then written to
    
    $testfile = "mytest.txt"; # the temp file to write to
    
    $LOGFILE = "stats.log"; # open log file
    open(LOGFILE) or die("Could not open log file.");
    foreach $line (<LOGFILE>) {
        
        ($server_name, $date, $alloccount, $used) = split(/\|/,$line); #sets 4 variables, from each line
        $processnumber = substr($used, 9, 10); # take the $used variable, cut the 9th and 10th digit and place in the $processnumber variable.
    	if ($processnumber > $ctxuplimit) { # if the $processnumber variable is more than the $countlimit variable, write to the $testfile variable.
    	print "hey its over the limit!" > $testfile;
    }
    does this look like i'm going in the right direction. also could anyone tell me how to get it to ignore the "\n" new line character at the end of the $line variable?

    thanks for any advice
  2. #2
  3. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,252
    Rep Power
    1810
    That's certainly the right idea, but you should develop the habit of using strict mode from the beginning.

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    my $countlimit = 75;
    my $logfile = "stats.log";
    
    open my $fh, "<", $logfile or die "cannot open $logfile: $!";
    
    foreach my $line (<$fh>) {
       chomp $line;   # removed the newline character from the line
       my ($server_name, $date, $alloccount, $used) = split(/\|/,$line);
       my $processnumber = substr($used, 9, 10); # take the $used variable, cut the 9th and 10th digit and place in the $processnumber variable
    
       # don't know where this $ctxuplimit is defined
       # is this supposed to be $countlimit?
    
       if ($processnumber > $ctxuplimit) {
          print "hey its over the limit!" > $testfile;
       } 
    }
    Use 'my' to declare variables when they are first used. That prevents spelling errors and other scope problems when you are working. If you run into compile errors you don't understand, ask us here.

    chomp is the function that removes a newline character. The default is to remove the newline type for the operating system you are working on currently. That can be changed. For example, you may need to process a Windows file on a Unix operating system. I'll provide details if you need them.
  4. #3
  5. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,252
    Rep Power
    1810
    Just wanted to correct something from this morning. Hadn't taken a good look at your print function.

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    use Data::Dumper;
    
    my $countlimit = 75;
    my $logfile = "stats.log";
    my $testfile = "mytest.txt";
    
    open my $infh, "<", $logfile or die "cannot open $logfile: $!";
    open my $outfh, ">", $testfile or die "cannot open $testfile for output: $!";
    
    foreach my $line (<$infh>) {
       chomp $line;
       
       my ($server_name, $date, $alloccount, $used) = split(/\|/,$line);
       my $processnumber = substr($used, 6);
       print "processnumber: $processnumber\n";
    
       if ($processnumber > $countlimit) {
          print $outfh "hey its over the limit!";
       } 
    }
    I think you meant to print to a filehandle at the end of your script. Also, the $used variable substring wouldn't have worked with the example you gave since the offset was too long for the given string. No need to specify the length in the substring if you are going to the end.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by keath
    Just wanted to correct something from this morning. Hadn't taken a good look at your print function.

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    use Data::Dumper;
    
    my $countlimit = 75;
    my $logfile = "stats.log";
    my $testfile = "mytest.txt";
    
    open my $infh, "<", $logfile or die "cannot open $logfile: $!";
    open my $outfh, ">", $testfile or die "cannot open $testfile for output: $!";
    
    foreach my $line (<$infh>) {
       chomp $line;
       
       my ($server_name, $date, $alloccount, $used) = split(/\|/,$line);
       my $processnumber = substr($used, 6);
       print "processnumber: $processnumber\n";
    
       if ($processnumber > $countlimit) {
          print $outfh "hey its over the limit!";
       } 
    }
    I think you meant to print to a filehandle at the end of your script. Also, the $used variable substring wouldn't have worked with the example you gave since the offset was too long for the given string. No need to specify the length in the substring if you are going to the end.

    this is great. its working for me thanks! i really appreciate that help!

    just one more question, i need to put in place the functionality to do this:

    i need to monitor the log file by server name for a specific number of entries.....so for example, I will monitor serverx for 20 entries in a row.

    so if the (my $countlimit 75) is reached say 20 entries in a row, an alert is fired. if not, no alert is fired.

    as a new entry is found, the oldest entry is dropped (from what i'm guessing should be an array?)

    is this possible? would i use an array?
  8. #5
  9. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,252
    Rep Power
    1810
    You should be able to tail the last 20 lines of the log and check for the data easily.

    You may have some other constraints you're looking for, not sure.

IMN logo majestic logo threadwatch logo seochat tools logo