PowerCLI

 View Only
  • 1.  Scripting Creation of Custom CPU Alert

    Posted Oct 14, 2014 06:05 PM

    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?

    Any help would be GREATLY appreciated. Thanks in advance!



  • 2.  RE: Scripting Creation of Custom CPU Alert

    Posted Oct 14, 2014 06:28 PM

    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)



  • 3.  RE: Scripting Creation of Custom CPU Alert
    Best Answer

    Posted Oct 19, 2014 10:00 AM

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

    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)



  • 4.  RE: Scripting Creation of Custom CPU Alert

    Posted Oct 20, 2014 01:01 PM

    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



  • 5.  RE: Scripting Creation of Custom CPU Alert

    Posted Oct 20, 2014 01:26 PM

    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.



  • 6.  RE: Scripting Creation of Custom CPU Alert

    Posted Oct 20, 2014 01:38 PM

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

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



  • 7.  RE: Scripting Creation of Custom CPU Alert

    Posted Oct 20, 2014 01:43 PM

    No problem, glad it is working.



  • 8.  RE: Scripting Creation of Custom CPU Alert

    Posted Aug 16, 2018 02:51 PM

    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



  • 9.  RE: Scripting Creation of Custom CPU Alert

    Posted Aug 16, 2018 03:40 PM

    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?



  • 10.  RE: Scripting Creation of Custom CPU Alert

    Posted Aug 20, 2018 02:46 AM

    The environment variables is good enough :smileyhappy: