Introduction

This is the 12th instalment of the Automating OpsMgr series. Previously on this series:

From now on, I will start concentrating on creating various monitoring workflows (rules, monitors, template instances, etc) using the OpsMgrExtended module. I will dedicate at least 6-7 posts on this topic. Since OpsMgr is a monitoring solution, I am now getting to the core offering of this module – providing ways for OpsMgr professionals to automate the creation of their monitoring requirements. In this post, I will demonstrate a runbook utilising New-OMPerformanceCollectionRule activity from the OpsMgrExtended module, to create performance collection rules in OpsMgr.

Runbook New-PerfCollectionRule

Workflow New-PerfCollectionRule
{
  Param(
    [Parameter(Mandatory=$true)][String]$RuleName,
    [Parameter(Mandatory=$true)][String]$RuleDisplayName,
    [Parameter(Mandatory=$true)][String]$CounterName,
    [Parameter(Mandatory=$true)][String]$ObjectName,
    [Parameter(Mandatory=$false)][String]$InstanceName,
    [Parameter(Mandatory=$true)][String]$ClassName,
    [Parameter(Mandatory=$false)][Boolean]$RuleDisabled
  )

  #Get OpsMgrSDK connection object
  $OpsMgrSDKConn = Get-AutomationConnection -Name "OpsMgrSDK_HOME"

  #Hard code which MP to use
  $MPName = "TYANG.Test.Windows.Monitoring"

  #Hard code frequency (900 seconds)
  $Frequency = 900

  #Create Performance Collection Rule, MP Version will be increased by 0.0.0.1
  $RuleCreated = InlineScript
  {
    #Validate rule Name
    If ($USING:RuleName -notmatch "([a-zA-Z0-9]+\.)+[a-zA-Z0-9]+")
    {
      #Invalid rule name entered
      $ErrMsg = "Invalid rule name specified. Please make sure it only contains alphanumeric charaters and only use '.' to separate words. i.e. 'Your.Company.Percentage.Processor.Time.Performance.Collection.Rule'."
      Write-Error $ErrMsg
    } else {
      #Name is valid, creating the rule
      New-OMPerformanceCollectionRule -SDKConnection $USING:OpsMgrSDKConn -MPName $USING:MPName -RuleName $USING:RuleName -RuleDisplayName $USING:RuleDisplayName -ClassName $USING:ClassName -CounterName $USING:CounterName -ObjectName $USING:ObjectName -InstanceName $USING:InstanceName -Frequency $USING:Frequency -Disabled $USING:RuleDisabled -IncreaseMPVersion $true
    }
  }

  If ($RuleCreated)
  {
    Write-Output "Rule `"$RuleName`" created."
  } else {
    Throw "Unable to create rule `"$RuleName`"."
  }
}

In order to use this runbook, you firstly need to modify line 14 with the name of the SMA connection to your OpsMgr management group:

image

I have also hardcoded few other parameters in the runbook:

image

$MPName is the name of the unsealed MP where the rule is going to be saved to, and $Frequency is the interval in seconds on how often does this perf collection rule need to run. You also need to modify these 2 variables, especially the $MPName – the unsealed MP must exist in your management group already.

This runbook requires the following input parameters:

  • $RuleName – name of the perf collection rule
  • $RuleDisplayName – The display name of the perf collection rule
  • $CounterName – name of the perf counter you need to collect
  • $ObjectName – name of the object where the counter belongs to (i.e. memory, logical disk, etc.)
  • $InstanceName (optional) – name of the instance of the counter. if not specified, the rule will collect All Instances.
  • $ClassName – name of the OpsMgr monitoring class of which the perf collection rule is targeting
  • $RuleDisabled – Boolean variable (true or false). specify if the rule should be left disabled by default

Runbook execution result:

image

Rule configuration (from the console):

image

image

Accessing Perf data collected by this rule in a Perf view:

SNAGHTMLb5a869e

Conclusion

In this post, I have demonstrated how to use a runbook to create a performance collection rule in OpsMgr. In the next post, I will demonstrate how to create a 2-state performance monitor.

Leave a comment