Professional Web Applications Themes

Help: Program to read from EOR end of last read? - Linux / Unix Administration

Hello, I am looking for a way to look at a log from the last time I've read it and look for a specific string. For example, I have a logfile called /var/adm/messages, and I am looking for an error string like "ERROR: Loss of sync" I know how to open the log and search for the error, but I want to avoid reporting the same error multiple times. For instance, if I find an error at 01:00 AM and send a page to a support team, when the program/script runs every 10 minutes, I don't want to page again ...

  1. #1

    Default Help: Program to read from EOR end of last read?

    Hello,
    I am looking for a way to look at a log from the last time I've read
    it and look for a specific string. For example, I have a logfile
    called /var/adm/messages, and I am looking for an error string like
    "ERROR: Loss of sync"

    I know how to open the log and search for the error, but I want to
    avoid reporting the same error multiple times. For instance, if I find
    an error at 01:00 AM and send a page to a support team, when the
    program/script runs every 10 minutes, I don't want to page again if I
    already alerted for it.

    In the past I would catch the error and place it in a temp file, I
    would then check for the indeticle line log in a log.page file. If
    they were the same I already alerted for it and would do nothing. If
    it was differnt I would append this error to the log.page file and
    would send out a page/alert.

    I know there are logwatcher programs that can read fro EOF (End of
    file) or EOR (End of Read). How can I do this in something like Perl,
    sh/ksh/csh scripting, or Java? The program will be running on a unix
    system.

    Any suggestions would be appreciated. Thanks.

    snoopy_@excite.com Guest

  2. #2

    Default Re: Help: Program to read from EOR end of last read?

    In Java the DataInputStream class has the skipBytes(int n) method. If
    you keep track of how many bytes you read so far (as of the last time)
    you can skip them pretty quickly with this method then next time you go
    in.

    ducnbyu@aol.com Guest

  3. #3

    Default Re: Help: Program to read from EOR end of last read?

    com wrote: 

    I've done something similar with Perl's File::ReadBackwards to scan the
    log entries that were written since my previous read. Every 10 minutes,
    your program could read the log backwards, then convert the log entry
    timestamps to epoch times to see if they fall in the 10-minute window
    since the program started. Assuming log entries are marshalled so no
    out-of-sequence timestamps occur, the program can stop reading as soon a
    timestamp occurs that isn't within your 10 minute window.

    hth,
    --
    Charles DeRykus



    Charles Guest

  4. #4

    Default Re: Help: Program to read from EOR end of last read?

    com wrote: 

    This may do what you want:

    #!/usr/bin/perl
    use warnings;
    use strict;
    use Fcntl ':seek';

    ( my $prog = $0 ) =~ s!.*/!!;
    my $log_file = '/var/adm/messages';
    my $config_file = "$ENV{HOME}/.$prog";
    my $temp_file = "$ENV{HOME}/$prog.temp";

    my $string = 'ERROR: Loss of sync';

    # get the previous position
    my $position = do {
    open my $cfg, '<', $config_file;
    fileno $cfg && <$cfg> || 0
    };

    open my $tmp, '>>', $temp_file or die "Cannot open '$temp_file' $!";
    open my $log, '<', $log_file or die "Cannot open '$log_file' $!";

    # reset the position if the file is now smaller
    $position = 0 if $position > -s $log;

    seek $log, $position, SEEK_SET or die "Cannot seek '$log_file' $!";

    while ( my $line = <$log> ) {
    print $tmp $line if $line =~ /$string/;
    }

    open my $cfg, '>', $config_file or die "Cannot open '$config_file' $!";
    print tell $log;

    __END__



    John
    --
    use Perl;
    program
    fulfillment
    John Guest

Similar Threads

  1. how can i read a dreamweaver program?
    By tricetta in forum Macromedia Dynamic HTML
    Replies: 1
    Last Post: October 11th, 02:52 AM
  2. something to read
    By Paweł in forum Photography
    Replies: 7
    Last Post: February 27th, 10:19 AM
  3. File system get auto change from read-write to read-oly
    By RC in forum Linux / Unix Administration
    Replies: 1
    Last Post: October 23rd, 10:51 PM
  4. Read & Read/Write Groups
    By Damien Harrod in forum Sun Solaris
    Replies: 1
    Last Post: July 22nd, 01:52 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139