[Webinar] Streamline your web hosting managementRegister Today


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

Posted on 2010-03-25
Medium Priority
Last Modified: 2012-05-09
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?

Question by:YankeeFan03
  • 2
  • 2

Expert Comment

ID: 28578928

Author Comment

ID: 28586790
that's the first half but how do i get it to send an email when a specific word pops up.

Expert Comment

ID: 28596027
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 = "XXXjdemerjian@co.com"
'objMessage.To = "XXXjdemerjian@co.com"
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 _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "XXXyourSMTPserver"

'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 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.


Accepted Solution

YankeeFan03 earned 0 total points
ID: 28705083
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.

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Batuhan Cetin In this article I will be guiding through the process of removing a failed DC metadata from Active Directory (hereafter, AD) using the ntdsutil tool in a Windows Server 2003 environment. These steps are not necessary in a Win…
Learn about cloud computing and its benefits for small business owners.
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
Suggested Courses

611 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question