Automation

 View Only
  • 1.  Create alarms by script?

    Posted Nov 09, 2009 04:40 PM

    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



  • 2.  RE: Create alarms by script?

    Posted Nov 09, 2009 08:16 PM

    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)
    



  • 3.  RE: Create alarms by script?

    Posted Nov 10, 2009 05:23 PM

    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



  • 4.  RE: Create alarms by script?

    Posted Nov 10, 2009 09:54 PM

    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)
    



  • 5.  RE: Create alarms by script?

    Posted Nov 11, 2009 04:45 PM

    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



  • 6.  RE: Create alarms by script?
    Best Answer

    Posted Nov 11, 2009 06:11 PM

    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)
    



  • 7.  RE: Create alarms by script?

    Posted Nov 11, 2009 06:29 PM

    Luc,

    Thank you very much for your help!!!

    qwert



  • 8.  RE: Create alarms by script?

    Posted Nov 11, 2009 09:25 PM

    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