Hello:
I need to modify settings for my existing alarm by script. I need to add SNMP action to specific alarm (I know how to filter by name).
Can somebody please help me with it?
Thanks,
qwert
I tested with an alarm that had 1 action (send email) and the adapted script below seems to work.
$entity = Get-View (Get-Folder -Name "Datacenters").ID $alarmMgr = get-view AlarmManager $tgtAlarmName = "Test Alarm" $alarms = $alarmMgr.GetAlarm($null) $alarms | % { $alarm = Get-View $_ if($alarm.Info.Name -eq $tgtAlarmName){ $spec = New-Object VMware.Vim.AlarmSpec $spec.Expression = $alarm.Info.Expression $spec.Setting = $alarm.Info.Setting $spec.Name = $alarm.Info.Name $spec.Enabled = $alarm.Info.Enabled $spec.Description = $alarm.Info.Description $spec.Action = New-Object VMware.Vim.GroupAlarmAction $action = New-Object VMware.Vim.AlarmTriggeringAction $action.Action = New-Object VMware.Vim.SendSNMPAction $trans = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec $trans.StartState = "yellow" $trans.FinalState = "red" $action.TransitionSpecs += $trans $spec.Action.Action = $alarm.Info.Action.Action $spec.Action.Action += $action $alarm.ReconfigureAlarm($spec) } }
Note1: you don't need to do the LoadWithPartialName anymore since VITK 1.5
Note2: for the AlarmManager, the Get-View has a direct pointer. No need to go via ServiceInstance anymore.
Note3: first I copy over the existing actions from $alarm.Info.Action and then I add the new $action
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Have a look at my scripts in CreateAlarm not (always) compatible with the vSphere client.
In there I use a SendSNMPAction action.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Luc,
I know how to create Alarm that does NOT exist yet with action - thanks to your post (see attachment).
However, I cannot figure out how I can just add action to the existing alarm...
I guess, I have to use EditAlarm or ReconfigureAlarm, but have problem with it...
I really appreciate the help.
Thanks,
qwert
You have to use the ReconfigureAlarm method
Something like this
$tgtAlarmName = <alarm-name> $alarmMgr = Get-View AlarmManager $alarms = $alarmMgr.GetAlarm($null) $alarms | %{ $alarm = Get-View $_ if($alarm.Info.Name -eq $tgtAlarmName){ $spec = New-Object VMware.Vim.AlarmSpec $spec.Action = $alarm.Info.Action $spec.Expression = $alarm.Info.Expression $spec.Setting = $alarm.Info.Setting $spec.Name = $alarm.Info.Name $spec.Description = $alarm.Info.Description $alarmTrigger = New-Object VMware.Vim.AlarmTriggeringAction $alarmTrigger.TransitionSpecs = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec $alarmTrigger.TransitionSpecs[0].StartState = $spec.Action.Action[0].TransitionSpecs[0].StartState $alarmTrigger.TransitionSpecs[0].FinalState = $spec.Action.Action[0].TransitionSpecs[0].FinalState $alarmTrigger.Action = New-Object VMware.Vim.SendEmailAction $alarmTrigger.Action.toList = "luc.dekens@lucd.info" $alarmTrigger.Action.CcList = "" $alarmTrigger.Action.Subject = "Alarm subject" $alarmTrigger.Action.Body = "" $spec.Action.Action += $alarmTrigger $alarm.ReconfigureAlarm($spec) } }
I hope this puts you on the right track.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Luc,
Thank you very much for your help. Unfortunately, it’s still does not work.
You code gives error “$alarmTrigger.TransitionSpecs[0].StartState = $spec.Action.Action[ <<<< 0].TransitionSpecs[0].StartState”
Also, looks like I have to add line $spec.Enabled = $alarm.Info.Enabled for the alarm to stay enabled.
I tried to modify the code and in my last draft it looks like this:
http://Reflection.Assembly::LoadWithPartialName("vmware.vim")
$svcRef = new-object VMware.Vim.ManagedObjectReference
$svcRef.Type = "ServiceInstance"
$svcRef.Value = "ServiceInstance"
$serviceInstance = get-view $svcRef
$entity = Get-View (Get-Folder -Name "Datacenters").ID $alarmMgr = get-view $serviceInstance.Content.alarmManager
$tgtAlarmName = "TEST_ALARM"
$alarmMgr = get-view $serviceInstance.Content.alarmManager
$alarms = $alarmMgr.GetAlarm($null)
$alarms | %{
$alarm = Get-View $_
if($alarm.Info.Name -eq $tgtAlarmName){
$spec = New-Object VMware.Vim.AlarmSpec
$spec.Expression = $alarm.Info.Expression
$spec.Setting = $alarm.Info.Setting
$spec.Name = $alarm.Info.Name
$spec.Enabled = $alarm.Info.Enabled
$spec.Description = $alarm.Info.Description
$spec.Action = New-Object VMware.Vim.GroupAlarmAction
$action = New-Object VMware.Vim.AlarmTriggeringAction
$action.Action = New-Object VMware.Vim.SendSNMPAction
$trans = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec
$trans.StartState = "yellow"
$trans.FinalState = "red"
$action.TransitionSpecs += $trans
$alarm.ReconfigureAlarm($spec)
}
}
But when I am running it I got error on my last line:
“Exception calling "ReconfigureAlarm" with "1" argument(s): "Not initialized: vim.alarm.AlarmAction[] action" $alarm.ReconfigureAlarm <<<< ($spec)”
Can you please point me what to mistakes in this code?
Thanks,
qwert
I tested with an alarm that had 1 action (send email) and the adapted script below seems to work.
$entity = Get-View (Get-Folder -Name "Datacenters").ID $alarmMgr = get-view AlarmManager $tgtAlarmName = "Test Alarm" $alarms = $alarmMgr.GetAlarm($null) $alarms | % { $alarm = Get-View $_ if($alarm.Info.Name -eq $tgtAlarmName){ $spec = New-Object VMware.Vim.AlarmSpec $spec.Expression = $alarm.Info.Expression $spec.Setting = $alarm.Info.Setting $spec.Name = $alarm.Info.Name $spec.Enabled = $alarm.Info.Enabled $spec.Description = $alarm.Info.Description $spec.Action = New-Object VMware.Vim.GroupAlarmAction $action = New-Object VMware.Vim.AlarmTriggeringAction $action.Action = New-Object VMware.Vim.SendSNMPAction $trans = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec $trans.StartState = "yellow" $trans.FinalState = "red" $action.TransitionSpecs += $trans $spec.Action.Action = $alarm.Info.Action.Action $spec.Action.Action += $action $alarm.ReconfigureAlarm($spec) } }
Note1: you don't need to do the LoadWithPartialName anymore since VITK 1.5
Note2: for the AlarmManager, the Get-View has a direct pointer. No need to go via ServiceInstance anymore.
Note3: first I copy over the existing actions from $alarm.Info.Action and then I add the new $action
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Luc,
Thank you very much for your help!!!
It does work!!!
Thanks again,
qwert
Hey LucD, can u help me creating the VI Java equivalent of this ?
Here is my code :
<Code>
</Code>
Please help
Thanks in advance.
Message was edited by: apapap
Sorry to disappoint you, but my Java knowledge is $null.
Why don't you try your luck in the VI Java forum ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ok. Thanks for the quick response.
Luc,
Can you please help me figure out what am I missing on my server that preventing me to run "Get-View AlarmManager"?
Here is the error I am getting:
PS C:\temp> $alarmManager = Get-View AlarmManager
Get-View : 5/23/2012 1:00:14 PM Get-View
At line:1 char:25
+ $alarmManager = Get-View <<<< AlarmManager
+ CategoryInfo : NotSpecified: (:) [Get-View], NotAuthenticated
+ FullyQualifiedErrorId : Client20_MoServiceImpl_GetNetInteropView_ViError
,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView
Get-View : 5/23/2012 1:00:14 PM Get-View View with Id 'AlarmManager-
AlarmManager' was not found on the server(s).
At line:1 char:25
+ $alarmManager = Get-View <<<< AlarmManager
+ CategoryInfo : ObjectNotFound: (:) [Get-View], VimException
+ FullyQualifiedErrorId : Core_GetView_WriteNotFoundError,VMware.VimAutoma
tion.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView
PS C:\temp>
I am using vSphere PowerCLI 5.0.1.4431 and PowerShell 2.0. It's working for me on another server wher I am running the same version of PowerCLI and PowerShell... I think I am missing something; but what?
Thank you,
qwert
Are you perhaps connected to a vCenter ?
That error normally appears when connected to an ESXi server.
Btw, in the current PowerCLI version there is no need anymore to use the SDK methods to change Alarm actions.
There are now cmdlets for that, see New-AlarmAction.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Luc,
Thanks a lot!
No, I connected to VC, not to ESX host, but you were right it was connection issue.
Thanks again,
qwert