A class for performing SNMP V2 queries and processing the results.

copyright Copyright (c) 2012, Open Source Solutions Limited, Dublin, Ireland
author Barry O'Donovan

 Methods

Magic method for generic function calls

__call(string $method, array $args) 

Parameters

$method

string

$args

array

Exceptions

\Exception

The constructor.

__construct(string $host, string $community) : \OSS_SNMP

Parameters

$host

string

The target host for SNMP queries.

$community

string

The community to use for SNMP queries.

Returns

\OSS_SNMPAn instance of $this (for fluent interfaces)

Query whether we are using the cache or not

cache() : boolean

Returns

booleanTrue of the local lookup cache is enabled. Otherwise false.

Disable lookups of the local cache

disableCache() : \SNMP

Returns

\SNMPAn instance of this for fluent interfaces

Enable lookups of the local cache

enableCache() : \SNMP

Returns

\SNMPAn instance of this for fluent interfaces

Get a single SNMP value

get(string $oid) : mixed

Parameters

$oid

string

The OID to get

Exceptions

\OSS_SNMP\Exception On *any* SNMP error, warnings are supressed and a generic exception is thrown

Returns

mixedThe resultant value

Get the cache in use (or create a Cache\Basic instance

getCache() : \OSS_SNMP\Cache

We kind of mandate the use of a cache as the code is written with a cache in mind. You are free to disable it via disableCache() but your machines may be hammered!

We would suggest disableCache() / enableCache() used in pairs only when really needed.

Returns

\OSS_SNMP\CacheThe cache object

Returns the community string currently in use.

getCommunity() : string

Returns

stringThe community string currently in use.

Returns the target host as currently configured for SNMP queries

getHost() : string

Returns

stringThe target host as currently configured for SNMP queries

Returns the unaltered original last SNMP result

getLastResult() : mixed

Returns

mixedThe unaltered original last SNMP result

Returns the internal result cache

getResultCache() : array

Returns

arrayThe internal result cache

Returns the SNMP query retry count

getRetry() : string

Returns

stringThe SNMP query retry count

Returns the SNMP query timeout (microseconds).

getTimeout() : int

Returns

intThe the SNMP query timeout (microseconds)

Parse the result of an SNMP query into a PHP type

parseSnmpValue(string $v) : mixed

For example, [STRING: "blah"] is parsed to a PHP string containing: blah

Parameters

$v

string

The value to parse

Exceptions

\Exception

Returns

mixedThe parsed value

Utility function to convert TruthValue SNMP responses to true / false

ppTruthValue(integer $value) : boolean

Parameters

$value

integer

The TruthValue ( 1 => true, 2 => false) to convert

Returns

boolean

Proxy to the snmp2_real_walk command

realWalk(string $oid) : array

Parameters

$oid

string

The OID to walk

Returns

arrayThe results of the walk

Set the cache to use

setCache(\OSS_SNMP\Cache $c) : \OSS_SNMP\SNMP

Parameters

$c

\OSS_SNMP\Cache

The cache to use

Returns

\OSS_SNMP\SNMPFor fluent interfaces

Sets the community string to use for SNMP queries.

setCommunity(string $c) : \OSS_SNMP

Parameters

$c

string

The community to use for SNMP queries.

Returns

\OSS_SNMPAn instance of $this (for fluent interfaces)

Sets the target host for SNMP queries.

setHost(string $h) : \OSS_SNMP\SNMP

Parameters

$h

string

The target host for SNMP queries.

Returns

\OSS_SNMP\SNMPAn instance of $this (for fluent interfaces)

Sets the output format for SNMP queries.

setOidOutputFormat(int $f) : \OSS_SNMP\SNMP

Should be one of the class OID_OUTPUT_* constants

Parameters

$f

int

The fomat to use

Returns

\OSS_SNMP\SNMPAn instance of $this (for fluent interfaces)

Sets the SNMP query retry count.

setRetry(int $r) : \OSS_SNMP

Parameters

$r

int

The SNMP query retry count

Returns

\OSS_SNMPAn instance of $this (for fluent interfaces)

Sets the timeout to use for SNMP queries (microseconds).

setTimeout(int $t) : \OSS_SNMP

Parameters

$t

int

The timeout to use for SNMP queries (microseconds).

Returns

\OSS_SNMPAn instance of $this (for fluent interfaces)

Get indexed SNMP values where the array key is the given position of the OID

subOidWalk(string $oid, int $position) : array

I.e. the following query with sample results:

subOidWalk( '.1.3.6.1.4.1.9.9.23.1.2.1.1.9', 15 )

  .1.3.6.1.4.1.9.9.23.1.2.1.1.9.10101.5 = Hex-STRING: 00 00 00 01
  .1.3.6.1.4.1.9.9.23.1.2.1.1.9.10105.2 = Hex-STRING: 00 00 00 01
  .1.3.6.1.4.1.9.9.23.1.2.1.1.9.10108.4 = Hex-STRING: 00 00 00 01

would yield an array:

 10101 => Hex-STRING: 00 00 00 01
 10105 => Hex-STRING: 00 00 00 01
 10108 => Hex-STRING: 00 00 00 01

Parameters

$oid

string

The OID to walk

$position

int

The position of the OID to use as the key

Exceptions

\OSS_SNMP\Exception On *any* SNMP error, warnings are supressed and a generic exception is thrown

Returns

arrayThe resultant values

Utility function to translate one value(s) to another via an associated array

translate(mixed $values, array $translator) : mixed

I.e. all elements '$value' will be replaced with $translator( $value ) where $translator is an associated array.

Huh? Just read the code below!

Parameters

$values

mixed

A scalar or array or values to translate

$translator

array

An associated array to use to translate the values

Returns

mixedThe translated scalar or array

This is the MIB Extension magic

useExtension(string $mib, array $args) : \OSS_SNMP\MIBS

Calling $this->useXXX_YYY_ZZZ()->fn() will instantiate an extension MIB class is the given name and this $this SNMP instance and then call fn().

See the examples for more information.

Parameters

$mib

string

The extension class to use

$args

array

Returns

\OSS_SNMP\MIBS

Get indexed SNMP values (first degree)

walk1d(string $oid) : array

Walks the SNMP tree returning an array of key => value pairs.

This is a first degree walk and it will throw an exception if there is more that one degree of values.

I.e. the following query with sample results:

walk1d( '.1.0.8802.1.1.2.1.3.7.1.4' )

  .1.0.8802.1.1.2.1.3.7.1.4.1 = STRING: "GigabitEthernet1/0/1"
  .1.0.8802.1.1.2.1.3.7.1.4.2 = STRING: "GigabitEthernet1/0/2"
  .1.0.8802.1.1.2.1.3.7.1.4.3 = STRING: "GigabitEthernet1/0/3"
  .....

would yield an array:

 1 => GigabitEthernet1/0/1
 2 => GigabitEthernet1/0/2
 3 => GigabitEthernet1/0/3

Parameters

$oid

string

The OID to walk

Exceptions

\OSS_SNMPException On *any* SNMP error, warnings are supressed and a generic exception is thrown

Returns

arrayThe resultant values

Get indexed SNMP values where they are indexed by IPv4 addresses

walkIPv4(string $oid) : array

I.e. the following query with sample results:

subOidWalk( '.1.3.6.1.2.1.15.3.1.1. )

  .1.3.6.1.2.1.15.3.1.1.10.20.30.4 = IpAddress: 192.168.10.10
  ...

would yield an array:

 [10.20.30.4] => "192.168.10.10"
 ....

Parameters

$oid

string

The OID to walk

Exceptions

\OSS_SNMP\Exception On *any* SNMP error, warnings are supressed and a generic exception is thrown

Returns

arrayThe resultant values

 Properties

 

$SNMP_TRUTHVALUES

$SNMP_TRUTHVALUES 

 

$_cache

$_cache 

 

$_community

$_community 

 

$_disableCache

$_disableCache 

 

$_host

$_host 

 

$_lastResult

$_lastResult 

 

$_retry

$_retry 

 

$_timeout

$_timeout 

 Constants

 

OID_OUTPUT_FULL

OID_OUTPUT_FULL 

 

OID_OUTPUT_NUMERIC

OID_OUTPUT_NUMERIC 

 

SNMP_TRUTHVALUE_FALSE

SNMP_TRUTHVALUE_FALSE 

 

SNMP_TRUTHVALUE_TRUE

SNMP_TRUTHVALUE_TRUE