Entity.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /*
  3. Copyright (c) 2012, Open Source Solutions Limited, Dublin, Ireland
  4. All rights reserved.
  5. Contact: Barry O'Donovan - barry (at) opensolutions (dot) ie
  6. http://www.opensolutions.ie/
  7. This file is part of the OSS_SNMP package.
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions are met:
  10. * Redistributions of source code must retain the above copyright
  11. notice, this list of conditions and the following disclaimer.
  12. * Redistributions in binary form must reproduce the above copyright
  13. notice, this list of conditions and the following disclaimer in the
  14. documentation and/or other materials provided with the distribution.
  15. * Neither the name of Open Source Solutions Limited nor the
  16. names of its contributors may be used to endorse or promote products
  17. derived from this software without specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  22. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. namespace OSS\SNMP\MIBS;
  30. /**
  31. * A class for performing SNMP V2 queries on generic devices
  32. *
  33. * @copyright Copyright (c) 2012, Open Source Solutions Limited, Dublin, Ireland
  34. * @author Barry O'Donovan <barry@opensolutions.ie>
  35. */
  36. class Entity extends \OSS\SNMP\MIB
  37. {
  38. const OID_ENTITY_PHYSICAL_CLASS = '.1.3.6.1.2.1.47.1.1.1.1.5';
  39. const OID_ENTITY_PHYSICAL_PARENT_REL_POS = '.1.3.6.1.2.1.47.1.1.1.1.6';
  40. const OID_ENTITY_PHYSICAL_ALIAS = '.1.3.6.1.2.1.47.1.1.1.1.14';
  41. /**
  42. * Physical entitly class type
  43. * @var Physical entitly class type
  44. */
  45. const PHYSICAL_CLASS_CHASSIS = 3;
  46. /**
  47. * Physical entitly class type
  48. * @var Physical entitly class type
  49. */
  50. const PHYSICAL_CLASS_CONTAINER = 5;
  51. /**
  52. * Physical entitly class type
  53. * @var Physical entitly class type
  54. */
  55. const PHYSICAL_CLASS_POWER_SUPPLY = 6;
  56. /**
  57. * Physical entitly class type
  58. * @var Physical entitly class type
  59. */
  60. const PHYSICAL_CLASS_FAN = 7;
  61. /**
  62. * Physical entitly class type
  63. * @var Physical entitly class type
  64. */
  65. const PHYSICAL_CLASS_SENSOR = 8;
  66. /**
  67. * Physical entitly class type
  68. * @var Physical entitly class type
  69. */
  70. const PHYSICAL_CLASS_MODULE = 9;
  71. /**
  72. * Physical entitly class type
  73. * @var Physical entitly class type
  74. */
  75. const PHYSICAL_CLASS_PORT = 10;
  76. /**
  77. * Translator array for physical class types
  78. * @var array Translator array for physical class types
  79. */
  80. public static $ENTITY_PHSYICAL_CLASS = array(
  81. self::PHYSICAL_CLASS_CHASSIS => 'chassis',
  82. self::PHYSICAL_CLASS_CONTAINER => 'container',
  83. self::PHYSICAL_CLASS_POWER_SUPPLY => 'powerSupply',
  84. self::PHYSICAL_CLASS_FAN => 'fan',
  85. self::PHYSICAL_CLASS_SENSOR => 'sensor',
  86. self::PHYSICAL_CLASS_MODULE => 'module',
  87. self::PHYSICAL_CLASS_PORT => 'port'
  88. );
  89. /**
  90. * Returns an associate array of entPhysicalClass
  91. *
  92. * e.g. [1005] => 10 / port
  93. *
  94. *
  95. * @param boolean $translate If true, return the string representation via self::$ENTITY_PHSYICAL_CLASS
  96. * @return array Associate array of entPhysicalClass
  97. */
  98. public function physicalClass( $translate = false )
  99. {
  100. $classes = $this->getSNMP()->walk1d( self::OID_ENTITY_PHYSICAL_CLASS );
  101. if( !$translate )
  102. return $classes;
  103. return $this->getSNMP()->translate( $classes, self::$ENTITY_PHSYICAL_CLASS );
  104. }
  105. /**
  106. * Returns an associate array of entPhysicalParentRelPos
  107. *
  108. * e.g. [1005] => 1
  109. *
  110. *
  111. * @return array Associate array of entPhysicalParentRelPos
  112. */
  113. public function physicalParentRelPos()
  114. {
  115. return $this->getSNMP()->walk1d( self::OID_ENTITY_PHYSICAL_PARENT_REL_POS );
  116. }
  117. /**
  118. * Returns an associate array of physical aliases
  119. *
  120. * e.g. [1005] => 10001
  121. *
  122. *
  123. * @return array Associate array of physical aliases
  124. */
  125. public function physicalAlias()
  126. {
  127. return $this->getSNMP()->walk1d( self::OID_ENTITY_PHYSICAL_ALIAS );
  128. }
  129. /**
  130. * Utility function for MIBS\Cisco\RSTP::rstpPortRole() to try and translate a port index
  131. * into a port ID
  132. *
  133. * Makes a number of assumptions including that it has to be of type port, that the ID must be >10000,
  134. * etc.
  135. *
  136. * @return Array of relative positions to port IDs
  137. */
  138. public function relPosToAlias()
  139. {
  140. $rtn = [];
  141. $aliases = $this->physicalAlias();
  142. foreach( $this->physicalParentRelPos() as $index => $pos )
  143. {
  144. if( isset( $aliases[ $index ] ) && strlen( $aliases[ $index ] )
  145. && is_numeric( $aliases[ $index ] ) && $aliases[ $index ] > 10000
  146. && !isset( $rtn[ $pos ] ) && $this->physicalClass()[ $index ] == self::PHYSICAL_CLASS_PORT )
  147. $rtn[ $pos ] = $aliases[ $index ];
  148. }
  149. return $rtn;
  150. }
  151. }