Results 1 to 11 of 11
  1. #1
    Join Date
    Jun 2014
    Posts
    3

    Post Unanswered: script to check status in logfile

    Hello friends,
    Hope someone can help me to write a script shell:
    I have a log file with data:
    Name server1 down 00:10:00
    Name server1 up 00:20:00
    Name server2 down 00:12:00
    Name server2 up 00:22:00
    Name server1 down 00:23:00 ( i ve down without up so error )
    Name server3 down 00:11:00
    Name server3 up 00:23:00

    For each line servername,
    I must to check this logfile every 10 min, if after Down I have Up so all is "OK".
    If one server is Down with out Up from more than 5 min so " KO"
    I have to put the name of server Down in the error file.

    thks
    george
    Last edited by georg2014; 06-29-14 at 04:16. Reason: more info

  2. #2
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    How frequently are you going to run this, and how do you intend to notify whomever?

  3. #3
    Join Date
    Jun 2014
    Posts
    3
    Quote Originally Posted by kitaman View Post
    How frequently are you going to run this, and how do you intend to notify whomever?
    Hi
    Check log file every 10 min,
    If server is down more than 5 min notify in a outfile and send a mail
    thks for your help
    george

  4. #4
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Code:
    while read name server status time  
    do                                  
            echo $status $time >$server 
    done <logfile                                                   
    nowhr=`date +"%H"`                                              
    nowmm=`date +"%M"`                                              
    nowss=`date +"%S"`                                              
    now=`expr $nowhr \* 3600 + $nowmm \* 60 + $nowss`               
    list=`ls server?`                                               
    for server in $list                                             
    do                                                              
            while read status time                                  
            do                                                      
            if [ "$status" = "down" ]                               
            then                                                    
                    hh=`echo $time |cut -d":" -f1`                  
                    mm=`echo $time |cut -d":" -f2`                  
                    ss=`echo $time |cut -d":" -f3`                  
                    timesec=`expr $hh \* 3600 + $mm \* 60 + $ss`    
                    diff=`expr $now - $timesec`                     
                    if [ $diff > 300 -o $timesec > $now ]           
                    then                                            
                            echo $server has been down since $time  
                    fi                                              
            fi                                                      
            done <$server                                           
    done
    Now you explain to me how it works, what resources it uses and its limitations.

  5. #5
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713
    Quote Originally Posted by georg2014 View Post
    Hello friends,
    . . . I have a log file with data:
    Name server1 down 00:10:00
    Name server1 up 00:20:00
    Name server2 down 00:12:00
    Name server2 up 00:22:00
    Name server1 down 00:23:00 ( i ve down without up so error )
    Name server3 down 00:11:00
    Name server3 up 00:23:00
    . . .
    thks
    george
    The time stamp of your logfile is incomplete therefore it's flawed.
    What happens when it's down one night and is up the next day?

    But if you do not care about the time-stamp, try this:

    Code:
    awk '{ s[$2]=$3";"$4 }
    END {
    for(i in s){split(s[i],w,";"); 
      if (w[1]=="down"){print i" has been down since "w[2]}}
    }
    ' logfile.txt
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  6. #6
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    The time stamp of your logfile is incomplete therefore it's flawed.
    The only time that produces an invalid result is if the server goes down between 23:56 and midnight. It will be reported as down for over 5 minutes (the down time is greater than the now time.)
    This could also be avoided by setting the cron job to run at the 09,19,29,39,49, and 59 minute marks each hour.

  7. #7
    Join Date
    Jun 2014
    Posts
    3

    Post

    To avoid the day I try this :
    while read field1 field2 field3 field4
    do
    if [ "${field2}" == "down" ]
    then
    > "$DIR"$field1
    elif [ "${field2}" == "up" ]
    then
    rm "$DIR"$field1 2>/dev/null
    continue
    fi
    done < fin.log
    ************************************************** ******** --check file in directroy if file in more 10 min i send a mail
    find $DIR -type f -cmin +10
    what do you think about this way?

  8. #8
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Cool

    @kitaman
    My intention was not to contest/critique your code but rather point out that there might be time-stamp issues when a day rolls over to the next.
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  9. #9
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    George, did you post a reply, then delete it. I received an email notification. Anyway, your solution works other than "up"/"down" is field3 not field2, and the server is field2 not field1. Also, you are testing for the file being older than 10 minutes, but the specs say 5.
    To avoid the day I try this :
    Code:
    while read field1 field2 field3 field4
    do
      if [ "${field2}" == "down" ]
     then
        > "$DIR"$field1
     elif [ "${field2}" == "up" ]
    then
    rm "$DIR"$field1  2>/dev/null   
    continue
      fi
    done < fin.log
    *************** --check file in directroy if file in more 10 min i send a mail
    find $DIR -type f -cmin +10
    what do you think about this way?
    ***************
    You should try using meaningful names rather than field1,field2 etc, which is really no different than 1,2,3,4. The mistakes you made would be obvious, and it will be easier to read five years from now.
    Last edited by kitaman; 07-01-14 at 11:13.

  10. #10
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    Quote Originally Posted by kitaman View Post
    George, did you post a reply, then delete it.
    ...Georges post got trapped by the automoderation filter..... it has now been removed from there and should be visible
    I'd rather be riding on the Tiger 800 or the Norton

  11. #11
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    After some more thought, the solution that George has presented does not work because the timestamp on the file is not the time that the server stopped, but the time the job was run.
    My solution works if the server is down for a short period of time, but fails if the server is down for more than 24 hours.
    To make this work properly, I think that either the input to the log file should be 'tee'ed to this script, or the script be allowed to write a marker to the log file to show that records up to this point have been processed.
    Maybe we could avoid marking the file if we save the number of this type of record in the log file, and skip that many records each run. Using this method ensures that each entry in the log file is only processed once.

Posting Permissions

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