VMware Cloud Community
qwert1235
Enthusiast
Enthusiast
Jump to solution

Create alarms by script?

Hello:

I am trying to create alarms by script and really would welcome any help.

Right now I am trying to write script that will create basic alarms (for example, Host memory status) that will send SNMP trap if memory reach specific parameters...

Is it possible?

If you have scripts that will create any other alarms and could share them I would be really appreciate it... (I found just a few and cannot get too much out of them).

Thanks,

qwert

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Sure, the following script will send an SNMP trap when the ESX host goes into maintenance mode.

$esxName = <ESX-hostname>

$alarmMgr = Get-View AlarmManager
$entity = Get-VMHost $esxName | Get-View

# AlarmSpec
$alarm = New-Object VMware.Vim.AlarmSpec
$alarm.Name = "My new alarm"
$alarm.Description = "My alarm description"
$alarm.Enabled = $TRUE

#Action
$alarm.action = New-Object VMware.Vim.GroupAlarmAction

$trigger = New-Object VMware.Vim.AlarmTriggeringAction
$trigger.action = New-Object VMware.Vim.SendSNMPAction

# Transaction
$trans = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec
$trans.startstate = "yellow"
$trans.finalstate = "red"
$trans.repeats = $false

$trigger.transitionspecs += $trans

$alarm.action.action += $trigger

# Expression
$expression = New-Object VMware.Vim.EventAlarmExpression
$expression.EventType = "EnteringMaintenanceModeEvent"
$expression.ObjectType = "HostSystem"
$expression.Status = "red"

$alarm.expression = New-Object VMware.Vim.OrAlarmExpression
$alarm.expression.expression += $expression

$alarm.setting = New-Object VMware.Vim.AlarmSetting
$alarm.setting.reportingFrequency = 0
$alarm.setting.toleranceRange = 0

# Create alarm.
$alarmMgr.CreateAlarm($entity.MoRef, $alarm)


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

View solution in original post

Reply
0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

Did you already have a look at this thread ( ) ?

The following script should create an alarm as you specified.

$esxName =<ESX-hostname>

$alarmMgr = Get-View AlarmManager
$entity = Get-VMHost $esxName | Get-View

$alarm = New-Object VMware.Vim.AlarmSpec

$alarm.Name = "My new alarm"
$alarm.Description = "My alarm description"
$alarm.Enabled = $TRUE

$expression = New-Object VMware.Vim.MetricAlarmExpression
$Expression.Operator = "isAbove"
$expression.red = 9000
$expression.RedInterval = 300
$expression.type = "HostSystem"
$expression.yellow = 7500
$expression.yellowinterval = 300
$expression.metric = New-Object VMware.Vim.PerfMetricId
$expression.metric.counterid = 16
$expression.metric.instance = ""

$alarm.expression = New-Object VMware.Vim.OrAlarmExpression
$alarm.expression.expression += $expression

$alarm.action = New-Object VMware.Vim.AlarmTriggeringAction
$alarm.action.action = New-Object VMware.Vim.SendSNMPAction

$trans = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec
$trans.startstate = "yellow"
$trans.finalstate = "red"
$trans.repeats = $false
$alarm.action.transitionspecs += $trans

$alarm.setting = New-Object VMware.Vim.AlarmSetting
$alarm.setting.reportingFrequency = 0
$alarm.setting.toleranceRange = 0

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


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

qwert1235
Enthusiast
Enthusiast
Jump to solution

Luc,

Thank you very much for your example.

However, looks like there is some bug in a code (again, I really appreciate you for help)

This code will create an alarm, but I will not be able even to check the settings on my it through VC. "Edit settings" on this new alarm would be grey out (I can "remove" it).

If I will run the same code without creating SNMP (take away the following lines:

$alarm.action = New-Object VMware.Vim.AlarmTriggeringAction

$alarm.action.action = New-Object VMware.Vim.SendSNMPAction

$trans = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec

$trans.startstate = "yellow"

$trans.finalstate = "red"

$trans.repeats = $false

$alarm.action.transitionspecs += $trans).

it creates new alarm and I can see all setting on it.

Again, looks like something wrong with this code for alarm action -SNMP (6 lines above).

Is there something I need to adjust?

Thanks,

qwert

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I screwed up. The script I included was not complete.

Try this version. This should work.

$esxName = <ESX-servername>

$alarmMgr = Get-View AlarmManager
$entity = Get-VMHost $esxName | Get-View

$alarm = New-Object VMware.Vim.AlarmSpec

$alarm.Name = "My new alarm"
$alarm.Description = "My alarm description"
$alarm.Enabled = $TRUE

$expression = New-Object VMware.Vim.MetricAlarmExpression
$Expression.Operator = "isAbove"
$expression.red = 9000
$expression.RedInterval = 300
$expression.type = "HostSystem"
$expression.yellow = 7500
$expression.yellowinterval = 300
$expression.metric = New-Object VMware.Vim.PerfMetricId
$expression.metric.counterid = 16
$expression.metric.instance = ""

$alarm.expression = New-Object VMware.Vim.OrAlarmExpression
$alarm.expression.expression += $expression

$alarm.action = New-Object VMware.Vim.GroupAlarmAction
$action = New-Object VMware.Vim.AlarmTriggeringAction
$action.action = New-Object VMware.Vim.SendSNMPAction

$trans = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec
$trans.startstate = "yellow"
$trans.finalstate = "red"
$trans.repeats = $false
$action.transitionspecs += $trans
$alarm.action.action += $action

$alarm.setting = New-Object VMware.Vim.AlarmSetting
$alarm.setting.reportingFrequency = 0
$alarm.setting.toleranceRange = 0

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


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

qwert1235
Enthusiast
Enthusiast
Jump to solution

Luc,

Thank you very much for your help. It does work!!!

Can I please ask you one more question?

How can I write the script that will create alarm for special event (like for host Entered Maintenance Mode)?

Looks like I have to use $alarm.expression = New-Object VMware.Vim.EventAlarmExpression and $alarm.Expression.EventType = "EnteredMaintenanceModeEvent"but I think I am missing something important here...

Thank you for your help,

qwert

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, the following script will send an SNMP trap when the ESX host goes into maintenance mode.

$esxName = <ESX-hostname>

$alarmMgr = Get-View AlarmManager
$entity = Get-VMHost $esxName | Get-View

# AlarmSpec
$alarm = New-Object VMware.Vim.AlarmSpec
$alarm.Name = "My new alarm"
$alarm.Description = "My alarm description"
$alarm.Enabled = $TRUE

#Action
$alarm.action = New-Object VMware.Vim.GroupAlarmAction

$trigger = New-Object VMware.Vim.AlarmTriggeringAction
$trigger.action = New-Object VMware.Vim.SendSNMPAction

# Transaction
$trans = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec
$trans.startstate = "yellow"
$trans.finalstate = "red"
$trans.repeats = $false

$trigger.transitionspecs += $trans

$alarm.action.action += $trigger

# Expression
$expression = New-Object VMware.Vim.EventAlarmExpression
$expression.EventType = "EnteringMaintenanceModeEvent"
$expression.ObjectType = "HostSystem"
$expression.Status = "red"

$alarm.expression = New-Object VMware.Vim.OrAlarmExpression
$alarm.expression.expression += $expression

$alarm.setting = New-Object VMware.Vim.AlarmSetting
$alarm.setting.reportingFrequency = 0
$alarm.setting.toleranceRange = 0

# Create alarm.
$alarmMgr.CreateAlarm($entity.MoRef, $alarm)


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

Reply
0 Kudos
qwert1235
Enthusiast
Enthusiast
Jump to solution

Luc,

Thank you very much for your help!!!

qwert

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

After some further investigation it looks as if the previous problem with the settings was not my script but apparently the vSphere client.

Have a look at CreateAlarm not (always) compatible with the vSphere client


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

Reply
0 Kudos