Pages

onsdag 10. april 2013

OpsMgr: Script Monitor: Check that no folder with name starting with X exist in subfolders

Monitoring details


Using: Operations Manager 2012.
Agent: running Windows Server 2008 R2.
Objective: Verify that no folder starting with Error_AP01 exist in any subfolder of specified folder.
Method: VBScript using two state Script Monitor.
State: Trigger Critical state if folder 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]FolderName: Name of folder to search for, matches folders that begins with this name
  • Properties (1):
    • [string]Result: GOOD or BAD

Script

 '======================================================================  
 ' NAME  : fscheckiffoldernameexist.vbs  
 ' AUTHOR : Reidar Johansen  
 ' DATE  : 4/10/2013  
 ' COMMENT: Monitor a folder and its subfolders for a foldername starting  
 ' With a specified name. Trigger an error state if the folder exit.  
 ' Used in a two-state monitor.  
 '======================================================================  
 Option Explicit  
 Dim oArgs, oAPI, sMonitorStatus, oBag  
 Dim oFso, sFolderPath, sFolderName, lExist  
 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,101,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)  
 sFolderName = oArgs(1)  
 ' See if folder exists  
 If (oFso.FolderExists(sFolderPath)) Then  
   lExist = FindFolder (sFolderPath,sFolderName)  
   If lExist Then   
     sMonitorStatus = "BAD"  
   Else   
     sMonitorStatus = "GOOD"   
   End If   
 Else  
   ' If folder do not exist, create event and quit  
   Call oAPI.LogScriptEvent(WScript.ScriptName,101,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 FindFolder (ByVal sFolder, ByVal sName)  
   Dim oParentFld, oSubFld  
   Set oParentFld = oFso.GetFolder (sFolder)  
   ' Matc name of current directory  
   If Left(oParentFld.Name,Len(sName)) = sName Then  
    FindFolder = True  
    Exit Function  
   End If  
   For Each oSubFld In oParentFld.SubFolders  
     ' Match name of each subfolder  
     If FindFolder(oSubFld.Path,sName) Then  
      FindFolder = True  
      Exit Function  
     End If  
   Next  
   'Folder not found  
   FindFolder = False  
 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.