PHP's SNMP functions to retrieve data with OIDs defined in MIB not working

George Fendler
George Fendler used Ask the Experts™
on
I am trying to use SNMP to monitor some equipment made by Ubiquiti.
I have downloaded the MIB for their airmax radios.
I can run snmpget from a command line and retrieve the data correctly like this:

# snmpget -v1 -c xxxxxxxxxxxxxxxxxxxxxxx 192.168.130.144 UBNT-AirMAX-MIB::ubntRadioFreq.1 UBNT-AirMAX-MIB::ubntWlStatCcq.1
UBNT-MIB::ubntRadioFreq.1 = INTEGER: 5190
#

When I attempt to retrieve the same data withing PHP, no value is returned
excerpt:

<?php
$host="192.168.130.144";
$oid="UBNT-AirMAX-MIB::ubntRadioFreq.1";

$res=snmp2_get($host, $community, $oid);
if(is_array($res)) {
      print_r($res);
} else {
      echo("$res");
}
?>

There is no result returned. The Apache2 error log contains:
[Wed May 30 22:24:25.754967 2018] [:error] [pid 773] [client ::1:36908]
PHP Warning:  snmp2_get(): Invalid object identifier: UBNT-AirMAX-MIB::ubntRadioFreq.1 in /var/www/html/crmtest/snmptest.php on line 22

I can use snmp2_get() to retrieve the voltage of a battery bank at a solar site with the following:

<?php
$host = "192.168.130.140";
$oid="1.3.6.1.4.1.32050.2.1.27.5.12";

$res = snmp2_get("$host","$community","$oid") or die("didn't work");
if(is_array($res)) {
      echo"its an array ";
      print_r($res);
      echo"<BR>";
} else {
      echo "its not an array";
      echo("$res<br>");
}
?>
The result that I get from this is:
its not an array INTEGER: 2771

The difference is using a textual descriptor from the published MIB as opposed to the numeric OID.
The code will be much easier to maintain if I can use the MIB file.
UBNT-MIB.txt
UBNT-AirMAX-MIB.txt
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Developer & EE Moderator
Fellow 2018
Most Valuable Expert 2013
Commented:
I wonder if it would just be easier to run the command line from PHP.  http://php.net/manual/en/function.exec.php
George Fendlerprogrammer

Author

Commented:
Thanks, Scott,
The thought of doing that crossed my mind but I thought there may be security issues. However, I just tried it and it works. But, you can only do one MIB at a time.
It always worrys me when I don't understand why something doesn't work that should or something does that shouldn't.
I don't usually cut and paste someone elses code into my program unless I completely understand it. That way, I have a chance of modifying it to more closely meet my needs and understand it when it needs modification in the future.

SNMP doesn't seem to be something that has a lot of experts, at least with PHP.

Thanks for pushing me off of dead center.
George Fendlerprogrammer

Author

Commented:
In the interest of time I decided to use the exec() function. Its not exactly what I wanted to do, but it works. Maybe I can figure out why the snmpget() and snmp2_get() functions don't work with MIBs (only OIDs seem to work).
Scott FellDeveloper & EE Moderator
Fellow 2018
Most Valuable Expert 2013

Commented:
> It always worrys me when I don't understand why something doesn't work that should

Yes, same here.  

However, you have your code working in the command line and you know how that works. The exec is just running that code and if there is a security issue, I wouldn't know. But if the php code is not accepting any user input outside of your own, I think it will be fine and not any more or less secure than running it in the browser or in php.  

I briefly read up on what you were trying to do and it seemed like snmp2_get is not completely stable?

I run windows vbs scripts via php using exec and it works well.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial