Solved

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

Posted on 2008-10-13
3
417 Views
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?
0
Comment
Question by:rheasam
  • 2
3 Comments
 
LVL 14

Expert Comment

by:rejoinder
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);
http://www.codeproject.com/KB/asp/trap_asp_sql_error.aspx
and mixed with the email part from here (or my example attached);
http://www.asp101.com/samples/viewasp.asp?file=email_html.asp

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
Response.Clear
 
 
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
objCDOMail.Send
Set objCDOMail = Nothing
 
%>
<html>
<head>
	<title>An Error Has Occured</title>
</head>
 
<body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0" class="bgPageBackColor">
 
<!---------- START - Body Content --------------------------------> 
<center><br><br>
<h3>An error has occurred.</h3>
<table cellpadding="3">
<tr>
	<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>
</tr>
<tr>
	<td colspan=2>Please refer to this number when speaking with support:     <b><%=mstrCustRefID%></b></td>
</tr>
</table><br><br>
</center>
<!---------- END   - Body Content -------------------------------->
</body>
</html>

Open in new window

0
 

Author Comment

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

Accepted Solution

by:
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
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

856 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