PowerCLI – Alarms

Introduction

I’ve decided to share my PowerCLI scripts that I think were useful.

First script I would like to share is to generate Alarm definitions. One thing to notice is that this script will only load alarms with Actions, e.g. Send SNMP, Send Mail…etc.

The following script should be executed once you are connected to vCenter server(s).

Script

output = foreach ($alarm in (Get-AlarmDefinition | Sort Name | Get-AlarmAction))
{
    $threshold = foreach ($expression in ($alarm | %{$_.AlarmDefinition.ExtensionData.Info.Expression.Expression}))
    {
        if ($expression.EventTypeId -or ($expression | %{$_.Expression}))
        {
            if ($expression.Status) { switch ($expression.Status) { "red" {$status = "Alert"} "yellow" {$status = "Warning"} "green" {$status = "Normal"}}; "" + $status + ": " + $expression.EventTypeId } else { $expression.EventTypeId }
        }
        elseif ($expression.EventType)
        {
            $expression.EventType
        }
        if ($expression.Yellow -and $expression.Red)
        {
            if (!$expression.Yellow) { $warning = "Warning: " + $expression.Operator } else { $warning = "Warning: " + $expression.Operator + " to " + $expression.Yellow };
            if (!$expression.Red) { $alert = "Alert: " + $expression.Operator } else { $alert = "Alert: " + $expression.Operator + " to " + $expression.Red };
            $warning + " " + $alert
        }
    }
    $alarm | Select-Object @{N="Alarm";E={$alarm | %{$_.AlarmDefinition.Name}}},
                           @{N="Description";E={$alarm | %{$_.AlarmDefinition.Description}}},
                           @{N="Threshold";E={[string]::Join(" // ", ($threshold))}},
                           @{N="Action";E={if ($alarm.ActionType -match "SendEmail") { "" + $alarm.ActionType + " to " + $alarm.To } else { "" + $alarm.ActionType }}}
}
$output | Export-Csv alarm.csv -UseCulture -NoTypeInformation

Sample Output

Alarm Description Threshold Action
Cannot connect to storage Default alarm to monitor host connectivity to storage device vprob.storage.connectivity.lost // vprob.storage.redundancy.lost // vprob.storage.redundancy.degraded // esx.problem.vmfs.nfs.server.disconnect SendSNMP
Exit standby error Default alarm to monitor if a host cannot exit standby mode ExitStandbyModeFailedEvent // DrsExitStandbyModeFailedEvent SendSNMP
Health status changed alarm Default alarm to monitor changes to service and extension health status HealthStatusChangedEvent SendSNMP
Virtual Machine with Snapshots Testing alarm to notify if someone creates a snapshot on a certain virtual machine. Warning: isAbove to 1048576 Alert: isAbove to 2097152

Hope this helps to people who wants to review alarms created within vCenter servers.

PowerCLI – Alarms

One thought on “PowerCLI – Alarms

  1. Hugo says:

    Excellent post, just one note at the beginning of the code
    output = foreach
    must be
    $output = foreach

    With $ at the first since you are declaring a variable.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s