1 2 3 Previous Next 43 Replies Latest reply on Sep 16, 2020 9:18 AM by mk5213

    Create a custom CPU alarm for VM using PowerCli

    nikpetrov95 Novice

      Hello! I need to create an alarm on the CPU for each virtual machine, after the alarm has worked, I need to send a message to the user's mail, mail information should be taken from the user attribute of VM (I did it myself). Please help me with this matter.

        • 1. Re: Create a custom CPU alarm for VM using PowerCli
          LucD Guru
          vExpertCommunity WarriorsUser Moderators

          Can you provide some more details about the requirement?

          What do you mean by "... alarm on the CPU"?
          Is that when CPU utilisation goes over a specific watermark?

           

          Also, what do you mean by "... mail information should be taken from the user attribute"?

          Is that the email To address?

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: Create a custom CPU alarm for VM using PowerCli
            nikpetrov95 Novice

            What do you mean by "... alarm on the CPU"?-  for example standart vcenter alarm "Virtual machine CPU usage"

            Is that when CPU utilisation goes over a specific watermark? - Yes, it is

            I need to create an alarm action on each virtual machine (by the name of the machine) this is the main requirement

             

             

            Also, what do you mean by "... mail information should be taken from the user attribute"?

            Is that the email To address?

             

             

            foreach($vmName in $vmNames){

             

            $entity = Get-VM -Name $vmName

             

            $mail=(Get-Annotation -Entity (get-vm $vmName) -CustomAttribute Contact).value

             

            $action = New-AlarmAction -Email -Subject 'Test' -To "$mail" -AlarmDefinition $def

             

            I need something like this:

             

            • 3. Re: Create a custom CPU alarm for VM using PowerCli
              LucD Guru
              vExpertUser ModeratorsCommunity Warriors

              That is in fact the solution.
              What is missing from that code?

              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              • 4. Re: Create a custom CPU alarm for VM using PowerCli
                nikpetrov95 Novice

                I can't found this part for cpu

                 

                 

                $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

                • 5. Re: Create a custom CPU alarm for VM using PowerCli
                  LucD Guru
                  User ModeratorsCommunity WarriorsvExpert

                  In the recent PowerCLI release there are new cmdlets to create metric based alarms.

                  Something like this

                   

                  $vm = Get-VM -Name MyVM

                  $userEmail = (Get-Annotation -Entity $vm -CustomAttribute 'Contact').value

                  $sAlarmTrigger = @{

                      Metric = Get-Metric -MetricGroup 'CPU' -Name 'usage' | select -First 1

                      MetricAlarmOperator = 'Above'

                      Yellow = 50

                      Red = 75

                      EntityType = 'VirtualMachine'

                  }

                  $trigger = New-AlarmTrigger @sAlarmTrigger


                  $sAlarmAction = @{

                      Email = $true

                      Subject = 'CPU Usage high'

                      To = $userEmail

                  }

                  $action = New-AlarmAction @sAlarmAction


                  $sAlarmDef = @{

                      Name = 'CPU High'

                      Description = 'Trigger alarm when CPU utilisation is too high'

                      Entity = $vm

                      AlarmTrigger = $trigger

                      AlarmAction = $action

                  }

                  New-AlarmDefinition @sAlarmDef

                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                  • 6. Re: Create a custom CPU alarm for VM using PowerCli
                    nikpetrov95 Novice

                    Thank you, but I had error if I use 2 or more vms.

                     

                    Get-Annotation: Cannot convert "System.Object []" to type "VMware.VimAutomation.ViCore.Types.V1.Inventory.Inven
                    toryItem "required for the Entity parameter. The specified method is not supported.
                    line: 1 character: 38
                    + $ userEmail = (Get-Annotation -Entity $ vm -CustomAttribute 'Contact').

                    • 7. Re: Create a custom CPU alarm for VM using PowerCli
                      LucD Guru
                      User ModeratorsCommunity WarriorsvExpert

                      You seem to have a blank between $ and vm on the Entity parameter

                      Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                      • 8. Re: Create a custom CPU alarm for VM using PowerCli
                        nikpetrov95 Novice

                        I haven't blunk between $ and vm

                         

                        $vm = Get-VM -Name "test1","test2"

                        $userEmail = (Get-Annotation -Entity $vm -CustomAttribute 'Contact').value

                        Get-Annotation : Не удается преобразовать "System.Object[]" в тип "VMware.VimAutomation.ViCore.Types.V1.Inventory.InventoryItem", необходимый для п

                        араметра "Entity". Указанный метод не поддерживается.

                        строка:1 знак:38

                        + $userEmail = (Get-Annotation -Entity $vm -CustomAttribute 'Contact').

                        • 9. Re: Create a custom CPU alarm for VM using PowerCli
                          LucD Guru
                          User ModeratorsvExpertCommunity Warriors

                          It looked like that from the previous screenshot.

                           

                          But if you want to do this for multiple VMs, the script need some changes.

                          Something like this

                           

                          Get-VM -Name 'Test1', 'Test2' -PipelineVariable vm |

                          ForEach-Object -Process {

                              $userEmail = (Get-Annotation -Entity $vm -CustomAttribute 'Contact').value

                              $sAlarmTrigger = @{

                                  Metric              = Get-Metric -MetricGroup 'CPU' -Name 'usage' | Select-Object -First 1

                                  MetricAlarmOperator = 'Above'

                                  Yellow              = 50

                                  Red                 = 75

                                  EntityType          = 'VirtualMachine'

                              }

                              $trigger = New-AlarmTrigger @sAlarmTrigger


                              $sAlarmAction = @{

                                  Email   = $true

                                  Subject = 'CPU Usage high'

                                  To      = $userEmail

                              }

                              $action = New-AlarmAction @sAlarmAction


                              $sAlarmDef = @{

                                  Name         = 'CPU High'

                                  Description  = 'Trigger alarm when CPU utilisation is too high'

                                  Entity       = $vm

                                  AlarmTrigger = $trigger

                                  AlarmAction  = $action

                              }


                              New-AlarmDefinition @sAlarmDef

                          }

                          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                          • 10. Re: Create a custom CPU alarm for VM using PowerCli
                            nikpetrov95 Novice

                            Excellent! It works.

                            How can I use the miltipale mail address?

                             

                            New-AlarmAction: Unable to bind the "To" parameter to the target. Exception when setting "To": "Invalid email address: 1@test.com; 2@test.om "

                             

                             

                             

                             

                             

                             

                            • 11. Re: Create a custom CPU alarm for VM using PowerCli
                              LucD Guru
                              User ModeratorsvExpertCommunity Warriors

                              Multiple mail addresses should be separated with a comma afaik.

                              Try replacing the ; with a ,

                              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                              • 12. Re: Create a custom CPU alarm for VM using PowerCli
                                nikpetrov95 Novice

                                The comma did n't help

                                 

                                New-AlarmAction :Cannot bind “To” parameter to target. An exception "To" : "Invalid email address: 1@test.com,2@test.com"

                                • 13. Re: Create a custom CPU alarm for VM using PowerCli
                                  LucD Guru
                                  vExpertUser ModeratorsCommunity Warriors

                                  Seems to work for me with an array of strings

                                   

                                  To      = 'user1@local.lab','user2@local.lab'

                                   

                                  In your case you could try

                                   

                                  To      = $userEmail.Split(';')
                                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                                  • 14. Re: Create a custom CPU alarm for VM using PowerCli
                                    nikpetrov95 Novice

                                    It's work! Thank you so much!

                                     

                                    Get-VM -Name 'Test1','Test2' -PipelineVariable vm |

                                    ForEach-Object -Process {

                                     

                                     

                                        $userEmail = (Get-Annotation -Entity $vm -CustomAttribute 'Contact').value

                                     

                                     

                                        $sAlarmTrigger = @{

                                     

                                     

                                            Metric              = Get-Metric -MetricGroup 'CPU' -Name 'usage' | Select-Object -First 1

                                     

                                     

                                            MetricAlarmOperator = 'Above'

                                     

                                     

                                            Yellow              = 50

                                     

                                     

                                            Red                 = 75

                                     

                                     

                                            EntityType          = 'VirtualMachine'

                                     

                                     

                                        }

                                     

                                     

                                        $trigger = New-AlarmTrigger @sAlarmTrigger

                                     

                                     

                                     

                                     

                                        $sAlarmAction = @{

                                     

                                     

                                            Email   = $true

                                     

                                     

                                            Subject = 'CPU Usage high'

                                     

                                     

                                            To      = $userEmail.Split(';')

                                     

                                     

                                        }

                                     

                                     

                                        $action = New-AlarmAction @sAlarmAction

                                     

                                     

                                     

                                     

                                        $sAlarmDef = @{

                                     

                                     

                                            Name         = "CPU High_$vm"

                                     

                                     

                                            Description  = 'Trigger alarm when CPU utilisation is too high'

                                     

                                     

                                            Entity       = $vm

                                     

                                     

                                            AlarmTrigger = $trigger

                                     

                                     

                                            AlarmAction  = $action

                                     

                                     

                                        }

                                     

                                     

                                     

                                     

                                        New-AlarmDefinition @sAlarmDef

                                     

                                     

                                    }

                                    1 2 3 Previous Next