7 Replies Latest reply on Nov 11, 2009 1:25 PM by LucD

    Create alarms by script?

    qwert1235 Hot Shot

      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

        • 1. Re: Create alarms by script?
          LucD Guru
          User ModeratorsCommunity WarriorsvExpert

          Did you already have a look at this thread ( Is it possible to create Alarms by using CLI?) ?

           

          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)
          

           

          1 person found this helpful
          • 2. Re: Create alarms by script?
            qwert1235 Hot Shot

            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

            • 3. Re: Create alarms by script?
              LucD Guru
              User ModeratorsCommunity WarriorsvExpert

              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)
              

               

              1 person found this helpful
              • 4. Re: Create alarms by script?
                qwert1235 Hot Shot

                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

                • 5. Re: Create alarms by script?
                  LucD Guru
                  User ModeratorsvExpertCommunity Warriors

                  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)
                  

                   

                  • 6. Re: Create alarms by script?
                    qwert1235 Hot Shot

                    Luc,

                     

                    Thank you very much for your help!!!

                     

                    qwert

                    • 7. Re: Create alarms by script?
                      LucD Guru
                      vExpertCommunity WarriorsUser Moderators

                      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