VMware Cloud Community
jorber
Contributor
Contributor
Jump to solution

Powercli script Alarms

Hi.

I'm trying to create a script that can take all the vm's that have triggered a specific alarm like "Virtual machine cpu usage" with an alert and put the machine names into a csv file. so far i have found alot of information how to do this on all alarms but not how I can define just one type of alarm. Is there any simple solution to this??

BR jorber

Reply
0 Kudos
1 Solution

Accepted Solutions
sneddo
Hot Shot
Hot Shot
Jump to solution

Alright, so basically you want to filter out anything that contains "Virtual machine cpu usage" in the AlarmInfo attribute.

The simple way to do this is to just add a Where clause before you pipe to Export-CSV, like so (see the changes in bold):

$ignoreAlarms = @("Virtual machine cpu usage")
$vm_all = Get-View -ViewType VirtualMachine
$Report=@()
foreach ($vm in $vm_all){
  foreach($triggered in $vm.TriggeredAlarmState){
    If ($triggered.OverallStatus -like "red" ){
      $lineitem={} | Select Name, AlarmInfo
      $alarmDef = Get-View -Id $triggered.Alarm
      $lineitem.Name = $vm.Name
      $lineitem.AlarmInfo = $alarmDef.Info.Name
      $Report+=$lineitem
    } 
  }
}
$Report |Sort Name | Where {$ignoreAlarms -notcontains $_.AlarmInfo } | export-csv "c:\temp\VM-Red-Alarms.csv" -notypeinformation -useculture
Invoke-item "c:\temp\VM-Red-Alarms.csv"

I also added an array of alarms to ignore, that way if you have multiple different alarm types you want to ignore you can just add it to the array.

Changing to "Get-View -ViewType VirtualMachine" should give you a bit of a speed improvement as well Smiley Happy

View solution in original post

Reply
0 Kudos
4 Replies
sneddo
Hot Shot
Hot Shot
Jump to solution

This is pretty simple actually, you just need to use the Where-Object (or where, or ?, depending on how lazy you want to be!). Once you have that, pipe it to Export-CSV

Maybe post the code you are using and we can show you what needs to added/updated

Reply
0 Kudos
jorber
Contributor
Contributor
Jump to solution

I was using something I found on the net that looks like this.

$vm_all = Get-VM | Get-View
$Report=@()
foreach ($vm in $vm_all){
  foreach($triggered in $vm.TriggeredAlarmState){
  If ($triggered.OverallStatus -like "red" ){
  $lineitem={} | Select Name, AlarmInfo
  $alarmDef = Get-View -Id $triggered.Alarm
  $lineitem.Name = $vm.Name
  $lineitem.AlarmInfo = $alarmDef.Info.Name
  $Report+=$lineitem
  }
  }
}
$Report |Sort Name | export-csv "c:\temp\VM-Red-Alarms.csv" -notypeinformation -useculture
Invoke-item "c:\temp\VM-Red-Alarms.csv"

Reply
0 Kudos
sneddo
Hot Shot
Hot Shot
Jump to solution

Alright, so basically you want to filter out anything that contains "Virtual machine cpu usage" in the AlarmInfo attribute.

The simple way to do this is to just add a Where clause before you pipe to Export-CSV, like so (see the changes in bold):

$ignoreAlarms = @("Virtual machine cpu usage")
$vm_all = Get-View -ViewType VirtualMachine
$Report=@()
foreach ($vm in $vm_all){
  foreach($triggered in $vm.TriggeredAlarmState){
    If ($triggered.OverallStatus -like "red" ){
      $lineitem={} | Select Name, AlarmInfo
      $alarmDef = Get-View -Id $triggered.Alarm
      $lineitem.Name = $vm.Name
      $lineitem.AlarmInfo = $alarmDef.Info.Name
      $Report+=$lineitem
    } 
  }
}
$Report |Sort Name | Where {$ignoreAlarms -notcontains $_.AlarmInfo } | export-csv "c:\temp\VM-Red-Alarms.csv" -notypeinformation -useculture
Invoke-item "c:\temp\VM-Red-Alarms.csv"

I also added an array of alarms to ignore, that way if you have multiple different alarm types you want to ignore you can just add it to the array.

Changing to "Get-View -ViewType VirtualMachine" should give you a bit of a speed improvement as well Smiley Happy

Reply
0 Kudos
jorber
Contributor
Contributor
Jump to solution

Thanx Sneddo.

This was a nice embryo to start with.

Reply
0 Kudos