Pages

mandag 20. mai 2013

OpsMgr: Script Monitor: Verify that new file exist

Monitoring details


Using: Operations Manager 2012.
Agent: running Windows Server 2008 R2.
Objective: Monitor that file(s) not older than x hours, exist in specified folder or subfolders.
Method: VBScript using two state Script Monitor.
State: Trigger Critical state if no file newer than x hours exist.
Schedule: Run every 60 minutes.
Action to take: If Critical state, notify application owner, by e-mail.

Monitor characteristics

  • Monitor is disabled by default, use override to run on specified computer or group of Computers.
  • Monitor is in Critical state if script returns a status message of BAD
  • Monitor is in Healthy state if script returns a status message of GOOD
  • Arguments (2):
    • [string]FolderPath: Path to the folder we want to monitor
    • [string]Hours: Check for files newer than specified hours.
  • Properties (1):
    • [string]Result: GOOD or BAD

Script

 '======================================================================  
 ' NAME : fscheckthatnewfileexist.vbs
 ' AUTHOR : Reidar Johansen
 ' DATE : 5/20/2013
 ' COMMENT: Monitor that file(s) not older than x hours, exist in folder.
 ' Include subfolders. Trigger Critical state if no file newer than x hours exist.
 ' Used in a two-state monitor.
 '======================================================================
 Option Explicit  
 Dim oArgs, oAPI, sMonitorStatus, oBag  
 Dim oFso, sFolderPath, iHours  
 Set oArgs = Wscript.Arguments  
 Set oAPI = CreateObject("MOM.ScriptAPI")  
 If oArgs.Count < 2 Then  
  ' If called without argument, create event and quit  
  Call oAPI.LogScriptEvent(WScript.ScriptName,1001,2,"The script was called without correct arguments and was not executed.")  
  Wscript.Quit -1  
 End If  
 Set oFso = CreateObject("Scripting.FileSystemObject")  
 ' Capture path from arguments  
 sFolderPath = oArgs(0)  
 iHours = cInt(oArgs(1))  
 ' See if folder exists  
 If (oFso.FolderExists(sFolderPath)) Then  
  If TestFiles(sFolderPath) Then  
   sMonitorStatus = "GOOD"  
  Else  
   sMonitorStatus = "BAD"  
  End If  
 Else  
  ' If folder do not exist, create event and quit  
  Call oAPI.LogScriptEvent(WScript.ScriptName,1001,2,"The path " + sFolderPath + " was not found. Verify that it exists and check permissions.")  
  Wscript.Quit -1  
 End If  
 ' Create and submit the propertybag  
 Set oBag = oAPI.CreatePropertyBag()  
 Call oBag.AddValue("Result",sMonitorStatus)  
 Call oAPI.Return(oBag)  
 Function TestFiles (ByVal sFolder)  
  Dim oParentFld, oSubFld, cFiles, oFile  
  Set oParentFld = oFso.GetFolder (sFolder)  
  Set cFiles = oParentFld.Files  
  TestFiles = False  
  For Each oFile In cFiles  
   If DateDiff("h", oFile.DateCreated, Now, vbMonday, vbFirstFourDays) <= iHours Then  
    TestFiles = True  
    Exit Function  
   End If  
  Next  
  For Each oSubFld In oParentFld.SubFolders  
   ' Test all files in each subfolder  
   If TestFiles(oSubFld.Path) Then  
    TestFiles = True  
    Exit Function  
   End If  
  Next  
 End Function  

To create a Script Monitor in Operations Manager, take a look at my other blogg: OpsMgr: Script Monitor: Check that no files exist in subfolders.