Alarms – Adding an action

An interesting question on Alarms arrived in my mailbox recently. Charlie wanted to know if it was possible to add an action to a selected set of the alarms he has defined in his vCenter.

The current PowerCLI build (version 4 update 1 – build 208462) unfortunately has no cmdlets to work with alarms. There are some alarm-related cmdlets available in the VI Toolkit for Windows Community Extensions. But none of these provides the functionality Charlie wanted to have.

To start with alarm action maintenance, you should be able to report on what is currently defined in the vCenter.

As a reminder, the following types of alarm actions are available in the current vSphere version.

  1. Create a task
  2. Execute a “method” on a specific entity
  3. Run a script
  4. Send an email
  5. Send an SNMP trap

An alarm can have one or more of the above defined.

Inventory

The following script produces a CLIXML file containing all the existing alarms and for each of them a selection of their properties, including the action(s) that are defined.

Why CLIXML and not to a plain XML or CSV file ? The answer is simple: “structure“.

While a CSV file is handy to export object that all have the same layout, it’s quite cumbersome to export objects that have varying content. Same is valid for the ConvertTo-Xml cmdlet that came with PowerShell v2.

Since an alarm can have one or more actions of different types (see above), the easiest serialisation solution was CLIXML and to use the built-in Export-CliXML cmdlet.

One of the disadvantages of the CLIXML format; it’s not obvious for a user to read this type of XML file !

Annotations

Line 1: This function is called recursively (for the GroupAlarmAction). All actions will be returned in an array of PSObjects.

Line 6: This switch handles all possibilities that can be found in the Action property.

Line 7-13: A GroupAlarmAction can contain one or more of the other “basic” actions. This case loops through each of them and calls the Get-AlarmAction function recursively.

Line 14-47: Handles each of the “base” actions and stores the specific properties for each of them in a PSObject. Note that the New-Object format that is used here is only valid in PowerShell v2 RTM.

Line 55: If the GetAlarm method is called with the $null parameter it will return all alarms defined in the vCenter.

Line 68: The report with all the alarms is saved to an XML file. The -NoClobber parameter makes sure n existing file is not overwritten.

As you can notice from the following extract, the content of the file is rather hard to read if you’re not used to the CLIXML format..

The file was opened with XMLPad in the Tabular view.

Quick report

To get a quick listing of the file contents you can execute the following line

This will produce a listing in the console window looking like this.

Which is definitely more legible than the CLIXML file itself.

Add an action

The following script will allow you to add an action on a selected set of alarms.

Annotations

Line 2: The variable $tgtAlarms is an array of strings where each element is the name of an alarm where you want to add an action.

Line 5-32: Uncomment one of the actions. This will be the action that is added to the alarms defined in $tgtAlarms.

Line 37-67: In this switch construction the new action is set up.

Line 68-77: The new action requires one or more triggers, in other words when will the action be executed. In the script I defined the triggers to be fired when the alarm goes from green to yellow and when the alarm goes from yellow to red. If you require other triggers change the contents of the AlarmTriggeringAction object.

Line 79-91: These lines make sure the new action is added in the correct place. Note that the script uses the GroupAlarmAction to combine all the actions. This to avoid the problem I mentioned in Alarm expressions – Part 1 : Metric alarms where your alarm settings are not editable anymore in the vSphere client.

Line 93-100: The AlarmSpec object is set up. Except for the Action property all properties are copied from the existing alarm.

Line 102: The ReconfigureAlarm method is used to add the new action.

Line 105-107: Get all the defined alarms.

Line 110-115: A simple loop to find all the alarms that were specified in the $tgtAlarms variable. For each of the specified alarms the Set-AlarmAction is called.

After the script is executed you will see the new action on the selected alarms.

Notice the triggers in the four columns on the right.

16 Comments

    NMED

    Hi Luc; very good post, but i’don’t know after an export and after to have deleted all alarms in my vCenter to imprt a alrm-actions.xml file?
    Thank you again for your Help.

      LucD

      @NMED, there are some good export-import functions from Angel in the Script to export/import Alarms between VC or synchronize alarms between 2 Virtual Centers thread on the PowerCLI Community.
      Did you try those ?

        mahednra

        i have tried this power script but while importing is not success please help to how to import script

          LucD

          Hi, not sure what you are trying to do here.
          What exactly are you trying to import ?

    Cal

    Hello Luc,

    Could you tell me if it is possible to add an alarm on the Resource Pools (CPU usage) ? And if yes, how one can manage to implement it ? We kind of tried but all we got so far is an error : “A Specified parameter was not correct”.

    Charles

      LucD

      Hi Cal, I assume you also raised a similar question in the PowerCLI Community (where I just answered).

      Afaik it’s not possible to define a metric alarm for a Resource Pool.
      My understanding is that the metric alarms use “realtime” metrics to decide if an alarm needs to fire or not.

      The Resource Pool metrics that you see in the vSphere client are “aggregated values“, in other words they only exist for the Historical Intervals, not for the Realtime interval. See my PowerCLI & vSphere statistics – Part 1 – The basics post for more info on these intervals.

      So I’m afraid you can only have a CPU/MEM alarm for hosts and virtual machines.

    Twanoo

    Hello Luc!

    I have a problem using your script on my environment,
    when I configure an email alert by your script I get an event “cannot send email to …”
    and no email is received in my mailbox.

    beside when I configure it manually, I get the “sent email to…” event,
    and the email is properly receive.

    Any thougths for me?

      LucD

      @Twanoo, just tried the script and it works for me.
      Could you perhaps PM me with a bit more information (for example screenshot of the Alarm settings before and after,…) ?

    Twano_O

    @Charlie
    Hi Charlie,

    I met the exact same error as you with the “Datastore usage on disk” reconfiguration,
    did you found the solution for that one?

    Charlie

    Luc,

    I’m leaving this here because we are just gettting around to trying this and my skills are still lacking. I ran into an issue with “Datastore usage on disk”, it’s the last of 19 alarms that we updated. The rest of the alarms are updated correctly.

    Here is the error:

    Exception calling “ReconfigureAlarm” with “1” argument(s): “A specified parameter was not correct.

    At C:\Users\clonergan.INNPROC\Downloads\Powershell\Scripts\UpdateListedAlarmsInVSphere.ps1:102 char:25
    + $alarm.ReconfigureAlarm <<<< ($spec)
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

    Again Thanks for all your help!!

    Charlie

      LucD

      Hi Charlie, I would need a bit more information on what you were trying to do.
      What was already in the “Datastore usage on disk” alarm ?
      And which action were you trying to add ?

      If you want you can email me some screenshots.
      Use lucd (at) lucd (dot) info

    Jonathan Phillips

    Excellent post and exactly what we needed to document the alarms we have in each of our Virtual Centers.

    You saved me hours of picking apart the VMWare SDK looking for the right properties/methods.

    Shane

    Looks good!

    Question for you though, how would this be modified so as to:
    – email me@address.com if a user id other than those listed in “allowed_users.txt” logged onto the vc?

    Thanks!

      LucD

      Love the idea.
      I’ll have a look how this could be implemented.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.