Monitoring REST APIs with PowerShell and SCOM

Looking to get some REST endpoints monitors quickly? Follow the below steps to setup a could REST monitors in about 15 minutes (weather in this demo).

Pre-requisites

You'll need to get the PowerShell authoring management pack, which can be downloaded free at https://cookdown.com/scom-essentials/powershell-authoring/

The recording of my session from SCOMathon will also be needed, which you can find below. We hope you were able to make it live, but if not we're excited to offer the recording and this blog post so you can get the scripts for your environment.

Follow along with the SCOMathon recording.

Stage one in our REST eco-system

image-4-1024x576.png

Check out our other blog posts to get your alerts out to webhooks too!

We're going to be pulling data from the OpenWeather API and evaluating it within the SCOM system. This will be done using some core PowerShell to get the data the we'll pass the data into the SCOM system.

PowerShell Core Code

image-5.png

Only a couple of lines needed

Using the Invoke-RestMethod cmdlet we will pull the payload from api.openweathermap.org. Once the payload is fetched PowerShell will conveniently convert it into an easy to use object. The example above created below.

image-6.png

Easy to get to API data

REST Based Performance

Use the below script in your PowerShell performance rule to collect the current weather at you Geo Locations

# Any Arguments specified will be sent to the script as a single string.
# If you need to send multiple values, delimit them with a space, semicolon or other separator and then
# use split.param([string]$Arguments)$ScomAPI = New-Object -comObject "MOM.ScriptAPI"
# Collect your performance counter here.  Note that in order to support the DW this script MUST only return a single
# object/counter and those must be static values.  You can return multiple instances just fine though.

$lat = $Arguments.Split(',')[0]$long = $Arguments.Split(',')[1]
$apiKey = "60ab6601123412341232412341234"
$units = "metric" #standard/imperial options
$restUrl = "https://api.openweathermap.org/data/2.5/weather?lat=$lat&lon=$long&appid=$apiKey&units=$units"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$weatherResults = Invoke-RestMethod -Uri
$restUrl$PropertyBag = $ScomAPI.CreatePropertyBag()
$PropertyBag.AddValue("Metric", $weatherResults.main.temp)
$PropertyBag.AddValue("Instance","Main")

# Send output to SCOM
$PropertyBag

REST Based 3-State Monitor

Use the below script in your 3-State PowerShell script monitor. Then map the resolution states as shown in the video.

# Any Arguments specified will be sent to the script as a single string.
# If you need to send multiple values, delimit them with a space, semicolon or other separator and then
# use split.param([string]$Arguments)

$ScomAPI = New-Object -comObject "MOM.ScriptAPI"
$PropertyBag = $ScomAPI.CreatePropertyBag()
$lat = $Arguments.Split(',')[0]
$long = $Arguments.Split(',')[1]
$apiKey = "60ab66019123412346b61df99ac7730eb5"
$units = "metric" #standard/imperial options
$restUrl = "https://api.openweathermap.org/data/2.5/weather?lat=$lat&lon=$long&appid=$apiKey&units=$units"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$weatherResults = Invoke-RestMethod -Uri $restUrl
$PropertyBag.AddValue("State",$weatherResults.weather[0].main)

# Send output to SCOM
$PropertyBag

If you made it this far you will have pulled data from the REST API of your choosing, used PowerShell to capture it as an object and then setup a 3-state monitor in SCOM to surface the data as alerts.

I used the Open Weather API as an example but the same method could practically be used for pulling data from anything with a REST API.

See my other blog which contains info on how to send these alerts to Slack (which was part of the same SCOMathon session and so part of the recording).

Previous
Previous

Quick Start - SCOM REST API

Next
Next

My thoughts on SCOMathon 2020 by Leon Laude