Link to home
Start Free TrialLog in
Avatar of rheasam
rheasam

asked on

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

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?
Avatar of rejoinder
rejoinder
Flag of Canada image

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

Avatar of rheasam
rheasam

ASKER

what if my error is not 500 it is something else and the error no is not know,
ASKER CERTIFIED SOLUTION
Avatar of rejoinder
rejoinder
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial