VMware Cloud Community
TCPvmadmin
Contributor
Contributor

export / store / delete / import alarms on many datacenters object

Hi there,

I read many thing and see scripts from LucD but I still have an issue about my need.

Actually I have Alarm defined on my vCenter object and I have 5 Datacenters on this vCenter.

Each Datacenter are managed by differents support team.

We need :

- Copy / export Alarms from vCenter objects and delete them

- import these alarms to each differents datacenter object

If anyone can help us , maybe based on LucD script because I'm not able to understand all LucD's alarm script.

Thanks for your help.


Jeremy

Reply
0 Kudos
16 Replies
LucD
Leadership
Leadership

Hi Jeremy, which script do you refer to ?


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

Reply
0 Kudos
TCPvmadmin
Contributor
Contributor

Hi Luc,

This one : http://www.lucd.info/2010/02/20/alarms-moving-them-around/ I need same kind of scripts but moving theses alarms from vCenter object to a Datacenter and after that re execute it to copy on other datacenter object.

Reply
0 Kudos
LucD
Leadership
Leadership

It's a matter of setting the $From and $To parameters of the function correctly.

The following is an updated version of my script that copies alarms from the vCenter root to 2 datacenters.

Set-Variable -Name alarmLength -Value 80 -Option "constant"

function
Move-Alarm{   param($Alarm, $To, [switch]$DeleteOriginal = $false)   $alarmObj = Get-View $Alarm
 
$alarmMgr = Get-View AlarmManager   if($deleteOriginal){     $alarmObj.RemoveAlarm()   }   else{     $updateAlarm = New-Object VMware.Vim.AlarmSpec
    $updateAlarm = $alarmObj.Info
    $oldName = $alarmObj.Info.Name
    $oldState = $alarmObj.Info.Enabled
    $oldDescription = $alarmObj.Info.Description
    $suffix = " (moved to " + ([string]($to | %{$_.Name + ","})).TrimEnd(",") + ")"
    if(($oldName.Length + $suffix.Length) -gt $alarmLength){       $newName = $oldName.Substring(0, $alarmLength - $suffix.Length) + $suffix
    }     else{       $newName = $oldName + $suffix
    }    
$updateAlarm.Name =  $newName
   
$updateAlarm.Enabled = $false
    $updateAlarm.Description += ("`rOriginal name: " + $oldName)     $updateAlarm.Expression.Expression | %{       if($_.GetType().Name -eq "EventAlarmExpression"){         $_.Status = $null
        $needsChange = $true
      }     }     $alarmObj.ReconfigureAlarm($updateAlarm)     $alarmObj.Info.Name = $oldName
    $alarmObj.Info.Enabled = $oldState
    $alarmObj.Info.Description = $oldDescription
  }   $newAlarm = New-Object VMware.Vim.AlarmSpec
  $newAlarm = $alarmObj.Info   $oldName = $alarmObj.Info.Name
  $oldDescription = $alarmObj.Info.Description   foreach($destination in $To){     if($To.Count -gt 1){       $suffix = " (" + $destination.Name + ")"
      if(($oldName.Length + $suffix.Length) -gt $alarmLength){         $newName = $oldName.Substring(0, $alarmLength - $suffix.Length) + $suffix
      }       else{         $newName = $oldName + $suffix
      }      
$newAlarm.Name = $newName
      $newAlarm.Description += ("`rOriginal name: " + $oldName)     }     $newAlarm.Expression.Expression | %{       if($_.GetType().Name -eq "EventAlarmExpression"){         $_.Status = $null
        $needsChange = $true
      }     }     $alarmMgr.CreateAlarm($destination.MoRef,$newAlarm)     $newAlarm.Name = $oldName
    $newAlarm.Description = $oldDescription
  } } $alarmMgr = Get-View AlarmManager
$from
= Get-Folder -Name "Datacenters"
$to1 = Get-Datacenter -Name "DC1" $to2 = Get-Datacenter -Name "DC2" $alarms = $alarmMgr.GetAlarm($from.ExtensionData.MoRef) $alarms | % {   Move-Alarm -Alarm $_ -To $to.ExtensionData -DeleteOriginal:$false
}

Let me know if this does what you are looking for ?


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

Reply
0 Kudos
TCPvmadmin
Contributor
Contributor

Luc ,

Thanks for your time and effort I will try this right now and give you a feedback !

Reply
0 Kudos
TCPvmadmin
Contributor
Contributor

It's not working at all , I have these kind of error :

Get-View : 11/1/2012 9:39:28 AM    Get-View        The request failed with HTTP status 501: Not Imp
lemented.
At C:\scripts\script.ps1:10 char:23
+   $alarmObj = Get-View <<<<  $Alarm
    + CategoryInfo          : NotSpecified: (:) [Get-View], VimException
    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Com
   mands.DotNetInterop.GetVIView
Get-View : 11/1/2012 9:39:28 AM    Get-View        View with Id  'Alarm-alarm-147' was not found on
the server(s).
At C:\scripts\script.ps1:10 char:23
+   $alarmObj = Get-View <<<<  $Alarm
    + CategoryInfo          : ObjectNotFound: (:) [Get-View], VimException
    + FullyQualifiedErrorId : Core_GetView_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.
   Commands.DotNetInterop.GetVIView
Property 'Description' cannot be found on this object; make sure it exists and is settable.
At C:\scripts\script.ps1:70 char:15
+     $newAlarm. <<<< Description = $oldDescription
    + CategoryInfo          : InvalidOperation: (Description:String) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound
Exception calling "CreateAlarm" with "2" argument(s): "
Required parameter entity is missing
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:\scripts\script.ps1:68 char:26
+     $alarmMgr.CreateAlarm <<<< ($destination.MoRef,$newAlarm)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

The script needs to store all vCenters alarm , delete them and after these operation copy them on each datacenter.

Reply
0 Kudos
LucD
Leadership
Leadership

Did the script get the alarms in $alarms ?

If there are no alarm definitions at the vCenter root, this variable will be empty.


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

Reply
0 Kudos
TCPvmadmin
Contributor
Contributor

Yes there still have alarm in vCenter object and I can't tell you if $alarm variable is not empty I have any debuging tool (like PowerGUI) on my vCenter test lab.

Could you confirm that Get-Folder -Name "Datacenters" mean my vCenter objects ?

Reply
0 Kudos
LucD
Leadership
Leadership

You could just add an extra line to the script with

$alarms | Select @{N="Name";E={$_.Info.Name}}

on it.

That way you should be able to see if some alarms are in the $alarms variable


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

Reply
0 Kudos
TCPvmadmin
Contributor
Contributor

You'r right, $alarm variable is always empty Smiley Sad

Reply
0 Kudos
LucD
Leadership
Leadership

Do you see any alarm definitions in the vCenter root (datacenters) when you look with the vSphere client ?

The $From variable can be populated with a pointer to another VI Container. The script will handle that as well


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

Reply
0 Kudos
TCPvmadmin
Contributor
Contributor

Yes I see definition Alarm on vCenter root object.

Today I will install a script editor in my lab to point what's happened exactly I will give you a feedback.

Reply
0 Kudos
TCPvmadmin
Contributor
Contributor

Luc,

I just ran the script with my script editor and now the script seem's working better but there is still error.

Actually the script stop at :

$newAlarm.Description = $odlDescription

The property Description cannot be found on this object

It's seems this error doesn't occur for every object (Alarm description) maybe due to description lenght ?

I'm currently try to "debug" but the script is too complex for me :smileyconfused:

Reply
0 Kudos
LucD
Leadership
Leadership

I suspect that could be a typo, that should be

$newAlarm.Description = $oldDescription


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

Reply
0 Kudos
TCPvmadmin
Contributor
Contributor

Yes sorry it's my typo error.

I think find the beggining of the error, I attached a debug text file and the $alarms array is not empty but there is an error here :

  $alarms | Select @{N="Name";E={$_.Info.Name}}

There is any property existing in your select if you make a $alarms | get-member are you sure about that ?

$alarms | get-member

TypeName: VMware.Vim.ManagedObjectReference

Name        MemberType Definition                   

----        ---------- ----------                   

Equals      Method     bool Equals(System.Object obj)

GetHashCode Method     int GetHashCode()            

GetType     Method     type GetType()               

ToString    Method     string ToString()            

Type        Property   System.String Type {get;set;}

Value       Property   System.String Value {get;set;}

And these error :

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

"

At C:\scripts\script.ps1:34 char:31

+     $alarmObj.ReconfigureAlarm <<<< ($updateAlarm)

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : DotNetMethodException

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

Required parameter entity is missing

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:\scripts\script.ps1:64 char:26

+     $alarmMgr.CreateAlarm <<<< ($destination.MoRef,$newAlarm)

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : DotNetMethodException

Thanks for your help

Ce message a été modifié par: TCPvmadmin

Reply
0 Kudos
LucD
Leadership
Leadership

My mistake. The $alarms variable contains a series of MoRefs (pointers) to the Alarm objects.

So that should be

Get-View $alarms | Select @{N="Name";E={$_.Info.Name}}

Sorry about that


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

Reply
0 Kudos
TCPvmadmin
Contributor
Contributor

I will try now and get you a feedback thanks !

Edit : I don't understand where I'm supposed to change this line. Could you repaste me the good script please ?

Reply
0 Kudos