Alarm expressions – Part 2 : Event alarms
In the previous part of this series (Alarm expressions – Part 1 : Metric alarms) I showed how you could create alarms that are triggered when a metric crosses a watermark.
In this part I will show you how to create alarms when one or more specific events occur in your vSphere environment. More specifically I will show you how to create an alarm that will fire when someone adds or removes a license from your vCenter.
Warning: this function will not work in vCenter 4.x when you are using the builtin licensing instead of the separate License Manager. I’ll post an update soon.
The first step for creating alarms based on this type of triggers is to define which event(s) to use. As I showed in Events, Dear Boy, Events – Part 2, there are an enormous number of possible events to chose from.
With the CSV file from that blog entry it’s easy to find out which events, related to licensing, are available. I used the following script for this
$targetStr = "license"
Import-Csv "C:\Events.csv" -UseCulture | where {$_.Name -match $targetStr -or $_.Description -match $targetStr}
From the output it is clear that I will have to use events from the EventEx type.
In the EventAlarmExpression description it says that one needs to pass an eventTypeId. Unfortunately the Reference Guide doesn’t explain how to get such an eventTypeId. After some trial and error I discovered you can get that property via the Extensionmanager. The following short script list all the extensions registered in your vSphere environment and shows their eventTypeIds.
$extMgr = Get-View ExtensionManager
$extMgr.ExtensionList | %{
if($_.EventList -ne $null){
$_.Key
$_.EventList | %{
"`t" + $_.EventId
}
}
}
The output of this script shows what I need.
The following script puts it all together and creates the alarm.
$dcName = "MyDatacenter" $mailto = "luc.dekens@lucd.info" $alarmMgr = Get-View AlarmManager $entity = Get-Datacenter $dcName | Get-View # AlarmSpec $alarm = New-Object VMware.Vim.AlarmSpec $alarm.Name = "Add/remove license" $alarm.Description = "Add or remove a license" $alarm.Enabled = $TRUE # Action - Send email $alarm.action = New-Object VMware.Vim.GroupAlarmAction $trigger1 = New-Object VMware.Vim.AlarmTriggeringAction $trigger1.action = New-Object VMware.Vim.SendEmailAction $trigger1.action.ToList = $mailTo $trigger1.action.Subject = "License added/removed" $trigger1.Action.CcList = "" $trigger1.Action.Body = "" # Transition 1a - yellow --> red $trans1a = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec $trans1a.StartState = "green" $trans1a.FinalState = "yellow" # Transition 1b - red --> yellow $trans1b = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec $trans1b.StartState = "yellow" $trans1b.FinalState = "green" $trigger1.TransitionSpecs += $trans1a $trigger1.TransitionSpecs += $trans1b $alarm.action.action += $trigger1 # Expression 1 - License added $expression1 = New-Object VMware.Vim.EventAlarmExpression $expression1.EventType = $null $expression1.eventTypeId = "com.vmware.license.AddLicenseEvent" $expression1.objectType = "Datacenter" $expression1.status = "yellow" # Expression 2 - License removed $expression2 = New-Object VMware.Vim.EventAlarmExpression $expression2.EventType = $null $expression2.eventTypeId = "com.vmware.license.RemoveLicenseEvent" $expression2.objectType = "Datacenter" $expression2.status = "yellow" $alarm.expression = New-Object VMware.Vim.OrAlarmExpression $alarm.expression.expression += $expression1 $alarm.expression.expression += $expression2 $alarm.setting = New-Object VMware.Vim.AlarmSetting $alarm.setting.reportingFrequency = 0 $alarm.setting.toleranceRange = 0 # Create alarm. $alarmMgr.CreateAlarm($entity.MoRef, $alarm)
Annotations:
Line 40 & 47: although the SDK Reference says that the eventTypeId property replaces the EventType property you have to explicitly set the EventType property to $null. Otherwise the CreateAlarm method will fail
You can now automate the creation of your event-driven alarms.


Luc,
When I run the script above it creates the alarm fine, however when I try to edit the alarm after creation I get the following error: Not initialized: vmodl.TypeName.eventType.
I tested the alarm and could not get it to alert either. Testing with vCenter 4.0U2 & PowerCLI 4.1. Any thoughts?
@Jason, you’re right. This doesn’t work anymore in vCenter 4.x due to the changed licensing extension. I’ll have to rewrite the script.