1 2 Previous Next 22 Replies Latest reply on Feb 28, 2020 3:55 AM by nikpetrov95 Go to original post
      • 15. Re: Create a custom CPU alarm for VM using PowerCli
        nikpetrov95 Novice

        LucD Hello, how  I can create $sAlarmTrigger like this?

         

         

         

        or set time option for this

         

          $sAlarmTrigger = @{

         

         

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

         

         

                MetricAlarmOperator = 'Above'

         

         

                Yellow              = 50

         

         

                Red                 = 75

         

         

                EntityType          = 'VirtualMachine'

         

         

            }

         

         

            $trigger = New-AlarmTrigger @sAlarmTrigger

         

         

        and I had incorrect alarms

         

        Target: Test1

        Previous Status: Gray

        New Status: Red

         

        Alarm Definition:

        ([Yellow metric Is above 0%; Red metric Is above 0%] OR [Yellow metric Is above 0%; Red metric Is above 0%])

         

        Current values for metric/state:

        Metric CPU Usage = 30% OR Metric CPU Usage = 30%

         

        Description:

        Alarm 'CPU High_test1' on MSK-DPRO-APL555 changed from Gray to Red

        • 16. Re: Create a custom CPU alarm for VM using PowerCli
          LucD Guru
          Community WarriorsvExpertUser Moderators

          There were a few issues with my previous solution.

          The Get-Metric cmdlet doesn't really one to select the correct metric.

          The values for the thresholds need to be multiplied by 100.

          And the duration can be defined.

           

          My new version which should fix the issues.

           

          $vmNames = 'test01','test02'

          $metricName = 'cpu.usage.average'


          $mGroup,$mName,$mRollup = $metricName.Split('.')

          $si = Get-View ServiceInstance

          $perfMgr = Get-View -Id $si.Content.PerfManager

          $metric = $perfMgr.PerfCounter |

              where{$_.GroupInfo.Key -eq $mGroup -and

                    $_.NameInfo.Key -eq $mName -and

                    $_.RollupType -eq $mRollup}


          Get-VM -Name $vmNames-PipelineVariable vm |

          ForEach-Object -Process {

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

              $sAlarmTrigger = @{

                  Metric              = Get-Metric -MetricGroup 'CPU' -Name 'usage' | where{$_.Key -eq $metric.Key}

                  MetricAlarmOperator = 'Above'

                  Red                 = 7500

                  RedIntervalSeconds  = 300

                  EntityType          = 'VirtualMachine'

              }

              $trigger = New-AlarmTrigger @sAlarmTrigger


              $sAlarmAction = @{

                  Email   = $true

                  Subject = 'CPU Usage high'

                  To      = $userEmail.Split(';')

              }

              $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
          • 18. Re: Create a custom CPU alarm for VM using PowerCli
            nikpetrov95 Novice

            Hi LucD, I need your help again .... I can't set up a mail resend(after 5 minutes) notification for this alarm. can you help me?

             

             

            I can only do this after creating an alarm and can't change the source of the alarm, and therefore additional steps must be taken.

             

            Get-AlarmDefinition -Name "CPU High DBA340"| Get-AlarmAction | New-AlarmActionTrigger -StartStatus Red -EndStatus Yellow -Repeat

            Get-AlarmDefinition -Name "CPU High DBA340"| Get-AlarmAction|Get-AlarmActionTrigger |select -First 1 | Remove-AlarmActionTrigger -Confirm:$false

            Get-AlarmDefinition -Name "CPU High DBA340"| Get-AlarmAction | set-AlarmActionTrigger -StartStatus Yellow -EndStatus Red -Repeat

            Get-AlarmDefinition -Name "CPU High DBA340"| Get-AlarmAction|Get-AlarmActionTrigger |select -First 1 | Remove-AlarmActionTrigger -Confirm:$false

            • 19. Re: Create a custom CPU alarm for VM using PowerCli
              LucD Guru
              Community WarriorsvExpertUser Moderators

              You mean something like this?

               

              $alarm = Get-AlarmDefinition -Name 'CPU High'

              $action = Get-AlarmAction -AlarmDefinition $alarm -ActionType SendEmail

               

              Remove-AlarmAction -AlarmAction $action -Confirm:$false

               

              $trigger = New-AlarmActionTrigger -StartStatus $action.Trigger[0].StartStatus -EndStatus $action.Trigger[0].EndStatus -Repeat

              New-AlarmAction -AlarmDefinition $alarm -Email -Subject $action.Subject -To $action.To -AlarmActionTrigger $trigger


              Set-AlarmDefinition -AlarmDefinition $alarm -ActionRepeatMinutes 5 -Confirm:$false

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

                No, I want to make changes to the current script.

                 

                $ErrorActionPreference= 'Stop'
                $metricName = 'cpu.usage.average'
                
                
                $mGroup,$mName,$mRollup = $metricName.Split('.')
                
                
                $si = Get-View ServiceInstance
                
                
                $perfMgr = Get-View -Id $si.Content.PerfManager
                
                
                $metric = $perfMgr.PerfCounter |
                
                
                    where{$_.GroupInfo.Key -eq $mGroup -and
                
                
                          $_.NameInfo.Key -eq $mName -and
                
                
                          $_.RollupType -eq $mRollup}
                
                
                $VMcluster=read-host "Enter Cluster name"
                $VM_with_contact=(Get-Cluster $VMcluster|get-vm |Get-Annotation -CustomAttribute 'Contact'|where{$_.Value -ne ""}).AnnotatedEntity.name
                
                
                Get-VM $VM_with_contact|?{$_.Name -notlike  "*Template*" -and $_.Name -notlike  "*Old*"} -PipelineVariable vm |
                
                
                ForEach-Object -Process {
                
                
                    $userEmail = (Get-Annotation -Entity $vm -CustomAttribute 'Contact').value -replace " "
                
                
                    $sAlarmTrigger = @{
                
                
                        Metric              = Get-Metric -MetricGroup 'CPU' -Name 'usage' | where{$_.Key -eq $metric.Key}
                
                
                        MetricAlarmOperator = 'Above'
                
                
                        Red                 = 9500
                
                
                        RedIntervalSeconds  = 86400
                
                
                        EntityType          = 'VirtualMachine'
                
                    }
                
                    $trigger = New-AlarmTrigger @sAlarmTrigger
                
                
                    $sAlarmAction = @{
                
                
                        Email   = $true
                
                Body = "$vm CPU Usage is above 95% for 24 hours , please check your OS."
                        Subject = "High CPU Usage $vm"
                
                        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|ft -AutoSize
                }
                
                $ErrorActionPreference= 'Continue'
                

                 

                And after running the script, I want to see the following:

                 

                Get-AlarmDefinition -Name "CPU High DBA340"| Get-AlarmAction |Get-AlarmActionTrigger

                 

                 

                StartStatus     EndStatus       Repeat

                -----------     ---------       ------

                Yellow          Red             TRUE

                • 21. Re: Create a custom CPU alarm for VM using PowerCli
                  LucD Guru
                  Community WarriorsvExpertUser Moderators

                  Ok, try like this

                   

                  $vmNames = 'test01','test02'

                  $metricName = 'cpu.usage.average'


                  $mGroup,$mName,$mRollup = $metricName.Split('.')

                  $si = Get-View ServiceInstance

                  $perfMgr = Get-View -Id $si.Content.PerfManager

                  $metric = $perfMgr.PerfCounter |

                      where{$_.GroupInfo.Key -eq $mGroup -and

                            $_.NameInfo.Key -eq $mName -and

                            $_.RollupType -eq $mRollup}


                  Get-VM -Name $vmNames -PipelineVariable vm |

                  ForEach-Object -Process {

                      Get-AlarmDefinition -Name 'CPU High' -Entity $_ -ErrorAction SilentlyContinue |

                      Remove-AlarmDefinition -Confirm:$false

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


                      $sAlarmTrigger = @{

                          Metric              = Get-Metric -MetricGroup 'CPU' -Name 'usage' | where{$_.Key -eq $metric.Key}

                          MetricAlarmOperator = 'Above'

                          Red                 = 7500

                          RedIntervalSeconds  = 300

                          EntityType          = 'VirtualMachine'

                      }

                      $trigger = New-AlarmTrigger @sAlarmTrigger


                      $sActionTrigger = @{

                          StartStatus = 'yellow'

                          EndStatus = 'red'

                          Repeat = $true

                      }

                      $actionTrigger = New-AlarmActionTrigger @sActionTrigger


                      $sAlarmAction = @{

                          Email   = $true

                          Subject = 'CPU Usage high'

                          To      = $userEmail

                          AlarmActionTrigger = $actionTrigger

                      }

                      $action = New-AlarmAction @sAlarmAction


                      $sAlarmDef = @{

                          Name         = 'CPU High'

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

                          Entity       = $vm

                          AlarmTrigger = $trigger

                          AlarmAction  = $action

                          ActionRepeatMinutes = 5

                      }


                      New-AlarmDefinition @sAlarmDef

                  }

                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                  1 2 Previous Next