VBScript to look for certain error in logs and send out an email/alert

Posted on 2008-10-13
Last Modified: 2012-08-13
I have a log which contains errors. I want to look for http 500 errors within these logs. So I need a script/monitoring system for windows which looks at these logs every hour and if it find "http 500" in error.log it needs to send out an alert/email to a distribution list. What is the best way to do that?
Question by:rheasam
  • 2
LVL 14

Expert Comment

ID: 22774120
As an alternative to running a script to search through the log files, you can get the system to email at the moment the error occurs.  This can be done through IIS.  You will have to create a custom 500.asp page which you will set your site to (in the event of a 500 error.  When a page is reached that would otherwise create the error, the custom page would be launched.  You can have the page capture some stats about the error and email those to you.  The page could display something to the effect that the page could not be loaded due to an error and will be looked at shortly - or whatever you want to say.
The 500.asp page could have code from something like this (or my example attached);
and mixed with the email part from here (or my example attached);

The attached code works well for me and is a combination of the above two links.  Save the attached as 500-100.asp and place within the website.  Like I mentioned, alter the IIS configuration to redirect 500-100 errors to this new page.  Two good things about this are that one, you will get far more data captured when the error happened and two, why wait till the script runs - why not act on the error right away.
<%@ Language=VBScript %>



' System Name: Error Handling


' Page Name: 500-100.asp


' Page Description:

' This page provides the functionality to handle 500-100 errors that

' happen on a website. These errors are logged via email.


'  REV #      DATE     BY   COMMENTS

' 1.0.000   07/25/2002 AMT  Initial write


Dim mstrCustRefID

Dim ASPErr

Dim strErrorMessage

'Create an id the customer can use when they call up.

mstrCustRefID = Session.SessionID & "-" & Hour(Now) & Minute(Now) & Second(Now)

Set ASPErr = Server.GetLastError


Dim objFS

Dim objFile

strErrorMessage = "Error at " & Now & vbCrLf &_

                  "CustomerRefID "  & mstrCustRefID & vbCrLf &_

                  "Session ID " & Session.SessionID & vbCrLf &_

                  "RequestMethod " & Request.ServerVariables("REQUEST_METHOD") & vbCrLf &_

                  "ServerPort " & Request.ServerVariables("SERVER_PORT") & vbCrLf &_

                  "HTTPS " & Request.ServerVariables("HTTPS") & vbCrLf &_

                  "LocalAddr "  & Request.ServerVariables("LOCAL_ADDR") & vbCrLf &_

                  "HostAddress "  & Request.ServerVariables("REMOTE_ADDR") & vbCrLf &_

                  "UserAgent " & Request.ServerVariables("HTTP_USER_AGENT") & vbCrLf &_

                  "URL " &  Request.ServerVariables("URL") & vbCrLf

If ASPErr.ASPCode        <> "" Then strErrorMessage = strErrorMessage & "Error #:      " & ASPErr.ASPCode & vbCrLf

If ASPErr.Number         <> 0  Then strErrorMessage = strErrorMessage & "COM Error #:  " & ASPErr.Number & " (" & Hex (ASPErr.Number) & ")" & vbCrLf

If ASPErr.Source         <> "" Then strErrorMessage = strErrorMessage & "Source:       " & ASPErr.Source & vbCrLf

If ASPErr.Category       <> "" Then strErrorMessage = strErrorMessage & "Category:     " & ASPErr.Category & vbCrLf

If ASPErr.File           <> "" Then strErrorMessage = strErrorMessage & "File:         " & "//" & Request.ServerVariables ("SERVER_NAME") & ASPErr.File & vbCrLf

If ASPErr.Line           <> 0  Then strErrorMessage = strErrorMessage & "Line, Column: " & ASPErr.Line & ", " & ASPErr.Column & vbCrLf

If ASPErr.Description    <> "" Then strErrorMessage = strErrorMessage & "Description:  " & ASPErr.Description & vbCrLf

If ASPErr.ASPDescription <> "" Then strErrorMessage = strErrorMessage & "ASP Desc:     " & ASPErr.ASPDescription  & vbCrLf

strErrorMessage = strErrorMessage & "FormData " & Request.Form & vbCrLf &_

                  "HTTP Headers: " & vbCrLf &_

                  Replace(Request.ServerVariables("ALL_HTTP"),vbLf,vbCrLf) & vbCrLf

Dim objCDOMail : Set objCDOMail = Server.CreateObject("CDO.Message")

objCDOMail.From     = ""

objCDOMail.To       = ""

objCDOMail.Subject  = "An error was found in the website"

objCDOMail.TextBody = strErrorMessage


Set objCDOMail = Nothing




	<title>An Error Has Occured</title>


<body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0" class="bgPageBackColor">

<!---------- START - Body Content --------------------------------> 


<h3>An error has occurred.</h3>

<table cellpadding="3">


	<td colspan="2">

		An error has occured in this application. All information about this error has been logged and the support team<br>

		has been notified of the error. You may contact the helpdesk at: 555-555-5555<br><br>

		We apologize for any inconvenience.




	<td colspan=2>Please refer to this number when speaking with support:     <b><%=mstrCustRefID%></b></td>




<!---------- END   - Body Content -------------------------------->



Open in new window


Author Comment

ID: 22868290
what if my error is not 500 it is something else and the error no is not know,
LVL 14

Accepted Solution

rejoinder earned 500 total points
ID: 22870970
If they are not getting the 500 page, then which error page are they seeing?

Status      Message
400      Bad Request
401.1      Unauthorized: Logon Failed
401.2      Unauthorized: Logon Failed due to server configuration
401.3      Unauthorized: Unauthorized due to ACL on resource
401.4      Unauthorized: Authorization failed by filter
401.4      Unauthorized: Authorization failed by filter
401.5      Unauthorized: Authorization failed by ISAPI/CGI app  
403.1      Forbidden: Execute Access Forbidden
403.1      Forbidden: Execute Access Forbidden
403.2      Forbidden: Read Access Forbidden
403.3      Forbidden: Write Access Forbidden
403.4      Forbidden: SSL required
403.5      Forbidden: SSL 128 required
403.6      Forbidden: IP address rejected
403.7      Forbidden: Client certificate required
403.8      Forbidden: Site access denied
403.9      Access Forbidden: Too many users are connected
403.10      Access Forbidden: Invalid Configuration
403.11      Access Forbidden: Password Change
403.12      Access Forbidden: Mapper Denied Access
404      File Not Found
405      Method Not Allowed
406      Not Acceptable
407      Proxy Authentication Required
412      Precondition Failed
414      Request-URI Too Long
500      Internal Server Error
501      Not Implemented
502      Bad Gateway

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

910 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now