VMware Cloud Community
DZ1
Hot Shot
Hot Shot

Adding email to multiple alarms. (Used Project Onyx to help create the bulk of the script)

First, I want to thank everyone who has ever helped me on these forums, I have really learned a lot, and special thanks to LucD, since he seems to superman down the most and save me. 


Ok, here is my issue.  I used Project Onyx to create an alarm, and I was able to successfully take the output and change some things so that I could create alarms in multiple Datacenters.  Then I used Onyx again, to add an email notification, and I edited that out, but now I have a problem.  I created a csv file, so that each Datacenter will have a specific email account that will get notified, that works just fine, but the problem is when the script edits the alarm.


I think the main problem is at the bottom in red, in Onyx it outputs the name of the user that made the change to the alarm ($spec.lastModifiedUser), and of course my account name was the one used.  There is also a timestamp ($spec.lastModifiedTime). 


I've looked at some alarm scripting that LucD had as a post, but it's really out of my league.  I also tried to comment out some code for the user, but it appears to be mandatory, and I don't know how to dynamically make the username appear, or the timestamp.  I thinking that there is some way to take those sections out and create the alarm, but I don't know how to do that either.


  Here is the code.  The green part is stuff that I changed, the black is all onyx, and then the red is where I think the problem is. 


if ( (Get-ExecutionPolicy) -ne "Unrestricted" ) {


Write-Warning "Your Execution policy prohibits this script from running, please type this command at the Powershell prompt without the quotes: 'Set-ExecutionPolicy Unrestricted'"


}


#Checking for the Snapin to use PowerCLI cmdlets, the Snapin will be added if it's not already.


if ((Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null) {


Add-PSSnapin -Name VMware.VimAutomation.Core


}


$EmailList = Import-Csv c:\List\EmailTest.csv


Get-folder 'folder' | Get-Datacenter | foreach {


$DC = $_


if ( $_ | Get-AlarmDefinition | where { $_.Name -match "$($DC.Name)" + " Storage Alarm" } ) {


$SpecAlarmVal = $DC.Id.Split("-",2)[1]


$SpecEnVal = $DC.Id.Split("-",2)[1]


$AlarmidName = (Get-AlarmDefinition -Entity $DC | where { $_.name -eq "$($DC.name)" + " Storage Alarm" } ).id

  1. ------- QueryOptions -------</div>


$_this = Get-View -Id 'OptionManager-VpxSettings'


$_this.QueryOptions("mail.smtp.server")

  1. ------- QueryOptions -------</div>


$_this = Get-View -Id 'OptionManager-VpxSettings'


$_this.QueryOptions("mail.sender")

  1. ------- ReconfigureAlarm -------</div>


$spec = New-Object VMware.Vim.AlarmInfo


$spec.name = "$($DC.Name)" + " Storage Alarm"


$spec.description = "$($DC.Name)" + " Custom Storage Alarm"


$spec.enabled = $true


$spec.expression = New-Object VMware.Vim.OrAlarmExpression


$spec.expression.expression = New-Object VMware.Vim.AlarmExpression[] (1)


$spec.expression.expression[0] = New-Object VMware.Vim.MetricAlarmExpression


$spec.expression.expression[0].operator = "isAbove"


$spec.expression.expression[0].type = "Datastore"


$spec.expression.expression[0].metric = New-Object VMware.Vim.PerfMetricId


$spec.expression.expression[0].metric.counterId = 176


$spec.expression.expression[0].metric.instance = ""


$spec.expression.expression[0].yellow = 7500


$spec.expression.expression[0].red = 9000


$spec.action = New-Object VMware.Vim.GroupAlarmAction


$spec.action.action = New-Object VMware.Vim.AlarmAction[] (1)


$spec.action.action[0] = New-Object VMware.Vim.AlarmTriggeringAction


$spec.action.action[0].action = New-Object VMware.Vim.SendEmailAction


$spec.action.action[0].action.toList = ($EmailList | where { $_ -match "$($DC.name)" } | select Email).Email


$spec.action.action[0].action.ccList = ""


$spec.action.action[0].action.subject = ""


$spec.action.action[0].action.body = ""


$spec.action.action[0].transitionSpecs = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec[] (2)


$spec.action.action[0].transitionSpecs[0] = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec


$spec.action.action[0].transitionSpecs[0].startState = "yellow"


$spec.action.action[0].transitionSpecs[0].finalState = "red"


$spec.action.action[0].transitionSpecs[0].repeats = $false


$spec.action.action[0].transitionSpecs[1] = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec


$spec.action.action[0].transitionSpecs[1].startState = "green"


$spec.action.action[0].transitionSpecs[1].finalState = "yellow"


$spec.action.action[0].transitionSpecs[1].repeats = $false


$spec.action.action[0].green2yellow = $false


$spec.action.action[0].yellow2red = $false


$spec.action.action[0].red2yellow = $false


$spec.action.action[0].yellow2green = $false


$spec.setting = New-Object VMware.Vim.AlarmSetting


$spec.setting.toleranceRange = 0


$spec.setting.reportingFrequency = 0


$spec.key = ""


$spec.alarm = New-Object VMware.Vim.ManagedObjectReference


$spec.alarm.type = "Alarm"


$spec.alarm.Value = $SpecAlarmVal


$spec.entity = New-Object VMware.Vim.ManagedObjectReference


$spec.entity.type = "Datacenter"


$spec.entity.Value = $SpecEnVal


$spec.lastModifiedTime = https://communities.vmware.com/System.DateTime::Parse("2014-03-26T17:25:13.456628Z")


$spec.lastModifiedUser = "Domain\User"


$spec.creationEventId = 0


$_this = Get-View -Id "$AlarmidName"


$_this.ReconfigureAlarm($spec)


}


}

Reply
0 Kudos
5 Replies
LucD
Leadership
Leadership

Have a look at Angel's last comment in Managing vSphere Alarms with PowerCLI

That might be what you're after, and no need to go Onyx, you can do it with a regular PowerCLI cmdlet :smileygrin:


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
DZ1
Hot Shot
Hot Shot



I'm looking at it, it looks great.  I should be able to implement that.  I'll get to it.  

Reply
0 Kudos
DZ1
Hot Shot
Hot Shot



I can't believe this, I simply didn't look into the default cmdlets first.  Since there weren't any cmdlets for creating a brand new alarm definition, I just browsed over what was there, and didn't dig deeper for what I needed.  On the bright side, trying to do it the hard way is making this part much easier.  Thanks for your help.  Also, I would really like to learn about Powershell with .NET, and just things in general that go past the normal cmdlets.  Any recommendations?  I guess I want more developer type stuff, even though I'm no where near a developer, reading some of it can only help.  

Reply
0 Kudos
LucD
Leadership
Leadership

There are a number of links in My PS library post.

But the best information sources are still Google/Bing and some good key words.

And of course this community :smileygrin:


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
DZ1
Hot Shot
Hot Shot



Thanks again, I was able to create a script that emails alerts based on the Datacenter.  I'll also check out some of the links and books.  I have a few of those books already, but I can never have too many.  

Reply
0 Kudos