onsdag 10. april 2013

OpsMgr: Script Monitor: Check that no files exist in subfolders

Monitoring details

Using: Operations Manager 2012.
Agent: running Windows Server 2008 R2.
Objective: Verify that all invoices (pdf files stored in various subfolders) have been processed at given time.
Method: VBScript using two state Script Monitor.
State: Trigger Critical state if subfolders contains files at given time.
Schedule: Run once each day, at specified time.
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
  • 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 (1):
    • [string]FolderPath: Path to the folder we want to monitor
  • Properties (2):
    • [integer]FileCount: Number of files in subfolders
    • [string]Result: GOOD or BAD


 ' NAME  : fscheckthatnofilesexist.vbs  
 ' AUTHOR : Reidar Johansen  
 ' DATE  : 4/10/2013  
 ' COMMENT: Monitor file count in a folder and its subfolders.
 ' Trigger an error state if the folder or its subfolders contain files.  
 ' Used in a two-state monitor.  
 Option Explicit  
 Dim oArgs, oAPI, sMonitorStatus, oBag  
 Dim oFso, sFolderPath, iFileCount  
 Set oArgs = Wscript.Arguments  
 Set oAPI = CreateObject("MOM.ScriptAPI")  
 If oArgs.Count < 1 Then  
   ' If called without argument, create event and quit  
   Call oAPI.LogScriptEvent(WScript.ScriptName,1001,2,"The script was called without any arguments and was not executed.")  
   Wscript.Quit -1  
 End If  
 Set oFso = CreateObject("Scripting.FileSystemObject")  
 ' Capture path from arguments  
 sFolderPath = oArgs(0)  
 ' See if folder exists  
 If (oFso.FolderExists(sFolderPath)) Then  
   iFileCount = CountFiles (sFolderPath)  
   If iFilecount = 0 Then   
     sMonitorStatus = "GOOD"   
     sMonitorStatus = "BAD"  
   End If   
   ' 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("FileCount",iFileCount)  
 Call oBag.AddValue("Result",sMonitorStatus)  
 Call oAPI.Return(oBag)  
 Function CountFiles (ByVal sFolder)  
   Dim oParentFld, oSubFld, iCount  
   Set oParentFld = oFso.GetFolder (sFolder)  
   ' Count the number of files in the current directory  
   iCount = oParentFld.Files.Count  
   For Each oSubFld In oParentFld.SubFolders  
     ' Count all files in each subfolder  
     iCount = iCount + CountFiles(oSubFld.Path)  
   'Returns the counted files  
   CountFiles = iCount  
 End Function  

Create Script Monitor

1. In the Operations Console, select the Authoring workspace, and then select Monitors.
2. Select Create a Monitor and then Unit Monitor...

3. Expand Scripting and then expand Generic. Select Timed Script Two State Monitor. Also, for this purpose, we will click New... to create Management Pack MyCompany InvoiceSender Monitoring for the application we will monitor. Click Next.

4. In the Name box we type InvoiceSender Processed State. Then we select Monitor target class Windows Computer. Set Parent monitor to Availability. Also unselect the Monitor is enabled box. Click Next.

5. In the Run every box, type 1 and select Days. Also check the Synchronize at box and type 01:30. Click Next.

6. For the File Name value, type fscheckthatnofilesexist.vbs. Leave the default Timeout value. In the Script box, paste the complete contents of the script. Click the Parameters button. Type the path to the folder you would like to monitor like this: "C:\Invoice Folder". Click OK. Click Next.

7. On the Unhealthy Expression page, Click Insert. In the Parameter Name box type Property[@Name='Result']. In the Operator box select Equals. In the Value box type BAD. Click Next.

8. On the Healthy Expression page, Click Insert. In the Parameter Name box type Property[@Name='Result']. In the Operator box select Equals. In the Value box type BAD. Click Next.

9. On the Configure Health page, Change the Health State for the Unhealthy condition to Critical. Click Next.

10. On the Configure Alerts page Select Generate alerts for this monitor. Select The monitor is in a critical health state. Leave the box selected to automatically resolve the alert. In the Alert name box leave the default title. Clear the existing text in the Alert description box and type The folder C:\Invoice Folder on then click the ellipse button. Click Target and then DNS name. Type contains then click Data and then Property. Replace <<STRING>> with FileCount. Type unprocessed files. Click OK. Click Create.

Enable the monitor for a Windows Computer

1. In the Operations Console, select the Authoring workspace, and then select Monitors.
2. Choose Change Scope..., select Clear All and type Windows Computer. Select Windows Computer. Click OK.

3. Expand Windows Computer and then Entity Health and then Availability. Right click Invoice Sender Processed State and select Overrides > Override the Monitor > For a specific object of class: Windows Computer (or you could create a Group with Windows Computers and select For a group...).

4. Select Computer Object from the list or type a Text string to search. Click OK. In Override Properties select the Override box for row with Paramter Name Enabled and set the Override Value to True.

If you open the Health Explorer for the Computer you selected in step 3 above, and then turn off the filter, you will now find the InvoiceSender Processed State monitor by expanding Entity Health and then Availability. You can also open Properties and change settings for the monitor. Best practice would be to export the management pack, seal it, and then import it back (before creating any Overrides). Then you would create an unseald management pack for the overrides.

5. When you receive an alert from this monitor you can create a notification by right clicking the alert and then select Notification subscription.

6. Criterias can be defined with some additional Conditions to prevent a new notification when someone close the alert, if desired...

After completing the wizard we are done...