Highlighted
Contributor
Contributor

Trying to copy alarms from vCenter level to DC level

Jump to solution

I'm basing this off of some code I found written by LucD and some other sources.

It's working for the most part, but the problem I'm having is it's taking the ​original 1 rule and splitting the AND into 2 separate rules.

Can someone tell me what I'm doing wrong?

pastedImage_1.png

like so:

pastedImage_0.png

pastedImage_3.png

function Move-Alarm {

  param($Alarm, $To, [switch]$DeleteOriginal = $false)

  $alarmDef = Get-AlarmDefinition $Alarm

  $alarmTrigger = Get-AlarmTrigger $Alarm

  $alarmAction = Get-AlarmAction $Alarm

  $email = $alarmAction.To

  $newAlarmName = $alarmDef.Name + " (" + $To + ")"

  $newAlarmAction = @{

    Email = $true

    Subject = $alarmAction.Subject

    To = $email.trim(";")

  }

  #$action = New-AlarmAction @newAlarmAction

  $alarmDefInput = @{

    Name = $newAlarmName

    Description = $alarmDef.description

    AlarmTrigger = $alarmTrigger

    AlarmAction = $action

    Entity = $To

  }

  New-AlarmDefinition @alarmDefInput

}

$to = Get-DataCenter ####

$tgtAlarm = "Datastore usage on disk","Host connection and power state"

$alarms = Get-AlarmDefinition -name $tgtAlarm

$alarms | % {

  Move-Alarm -Alarm $_ -To $to -DeleteOriginal:$false

}

0 Kudos
1 Solution

Accepted Solutions
Highlighted
User Moderator
User Moderator

The following is a short example of using that method.
It copies an alarm from the root of the vCenter (folder Datacenters) to a cluster.
It adds the suffix '-Copy' to the Alarm name.

$alarmName = 'Host connection and power state'

$from = Get-Folder -Name "Datacenters"

$to = Get-Cluster -Name cluster


$alarm = Get-AlarmDefinition -Name $alarmName


$alarmMgr = Get-View AlarmManager


$spec = New-Object -TypeName VMware.Vim.AlarmSpec

$spec.Name = $alarm.Name + "-Copy"

$spec.Action = $alarm.ExtensionData.Info.Action

$spec.ActionFrequency = $alarm.ExtensionData.Info.ActionFrequency

$spec.Description = $alarm.ExtensionData.Info.Description

$spec.Enabled  = $alarm.ExtensionData.Info.Enabled

$spec.Expression = $alarm.ExtensionData.Info.Expression

$spec.Setting = $alarm.ExtensionData.Info.Setting

$alarmMgr.CreateAlarm($to.ExtensionData.MoRef,$spec)


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

View solution in original post

0 Kudos
6 Replies
Highlighted
User Moderator
User Moderator

I'm afraid you're not doing anything wrong.
That is the way those PowerCLI cmdlets were implemented.

If you want to have the original format for the rules, you will have to revert to calling the API method yourself.

Btw, this does not change anything on how the alarm works, the default with multiple rules is the AND operator.


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

0 Kudos
Highlighted
User Moderator
User Moderator

I forgot to mention, that format with the separate Rules is the new standard format.
The other one, with the AND in the Rule, is the old format.


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

0 Kudos
Highlighted
Contributor
Contributor

First off, thanks for the quick reply.

It's definitely changing how it works because the trigger is getting set on both rules so where the original alarm would only trigger the alarm action if both conditions were true, the new alarm is triggering an action if either condition is true.  It's behaving more like an OR rather than an AND in this regard.

I inadvertently triggered a flood of emails when this new alarm triggered for every host that was NOT in standby Smiley Sad

0 Kudos
Highlighted
User Moderator
User Moderator

Oops, yes, you are right.

The New-AlarmDefinition cmdlet indeed combines the Rules with an OR.

That means you would have to use the API CreateAlarm method to copy alarms.


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

0 Kudos
Highlighted
User Moderator
User Moderator

The following is a short example of using that method.
It copies an alarm from the root of the vCenter (folder Datacenters) to a cluster.
It adds the suffix '-Copy' to the Alarm name.

$alarmName = 'Host connection and power state'

$from = Get-Folder -Name "Datacenters"

$to = Get-Cluster -Name cluster


$alarm = Get-AlarmDefinition -Name $alarmName


$alarmMgr = Get-View AlarmManager


$spec = New-Object -TypeName VMware.Vim.AlarmSpec

$spec.Name = $alarm.Name + "-Copy"

$spec.Action = $alarm.ExtensionData.Info.Action

$spec.ActionFrequency = $alarm.ExtensionData.Info.ActionFrequency

$spec.Description = $alarm.ExtensionData.Info.Description

$spec.Enabled  = $alarm.ExtensionData.Info.Enabled

$spec.Expression = $alarm.ExtensionData.Info.Expression

$spec.Setting = $alarm.ExtensionData.Info.Setting

$alarmMgr.CreateAlarm($to.ExtensionData.MoRef,$spec)


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

View solution in original post

0 Kudos
Highlighted
Contributor
Contributor

Thank you so much LucD

After incorporating this method into the script it's now working as I want it.

function Move-Alarm {

  param($Alarm, $To, [switch]$DeleteOriginal = $false)

  $alarm = Get-AlarmDefinition $Alarm

  $alarmMgr = Get-View AlarmManager

  $spec = New-Object -TypeName VMware.Vim.AlarmSpec

  $spec.Name = $alarm.Name + " (" + $To + ")"

  $spec.Action = $alarm.ExtensionData.Info.Action

  $spec.ActionFrequency = $alarm.ExtensionData.Info.ActionFrequency

  $spec.Description = $alarm.ExtensionData.Info.Description

  $spec.Enabled  = $alarm.ExtensionData.Info.Enabled

  $spec.Expression = $alarm.ExtensionData.Info.Expression

  $spec.Setting = $alarm.ExtensionData.Info.Setting

  $alarmMgr.CreateAlarm($to.ExtensionData.MoRef,$spec)

}

$to = Get-DataCenter ####

$tgtAlarm = "Datastore usage on disk","Host connection and power state"

$alarms = Get-AlarmDefinition -name $tgtAlarm

$alarms | % {

  Move-Alarm -Alarm $_ -To $to -DeleteOriginal:$false

}

0 Kudos