Solved

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

Posted on 2008-10-13
3
408 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

747 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

7 Experts available now in Live!

Get 1:1 Help Now