Thursday, November 21, 2013

SCOM scripting basics

This is the part three of a set of articles related to SCOM


Why scripting with SCOM?

Scripting is the most powerful tool in SCOM, with scripts and imagination you can monitor everything, from a folder in the filesystem to a coffee machine.

Usually we use Visual Basic Script for scripting in SCOM, because it works in all Windows systems, from 2003 to 2012R2, but you can use Powershell if you prefer,  as long as it is installed in the monitored system. Bash or Python can be used as well, if you are monitoring Linux servers.
Scripts could be emebeded in Management packs or can be developed from the administration console as monitor or rule.
This article is focused on monitor scripts, but it can give you an idea about how scripts work in SCOM and how to use them on rules or management packs.

Obag object

Operations Manager scripting runtime is called PropertyBag. A PropertyBag is an object that holds name value pairs that can be used to indicate state, hold event data, or even create custom performance data counter.
This is an example of how to initiate this runtime and set values in VBS script.
 ‘Instantiate OpsMgr scripting runtime and create PropertyBag   
 Set oAPI = CreateObject("MOM.ScriptAPI")   
 Set oBag = oAPI.CreateTypedPropertyBag(StateDataType)   
 ‘Adding a GOOD or BAD value to the PropertyBag   
 Call oBag.AddValue("State","OK")   
 Call oBag.AddValue("State","KO")   
'Return to SCOM
oAPI.Return(oBag)

Anonther example using Powershell
 ‘Instantiate OpsMgr scripting runtime and create PropertyBag   
 $api = New-Object -comObject 'MOM.ScriptAPI'   
 $bag = $api.CreatePropertyBag  
 ‘Adding a GOOD or BAD value to the PropertyBag   
 $Script:Bag.AddValue("State","OK")  
 $Script:Bag.AddValue("State","KO")  
'Return to SCOM
$api.Return($bag)

VBS basic script

Firstly, here is a VBS two states monitor script with comments attached.

 'This script checks if there are files older than five minutes in the given folders.  
 'You have to load the SCOM runtime  
 Dim oAPI, oBag  
 Set oAPI = CreateObject("MOM.ScriptAPI")  
 Set oBag = oAPI.CreateTypedPropertyBag(StateDataType)  
 'Setup variables  
 Dim Folders(3)  
 Dim AlertText   
 AlertText = ""  
 'Setup folders  
 Folders(0) = "\\XXX"  
 Folders(1) = "\\YYY"  
 Folders(2) = "\\ZZZ"  
 Folders(3) = "\\WWWWW"  
 for x=0 to UBound(Folders)  
  CheckTime(Folders(x))  
 next  
 If Len(AlertText) > 1 Then  
   'It returns the alert state KO and altert text to SCOM  
      Call oBag.AddValue("State","KO")  
      Call oBag.AddValue("Alert", AlertText)  
 Else   
   'it returns OK state to the SCOM  
   Call oBag.AddValue("State","OK")  
 End If         
 Call oAPI.Return(oBag)   
 'function which checks if the folder has old files   
 Sub CheckTime(byval folder)  
      objStartFolder = folder  
      Set objFSO = CreateObject("Scripting.FileSystemObject")  
      Set objFolder = objFSO.GetFolder(objStartFolder)  
      Set colFiles = objFolder.Files  
      Files = 0  
      date1=DateAdd("n",-4,now)  
      For Each objFile in colFiles  
           If objFile.DateCreated < date1 Then   
                Files=Files+1  
           End If   
      Next  
      If Files > 0 Then   
        'Text which is displayed by an alert  
           AlertText = AlertText & folder & vbCrLf              
      End If   
  End Sub   

When you have the script written, you have to create a monitor, in this case two states script monitor, to do this you have to go to Authoring ---> Monitors --> Create unit monitor.
Firstly select the Management pack and select Scripting --> Generic --> Timed script two state Monitor and click NEXT
Now wizard will ask you for general propierties of the monitor, like name description , target and parent monitor. It is very important to select well the target, because the target is where the script is going to be distributed.
Next step, wizard will ask you for the execution schedule, by default it is set to 15 minutes.
Next step, will ask you for the script and you have to write here the script, as you can seen in the screenshot.


Next step, wizzard will ask you for the unhealthy expression, this is an important point,  you have to fill it with the parameters caputred in the script, in this case  oBag.AddValue("State","KO") , in the parameter name, you have to write Property[@Name='State'] 'State' is the first parameter of .AddValue method, and KO is the second parameter of this method, as you can see in the screenshot.


Next step is about healthy expression,it is the same but using OK instead of KO.
Next screen will ask you about  the two states as you can see in this screenshot, the unhealthy state can be warning or fail.


Finally wizard will ask you about alert generated by the monitor, to add the parameter created in the script oBag.AddValue("Alert", AlertText), you have to add it to the alert as you can see in the screenshot, to add the code automatically, you have to click on data button and select parameters.


Click CREATE and now you have your monitor ready.
Some people ask us if they can do a SQL queries from SCOM monitor script and the answer is obvious, of course they can do it, here is an exaple:

 Const adOpenStatic = 3  
 Const adLockOptimistic = 3  
 Dim AlertText   
 AlertText = ""  
 Dim oAPI, oBag  
 Set oAPI = CreateObject("MOM.ScriptAPI")  
 Set oBag = oAPI.CreateTypedPropertyBag(StateDataType)  
 Set objConnection = CreateObject("ADODB.Connection")  
 Set objRS = CreateObject("ADODB.Recordset")  
 objConnection.Open "Provider=SQLOLEDB;Server=SERVERNAME;Database=DatabaseNAME;Integrated Security=SSPI;"  
 objRS.Open "SELECT * from table where field like 'XXX' ", objConnection, adOpenStatic, adLockOptimistic  
 do until objRS.EOF  
   for each x in objRS.Fields  
     AlertText = AlertText & x.value & " "  
   next  
   objRS.MoveNext  
 loop  
 If Len(AlertText) > 1 Then  
      AlertText = AlertText & "XXXXXXX"   
      Call oBag.AddValue("State","KO")  
      Call oBag.AddValue("Alert", AlertText)  
 Else   
       Call oBag.AddValue("State","OK")  
 End If         
 Call oAPI.Return(oBag)  

Powershell basic script

When you are using Powershell scripting, the process is the same as VBS, but writing powershell instead of VBS. Here is a great trable comparing VBS and Powershell, extracted from: http://blogs.technet.com/b/brianwren/archive/2009/06/04/powershell-scripts-in-a-management-pack-part-2.aspx

 Discovery Data        
 VBScript     PowerShell  
 Set oAPI = CreateObject("MOM.ScriptAPI")   
 Set oDiscoveryData = oAPI.CreateDiscoveryData()   
 …   
 …   
 oAPI.Return(oDiscoveryData)  
 $api = New-Object -comObject 'MOM.ScriptAPI'   
 $discoveryData = $api.CreateDiscoveryData()   
 …   
 …   
 $discoveryData  
 Single Property Bag        
 VBScript     PowerShell  
 Set oAPI = CreateObject("MOM.ScriptAPI")   
 Set oBag = oAPI.CreatePropertyBag()   
 …   
 …   
 oAPI.Return(oBag)  
 $api = New-Object -comObject 'MOM.ScriptAPI'   
 $bag = $api.CreatePropertyBag()   
 …   
 …   
 $bag  
 Multiple Property Bags        
 VBScript     PowerShell  
 Set oAPI = CreateObject("MOM.ScriptAPI")   
 For Each object in collection   
   Set oBag = oAPI.CreatePropertyBag()   
   …   
   …   
 Next   
 oAPI.ReturnItems()  
 $api = New-Object -comObject 'MOM.ScriptAPI'   
 foreach (object in collection)   
 {   
   $bag = $api.CreatePropertyBag()   
   …   
   …   
   $bag   
 }  

Linux / Unix Scripts

One of the most interesting things with SCOM is the possiblity of monitoring non-microsoft systems, such as Linux or Unix. SCOM 2012 improves this possibility including management packs for more Linux and Unix types than SCOM 2007.
The first thing to do to bulild a monitor is to create a shell script, it could be in Bash, Perl, Python, Ruby or anything that can generate a standard output which the agent can interpret.

Script like this one:
 #!/bin/bash  
 ls -1 | wc -l  

This script writes the number of files in the standard output, for example, it can output number 4.
To create new Linux script monitor as you do in Windows monitor, go to Authoting --> monitors --> and execute the creation monitor wizard. Follow the wizard (it is the same as Windows script) until it asks you about the shell commands details, here you have to provide the path to the shell script, for example /scripts/list.sh
The next step will ask you for the error expression, as you can see in the screenshot, it will ask for the stdout and returncode.


For example using the ls script, you can set Stdout value to greater than 5 and it will generate an alert when you have a number bigger than 5 in the Stdout.
The same for the healhty expression and for "configure alerts" step.

Now you have a Linux monitor.

Resources

There are fantastic resources about SCOM scripting like:

VB scripts
Powershell scripts
Linux scripts


No comments:

Post a Comment