| Total Complexity | 82 |
| Total Lines | 646 |
| Duplicated Lines | 0 % |
| Changes | 9 | ||
| Bugs | 3 | Features | 0 |
Complex classes like Mib often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Mib, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Mib |
||
| 19 | { |
||
| 20 | use \MibDatabase; |
||
|
|
|||
| 21 | |||
| 22 | /** @var Logging $logging logging class */ |
||
| 23 | protected $logging; |
||
| 24 | /** @var Database $trapsDB Database class */ |
||
| 25 | protected $trapsDB; |
||
| 26 | |||
| 27 | /** @var string $snmptranslate */ |
||
| 28 | public $snmptranslate; |
||
| 29 | /** @var string $snmptranslateDirs */ |
||
| 30 | public $snmptranslateDirs; |
||
| 31 | |||
| 32 | private $dbOidAll; //< All oid in database; |
||
| 33 | private $dbOidIndex; //< Index of oid in dbOidAll |
||
| 34 | private $objectsAll; //< output lines of snmptranslate list |
||
| 35 | private $trapObjectsIndex; //< array of traps objects (as OID) |
||
| 36 | |||
| 37 | private $oidDesc=array(); //< $oid,$mib,$name,$type,$textConv,$dispHint,$syntax,$type_enum,$description=NULL |
||
| 38 | |||
| 39 | // Timing vars for update |
||
| 40 | private $timing=array(); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Setup Mib Class |
||
| 44 | * @param Logging $logClass : where to log |
||
| 45 | * @param Database $dbClass : Database |
||
| 46 | */ |
||
| 47 | function __construct($logClass,$dbClass,$snmptrans,$snmptransdir) |
||
| 48 | { |
||
| 49 | $this->logging=$logClass; |
||
| 50 | $this->trapsDB=$dbClass; |
||
| 51 | $this->snmptranslate=$snmptrans; |
||
| 52 | $this->snmptranslateDirs=$snmptransdir; |
||
| 53 | |||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @return \Trapdirector\Logging |
||
| 58 | */ |
||
| 59 | public function getLogging() |
||
| 60 | { |
||
| 61 | return $this->logging; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @return \Trapdirector\Database |
||
| 66 | */ |
||
| 67 | public function getTrapsDB() |
||
| 68 | { |
||
| 69 | return $this->trapsDB; |
||
| 70 | } |
||
| 71 | |||
| 72 | |||
| 73 | |||
| 74 | /** |
||
| 75 | * Create object in DB with object in dbOidIndex |
||
| 76 | * @return number : 0=unchanged, 1 = changed, 2=created |
||
| 77 | */ |
||
| 78 | private function update_oid_create() |
||
| 79 | { |
||
| 80 | // Insert data |
||
| 81 | |||
| 82 | $db_conn=$this->trapsDB->db_connect_trap(); |
||
| 83 | $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache '. |
||
| 84 | '(oid, name, type , mib, textual_convention, display_hint '. |
||
| 85 | ', syntax, type_enum , description ) ' . |
||
| 86 | 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
||
| 87 | ', :syntax, :type_enum, :description )'; |
||
| 88 | |||
| 89 | if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
||
| 90 | |||
| 91 | $sqlQuery=$db_conn->prepare($sql); |
||
| 92 | |||
| 93 | $sqlParam=array( |
||
| 94 | ':oid' => $this->oidDesc['oid'], |
||
| 95 | ':name' => $this->oidDesc['name'], |
||
| 96 | ':type' => $this->oidDesc['type'], |
||
| 97 | ':mib' => $this->oidDesc['mib'], |
||
| 98 | ':tc' => $this->oidDesc['textconv']??'null', |
||
| 99 | ':display_hint' => $this->oidDesc['dispHint']??'null', |
||
| 100 | ':syntax' => $this->oidDesc['syntax']??'null', |
||
| 101 | ':type_enum' => $this->oidDesc['type_enum']??'null', |
||
| 102 | ':description' => $this->oidDesc['description']??'null' |
||
| 103 | ); |
||
| 104 | |||
| 105 | if ($sqlQuery->execute($sqlParam) === false) { |
||
| 106 | $this->logging->log('Error in query : ' . $sql,1,''); |
||
| 107 | } |
||
| 108 | |||
| 109 | switch ($this->trapsDB->trapDBType) |
||
| 110 | { |
||
| 111 | case 'pgsql': |
||
| 112 | // Get last id to insert oid/values in secondary table |
||
| 113 | if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
||
| 114 | $this->logging->log('Error getting id - pgsql - ',1,''); |
||
| 115 | } |
||
| 116 | if (! isset($inserted_id_ret['id'])) { |
||
| 117 | $this->logging->log('Error getting id - pgsql - empty.',ERROR); |
||
| 118 | return 0; |
||
| 119 | } |
||
| 120 | $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id_ret['id']; |
||
| 121 | break; |
||
| 122 | case 'mysql': |
||
| 123 | // Get last id to insert oid/values in secondary table |
||
| 124 | $sql='SELECT LAST_INSERT_ID();'; |
||
| 125 | if (($ret_code=$db_conn->query($sql)) === false) { |
||
| 126 | $this->logging->log('Erreur getting id - mysql - ',ERROR); |
||
| 127 | return 0; |
||
| 128 | } |
||
| 129 | |||
| 130 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
||
| 131 | if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
||
| 132 | $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id; |
||
| 133 | break; |
||
| 134 | default: |
||
| 135 | $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType,ERROR); |
||
| 136 | return 0; |
||
| 137 | } |
||
| 138 | |||
| 139 | // Set as newly created. |
||
| 140 | $this->dbOidIndex[$this->oidDesc['oid']]['key']=-1; |
||
| 141 | return 2; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Update or add an OID to database uses $this->dbOidIndex for mem cache |
||
| 146 | * and $this->oidDesc doe data |
||
| 147 | * @return number : 0=unchanged, 1 = changed, 2=created |
||
| 148 | */ |
||
| 149 | public function update_oid() |
||
| 161 | |||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * get all objects for a trap. |
||
| 166 | * @param integer $trapId |
||
| 167 | * @return array : array of cached objects |
||
| 168 | */ |
||
| 169 | private function cache_db_objects($trapId) |
||
| 170 | { |
||
| 171 | $dbObjects=array(); // cache of objects for trap in db |
||
| 172 | $db_conn=$this->trapsDB->db_connect_trap(); |
||
| 173 | // Get all objects |
||
| 174 | $sql='SELECT * FROM '.$this->trapsDB->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
||
| 175 | $this->logging->log('SQL query get all traps: '.$sql,DEBUG ); |
||
| 176 | if (($ret_code=$db_conn->query($sql)) === false) { |
||
| 177 | $this->logging->log('No result in query : ' . $sql,1,''); |
||
| 178 | } |
||
| 179 | $dbObjectsRaw=$ret_code->fetchAll(); |
||
| 180 | |||
| 181 | foreach ($dbObjectsRaw as $val) |
||
| 182 | { |
||
| 183 | $dbObjects[$val['object_id']]=1; |
||
| 184 | } |
||
| 185 | return $dbObjects; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get object details & mib , returns snmptranslate output |
||
| 190 | * @param string $object : object name |
||
| 191 | * @param string $trapmib : mib of trap |
||
| 192 | * @return NULL|array : null if not found, or output of snmptranslate |
||
| 193 | */ |
||
| 194 | private function get_object_details($object,$trapmib) |
||
| 195 | { |
||
| 196 | $match=$snmptrans=array(); |
||
| 197 | $retVal=0; |
||
| 198 | $this->oidDesc['mib']=$trapmib; |
||
| 199 | exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
||
| 200 | ' -On -Td '.$this->oidDesc['mib'].'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
||
| 201 | if ($retVal!=0) |
||
| 202 | { |
||
| 203 | // Maybe not trap mib, search with IR |
||
| 204 | exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
||
| 205 | ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
||
| 206 | if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
||
| 207 | { // Not found -> continue with warning |
||
| 208 | $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
||
| 209 | return null; |
||
| 210 | } |
||
| 211 | $this->oidDesc['mib']=$match[1]; |
||
| 212 | |||
| 213 | // Do the snmptranslate again. |
||
| 214 | exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
||
| 215 | ' -On -Td '.$this->oidDesc['mib'].'::'.$object,$snmptrans,$retVal); |
||
| 216 | if ($retVal!=0) { |
||
| 217 | $this->logging->log('Error finding trap object : '.$this->oidDesc['mib'].'::'.$object,2,''); |
||
| 218 | return null; |
||
| 219 | } |
||
| 220 | |||
| 221 | } |
||
| 222 | return $snmptrans; |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Parse snmptranslate output and set $this->oidDesc with elements |
||
| 227 | * @param array $snmptrans : multi line output of snmptrans |
||
| 228 | */ |
||
| 229 | private function parse_object($snmptrans) |
||
| 284 | } |
||
| 285 | } |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * create or update (with check_existing = true) objects of trap |
||
| 290 | * @param string $trapOID : trap oid |
||
| 291 | * @param string $trapmib : mib of trap |
||
| 292 | * @param array $objects : array of objects name (without MIB) |
||
| 293 | * @param bool $check_existing : check instead of create |
||
| 294 | */ |
||
| 295 | public function trap_objects($trapOID,$trapmib,$objects,$check_existing) |
||
| 296 | { |
||
| 297 | $trapId = $this->dbOidIndex[$trapOID]['id']; // Get id of trap |
||
| 298 | |||
| 299 | if ($check_existing === true) |
||
| 300 | { |
||
| 301 | $dbObjects=$this->cache_db_objects($trapId); |
||
| 302 | } |
||
| 303 | |||
| 304 | foreach ($objects as $object) |
||
| 305 | { |
||
| 306 | |||
| 307 | $this->reset_oidDesc(); |
||
| 308 | |||
| 309 | $snmptrans=$this->get_object_details($object, $trapmib); // Get object mib & details |
||
|
1 ignored issue
–
show
|
|||
| 310 | if ($snmptrans === null) continue; // object not found |
||
| 311 | |||
| 312 | $this->parse_object($snmptrans); |
||
| 313 | |||
| 314 | $this->oidDesc['name'] = $object; |
||
| 315 | |||
| 316 | $this->logging->log("Adding object ".$this->oidDesc['name']." : ".$this->oidDesc['oid']." / ".$this->oidDesc['syntax']." / ".$this->oidDesc['type_enum']." / ".$this->oidDesc['dispHint']." / ".$this->oidDesc['textconv'],DEBUG ); |
||
| 317 | |||
| 318 | // Update |
||
| 319 | $this->update_oid(); |
||
| 320 | |||
| 321 | if (isset($dbObjects[$this->dbOidIndex[$this->oidDesc['oid']]['id']])) |
||
| 322 | { // if link exists, continue |
||
| 323 | $dbObjects[$this->dbOidIndex[$this->oidDesc['oid']]['id']]=2; |
||
| 324 | continue; |
||
| 325 | } |
||
| 326 | if ($check_existing === true) |
||
| 327 | { |
||
| 328 | // TODO : check link trap - objects exists, mark them. |
||
| 329 | } |
||
| 330 | // Associate in object table |
||
| 331 | $db_conn=$this->trapsDB->db_connect_trap(); |
||
| 332 | $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache_trap_object (trap_id,object_id) '. |
||
| 333 | 'values (:trap_id, :object_id)'; |
||
| 334 | $sqlQuery=$db_conn->prepare($sql); |
||
| 335 | $sqlParam=array( |
||
| 336 | ':trap_id' => $trapId, |
||
| 337 | ':object_id' => $this->dbOidIndex[$this->oidDesc['oid']]['id'], |
||
| 338 | ); |
||
| 339 | |||
| 340 | if ($sqlQuery->execute($sqlParam) === false) { |
||
| 341 | $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$this->oidDesc['oid']]['id'] ,1,''); |
||
| 342 | } |
||
| 343 | } |
||
| 344 | if ($check_existing === true) |
||
| 345 | { |
||
| 346 | // TODO : remove link trap - objects that wasn't marked. |
||
| 347 | } |
||
| 348 | |||
| 349 | } |
||
| 350 | |||
| 351 | private function reset_oidDesc() |
||
| 352 | { |
||
| 353 | $this->oidDesc['oid']=null; |
||
| 354 | $this->oidDesc['name']=null; |
||
| 355 | $this->oidDesc['type']=null; |
||
| 356 | $this->oidDesc['mib']=null; |
||
| 357 | $this->oidDesc['textconv']=null; |
||
| 358 | $this->oidDesc['dispHint'] =null; |
||
| 359 | $this->oidDesc['syntax']=null; |
||
| 360 | $this->oidDesc['type_enum']=null; |
||
| 361 | $this->oidDesc['description']=null; |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Fills $this->objectsAll with all mibs from snmptranslate |
||
| 366 | * @return integer : number of elements |
||
| 367 | */ |
||
| 368 | private function load_mibs_snmptranslate() |
||
| 369 | { |
||
| 370 | $retVal=0; |
||
| 371 | // Get all mib objects from all mibs |
||
| 372 | $snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.' -On -Tto 2>/dev/null'; |
||
| 373 | $this->logging->log('Getting all traps : '.$snmpCommand,DEBUG ); |
||
| 374 | unset($this->objectsAll); |
||
| 375 | exec($snmpCommand,$this->objectsAll,$retVal); |
||
| 376 | if ($retVal!=0) |
||
| 377 | { |
||
| 378 | $this->logging->log('error executing snmptranslate',ERROR,''); |
||
| 379 | } |
||
| 380 | // Count elements to show progress |
||
| 381 | $numElements=count($this->objectsAll); |
||
| 382 | $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO ); |
||
| 383 | return $numElements; |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * load all mib objects db in dbOidAll (raw) and index in dbOidIndex |
||
| 388 | */ |
||
| 389 | private function load_mibs_from_db() |
||
| 390 | { |
||
| 391 | // Get all mibs from databse to have a memory index |
||
| 392 | |||
| 393 | $db_conn=$this->trapsDB->db_connect_trap(); |
||
| 394 | |||
| 395 | $sql='SELECT * from '.$this->trapsDB->dbPrefix.'mib_cache;'; |
||
| 396 | $this->logging->log('SQL query : '.$sql,DEBUG ); |
||
| 397 | if (($ret_code=$db_conn->query($sql)) === false) { |
||
| 398 | $this->logging->log('No result in query : ' . $sql,ERROR,''); |
||
| 399 | } |
||
| 400 | $this->dbOidAll=$ret_code->fetchAll(); |
||
| 401 | $this->dbOidIndex=array(); |
||
| 402 | // Create the index for db; |
||
| 403 | foreach($this->dbOidAll as $key=>$val) |
||
| 404 | { |
||
| 405 | $this->dbOidIndex[$val['oid']]['key']=$key; |
||
| 406 | $this->dbOidIndex[$val['oid']]['id']=$val['id']; |
||
| 407 | } |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Reset all update timers & count to zero |
||
| 412 | */ |
||
| 413 | private function reset_update_timers() |
||
| 414 | { |
||
| 415 | $this->timing['base_parse_time']=0; |
||
| 416 | $this->timing['base_check_time']=0; |
||
| 417 | $this->timing['type0_check_time']=0; |
||
| 418 | $this->timing['nottrap_time']=0; |
||
| 419 | $this->timing['update_time']=0; |
||
| 420 | $this->timing['objects_time']=0; |
||
| 421 | $this->timing['base_parse_num']=0; |
||
| 422 | $this->timing['base_check_num']=0; |
||
| 423 | $this->timing['type0_check_num']=0; |
||
| 424 | $this->timing['nottrap_num']=0; |
||
| 425 | $this->timing['update_num']=0; |
||
| 426 | $this->timing['objects_num']=0; |
||
| 427 | $this->timing['num_traps']=0; |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Detect if $this->objectsAll[$curElement] is a trap |
||
| 432 | * @param integer $curElement |
||
| 433 | * @param bool $onlyTraps : set to false to get all and not only traps. |
||
| 434 | * @return boolean : false if it's a trap , true if not |
||
| 435 | */ |
||
| 436 | private function detect_trap($curElement,$onlyTraps) |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * get_trap_mib_description |
||
| 495 | * @return array|null : array of snmptranslate output or null on error |
||
| 496 | **/ |
||
| 497 | private function get_trap_mib_description() |
||
| 534 | } |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Get trap objects |
||
| 538 | * @param array $snmptrans : output of snmptranslate for TrapModuleConfig |
||
| 539 | * @return array|null : array of objects or null if not found |
||
| 540 | **/ |
||
| 541 | private function get_trap_objects($snmptrans) |
||
| 542 | { |
||
| 543 | $objectName=null; |
||
| 544 | $match=array(); |
||
| 545 | foreach ($snmptrans as $line) |
||
| 546 | { |
||
| 547 | if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match)) |
||
| 548 | { |
||
| 549 | $objectName=$match[1]; |
||
| 550 | } |
||
| 551 | } |
||
| 552 | if ($objectName == null) |
||
| 553 | { |
||
| 554 | $this->logging->log('No objects for ' . $this->oidDesc['oid'],DEBUG); |
||
| 555 | $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
||
| 556 | return null; |
||
| 557 | } |
||
| 558 | |||
| 559 | $trapObjects=array(); |
||
| 560 | while (preg_match('/ *([^ ,]+) *,* */',$objectName,$match)) |
||
| 561 | { |
||
| 562 | array_push($trapObjects,$match[1]); |
||
| 563 | $objectName=preg_replace('/'.$match[0].'/','',$objectName); |
||
| 564 | } |
||
| 565 | return $trapObjects; |
||
| 566 | } |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Cache mib in database |
||
| 570 | * @param boolean $display_progress : Display progress on standard output |
||
| 571 | * @param boolean $check_change : Force check of trap params & objects |
||
| 572 | * @param boolean $onlyTraps : only cache traps and objects (true) or all (false) |
||
| 573 | * @param string $startOID : only cache under startOID (NOT IMPLEMENTED) |
||
| 574 | */ |
||
| 575 | public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1') |
||
| 664 | } |
||
| 665 | } |
||
| 666 | |||
| 667 | |||
| 668 | } |