Cradle  0.3.3
Simple library for creating Web-based applications
Snmp Class Reference

SNMP protocol wrapper. More...

Public Member Functions

 __construct ($config)
 
 __destruct ()
 
 open ($config=null)
 
 close ()
 
 getAgentVersion ()
 
 getAgentReadCommunity ()
 
 getAgentStatus ()
 
 getLastError ()
 
 set ($oid, $value)
 
 get ($oid)
 
 getParsed ($oid)
 
 getValue ($oid)
 

Static Public Member Functions

static parseValue ($value)
 
static parseTimeTicks ($stringValue, $inSeconds=false)
 
static parseTime ($seconds, $format=null)
 
static parseIpAddr ($oid)
 

Data Fields

const ERROR_UNKNOWN_VERSION = 1
 
const ERROR_UNKNOWN_OID = 2
 
const ERROR_UNKNOWN_TYPE = 3
 
const ERROR_CATNT_SET_OID = 4
 
 $host
 
 $readCommunity
 
 $writeCommunity
 
 $version = 0
 
 $timeout = 2
 
 $retries = 2
 
 $oidOutputFormat = SNMP_OID_OUTPUT_NUMERIC
 
 $version3
 

Protected Member Functions

 init ($config)
 
 setError ($code=null)
 
 getTypeCode ($type)
 
 snmpget ($oid, $version=null, $community=null)
 
 snmpset ($oid, $value, $type=null, $version=null, $community=null)
 

Protected Attributes

 $agentStatus
 
 $agentVersion
 
 $agentReadCommunity
 
 $errors
 
 $typeCodes
 

Detailed Description

SNMP protocol wrapper.

A simple class to send SNMP protocol requests.

Version
4.0
Author
Digger mrdig.nosp@m.ger@.nosp@m.sad-s.nosp@m.yste.nosp@m.ms.ru

Example of usage:

<?php
require_once __DIR__ . '/../../../../autoload.php';
//--- 1. Short usage:
print_r( (new Snmp(['host'=>'localhost', 'readCommunity'=>'public']))->get('.1.3.6.1.2.1.1.5.0') );
print_r( (new Snmp(['host'=>'localhost', 'readCommunity'=>'public']))->getParsed('.1.3.6.1.2.1.1.5.0') );
print_r( (new Snmp(['host'=>'localhost', 'readCommunity'=>'public']))->getValue('.1.3.6.1.2.1.1.5.0') );
//--- 2. Normal usage:
$snmp = new Snmp(['host'=>'localhost', 'readCommunity'=>'public']);
print_r( $snmp->get('.1.3.6.1.2.1.1.5.0') );
print_r( $snmp->getParsed('.1.3.6.1.2.1.1.5.0') );
print_r( $snmp->getValue('.1.3.6.1.2.1.1.5.0') );
//--- 3. Advanced usage (with several targets):
$snmp = new Snmp([
'readCommunity' => ['public', 'other', '...'],
'version' => [2,1],
'writeCommunity'=> 'forsave',
]);
//--- Get current hostname:
$snmp->open('first_host');
print_r( $snmp->get('.1.3.6.1.2.1.1.5.0') );
//--- Set new hostname:
print_r( $snmp->set('.1.3.6.1.2.1.1.5.0', 'New_hostname') );
print_r( $snmp->getLastError() );
//--- Connect to a new host:
print_r( $snmp->open('other_host')->get('.1.3.6.1.2.1.1.5.0') );
//--- Connect to an other host:
print_r( $snmp->open('other_host2')->get('.1.3.6.1.2.1.1.5.0') );
// ...

Constructor & Destructor Documentation

__construct (   $config)

Constructor

Parameters
array$configAn array of properties to initialize the class.
__destruct ( )

Destructor

Member Function Documentation

close ( )

Close current SNMP session

get (   $oid)

Get a raw value of SNMP object. This method is equal of snmpwalk.

Parameters
string$oidSNMP object id (OID).
Returns
array An array of values returned by snmpwall request. Array format:
[ oid-index => "TYPE: Some value", ]
getAgentReadCommunity ( )

Tries to define a real "read community" of SNMP agent.

List of possible "read community" values to detect should be set in property $readCommunity.

Returns
string A "read community" of SNMP agent
getAgentStatus ( )

Returns a current SNMP agent status - "alive" or "unreacheble".

Returns
boolean TRUE - agent is "alive" and ready to communicate.
FALSE - agent is "unreacheble".
NULL - is not defined yet.
getAgentVersion ( )

Tries to define a real version of SNMP agent.

List of possible version values should be set in property $version. By default (if property $version = 0) list of possible version values wille be set to [2,1] and this method will try to auto detect what version is correct for current SNMP agent.

Returns
int A real version of SNMP agent
getLastError ( )

Returns a last error

Returns
array An array of error details.
getParsed (   $oid)

Get a value of SNMP object and parse it. This method is equal of snmpwalk.

Parameters
string$oidSNMP object id (OID).
Returns
array An array of values returned by snmpwall request. Array format:
[ 0 => [ "some value", oid-index, "TYPE"], ... ]
getValue (   $oid)

Get a single value of SNMP object. This method is equal of snmpget.

Parameters
string$oidSNMP object id (OID).
Returns
string A single clean value (without data type prefix)
init (   $config)
protected

Init the class

open (   $config = null)

Open new SNMP session

Parameters
array | string$configAn array of properties to initialize the class. If $config is a string it will be interpreted as a host (same as $config = [ 'host' => $config ]).
Returns
this A link to an instance of this class.
static parseIpAddr (   $oid)
static

Extract an IP address from SNMP OID.

Parameters
string$oidSNMP Object ID (OID) ends with IP address value.
Returns
string Extracted IP Address
static parseTime (   $seconds,
  $format = null 
)
static

Converts seconds to output format.

Parameters
int$secondsInput seconds.
string$formatA string contains a keys for replacement:
[D] - days
[H] - hours
[M] - minutes
[S] - seconds
Returns
array | string If $format is empty or NULL, returned value is an array: [$days, $hours, $minutes, $seconds]
static parseTimeTicks (   $stringValue,
  $inSeconds = false 
)
static

Parse a raw value of SNMP object contains a TimeTicks

Parameters
string$stringValuevalue of SNMP object of format: "(3454566544) ..."
boolean$inSeconds=TRUE if you need to convert the result in seconds.
Returns
string TimeTicks value in milliseconds, or in seconds
static parseValue (   $value)
static

Parse a raw value of SNMP object

Parameters
string$valuevalue of SNMP object of format: "TYPE: Some value"
Returns
array An array: [$value, $type]
set (   $oid,
  $value 
)

Set a value for SNMP object

Parameters
string$oidSNMP object id (OID).
mixed$valueValue to set. (Type of value will get from SNMP agent).
Returns
boolean TRUE on success. FALSE if error has occurred.
setError (   $code = null)
protected

Set an error by error code.

Parameters
int$codeError code

Field Documentation

$host

string Target hostname or IP address

$oidOutputFormat = SNMP_OID_OUTPUT_NUMERIC

int the OID output format Acceptable values: SNMP_OID_OUTPUT_NUMERIC | SNMP_OID_OUTPUT_FULL

See also
[http://php.net/manual/ru/function.snmp-set-oid-output-format.php]
$readCommunity

string SNMP read community. You may to set an array of possible values, for auto detect a correct one.

$retries = 2

int The number of times to retry if timeouts occur.

$timeout = 2

int The number of seconds until the first timeout.

$typeCodes
protected
Initial value:
=[
'INTEGER' => 'i'

array SNMP object value types

$version = 0

array An array of possible SNMP agent versions to use. =0 - auto detect the correct SNMP agent version.

$version3
Initial value:
= [
'secName' => ''

SNMP v3 params

array

$writeCommunity

string SNMP write community.


The documentation for this class was generated from the following file: