Last active
August 29, 2015 13:58
-
-
Save dstreefkerk/10226460 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#requires -version 3 | |
<# | |
.SYNOPSIS | |
Get-ProliantTemperatures - Get HP ProLiant temperature sensor readings and status for PRTG | |
.DESCRIPTION | |
Reads the status of HP ProLiant temperatures sensors via WMI and HP WBEM | |
and | |
http://www.hp.com/go/HPwbem | |
.OUTPUTS | |
XML formatted for a PRTG Advanced Script Sensor. | |
Numeric status per channel. | |
.LINK | |
http://daniel.streefkerkonline.com/monitoring-hp-proliant-via-powershell-wmi-temperature-sensors/ | |
.NOTES | |
Written By: Daniel Streefkerk | |
Website: http://daniel.streefkerkonline.com | |
Twitter: http://twitter.com/dstreefkerk | |
Todo: Nothing at the moment | |
Change Log | |
https://gist.github.com/dstreefkerk/10226460 | |
#> | |
Param( | |
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$false,HelpMessage='Name of the server to connect to')] | |
[String] | |
$ServerName | |
) | |
#region Setup | |
$operationalStatus = @{ | |
'0'="Unknown" | |
'2'="OK" | |
'3'="Degraded" | |
'6'="Error" | |
} | |
$healthStates = @{ | |
'0'="Unknown" | |
'5'="OK" | |
'10'="Degraded" | |
'15'="Minor" | |
'20'="Major" | |
'25'="Critical" | |
'30'="Non-Recoverable" | |
} | |
# I'm setting an arbitrary warning threshold at 75% | |
$warningThreshold = 0.75 | |
#endregion | |
# First, test that we can use WMI | |
$wmiErrorVariable = @() | |
try { | |
$computerSystem = Get-WmiObject -Namespace ROOT\CIMV2 -Class Win32_ComputerSystem -ComputerName $ServerName -ErrorAction Stop -WarningAction SilentlyContinue -ErrorVariable wmiErrorVariable | |
} | |
catch [System.UnauthorizedAccessException] { | |
$wmiError = $true | |
$wmiErrorMessage = "WMI Access to $ServerName is denied. Error: $($_.Exception.Message)" | |
} | |
catch [System.Runtime.InteropServices.COMException] { | |
$wmiError = $true | |
$wmiErrorMessage = "Couldn't query $ServerName via WMI. Error: $($_.Exception.Message)" | |
} | |
catch { | |
$wmiError = $true | |
$wmiErrorMessage = $_.Exception.Message | |
} | |
finally { | |
if ($wmiError -eq $true) { | |
"<prtg>" | |
"<error>1</error>" | |
"<text>$wmiErrorMessage $($_.Exception.Message)</text>" | |
"</prtg>" | |
Exit | |
} | |
} | |
# Get all of the sensors via WMI | |
$tempSensors = Get-WmiObject -Namespace ROOT\HPQ -Class HP_NumericSensor -ComputerName $ServerName -WarningAction SilentlyContinue -ErrorAction SilentlyContinue -ErrorVariable wmiError | |
# If the ROOT\HPQ WMI query failed, throw an error. | |
if ($wmiError.Count -gt 0) { | |
"<prtg>" | |
"<error>1</error>" | |
"<text>The Insight WBEM Management Providers don't seem to be installed on $($ServerName.ToUpper()). Error: $($wmiError.Exception.Message)</text>" | |
"</prtg>" | |
Exit | |
} | |
# If there are no temp sensors | |
if ($tempSensors.Count -eq 0) { exit } | |
$customSensors = @() | |
foreach ($sensor in $tempSensors) { | |
$thisSensor = New-Object psobject | |
# If we can't get a temperature reading, skip to the next sensor | |
if (($sensor | Get-Member *currentreading*) -eq $null) { | |
continue | |
} | |
# A name for the channel | |
$thisSensor | Add-Member -MemberType "NoteProperty" -Name "Name" -Value "$($sensor.name): $((Select-String -InputObject $sensor.description -Pattern '(?<=detects for )(.*)(?=. Temp)' -AllMatches).Matches[0].Value)" | |
# The temperature reading | |
$thisSensor | Add-Member -MemberType "NoteProperty" -Name "Reading" -Value $sensor.CurrentReading | |
# Try and get the upper threshold value if it's supported | |
if (($sensor | Get-Member *UpperThresholdCritical*) -ne $null) { | |
$thisSensor | Add-Member -MemberType "NoteProperty" -Name "LimitMaxError" -Value $($sensor.UpperThresholdCritical) | |
$thisSensor | Add-Member -MemberType "NoteProperty" -Name "LimitMaxWarning" -Value $($sensor.UpperThresholdCritical * $warningThreshold) | |
} | |
# If the Health State of the sensor is not equal to 5 (OK), we'll be setting the whole PRTG sensor to error | |
if ($sensor.HealthState -ne 5) { | |
$thisSensor | Add-Member -MemberType NoteProperty -Name "Error" -Value $true | |
$thisSensor | Add-Member -MemberType NoteProperty -Name "StateMessage" -Value "$($sensor.name) is in the following health state: $($healthStates["$($sensor.HealthState)"]). " | |
} else { | |
$thisSensor | Add-Member -MemberType NoteProperty -Name "Error" -Value $false | |
$thisSensor | Add-Member -MemberType NoteProperty -Name "StateMessage" -Value $null | |
} | |
$customSensors += $thisSensor | |
} | |
# If you don't use PRTG, you could just un-comment the below line | |
#$customSensors | ft -AutoSize; exit; | |
"<prtg>" | |
foreach ($customSensor in $customSensors) { | |
"<result>" | |
"<channel>$($customSensor.name)</channel>" | |
"<value>$($customSensor.reading)</value>" | |
"<unit>Temperature</unit>" | |
if ($customSensor.limitmaxerror -ne $null) { | |
"<LimitMaxError>$([decimal]::round($customSensor.limitmaxerror))</LimitMaxError>" | |
} | |
"<LimitMaxWarning>$([decimal]::round($customSensor.limitmaxwarning))</LimitMaxWarning>" | |
"</result>" | |
} | |
if (($customSensors | where error -eq $true).count -gt 0) { | |
"<error>1</error>" | |
"<text>$($customSensors | where error -eq $true | % {$($_.StateMessage)})</text>" | |
} | |
"</prtg>" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment