icinga-powershell-plugins icon indicating copy to clipboard operation
icinga-powershell-plugins copied to clipboard

Feature request - more thresholds for Invoke-IcingaCheckPerfcounter

Open audiocoach opened this issue 1 year ago • 1 comments

With Invoke-IcingaCheckPerfcounter it is possible to specify an array of perfcounters to be checked but only one warning and one critical threshold. It would be great if you can specify individual thresholds for each checked perfcounter.

audiocoach avatar Jan 30 '24 22:01 audiocoach

I have two possible solutions for that.

Solution 1 - Use Warnings- and Criticals-Hashtable

Unfortunatley hashtables are currently not supported for custom datafields within icinga director or at least I haven't found anything about it. So this solution probably only works from the cli because I don't know how to pass a hashtable from icinga director to the powershell framework plugins. But maybe this solution is interessting for the future if some day hashtables will be supported.

Add the follwoing to the param section in the Invoke-IcingaCheckPerfCounter.psm1 located at C:\Program Files\WindowsPowerShell\Modules\icinga-powershell-plugins\plugins:

         [hashtable]$Warnings       = @{},
         [hashtable]$Criticals      = @{},

Change Line 113 and 120 from

                $IcingaCheck.WarnOutOfRange($Warning).CritOutOfRange($Critical) | Out-Null;

to

                $IcingaCheck.WarnOutOfRange($Warnings["Warning-$counter"]).CritOutOfRange($Criticals["Critical-$counter" | Out-Null; 

With that you can define individual thresholds for each checked counter. The Thresholds have to be defined in an hashtable as follows: "Warning-CounterName" = "ThresholdValue", e.g. for the counter "\processor()% processor time": "Warning-\processor()% processor time" = "20" "Critical-CounterName = ThresholdValue", e.g. for the counter "\processor()% processor time" : "Critical-\processor()% processor time" = "30"

Example:

PS C:\> icinga -developermode { Invoke-IcingaCheckPerfCounter -PerfCounter @('\processor(*)\% processor time', '\Memory\Available Bytes') -Warnings @{"Warning-\processor(*)\% processor time" = "20";"Warning-\Memory\Available Bytes" = "1048576"} -Criticals @{"Critical-\processor(*)\% processor time" = "30";"Critical-\Memory\Available Bytes" = "2097152"} -NoPerfData -Verbosity 2}

Output:

[CRITICAL] Performance Counter [CRITICAL] \Memory\Available Bytes
\_ [CRITICAL] \Memory\Available Bytes
   \_ [CRITICAL] \Memory\Available Bytes: 1519448000 is greater than threshold 2097152
\_ [OK] \processor(*)\% processor time
   \_ [OK] \processor(_Total)\% processor time: 0.782748
   \_ [OK] \processor(0)\% processor time: 0.001500
   \_ [OK] \processor(1)\% processor time: 3.126453
   \_ [OK] \processor(2)\% processor time: 9.524347
   \_ [OK] \processor(3)\% processor time: 3.03145
2

Solution 2 - Use Warnings- and Criticals-Array

This solution should work for both. CLI and icinga director but it is probably not the smartest solution because I am not very experienced with programming. Maybe some Powershell Geeks out there have a better solution. If so please feel free to comment.

For this solution I made the following changes to the Invoke-IcingaCheckPerfCounter.psm1 located at C:\Program Files\WindowsPowerShell\Modules\icinga-powershell-plugins\plugins:

Add the follwoing to the param section :

        [array]$Warnings        = @(),
        [array]$Criticals       = @(),

Add the following below the param section to create a Warning Variable for each array entry

    foreach ($entry in $Warnings) {
    $WarningName = ($entry -split "=")[0]
    $WarningValue = ($entry -split "=")[1]
    New-Variable -Name $WarningName -Value $WarningValue -Force
    }

    foreach ($entry in $Criticals) {
    $CriticalName = ($entry -split "=")[0]
    $CriticalValue = ($entry -split "=")[1]
    New-Variable -Name $CriticalName -Value $CriticalValue -Force
    }

Add the following below the line "$IcingaCheck = New-IcingaCheck -Name $counter -Value $Counters[$counter].Value -MetricIndex $CounterInfo.Category -MetricName $CounterInfo.Counter;"

                $IcingaCheckWarning = Get-Variable -Name "Warning-$counter" -ValueOnly -ea ignore
                $IcingaCheckCritical = Get-Variable -Name "Critical-$counter" -ValueOnly -ea ignore

And change line

                $IcingaCheck.WarnOutOfRange($Warning).CritOutOfRange($Critical) | Out-Null;

to

                $IcingaCheck.WarnOutOfRange($IcingaCheckWarning).CritOutOfRange($IcingaCheckCritical) | Out-Null;

Add the following below the line "$IcingaCheck = New-IcingaCheck -Name $instanceName -Value $instance.Value -MetricIndex $CounterInfo.Category -MetricName $CounterInfo.CounterInstance;"

                $IcingaCheckWarning = Get-Variable -Name "Warning-$counter" -ValueOnly -ea ignore
                $IcingaCheckCritical = Get-Variable -Name "Critical-$counter" -ValueOnly -ea ignore

And change line

                $IcingaCheck.WarnOutOfRange($Warning).CritOutOfRange($Critical) | Out-Null;

to

                $IcingaCheck.WarnOutOfRange($IcingaCheckWarning).CritOutOfRange($IcingaCheckCritical) | Out-Null;

With that you can define individual thresholds for each checked counter. The Thresholds have to be defined in an array as follows: "Warning-CounterName=ThresholdValue", e.g. for the counter "\processor()% processor time": "Warning-\processor()% processor time=20" "Critical-CounterName=ThresholdValue", e.g. for the counter "\processor()% processor time" : "Critical-\processor()% processor time=30"

Example:

PS C:\Program Files\WindowsPowerShell\Modules\icinga-powershell-framework> icinga -developermode { Invoke-IcingaCheckPerfCounter -PerfCounter @('\processor(*)\% processor time', '\Memory\Available Bytes') -Warnings @('Warning-\processor(*)\% processor time=20', 'Warning-\Memory\Available Bytes=1048576') -Criticals @('Critical-\processor(*)\% processor time=20', 'Critical-\Memory\Available Bytes=1048576') -NoPerfData -Verbosity 2}

Output:

[CRITICAL] Performance Counter [CRITICAL] \Memory\Available Bytes, \processor(*)\% processor time
\_ [CRITICAL] \Memory\Available Bytes
   \_ [CRITICAL] \Memory\Available Bytes: 1508471000 is greater than threshold 1048576
\_ [CRITICAL] \processor(*)\% processor time
   \_ [CRITICAL] \processor(_Total)\% processor time: 35.3699 is greater than threshold 20
   \_ [CRITICAL] \processor(0)\% processor time: 60.92133 is greater than threshold 20
   \_ [CRITICAL] \processor(1)\% processor time: 27.85477 is greater than threshold 20
   \_ [CRITICAL] \processor(2)\% processor time: 60.58743 is greater than threshold 20
   \_ [OK] \processor(3)\% processor time: 12.82452
2

For those interessted, I have attached the full code for both solutions as text-file.

Solution1-Invoke-IcingaCheckPerfcounter-with-hashtable.txt

Solution2-Invoke-IcingaCheckPerfcounter-with-array.txt

audiocoach avatar Mar 20 '24 16:03 audiocoach