VMware Cloud Community
qwert1235
Enthusiast
Enthusiast
Jump to solution

Script to export/import Alarms between VC or synchronize alarms between 2 Virtual Centers

Hello:

I am wonder if there is a way to synchronize alarms between 2 Virtual Centers?

Or for example, how to export all alarms from one VC and import them to another? Is it possible?

Any help would be really appreciated.

Thanks a lot!

49 Replies
RS_1
Enthusiast
Enthusiast
Jump to solution

Thanks Luc, the MetricAlarmExpression part seems really more difficult than it looks like Smiley Happy

EDIT : maybe an id translation table ?

Reply
0 Kudos
jer0nim0
Contributor
Contributor
Jump to solution

Hi there,

is there a solution that "just works as one would expect", somewhere?

Alarms defined in vcenter root only would be fine in my case.

@RS_1:

1) The script that you posted bails out with

Exception calling "CreateAlarm" with "2" argument(s): "spec"
At C:\Users\user\Desktop\copy-alarms.ps1:55 char:27
+         $alarmMgrDst.CreateAlarm <<<< ($EntityDst.moref,$AlarmDstInfo)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

on import. (It does a "foreach ($AlarmDstInfo in $AlarmListSrcInfo)", however there is no AlarmListSrcInfo...)

2) Does it actually restore *actions* etc.?

I'm asking because I was searching for an email address of an email notification that I have set up, yet I could not find the email address in the XML file, only abstract stuff like <T>VMware.Vim.GroupAlarmAction</T>.

Meaning: is there any added value to Get-AlarmDefinition and Set-AlarmDefintion? (http://blogs.vmware.com/vipowershell/2010/12/managing-vsphere-alarms-with-powercli.html)

Greetings

JM

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

2) You do not see the email address because the Xport-Alarm script doesn't explictely use the -Depth parameter on the Export-CliXml cmdlet.

The default is 2, and the email address is located deeper in the Alarm object.

Replace that line for exampe with

$AlarmListSrcInfo| Export-CliXML $xmlpath -Depth 5

and the email address will be in there. And the export/import will work.


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

Reply
0 Kudos
jer0nim0
Contributor
Contributor
Jump to solution

Yeah I figured out the -Depth parameter already, but no, Xport-alarms.ps1 from http://communities.vmware.com/message/1654109#1654109 is not working. At least not the import part.

AlarmManager.CreateAlarm seems to need an AlarmSpec and I wouldn't know exactly where to get it from (the import-clixml'ed data).

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The Export-CliXml/Import-CliXml cmdlets serialise and deserialise your object (an Alarm in this case).

After the Import-CliXml you will notice that the typename of the object is now 'Deserialized.VMware.Vim.AlarmInfo'.

That's where the script from Angel, earlier in this thread comes into play.

Angel's script will reconstruct a VMware.Vim.AlarmInfo object from the imported Deserialized.VMware.Vim.AlarmInfo object.

And when you pass that to the CreateAlarm method, the alarm will be created.

There are some pitfalls with this method:

  • MoRef that are valid on one vCenter will not necessarily be valid on another vCenter or will point to another object
  • Metric alarms use metricIds. These are not the same on every vCenter
  • Event alarms can use events generated by Extensions, these Extensions are not necessarily present on the new vCenter

So there is a bit more to it then what you can find in this thread.


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

Reply
0 Kudos
jer0nim0
Contributor
Contributor
Jump to solution

I am now copy/pasting alarms this way:

Note that some alarms are imported correctly while others trigger without reason and the "Triggers" tab says "triggers for this alarm cannot be displayed or changed through the vSphere Client. Use the vSphere API to modify these alarm triggers".

Even though both vcenter servers are the same build.

### this script is only tested with alarms defined on root vcenter item

# load Vmware Module
$xPsCheck = Get-PSSnapin | Select Name | Where {$_.Name -Like "*VMware*"}
If ($xPsCheck -eq $Null) {Add-PsSnapin *VMware*}

# change to multi-mode vcenter management
Set-PowerCLIConfiguration -ProxyPolicy NoProxy -DefaultVIServerMode Multiple -Confirm:$false

# connect to vcenters
$srv1 = Read-Host "source vcenter"
$creds1 = get-credential
connect-viserver -server $srv1 -credential $creds1 -NotDefault:$false
$srv2 = Read-Host "destination vcenter"
$creds2 = get-credential
connect-viserver -server $srv2 -credential $creds2 -NotDefault:$false

$alarmMgr2 = Get-View AlarmManager -server $srv2

# prepopulated $alarm item needed below
# there is no New-AlarmDefinition cmdlet, so we do this manually
$alarm = New-Object VMware.Vim.AlarmSpec
$alarm.expression = New-Object VMware.Vim.OrAlarmExpression
$expression1 = New-Object VMware.Vim.EventAlarmExpression
$expression1.EventType = $null
$expression1.eventTypeId = $null
$expression1.objectType = $null
$expression1.status = $null
$alarm.expression.expression += $expression1

$matches = @("YD-*", "JM-*")
foreach ($m in $matches) {
    Get-AlarmDefinition -Server $srv1 -Name $m | foreach {
        $alarm.Name = $_.Name
        $alarm.Description = $_.Description

        $root = Get-Folder -NoRecursion -Server $srv2 | Get-View
        $DEVNULL = $alarmMgr2.CreateAlarm($root.MoRef, $alarm)

        # populate the newly created alarm
        $adst_spec = (Get-AlarmDefinition -Server $srv2 -Name $_.Name).ExtensionData
        $alarmView = Get-View $adst_spec.Alarm
        $alarmView.ReconfigureAlarm( $_.ExtensionData )
   
        Write-Host -Fore Green Done: $_
    }
}

Reply
0 Kudos
rhor
Enthusiast
Enthusiast
Jump to solution

Hi Aevrov,

Firstable I wanted to thank you for sharing your script, which I'm sure took a lot of efforts to make (as anyone elses scripts to).

I tried to use your script, as it perfectly met my requirements for moving alarms definition from one VC to another - unfortunatelly I cannot be connected with both at the same time.

While trying to export alarms definition from source VC I got:

Get-View : Cannot validate argument on parameter 'Id'. The argument is null or empty. Supply an argument that is not nu
ll or empty and then try the command again.
At Z:\SerializeSpec.ps1:4 char:19
+     $a = Get-View -Id <<<<  $alarmToExport.Id
+ CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInte
rop.GetVIView

The problem is that I cannot see any (Get-AlarmDefinition).Id value, Get-AlarmDefinition only brings: 'Name', 'Description' and 'Enabled' parameters.

I'm new with PowerCLI same as with PowerShell, so maybe there is something I did wrong. Despite of if i made some silly mistake or if there is an issue in that script I would kindly ask for some help.

Both VCs are 4.1.0, 258902

PowerCLI version is 4.1 U1 build 332441 (snapin is the same)

Thanks in advance.

P.S. I know this is an old thread but still hope somebody will notice.

Reply
0 Kudos
aevrov
VMware Employee
VMware Employee
Jump to solution

Hi Rhor,

I'm really sorry for the late reply. I've been quite busy with the the development of the new PowerCLI release and did't have any time for the community lately.

In case you haven't figured this out already: I think the problem is that the alarm you're trying to export is not found.

On line 3 of the script:

> $alarmToExport = Get-AlarmDefinition $alarmToExportName

Please check if alarm with this name ($alarmToExportName) exists on your source VC and $alarmToExport has some value after the above line is executed.

Also - the way the script is written now - it expects a single alarm to be selected. If you set $alarmToExportName to a star (*) - all alarms will be selected and the script will not work as expected. You should tweak it a little bit if you want to export all the alarms. Let me know if you need help with this.

I hope this helps.

Regards,

- Angel

Reply
0 Kudos
malabelle
Enthusiast
Enthusiast
Jump to solution

Hi Angel,

is there a way to export ALL the alarms?

I tried * but it won't work...

vExpert '16, VCAP-DCA, VCAP-DCD
Reply
0 Kudos
rhor
Enthusiast
Enthusiast
Jump to solution

Hi Aevrov,

Let me also apollogy for the late reply, I to had many things on my head (and still have) and didn't have a time to look here.

I wanted to thank you for you reply as it correctly point to what was wrong with what I was trying to do. I tried do import all the alarms with $alarmToExportName set as "" (blank) or "*" (star) so it didn't work, but it works fine for a single alarm. Later I will try to tweak your script to be able to export all the alarms (or even all the alarms where description is starting with "customized" as it is my main interest). Any help would be appreciated.

Many thanks!

Regards,

- Rhor

Reply
0 Kudos
aevrov
VMware Employee
VMware Employee
Jump to solution

Hi guys,

I've improved the script (attached) a little bit, so it can export/import multiple alarms now.

Rhor, you can use the script and just modify line number 7 in order to solve your case, like this:

$alarmToExport = Get-AlarmDefinition | where { $_.Description -like "customized*" }

Hope this helps,

-Angel Evrov

Reply
0 Kudos
rhor
Enthusiast
Enthusiast
Jump to solution

Hi Angel,

Thank you for your script update!

I find it very useful and would definitely use it in my future projects.

Best regards,

- Rhor

Reply
0 Kudos
mossko
Contributor
Contributor
Jump to solution

Hey Angel and Luc, thanks so much for your posts with the code examples and full scripts! really helpful

I'm trying to copy over a whole swag of alarms from one Prod VC server to our off-site DR VC server. Exporting works a treat, but Importing fails

I've been using with the SerialzeSpec.ps1 script and have been running in to trouble with the ImportAlarm function, the CreateAlarm method does not exist, according to PowerCLI.

function ImportAlarm {
   ...
   $alarmManager = Get-View -Id 'AlarmManager-AlarmManager'
   $alarmManager.CreateAlarm($entity.Id, $importedAlarmInfo)
   ...
}

The output of this is as follows

Method invocation failed because [System.Object[]] doesn't contain a method named 'CreateAlarm'.
At C:\Documents and Settings\rmoss\My Documents\SerializeSpec.ps1:28 char:28
+         $alarmManager.CreateAlarm <<<< ($entity.Id, $importedAlarmInfo)
    + CategoryInfo          : InvalidOperation: (CreateAlarm:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Running this manually in PowerCLI looks like this

[vSphere PowerCLI] C:\> $alarmManager = Get-View -Id 'AlarmManager-AlarmManager'

[vSphere PowerCLI] C:\> $alarmManager.CreateAlarm($entity.Id, $importedAlarmInfo)
Method invocation failed because [System.Object[]] doesn't contain a method named 'CreateAlarm'.
At line:1 char:26
+ $alarmManager.CreateAlarm <<<< ($entity.Id, $importedAlarmInfo)
    + CategoryInfo          : InvalidOperation: (CreateAlarm:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

I'm running PowerCLI 4.1u1 and can see this method is there when I do Get-Member

[vSphere PowerCLI] C:\> $a =  Get-View  AlarmManager
[vSphere PowerCLI] C:\> $a.CreateAlarm('foo', 'test')
Method invocation failed because [System.Object[]] doesn't contain a method named 'CreateAlarm'.
At line:1 char:15
+ $a.CreateAlarm <<<< ('foo', 'test')
    + CategoryInfo          : InvalidOperation: (CreateAlarm:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

[vSphere PowerCLI] C:\> $a

DefaultExpression   Description         MoRef               Client
-----------------   -----------         -----               ------
{VMware.Vim.Stat... VMware.Vim.Alarm... AlarmManager-Ala... VMware.Vim.VimCl...
{VMware.Vim.Stat... VMware.Vim.Alarm... AlarmManager-Ala... VMware.Vim.VimCl...

[vSphere PowerCLI] C:\> $a | get-member


   TypeName: VMware.Vim.AlarmManager

Name                   MemberType Definition
----                   ---------- ----------
AcknowledgeAlarm       Method     System.Void AcknowledgeAlarm(VMware.Vim.Ma...
AreAlarmActionsEnabled Method     bool AreAlarmActionsEnabled(VMware.Vim.Man...
CreateAlarm            Method     VMware.Vim.ManagedObjectReference CreateAl...
EnableAlarmActions     Method     System.Void EnableAlarmActions(VMware.Vim....
Equals                 Method     bool Equals(System.Object obj)
GetAlarm               Method     VMware.Vim.ManagedObjectReference[] GetAla...
GetAlarmState          Method     VMware.Vim.AlarmState[] GetAlarmState(VMwa...
GetHashCode            Method     int GetHashCode()
GetType                Method     type GetType()
SetViewData            Method     System.Void SetViewData(VMware.Vim.ObjectC...
ToString               Method     string ToString()
UpdateViewData         Method     System.Void UpdateViewData(Params string[]...
WaitForTask            Method     System.Object WaitForTask(VMware.Vim.Manag...
Client                 Property   VMware.Vim.VimClient Client {get;}
DefaultExpression      Property   VMware.Vim.AlarmExpression[] DefaultExpres...
Description            Property   VMware.Vim.AlarmDescription Description {g...
MoRef                  Property   VMware.Vim.ManagedObjectReference MoRef {g...

Is this a bug or am I doing something wrong?

Reply
0 Kudos
Sharantyr3
Enthusiast
Enthusiast
Jump to solution

Hello old topic but I found the solution for your problem mossko : you can't import AND export at the same time with aevrov's scrit.

Anyway, using export, then using import, works flawlessly for me. Thanks a lot aevrov Smiley Wink

Reply
0 Kudos
jkb5054
Contributor
Contributor
Jump to solution

I am recieving the same error as mossko.

The export works, but importing the files seems to fail. See his error message.

such an old thread, i hope someone will find this!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The error message indicates that you are connected to 2 or more vCenters (the 'System.Object[]' part).

That means that when you do

Get-View AlarmManager

you will get an array.

And the CreateAlarm method does, of course, not exist on an array.

Make sure to disconnect from the source vCenter, after the export, before starting the import on the destination vCenter.


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

Reply
0 Kudos
jkb5054
Contributor
Contributor
Jump to solution

Thanks LucD.

It works now when I export and Import in two different scripts, disconnecting the session between them.

However I am getting a couple of errors stating that:

Exception calling "CreateAlarm" with "2" argument(s): "A specified parameter wa
s not correct.
"
At C:\Users\AA630DADM01\Desktop\Scheduled\Alarmimport.ps1:13 char:28
+         $alarmManager.CreateAlarm <<<< ($entity.Id, $importedAlarmInfo)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

I am assuming it has something to do with the actual alarm settings in the xml file.

Also, some of the alarms that got imported, will not let me change the triggers now. Stating:

The triggers for this alarm cannot be displayed or changed through the vsphere client. Use the vSphere API to modify these alarm triggers.

Any ideas?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is definitely an error. I would have to see the XML lines for the alarm that causes the error to determine what goes wrong.

There are indeed alarms that can not be edited in the vSphere client.

See my Alarm expressions – Part 1 : Metric alarms post.

It's mostly a matter of adding an OrAlarmExpression or AndAlarmExpression at the top of the trigger construct.


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

Reply
0 Kudos
RaghaTS
Contributor
Contributor
Jump to solution

I tried this script today to export and import alarms from existing vcenter to the new one. Export worked for me but I got the same error which is mentioned in athe earlier post here while importing the alarms on the new vcenter Server. All the Alarms are default vcenter alarms.

Please share the latest script if this is fixed. This is really a greate stuff saves our time in setting up alarms.

Thanks.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Is that the CreateAlarm error you see ?

Which vSphere version are you using ?


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

Reply
0 Kudos