VMware Cloud Community
skrausealixpart
Contributor
Contributor
Jump to solution

Scripting Creation of Custom CPU Alert

Specifically, I am looking for a way to get it to set the Warning and Alert values, and set the Condition Length for each.

Through other posts and thanks to LucD and this post (http://www.lucd.info/2009/11/27/alarm-expressions-part-2-event-alarms/) I am able to get the alarm to create, however, I cannot figure out how to change the Alarm Type from "Monitor for specific events occurring..." to "Monitor for specific conditions or state..." and the subsequent trigger values as necessary such as shown in the picture attached.

Is this even possible via script?

settings.jpg

Any help would be GREATLY appreciated. Thanks in advance!

Tags (3)
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

There are several types of expressions available when setting up alarms.

alarm-expressions.png

It looks as if you want to use the MetricAlarmExpression in this case.

I have another post on this type of expression, see Alarm expressions – Part 1 : Metric alarms

The trick with metric alarms, is that the counter id can be different in each vSphere environment.

The script could like this, but make sure to check the CounterID first !

Param (

  # Define vCenter you are connecting to (ie. vcentername.domain.com)

  [string]$vCenterName =$(read-host "Define vCenter instance you are connecting to (ie. vcentername.domain.com)"),

  # The DataCenter which you will be connecting to (Name of DataCenter)

  [string]$vDataCenterName =$(read-host "Define the DataCenter which you will connecting to (Name of DataCenter)")

)

$toAddr = "email@email.com"

Connect-VIServer -Server $vCenterName

$alarmMgr = Get-View AlarmManager

$rootFolder = Get-Folder -Name 'vm' -Location (Get-DataCenter -Name $vDataCenterName)

$entity = Get-Folder -Name 'Production' -Location ($rootFolder) | Get-View

# AlarmSpec

$alarm = New-Object VMware.Vim.AlarmSpec

$alarm.Name = "VM CPU Usage"

$alarm.Description = "VM CPU Usage"

$alarm.Enabled = $false

#Action

$alarm.action = New-Object VMware.Vim.GroupAlarmAction

$trigger = New-Object VMware.Vim.AlarmTriggeringAction

$trigger.action = New-Object VMware.Vim.SendEmailAction

$trigger.action.ToList = $toAddr

$trigger.action.Subject = "VM CPU Usage Alert"

$trigger.Action.CcList = ""

$trigger.Action.Body = ""

$trigger.Yellow2red = $true

# Transaction

$trans = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec

$trans.startstate = "yellow"

$trans.finalstate = "red"

$trans.repeats = $false

$trigger.transitionspecs += $trans

$alarm.action.action += $trigger

# Expression

$expression = New-Object VMware.Vim.MetricAlarmExpression

$expression.Operator = "isAbove"

$expression.type = "VirtualMachine"

$expression.metric = New-Object VMware.Vim.PerfMetricId

$expression.metric.counterId = 2

$expression.metric.instance = ""

$expression.yellow = 7500

$expression.yellowInterval = 300

$expression.red = 9000

$expression.redInterval = 300

$alarm.expression = New-Object VMware.Vim.OrAlarmExpression

$alarm.expression.expression += $expression

#Set alarm from once every 24 Hours

$alarm.setting = New-Object VMware.Vim.AlarmSetting

$alarm.setting.reportingFrequency = 86400

$alarm.setting.toleranceRange = 0

# Create alarm.

$alarmMgr.CreateAlarm($entity.MoRef,$alarm)


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

View solution in original post

0 Kudos
9 Replies
skrausealixpart
Contributor
Contributor
Jump to solution

This is Basically what I have so far - I'm pretty sure the issue is in the bold section, I just don't know where to go from here.

Param (

  # Define vCenter you are connecting to (ie. vcentername.domain.com)

  [string]$vCenterName =$(read-host "Define vCenter instance you are connecting to (ie. vcentername.domain.com)"),

  # The DataCenter which you will be connecting to (Name of DataCenter)

  [string]$vDataCenterName =$(read-host "Define the DataCenter which you will connecting to (Name of DataCenter)")

)

Connect-VIServer -Server $vCenterName

$toAddr = "email@email.com"

$alarmMgr = Get-View AlarmManager

$rootFolder = Get-Folder -Name 'vm' -Location (Get-DataCenter -Name $vDataCenterName)

$entity = Get-Folder -Name 'Production' -Location ($rootFolder) | Get-View

# AlarmSpec

$alarm = New-Object VMware.Vim.AlarmSpec

$alarm.Name = "VM CPU Usage"

$alarm.Description = "VM CPU Usage"

$alarm.Enabled = $TRUE

#Action

$alarm.action = New-Object VMware.Vim.GroupAlarmAction

$trigger = New-Object VMware.Vim.AlarmTriggeringAction

$trigger.action = New-Object VMware.Vim.SendEmailAction

$trigger.action.ToList = $toAddr

$trigger.action.Subject = "VM CPU Usage Alert"

$trigger.Action.CcList = ""

$trigger.Action.Body = ""

# Transaction

$trans = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec

$trans.startstate = "yellow"

$trans.finalstate = "red"

$trans.repeats = $false

$trigger.transitionspecs += $trans

$alarm.action.action += $trigger

# Expression

$expression = New-Object VMware.Vim.EventAlarmExpression

$expression.EventType = "EventEx"

$expression.EventTypeId = "VM CPU Usage (%)"

$expression.ObjectType = "VirtualMachine"

$expression.Status = "red"

$alarm.expression = New-Object VMware.Vim.OrAlarmExpression

$alarm.expression.expression += $expression

#Set alarm from once every 24 Hours

$alarm.setting = New-Object VMware.Vim.AlarmSetting

$alarm.setting.reportingFrequency = 86400

$alarm.setting.toleranceRange = 0

# Create alarm.

$alarmMgr.CreateAlarm($entity.MoRef,$alarm)

0 Kudos
LucD
Leadership
Leadership
Jump to solution

There are several types of expressions available when setting up alarms.

alarm-expressions.png

It looks as if you want to use the MetricAlarmExpression in this case.

I have another post on this type of expression, see Alarm expressions – Part 1 : Metric alarms

The trick with metric alarms, is that the counter id can be different in each vSphere environment.

The script could like this, but make sure to check the CounterID first !

Param (

  # Define vCenter you are connecting to (ie. vcentername.domain.com)

  [string]$vCenterName =$(read-host "Define vCenter instance you are connecting to (ie. vcentername.domain.com)"),

  # The DataCenter which you will be connecting to (Name of DataCenter)

  [string]$vDataCenterName =$(read-host "Define the DataCenter which you will connecting to (Name of DataCenter)")

)

$toAddr = "email@email.com"

Connect-VIServer -Server $vCenterName

$alarmMgr = Get-View AlarmManager

$rootFolder = Get-Folder -Name 'vm' -Location (Get-DataCenter -Name $vDataCenterName)

$entity = Get-Folder -Name 'Production' -Location ($rootFolder) | Get-View

# AlarmSpec

$alarm = New-Object VMware.Vim.AlarmSpec

$alarm.Name = "VM CPU Usage"

$alarm.Description = "VM CPU Usage"

$alarm.Enabled = $false

#Action

$alarm.action = New-Object VMware.Vim.GroupAlarmAction

$trigger = New-Object VMware.Vim.AlarmTriggeringAction

$trigger.action = New-Object VMware.Vim.SendEmailAction

$trigger.action.ToList = $toAddr

$trigger.action.Subject = "VM CPU Usage Alert"

$trigger.Action.CcList = ""

$trigger.Action.Body = ""

$trigger.Yellow2red = $true

# Transaction

$trans = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec

$trans.startstate = "yellow"

$trans.finalstate = "red"

$trans.repeats = $false

$trigger.transitionspecs += $trans

$alarm.action.action += $trigger

# Expression

$expression = New-Object VMware.Vim.MetricAlarmExpression

$expression.Operator = "isAbove"

$expression.type = "VirtualMachine"

$expression.metric = New-Object VMware.Vim.PerfMetricId

$expression.metric.counterId = 2

$expression.metric.instance = ""

$expression.yellow = 7500

$expression.yellowInterval = 300

$expression.red = 9000

$expression.redInterval = 300

$alarm.expression = New-Object VMware.Vim.OrAlarmExpression

$alarm.expression.expression += $expression

#Set alarm from once every 24 Hours

$alarm.setting = New-Object VMware.Vim.AlarmSetting

$alarm.setting.reportingFrequency = 86400

$alarm.setting.toleranceRange = 0

# Create alarm.

$alarmMgr.CreateAlarm($entity.MoRef,$alarm)


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

0 Kudos
skrausealixpart
Contributor
Contributor
Jump to solution

I'm currently getting the error below. Please note, I ran your Get-PerfCounterList function first to verify the CounterID. The only change I made, was I adjusted these values, to meet our desired intervals and values:

$expression.yellow = 8500

$expression.yellowInterval = 900

$expression.red = 9500

$expression.redInterval = 900

Error:

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

Required property expression is missing from data object of type OrAlarmExpression

while parsing serialized DataObject of type vim.alarm.OrAlarmExpression

at line 1, column 471

while parsing property "expression" of static type AlarmExpression

while parsing serialized DataObject of type vim.alarm.AlarmSpec

at line 1, column 336

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:\Test\Create-Alarm.ps1:102 char:1

+ $alarmMrg.CreateAlarm($entity.MoRef,$alarm)

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

    + FullyQualifiedErrorId : VimException

0 Kudos
LucD
Leadership
Leadership
Jump to solution

According to the error message, the expression property is not there.

Can you put a breakpoint on the CreateAlarm line, and check the content of the $alarm object, more specifically check if the expression property is there.


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

0 Kudos
skrausealixpart
Contributor
Contributor
Jump to solution

ugh... it would probably help if I hadn't accidentally done a typo, huh? Smiley Happy

This is working now, thanks again for all the help!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

No problem, glad it is working.


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

0 Kudos
ustcsh
Contributor
Contributor
Jump to solution

Hi Luc,

How can we make the email subject with specific alarmName and body with specific eventDescription (there are 3 different events for the alarm).

$myTrigger.action.Subject = Alarm - {alarmName}

$myTrigger.action.Body = Description:\n{eventDescription}

Thanks

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm not sure if you have access to all the environment variables for a Mail action as you have for a script.

Worst case, use a script action. And in the script send the email.

Only problem could be with a VCSA, then you will have to run the script on another machine.

William did a post on that, see How to run a script from a vCenter Alarm action in the VCSA?


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

ustcsh
Contributor
Contributor
Jump to solution

The environment variables is good enough Smiley Happy

0 Kudos