Creating vCenter alarms with PowerCLI

I stumbled over some GUI limitations in the vSphere Web Client and the Legacy C# client when trying to create an alarm to monitor vSphere Replication RPO violations, and finally I wondered: Who uses GUIs anyway? ;-) Let's script this task with PowerCLI!

Easier said than done ...

In the latest PowerCLI releases there are some cmdlets for handling alarms (see the Online docs) - they all manipulate only already existing alarms, but there is still no cmdlet available to create a new alarm.

Nevertheless creating a new alarm is also possible via PowerCLI, and Guru Luc Dekens (who else?) showed us a while ago how to do this using the AlarmManager view. My work here is heavily based on a script he posted explaining how to create an alarm that monitors add/remove license events.

Luc's script is from the vCenter 4.x times, so I made it compatible with vCenter 5.1, simplified it a bit by using two of the alarm handling cmdlets that were introduced with PowerCLI 5.x, and changed it to create exactly the alarm that I manually created in my last post:
$mailto = "[email protected]"

$alarmMgr = Get-View AlarmManager

# Create AlarmSpec object
$alarm = New-Object VMware.Vim.AlarmSpec
$alarm.Name = "vSphere Replication RPO violation"
$alarm.Description = "Tracks changes of a VM's vSphere Replication RPO violation status"
$alarm.Enabled = $TRUE

# Event expression 1 - RPO violated by more than 1 minute
# will change state to "Red"
$expression1 = New-Object VMware.Vim.EventAlarmExpression
$expression1.EventType = "EventEx"
$expression1.eventTypeId = "com.vmware.vcHms.rpoViolatedEvent"
$expression1.objectType = "VirtualMachine"
$expression1.status = "red"

# Attribute comparison for expression 1
$comparison1 = New-Object VMware.Vim.EventAlarmExpressionComparison
$comparison1.AttributeName = "currentRpoViolation"
$comparison1.Operator = "notEqualTo"
$comparison1.Value = "1"
$expression1.Comparisons += $comparison1

# Event expression 2 - RPO restored
# will change state back to "Green"
$expression2 = New-Object VMware.Vim.EventAlarmExpression
$expression2.EventType = "EventEx"
$expression2.eventTypeId = "com.vmware.vcHms.rpoRestoredEvent"
$expression2.objectType = "VirtualMachine"
$expression2.status = "green"

# Add event expressions to alarm
$alarm.expression = New-Object VMware.Vim.OrAlarmExpression
$alarm.expression.expression += $expression1
$alarm.expression.expression += $expression2

# Create alarm in vCenter root
$alarmMgr.CreateAlarm("Folder-group-d1",$alarm)
 
# Add action (send mail) to the newly created alarm
Get-AlarmDefinition $alarm.Name | New-AlarmAction -Email -Subject "vSphere Replication RPO violation" -To $mailTo
# New-AlarmAction will automatically add the trigger Yellow->Red (!)

# Add a second trigger for Yellow->Green
Get-AlarmDefinition $alarm.Name | Get-AlarmAction | New-AlarmActionTrigger -StartStatus "Yellow" -EndStatus "Green"
The script assumes that you are already connected to the vCenter server (using Connect-VIServer). Here are some more comments, mainly describing the differences and additions to Luc's original script:

Lines 14 and 29: With vSphere 5.1 you need to set the EventType property to "EventEx" (rather than to $null). Actually this is the only change that is really required to make Luc's original script work with vCenter 5.1.

Lines 13ff: This creates the first alarm expression object (expression1) implementing the "RPO violated" trigger event which will change the alarm status to Red.

Line 20ff: This adds a comparison object (comparison1) as a property of the first alarm expression object (expression1) and implements the trigger condition "currentRpoViolation notEqualTo 1". I tried to use "greaterThan" and other strings for the operator here. The script will run without error and create the alarm no matter what I put as the operator, but whenever I tried to edit such an event in the vSphere Client it throwed an error complaining about an "illegal operator", so I guess you are really limited to what the vSphere Client's dropdown menu contains.

Line 28ff: The second alarm expression object (expression2) implements the "RPO restored" trigger event that will change the alarm status back to Green.

In line 35 to 37 we add the two expressions to the AlarmSpec object before we actually create the alarm definition (in line 40) by calling the CreateAlarm method of the AlarmManager view. The alarm definition is created in the root folder of the vCenter inventory that is referenced by "Folder-group-d1" here. Luc's script shows how to create the alarm in a specific datacenter instead.

Line 43: Adding the alarm action (here: sending an e-mail) and action triggers has been made easier in PowerCLI 5.x by providing the New-AlarmAction and New-AlarmActionTrigger cmdlets. With the action triggers we define the state changes that will fire the alarm action.
It is important to know here that any alarm action must have at least one trigger associated with it. This is why New-AlarmAction will automatically add the action trigger Yellow->Red, and we only need to add the second trigger Yellow->Green (in line 47). It took me a while to figure this out, because the error message that you get when you try to add an already existing trigger for the second time ("Invalid argument") is not very helpful ...

I hope that you will find this script useful, and that it will help you to create your own customized vCenter alarms using PowerCLI!




1 comment:

  1. Hello,
    I am a beginner with vmWare powerCli and I want to thank you about your blog, it’s very helpfull. I have understand your script.

    But I dont know how I can create a Alarm with following settings:
    Name: Host
    Description Host test
    Monitor: Host
    Monitor for specific conditions or state, …
    Trigger Type1: Host Connection state
    Condition: is Equal to
    Warning: None
    Alert: Disconnected
    Trigger Type2: Host Console SwapIn Rate (kBps)
    Condition: is above
    Warning: 1024
    Condition Lenght: for 1 min
    Alert: 4096
    Condition Length: for 1 min
    Action: Run a command
    Configuration: c:\test.ps1

    I will very happy if you can help me to solve my probelm.

    Thank you
    Philippe

    ReplyDelete

***** All comments will be moderated! *****
- Please post only comments or questions that are related to this post's contents!
- Advertising and link spamming will not be tolerated!