how to "tail" a log and generate an email alert triggered by text in the log?

I am tyring to setup a process in which in Windows 2003 i can "tail" a log file and have an email sent to me if it detects a word i designate, such as "error"

Anyone have any ideas or produts that can do this in Windows 2003?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

YankeeFan03Author Commented:
that's the first half but how do i get it to send an email when a specific word pops up.
Okay you will have to work on this a bit but the general idea works because I have it in use right now.  I am sure there are developers that could make a nicer solution but here you go.  Solution has three steps:

Step 1 - you need to parse the output of the tail command on a schedule to look for the text string in question.  You could use the "type" command for this with the "findstr" command to specify what you are looking for.  The general command would be "type MyTextFile.txt | findstr/c:ERROR - this would take the output of the type command and find the string ERROR.

Step 2 - dump the text string into a text file (when it exists) for the system to email to you.  Both step 1 and 2 are combined in this command which is formatted to be run from a batch file.  You may need to tweak this just a bit because I took it from a working example and modified it on the fly for you.  The general idea works well.

for /f "tokens=1,2,3*" %%a in ('type error.txt ^|findstr /c:ERROR!') do (echo I found an error in the log.) >>"C:\EmailBody.txt"

Step 3 - now that you have a file "EmailBody.txt" that was generated by the presence of the text string in the log you use it to trigger an email to you.  But first you need to have a routine that checks for it to exist.  So in a batch file that line would be something like "If <file> exist goto EMAIL ROUTINE.  

The following vbscript uses the contents of EmailBody.txt as (you guess it) the email body.  Note where I put XXX because you need to change these for your system.  Copy the following text into something like email.vbs and run it from a command prompt with "cscript email.vbs"

'These constants are defined to make the code more readable
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
'Open the file for reading
Set f = fso.OpenTextFile("C:\EmailBody.txt", ForReading)
'The ReadAll method reads the entire file into the variable BodyText
BodyText = f.ReadAll
'Close the file
Set f = Nothing
Set fso = Nothing

Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Alert on server"
objMessage.Sender = "XXXMyalertingsystem"
objMessage.To = ""
'objMessage.To = ""
objMessage.From = "XXXMyAlertingSystem"
objMessage.TextBody = BodyText

'==This section provides the configuration information for the remote SMTP server.
'==Normally you will only change the server name or IP.

objMessage.Configuration.Fields.Item _
("") = 2

'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("") = "XXXyourSMTPserver"

'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("") = 25


'==End remote SMTP server configuration section==



So what you have with this is the beginnings to your system.  One shortcoming is that you'll need to incorporate a way to delete the EmailBody.txt file after it has emailed you once.  Let me know how you do.

YankeeFan03Author Commented:
thank you but this a little more than i was planning on doing :)  I was hoping for something a little more straight forward and builtin.  I did find a product called PA Server Pro that has a log file monitor option included that does exactly what i wanted.  Thanks for your efforts above but i have chosen an already created solution.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Server 2003

From novice to tech pro — start learning today.