Need to check logs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to check logs
# 1  
Old 09-02-2014
Need to check logs

I have nearly 25+ tail commands which we need to verify the logs if there is any errors on current or previous date with time. I need this to be automate and send email to me with details. Please help me on this.
# 2  
Old 09-02-2014
Please post example input data, and expected output.

Please post an example of the code you have so far.
# 3  
Old 09-02-2014
Sample code for log checks

I need exact output in emails, like i am checking logs via tail command with current dates or previous if there is any error we need to report.


Code:
#!/bin/sh
mailcontents=mailbody
while read line
do
    if [ ! -f $line ]; then
        echo "The file $line doesn't exist. Continuing with the next file..." >> $mailcontents
        echo "============" >> $mailcontents 
        continue
    else
        last_mod_time=$(perl -MFile::stat -e "print stat('$line')->mtime") # this line checks the log's last modification time and converts it to Unix's epoch
        last_24_hours=$(perl -e 'print time-86400') # this line returns the epoch for the current timestamp minus 24 hours

        if [ $last_mod_time -lt $last_24_hours ]; then
            echo "Log $line has NOT been updated in the last 24 hours" >> $mailcontents
        else
            echo "Log $line was updated during the last 24 hours" >> $mailcontents
        fi
    
        tail -100 $line > filename.txt

        error=$(grep ERROR filename.txt | wc -l) # We look for the lines containing the word "ERROR" in the filename.txt file.
                                             # Then we redirect the output to the wc -l command that will count the number
                                             # of lines where the word ERROR appears.
    
        if [ $error -gt 0 ]; then # If this condition is satisfied, that means the word ERROR appeared at least once in
                                  # the log that's being examined in the current loop.
            echo "ERROR found in log $line" >> $mailcontents
        else
            echo "No errors found in $line" >> $mailcontents
        fi
    fi
echo "============" >> $mailcontents
done < filename.txt

if [ -s $mailcontents ]; then
    mail -s "Batch Job check on the server `hostname` - $(date +'%A %B %d, %Y')" nasirhussain4u4@gmail.com < $mailcontents
fi

# rm $mailcontents filename.txt # Delete auxiliary files when we're done.


Last edited by Don Cragun; 09-02-2014 at 09:48 PM.. Reason: Add CODE tags.
# 4  
Old 09-04-2014
So - that's the script. What's the error or the unfulfilled requirement?

P.S.: One thing that jumps to my eyes is that you repeatedly overwrite filename.txt in the middle of reading from it.
# 5  
Old 09-04-2014
Yes,
Code:
        tail -100 $line > filename.txt

changes the file that the loop currently reads from:
Code:
done < filename.txt

Should certainly be different e.g.
Code:
        tail -100 $line > tmp.txt
        error=$(grep ERROR tmp.txt | wc -l)

Or, in one stroke without a tmp file
Code:
        error=$(tail -100 "$line" | grep -c "ERROR")

# 6  
Old 09-05-2014
You could replace this:

Code:
        tail -100 $line > filename.txt

        error=$(grep ERROR filename.txt | wc -l) # We look for the lines containing the word "ERROR" in the filename.txt file.
                                             # Then we redirect the output to the wc -l command that will count the number
                                             # of lines where the word ERROR appears.
    
        if [ $error -gt 0 ]; then # If this condition is satisfied, that means the word ERROR appeared at least once in
                                  # the log that's being examined in the current loop.
          echo "ERROR found in log $line" >> $mailcontents

with
Code:
   if tail -100 $line | grep -q ERROR
   then
        echo "ERROR found in log $line" >> $mailcontents

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To check if a file is open and in use (logs are being written to it)

Hello Experts, I need to write a shell script to check if a file is open and something is being written to it. I want to know how OS handles it. I checked with lsof command but it is not working. For a test I did this. while true; do echo `date` >>abc.txt; done then I checked lsof |... (5 Replies)
Discussion started by: shekhar_4_u
5 Replies

2. Shell Programming and Scripting

Script to check response time from nginx logs

Hi, My goal is to monitor the response time from the access logs of nginx server. I am using gawk to print the needed fields - 'response time' and 'name of the service' from nginx logs. Command: gawk '($6 ~ /cloudservice/) {print $10, $6}' access.log Output: 0.645 /nc/cloudservice... (6 Replies)
Discussion started by: nshah11
6 Replies

3. Shell Programming and Scripting

Any utility or tool to check logs

HI, In our project daily we need to check for some errors in around 45-50 folders. Please let me know if there is any utility tool using which 1 can check each folder and file for error. Use: Monday we are checking if there were any error in files generated on Monday Tuesday to Wednesday we... (7 Replies)
Discussion started by: ankush_mehra
7 Replies

4. UNIX for Advanced & Expert Users

AIX idea needed to check the logs updated date and time

Hi with the help of Gabriel canepa, i have just edited filename only in his code. The help which i got and he helped is 1) I have around 22 logs and each log should be updated in the last 24 hours from the current timestamp. 2) It should check for ERROR message (not error,Error) in the log and... (2 Replies)
Discussion started by: Kalaihari
2 Replies

5. UNIX for Dummies Questions & Answers

Check for updation/error/stuck of logs

Hi All, I'm a newbie in Linux Programming.:) Got some 500 processes running and I have around 20-30 logs updating for every 2mins on a server. The logs which i'm referring usually contains book name,run ids(not PID's),process name etc etc. I'm interested in finding out whether some particular... (1 Reply)
Discussion started by: Nand Kishor
1 Replies

6. Shell Programming and Scripting

Script to check logs

I have 5 log files under different directores . say for eg abc under /home/dir1 , xyz under home/dir2 . is there a script that i can run from say /home that searchers all these files for string or combination of strings and write to a file eg search file by timestamp|keyword o/p in a file (6 Replies)
Discussion started by: Nevergivup
6 Replies

7. Solaris

archive logs mount point space check script

I have the below shell script which is checking /archlog mount point space on cappire(solaris 10) server. When the space usage is above 80% it should e-mail. When i tested this script it is working as expected. -------------------------------------------------------------------------... (0 Replies)
Discussion started by: dreams5617
0 Replies

8. Solaris

logs to check

Hi all i want to know what are the logs we need to check when the server is down and how to resolve to make server UP? please help me with this (8 Replies)
Discussion started by: vkav
8 Replies

9. Shell Programming and Scripting

How to check whether logs are updating or not?

how to check whether logs are updating or not in unix is there any built in command or function ? (1 Reply)
Discussion started by: mail2sant
1 Replies

10. UNIX for Dummies Questions & Answers

is it possible to check logs in UNIX who deleted the files?

Hello, is it possible to check logs in UNIX who deleted the files? Is there logs in UNIX besides .sh_history? (1 Reply)
Discussion started by: james_falco
1 Replies
Login or Register to Ask a Question