Adding SNMP support to your PHP Development

Hello Developers and Server Admins

We would like to present a technical documentation walk-through on setting up SNMP support for PHP development.

Principle of SNMP Communication

Enjoy and read on.

For those who want to understand what SNMP is, check these links out. First, is the Wikipedia page and then followed by the YouTube video of SNMP concepts and principles. The original PHP manual for SNMP is a good read, as always.

We are using Debian Linux and SNMP – Debian Wiki is here

When it comes to setting up users, groups for SNMP v3, we recommend this YouTube video.

Scenario / Program Concept

Here in this walk-thru scenario, we want to configure SNMP on our development server, this is where we develop a PHP program and use it to communicate to a SNMP capable equipment, obtain and display some specific OIDs.

What you need:

  • Development Server for you to install SNMP onto
  • An SNMP enabled device on the same network (i.e. Switch, Router, UPS with network management card or anything supporting SNMP)
  • PHP development. The running PHP program will need to be on the same network
  • (Optional) We can also use a basic Network Management Tool (In this example, we use ManageEngine MibBrowser Free Tool) on separate Windows PC for testing and confirmation.

To-Do-List

  1. Backup your development server since you are installing new function and software to it.
  2. Obtain administrative credentials and access server
  3. apt-get update
  4. apt-get upgrade
  5. apt-get install snmp
  6. apt-get install php5-snmp

If you install php5-snmp but your system has no base SNMP library to beginning with, you will get a lot of error messages when you type php on command line.

webadmin@intranet:/var/www/$ php
MIB search path: /home/webadmin/.snmp/mibs:/usr/share/snmp/mibs:/usr/share/snmp/mibs/iana:/usr/share/snmp/mibs/ietf:/usr/share/mibs/site:/usr/share/snmp/mibs:/usr/share/mibs/iana:/usr/share/mibs/ietf:/usr/share/mibs/netsnmp
Cannot find module (SNMPv2-TC): At line 10 in /usr/share/snmp/mibs/UCD-DLMOD-MIB.txt
Cannot find module (SNMPv2-SMI): At line 34 in /usr/share/snmp/mibs/UCD-SNMP-MIB.txt
Cannot find module (SNMPv2-TC): At line 37 in /usr/share/snmp/mibs/UCD-SNMP-MIB.txt
Did not find ‘enterprises’ in module #-1 (/usr/share/snmp/mibs/UCD-SNMP-MIB.txt)
Did not find ‘DisplayString’ in module #-1 (/usr/share/snmp/mibs/UCD-SNMP-MIB.txt)
Did not find ‘TruthValue’ in module #-1 (/usr/share/snmp/mibs/UCD-SNMP-MIB.txt)
Unlinked OID in UCD-SNMP-MIB: ucdavis ::= { enterprises 2021 }
Undefined identifier: enterprises near line 39 of /usr/share/snmp/mibs/UCD-SNMP-MIB.txt…..

In this case, make sure you install SNMP by doing step #5 above. The overall SNMP installation for PHP development is pretty much done here.

SNMP Target device

In order to get everything working as planned, let’s go thru some setup.

  • Enable SNMP v3
  • Setup SNMP user and authentication parameters
  • Setting up SNMP access parameters
snmpv3_access
snmpv3_user
snmpv3

Before we get to PHP development, we will test out the SNMP communication between our target device (In our case, an UPS with network management card) and ManageEngine MibBrowser Free Tool from our Windows PC. We first download the APC UPS MIB database (powernet417.mib) from APC website.

MibBrowser

The above screenshot shows MibBrowser showing SNMP output.

We queried our UPS (ups.int.binarytaskforce.com), using OID

.iso.org.dod.internet.private.enterprises.apc.products.hardware.ups.upsIdent.upsBasicIdent.upsBasicIdentModel.

Which is equallivent to

.1.3.6.1.4.1.318.1.1.1.1.1.1

and the UPS replied

SMART-UPS 1400

Great, the SNMP connection and communication is a success!

PHP Development

The PHP development reference website shows all SNMP related programming functions and we will use some of them to demonstrate. DO NOT BOTHER using SNMP v1 or v2.

<?php
//==============================================================================
// BinaryTaskforce Inc.
// SNMP test (SNMP v3)
//==============================================================================

$ups = "ups.int.binarytaskforce.com";
$sec_name = "netadmin";
$sec_level = "authPriv";
$auth_protocol = "MD5";
$auth_passphrase = "sampleAuthpassphrase";
$priv_protocol = "DES";
$priv_passphrase = "samplePrivpassphrase";

$OID = array(
	".1.3.6.1.4.1.318.1.1.1.1.1.1.0",
	".1.3.6.1.4.1.318.1.1.1.2.1.1.0",
	".1.3.6.1.4.1.318.1.1.1.2.2.1.0",
	".1.3.6.1.4.1.318.1.1.1.2.2.2.0",
	".1.3.6.1.4.1.318.1.1.1.2.2.3.0",
	".1.3.6.1.4.1.318.1.1.1.4.2.3.0",
	);
	
for ($counter = 0; $counter <= sizeof($OID) - 1; $counter++)
 {
 $result[$counter] = snmp3_get($ups,$sec_name,$sec_level,$auth_protocol,$auth_passphrase,$priv_protocol,$priv_passphrase,$OID[$counter]); //SNMP V3
 }
 print_r ($result);
?>

Run the PHP program and we got…

Array
(
[0] => STRING: “SMART-UPS 1400”
[1] => INTEGER: 2
[2] => Gauge32: 100
[3] => Gauge32: 36
[4] => Timeticks: (396000) 1:06:00.00
[5] => Gauge32: 25
)

Just like that we are done.

Team BTF

Leave a Reply

Your email address will not be published. Required fields are marked *