Create Custom Powershell CPU Monitor

Posted by : on

powershell   operationsManager   guides

:book: Introduction

Recently I had a case and I needed to assist my customer with a CPU Monitor that targets all Windows Computers. The customer is using the Powershell Authoring Community MP by Cookdown. I will document my steps to get this working as intended.

:red_circle: Prerequisites

:heavy_check_mark: Example MP

You can get a copy of the Management Pack I created in this guide, here:
Custom Monitor for CPU Usage :arrow_left: Direct Download Link

:notebook: Note

This guide will require you to edit the Management Pack directly, be aware that you need to be careful when editing any Management Pack to ensure you do not accidently change the wrong thing.

 

:page_with_curl: Start of Guide

Step 1. Create a new Unit Monitor

You will need to create a new Unit Monitor.

  1. Open the SCOM Console and navigate to the Authoring Tab -> Monitors -> Right Click Monitors -> Hover over Create a Monitor and select Unit Monitor…:
    Create Unit Monitor in SCOM Console
  2. Open the Scripting folder -> PowerShell Based -> PowerShell Script Two State Monitor (Community)
    Select Scripting and PowerShell Script
  3. Select the appropriate Management Pack to save the new Monitor.
    It is HIGHLY recommended to save to a new MP instead of any existing MPs!
  4. Select Next >
  5. Type in an appropriate name / description, I used: Custom CPU Monitor
  6. Change the Monitor Target to Windows Computer
    Select Windows Computer as Target
  7. Click Next >
    Verify data matches
  8. Run every 15 minutes by default, this is a good interval to start with. Click Next >
  9. Type in the File Name you want to use, I used: my_custom_cpu_monitoring_script.ps1
  10. Copy and Paste the below script into the Script section and click Next >:
    $api = New-Object -ComObject "MOM.ScriptAPI";
    $PropertyBag = $api.CreatePropertyBag();
    [int]$Result = [int]((Get-Counter "\Processor(_Total)\% Processor Time").CounterSamples.CookedValue);
    $PropertyBag.AddValue("CPUUsage", $Result);
    $PropertyBag
    

    Copy and paste the above script to your script pane

  11. Copy and paste the below, when everything below has been copied, click Next >:
    • Parameter Name column: Property[@Name="CPUUsage"]
    • Operator: Greater than or equal to
    • Value: 50 Verify values are set for Unhealthy expression
  12. Copy and paste the below, when everything below has been copied, click Next >:
    • Parameter Name column: Property[@Name="CPUUsage"]
    • Operator: Less than
    • Value: 50 Verify values are set for Healthy expression
  13. Optional: Change Unhealthy from Warning to Critical. Configure Monitor Health
  14. Click the Generate alerts for this monitor checkbox and change the Alert Description text to this:
    The CPU has reached at or above 50% usage.
    
          Current CPU usage is: $Data/Context/Property[@Name="CPUUsage"]$%
    
  15. Done creating the unit monitor, now onto the more advanced stuff!

Step 2. Modify the Management Pack

We will need to modify the Management Pack in order to allow the expressions to evaluate correctly. The Monitor is setup to use string values instead of integers, which will cause problems when we try to evaluate the health of the monitor.

  1. Go to Administration Tab -> Management Packs -> Installed Management Packs -> Search for the Management Pack where your Monitor is saved. Select and export the Management Pack to any location.
    Export Management Pack
  2. Navigate to the exported Management Pack xml file, open the MP XML with Notepad.
  3. Find and replace:
    <XPathQuery>Property[@Name="CPUUsage"]</XPathQuery>
    • Replace any occurrences with the following:
      <XPathQuery Type="Integer">Property[@Name="CPUUsage"]</XPathQuery>
    • The above change allows us to interpret the output as integer, instead of string.
  4. Find and replace:
    <Value Type="String">50</Value>
    • Replace any occurrences with the following:
      <Value Type="Integer">50</Value>
    • The above change allows us to interpret the output as integer, instead of string.
  5. Modify Line 5 (the version of the Management Pack) in the xml file from:
    <Version>1.0.0.0</Version>
    to
    <Version>1.0.0.1</Version>
  6. Save the Management Pack XML and import the Management Pack back into your environment.

Leave some feedback if this helped you! :v:

Page Views


Share on:
About Blake Drumm
Blake Drumm

I like to collaborate and work on projects. My skills with Powershell allow me to quickly develop automated solutions to suit my customers, and my own needs.

Email :

Website :

About Blake Drumm

My name is Blake Drumm, I am working on the Azure Monitoring Enterprise Team with Microsoft. Currently working to update public documentation for System Center products and write troubleshooting guides to assist with fixing issues that may arise while using the products. I like to blog on Operations Manager and Azure Automation products, keep checking back for new posts. My goal is to post atleast once a month if possible.

Follow @blakedrumm
Useful Links