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

Posted on 2008-10-13
Medium Priority
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     = "website@yourdomain.com"
objCDOMail.To       = "distributiongroup@yourdomain.com"
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 1500 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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

621 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