Automation

 View Only
Expand all | Collapse all

Create a custom CPU alarm for VM using PowerCli

mk5213

mk5213Sep 09, 2020 08:10 PM

LucD

LucDSep 09, 2020 08:15 PM

  • 1.  Create a custom CPU alarm for VM using PowerCli

    Posted Feb 05, 2020 06:57 AM

    Hello! I need to create an alarm on the CPU for each virtual machine, after the alarm has worked, I need to send a message to the user's mail, mail information should be taken from the user attribute of VM (I did it myself). Please help me with this matter.



  • 2.  RE: Create a custom CPU alarm for VM using PowerCli

    Posted Feb 06, 2020 09:16 AM

    Can you provide some more details about the requirement?

    What do you mean by "... alarm on the CPU"?
    Is that when CPU utilisation goes over a specific watermark?

    Also, what do you mean by "... mail information should be taken from the user attribute"?

    Is that the email To address?



  • 3.  RE: Create a custom CPU alarm for VM using PowerCli

    Posted Feb 06, 2020 09:30 AM

    What do you mean by "... alarm on the CPU"?-  for example standart vcenter alarm "Virtual machine CPU usage"

    Is that when CPU utilisation goes over a specific watermark? - Yes, it is

    I need to create an alarm action on each virtual machine (by the name of the machine) this is the main requirement

    Also, what do you mean by "... mail information should be taken from the user attribute"?

    Is that the email To address?

    foreach($vmName in $vmNames){

    $entity = Get-VM -Name $vmName

    $mail=(Get-Annotation -Entity (get-vm $vmName) -CustomAttribute Contact).value

    $action = New-AlarmAction -Email -Subject 'Test' -To "$mail" -AlarmDefinition $def

    I need something like this:



  • 4.  RE: Create a custom CPU alarm for VM using PowerCli

    Posted Feb 06, 2020 09:44 AM

    That is in fact the solution.
    What is missing from that code?



  • 5.  RE: Create a custom CPU alarm for VM using PowerCli

    Posted Feb 06, 2020 09:49 AM

    I can't found this part for cpu

    $stat = 'disk.used.latest'

    $statSplit = $stat.Split('.')

    $si = Get-View ServiceInstance

    $perfMgr = Get-View -Id $si.Content.PerfManager

    $metric = $perfMgr.PerfCounter | where{$_.GroupInfo.Key -eq $statSplit[0] -and $_.NameInfo.Key -eq $statSplit[1] -and $_.RollupType -eq $statSplit[2]}

    $alarmMgr = Get-View AlarmManager

    $entity = Get-View -ViewType VirtualMachine -Filter @{'Name'=$vmName}

    $spec = New-Object VMware.Vim.AlarmSpec

    $spec.Name = $alarmName

    $spec.Enabled = $true

    $spec.Description = $alarmDescription

    $metricId = New-Object VMware.Vim.PerfMetricId

    $metricId.CounterId = $metric.Key

    $metricId.Instance = ''

    $expression = New-Object VMware.Vim.MetricAlarmExpression

    $expression.Metric += $metricId

    $expression.Type = 'VirtualMachine'

    $expression.Operator = [VMware.Vim.MetricAlarmOperator]::isAbove

    $expression.Yellow = $yellowGB * 1MB

    $expression.Red = $redGB * 1MB

    $spec.Expression = $expression



  • 6.  RE: Create a custom CPU alarm for VM using PowerCli

    Posted Feb 06, 2020 10:14 AM

    In the recent PowerCLI release there are new cmdlets to create metric based alarms.

    Something like this

    $vm = Get-VM -Name MyVM

    $userEmail = (Get-Annotation -Entity $vm -CustomAttribute 'Contact').value

    $sAlarmTrigger = @{

        Metric = Get-Metric -MetricGroup 'CPU' -Name 'usage' | select -First 1

        MetricAlarmOperator = 'Above'

        Yellow = 50

        Red = 75

        EntityType = 'VirtualMachine'

    }

    $trigger = New-AlarmTrigger @sAlarmTrigger


    $sAlarmAction = @{

        Email = $true

        Subject = 'CPU Usage high'

        To = $userEmail

    }

    $action = New-AlarmAction @sAlarmAction


    $sAlarmDef = @{

        Name = 'CPU High'

        Description = 'Trigger alarm when CPU utilisation is too high'

        Entity = $vm

        AlarmTrigger = $trigger

        AlarmAction = $action

    }

    New-AlarmDefinition @sAlarmDef



  • 7.  RE: Create a custom CPU alarm for VM using PowerCli

    Posted Feb 06, 2020 10:41 AM