So, we ahve a global alarm to alert on disk space, but we have a few that are above that and keep alerting. I'd like to remove the global alarm and set it on each object.
I was looking at the powercli commandlets and only see a way to modify the definitions but not set hte alarm on an object. Is this possible or will I need to get some onyx code?
No cmdlets for that I'm afraid.
But you can move/copy that alarm. Have a look at Alarms – Moving them around
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks, so I have to create the exact same alarm on all of them so I decided to just get the code after creating the alarm through the gui. I then deleted the alarm and tried to run it, but it is saying that the alarm has been deleted or not fully created. It's like it has a cached copy of it somewhere?
Exception calling "CreateAlarm" with "2" argument(s): "The object has already been deleted or has
not been completely created"
At line:1 char:1
+ $_this.CreateAlarm($entity, $spec)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : VimException
$datastore = Get-Datastore datastore
# ------- CreateAlarm ------- $entity = New-Object VMware.Vim.ManagedObjectReference $entity.type = "Datastore" $entity.Value = "$datastore.extensiondata.moref.value" $spec = New-Object VMware.Vim.AlarmSpec $spec.name = "DatastoreSpace-$datastore.name" $spec.description = "" $spec.enabled = $true $spec.expression = New-Object VMware.Vim.OrAlarmExpression $spec.expression.expression = New-Object VMware.Vim.AlarmExpression[] (1) $spec.expression.expression[0] = New-Object VMware.Vim.MetricAlarmExpression $spec.expression.expression[0].operator = "isAbove" $spec.expression.expression[0].type = "Datastore" $spec.expression.expression[0].metric = New-Object VMware.Vim.PerfMetricId $spec.expression.expression[0].metric.counterId = 240 $spec.expression.expression[0].metric.instance = "" $spec.expression.expression[0].yellow = 8500 $spec.expression.expression[0].red = 9000 $spec.action = New-Object VMware.Vim.GroupAlarmAction $spec.action.action = New-Object VMware.Vim.AlarmAction[] (1) $spec.action.action[0] = New-Object VMware.Vim.AlarmTriggeringAction $spec.action.action[0].action = New-Object VMware.Vim.SendEmailAction $spec.action.action[0].action.toList = "blah@blah.com;" $spec.action.action[0].action.ccList = "" $spec.action.action[0].action.subject = "" $spec.action.action[0].action.body = "" $spec.action.action[0].transitionSpecs = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec[] (1) $spec.action.action[0].transitionSpecs[0] = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec $spec.action.action[0].transitionSpecs[0].startState = "yellow" $spec.action.action[0].transitionSpecs[0].finalState = "red" $spec.action.action[0].transitionSpecs[0].repeats = $true $spec.action.action[0].green2yellow = $false $spec.action.action[0].yellow2red = $false $spec.action.action[0].red2yellow = $false $spec.action.action[0].yellow2green = $false $spec.actionFrequency = 28800 $spec.setting = New-Object VMware.Vim.AlarmSetting $spec.setting.toleranceRange = 0 $spec.setting.reportingFrequency = 0 $_this = Get-View -Id 'AlarmManager-AlarmManager' $_this.CreateAlarm($entity, $spec)
Afaik there is no cached copy. If the alarm is deleted, it is gone.
Can you add a check before calling CreateAlarm ?
Something like
Get-AlarmDefinition -Name "DatastoreSpace-$datastore.name"
If this returns an object, there must be an alarm with the same name present.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I added a check but there is no alarm with that name 😕 here is the ouput. Any ideas?
PowerCLI U:\powercli\create alarm> .\alarm_datastore.ps1
Get-AlarmDefinition : 3/13/2013 7:51:32 AM Get-AlarmDefinition AlarmDefinition with name
'DatastoreSpace-datastore1234' was not found using the specified filter(s).
At U:\powercli\create alarm\alarm_datastore.ps1:2 char:15
+ $checkalarm = Get-AlarmDefinition -Name "DatastoreSpace-$datastore"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [Get-AlarmDefinition], VimException
+ FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmd
lets.Commands.Alarm.GetAlarmDefinition
Exception calling "CreateAlarm" with "2" argument(s): "The object has already been deleted or has
not been completely created"
At U:\powercli\create alarm\alarm_datastore.ps1:51 char:1
+ $_this.CreateAlarm($entity, $spec)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : VimException
I did remove the .name part on the datastore name because it looks like it outputs the name only when referencing the variable?
$datastore = Get-Datastore datastore1234
$checkalarm = Get-AlarmDefinition -Name "DatastoreSpace-$datastore" if ($checkalarm) { Write-Host "Alarm is already defined!" } else{ # ------- CreateAlarm ------- $entity = New-Object VMware.Vim.ManagedObjectReference $entity.type = "Datastore" $entity.Value = "$datastore.extensiondata.moref.value" $spec = New-Object VMware.Vim.AlarmSpec $spec.name = "DatastoreSpace-$datastore" $spec.description = "" $spec.enabled = $true $spec.expression = New-Object VMware.Vim.OrAlarmExpression $spec.expression.expression = New-Object VMware.Vim.AlarmExpression[] (1) $spec.expression.expression[0] = New-Object VMware.Vim.MetricAlarmExpression $spec.expression.expression[0].operator = "isAbove" $spec.expression.expression[0].type = "Datastore" $spec.expression.expression[0].metric = New-Object VMware.Vim.PerfMetricId $spec.expression.expression[0].metric.counterId = 240 $spec.expression.expression[0].metric.instance = "" $spec.expression.expression[0].yellow = 8500 $spec.expression.expression[0].red = 9000 $spec.action = New-Object VMware.Vim.GroupAlarmAction $spec.action.action = New-Object VMware.Vim.AlarmAction[] (1) $spec.action.action[0] = New-Object VMware.Vim.AlarmTriggeringAction $spec.action.action[0].action = New-Object VMware.Vim.SendEmailAction $spec.action.action[0].action.toList = "blah@blah.com;" $spec.action.action[0].action.ccList = "" $spec.action.action[0].action.subject = "" $spec.action.action[0].action.body = "" $spec.action.action[0].transitionSpecs = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec[] (1) $spec.action.action[0].transitionSpecs[0] = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec $spec.action.action[0].transitionSpecs[0].startState = "yellow" $spec.action.action[0].transitionSpecs[0].finalState = "red" $spec.action.action[0].transitionSpecs[0].repeats = $true $spec.action.action[0].green2yellow = $false $spec.action.action[0].yellow2red = $false $spec.action.action[0].red2yellow = $false $spec.action.action[0].yellow2green = $false $spec.actionFrequency = 28800 $spec.setting = New-Object VMware.Vim.AlarmSetting $spec.setting.toleranceRange = 0 $spec.setting.reportingFrequency = 0 $_this = Get-View -Id 'AlarmManager-AlarmManager' $_this.CreateAlarm($entity, $spec) }
One more update, I tried it with random numbers at end of the name 12345678 and I receive same error
Get-AlarmDefinition : 3/13/2013 7:55:39 AM Get-AlarmDefinition AlarmDefinition with name
'DatastoreSpace-12345678' was not found using the specified filter(s).
At U:\powercli\create alarm\alarm_datastore.ps1:2 char:15
+ $checkalarm = Get-AlarmDefinition -Name "DatastoreSpace-12345678"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [Get-AlarmDefinition], VimException
+ FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmd
lets.Commands.Alarm.GetAlarmDefinition
Exception calling "CreateAlarm" with "2" argument(s): "The object has already been deleted or has
not been completely created"
At U:\powercli\create alarm\alarm_datastore.ps1:51 char:1
+ $_this.CreateAlarm($entity, $spec)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : VimException
Maybe I have something wrong with the code I got. I'll check onyx again and make sure I coped it correctly.
Have a look in the vpxd log on your vCenter.
Sometimes you'll find a better indication of what is missing in the specs in that log.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Aha! It can't find the object. I found my typo, I had the object in quotes instead of just the variable
$entity.Value = "$datastore.extensiondata.moref.value"
$entity
.Value = $datastore.extensiondata.moref.value
2013-03-13T08:06:12.236-05:00 [09068 info 'Default' opID=133f4f42] [VpxLRO] -- BEGIN task-12050 -- -- vim.alarm.AlarmManager.create -- 52665e09-8dd6-9573-897e-a2358e302326(526baa29-7f14-ff92-8b15-a81bca0a3f7b)
2013-03-13T08:06:12.236-05:00 [09068 error 'Default' opID=133f4f42] (Log recursion level 2) vmodl.fault.ManagedObjectNotFound
------ In-memory logs end --------
2013-03-13T08:06:12.236-05:00 [09068 error 'Default' opID=133f4f42] Section for VMware VirtualCenter, pid=3860, version=5.0.0, build=build-913577, option=Release
-->
2013-03-13T08:06:12.236-05:00 [09068 info 'Default' opID=133f4f42] [VpxLRO] -- FINISH task-12050 -- -- vim.alarm.AlarmManager.create -- 52665e09-8dd6-9573-897e-a2358e302326(526baa29-7f14-ff92-8b15-a81bca0a3f7b)
2013-03-13T08:06:12.236-05:00 [09068 info 'Default' opID=133f4f42] [VpxLRO] -- ERROR task-12050 -- -- vim.alarm.AlarmManager.create: vmodl.fault.ManagedObjectNotFound:
--> Result:
--> (vmodl.fault.ManagedObjectNotFound) {
--> dynamicType = <unset>,
--> faultCause = (vmodl.MethodFault) null,
--> obj = 'vim.ManagedEntity:datastore123.extensiondata.moref.value',
--> msg = "",
--> }
--> Args:
-->
--> Arg entity:
--> 'vim.ManagedEntity:datastore123.extensiondata.moref.value'
--> Arg spec:
--> (vim.alarm.AlarmSpec) {
--> dynamicType = <unset>,
--> name = "TestSpace",
--> systemName = <unset>,
--> description = "",
--> enabled = true,
--> expression = (vim.alarm.OrAlarmExpression) {
--> dynamicType = <unset>,
--> expression = (vim.alarm.AlarmExpression) [
--> (vim.alarm.MetricAlarmExpression) {
--> dynamicType = <unset>,
--> operator = "isAbove",
--> type = "vim.Datastore",
--> metric = (vim.PerformanceManager.MetricId) {
--> dynamicType = <unset>,
--> counterId = 240,
--> instance = "",
--> },
--> yellow = 8500,
--> yellowInterval = <unset>,
--> red = 9000,
--> redInterval = <unset>,
--> }
--> ],
--> },
--> action = (vim.alarm.GroupAlarmAction) {
--> dynamicType = <unset>,
--> action = (vim.alarm.AlarmAction) [
--> (vim.alarm.AlarmTriggeringAction) {
--> dynamicType = <unset>,
--> action = (vim.action.SendEmailAction) {
--> dynamicType = <unset>,
--> toList = "blah@blah.com;",
--> ccList = "",
--> subject = "",
--> body = "",
--> },
--> transitionSpecs = (vim.alarm.AlarmTriggeringAction.TransitionSpec) [
--> (vim.alarm.AlarmTriggeringAction.TransitionSpec) {
--> dynamicType = <unset>,
--> startState = "yellow",
--> finalState = "red",
--> repeats = true,
--> }
--> ],
--> green2yellow = false,
--> yellow2red = false,
--> red2yellow = false,
--> yellow2green = false,
--> }
--> ],
--> },
--> actionFrequency = 28800,
--> setting = (vim.alarm.AlarmSetting) {
--> dynamicType = <unset>,
--> toleranceRange = 0,
--> reportingFrequency = 0,
--> },
--> alarmMetadata = <unset>,
--> }
2013-03-13T08:06:12.454-05:00 [03756 info 'Defau