Advertisements

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

29 comments so far

  1. […] Update: This script was intended for vSphere 4 and had a bunch of limitations. For a newer version with more functionality check out FixingITpro.com. […]

  2. Sebastien Frenette on

    Dead link (404 not found) on Dropbox for the script.
    https://dl.dropbox.com/u/1995609/Set-vCenterAlarmEmails.ps1

    • Marquis Calmes on

      I just updated the blog. On Friday I discovered that VMware renamed several alarms in 5.1 (some times just adding a period at the end) so 5.1 requires a different script than 5.0.

  3. JL on

    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

    • Marquis Calmes on

      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.

  4. JL on

    Hi

    Yes I am using the 5.0 script.

    I will double check the alarms names and post here my findings.

    Thank You

    JL

  5. Raoul Schaffner on

    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.

    • Martin Fronsmons on

      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?

      • Marquis Calmes on

        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.

  6. […] thanks to fixingitpro.com for this updated vCenter 5.1 version, it worked perfectly for […]

  7. Todd Ouimet on

    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

    • Marquis Calmes on

      Yeah. The script was definitely built for greenfield deployment. I’m sure some clever person could build in a way around it.

  8. Mike on

    Great Script! Exactly what I was looking for and very concise! Great work.

  9. Chris Smith on

    Love it!

  10. John Ball on

    Worked great in 5.1. Thanks!

  11. kristof on

    Is also a script for version 5.5 available? Thanks

  12. Scott McNeil on

    Hey, this looks great, but I can’t get it to run in my environment (5.1), for all alarms, it says The name “….Alarm Name…..” already exists, any ideas?

    • Marquis Calmes on

      I haven’t seen that error and my guys run this in 5.1 environments frequently. What specific build of vSphere 5.1 are you running?

      • Scott McNeil on

        Build 1364037, if it makes a difference it the VMWare infrastructure was upgraded recently from 4.1

  13. Pirmin on

    Links are dead, can you post it again?

  14. Chris on

    Will this work in 6? Thanks.

    • Marquis Calmes on

      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).

  15. Sam on

    Some Alarms do not exist anymore in 6, but other than that it works great! Thank you!

  16. Just being friendly on

    I think this one should work for 6.0 if you don’t want to do a few edits
    http://pastebin.com/3UwvhWHK

  17. […] Source: https://fixingitpro.com/2012/07/09/powercli-script-to-automatically-setup-vcenter-alarm-email-notifi… […]


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

Advertisements
%d bloggers like this: