Computing.Net > Forums > Unix > scan log file by a search string

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

scan log file by a search string

Reply to Message Icon

Name: pretty21
Date: June 8, 2008 at 22:38:13 Pacific
OS: unix
CPU/Ram: 2gb
Product: solaris
Comment:

Hi, need your help please. I want to scan our daily log file (its increasing daily) for the Error message encountered.

After scanning, if ERROR is found, it will send email alert.

Thanks in advance




Sponsored Link
Ads by Google

Response Number 1
Name: Curt R
Date: June 11, 2008 at 12:50:36 Pacific
Reply:

***Begin Script***

#!/bin/ksh

#delete file created by the grep command (start fresh)
rm /path/filename
#error checking
if [ $# -eq 0 ]
then
echo "Usage is: $0 logfile error_message"
exit 1
elif [ $# -gt 2 ]
then
echo "Usage is: $0 logfile error_message"
exit 1
else
#perform scan for error message, put error message in #text file if found
grep "$2" "$1" > /path/filename
fi
#send "alert" email to admin if error msg found
#or send "No errors" email if none found
if [ -s "/usr/home/curtr/test2" ]
then
mail -s "Error Alert!" admin@domain.net < /path/filename
else
echo "No Errors" | mail -s "No Errors in Log file" admin@domain.net
fi
exit 0

***End Script***

Where:
path = path to file created by the script
filename = the name of the file created by script
admin@domain.net = your (valid) email address

I tested this and it works on my OpenBSD system.


0

Response Number 2
Name: Curt R
Date: June 11, 2008 at 12:56:34 Pacific
Reply:

Forgot to add, this should accept a file in another folder as input but to be sure, test it.

If not, you'll want to run this from the directory the log file is located in and have the " grep "$2" "$1" > /path/filename" create the file in the same folder (as the script and log file) as well.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: scan log file by a search string

Script to scan files for a string www.computing.net/answers/unix/script-to-scan-files-for-a-string/7532.html

How to keep the log file a fixed size www.computing.net/answers/unix/how-to-keep-the-log-file-a-fixed-size/2436.html

Two scripts pipe to a log file www.computing.net/answers/unix/two-scripts-pipe-to-a-log-file/3599.html