'================================================================================== ' ' NAME: Application Directory and Queue Monitoring ' VERSION: 1.0 ' ' AUTHOR: Pete Zerger, AKOS Technology Services, www.momresources.org ' DATE : 12/21/2006 ' ' DESCRIPTION: This script monitors file age and count in a user-defined target directory. ' It optionally creates a performance object to chart file count in this directory. ' You can adjust the file age and count to suit the application you are monitoring. ' The script raises a Warning Event in MOM. You can create an event rule to raise an Alert in MOM ' ' PARAMETERS: ' ' Directory - Target directory to be monitored. ' ' MaxFileCount - Count of old files (older than days) ' ' MaxFileAge - Max file age in days. Files with a DateLastModified value older than this are considered old / stale. ' This could be converted from days to hours or minutes if necessary. ' ' LogPerformanceData - Boolean value - 1 logs file count as performance data, 0 disables this '================================================================================== '****************************** 'MOM Alert and Event Constants '****************************** 'map event types & numbers to friendly names 'Const MOM_SCRIPT_EVENT_ID = 5005 Const EVENT_TYPE_SUCCESS = 0 Const EVENT_TYPE_ERROR = 1 Const EVENT_TYPE_WARNING = 2 Const EVENTLOG_INFORMATION_TYPE = 4 Const EVENTLOG_AUDIT_SUCCESS = 8 Const EVENTLOG_AUDIT_FAILURE = 16 'map alert types & numbers to friendly names Const ALERT_SUCCESS = 10 Const ALERT_INFORMATION = 20 Const ALERT_WARNING = 30 Const ALERT_ERROR = 40 Const ALERT_CRITICAL_ERROR = 50 Const ALERT_SECURITY_BREACH = 60 Const ALERT_SERVICE_UNAVAILABLE = 70 '****************************************************************************** ' File.Attributes Constants '****************************************************************************** Const FileAttrNormal = 0 Const FileAttrReadOnly = 1 Const FileAttrHidden = 2 Const FileAttrSystem = 4 Const FileAttrVolume = 8 Const FileAttrDirectory = 16 Const FileAttrArchive = 32 Const FileAttrAlias = 1024 Const FileAttrCompressed = 2048 '****************************************************************************** ' Constants for opening files '****************************************************************************** Const OpenFileForReading = 1 Const OpenFileForWriting = 2 Const OpenFileForAppending = 8 '****************************** ' Declare Variables '****************************** Dim objFSO, objFile, objFiles, objFolder, objFolders, objDir, intMaxFileAge, rootDir, intMaxFileCount '****************************** ' MOM Script Parameters '****************************** rootDir = ScriptContext.Parameters.get("Directory") 'Max number of files beyond intMaxFileCount = ScriptContext.Parameters.get("MaxFileCount") 'Maximum file age in days intMaxFileAge = ScriptContext.Parameters.Get("MaxFileAge") '1=Logs old file count as Performance Object in MOM, 0=turns this off blLogPerfData = ScriptContext.Parameters.Get("LogPerformanceData") '************************************* ' File age and count '************************************* 'Instantiate necessary FileSystemObject Set objFSO = CreateObject("Scripting.FileSystemObject") 'Set starting value for our counter variable i=0 If rootDir = "" Then 'Generate empty directory error CreateEvent 35000, EVENT_TYPE_WARNING, "File & Folder Monitoring", "Target directory is null. Please set value for Directory script parameter." End If If intMaxFileAge = 0 Then 'Generate invalid max age error WScript.Quit 1 End If 'Initialize objDir to argument or environnement Folder. Set objDir = objFSO.GetFolder(rootDir) 'intMaxFileAge has to be greater than 0 If CInt(intMaxFileAge) < 1 Then WScript.Quit 1 For Each objFile In objFSO.GetFolder(rootDir).Files If objFile.DateLastModified < Now()-intMaxFileAge Then i=i+1 End If Next 'If count is higher than the threshold, raise an event in MOM If i > CInt(intMaxFileCount) Then 'SYNTAX: CreateEvent number, type, source, messagetext CreateEvent 35000, EVENT_TYPE_WARNING, "File & Folder Monitoring", "Old file count of " & i & " in directory " & rootDir & " is higher than threshold of " & intMaxFileCount End If 'If LogPerformanceData = 1, then Create Performance Object in MOM to count number of old files If CBool(blLogPerfData) Then CreatePerfData "File & Folder Monitoring", "Old File Count", "Directory:" & rootDir, i End If '************************************* ' Standard MOM 2005 Subs and Functions '************************************* '****************************************************************************** ' Name: CreatePerfData ' ' Purpose: Creates a performance object to display response time in Performance View '****************************************************************************** Sub CreatePerfData(strObjectName,strCounterName,strInstanceName,numValue) Set objPerfData = ScriptContext.CreatePerfData objPerfData.ObjectName = strObjectName objPerfData.CounterName =strCounterName objPerfData.InstanceName = strInstanceName objPerfData.Value = numValue ScriptContext.Submit objPerfData End Sub '****************************************************************************** ' Name: CreateEvent ' ' Purpose: Logs an event '****************************************************************************** 'SYNTAX: CreateEvent number, type, source, messagetext Sub CreateEvent(intEventNumber,intEventType,strEventSource,strEventMessage) Set objEvent = ScriptContext.CreateEvent() objEvent.EventNumber = intEventNumber objEvent.EventType = intEventType objEvent.EventSource = strEventSource objEvent.Message = strEventMessage ScriptContext.Submit objEvent ScriptContext.Quit End Sub '****************************************************************************** ' Name: CreateAlert ' ' Purpose: Raises an alert ' ' Parameters: ' iSeverity - The severity of the alert. ' strName - The name of the alert. ' strDescription - The alert description. '****************************************************************************** Function CreateAlert(iSeverity, strName, strDescription) Dim oAlert Set oAlert = ScriptContext.CreateAlert() oAlert.Name = strName 'oAlert.AlertSource = strSource oAlert.Description = strDescription oAlert.AlertLevel = iSeverity ScriptContext.Submit oAlert End Function