Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

Commit

Permalink
- prepare release 0.5: tag, finish tree/array stuff, update settings mib
Browse files Browse the repository at this point in the history
  • Loading branch information
gggeek committed Feb 16, 2010
1 parent 6585d27 commit f20fccd
Show file tree
Hide file tree
Showing 10 changed files with 501 additions and 436 deletions.
88 changes: 60 additions & 28 deletions classes/ezmibtree.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* The class used to work on the MIB represented as an array
* The class used to work on the MIB represented as a tree (php nested array)
*
* @author G. Giunta
* @version $Id: ezsnmpd.php 83 2010-01-25 13:27:09Z gg $
Expand All @@ -10,18 +10,17 @@

/**
* Each OID in the MIB tree should be an array with the following members:
* name => '',
* syntax => ,
* access => ,
* status => eZMIBTree::status_mandatory,
* description => '',
* reference => '', //optional
* children => array()
*
* tree node (OBJECT IDENTIFIER): name => '', children => array() [, nochildreninmib = true]
* tree leaf (OBJECT TYPE): name => '', syntax => '', access => '', status => '', description => ''
* tree leaf (SEQUENCE): name => '', syntax => 'SEQUENCE', items => array()
*/

class eZMIBTree {

// enum: status
const status_current = 'current';

const status_mandatory = 'mandatory';
const status_optional = 'optional';
const status_obsolete = 'obsolete';
Expand All @@ -38,7 +37,7 @@ class eZMIBTree {
/**
* Creates an OID struct with all the fields at deafult value
*/
static function OIDinstance( $name, $type, $description='', $access=eZMIBTree::access_read_only, $status=eZMIBTree::status_mandatory, $reference='' )
/*static function OIDinstance( $name, $type, $description='', $access=eZMIBTree::access_read_only, $status=eZMIBTree::status_mandatory, $reference='' )
{
return array(
'name' => $name,
Expand All @@ -49,15 +48,17 @@ static function OIDinstance( $name, $type, $description='', $access=eZMIBTree::a
'reference' => '',
'children' => array()
);
}
}*/

/**
* Walk the tree starting with $oid, and call back $funcname on every node
*
* @param oid struct $oid
* @param callable $funcname callback function. Will receive an OID struct and an identifier string
* @param string $prefix the OID prefix that will be used to generated the identifier passed to the callback
* @param bool $leaves_only if true, only terminal nodes in the tree (leaves) will be passed to the callback, otherwise all nodes
* @param bool $leaves_only if true, only terminal nodes in the tree (leaves) will be passed to the callback, otherwise all nodes (including sequences)
* @return null
* @todo add support for tables...
*/
static function walk( $oid, $funcname, $prefix='', $leaves_only=true )
{
Expand All @@ -84,7 +85,9 @@ static function toMIB( $oid, $prefix='' )
return self::$_mib;
}

// return the leaves in the tree, flattened to an array indexed by oid
/**
* return the leaves in the tree, flattened to an array indexed by oid
*/
static function toArray( $oid, $prefix='' )
{
self::$_mib = array();
Expand All @@ -94,35 +97,64 @@ static function toArray( $oid, $prefix='' )

/**
* Converts to ASN.1 format the children of the given oid
*
*
* @todo add optional 'reference' part
* @todo escape double quotes and possibly other stuff in description
*/
private static function OIDtoMIB( $oid, $prefix='' )
{
$oidname = eZSNMPd::asncleanup( $oid['name'] );
foreach( $oid['children'] as $i => $child )
if ( isset( $oid['children'] ) && !isset( $oid['nochildreninmib'] ) )
{
$childname = eZSNMPd::asncleanup( $child['name'] );
if ( count( $child['children'] ) )
{
self::$_mib .= str_pad( $childname, 15 ) . " OBJECT IDENTIFIER ::= { $oidname $i }\n\n";
}
else

$oidname = eZSNMPd::asncleanup( $oid['name'] );
foreach( $oid['children'] as $i => $child )
{
$child = array_merge( array( 'access' => eZMIBTree::access_read_only, 'status' => eZMIBTree::status_mandatory, 'description' => '' ), $child );
self::$_mib .= str_pad( $childname, 15 ) . " OBJECT-TYPE\n" .
" SYNTAX {$child['syntax']}\n" .
" MAX-ACCESS {$child['access']}\n" .
" STATUS {$child['status']}\n" .
" DESCRIPTION \"{$child['description']}\"\n" .
" ::= { $oidname $i }\n\n";
$childname = eZSNMPd::asncleanup( $child['name'] );
if ( !isset( $child['syntax'] ) || $child['syntax'] == '' )
{
self::$_mib .= str_pad( $childname, 15 ) . " OBJECT IDENTIFIER ::= { $oidname $i }\n\n";
}
else if ( $child['syntax'] == 'SEQUENCE' )
{
self::$_mib .= str_pad( ucfirst( $childname ), 15 ) . " ::=\n SEQUENCE\n {\n";
foreach ( $child['items'] as $i => $item )
{
/// @todo remove trailing comma form last iteration
self::$_mib .= " {$item['name']}\n {$item['syntax']}";
if ( $i < count( $child['items'] ) )
{
self::$_mib .= ",\n";
}
}
self::$_mib .= "\n }\n\n";
}
else
{
$child = array_merge( array( 'access' => eZMIBTree::access_read_only, 'status' => eZMIBTree::status_current, 'description' => '' ), $child );
self::$_mib .= str_pad( $childname, 15 ) . " OBJECT-TYPE\n" .
" SYNTAX {$child['syntax']}\n" .
" MAX-ACCESS {$child['access']}\n" .
" STATUS {$child['status']}\n" .
" DESCRIPTION\n \"{$child['description']}\"\n";
if ( isset( $child['index'] ) && $child['index'] != '' )
{
self::$_mib .= " INDEX { {$child['index']} }\n";
}
self::$_mib .= " ::= { $oidname $i }\n\n";
}
}

}
}

/**
* The simple tree walking got dirtier for supporting tables:
* in 'leaves only' mode, we skip SEQUENCE nodes, that are leaves
*/
private static function OIDtoArray( $oid, $prefix='' )
{
if ( !isset( $oid['children'] ) || !count( $oid['children'] ) )
if ( !isset( $oid['syntax'] ) || $oid['syntax'] != 'SEQUENCE' )
{
self::$_mib[$prefix] = $oid;
}
Expand Down
66 changes: 37 additions & 29 deletions classes/ezsnmpd.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class eZSNMPd {
const TYPE_UNSIGNED32 = 'unsigned';
const TYPE_UNSIGNED64 = 'unsigned';

const VERSION = '0.5-dev';
const VERSION = '0.5';

static $daemon_mode = false;
protected static $daemon_mode = false;

/*
Expand Down Expand Up @@ -236,7 +236,32 @@ public function set( $oid, $value, $type )
}

/**
* @param string $next root oid for the walk, leave empty for walk of full eZP tree
* @return array an array of 1-liner strings, in snmpwalk format
*/
public function walk( $oid = '' )
{
$result = array();
while( ( $response = $this->getnext( $oid ) ) !== null )
{
$parts = explode( "\n", $response );
$parts[1] = strtoupper( $parts[1] );
/// @todo more decoding of snmpd.conf formats to snmpwalk ones?
if ( $parts[1] == 'STRING' )
{
/// @todo verify if we nedd substitution for " and other chars (which one?)
$parts[2] = '"' . $parts[2] . '"';
}
$result[] = ".{$parts[0]} = {$parts[1]}: {$parts[2]}";
$oid = $parts[0];
}
return $result;
}

/**
* Return the ASN.1 representation of the eZ Publish MIB
* @return string
* @todo rename this method to getMIB, for consistency (!important)
*/
public function getFullMIB()
{
Expand All @@ -245,6 +270,7 @@ public function getFullMIB()
}

/**
* Return the eZ Publish MIB as a nested php array
* @return array
*/
public function getMIBTree()
Expand All @@ -253,6 +279,7 @@ public function getMIBTree()
}

/**
* Return the eZ Publish MIB as a flattened php array (only tree leaves left)
* @return array
*/
public function getMIBArray()
Expand All @@ -261,29 +288,7 @@ public function getMIBArray()
}

/**
* @param string $next root oid for the walk, leave empty for walk of full eZP tree
* @return array an array of 1-liner strings, in snmpwalk format
*/
public function walk( $oid = '' )
{
$result = array();
while( ( $response = $this->getnext( $oid ) ) !== null )
{
$parts = explode( "\n", $response );
$parts[1] = strtoupper( $parts[1] );
/// @todo more decoding of snmpd.conf formats to snmpwalk ones?
if ( $parts[1] == 'STRING' )
{
/// @todo verify if we nedd substitution for " and other chars (which one?)
$parts[2] = '"' . $parts[2] . '"';
}
$result[] = ".{$parts[0]} = {$parts[1]}: {$parts[2]}";
$oid = $parts[0];
}
return $result;
}

/**
* Return the ASN.1 header part for the eZ Publish MIB
* @param string $uniqid md5 or other identifier used to tell apart versions
* of the mib that differ (typically because of the
* variable handler part)
Expand All @@ -298,7 +303,9 @@ protected function getRootMIB( $uniqid )
}

/**
* @return string
* Return the MIB of all registered handlers, either in ASN.1 string format or
* as php array
* @return string | array
*/
protected function getHandlerMIBs( $return_objects = false )
{
Expand Down Expand Up @@ -359,15 +366,16 @@ protected function getHandlerMIBs( $return_objects = false )
}

/**
* Remove the prefix part from a full oid (the full oid must end with a dot)
* Remove the prefix part from a full oid string (the full oid must end with a dot).
* The prefix is set in snmpd.ini.
*/
protected function removePrefix( $oid )
{
return preg_replace( $this->prefixregexp, '', $oid );
}

/**
* Remove the suffix part from a full oid.
* Remove the suffix part from a full oid string.
* This is used to simplify writing handlers - the .0 used for scalar values is removed
*/
public static function removeSuffix( $oid )
Expand All @@ -376,7 +384,7 @@ public static function removeSuffix( $oid )
}

/**
* Given an oid, return the corresponding handler class, or null
* Given an oid string, return the corresponding handler class, or null
*/
protected function getHandler( $oid )
{
Expand Down
24 changes: 15 additions & 9 deletions classes/ezsnmpdhandler.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php
/**
* Abstract SNMP Handler class
* Implements basic genext logic for all handlers with a fixed list of oids
* Abstract SNMP Handler class.
*
* Implements basic oid tree => oid array conversion logic and array-based getnext
* logic for all handlers with a fixed list of oids
*
* @author G. Giunta
* @version $Id$
* @copyright (C) G. Giunta 2009
* @copyright (C) G. Giunta 2009, 2010
* @license code licensed under the GPL License: see README
*
* @todo add support for columnar objects
*/

abstract class eZsnmpdHandler implements eZsnmpdHandlerInterface {
Expand All @@ -17,7 +17,8 @@ abstract class eZsnmpdHandler implements eZsnmpdHandlerInterface {

/**
* Must return an array of all the OIDs handled, properly sorted.
* If you implement the getMIBTree function, ne need to reimplement this one.
* Called internally by getnext.
* If you implement the getMIBTree function, no need to reimplement this one.
* Use usort( $array, 'version_compare' ) for sorting the array.
* NB: for scalar objects, the trailing .0 is to be omitted
* Trailing wildcards are NOT accepted: this class must return a finite list of oids.
Expand All @@ -35,11 +36,15 @@ public function oidList()
}

/**
* Used by oidList to build the array of oids out of the tree
* Used by oidList to build the array of oids out of the tree.
* Needs to be public because it is invoked by eZMIBTree tree traversal
*/
public function oidListBuilder( $oid, $prefix )
{
$this->_oidlist[] = preg_replace( '/^\./', '', $prefix );
if ( !isset( $oid['syntax'] ) || $oid['syntax'] != 'SEQUENCE' )
{
$this->_oidlist[] = preg_replace( '/^\./', '', $prefix );
}
}

/**
Expand All @@ -52,6 +57,7 @@ public function oidListBuilder( $oid, $prefix )
public function getnext( $oid )
{
$array = $this->oidList();

// if it is not a scalar value, look first to see if the object exists
// if it does, return its scalar value
if ( !preg_match( '/\.0$/', $oid ) )
Expand Down Expand Up @@ -103,7 +109,7 @@ public function getnext( $oid )
}

/**
* Override this in subclasses for write support.
* Override this in subclasses to add write support.
*/
public function set( $oid, $value, $type )
{
Expand Down
Loading

0 comments on commit f20fccd

Please sign in to comment.