PowerCLI Script to automatically setup vCenter Alarm Email Notification.


After manually setting up email Alarm notifications for numerous vSphere deployments I decided it was time to harness PowerCLI and automate the process.

I found numerous examples of people who had already done this before me, so I figured it would be pretty easy.  It looks like the guys at VMPros.nl built a script that several others adapted. VirtuallyMikeBrown had created a script for vSphere 4.1, but it wasn’t updated for vSphere 5 and only had a single email address.  Justin at Justin’s IT Blog had created a script that allowed three email addresses, but it couldn’t handle 4 or 2 or just 1 without editing the script or using dummy addresses.   And both of these scripts used a variable for each vCenter Alarm which meant if you added or removed an alarm you had to make multiple edits throughout the script.

So I decided I wanted my script to do the following:

  • Use arrays for both the alarms and email addresses to make it easier to add and remove entries.
  • Use a foreach loop to work through the alarms.
  • Have different classification or priorities for alarms so that some would notify repeatedly and with different frequencies. For example. You might not care if a VM is maxing out it’s CPU, but you definitely want to know if a host is.

These additional requirements took “easy” and turned it into a frustrating half-day, but I feel pretty good about the result.

Some notes about the script:

  • The $user, $pass and $vCenterServer variables need to be edited to be correct for your environment.
  • This currently sets up email alerts for almost every single default alarm in vCenter (based on 5.0).  This is undoubtedly overkill and you’ll likely want to remove some from the lists.
  • In the current setup this script takes a while to run through all the alarms.  It took about 10 minutes for me.  But that’s still a lot faster than doing it manually.
  • If you add or remove an alarm from the list make sure the following syntax is maintained:
    • Alarm names should be in double-quotes (“)
    • Alarms should be separated by commas (,)
    • Back-ticks (`) are used to continue a breakup the single line of alarm names into multiple lines for readability.   All alarms should have a back-tick at the end of the line except the last in the array.
  • If you want to add or remove email addresses make sure they too are in double-quotes, separated by commas with no spaces.
  • If you want to change the frequency of the repeating alerts just change the second number in the  “-ActionRepeatMinutes (60 * 24)” section.

This script is provided with no guarantees or warranties.  Use at your own risk.  Comments and feedback are welcome.

*********Updated 10/29/2012*******************

VMware decided to rename some alarms in 5.1 so I’ve now have two versions of the script.

vSphere 5.0 Script

vSphere 5.1 Script

Update 3/18/2015

Today I learned two things to be aware of:

  1. If the password contains special characters (see below) then those characters need to be escaped by prefixing the back tick or grave accent character (`).  So the password P@$$word would need to be entered as “P@`$`$word”
    1. Special characters in PowerShell are $ (  ) * + . [  ] ? \ / ^ { } |
  2. Since the vCenter Server Appliance uses Sendmail the delimiter between multiple email addresses has been changed from a semicolon to a comma. Because the PowerCLI command is the one formatting the entry with the semi-colon I’m not sure yet how to fix this.  (Thanks to AdminAfterWork for pointing this out)
Advertisements

31 thoughts on “PowerCLI Script to automatically setup vCenter Alarm Email Notification.

  1. Hi

    For me doesn’t work(vCenter 5.0.0 build 623373). All alarms have the same error:

    Get-AlarmDefinition : 1/16/2013 3:12:36 PM Get-AlarmDefinition AlarmDefinition with name ‘Datastore usage on disk’ was not found, using the specified filter(s).

    At C:\Users\edualuc\Documents\VMware\Scripts\PowerShell\vCenter Alarms.ps1:110 char:24
    + Get-AlarmDefinition <<<< -Name "$HighPriorityAlarm" | Get-AlarmAction -ActionType SendEmail | New-AlarmActionTrigger -StartStatus "Yellow" -EndStatus "Green"
    + CategoryInfo : ObjectNotFound: (:) [Get-AlarmDefinition], VimException
    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Alarm.GetAlarmDefinition

    Do you know what may be the problem?

    Thanks

    JL

    1. Make sure you are using the 5.0 script. I’m not sure what build number I built the script with originally, but this was the error that required building a new script for 5.1. If it still doesn’t work it could be the alarm names are different from when I built the script. You can compare them by opening PowerCLI and running the command “Get-AlarmDefinition | ft name” and compare the output to the names listed in the script. VMware seems to like to change alarm names for some reason (which is why there is a different script for 5.0 and 5.1). Maybe they changed them in a patch. Be sure to post what you find.

  2. Hi

    you might need to set the locale to english in the script:

    $sessionManager = Get-View -Id $global:DefaultVIServer.ExtensionData.Content.SessionManager
    $sessionManager.SetLocale(“en-US”)

    Cheers,
    Raoul.

    1. Thanks for the script! It worked with Raoul’s additional code.

      I noticed that 2 alarms don’t generate as many e-mails as they should: “Virtual machine memory usage and “Virtual machine CPU usage” – I receive mails for green->yellow and for red->green, but not for all other alarm changes. In the events tab for the virtual machine I can see that all alarm changes are reported and logged, but they do not trigger an action. The reason might be the condition length (=10 Minutes) for their triggers, since the VM only exceeds the threshold for a couple of minutes (less than 10), but shouldn’t I get additional emails nevertheless?

      1. I’ve seen similar behavior. My suspicion is that you are correct about the trigger condition length (by default 5 minutes) is the reason. For example, Suppose a VM is at 85% for 5 minutes, then it moves 95% for 3 minutes (into the red but not for the 5 minutes requried), then it drops to below 75% for 5 minutes. This would trigger a green to yellow email, and a red to green email but never hit the condition length for the yellow to red email. I am by no means a vCenter alarm expert, so I’m just guessing here.

  3. Great script!!! However…..I have a very specific issue. Running the 5.0 script on vCenter 5.0 U2 everything works great UNLESS “Enter maintenance mode”.action is preexisting on the alarm. If that action exists then the script can only setup a non-repeating Yellow to Red email notification. All other configuration such as repeat every 2 hours, email on Green to Yellow, Red to Yellow and Yellow to Green fail to setup. Here are the errors I get:

    Get-AlarmAction : 5/5/2013 2:58:00 AM Get-AlarmAction Object reference not set to an instance of an object.
    At C:\alarms.ps1:22 char:69
    + Get-AlarmDefinition -Name “$HighPriorityAlarm” | Get-AlarmAction <<<< -ActionType SendEmail| Remove-AlarmAction -Confirm:$false
    + CategoryInfo : NotSpecified: (:) [Get-AlarmAction], ViError
    + FullyQualifiedErrorId : Client20_AlarmServiceImpl_GetAlarmAction_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Alarm.GetAlarm
    Action

    Get-AlarmAction : 5/5/2013 2:58:02 AM Get-AlarmAction Object reference not set to an instance of an object.
    At C:\alarms.ps1:22:25 char:69
    + Get-AlarmDefinition -Name "$HighPriorityAlarm" | Get-AlarmAction <<<< -ActionType SendEmail | New-AlarmActionTrigger -StartStatus "G
    reen" -EndStatus "Yellow"
    + CategoryInfo : NotSpecified: (:) [Get-AlarmAction], ViError
    + FullyQualifiedErrorId : Client20_AlarmServiceImpl_GetAlarmAction_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Alarm.GetAlarm
    Action

    Get-AlarmAction : 5/5/2013 2:58:03 AM Get-AlarmAction Object reference not set to an instance of an object.
    At C:\alarms.ps1:22:26 char:69
    + Get-AlarmDefinition -Name "$HighPriorityAlarm" | Get-AlarmAction <<<< -ActionType SendEmail | Get-AlarmActionTrigger | Select -First
    1 | Remove-AlarmActionTrigger -Confirm:$false
    + CategoryInfo : NotSpecified: (:) [Get-AlarmAction], ViError
    + FullyQualifiedErrorId : Client20_AlarmServiceImpl_GetAlarmAction_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Alarm.GetAlarm
    Action

    Get-AlarmAction : 5/5/2013 2:58:04 AM Get-AlarmAction Object reference not set to an instance of an object.
    At C:\alarms.ps1:22:27 char:69
    + Get-AlarmDefinition -Name "$HighPriorityAlarm" | Get-AlarmAction <<<< -ActionType SendEmail | New-AlarmActionTrigger -StartStatus "Y
    ellow" -EndStatus "Red" -Repeat
    + CategoryInfo : NotSpecified: (:) [Get-AlarmAction], ViError
    + FullyQualifiedErrorId : Client20_AlarmServiceImpl_GetAlarmAction_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Alarm.GetAlarm
    Action

    Get-AlarmAction : 5/5/2013 2:58:04 AM Get-AlarmAction Object reference not set to an instance of an object.
    At C:\alarms.ps1:22:28 char:69
    + Get-AlarmDefinition -Name "$HighPriorityAlarm" | Get-AlarmAction <<<< -ActionType SendEmail | New-AlarmActionTrigger -StartStatus "R
    ed" -EndStatus "Yellow"
    + CategoryInfo : NotSpecified: (:) [Get-AlarmAction], ViError
    + FullyQualifiedErrorId : Client20_AlarmServiceImpl_GetAlarmAction_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Alarm.GetAlarm
    Action

    Get-AlarmAction : 5/5/2013 2:58:05 AM Get-AlarmAction Object reference not set to an instance of an object.
    At C:\alarms.ps1:22:29 char:69
    + Get-AlarmDefinition -Name "$HighPriorityAlarm" | Get-AlarmAction <<<< -ActionType SendEmail | New-AlarmActionTrigger -StartStatus "Y
    ellow" -EndStatus "Green"
    + CategoryInfo : NotSpecified: (:) [Get-AlarmAction], ViError
    + FullyQualifiedErrorId : Client20_AlarmServiceImpl_GetAlarmAction_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Alarm.GetAlarm
    Action

    Thanks!!
    Todd

    1. I don’t know. I haven’t had a chance to test yet. It can probably easily be updated if it doesn’t. Historically all VMware changed was the names of the default alerts (for whatever reason).

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s