VMware Cloud Community
qwert1235
Enthusiast
Enthusiast
Jump to solution

Create alarm "Lost Network Redundancy" by script?

Hello:

I am trying to write a script that will create alarm to monitor Lost Network Redundancy (I know manually it’s) on host.

I know how to create alarm for “basic” event see attachment (LucD – thank you for your help) and know that for “Lost Network Redundancy” alarm I have to use “vprob.net.redundancy.lost” instead of “EnteringMaintenanceModeEvent” (like in my example); but I still have trouble to combine it together…

Can someone please help me?

Thank you,

qwert

0 Kudos
1 Solution

Accepted Solutions
yboychev
Hot Shot
Hot Shot
Jump to solution

I don't have a particular fancy script with parameters and everything else but I can show you the steps needed to filter the alarms by name and delete the desired one.

#retrieve alarm manager

$alarmManager = Get-View -Id 'AlarmManager-AlarmManager'

#Get the entity which contains the alarm you want to delete. I'll do it for a Vm

$entityView = Get-Vm -Name MyVm | Get-View

#get all alarm MoRefs

$alarmMoRefList = $alarmManager.GetAlarm($entityView.MoRef)

#Retrieve tha alarm views from the MoRefs

$alarmViewList = $alarmMoRefList | foreach { Get-View $_}

#Now filter the alrams by name

$alarmToDelete = $alarmViewList | where {$_.Info.Name -eq "AlarmToDelete"}

#Finally delete the alarms

$alarmToDelete.RemoveAlarm()

It shouldn't be to hard to put it in a reussable function or a script. If you have troubles with that I'll be happy to help again!

View solution in original post

0 Kudos
6 Replies
qwert1235
Enthusiast
Enthusiast
Jump to solution

I create this alarm by running the folloing code:

$alarm.Name = "Lost Network Connectivity"

$alarm.Enabled = $TRUE

$expression = New-Object VMware.Vim.EventAlarmExpression

$expression.EventType = "vprob.net.connectivity.lost"

$expression.ObjectType = "HostSystem"

$expression.Status = "red"

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

$alarm.expression.expression += $expression

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

But when I am trying to check settings on this new created alarm I have error "Object reference not set to an instance of an object"... However, this alarm will come up just fine if I restart VC service...

Any thoughts?

thanks,

qwert

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The message seems to indicate that there is something wrong with the parameter $entity.MoRef.

Is the $entitycorrect ? What did you put in $entity ?


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

qwert1235
Enthusiast
Enthusiast
Jump to solution

Luc,

For $entity I have:

$entity = Get-View (Get-Folder -Name "Datacenters").ID

Anyway, it's working after VC service restart...

Thanks,

qwert

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That's strange.

If you create a new alarm, do you each time have to restart the VC service before the alarm becomes active ?


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

qwert1235
Enthusiast
Enthusiast
Jump to solution

Luc,

No, I need to restart only for those types of alarm, but not for any other...

Anyway, I can restart my VC service - it is not a big problem for me...

Another challange I have now is how to remove alarms by script. I need to remove alarm with specific name and RemoveAlarm does not work for me for some reason.

If you (someone else) know how to do it your help will be really appreciated.

Thank you,

qwert

0 Kudos
yboychev
Hot Shot
Hot Shot
Jump to solution

I don't have a particular fancy script with parameters and everything else but I can show you the steps needed to filter the alarms by name and delete the desired one.

#retrieve alarm manager

$alarmManager = Get-View -Id 'AlarmManager-AlarmManager'

#Get the entity which contains the alarm you want to delete. I'll do it for a Vm

$entityView = Get-Vm -Name MyVm | Get-View

#get all alarm MoRefs

$alarmMoRefList = $alarmManager.GetAlarm($entityView.MoRef)

#Retrieve tha alarm views from the MoRefs

$alarmViewList = $alarmMoRefList | foreach { Get-View $_}

#Now filter the alrams by name

$alarmToDelete = $alarmViewList | where {$_.Info.Name -eq "AlarmToDelete"}

#Finally delete the alarms

$alarmToDelete.RemoveAlarm()

It shouldn't be to hard to put it in a reussable function or a script. If you have troubles with that I'll be happy to help again!

0 Kudos