Wednesday, February 22, 2012

Retrieving SCOM monitor thresholds by Powershell

Part of managing the SCOM infrastructure will include knowing what thresholds are being configured for the countless monitors that are available from the many Management Packs that comes from Microsoft or other vendors.
To compile all these thresholds tying them to their respective counters and monitors proves to be a challenge.
There is some powershell cmdlets that are provided on the Microsoft Site that enables us to do that but the thresholds suck as “Logical Disk Free Space” does not appear in the exported CSV.
The result will be something as per the format in the screenshot below


I have made some amendments to the cmdlet to provide this as below.
Other thresholds that are missing can be added by getting the information of the tag from the management pack xml or using the SCOM Management Pack Authoring console that can be downloaded from http://www.microsoft.com/download/en/details.aspx?id=18222. (Example of the tag for the threshold I added for logical Disk Free Space is as the screenshot below)


PowerShell Cmdlet (To use copy the entire contents and save it as <your filename>.ps1

    function GetThreshold ([String] $configuration)
    {
    $config = [xml] ("<config>" + $configuration + "</config>")
    $threshold = $config.Config.Threshold
    if($threshold -eq $null)
   {
    $threshold = $config.Config.MemoryThreshold
    }
    if($threshold -eq $null)
    {
    $threshold = $config.Config.CPUPercentageThreshold
    }
    if($threshold -eq $null)
    {
    if($config.Config.Threshold1 -ne $null -and $config.Config.Threshold2 -ne $null)
    {
                    $threshold = "Error threshold is: " + $config.Config.Threshold1 + " Warning threshold is: " + $config.Config.Threshold2
    }
    }
    if($threshold -eq $null)
    {
    if($config.Config.ThresholdWarnSec -ne $null -and $config.Config.ThresholdErrorSec -ne $null)
    {
     $threshold = "warning threshold is: " + $config.Config.ThresholdWarnSec + " error threshold is: " + $config.Config.ThresholdErrorSec
    }
    }
if($threshold -eq $null)
{
if($config.Config.SystemDriveErrorPercentThreshold -ne $null -and $config.Config.SystemDriveErrorMBytesThreshold -ne $null -and $config.Config.NonSystemDriveErrorMBytesThreshold -ne $null -and $config.Config.SystemDriveWarningPercentThreshold -ne $null)
{
$threshold = "System Drive Error/Warning(%) is: " + $config.Config.SystemDriveErrorPercentThreshold + "% / " + $config.Config.SystemDriveWarningPercentThreshold + "% System Drive Error/Warning (MB) threshold is: " + $config.Config.SystemDriveErrorMBytesThreshold + "%/ " + $config.Config.SystemDriveWarningMBytesThreshold + ". " + "Non-System Drive Error/Warning (%) is : " + $config.Config.NonSystemDriveErrorPercentThreshold + "% / " + $config.Config.SystemDriveWarningPercentThreshold + "%. " + "% Non-System Drive Error/Warning (MB) threshold is: " + $config.Config.NonSystemDriveErrorMBytesThreshold + "%/ " + $config.Config.NonSystemDriveWarningMBytesThreshold
}
}
    if($threshold -eq $null)
    {
     if($config.Config.LearningAndBaseliningSettings -ne $null)
     {            
      $threshold = "no threshold (baseline monitor)"
    }
    }
    return $threshold
    }
Function GetFrequency ([String] $configuration)
{
$config = [xml] ("<config>" + $configuration + "</config>")
$Frequency = $config.Config.Frequency
if($Frequency -eq $null)
{
$frequency = $config.Config.Frequency;
}
 return ($frequency)
}
Function GetNumsamples ([String] $configuration)
{
$config = [xml] ("<config>" + $configuration + "</config>")
$Samples = $config.Config.Samples
if($Samples -eq $null)
{
$Samples = $config.Config.NumSamples;
}
 return ($Samples)
}
Function GetCounterName ([String] $configuration)
{
$config = [xml] ("<config>" + $configuration + "</config>")
$Counter = $config.Config.Counter
if($Counter -eq $null)
{
$Counter = $config.Config.CounterName;
}
 return ($Counter)
}
Function GetObject ([String] $configuration)
{
$config = [xml] ("<config>" + $configuration + "</config>")
$Object = $config.Config.Object
if($Object -eq $null)
{
$Object = $config.Config.ObjectName;
}
 return ($Object)
}

    $perfMonitors = get-monitor -Criteria:"IsUnitMonitor=1"

$perfMonitors | select-object @{Name="MP";Expression={ foreach-object {$_.GetManagementPack().DisplayName }}},@{name="Target";expression={foreach-object {(Get-MonitoringClass -Id:$_.Target.Id).DisplayName}}},DisplayName,enabled,@{name="Threshold";expression={foreach-object {GetThreshold $_.Configuration}}}, @{name="Frequency";expression={foreach-object {GetFrequency $_.Configuration}}}, @{name="Samples";expression={foreach-object {GetNumSamples $_.Configuration}}}, @{name="Counter";expression={foreach-object {GetCounterName $_.Configuration}}}, @{name="Object";expression={foreach-object {GetObject $_.Configuration}}} | sort Target, DisplayName | export-csv "c:\temp\monitor_thresholds.csv"

10 comments:

  1. Vincent, will this work for any MP?

    ReplyDelete
    Replies
    1. Hi,

      The powershell script will list all available MPs installed.

      Delete
  2. Hi Vincent,

    I am being encountered with the lot of errors while running the query.

    Msg 156, Level 15, State 1, Line 1
    Incorrect syntax near the keyword 'function'.
    Msg 126, Level 15, State 1, Line 5
    Invalid pseudocolumn "$threshold".
    Msg 126, Level 15, State 1, Line 9
    Invalid pseudocolumn "$threshold".
    Msg 126, Level 15, State 1, Line 13
    Invalid pseudocolumn "$threshold".
    Msg 126, Level 15, State 1, Line 15
    Invalid pseudocolumn "$config".
    Msg 126, Level 15, State 1, Line 20
    Invalid pseudocolumn "$threshold".
    Msg 126, Level 15, State 1, Line 22
    Invalid pseudocolumn "$config".
    Msg 126, Level 15, State 1, Line 27
    Invalid pseudocolumn "$threshold".
    Msg 126, Level 15, State 1, Line 29
    Invalid pseudocolumn "$config".
    Msg 126, Level 15, State 1, Line 34
    Invalid pseudocolumn "$threshold".
    Msg 126, Level 15, State 1, Line 36
    Invalid pseudocolumn "$config".
    Msg 126, Level 15, State 1, Line 41
    Invalid pseudocolumn "$threshold".
    Msg 126, Level 15, State 1, Line 47
    Invalid pseudocolumn "$Frequency".
    Msg 126, Level 15, State 1, Line 51
    Invalid pseudocolumn "$frequency".
    Msg 126, Level 15, State 1, Line 57
    Invalid pseudocolumn "$Samples".
    Msg 126, Level 15, State 1, Line 61
    Invalid pseudocolumn "$Samples".
    Msg 126, Level 15, State 1, Line 67
    Invalid pseudocolumn "$Counter".
    Msg 126, Level 15, State 1, Line 71
    Invalid pseudocolumn "$Counter".
    Msg 126, Level 15, State 1, Line 77
    Invalid pseudocolumn "$Object".
    Msg 126, Level 15, State 1, Line 81
    Invalid pseudocolumn "$Object".
    Msg 102, Level 15, State 1, Line 86
    Incorrect syntax near '@'.

    ReplyDelete
  3. Hi,

    Try to download the ps1 from this location

    https://www.dropbox.com/s/6jmspzirt4rgcag/Threshold.ps1

    let me know if it works for you

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Hello,

    The script looks great, can I please get a copy, I have copied the code from your page, but when I ran it, it generated the following error:


    Get-Monitor : The 'Path' parameter is empty or the required provider location is not set."
    At C:\temp\Treshold1.ps1:168 char:32
    + $perfMonitors = get-monitor <<<< -Criteria:"IsUnitMonitor=1"
    + CategoryInfo : InvalidArgument: (:String) [Get-Monitor], ArgumentOutOfRangeException
    + FullyQualifiedErrorId : InvalidParameter,Microsoft.EnterpriseManagement.OperationsManager.ClientShell.GetMonitorCmdlet

    Thank you

    ReplyDelete
  6. Hi Prakash,

    You can get a copy of the ps1 via the link below
    https://www.dropbox.com/s/ogkkfwbggakj327/Threshold.ps1?dl=0

    ReplyDelete
  7. so many thank you, it was very helpful

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete