VMware {code} Community
Aristizabal
Enthusiast
Enthusiast
Jump to solution

RemoveAlarm problem on perl Toolkit 1.6

Hi.

I am using the the perl Toolkit 1.6 to create triggered alarms on VMs running on ESX 3.5.

I can create alarms with no problem with the following code:

my $alarmScript = 'C:\script.bat';

my $alarmMgr = Vim::get_service_content()->alarmManager;

my $alarm = Vim::get_view(mo_ref => $alarmMgr);

my $alarmExpr = EventAlarmExpression->new (eventType=>'VmPoweredOnEvent');

my $alarmAction = RunScriptAction->new(

script => $alarmScript

);

my $alarmTrigger = AlarmTriggeringAction->new(

action => $alarmAction,

red2yellow => 1,

green2yellow => 1,

yellow2red => 1,

yellow2green => 1

);

my $alarmSpec = AlarmSpec->new(

name => 'VM Started',

description => 'Trigger to run script when VM is started' ,

enabled => 1,

expression => $alarmExpr,

action => $alarmTrigger,

);

my $newAlarm = $alarm->CreateAlarm(entity => $vm, spec=>$alarmSpec);

Then I need to remove this alarm, but I understand that fist I must get the alarm and the delete it.

I execute the following code:

my $vm = Vim::find_entity_view(view_type => 'VirtualMachine',

filter => {name => $vmname});

my $alarmMgr = Vim::get_service_content()->alarmManager;

my $alarm = Vim::get_view(mo_ref => $alarmMgr);

my $existingAlarm = $alarm->GetAlarm(entity => $vm);

$existingAlarm->RemoveAlarm();

But I get the following error:

Can't call method "RemoveAlarm" on unblessed reference

I think I am having problems getting the alarm with "GetAlarm" because I cannot print any alarm properties after calling this method.

Any help will be appreciated.

Regards,

Juan Aristizabal.

Reply
0 Kudos
1 Solution

Accepted Solutions
njain
Expert
Expert
Jump to solution

Hi Juan,

There are couple of things that need to be corrected in the above code.

1. "GetAlarm" method returns an array of references to the Alarm objects whereas "RemoveAlarm" method takes only single instance of the reference to the Alarm.

2. As pointed by stumpr, you need to get the view of the returned alarm to be able to invoke RemoveAlarm method.

Below is the modified code snippet. You can replace the "myAlarm" to match the your alarm name.

>my $existingAlarm = $alarm->GetAlarm(entity => $vm);

>

>foreach (@$existingAlarm) {

> $vm_alarm = Vim::get_view(mo_ref => $_);

> if ($vm_alarm->info->name eq "myAlarm") {

> $vm_alarm->RemoveAlarm();

> }

>}

Hope this is helpful.

View solution in original post

Reply
0 Kudos
3 Replies
stumpr
Virtuoso
Virtuoso
Jump to solution

Without testing it, my first thought is you'll need to get do another get_view() call on your returned alarm from the GetAlarm() call. It's a ManagedObjectReference. Then you should be able to RemoveAlarm().

Reuben Stump | http://www.virtuin.com | @ReubenStump
njain
Expert
Expert
Jump to solution

Hi Juan,

There are couple of things that need to be corrected in the above code.

1. "GetAlarm" method returns an array of references to the Alarm objects whereas "RemoveAlarm" method takes only single instance of the reference to the Alarm.

2. As pointed by stumpr, you need to get the view of the returned alarm to be able to invoke RemoveAlarm method.

Below is the modified code snippet. You can replace the "myAlarm" to match the your alarm name.

>my $existingAlarm = $alarm->GetAlarm(entity => $vm);

>

>foreach (@$existingAlarm) {

> $vm_alarm = Vim::get_view(mo_ref => $_);

> if ($vm_alarm->info->name eq "myAlarm") {

> $vm_alarm->RemoveAlarm();

> }

>}

Hope this is helpful.

Reply
0 Kudos
Aristizabal
Enthusiast
Enthusiast
Jump to solution

Perfect, I just added this code to my script and everything works now.

Thank you very much.

Reply
0 Kudos