lamw
Community Manager
Community Manager

Undef error when trying to access SNMP MOR

I got the following error:

Can't call method "type" on an undefined value at /usr/lib/perl5/5.8.8/VMware/VICommon.pm line 1026.

End Disconnect

regarding the following snippet of code:

foreach my $cluster (@$cluster_views) {
	my $hosts = Vim::get_views (mo_ref_array => $cluster->host);
    if(@$hosts) {
		printHostHardwareInfo($hosts);
    }
}

sub printHostHardwareInfo {
	my ($local_hosts) = @_
	foreach my $host (@$local_hosts) {
		anotherFunction($host)
	}
}

sub anotherFunction {
	my ($host) = @_
	my $snmp_system = Vim::get_view (mo_ref => $host->configManager->snmpSystem); 
	print $snmp_system->configuration->enabled,"\n";
}

The error is specific to the following line:

my $snmp_system = Vim::get_view (mo_ref => $host->configManager->snmpSystem); 

I'm not sure why it's complaining about not having a defined type, but I'm able to execute that line if I try to get the view of the snmpSystem in the printHostHardwareInfo function, but when I try to create a sub function and passing the hostSystem reference, I'm unable to access the snmp object.

=========================================================================

--William

VMware ESX/ESXi scripts and resources at:

0 Kudos
8 Replies
njain
Expert
Expert

The code snippet posted seems to be correct. The error will only be thrown if the "configManager.snmpSystem" property is not set for that particular host. Are you sure you are able to get the view of "snmpSystem" in printHostHardwareInfo function and at the same time accessing the "configManager.snmpSystem" property by passing the host view to anotherFunction throws error?

You can also double check by accessing the MOB of your VirtualCenter, drilling down to the host for which the error occurs and validate if the configManager.snmpSystem is set.

Can you also post the complete output after adding the following print statement in both the functions:

sub printHostHardwareInfo {

my ($local_hosts) = @_;

foreach my $host (@$local_hosts) {

print "snmpSystem 1: " . $host->configManager->snmpSystem . "::\n";

anotherFunction($host);

}

}

sub anotherFunction {

my ($host) = @_;

print "snmpSystem 2: " . $host->configManager->snmpSystem . "::\n";

my $snmp_system = Vim::get_view (mo_ref => $host->configManager->snmpSystem);

print $snmp_system->configuration->enabled,"\n";

}

lamw
Community Manager
Community Manager

njain,

You're right, we recently added a new ESXi host and the script was going through the cluster and tried to access the new host vs. the old one which caused the undef error. I was wondering, how might I be able to check to see if the hostSystem has configManager.snmpSystem set prior to trying to get the view? I hit the error when it tries to do a get_view before I can even check if the property has been set or not?

my $snmp_system = Vim::get_view (mo_ref => $local_host->configManager->snmpSystem);
if($snmp_system) {
                        print "Before Function Call - snmpSystem 1: " . $snmp_system . "::\n";
}

The if statement is never reached because the script already errors out on trying to access an object that has not been set.

=========================================================================

--William

VMware ESX/ESXi scripts and resources at:

0 Kudos
stumpr
Virtuoso
Virtuoso

You can probably just wrap it in an eval block as a quick fix, handle the error after the call.

However, the VI SDK's error handling is pretty weak in general (not very robust). With perl you have to do a lot of value checking since a lot of objects will come back without a placeholder for certain properties (undefined). I just took to creating some standard functions to use when calling property values to deal with potential undefined values.

Reuben Stump | http://www.virtuin.com | @ReubenStump
lamw
Community Manager
Community Manager

Thanks, using the eval I was able to handle the exception and just continue processing.

Though back to my original question, I have a host that has SNMP configurations but the snippet of code continues to skip through as if it's unable to access the snmpSystem MOR:

In the 2nd function call I do the following:

my $snmp_system;
eval { $snmp_system = Vim::get_view (mo_ref => $host->configManager->snmpSystem); };
print "Code: ",$@,"\n";
if(!$@) {
      print $snmp_system->configuration->enabled,"\n";
 }

Though the code never goes inside of the if statement, but I'm able to reference this information in the printHardware... function

=========================================================================

--William

VMware ESX/ESXi scripts and resources at:

0 Kudos
stumpr
Virtuoso
Virtuoso

If the code is never going inside the IF statement, then an exception was raised based on your code. On my ESX 3.5 u3 test system I'm not getting any exceptions raised, so possible there is some bug with ESXi.

Reuben Stump | http://www.virtuin.com | @ReubenStump
0 Kudos
lamw
Community Manager
Community Manager

Actually the statement is not executing from my ESX 3.5u3 host which does have SNMP configured. Outside of the first function call, I'm able to access information such as the SNMP port/etc but once inside of the 2nd function, the if statement is never executed. I'll provide a snippet of output later if I get a chance.

=========================================================================

--William

VMware ESX/ESXi scripts and resources at:

0 Kudos
stumpr
Virtuoso
Virtuoso

Post up a working snippet (or not working as the case may be). Might be a mishandled reference or something.

Reuben Stump | http://www.virtuin.com | @ReubenStump
0 Kudos
BryanStansel
Contributor
Contributor

I am having a very similar issue using the VI Remote CLI tools from multiple machines, and operating systems , e.g. Windows and the Linux RCLI Appliance when accessing a VMware ESX Server 3i, 3.5.0, 123629 and Virtual Center 2.50. infrastructure.

The output from Windows:

C:\Program Files\VMware\VMware VI Remote CLI\bin>vmkfstools.pl --config .\config.cfg --vihost edclvmhp051.xxxxxxxx.com --clonevirtualdisk /vmfs/volumes/4a09d9ba-c1022122-d9ce-001f295d144c/VPCB990003/VPCB990003.vmx /vmfs/volumes/4a09d9ba-c1022122-d9ce-001f295d144c/VPCB990003/VPCB990003_t.vmx --diskformat thin --adapterType lsilogic

Can't call method "type" on an undefined value at C:/Program Files/VMware/VMware

VI Remote CLI/Perl/lib/VMware/VICommon.pm line 1026.

End Disconnect

I would like to be able to leverage the CLI toolset and am not able to do so currently..

Please advise.

0 Kudos