Skip to content
Barry O'Donovan edited this page Dec 9, 2013 · 4 revisions

The usage of OSS_SNMP is really easy (we hope!).

First you need to include the main project file:

require_once( $path_to_OSS_SNMP_root . "/OSS_SNMP/SNMP.php" );

This file defines the OSS_SNMP namespace and also registers a trivial auto loaded for the namespace.

Then for a given an SNMP v2 enabled hostname or IP address with community $community, we instantiate an SNMP object:

$snmpHost = new \OSS_SNMP\SNMP( $ip, $community );

You can perform standard SNMP queries on this if you know the OIDs (by the way, this is not the recommended usage!). For example, to get the system contact string:

echo $snmpHost->get( '.1.3.6.1.2.1.1.4.0' );

However, the real strength of OSS_SNMP comes into play when you use the existing or define your own MIBs. MIBs in this context are PHP classes defining OIDs and functions for these. For example, with the build in system MIB you can replicate the above with:

echo $snmpHost->useSystem()->contact();

See Using-MIBs for a discussion on the useXXX() function call above.

As another example, let's assume the above is a standard Cisco switch with defined VLANs such as:

vlan 2
    name mgmt
vlan 100
    name cust-widgets

Then I can get an associate array of VLAN names indexed by the VLAN ids:

print_r( $snmpHost->useCisco_VTP()->vlanNames() );

which yields:

Array
(
    [1] => default
    [2] => mgmt
    [100] => cust-widgets
    [1002] => fddi-default
    ...
)

Included Examples

In the examples/ directory there are a couple of files to demonstrate the library. The mst-port-roles.php script is demonstrated in this blog post.

The interfaces.php file will print interface information for a given host. The first mode of operation lists the available interfaces:

$ ./interfaces.php 127.0.0.1 public34

Number of interfaces on 127.0.0.1: 7

ID:  Name  - Descrition - Type - Admin/Operational State

1: lo - lo - softwareLoopback - up/up
2: eth0 - eth0 - ethernet-csmacd - up/up
3: wlan0 - wlan0 - ethernet-csmacd - up/down
4: virbr0 - virbr0 - ethernet-csmacd - up/down
7: vboxnet0 - vboxnet0 - ethernet-csmacd - down/down
1661: ppp0 - ppp0 - ppp - down/down
4337: tap15 - tap15 - ethernet-csmacd - up/up

Then, in its second mode, you can add the interface ID for detailed information:

$ ./interfaces.php 127.0.0.1 public34 2

Interface information for eth0 (eth0)
======================================

Alias:                     
Type:                      ethernet-csmacd

Admin / Operational State: up/up

MTU:                       1500
Speeds:                    1000000000
Physical Address:          F04DA2578FB7
Last Change:               33757704

In/Out Octets:             214675835 / 1005955749
In/Out Unicast:            342658 / 843409
In/Out Non Unicats:        655 / 0
In/Out Discards:           0 / 0
In/Out Errors:             0 / 0

In Unknown Protocols:      0
Out Queue Length:          0
Clone this wiki locally