Create a Script-Based Unit Monitor in OpsMgr2007 via the GUI

So this is how you do it:

Go to the “Authoring” space of OpsMgr 2007 Operations Console.Select the “Management Pack objects”, then “Monitors” node. Right click and choose “Create a monitor” -> “Unit Monitor”.
You get the “Create a monitor” wizard open:

 

Choose to create a two-states unit monitor based on a script. Creating a three- state monitor would be pretty similar, but I’ll show you the most simple one.Also, choose a Management pack that will contain your script and unit monitor, or create a new management pack.

Choose a “monitor target” (object classes or instances – see this webcast about targeting rules and monitors: www.microsoft.com/winme/0703/28666/Target_Monitoring_Edit… ) and the aggregate rollup monitor you want to roll the state up to.

Choose a schedule, that is: how often would you like your script to run. For demonstration purposes I usually choose a very short interval such a two or three minutes. For production environments, tough, choose a longer time range.


Choose a name for your script, complete with a .VBS extension, and write the code of the script in the rich text box:

As the sample code and comments suggest, you should use a script that checks for the stuff you want it to check, and returns a “Property Bag” that can be later interpreted by OpsMgr workflow to change the monitor’s state.This is substantially different than scripting in MOM 2005, where you could only launch scripts as responses, loosing all control over their execution.

For demonstration purpose, use the following script code:

On Error Resume Next
Dim oAPI, oBag
Set oAPI = CreateObject("MOM.ScriptAPI")
Set oBag = oAPI.CreateTypedPropertyBag(StateDataType)
Const FOR_APPENDING = 8
strFileName = "c:\testfolder\testfile.txt"
strContent = "test "
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objTS = objFS.OpenTextFile(strFileName,FOR_APPENDING)
If Err.Number <> 0 Then
Call oBag.AddValue("State","BAD")
Else
Call oBag.AddValue("State","GOOD")
objTS.Write strContent
End If
Call oAPI.Return(oBag)

The script will try to write into the file c:\testfolder\testfile.txt.If it finds the file and manages to write (append text) to it, it will return the property “State” with a value of “GOOD”.If it fails (for example if the file does not exist), it will return the property “State” with a value of “BAD”.

In MOM 2005 you could only let script generate Events or Alerts directly as a mean to communicate their results back to the monitoring engine. In OpsMgr 2007 you can let your script spit out a property bag and then continue the monitoring workflow and decide what to do depending on the script’s result.

So the next step is to go and check for the value of the property we return in the property bag, to determine which status the monitor will have to assume.

We use the syntax Property[@Name=’State’] in the parameter field, and we search for a content that means an unhealthy condition:


Or for the healty one:

Then we decide which status will the monitor have to assume in the healty and unhealty conditions (Green/Yellow or Green/Red usually)

Optionally, we can decide to raise an Alert when the status changes to unhealthy, and close it again when it goes back to healty.

Now our unit monitor is done.All we have to do is waiting it gets pushed down to the agent(s) that should execute it, and wait for its status to change.In fact it should go to the unhealthy state first.To test that it works, just create the text file it will be searching for, and wait for it to run again, and the state should be reset to Healthy.

This post was orginally posted at http://www.muscetta.com/2007/05/10/create-a-script-based-unit-monitor-in-opsmgr2007-via-the-gui/ I had been looking for a good SCOM vb script article for a long time. I figured I would re-post it here becuase his was so difficult to find. I give Daniele Muscetta full credit for this.

14 Responses to Create a Script-Based Unit Monitor in OpsMgr2007 via the GUI

  1. Sudhir September 22, 2008 at 11:02 pm #

    Hi,

    I was looking for difference and a good article to start scripting with SCOM,, Thanks a lot for a structured article !!! 🙂

    Regards
    Sudhir.

  2. Daniele Muscetta March 27, 2009 at 6:58 pm #

    This is a very old post I wrote long ago (opsmgr2007 RTM was just out of the door)… and now we are getting ready for R2 already!

  3. John Bradshaw June 30, 2009 at 9:05 pm #

    Hi,
    If I change the line
    strFileName = “c:\testfolder\testfile.txt”
    to
    strFileName = “\\RemoteServer\temp\My.txt”

    it doesn’t seem to work if I delete my.txt.

    Does the script need a local path?
    Thx,
    JB

    • Tim July 1, 2009 at 12:30 am #

      That is because all monitors and rules run as local system. What you will have to do is create a run as account. Store it in the mp. Then create the script. Go into the Advanced Authoring console (not the regular authoring console). Change the script monitor to run as that new run as account. Also make sure the the run as account has access to the remote server share.

      Let me know if you need me to explain it better.

  4. John Bradshaw July 1, 2009 at 12:58 am #

    Not sure I follow all that (Sorry not really used to some of this stuff), but if I change the script back to C:\temp\My.txt, and use an override to enable the monitor on the remote server (Still part of the same domain), will that work?? (It doesn’t seem to…. 😉

    Is it still a permissions issue do you think (I’m guessing yes), but is there no way that this monitor can be created from just the SCOM console?

    Appreciate you help very much.

    • Tim July 1, 2009 at 10:46 am #

      First Test the script on the local box with that user. If the SCOM agent it on the box you can log on as that user copy the vb script locally and then run it from the command line. So like C:\cscript “C:\temp\My.txt”

  5. Daniele Muscetta March 16, 2010 at 9:38 am #

    Guys, I am nearly ashamed a lot of people still find it useful, as it teaches something that should really NOT be done that way… that was the way I was able to do it 2 years ago.
    If you check out my original post at http://www.muscetta.com/2007/05/10/create-a-script-based-unit-monitor-in-opsmgr2007-via-the-gui/ again, you’ll see I have added a disclaimer to it…

  6. Catia April 6, 2010 at 2:11 pm #

    Ok..I followed the instructions and I got the two step monitor setup to run fine and reflect its state on the operator console. Then after testing, I disabled it and went back to enable it after a week’s time. Now, I see that the monitor script is working fine (by means of log entries when it runs and also verifying by running it on command line mode, I was able to identify that it returns the right value in the property bag), but the console is not updating the new state. Basically, I want this to run every X minutes and report the failure (not just the first failure) till the issue is resolved. I still see that the resolved original alert has the last known state check done, but I am not seeing that the alert updating every X minutes to update its repeat count.

    Since every thing works except for updating the operator console, I am a bit unsure about the next possible steps and would appreciate any help on this.

    Thanks,

  7. Raj April 12, 2013 at 4:47 am #

    Hey Guys,

    This monitor has some issue as below:

    When Service ‘A’ is stopped or not running on a Windows server then there would be an alert in console until the service is back to running state. When the service ‘B’ also came to stopped state on the same server there would not be any alert since we already have an active alert for Service ‘A’ and not closed yet. Could someone please advise how to approach on this situation.

    Thanks,
    Raj.

  8. Aru May 31, 2013 at 6:29 am #

    Tim,

    (That is because all monitors and rules run as local system. What you will have to do is create a run as account. Store it in the mp. Then create the script. Go into the Advanced Authoring console (not the regular authoring console). Change the script monitor to run as that new run as account. Also make sure the the run as account has access to the remote server share.

    Let me know if you need me to explain it better.
    )

    could you please explain how to open the monitor in Advanced Authoring console which is created in regular authoring console.

Trackbacks/Pingbacks

  1. SCOM 2007 Script to Monitor File Count inside a Directory | Everything System Center Operations Manager 2007 - September 11, 2008

    […] To Implement this please reference my earlier post on how to setup a Script-Based monitor. https://www.scom2k7.com/create-a-script-based-unit-monitor-in-opsmgr2007-via-the-gui/ […]

  2. Writing C# applications to do complex monitoring with SCOM | Everything System Center Operations Manager 2007 - June 18, 2009

    […] Ok now we have a good working script and c# executable.  Now we just need to put the script into a monitor and copy the file to the server we want to monitor and we are done.  You can follow these directions if you don’t know how to put the script into a monitor.  https://www.scom2k7.com/create-a-script-based-unit-monitor-in-opsmgr2007-via-the-gui/ […]

  3. System Center 2012 Operations Manager Web Application Monitoring Example - Mark Ghazai's Blog - Site Home - TechNet Blogs - September 27, 2013

    […] Create a Script-Based Unit Monitor in OpsMgr2007 via the GUI […]

  4. How To Create Reports In Scom 2007 R2 | Information - February 17, 2017

    […] Create a Script-Based Unit Monitor in OpsMgr2007 via the … – That is because all monitors and rules run as local system. What you will have to do is create a run as account. Store it in the mp. Then create the script. […]

Leave a Reply