Warning for people who landed here: this post is VERY OLD. It was written in the early days of struggling with OpsMgr 2007, and when nobody really knew how to do things.
I found that this way was working – and it surely does – but what is described here is NOT the recommended way to do things nowadays. This post was only meant to fill in a gap I was feeling existed, back in 2007.
But as time passes, and documentation gets written, knowledge improves.
Therefore, I recommend you read the newly released Composition chapter of the MP Authoring Guide instead
http://technet.microsoft.com/en-us/library/ff381321.aspx – and start building your custom modules to embed scripts as Brian Wren describes in there, so that you can share them between multiple rules and monitors.
This said, below is the original post.
There is not a lot of documentation for System Center Operations Manager 2007 yet.
It is coming, but there’s a lot of things that changed since the previous release and I think some more would only help. Also, a lot of the content I am seeing is either too newbie-oriented or too developer-oriented, for some reason.
I have not yet seen a tutorial, webcast or anything that explains how to create a simple unit monitor that uses a VBS script using 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)
[edited on 29th of May as pointed out by Ian: if you cut and paste the example script you might need to change the apostrophes (“) as that causes the script to fail when run – it is an issue with the template of this blog.] [edited on 30th of May: I fixed the blog so that now post content shows just plain, normal double quotes instead than fancy ones. It seems like a useful thing when from time to time I post code…]
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:
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.
Have fun with more complex scripts!
can not see the images.
The images are stored on my Flickr account. You might have configured your browser to only show images coming from the same domain /site you are browsing…
When I get to the script page, after I paste your script in, the next button is still disabled so I cannot continue to the next step. Any ideas?
Hi Larry, have you written the filename as “whatevernameyoulike.VBS” (with the .VBS extension) ?
Yeah I figured that out after I posted. Thanks.
Hi Daniele,
The MOM 2007 script (for entering content in a txt file) example that you have specified gave me some idea, many thanks for this, can you please gimme another example for executing a MOM script in MOM 2005 Thanks in advance -Fida
Hi Daniele,
The MOM 2007 script (for entering content in a txt file) example that you have specified gave me some idea, many thanks for this, can you please gimme another example for executing a MOM script responded by a “Rule”, it would be gr8 if it would work similar to raising an alert with CreateAlert function in MOM 20005
Fida, I only wrote that post because when I first did that kind of configuration I figured out that some passages were not extremely clear, and there wasn’t a lot of documentation on the topic yet. MOM2005 has a much longer history and it has a lot more documentation. The best place to start with it is probably at the MOM Script Center: http://www.microsoft.com/technet/scriptcenter/hubs/mom.mspx
HI,
thx for the tutorial…Please more script examples. I would like to get some script examples for snmp devices.. This would be great.
Thanks for a nice guide! I love when there are screens showing the whole process..
The setting up the rule is the easy part. What I find difficult and cannot figure out, is how to use variables from the Property Bag in the alert description. I mean, it’s easy enough to use in the unhealthy and healthy expressions, but not in the alert text that is actually generated. Any help here would be great.
If I use the line Call obag.AddValue (“State”,”Bad”)
it’s explained how to use Property[@Name=’State’] inthe expression conditions of the monitor,
but how do I use the variables in the alert text? (The last snapshot of the monitor) That is a very important piece to not include
Michael, everything is easy once you have figured out how to do it. Before that, if it is not documented, it is hard.
When I wrote this post, more than a year ago, there was no documentation about how to use Property[@Name=”name”] anywhere. So that was kind of hard to figure out, back then, for me. Then of course the world goes on, and things get documented/understood better 🙂
Using the properties in the alert description uses a similar syntax, and it has been described by Russ here: http://blogs.msdn.com/rslaten/archive/2007/10/12/populating-an-alert-description-with-propertybag-data.aspx
be sure to read all the comments in that as well, as the syntax changes slightly when you have a rule or a monitor.
Also, another interesting post is this other one, for SNMP monitors http://blogs.msdn.com/rslaten/archive/2007/10/31/snmpvarbinds-and-the-alert-description-field-in-opsmgr-2007.aspx
Hi,
thank you for the guide, I have a large problem, where can I find sample scripts of monitors based on a SQL query ?
I mean, I need to run a query on a distant SQL server, but I can’t find the right commands and syntax to return the result of my query to the result bag…
So the monitor remains healthy.
thanx for your help,
Fred from France
Fred, I would suggest that you use the “OLEDB” template http://blogs.msdn.com/komal/archive/2007/05/01/ole-db-module.aspx , rathern than creating a unit monitor, for that kind of task.
That said, if you still want to use a script then, there are plenty of examples on the Internet of how to query SQL Server from VBScript using ADODB. Just search for them http://www.google.com/search?hl=en&q=VBScript+ADODB