VMware Cloud Community
zimms
Contributor
Contributor
Jump to solution

powercli script to create datastore alarms

I have a lot of individual datastores that are not all the same size within a cluster. So using the default alarm is not an option. Looking for a script to create an alarm based on utilization and then have it email a notification as it changes status. I found a few scripts online but nothing that i could really get working. Below is what I have pieced together. This script could be way off base...i really don't know.

$storeName = Read-host "Enter Datastore name"

$alarmMgr = Get-View AlarmManager

$entity = Get-Datastore $storeName | Get-View

# AlarmSpec

$alarm = New-Object VMware.Vim.AlarmSpec

$alarm.Name = "Datastore-$storename"

$alarm.Description = "$storename disk utilization"

$alarm.Enabled = $TRUE

#Actions of what i want I want to be notified on

# A trigger for Green->Yellow

Get-AlarmDefinition $alarm.Name | Get-AlarmAction | New-AlarmActionTrigger -StartStatus "Green" -EndStatus "Yellow"

# A trigger for Green->Red

Get-AlarmDefinition $alarm.Name | Get-AlarmAction | New-AlarmActionTrigger -StartStatus "Green" -EndStatus "Red"

# A trigger for Yellow->Green

Get-AlarmDefinition $alarm.Name | Get-AlarmAction | New-AlarmActionTrigger -StartStatus "Yellow" -EndStatus "Green"

# A trigger for Yellow->Red

Get-AlarmDefinition $alarm.Name | Get-AlarmAction | New-AlarmActionTrigger -StartStatus "Yellow" -EndStatus "Red"

# A trigger for Red->Green

Get-AlarmDefinition $alarm.Name | Get-AlarmAction | New-AlarmActionTrigger -StartStatus "Red" -EndStatus "Green"

# A trigger for Red->Yellow

Get-AlarmDefinition $alarm.Name | Get-AlarmAction | New-AlarmActionTrigger -StartStatus "Red" -EndStatus "Yellow"

# 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.MetricAlarmExpression

$expression.Operator = "isAbove"

$expression.red = 9500

$expression.RedInterval = 300

$expression.type = "Datastore"

$expression.yellow = 9000

$expression.yellowinterval = 300

$expression.metric = New-Object VMware.Vim.PerfMetricId

$expression.metric.counterid = 240

$expression.metric.instance = ""

$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)

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

You'll have to update the emailAction properties.

$storeName = Read-host "Enter Datastore name"

$alarmMgr = Get-View AlarmManager

$entity = Get-View -ViewType Datastore -Property Name -Filter @{'Name'=$storeName}

# Find metric ID

$si = Get-View ServiceInstance

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

$metricId = $perfMgr.PerfCounter |

    where{$_.GroupInfo.Key -eq 'disk' -and $_.NameInfo.Key -eq 'used' -and $_.RollupType -eq 'latest'} |

    select -ExpandProperty Key

# AlarmSpec

$spec = New-Object VMware.Vim.AlarmSpec

$spec.Name = "Datastore-$($entity.Name)"

$spec.Description = "$($entity.Name) disk utilization"

$spec.Enabled = $TRUE

# Expression

$expression = New-Object VMware.Vim.MetricAlarmExpression

$expression.Operator = "isAbove"

$expression.red = 9000

$expression.type = "Datastore"

$expression.yellow = 8500

$expression.metric = New-Object VMware.Vim.PerfMetricId

$expression.metric.counterid = $metricId

$expression.metric.instance = ""

$spec.expression = New-Object VMware.Vim.AndAlarmExpression

$spec.expression.expression += $expression

# Action

$actionGroup = New-Object VMware.Vim.GroupAlarmAction

$actionTrigger = New-Object VMware.Vim.AlarmTriggeringAction

$emailAction = New-Object VMware.Vim.SendEmailAction

$emailAction.ToList = 'lucd@lucd.info'

$emailAction.Subject = 'Alarm {alarmName} fired!'

$emailAction.Body = "{eventDescription}"

$emailAction.CcList = ''

$actionTrigger.Action += $emailAction

$actionTrigger.Green2yellow = $TRUE

$actionTrigger.Yellow2red = $TRUE

$trans1 = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec

$trans1.StartState = [VMware.Vim.ManagedEntityStatus]::green

$trans1.FinalState = [VMware.Vim.ManagedEntityStatus]::yellow

$trans1.Repeats = $false

$actionTrigger.TransitionSpecs += $trans1

$trans2 = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec

$trans2.StartState = [VMware.Vim.ManagedEntityStatus]::yellow

$trans2.FinalState = [VMware.Vim.ManagedEntityStatus]::red

$trans2.Repeats = $false

$actionTrigger.TransitionSpecs += $trans2

$actionGroup.Action += $actionTrigger

$spec.Action = $actionGroup

# Transaction

$trans = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec

$trans.StartState = "yellow"

$trans.FinalState = "red"

$trans.Repeats = $false

# Setting

$spec.Setting = New-Object VMware.Vim.AlarmSetting

$spec.Setting.ReportingFrequency = 0

$spec.Setting.ToleranceRange = 0

$spec.ActionFrequency = 0

# Create alarm.

$alarmMgr.CreateAlarm($entity.MoRef, $spec)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
17 Replies
LucD
Leadership
Leadership
Jump to solution

Not sure what you are trying to accomplish here.

What is your rule for datastores to generate an alarm? In other words what would be the watermark values for `Warning and Alert?

You can use datastore usage in %, that way you wouldn't need to take the actual size into account.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
zimms
Contributor
Contributor
Jump to solution

We can't use a blanket policy for % either (I have datastores of 20+ different sizes). I need to set an absolute value on each datastore for warning. On some datastores it will be 85%, while others it could be 98%. I inherited a bunch of crap I can't really fix at the moment and just need to be able to set individual alarms. I realize its not optimal, but I don't really have a choice right now.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, let's start from the beginning.

Do you have a list, where per datastore it is listed at which percent or size the threshold should be set?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
zimms
Contributor
Contributor
Jump to solution

i would say most of the datastores... are 2TB in size. I'm wanting to use an 85%=yellow  and then 90% = red.  If I can get a script to do that, i can modify it later to do all the one offs.

0 Kudos
zimms
Contributor
Contributor
Jump to solution

Lucd, any update on how I can get this script working under powercli 6.x

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

You'll have to update the emailAction properties.

$storeName = Read-host "Enter Datastore name"

$alarmMgr = Get-View AlarmManager

$entity = Get-View -ViewType Datastore -Property Name -Filter @{'Name'=$storeName}

# Find metric ID

$si = Get-View ServiceInstance

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

$metricId = $perfMgr.PerfCounter |

    where{$_.GroupInfo.Key -eq 'disk' -and $_.NameInfo.Key -eq 'used' -and $_.RollupType -eq 'latest'} |

    select -ExpandProperty Key

# AlarmSpec

$spec = New-Object VMware.Vim.AlarmSpec

$spec.Name = "Datastore-$($entity.Name)"

$spec.Description = "$($entity.Name) disk utilization"

$spec.Enabled = $TRUE

# Expression

$expression = New-Object VMware.Vim.MetricAlarmExpression

$expression.Operator = "isAbove"

$expression.red = 9000

$expression.type = "Datastore"

$expression.yellow = 8500

$expression.metric = New-Object VMware.Vim.PerfMetricId

$expression.metric.counterid = $metricId

$expression.metric.instance = ""

$spec.expression = New-Object VMware.Vim.AndAlarmExpression

$spec.expression.expression += $expression

# Action

$actionGroup = New-Object VMware.Vim.GroupAlarmAction

$actionTrigger = New-Object VMware.Vim.AlarmTriggeringAction

$emailAction = New-Object VMware.Vim.SendEmailAction

$emailAction.ToList = 'lucd@lucd.info'

$emailAction.Subject = 'Alarm {alarmName} fired!'

$emailAction.Body = "{eventDescription}"

$emailAction.CcList = ''

$actionTrigger.Action += $emailAction

$actionTrigger.Green2yellow = $TRUE

$actionTrigger.Yellow2red = $TRUE

$trans1 = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec

$trans1.StartState = [VMware.Vim.ManagedEntityStatus]::green

$trans1.FinalState = [VMware.Vim.ManagedEntityStatus]::yellow

$trans1.Repeats = $false

$actionTrigger.TransitionSpecs += $trans1

$trans2 = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec

$trans2.StartState = [VMware.Vim.ManagedEntityStatus]::yellow

$trans2.FinalState = [VMware.Vim.ManagedEntityStatus]::red

$trans2.Repeats = $false

$actionTrigger.TransitionSpecs += $trans2

$actionGroup.Action += $actionTrigger

$spec.Action = $actionGroup

# Transaction

$trans = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec

$trans.StartState = "yellow"

$trans.FinalState = "red"

$trans.Repeats = $false

# Setting

$spec.Setting = New-Object VMware.Vim.AlarmSetting

$spec.Setting.ReportingFrequency = 0

$spec.Setting.ToleranceRange = 0

$spec.ActionFrequency = 0

# Create alarm.

$alarmMgr.CreateAlarm($entity.MoRef, $spec)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
zimms
Contributor
Contributor
Jump to solution

Let me see if this works and I'll get back to ya. Thanks

0 Kudos
zimms
Contributor
Contributor
Jump to solution

This is the error I get. Seems to error on the alarm creation.

Cannot convert argument "entity", with value: "System.Object[]", for "CreateAlarm" to type

"VMware.Vim.ManagedObjectReference": "Cannot convert the "System.Object[]" value of type "System.Object[]" to type

"VMware.Vim.ManagedObjectReference"."

At C:\temp\dstore_alert.ps1:67 char:1

+ $alarmMgr.CreateAlarm($entity.MoRef, $spec)

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodException

    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That seems to indicate the line

$entity = Get-View -ViewType Datastore -Property Name -Filter @{'Name'=$storeName}

is returning more than one object.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
zimms
Contributor
Contributor
Jump to solution

what is the work around for that?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

There are a number of options.

  • Create the Alarm in a ForEach loop for each datastore returned
  • Be more specific in the datastorename or the Get-View filter
  • Run the Get-View in a specific branch. Use the SearchRoot parameter

Do you actually have datastores with the same name?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
zimms
Contributor
Contributor
Jump to solution

Just for grins.... I was using powercli 6.5 version. So I uninstalled it and loaded 5.8 version. Ran the script you had and now it works.

What's different in the new powercli version?

0 Kudos
zimms
Contributor
Contributor
Jump to solution

This script will run on version 5.8 of powercli but not the newer versions.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The error you listed, about the array, shouldn't have anything to do with the PowerCLI version.

It is related to how many objects are returned by the Get-View cmdlet.

It would be interesting to see a different output of that Get-View line, depending on the PowerCLI version you are using.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
JoaquimCastro
Contributor
Contributor
Jump to solution

First, great thread guys! helped me a lot!

However i'm struggling with one thing..

I cannot find the way to check the "Trigger if any of the conditions are satisfied", since the "... if all ..." is the option that gets checked...

Can any of you guys help me?

thank you in advance!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You will need to replace this line

$spec.expression = New-Object VMware.Vim.AndAlarmExpression

with this line

$spec.expression = New-Object VMware.Vim.OrAlarmExpression

This will OR the expressions (if you have multiple) instead of AND


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

JoaquimCastro
Contributor
Contributor
Jump to solution

Thanks LucD! you're the best! Smiley Happy

0 Kudos