4

In vSphere Client on a VM host in the Alarms tab there is a view of Triggered Alarms.

How can I get those alarms using PowerCLI?

Andrew J. Brehm
  • 1,611
  • 7
  • 34
  • 57

3 Answers3

4

this is what i use.

$esx = Get-VMHost $vmhost | Get-View
foreach($triggered in $esx.TriggeredAlarmState){
  $alarmDef = Get-View -Id $triggered.Alarm
  Write-Host $alarmDef.Info.Name
}
johnshen64
  • 5,747
  • 23
  • 17
  • I had tried that but it never returned anything at all, while the "Triggered Alarms" view in vSPhere Client showed my triggered alarm. (I created a triggered alarm based on a VM being used more than 1% CPU over 30 seconds as a test alarm.) – Andrew J. Brehm Jul 04 '13 at 08:25
  • You may need to look at the vm view in that case. I am not sure if vm alarms are included in the vmhost view. – johnshen64 Jul 05 '13 at 14:04
4

To find all vmhosts with alarm status = "red", I expanded on this.

#
# Find all hosts with triggered alarms in "Red" state
#
$esx_all = Get-VMHost | Get-View
$Report=@()
foreach ($esx in $esx_all){
    foreach($triggered in $esx.TriggeredAlarmState){
        If ($triggered.OverallStatus -like "red" ){
            $lineitem={} | Select Name, AlarmInfo
            $alarmDef = Get-View -Id $triggered.Alarm
            $lineitem.Name = $esx.Name
            $lineitem.AlarmInfo = $alarmDef.Info.Name
            $Report+=$lineitem
        } 
    }
}
$Report |Sort Name | export-csv "c:\temp\ESX-Host-Red-Alarms.csv" -notypeinformation -useculture
Invoke-item "c:\temp\ESX-Host-Red-Alarms.csv"
Falcon Momot
  • 24,975
  • 13
  • 61
  • 92
Doug Cowie
  • 56
  • 2
0

To expand a bit on johnshen64's PowerCLI answer, I found Luc Dekens' post on communities.vmware.com to be very helpful:

foreach($dc in (Get-Datacenter | where {$_.ExtensionData.triggeredAlarmState})){
    $dc.ExtensionData.triggeredAlarmState |
    Select @{N="Entity";E={Get-View $_.Entity | Select -ExpandProperty Name}},
        @{N="Alarm";E={Get-View $_.Alarm | Select -ExpandProperty Info | Select -ExpandProperty Name}},Time,OverallStatus,
        @{N='vCenter';E={$dc.Uid.Split('@:')[1]}}
}

... and I ended up using a variation of that:

foreach ($toplevel in    @(Get-VDSwitch    | Where-Object { $_.ExtensionData.TriggeredAlarmState.count -gt 0 }) +
                         @(Get-Datacenter  | Where-Object { $_.ExtensionData.TriggeredAlarmState.count -gt 0 })) {
    foreach ($alarm in $toplevel.ExtensionData.TriggeredAlarmState) {
        $localt = [System.TimeZoneInfo]::ConvertTime($alarm.time, [System.TimeZoneInfo]::Local)
        $entity = Get-View $alarm.Entity
        $alarmview = Get-View $alarm.Alarm
        Write-Output ("Alarm: At top-level entity $($toplevel.Name), $($entity.Name) went " +
                        "$($alarm.OverallStatus) at $($localt) for $($alarmview.Info.Name)")
    }
}

The idea is to loop through each top-level entity that the alarms could roll up to. I discovered through trial and error that Get-VDSwitch and Get-Datacenter covered what I needed. The other tricky part is to realize that the Entity and Alarm objects inside the alarm are references to the actual objects, so to get details about the actual item, you have to dereference it with Get-View.

Jeff Schaller
  • 519
  • 6
  • 17