VMware Cloud Community
nikpetrov95
Contributor
Contributor
Jump to solution

Create a custom CPU alarm for VM using PowerCli

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.

Tags (1)
43 Replies
mk5213
Contributor
Contributor
Jump to solution

pastedImage_0.pngFrom my understanding this means that it is impossible to use a hashtable object to splat currently. Basically you need a created splatting variable?

Reply
0 Kudos
mk5213
Contributor
Contributor
Jump to solution

Here are the important parts of the script, the commented lines work but the uncommented cmdlets next to them do not work and am trying to get them working.

#Converts json file to hashtable variable

function ConvertTo-Hashtable {

    [CmdletBinding()]

    [OutputType('hashtable')]

    param (

        [Parameter(ValueFromPipeline)]

        $InputObject

    )

    process {

        if ($null -eq $InputObject) {

            return $null

        }

        if ($InputObject -is [System.Collections.IEnumerable] -and $InputObject -isnot [string]) {

            $collection = @(

                foreach ($object in $InputObject) {

                    ConvertTo-Hashtable -InputObject $object

                }

            )

            Write-Output -NoEnumerate $collection

        } elseif ($InputObject -is [psobject]) {

            $hash = @{}

            foreach ($property in $InputObject.PSObject.Properties) {

                $hash[$property.Name] = ConvertTo-Hashtable -InputObject $property.Value

            }

            $hash

        } else {

            $InputObject

        }

    }

}

$json = Get-Content -Raw -Path $jsonFile | ConvertFrom-Json | ConvertTo-Hashtable

$mGroup,$mName,$mRollup = $json.metricName.Split('.')

$si = Get-View ServiceInstance

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

$metric = $perfMgr.PerfCounter |

    where{$_.GroupInfo.Key -eq $mGroup -and

          $_.NameInfo.Key -eq $mName -and

          $_.RollupType -eq $mRollup}

$metricParam = Get-Metric -MetricGroup $mGroup -Name $mName | where{$_.Key -eq $metric.Key}

#$trigger = New-AlarmTrigger -metric $metricParam -MetricAlarmOperator $json.sAlarmTrigger.MetricAlarmOperator -Red $json.sAlarmTrigger.Red -RedIntervalSeconds $json.sAlarmTrigger.RedIntervalSeconds -EntityType $json.sAlarmTrigger.Entitytype

$trigger = New-AlarmTrigger $json.sAlarmTrigger

#$actionTrigger = New-AlarmActionTrigger -StartStatus $json.sActionTrigger.StartStatus -EndStatus $json.sActionTrigger.EndStatus -Repeat:$json.sActionTrigger.Repeat

$actionTrigger = New-AlarmActionTrigger $json.sActionTrigger

#$action = New-AlarmAction -Email:$json.sAlarmAction.Email -Subject $json.sAlarmAction.Subject -To $json.sAlarmAction.To -AlarmActionTrigger $actionTrigger

$action = New-AlarmAction $json.sAlarmAction

New-AlarmDefinition -Name $json.Name -Description $json.Description -Entity $object -AlarmTrigger $trigger -AlarmAction $action -ActionRepeatMinutes $json.ActionRepeatMinutes

_____________________________-

Here is the JSON

{

    "Name": "JSON TEST ALARM",

    "Description": "CPU alarm created from json file",

    "ActionRepeatMinutes": 5,

    "metricName": "CPU.usage.average",

    "sAlarmTrigger": [{

        "MetricAlarmOperator": "Above",

        "Red": 7500,

        "RedIntervalSeconds": 300,

        "EntityType": "VirtualMachine"

    }],

    "sActionTrigger": [{

        "StartStatus": "yellow",

        "EndStatus": "red",

        "Repeat": true

    }],

    "sAlarmAction": [{

        "Email": true,

        "Subject": "CPU Usage high",

        "To": "x@gmail.com"

    }]

}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Your ConvertTo-HashTable function does not seem to be recursive.

Starting from your JSON file, the following does seem to work for me.

$json = Get-Content -Raw -Path $jsonFile | ConvertFrom-Json

$sTrigger = $json.sActionTrigger | ConvertTo-Hashtable

New-AlarmActionTrigger @sTrigger


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

Reply
0 Kudos
mk5213
Contributor
Contributor
Jump to solution

Thanks LucD! Things seem to be working now. Your help is greatly appreciated

Reply
0 Kudos