SCOM Visual Studio Authoring – Basic Seed Class Discovery

In this blog post I’ll go over a basic seed class discovery. Basically a seed class is a class that normally utilizes a low cost discovery method (in this case a registry discovery) across a large group of servers to narrow the scope of which higher cost discoveries (A script in this case) then target in order to populate more classes.

So to further illustrate, say you have 1000 servers and an application that has 2 server types. You would like to create a class for each server type and you have a few things that can be used to identify the 2 server types:

  • Both server types have a registry key “HKLM:SOFTWARE\Leibundgutski”
  • One server type can be uniquely identified by the existence of a folder”C:\Leibundgutski”
  • The other server type can be uniquely identified by the existence of a file “C:\Andy.tx”

OK, so assume you have 3 of each server type for a total of 6 servers that represent your application and you want one class for each server type (2 classes in total). You have some options to discover them:

  1. High Cost – You create 2 classes that utilize a scripted discovery that target something like Windows Server Computer. Scripts have a lot of overhead and are considered a high cost method of doing things so what would happen is you have 2 scripts running on each of the 1000 servers each time the discoveries are ran to populate the two classes. That’s a lot of scripts (2000 scripts ran)!
  2. Low Cost – You create 3 classes. One seed class that targets that same Windows Server Computer and then 2 more classes, one for each server type, that target the seed class. The seed class utilizes a registry discovery which is a very low cost method of discovery. The registry key only exists on the 6 (total) application servers so only those 6 servers are populated within the seed class. Then what you do is run the higher cost scripted discoveries against that seed class and now you’re only running 12 high cost scripted discoveries across your environment.

If you haven’t guessed number 2 is the way to go. What I’ll be going over today is how to author a basic seed class discovery with Visual Studio. The code can be found here: https://gallery.technet.microsoft.com/SCOM-Visual-Studio-394b6d37

Alright so we’ll start with the seed class

BasicSeedClass_SeedClass

This is the class that represent all 6 application servers that we will then use as a target for the other two classes

  • ID – This uniquely identifies the class within the management pack
  • Comment – Comments about the class, put whatever you want in there
  • Accessibility – Public, when set to true this class can be used by other management packs provided you seal this management pack
  • Abstract – false, abstract classes have no instances and exist only to act as a base class for other classes. Don’t worry about this for now, consult the documentation if you want to know more (links to class documentation are in my MP)
  • Base – This is the class you are basing your class off of
  • Hosted – False, this is not hosted by another class
  • Singleton – False, this class has unique instances that will be discovered (IE its not a group or anything like that. Each discovered server is separate)

Next, seed class discovery

BasicSeedClass_Discovery

This is a registry base discovery used to populate the seed class. It discovers a server instance when the registry key HKLM\Software\Leibundgutski is found.

Discovery

  • ID – BasicSeedDiscovery.LeibundgutskiRegKey.Discovery – This is the name used to uniquely identify this discovery in the management pack.
  • Target – Windows!Microsoft.Windows.Server.Computer” – This is the class the discovery targets. “Windows” is a reference to the
    Microsoft.Windows.Library management pack and “Microsoft.Windows.Server.Computer is a class within that mp.
  • Remotable – true – this workflow can run for agentless monitoring.
  • Enabled – true – this discovery is enabled. It will run by default once its imported.

DiscoveryClass – BasicSeedDiscovery.Seed.Class – This is the class the discovery will discover.

DataSource

  • ID – “DS” – This is the name that uniquely identifies this datasource within the scope of this discovery.
  • TypeID – “Windows!Microsoft.Windows.FilteredRegistryDiscoveryProvider” – “Windows” is a reference to the
    Microsoft.Windows.Library management pack and “Microsoft.Windows.FilteredRegistryDiscoveryProvider” is a registry discovery datasource within that management pack that I’m for this discovery.
  • ComputerName – “$Target/Property[Type=”Windows!Microsoft.Windows.Computer”]/PrincipalName$” – This works out to be the computer name that the discovery is being ran on. Its a property of a parent class of Microsoft.Windows.Server.Computer.
  • AttributeName – “LeibundgutskiKeyExists” – This uniquely identifies the value this registry attribute definition is pulling from the registry.
  • Path – “SOFWARE\Leibundgutski” – This is the location in the registry that the registry attribute definition is looking at.
  • PathType – “0” – zero specifies that we’re looking for  a registry key.
  • AttributeType – “0” – zero specifies that we are returning a true/false value (does it exist or not)
  • Frequency – “86400” how often does the discovery run in seconds (once a day)
  • ClassID – “$MPElement[Name=”BasicSeedDiscovery.Seed.Class”]$” – This reprisendts the class being discovered
  • InstanceSettings
    • Name – “$MPElement[Name=”Windows!Microsoft.Windows.Computer”]/PrincipalName$” – The parent of our seed class “Microsoft.Windows.Computer” has a primary key of PrincipalName which is required to be discovered. This is referencing which value to populate
    • Value – “$Target/Property[Type=”Windows!Microsoft.Windows.Computer”]/PrincipalName$” – It looks kind of weird but the class we are populating (the seed class) is based on the same class as the seed discovery targets. So we are referencing the PrincipalName of the discoveries target class to populate our seed class which in this case are the same.
  • XPathQuery
    • XPathQuery- “Values/LeibundgutskiKeyExists” – This represents the value we pulled in the RegistryAttributeDefinition.
    • operation – “Equal” – The conditional parameter that is used to compare the XPathQuery and Value.
    • Value – “true” – if the value held within the RegistryAttributeDefinition is true then discover the server.

Alright, that’s how you populate the seed class. Next we’ll go over the seedling classes and how they work.

BasicSeedClassSeedlings

So I’ll just go over major differences in these classes from the seed class. Each of these classes represent a particular type of server. One, a type of server that contains a folder c:\Leibundgutski, and the other which represents a type server that contains a file c:\Andy.txt. The other important difference is the base of these seedling classes is the seed class. Notice that instead of targeting all server computers like the seed class does I’m just targeting the seed class itself. In other words only the computers that contain the registry key HKLM\Software\Leibundgutski will have the logic that looks for the folder or file ran on it.

Now I’ll go over the seedling discovery script (LocationDiscovery.vbs)

BasicSeedClassVBS

This script is used by each of the seedling discoveries to populate their classes. It uses conditional if/then logic against the sLocation argument to determine whether to populate (see CreateClassInstance) either the folder or file class. Or both if they both happened to exist. Some of the SCOMey code and considerations to take note of in the script is that every script that is used in SCOM will need to bring in the SourceId and ManagedEntityID. I believe these are used to keep track of the workflow within SCOM. You’ll also need to initiate the “MOM.ScriptAPI” which the script uses to interact with SCOM. Notice the oAPI.CreateDiscoveryData. That is creating the object used to surface the discovery data to the class using the “CreateClassInstance” and then on to the “AddProperty” which is used to populate the key of a parent class (Microsoft.Windows.Computer). Note, you’ll need to populate the key property of any classes in the class hierarchy that contains a key property. At the end is the oAPI.Return which returns the discovery data to the workflow so it can populate the class. One last thing to note is the LogScriptEvent which creates a logging entry (if bDebug is true) in the Operations Manager event log on the target computer.

Alright, lastly is the discovery configuration for the seedling classes

BasicSeedDiscoverySeedlingDiscovery

Each of the discoveries individually call the LocationDiscovery.vbs script to populate their seedling class.

Discovery

  • ID – This is used to uniquely identify the discoveries within the management pack
  • Target – This is the class the discovery targets (the seed discovery for both above)
  • Remotable – Whether the discovery can be used for agentless monitoring
  • Enabled – T/F is the discovery enabled by default

DataSource

  • ID – This is used to uniquely identify the datasource within the scope of the discovery
  • TypeID – This is referencing to a datasource configuration in the “Microsoft.Windows.Library” MP “Microsoft.Windows.TimedScript.DiscoveryProvider” which run a script on a schedule
  • IntervalSeconds – How often the script is ran in seconds
  • ScriptName – The name of the script
  • Arguments – These are the arguments passed to the script. See the description of how the script works above for information on the individual arguments
  • ScriptBody – Although you could just put the script in here I create a script item in visual studio and use an IncludeFileContent (see the Scripts\VBScript folder in the project) to reference it.
  • TimeoutSeconds – How many seconds before the script is set to time out.

That’s all for today, hope it was helpful. Next time I think I’ll skip around again and show you how to do a scripted monitor.

Site Index

 

 

 

SCOM Visual Studio Authoring – VBScript and PowerShell Based Alert Rules (Part 2)

Finally, part 2. I’ll cover a PowerShell based alert rule which looks for the existence of a file c:\Leibundgutski\Andy.txt and alerts if it is detected.

First things first, the complete solution is available for download here: https://gallery.technet.microsoft.com/SCOM-Visual-Studio-bfb42ec6

For the PowerShell alert rule I’ll start by explaining how the script works

PowerShellAlertRule

  • Ok, the first thing worth noting is that this script will accept two parameters from the DataScource “FileLocation” and “bDebug”. FileLocation is the file that the script will look for to determine if their is an error condition or not. In this case if you skip ahead and look at the rule configuration you’ll see that by default its the fileC:\Leibundgutski\Andy.txt that its looking for but it can be overridden to whatever you like in the console. The bDebug parameter (True/False) just tells the script if I want it to enter an event log entry each time it runs or discovers and error condition.
  • $API = New-Object -comObject ‘MOM.ScriptAPI’ – This is calling the SCOM scripting API for use in the script.
  • $Bag = $api.CreatePropertyBag() – This is using the SCOM scripting API to create a property bag object. Think of it as a “bag” you can put things into and then return to the SCOM workflow.
  • $API.logscriptevent(“PowerShellProbe.ps1”,100,0,“PowerShellProbe.ps1 Ran”)

    – Use the LogScriptEvent to log events to the Operations Manager event log

  • $bag.AddValue(‘Result’, “GOOD”) – Here I’m creating a property of “Result” in the bag with a value of either GOOD or BAD that will be used to determine whether or not to alert.
  • $bag.AddValue(‘FileLocation’, $FileLocation) – Here I’m creating a property of “FileLocation” with the value of the file location that is being monitored for later use in the alert description.
  • $Bag – Finally when its all done we return the bag to the DataSource\Workflow.

PowerShell DataSource

PowerShellAlertRuleDataSource

The DataSouce is where you create a configuration for your PowerShell script. This particular DataSource includes the configuration for how often the script is ran and script itself. Here’s the DataSourceModuleType Documentation: https://msdn.microsoft.com/en-us/library/Ee533617.aspx.

DataSourceModuleType

  • ID – “ScriptedAlertRule.PowershellProbe.DataSource” – This is the unique name used to reference the DataSource within the management pack.
  • Accessibility – “Internal” – That indicates the DataSource can only be referenced within this management pack, other MPs cannot use it.

Configuration – These Items are used to initiate the variables that are passed to the MemberModules. These variables will all be set in the actual rule configuration.

  • name=”FileLocation” type=”xsd:string” – The location of the file to monitor
  • name=”bDebug” type=”xsd:boolean” – Whether or not the script will be ran in debug mode (if its true it will write an event log entry, check out the script for how that works)
  • name=”TimeoutSeconds” type=”xsd:integer” – How long ths script runs before it times out
  • name=”Interval” type=”xsd:integer” – Setting for how often the script will be ran (in minutes)

OverridableParameters – Notice I’m setting all the configuration items as overridable. None of them have to be, it just allows the option for each variable to be overridden in the console.The IDs for the overridable parameters could be named anything you like, I make them the same for easy readability. The part that is actually referencing each configuration item is the $Config/VariableName$ portion.

MemberModules – This is where you pass the variables to the script, and set the configuration items for its DataSource.

DataSource – The datasources we are combining in this MemberModule is marrying a scheduler to our PowerShell probe. Without a schedule the script will never run.

  • ID – “Scheduler” is the name used to reference this module within the scope of this datasource.
  • TypeID – “System!System.Scheduler” – The “System!” portion is a reference to the “System.Library” MP and “System.Scheduler” is the datasource within that MP that we are passing our configuration to which runs the schedule for the script.
  • <Interval> – This is where the configuration for how often the script is ran via the scheduler for the script (in minutes).

 

  • ID  – “PowerShellProbe” is the name used to reference this module within the scope of this datasource.
  • TypeID – “Windows!Microsoft.Windows.PowerShellPropertyBagProbe” – The “Windows!” portion is a reference to the “Microsoft.Windows.Library” MP and “Microsoft.Windows.PowerShellPropertyBagProbe” is the datasource within that MP that we are passing our configuration to for the script.
  • <ScriptName> – The name of the script.
  • <ScriptBody> – Rather than sticking the script here I separate out the script and reference it here to keep things clean
  • <Parameters> – This is where you configure parameters to pass to the script.
  • <TimeoutSeconds> – This is where the config item for how long the script runs before it times out goes.

Composition – “Scheduler” and “PowerShellProbe – This is where you reference the DataSources and what order you want them to run in.

OutputType – System!System.PropertyBagData – This is indicating that we’re expecting an object from the script to pass up through the workflow to the rule.

PowerShell Rule

PowerShellAlertRuleConfig

Rule – This is where the rubber meets the road. The script, the schedule, the alert all come together here. Rule documentation: https://msdn.microsoft.com/en-us/library/ee533583.aspx.

  • ID – “ScriptedAlertRule.PowerShellProbe.AlertRule” – This is the unique name used for referencing the rule within the management pack.
  • Enabled – Is the rule enabled by default or not (overridable in the condole)
  • Target – “Windows!Microsoft.Windows.Server.Computer” – Winodws! is a reference to the “Microsoft.Windows.Library” MP and “Microsoft.Windows.Server.Computer” is a target class within that MP that we are targeting the rule against. In this case its all the windows server computers.
  • ConfirmDelivery, Remotable, Priority, and DiscardLevel – you can look them up in the documentation above if you like, they are all optional parameters. Nothing super relevant to what we are doing here.

Category – Custom – You would think rule right, but no its not an option and it doesn’t make much sense but I don’t make the rules… har har

DataSource – Here’s where we set the default values that are passed down the various configurations of the workflow.

  • ID – “PowerShellProbe” – This is the unique name for this datasource within the scope of the rule.
  • TypeID – “ScriptedAlertRule.PowershellProbe.DataSource” – This is the datasource we created above and are referencing to pass our variables Interval, TimeoutSeconds, FileLocation, and bDebug to.

ConditionDetection – The property bag that we created in the script, and passed up throught the datasource is surfaced here where we use the data stored in it to determine if there is an error condition for the property of “Result” equaling “BAD”. If the condition detection is met the rule moves along to the write action. These can be made with multiple expressions for more advanced logic if necessary. We’ll go over that some other time.

WriteAction – This is the configuration for the alert.

  • ID – “GenerateAlert” – This is the unique name for this write action within the scope of the rule.
  • TypeID – “Health!System.Health.GenerateAlert” – Health! is a reference to the System.Health.Library MP and the System.Health.GenerateAlert is a write action within it. System.Health.GenerateAlert Documentation: https://msdn.microsoft.com/en-us/library/ee809352.aspx
  • Priority – Alert Priority – 0-2 (low, medium, high)
  • Severity – Alert Severity – 0-2 (information, warning, error)
  • AlertmessageID – “$MPElement[Name=”ScriptedAlertRule.PowerShellProbe.AlertRule_AlertMessageResourceID”]$” – This is the unique name given to the alert and is used for providing “friendly” name within the console, more on this in a moment (see “string resource” below).
  • AlertParameters – These are used as variables that can be surfaced to the alert message within the console. Here’s a good resource for various options for parameters: http://blogs.technet.com/b/kevinholman/archive/2007/12/12/adding-custom-information-to-alert-descriptions-and-notifications.aspx. The alert parameter for this rule is the FileLocation name which was referenced from the property bag created in the PowerShell Script.

String Resource – This one is kinda weird, but in essence it represents the alert message and is required if you’re going to create an alert. Notice its the same as the AlertMessageID (without all the MPElement drapery).

PowerShellAlertRuleStringResource

Language Packs – Not required but if you want your stuff to look pretty in the console you’ll want to take the time to configure these.

PowerShellAlertRuleLanguagePack

  • ID – ENU – English. you can do multiple languages for any given anything so keep that in mind if you want to do multilingual MPs.
  • Default – Default language I think? Also, If you split up your language pack definitions in visual studio like I do it will get angry if you put true on one and false on another. Or maybe its all of them…. I’m not sure, for my purposes false is grand and it will likely be the same for you.
  • DisplayString ElementID – This corresponds to those unique TypeID identifiers for rules and alerts and provides friendly display data for them in the console.
  • Remember that AlertParameter earlier in the Writeaction? This is where you can reference the variable in the KB for the console. Take notice that the AlertParameter1 begins at 1 and goes on to Alertparameter2, 3, 4 etc but the reference in the description begins at zero {0} and then {1}, {2}, etc. I’ve always thought it odd that one reference sequence begins at 1 and the other at 0. Just thought I’d point it out as something to keep in mind when trying to reference the correct variable.
  • KnowledgeArticle – This is the KB for the alert, pretty straight forward. check the documentation if you want more info.

That’s all there is to it, fun stuff. I think next time we’ll circle back and go over seed based discoveries.

Site Index – https://leibundgutski.wordpress.com/2015/09/23/scom-visual-studio-authoring-site-index/