VMware Cloud Community
ThatMattGuy
Contributor
Contributor
Jump to solution

Change Alarm Triggers in VCenter through PowerCLI

Good afternoon all, I am trying to automate the changing of an alarm trigger with VCenter 5.  Currently when users use 75% or more of the storage on the local HD I get an alarm for "Datastore Disk Usage".  I want to change the Warning from 75% to 85% and the Alert from 85% to 90%.  I have been using VSphere PowerCLI for many tasks from adding and configuring hosts and their storage, networks, dns, etc...  So I understand the basics.  I just can't seem to figure out how to do anything other then get the name of the alarm.  Any help would be appreciated.

1 Solution

Accepted Solutions
mattboren
Expert
Expert
Jump to solution

Hello, ThatMattGuy-

You can modify an alarm item using the API.  You should be able to use the following to update the Warning- and Alert trigger percentages to 85% and 90% respectively using:

## get the .NET View object for the Alarm of the given name
$viewAlarmToUpdate = Get-View -Id (Get-AlarmDefinition -Name "Datastore usage on disk").Id

## copy the existing AlarmInfo item from the current alarm definition
$specNewAlarmInfo = $viewAlarmToUpdate.Info

## update the two desired properties on the MetricAlarmExpression (http://pubs.vmware.com/vsphere-50/index.jsp?topic=/com.vmware.wssdk.apiref.doc_50/vim.alarm.MetricAl...)
$specNewAlarmInfo.Expression.Expression[0].yellow = 8500
$specNewAlarmInfo.Expression.Expression[0].red = 9000

## reconfigure the alarm with the new threshold values
$viewAlarmToUpdate.ReconfigureAlarm($specNewAlarmInfo)

This takes the AlarmInfo from the existing Alarm definition, updates the two values that  you mentioned, and the uses the ReconfigureAlarm() method of the Alarm.

And, then to display the new values, you verify on the Triggers tab of the alarm definition in vCenter, or, by updating the View data on the object and echoing it out, like:

## update the View object
$viewAlarmToUpdate.UpdateViewData("Info")
## display new values for the alarm
$viewAlarmToUpdate.Info.Expression.Expression[0]

How does that do?

View solution in original post

3 Replies
mattboren
Expert
Expert
Jump to solution

Hello, ThatMattGuy-

You can modify an alarm item using the API.  You should be able to use the following to update the Warning- and Alert trigger percentages to 85% and 90% respectively using:

## get the .NET View object for the Alarm of the given name
$viewAlarmToUpdate = Get-View -Id (Get-AlarmDefinition -Name "Datastore usage on disk").Id

## copy the existing AlarmInfo item from the current alarm definition
$specNewAlarmInfo = $viewAlarmToUpdate.Info

## update the two desired properties on the MetricAlarmExpression (http://pubs.vmware.com/vsphere-50/index.jsp?topic=/com.vmware.wssdk.apiref.doc_50/vim.alarm.MetricAl...)
$specNewAlarmInfo.Expression.Expression[0].yellow = 8500
$specNewAlarmInfo.Expression.Expression[0].red = 9000

## reconfigure the alarm with the new threshold values
$viewAlarmToUpdate.ReconfigureAlarm($specNewAlarmInfo)

This takes the AlarmInfo from the existing Alarm definition, updates the two values that  you mentioned, and the uses the ReconfigureAlarm() method of the Alarm.

And, then to display the new values, you verify on the Triggers tab of the alarm definition in vCenter, or, by updating the View data on the object and echoing it out, like:

## update the View object
$viewAlarmToUpdate.UpdateViewData("Info")
## display new values for the alarm
$viewAlarmToUpdate.Info.Expression.Expression[0]

How does that do?

ThatMattGuy
Contributor
Contributor
Jump to solution

That worked perfect.  Thanks for the help!

Reply
0 Kudos
EdwinCastellano
Contributor
Contributor
Jump to solution

mattboren​ thank you very much for your response, it helped me a bunch to retrieve the information from one vCenter and insert it into another.

$ErrorActionPreference = "SilentlyContinue"

$vCenterSource = "vCenterSource.fqdn"

$vCenterTarget = "vCenterTarget.fqdn"

Connect-viServer -server $vCenterSource, $vCenterTarget

$AllAlarmsSource = $null

$AllAlarmsSource = Get-View -Server $vCenterSource -Id (Get-AlarmDefinition -Server $vCenterSource).Id

$NotFoundAlarms = $null

$NotFoundAlarms = @()

foreach ($AlarmSource in $AllAlarmsSource) {

    $AlarmTarget = $null

    Try {

        $AlarmTarget = Get-View -Server $vCenterTarget -Id (Get-AlarmDefinition -Server $vCenterTarget -Name $AlarmSource.Info.Name).Id

        $AlarmTarget.ReconfigureAlarm($AlarmSource.Info)

        Write-Host $AlarmTarget.Info.Name.ToString() "was reconfigured on $vCenterTarget as" $AlarmSource.Info.Name.ToString() "is configured on $vCenterSource"

    }

    Catch {

        $NotFoundAlarms += $AlarmSource.Info.Name.ToString() + " was not found on vCenter $vCenterTarget"

    }

}

$NotFoundAlarms | Sort-Object

Disconnect-VIServer * -Confirm:$false