Automating OpsMgr Part 16: Creating Windows Service Monitors

OpsMgrExntededIntroduction

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

I will dedicate this post and the next post to creating monitoring solutions for Windows services. In this post, I will demonstrate how to create a basic Windows service monitor using the New-OMServiceMonitor function from the OpsMgrExtended module.

You can access the help document for this function using the Get-Help cmdlet:

Get-Help New-OMServiceMonitor –Full

SNAGHTMLb42d9c

Sample Runbook: New-ServiceMonitor

Workflow New-ServiceMonitor
{
Param(
[Parameter(Mandatory=$true)][String]$MonitorName,
[Parameter(Mandatory=$true)][String]$MonitorDisplayName,
[Parameter(Mandatory=$true)][String]$ClassName,
[Parameter(Mandatory=$true)][String]$ParentMonitor,
[Parameter(Mandatory=$true)][String]$ServiceName,
[Parameter(Mandatory=$true)][Boolean]$IgnoreStartupType,
[Parameter(Mandatory=$true)][Boolean]$UnhealthyWhenRunning,
[Parameter(Mandatory=$true)][String]$UnhealthyState,
[Parameter(Mandatory=$false)][Boolean]$MonitorDisabled
)

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

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

#Make sure MP exists
Write-Verbose "Getting management pack '$MPName'"
$MP = Get-OMManagementPack -SDKConnection $OpsMgrSDKConn -Name $MPName -ErrorAction SilentlyContinue
If ($MP -eq $null)
{
#MP doesn't exist, create it
Write-Verbose "management pack '$MPName' does not exist. creating now."
$CreateMP = New-OMManagementPack -SDKConnection $OpsMgrSDKConn -Name $MPName -DisplayName "TYANG Test Windows Monitoring" -Version "1.0.0.0"
}

#Create Service Monitor, MP Version will be increased by 0.0.0.1
$MonitorCreated = InlineScript
{

#Validate Monitor Name
If ($USING:MonitorName -notmatch "([a-zA-Z0-9]+\.)+[a-zA-Z0-9]+")
{
#Invalid Monitor name entered
$ErrMsg = "Invalid monitor name specified. Please make sure it only contains alphanumeric charaters and only use '.' to separate words. i.e. 'Your.Company.Windows.Time.Service.Monitor'."
Write-Error $ErrMsg
} else {
#Name is valid, creating the monitor
Write-Output "Creating the servivce monitor `"$MonitorName`"..."

New-OMServiceMonitor -SDKConnection $USING:OpsMgrSDKConn -MPName $USING:MPName -MonitorName $USING:MonitorName -MonitorDisplayName $USING:MonitorDisplayName -ClassName $USING:ClassName -ParentMonitor $USING:ParentMonitor -ServiceName $USING:ServiceName -UnhealthyState $USING:UnhealthyState -IgnoreStartupType $USING:IgnoreStartupType -UnhealthyWhenRunning $USING:UnhealthyWhenRunning -Disabled $USING:MonitorDisabled -IncreaseMPVersion $true
}
}

If ($MonitorCreated)
{
Write-Output "Monitor `"$MonitorName`" created."
} else {
Write-Error "Unable to create monitor `"$Monitorname`"."
}

}

I have hardcoded the following parameters in the runbook:

  • SMA OpsMgr connection object name (which you will need to change to suit your environment)
  • (Unsealed) MP (where the rule  is going to be saved to) – “TYANG.Test.Windows.Monitoring”

Additionally, this runbook will firstly try to retrieve the management pack from the management group, if the MP deosn’t exist, it will create it first.

This runbook takes the following input parameters:

  • ClassName – The name of the target monitoring class (i.e.Microsoft.Windows.Server.OperatingSystem)
  • UnhealthyState– The unhealthy state of the monitor (either warning or error).
  • ServiceName–The name of the Windows service (i.e. w32time)
  • IgnoreStartupType– Set this Boolean parameter to True if you want the monitor to become unhealthy and generate alerts even when the service startup type is not set to “Automatic”. More details about this parameter can be found from Kevin Holman’s blog post Monitoring Windows Services – Automatic, Manual, and Disabled, using CheckStartupType
  • UnhealthyWhenRunning – Set this Boolean parameter to True when you want the monitor to become unhealthy and generate alert when the service is running (instead of stopped)
  • MonitorDisabled– Boolean, whether the event monitor should be disabled by default
  • MonitorDisplayName– Display name of the unit monitor
  • MonitorName – The name of the unit monitor
  • ParentMonitor – The parent dependency monitor for the event unit monitor

Runbook Execution Result:

image

Monitor Properties from OpsMgr Console:

image

image

image

image

Conclusion

In this post, I have shown how to create a basic windows service monitor using a simple SMA / Azure Automation runbook with New-OMServiceMonitor function in the OpsMgrExtended module. In the next post, I will show you how to create an instance of the Windows Service management pack template using the OpsMgrExtended module.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: