VMware Cloud Community
amr_almoshneb
Contributor
Contributor
Jump to solution

need to create an Alarm in multiple VM using pcli

Dears

I need to create an Alarm in multiple VM. using Pcli

Many thanks

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this.
Update the VM's name, and the yellow and red thresholds on which you want the alarm to fire.

$vmName = 'MyVM'

$alarmName = 'Check disk size'

$alarmDescription = 'Check total storage size the VM is using.'

$yellowGB = 50

$redGB = 75

$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

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


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

View solution in original post

Reply
0 Kudos
11 Replies
LucD
Leadership
Leadership
Jump to solution

Can you be a bit more specific?

What kind of Alarm? Event, performance metric...?
On all VMs on a specific cluster, in a specific folder...?


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

Reply
0 Kudos
amr_almoshneb
Contributor
Contributor
Jump to solution

Many Thanks for your Reply,

i have multiple VM in cluster and need to set Alarm for each VM Disk storage space.

for example:

-vm named "server-1" need to send Alarm and notification mail if the attached Disk exceed 100GB.

- another VM "server-2" need to send Alarm and notification mail if the attached Disk exceed 50GB.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I don't think you can have a trigger that fires when a specific vDisk goes over a threshold.

But you can set a trigger to fire when the VM takes up more than a specific number of GB.

Would that work for your setup?


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

Reply
0 Kudos
amr_almoshneb
Contributor
Contributor
Jump to solution

Dear LucD

yes it will be very good, "vm total size on disk GB" Trigger

waiting you

Many thanks

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try something like this.
Update the VM's name, and the yellow and red thresholds on which you want the alarm to fire.

$vmName = 'MyVM'

$alarmName = 'Check disk size'

$alarmDescription = 'Check total storage size the VM is using.'

$yellowGB = 50

$redGB = 75

$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

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


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

Reply
0 Kudos
amr_almoshneb
Contributor
Contributor
Jump to solution

Dear Lucd

Great, it is created successfully

but need to add "send notification mail in Actions"  from normal to warning, to Allow send notification mail

Many thanks again

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Run the following lines after the alarm is created

$def = Get-AlarmDefinition -Name $alarmName

$action = New-AlarmAction -Email -Subject 'Disk size exceeded' -To 'me@domain.com' -AlarmDefinition $def

$trigger = New-AlarmActionTrigger -StartStatus Green -EndStatus Yellow -AlarmAction $action

Get-AlarmActionTrigger -AlarmAction $action | where{$_.StartStatus -eq 'Yellow'} | Remove-AlarmActionTrigger -Confirm:$false


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

amr_almoshneb
Contributor
Contributor
Jump to solution

Many thanks for your support

last question

I can't add Multiple VM name at first line " , the error appeared

for example:-

$vmName = 'server-1','server-2','server-3'

and trying to use Cat command, it didn't work

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You have two options:

  1. Create an alarm for each individual VM. That can be done by running through the list of names in a ForEach loop, and create the alarm for name
  2. Create the alarm on a higher level in the vSphere inventory (for example a Folder or the Cluster), then all the VirtualMachines under that higher level will get the Alarm defined


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

Reply
0 Kudos
amr_almoshneb
Contributor
Contributor
Jump to solution

Yes Option 1 will be very good,

Could you help to add it to script  

Thanks again Lucd

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

$vmNames = 'VM1','VM2','VM3'

$alarmName = 'Check disk size'

$alarmDescription = 'Check total storage size the VM is using.'

$yellowGB = 50

$redGB = 75

$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

foreach($vmName in $vmNames){

    $entity = Get-VM -Name $vmName

    $spec.Name = "$alarmName for $vmName"

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

   

    $def = Get-AlarmDefinition -Name $spec.Name

    $action = New-AlarmAction -Email -Subject 'Disk size exceeded' -To 'me@domain.com' -AlarmDefinition $def

    $trigger = New-AlarmActionTrigger -StartStatus Green -EndStatus Yellow -AlarmAction $action

    Get-AlarmActionTrigger -AlarmAction $action | where{$_.StartStatus -eq 'Yellow'} | Remove-AlarmActionTrigger -Confirm:$false

}


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

Reply
0 Kudos