Hello:
I'm wondering how I can create alarm to monitor hardware by script?
For example, I want to write a script that will create alarm similar to default alarm “Status of other host hardware objects”.
Follow great docs from LucD website (http://www.lucd.info/2009/11/27/alarm-expressions-part-2-event-alarms/), I wrote the following code:
$alarm.Name = "Status of other host hardware objects"
$alarm.Description = "monitor other host hardware objects"
$alarm.Enabled = $TRUE
$expr1 = New-Object VMware.Vim.EventAlarmExpression
$expr1.EventType = "com.vmware.vc.cim.CIMGroupHealthStateChanged"
$expr1.ObjectType = "HostSystem"
$expr1.Status = "green"
$expr2 = New-Object VMware.Vim.EventAlarmExpression
$expr2.EventType = "EnteringMaintenanceModeEvent"
$expr2.ObjectType = "HostSystem"
$expr2.Status = "yellow"
$expr3 = New-Object VMware.Vim.EventAlarmExpression
$expr3.EventType = "EnteringMaintenanceModeEvent"
$expr3.ObjectType = "HostSystem"
$expr3.Status = "red"
$alarm.expression = New-Object VMware.Vim.OrAlarmExpression
$alarm.expression.expression += $expr1
$alarm.expression.expression += $expr2
$alarm.expression.expression += $expr3
$alarmMgr.CreateAlarm($entity.MoRef, $alarm)
But now I have problem to add Argument, Operator and Value to Condition (advanced) to all three different events?
Also, I know I have to restart VC after creating this alarm, but it’s fine with me (the same “issue” I had when added Network/Storage monitoring alarms by script).
I would really appreciate for help.
Thanks a lot!
Ok, I think I got it.
You need to use the Comparisons property to define the conditions.
Try this
$entity = (Get-VMHost <esx-hostname>).Extensiondata $alarm = New-Object VMware.Vim.AlarmSpec $alarmMgr = Get-View AlarmManager $alarm.Name = "Status of other host hardware objects" $alarm.Description = "monitor other host hardware objects" $alarm.Enabled = $TRUE $expr1 = New-Object VMware.Vim.EventAlarmExpression $expr1.EventType = "EventEx" $expr1.EventTypeId = "com.vmware.vc.cim.CIMGroupHealthStateChanged" $expr1.ObjectType = "HostSystem" $expr1.Status = "green" $condition11 = New-Object VMware.Vim.EventAlarmExpressionComparison $condition11.attributeName = "group" $condition11.operator = "equals" $condition11.value = "Other" $condition12 = New-Object VMware.Vim.EventAlarmExpressionComparison $condition12.attributeName = "newState" $condition12.operator = "equals" $condition12.value = "green" $expr1.Comparisons += $condition11 $expr1.Comparisons += $condition12 $expr2 = New-Object VMware.Vim.EventAlarmExpression $expr2.EventType = "EventEx" $expr2.EventTypeId = "com.vmware.vc.cim.CIMGroupHealthStateChanged" $expr2.ObjectType = "HostSystem" $expr2.Status = "yellow" $expr2.Comparisons += $condition11 $condition22 = New-Object VMware.Vim.EventAlarmExpressionComparison $condition22.attributeName = "newState" $condition22.operator = "equals" $condition22.value = "yellow" $expr2.Comparisons += $condition22 $expr3 = New-Object VMware.Vim.EventAlarmExpression $expr3.EventType = "EventEx" $expr3.EventTypeId = "com.vmware.vc.cim.CIMGroupHealthStateChanged" $expr3.ObjectType = "HostSystem" $expr3.Status = "red" $expr3.Comparisons += $condition11 $condition32 = New-Object VMware.Vim.EventAlarmExpressionComparison $condition32.attributeName = "newState" $condition32.operator = "equals" $condition32.value = "red" $expr3.Comparisons += $condition32 $alarm.expression = New-Object VMware.Vim.OrAlarmExpression $alarm.expression.expression += $expr1 $alarm.expression.expression += $expr2 $alarm.expression.expression += $expr3 $action = New-Object VMware.Vim.AlarmAction $action = New-Object VMware.Vim.AlarmTriggeringAction $action.action = New-Object VMware.Vim.SendEmailAction $action.action.toList = "lucd@lucd.info;" $action.action.ccList = "" $action.action.subject = "" $action.action.body = "" $action.green2yellow = $false $action.yellow2red = $false $action.red2yellow = $false $action.yellow2green = $false $spec = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec $spec.startState = "yellow" $spec.finalState = "red" $spec.repeats = $false $action.transitionSpecs = $spec $alarm.action = New-Object VMware.Vim.GroupAlarmAction $alarm.action.action += $action $setting = New-Object VMware.Vim.AlarmSetting $setting.toleranceRange = 0 $setting.reportingFrequency = 0 $alarm.setting = $setting $alarmMgr.CreateAlarm($entity.MoRef, $alarm)
Note that I reused the $condition11 in all three expressions since it doesn't change.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The following script contains an action. In this case sending an email.
Some notes on your original script.
-) the EnteringMaintenanceModeEvent event was present twice. Not sure if that was the intention, so I commented it out
-) Extended Events, like the com.vmware.vc.cim.CIMGroupHealthStateChanged, require an EventType and an EventTypeId
-) I'm not sure why you say that you have to restart the VC. Afaik that is not needed
$entity = (Get-VMHost <hostname>).Extensiondata $alarm = New-Object VMware.Vim.AlarmSpec $alarmMgr = Get-View AlarmManager $alarm.Name = "Status of other host hardware objects" $alarm.Description = "monitor other host hardware objects" $alarm.Enabled = $TRUE $expr1 = New-Object VMware.Vim.EventAlarmExpression $expr1.EventType = "EventEx" $expr1.EventTypeId = "com.vmware.vc.cim.CIMGroupHealthStateChanged" $expr1.ObjectType = "HostSystem" $expr1.Status = "green" $expr2 = New-Object VMware.Vim.EventAlarmExpression $expr2.EventType = "EnteringMaintenanceModeEvent" $expr2.ObjectType = "HostSystem" $expr2.Status = "yellow" # $expr3 = New-Object VMware.Vim.EventAlarmExpression # $expr3.EventType = "EnteringMaintenanceModeEvent" # $expr3.ObjectType = "HostSystem" # $expr3.Status = "red" $alarm.expression = New-Object VMware.Vim.OrAlarmExpression $alarm.expression.expression += $expr1 $alarm.expression.expression += $expr2 # $alarm.expression.expression += $expr3 $action = New-Object VMware.Vim.AlarmAction $action = New-Object VMware.Vim.AlarmTriggeringAction $action.action = New-Object VMware.Vim.SendEmailAction $action.action.toList = "lucd@lucd.info;" $action.action.ccList = "" $action.action.subject = "" $action.action.body = "" $action.green2yellow = $false $action.yellow2red = $false $action.red2yellow = $false $action.yellow2green = $false $spec = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec $spec.startState = "yellow" $spec.finalState = "red" $spec.repeats = $false $action.transitionSpecs = $spec $alarm.action = New-Object VMware.Vim.GroupAlarmAction $alarm.action.action += $action $setting = New-Object VMware.Vim.AlarmSetting $setting.toleranceRange = 0 $setting.reportingFrequency = 0 $alarm.setting = $setting $alarmMgr.CreateAlarm($entity.MoRef, $alarm)
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Luc,
Thanks a lot for your help!
But the main question is still unresolved…
You script will add trigger “Hardware Health Changed” (in my example I put there trigger "Entering maintenance mode" by mistake) and it’s exactly what I need, but the condition (Advanced will be empty, so it never will be triggered). Please see EmptySettings.png.
I need to be able add some advanced settings, so the end result would look like WithAdvanced.png
The question is, how can I add conditions/advanced settings for my event when creating new alarm by script?
Thank you.
Ok, I think I got it.
You need to use the Comparisons property to define the conditions.
Try this
$entity = (Get-VMHost <esx-hostname>).Extensiondata $alarm = New-Object VMware.Vim.AlarmSpec $alarmMgr = Get-View AlarmManager $alarm.Name = "Status of other host hardware objects" $alarm.Description = "monitor other host hardware objects" $alarm.Enabled = $TRUE $expr1 = New-Object VMware.Vim.EventAlarmExpression $expr1.EventType = "EventEx" $expr1.EventTypeId = "com.vmware.vc.cim.CIMGroupHealthStateChanged" $expr1.ObjectType = "HostSystem" $expr1.Status = "green" $condition11 = New-Object VMware.Vim.EventAlarmExpressionComparison $condition11.attributeName = "group" $condition11.operator = "equals" $condition11.value = "Other" $condition12 = New-Object VMware.Vim.EventAlarmExpressionComparison $condition12.attributeName = "newState" $condition12.operator = "equals" $condition12.value = "green" $expr1.Comparisons += $condition11 $expr1.Comparisons += $condition12 $expr2 = New-Object VMware.Vim.EventAlarmExpression $expr2.EventType = "EventEx" $expr2.EventTypeId = "com.vmware.vc.cim.CIMGroupHealthStateChanged" $expr2.ObjectType = "HostSystem" $expr2.Status = "yellow" $expr2.Comparisons += $condition11 $condition22 = New-Object VMware.Vim.EventAlarmExpressionComparison $condition22.attributeName = "newState" $condition22.operator = "equals" $condition22.value = "yellow" $expr2.Comparisons += $condition22 $expr3 = New-Object VMware.Vim.EventAlarmExpression $expr3.EventType = "EventEx" $expr3.EventTypeId = "com.vmware.vc.cim.CIMGroupHealthStateChanged" $expr3.ObjectType = "HostSystem" $expr3.Status = "red" $expr3.Comparisons += $condition11 $condition32 = New-Object VMware.Vim.EventAlarmExpressionComparison $condition32.attributeName = "newState" $condition32.operator = "equals" $condition32.value = "red" $expr3.Comparisons += $condition32 $alarm.expression = New-Object VMware.Vim.OrAlarmExpression $alarm.expression.expression += $expr1 $alarm.expression.expression += $expr2 $alarm.expression.expression += $expr3 $action = New-Object VMware.Vim.AlarmAction $action = New-Object VMware.Vim.AlarmTriggeringAction $action.action = New-Object VMware.Vim.SendEmailAction $action.action.toList = "lucd@lucd.info;" $action.action.ccList = "" $action.action.subject = "" $action.action.body = "" $action.green2yellow = $false $action.yellow2red = $false $action.red2yellow = $false $action.yellow2green = $false $spec = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec $spec.startState = "yellow" $spec.finalState = "red" $spec.repeats = $false $action.transitionSpecs = $spec $alarm.action = New-Object VMware.Vim.GroupAlarmAction $alarm.action.action += $action $setting = New-Object VMware.Vim.AlarmSetting $setting.toleranceRange = 0 $setting.reportingFrequency = 0 $alarm.setting = $setting $alarmMgr.CreateAlarm($entity.MoRef, $alarm)
Note that I reused the $condition11 in all three expressions since it doesn't change.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Luc,
Thank you very much for your help!
That's exactly what I need.
Thanks a lot!!!