Dears
I need to create an Alarm in multiple VM. using Pcli
Many thanks
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
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
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.
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
Dear LucD
yes it will be very good, "vm total size on disk GB" Trigger
waiting you
Many thanks
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
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
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
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
You have two options:
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes Option 1 will be very good,
Could you help to add it to script
Thanks again Lucd
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