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
RaghaTS
Contributor
Contributor
Jump to solution

Thanks for the quick reply LucD. I  see the below error during import.

At C:\Users\AA630DADM01\Desktop\Scheduled\Alarmimport.ps1:13 char:28
+         $alarmManager.CreateAlarm <<<< ($entity.Id, $importedAlarmInfo)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

I am using vCenter 5 U1. and PowerCLI 5.0.1

I got this error only for the default vcenter alarms. Other Alarms which I have created are imported succesfully.

Reply
0 Kudos
RaghaTS
Contributor
Contributor
Jump to solution

Hi LucD, If you have any updated version of this script, please share with me. I will try that today.

Thanks.

Reply
0 Kudos
DavideDG
Contributor
Contributor
Jump to solution

Hi,

I'm getting the same error of RaghaTS:

Exception calling "CreateAlarm" with "2" argument(s): "A specified parameter was not correct.
"
At D:\1\SerializeSpec.ps1:26 char:28
+         $alarmManager.CreateAlarm <<<< ($entity.Id, $importedAlarmInfo)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException


Exception calling "CreateAlarm" with "2" argument(s): "A specified parameter was not correct.

This is just with default Alarms (which I left EMPTY, btw, I suspect it may be because of this?).

I am trying on an empty VCenter 5.0 U1b with powercli 5.0.1 (running on another machine).

Any ideas? :smileygrin:

========================================================================= Davide DG - VCP 3.5/4.0
Reply
0 Kudos
DavideDG
Contributor
Contributor
Jump to solution

Did some more research...

This seems the same problem as http://communities.vmware.com/thread/330381

I also compared an XML exported from a CLEAN System Alarm with an XML exported from an "EMPTIED" System Alarm (that is, a System Alarm which has been modified and the modified back to its original state).

The "clean" XML does not get imported, with the error specified, while the "emptied" XML is imported with no issue.

The differences are just in 2 lines:

CLEAN XMLEMPTIED XML
        <S N="Key"></S>      <S N="Key">alarm-1</S>
        <S   N="SystemName">alarm.HostConnectionStateAlarm</S>      <Nil N="SystemName" />

I tried adding back the 2 values, 1 at a time, and indeed the SystemName was the culprit.

Then I found this is new in VSphere API for 5.0:

http://pubs.vmware.com/vsphere-50/topic/com.vmware.wssdk.apiref.doc_50/vim.alarm.AlarmSpec.html

Now, if I replace the offending line, it will be imported.

For what I have to do, I can just tinker the XMLs to let them be imported.

But, if one wants to restore System Alarms, exactly as they were created by installer, I do not know how it can be done... maybe some special checks in the deserializedObject routine? I'm no good at this, though Smiley Sad

========================================================================= Davide DG - VCP 3.5/4.0
Reply
0 Kudos
DavideDG
Contributor
Contributor
Jump to solution

I can confirm that I can import ALMOST all the System Alarms (tinkering the SystemName attribute in the XML file).

I am unable to import 1 ALARM, though:

Exception calling "CreateAlarm" with "2" argument(s):

'The host license edition is not compatible with the vCenter Server license edition' is invalid or exceeds the maximum number of characters permitted.

It is indeed the alarm whose name contains the largest amount of characters in all System Alarms...

Damn crap.

========================================================================= Davide DG - VCP 3.5/4.0
Reply
0 Kudos
AlanR
Contributor
Contributor
Jump to solution

http://www.lucd.info/2010/02/20/alarms-moving-them-around/

LucD, how do you take this script and make it move ALL alarms to a new folder. The reason is we find that having all alarms at the top level stop us from defining alarms for specific needs, based on buisness requiments and have a folder for them. So:-

1) New folder all alarms are moved to, for general purpose
2) Copy all alarms to second folder so they can be deleted/disabled as needed

Reply
0 Kudos
DavideDG
Contributor
Contributor
Jump to solution

I added these lines of code to truncate alarm names to 80 chars:

if($resultProperty.CanWrite){

                         $propertyValue = ( Invoke-Expression ('$deserializedObject.' + $_.Name) | % { ConvertFromDeserialized( $_ ) } )


#RESPECT VSPHERE ALARMS MAX LENGTH: 80

if($_.Name -eq "Name") {

     if($propertyValue.Length -gt 80){

          $propertyValue = $propertyValue.Substring(0,80)

          Write-Host "### Alarm Name truncated to maximum length of 80 chars: ###"

          Write-Host "### " + $propertyValue + " ###"

     }

}

                   

                   

if($propertyValue -and $resultProperty.PropertyType.IsArray ) {

========================================================================= Davide DG - VCP 3.5/4.0
Reply
0 Kudos
svneswar
Enthusiast
Enthusiast
Jump to solution

Hi Angel,

Thanks a lot for your export/import alarms script. I am novice in PowerShell/Powercli.

In your script you are exporting an alarm based on its name. But, I would like to export all the alarms that are defined on vCenter (only). So, I tried executing the code below.

function ExportAlarm(){

Connect-VIServer $serverToExportFrom -User 'XXXXX'-Password 'XXXXX'

      

    $alarmToExport = Get-AlarmDefinition -Entity (Get-Folder -NoRecursion)

   

    foreach ($alarm in $alarmToExport)

    {

    $a = Get-View -Id $alarm.Id

    $xml += $a.Info

    }

    $xml | Export-Clixml -Path $fileName -Depth ( [System.Int32]::MaxValue )

}

But Iam getting an error like:

Capture.PNG

Could you please give your suggestion on how to collect all the alarms data in to xml file rather than just a single alarm.

One more query:

$alarmToExport = Get-AlarmDefinition -Entity (Get-Folder -NoRecursion)

By using the above command, we are collecting the alarms which are created on Vcenter object/level only, but not on any other objects like datacenter,VMs e.t.c.Please correct me if I am wrong.

Thanks in advance.

Reply
0 Kudos
svneswar
Enthusiast
Enthusiast
Jump to solution

Hi Angel,

I am able to get all the alarms info by decalaring $xml=@().

function ExportAlarm(){

Connect-VIServer $serverToExportFrom -User 'xxx'-Password 'xxx'

#$alarmToExport = Get-AlarmDefinition $alarmToExportName

    $alarmToExport = Get-AlarmDefinition -Entity (Get-Folder -NoRecursion)

    $xml= @()

    foreach ($alarm in $alarmToExport)

    {

    $a = Get-View -Id $alarm.Id

    $xml += $a.Info

    }

    $xml | Export-Clixml -Path $fileName -Depth ( [System.Int32]::MaxValue )

}

Thanks to RS_1.

Reply
0 Kudos
svneswar
Enthusiast
Enthusiast
Jump to solution


Hi Angel,

Thanks for your script to export all alarms. Actually, in your script for each alarm there will be a .xml file created in the folder. But, I tried script like
appending all the alarms info to a single xml file as below:

function ExportAlarm(){
    Connect-VIServer $serverToExportFrom -User 'xxxx'-Password 'xxxx'
    $alarmToExport = Get-AlarmDefinition -Entity (Get-Folder -NoRecursion) -Name
    $xml= @()
    foreach ($alarm in $alarmToExport)
    {
    $a = Get-View -Id $alarm.Id
    $xml += $a.Info
    }
    $xml | Export-Clixml -Path $fileName -Depth ( [System.Int32]::MaxValue )
}

I am able to collect all the alarms info in a single xml file. But while importing the xml file with import function like below:

function ImportAlarm {

Connect-VIServer $serverToImportTo -User 'xxxx'-Password 'xxxx'
$deserializedAlarmInfo = Import-Clixml -Path $fileName
$importedAlarmInfo = ConvertFromDeserialized( $deserializedAlarmInfo )
$entity = Get-Folder -NoRecursion
$alarmManager = Get-View -Id "AlarmManager"
$alarmManager.CreateAlarm($entity.Id, $importedAlarmInfo)
}

I did not edit the ConvertFromDeserialized function.While importing, I am getting an error saying

Exception calling "SetValue" with "3" argument(s): "Object of type 'System.Object[]' cannot be converted to type 'System.String'."
At C:\Nag\requests\req4add-delete-modify-Alarms\newreq\newreqimporttest.ps1:59 char:6
+                     $resultProperty.SetValue($result, $propertyValue, $null)
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

Exception calling "CreateAlarm" with "2" argument(s): "
Required property name is missing from data object of type AlarmInfo
while parsing serialized DataObject of type vim.alarm.AlarmInfo
at line 1, column 335
while parsing call information for method CreateAlarm
at line 1, column 218
while parsing SOAP body
at line 1, column 207
while parsing SOAP envelope
at line 1, column 38
while parsing HTTP request for method create
on object of type vim.alarm.AlarmManager
at line 1, column 0"
At C:\Nag\requests\req4add-delete-modify-Alarms\newreq\newreqimporttest.ps1:10 char:2
+     $alarmManager.CreateAlarm($entity.Id, $importedAlarmInfo)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : VimException

Please find the complete error in the attachment.

Could you please guide me on how to export/import all alarms to/from a single xml file rather than a folder having xml file for each alarm. If there are any limitations, please let me know. Thanks a lot for all your help.


Reply
0 Kudos