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)