<DataSourceModuleType ID="EDC.Base.FileTimeStampMonitor.ScriptDataSource" Accessibility="Public" Batching="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="IntervalSeconds" type="xsd:int"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="SyncTime" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Debug" type="xsd:boolean"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="DebugFileName" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="LogSuccessEvent" type="xsd:boolean"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ScriptGroupId" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="FileName" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ThresholdInSec" type="xsd:int"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ThresholdIsMax" type="xsd:boolean"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="FileNotExistIsGreen" type="xsd:boolean"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ReplaceDblBackslashes" type="xsd:boolean"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="TimeoutSeconds" type="xsd:int"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="IntervalSeconds" Selector="$Config/IntervalSeconds$" ParameterType="int"/>
<OverrideableParameter ID="SyncTime" Selector="$Config/SyncTime$" ParameterType="string"/>
<OverrideableParameter ID="Debug" Selector="$Config/Debug$" ParameterType="bool"/>
<OverrideableParameter ID="DebugFileName" Selector="$Config/DebugFileName$" ParameterType="string"/>
<OverrideableParameter ID="LogSuccessEvent" Selector="$Config/LogSuccessEvent$" ParameterType="bool"/>
<OverrideableParameter ID="ScriptGroupId" Selector="$Config/ScriptGroupId$" ParameterType="string"/>
<OverrideableParameter ID="FileName" Selector="$Config/FileName$" ParameterType="string"/>
<OverrideableParameter ID="ThresholdInSec" Selector="$Config/ThresholdInSec$" ParameterType="int"/>
<OverrideableParameter ID="ThresholdIsMax" Selector="$Config/ThresholdIsMax$" ParameterType="bool"/>
<OverrideableParameter ID="FileNotExistIsGreen" Selector="$Config/FileNotExistIsGreen$" ParameterType="bool"/>
<OverrideableParameter ID="ReplaceDblBackslashes" Selector="$Config/ReplaceDblBackslashes$" ParameterType="bool"/>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<DataSource ID="DS" TypeID="Windows!Microsoft.Windows.TimedScript.PropertyBagProvider">
<IntervalSeconds>$Config/IntervalSeconds$</IntervalSeconds>
<SyncTime>$Config/SyncTime$</SyncTime>
<ScriptName>FileTimestampMon.vbs</ScriptName>
<Arguments>$Config/Debug$ "$Config/DebugFileName$" $Config/LogSuccessEvent$ "$Config/ScriptGroupId$" "$Config/FileName$" $Config/ThresholdInSec$ $Config/ThresholdIsMax$ $Config/FileNotExistIsGreen$ $Config/ReplaceDblBackslashes$</Arguments>
<ScriptBody><Script>
Option Explicit
'***********************************************************************************************************
' Disclaimer
'
' This sample script is not supported under any Microsoft standard support program or service. This sample
' script is provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties
' including, without limitation, any implied warranties of merchantability or of fitness for a particular
' purpose. The entire risk arising out of the use or performance of this sample script and documentation
' remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation,
' production, or delivery of this script be liable for any damages whatsoever (including, without limitation,
' damages for loss of business profits, business interruption, loss of business information, or other
' pecuniary loss) arising out of the use of or inability to use this sample script or documentation, even
' if Microsoft has been advised of the possibility of such damages.
'
'***********************************************************************************************************
' FileTimestampMon.vbs 1.0 v20080603
'
' Compares the timestamp (DateLastModified) of a given file.
'
' Arguments - Debug - True/False value to indicate to write activity to a log file
' - DebugFileName - Complete path to debug log file
' - LogSuccessEvent - True/False value to indicate to log an event if the script succeeds
' (useful for demos and debugging)
' - ScriptGroupId - String that gives the script group - used for consolidated views
' - FileName - Full path to monitored file
' - Threshold - Threshold in seconds
' - ThresholdIsMax - Boolean. If set to True (the default) the threshold will represent the maximum file age,
' i.e., the file must be modified in the last <threshold> s.
' - FileNotExistIsGreen - Boolean. If set to True (the default) the state will be healthy (green) if the given
' file does not exist.
' - ReplaceDblBackslashes - Boolean. If set to True then double backslashes in the given file name will be replaced
' with a single backslash. The default is False.
'
' Version for Operations Manager 2007
'
' JM
'***********************************************************************************************************
'Script specific constants
'Const SCRIPT_NAME = "FileTimestampMon"
Const SCRIPT_VERSION = "1.0"
'Const DEBUG_FILE_NAME = "%WinDir%\Debug\FileTimestampMon"
Const EVENT_ID_SUCCESS = 1000 'Use IDs in the range 1 - 1000
Const EVENT_ID_SCRIPTERROR = 999 'Then you can use eventcreate.exe to test the MP
'Const EVENT_ID_PROCESSERROR = 998
'Other constants
Const FOR_READING = 1 'Open a file for reading only. You can't write to this file.
Const FOR_WRITING = 2 'Open a file for writing.
Const FOR_APPENDING = 8 'Open a file and write to the end of the file.
'Global variables
Dim mblnDebug 'As Boolean
Dim mstrDebugFileName 'As String
Dim mstrIdentifier 'As String
Dim mobjDebugLog 'As TextStream
Dim mblnLogSuccessEvent 'As Boolean
Call Main
'********************************************************************************
' PROCEDURE: Main
' DESCRIPTION: Read the arguments and check whether services are running.
' PARAMETERS: void
'******************************************************************************
Private Sub Main()
Dim objFSO 'As Scripting.FileSystemObject
Dim blnSuccess 'As Boolean
Dim blnScriptSuccess 'As Boolean
Dim objSWbemServices 'As WbemScripting.SWbemServices
Dim strFileName 'As String
Dim lngThresholdInSec 'As Long
Dim blnThresholdIsMax 'As Boolean
Dim blnFileNotExistIsGreen 'As Boolean
Dim strDescription 'As String
Dim i 'As Long
Dim strTmp 'As String
Dim lngCounter 'As Long
Call SetLocale("en-us")
Dim objTypedPropertyBag 'As MOM.ScriptAPI.TypedPropertyBag
Dim objMomScriptAPI 'As MOM.ScriptAPI
'On Error Resume Next
Set objMomScriptAPI = CreateObject("MOM.ScriptAPI")
'Read script parameters and set arrIncludeProcessesNames, strExcludeProcesses.
'Call GetScriptParameters
If Not GetScriptParameters(strFileName, lngThresholdInSec, blnThresholdIsMax, blnFileNotExistIsGreen) Then
' If the script is called without the required arguments,
' create an information event and then quit.
Call WScript.Echo("Error! The script was called with fewer than 6 arguments or the arguments could not be parsed.")
Call objMomScriptAPI.LogScriptEvent(mstrIdentifier & " -- " & WScript.ScriptName & " " & SCRIPT_VERSION, EVENT_ID_SCRIPTERROR, EVENT_TYPE_WARNING, _
"The script was called with fewer than 6 arguments or the arguments could not be parsed.")
WScript.Quit -1
End If
'Open the debug log file if required
If mblnDebug Then
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FolderExists(Left(mstrDebugFileName, InStrRev(mstrDebugFileName, "\") - 1)) Then Call MakeFolder(Left(mstrDebugFileName, InStrRev(mstrDebugFileName, "\") - 1), objFSO)
Set mobjDebugLog = objFSO.CreateTextFile(mstrDebugFileName, True)
Call mobjDebugLog.WriteLine("Script " & WScript.ScriptName & " " & SCRIPT_VERSION & " started -- " & Now & vbCrLf & vbCrLf & _
"Checking last modified timestamp..." & vbCrLf & "File: " & strFileName & vbCrLf & _
"Threshold: " & CStr(lngThresholdInSec) & " s" & vbCrLf & _
"Threshold is the maximum value: " & CStr(blnThresholdIsMax) & vbCrLf & _
"Monitor is healthy if file does not exist: " & CStr(blnFileNotExistIsGreen) & vbCrLf)
End If
'Check the given file
blnScriptSuccess = CheckFile(strFileName, lngThresholdInSec, blnThresholdIsMax, blnFileNotExistIsGreen, objFSO, objMomScriptAPI)
If mblnDebug Then
Call mobjDebugLog.WriteLine("Script finished -- " & Now)
Call mobjDebugLog.Close
End If
'Create an error or, if enabled, a success event if the script ran through as expected.
If mblnLogSuccessEvent And blnScriptSuccess Then
Call objMomScriptAPI.LogScriptEvent(mstrIdentifier & " -- " & WScript.ScriptName & " " & SCRIPT_VERSION, EVENT_ID_SUCCESS, EVENT_TYPE_INFORMATION, _
"The script '" & WScript.ScriptName & " " & SCRIPT_VERSION & "' completed successfully")
ElseIf Not blnScriptSuccess Then
Call objMomScriptAPI.LogScriptEvent(mstrIdentifier & " -- " & WScript.ScriptName & " " & SCRIPT_VERSION, EVENT_ID_SCRIPTERROR, EVENT_TYPE_WARNING, _
"The script '" & WScript.ScriptName & " " & SCRIPT_VERSION & "' failed.")
Exit Sub
End If
'Return the typed property bags
Call objMomScriptAPI.ReturnItems
End Sub
'******************************************************************************
' FUNCTION: CheckFile
' DESCRIPTION: Checks the date last modified property of the given file,
' compares it with the given threshold and returns a state
' property bag.
' PARAMETERS: IN String strFileName: full path to monitored file
' IN Long lngThresholdInSec: Threshold in seconds
' IN Boolean blnThresholdIsMax: If set to True (the default) the threshold will represent the maximum file age,
' i.e., the file must be modified in the last <threshold> s.
' IN Boolean blnFileNotExistIsGreen: If set to True (the default) the state will be healthy (green) if the given
' file does not exist.
' IN Object objFSO: initialised Scripting.FileSystemObject
' IN/OUT Object objMomScriptAPI: initialised MOM.ScriptAPI object to return the property bag
' RETURNS: Boolean: True if all given processes are running, otherwise false
'******************************************************************************
Private Function CheckFile(ByRef strFileName, ByRef lngThresholdInSec, ByRef blnThresholdIsMax, _
ByRef blnFileNotExistIsGreen, ByRef objFSO, ByRef objMomScriptAPI) 'As Boolean
Dim objFile 'As File
Dim strHealtState 'As String
On Error Resume Next
'Check if file exists
If mblnDebug Then Call mobjDebugLog.WriteLine("Check if the file '" & strFileName & "' exists...")
If Not objFSO.FileExists(strFileName) Then
If mblnDebug Then Call mobjDebugLog.WriteLine("The file does not exist." & vbCrLf & _
"File does not exist is healthy: " & CStr(blnFileNotExistIsGreen))
If blnFileNotExistIsGreen Then
'Healthy
strHealtState = HEALTHY_STATE
Else
'Unhealthy
strHealtState = UNHEALTHY_STATE
End If
Else
If mblnDebug Then Call mobjDebugLog.WriteLine("The file exists. Retrieve it...")
'File exists
Set objFile = objFSO.GetFile(strFileName)
If mblnDebug Then Call mobjDebugLog.WriteLine("Date last modified: " & CStr(objFile.DateLastModified) & vbCrLf & _
"Date now: " & CStr(Now) & vbCrLf & _
"Difference in s: " & CStr(DateDiff("s", objFile.DateLastModified, Now)) & vbCrLf & _
"Threshold in s: " & CStr(lngThresholdInSec) & vbCrLf & _
"Threshold is a maximum: " & CStr(blnThresholdIsMax) & vbCrLf)
'Check when the file was last modified
If DateDiff("s", objFile.DateLastModified, Now) > lngThresholdInSec And blnThresholdIsMax Then
'Threshold is a maximum and it is exceeded: Unhealthy
strHealtState = UNHEALTHY_STATE
ElseIf DateDiff("s", objFile.DateLastModified, Now) > lngThresholdInSec And Not blnThresholdIsMax Then
'Threshold is a minimum and the value is lesser: Unhealthy
strHealtState = UNHEALTHY_STATE
Else
'Healthy
strHealtState = HEALTHY_STATE
End If
End If
If mblnDebug Then Call mobjDebugLog.WriteLine("State = " & strHealtState & vbCrLf & vbCrLf & _
"Create state typed property bag...")
'Create a state typed property bag to return the current state and the file name.
Call CreateStateTypedPropertyBag(STATE_PROPERTY_NAME, strHealtState, FILE_NAME_PROPERTY_NAME, strFileName, _
THRESHOLD_PROPERTY_NAME, CStr(lngThresholdInSec), objMomScriptAPI)
If mblnDebug Then Call mobjDebugLog.WriteLine("Created")
If Err.Number = 0 Then CheckFile = True
End Function
'**************************************************************
' FUNCTION: MakeFolder
' DESCRIPTION: Creates a folder and all necessary parent
' folders.
' PARAMETERS: IN String strFolder: full path of the folder
' IN Object objFSO: Scripting.FileSystemObject object
' RETURNS: Boolean
'**************************************************************
Private Function MakeFolder(ByRef strFolder, ByRef objFSO) 'As Boolean
Dim strParentFolder 'As String
On Error Resume Next
'Get parent folder of given folder
strParentFolder = Left(strFolder, InStrRev(strFolder, "\") - 1)
'If the parent folder does not exist create it. (recursive call)
If Not objFSO.FolderExists(strParentFolder) Then Call MakeFolder(strParentFolder, objFSO)
'Create folder
Call objFSO.CreateFolder(strFolder)
'If error no equals zero function is successful
If Err.Number = 0 Then MakeFolder = True
Call Err.Clear
End Function
'******************************************************************************
' FUNCTION: GetScriptParameters
' DESCRIPTION: Reads the script's parameters
' and sets the global variables.
' PARAMETERS: OUT String strFileName: full path to monitored file
' OUT Long lngThresholdInSec: Threshold in seconds
' OUT Boolean blnThresholdIsMax: If set to True (the default) the threshold will represent the maximum file age,
' i.e., the file must be modified in the last <threshold> s.
' OUT Boolean blnFileNotExistIsGreen: If set to True (the default) the state will be healthy (green) if the given
' file does not exist.
' RETURNS: Boolean: True if successful
'******************************************************************************
Private Function GetScriptParameters(ByRef strFileName, ByRef lngThresholdInSec, ByRef blnThresholdIsMax, _
ByRef blnFileNotExistIsGreen) 'As Boolean
Dim objArguments 'As WScript.Arguments
Dim blnReplaceDblBackslashes 'As Boolean
On Error Resume Next
Set objArguments = WScript.Arguments
If objArguments.Count < 6 Then Exit Function
'Get parameters and set global variables
mblnDebug = CBool(objArguments(0))
mstrDebugFileName = Replace(objArguments(1), Chr(34), "")
mblnLogSuccessEvent = CBool(objArguments(2))
mstrIdentifier = Replace(objArguments(3), Chr(34), "")
strFileName = Replace(objArguments(4), Chr(34), "")
lngThresholdInSec = CLng(objArguments(5))
'Set default
blnThresholdIsMax = True
blnFileNotExistIsGreen = True
blnReplaceDblBackslashes = False
'If the optional arguments are present set variables accordingly
If objArguments.Count > 6 Then blnThresholdIsMax = CBool(objArguments(6))
If objArguments.Count > 7 Then blnFileNotExistIsGreen = CBool(objArguments(7))
If objArguments.Count > 8 Then blnReplaceDblBackslashes = CBool(objArguments(8))
If blnReplaceDblBackslashes Then strFileName = Replace(strFileName, "\\", "\")
If Len(strFileName) > 0 And lngThresholdInSec > 0 Then GetScriptParameters = True
End Function
'***************************************************************************************
' PROCEDURE: CreateStateTypedPropertyBag
' DESCRIPTION: Generates a typed property bag and adds it to the script's XML output.
' This generic function creates one item with three name/value pairs only.
' PARAMETERS: IN String stName1: name of the name/value pair 1
' IN String strValue1: value of the name/value pair 1
' IN String stName2: name of the name/value pair 2
' IN String strValue2: value of the name/value pair 2
' IN String stName3: name of the name/value pair 3
' IN String strValue3: value of the name/value pair 3
' IN Object objMomScriptAPI: initialised MOM.ScriptAPI object
'***************************************************************************************
Sub CreateStateTypedPropertyBag(ByRef strName1, ByRef strValue1, ByRef strName2, ByRef strValue2, _
ByRef strName3, ByRef strValue3, ByRef objMomScriptAPI)
Dim objTypedPropertyBag 'As MOM.ScriptAPI.TypedPropertyBag
'Create a new typed property bag of the state data type
Set objTypedPropertyBag = objMomScriptAPI.CreateTypedPropertyBag(STATE_DATA_TYPE)
'Add the property bag to the script's XML output
Call objMomScriptAPI.AddItem(objTypedPropertyBag)
If mblnDebug Then Call mobjDebugLog.WriteLine("Added new state property bag with two name/value pairs:" & vbCrLf & _
Chr(34) & strName1 & Chr(34) & ", value = " & Chr(34) & strValue1 & Chr(34) & vbCrLf & _
Chr(34) & strName2 & Chr(34) & ", value = " & Chr(34) & strValue2 & Chr(34) & vbCrLf & _
Chr(34) & strName3 & Chr(34) & ", value = " & Chr(34) & strValue3 & Chr(34))
End Sub </Script></ScriptBody>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</DataSource>
</MemberModules>
<Composition>
<Node ID="DS"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.PropertyBagData</OutputType>
</DataSourceModuleType>