| Conditions | 34 |
| Paths | > 20000 |
| Total Lines | 207 |
| Code Lines | 118 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 306 | public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1') |
||
| 307 | { |
||
| 308 | // Timing |
||
| 309 | $timeTaken = microtime(true); |
||
| 310 | $retVal=0; |
||
| 311 | // Get all mib objects from all mibs |
||
| 312 | $snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.' -On -Tto 2>/dev/null'; |
||
| 313 | $this->logging->log('Getting all traps : '.$snmpCommand,DEBUG ); |
||
| 314 | unset($this->objectsAll); |
||
| 315 | exec($snmpCommand,$this->objectsAll,$retVal); |
||
| 316 | if ($retVal!=0) |
||
| 317 | { |
||
| 318 | $this->logging->log('error executing snmptranslate',ERROR,''); |
||
| 319 | } |
||
| 320 | |||
| 321 | // Get all mibs from databse to have a memory index |
||
| 322 | |||
| 323 | $db_conn=$this->trapsDB->db_connect_trap(); |
||
| 324 | |||
| 325 | $sql='SELECT * from '.$this->trapsDB->dbPrefix.'mib_cache;'; |
||
| 326 | $this->logging->log('SQL query : '.$sql,DEBUG ); |
||
| 327 | if (($ret_code=$db_conn->query($sql)) === false) { |
||
| 328 | $this->logging->log('No result in query : ' . $sql,ERROR,''); |
||
| 329 | } |
||
| 330 | $this->dbOidAll=$ret_code->fetchAll(); |
||
| 331 | $this->dbOidIndex=array(); |
||
| 332 | // Create the index for db; |
||
| 333 | foreach($this->dbOidAll as $key=>$val) |
||
| 334 | { |
||
| 335 | $this->dbOidIndex[$val['oid']]['key']=$key; |
||
| 336 | $this->dbOidIndex[$val['oid']]['id']=$val['id']; |
||
| 337 | } |
||
| 338 | |||
| 339 | // Count elements to show progress |
||
| 340 | $numElements=count($this->objectsAll); |
||
| 341 | $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO ); |
||
| 342 | |||
| 343 | $step=$basestep=$numElements/10; // output display of % done |
||
| 344 | $num_step=0; |
||
| 345 | $timeFiveSec = microtime(true); // Used for display a '.' every <n> seconds |
||
| 346 | |||
| 347 | // Create index for trap objects |
||
| 348 | $this->trapObjectsIndex=array(); |
||
| 349 | |||
| 350 | // detailed timing (time_* vars) |
||
| 351 | $time_parse1=$time_check1=$time_check2=$time_check3=$time_update=$time_objects=0; |
||
| 352 | $time_parse1N=$time_check1N=$time_check2N=$time_check3N=$time_updateN=$time_objectsN=0; |
||
| 353 | $time_num_traps=0; |
||
| 354 | |||
| 355 | for ($curElement=0;$curElement < $numElements;$curElement++) |
||
| 356 | { |
||
| 357 | $time_1= microtime(true); |
||
| 358 | if ((microtime(true)-$timeFiveSec) > 2 && $display_progress) |
||
| 359 | { // echo a . every 2 sec |
||
| 360 | echo '.'; |
||
| 361 | $timeFiveSec = microtime(true); |
||
| 362 | } |
||
| 363 | if ($curElement>$step) |
||
| 364 | { // display progress |
||
| 365 | $num_step++; |
||
| 366 | $step+=$basestep; |
||
| 367 | if ($display_progress) |
||
| 368 | { |
||
| 369 | echo "\n" . ($num_step*10). '% : '; |
||
| 370 | } |
||
| 371 | } |
||
| 372 | // Get oid or pass if not found |
||
| 373 | if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement])) |
||
| 374 | { |
||
| 375 | $time_parse1 += microtime(true) - $time_1; |
||
| 376 | $time_parse1N ++; |
||
| 377 | continue; |
||
| 378 | } |
||
| 379 | $oid=$this->objectsAll[$curElement]; |
||
| 380 | |||
| 381 | // get next line |
||
| 382 | $curElement++; |
||
| 383 | $match=$snmptrans=array(); |
||
| 384 | if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/', |
||
| 385 | $this->objectsAll[$curElement],$match)) |
||
| 386 | { |
||
| 387 | $time_check1 += microtime(true) - $time_1; |
||
| 388 | $time_check1N++; |
||
| 389 | continue; |
||
| 390 | } |
||
| 391 | |||
| 392 | $name=$match[1]; // Name |
||
| 393 | $type=$match[2]; // type (21=trap, 0: may be trap, else : not trap |
||
| 394 | |||
| 395 | if ($type==0) // object type=0 : check if v1 trap |
||
| 396 | { |
||
| 397 | // Check if next is suboid -> in that case is cannot be a trap |
||
| 398 | if (preg_match("/^$oid/",$this->objectsAll[$curElement+1])) |
||
| 399 | { |
||
| 400 | $time_check2 += microtime(true) - $time_1; |
||
| 401 | $time_check2N++; |
||
| 402 | continue; |
||
| 403 | } |
||
| 404 | unset($snmptrans); |
||
| 405 | exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
||
| 406 | ' -Td '.$oid . ' | grep OBJECTS ',$snmptrans,$retVal); |
||
| 407 | if ($retVal!=0) |
||
| 408 | { |
||
| 409 | $time_check2 += microtime(true) - $time_1; |
||
| 410 | $time_check2N++; |
||
| 411 | continue; |
||
| 412 | } |
||
| 413 | //echo "\n v1 trap found : $oid \n"; |
||
| 414 | // Force as trap. |
||
| 415 | $type=21; |
||
| 416 | } |
||
| 417 | if ($onlyTraps===true && $type!=21) // if only traps and not a trap, continue |
||
| 418 | { |
||
| 419 | $time_check3 += microtime(true) - $time_1; |
||
| 420 | $time_check3N++; |
||
| 421 | continue; |
||
| 422 | } |
||
| 423 | |||
| 424 | $time_num_traps++; |
||
| 425 | |||
| 426 | $this->logging->log('Found trap : '.$match[1] . ' / OID : '.$oid,INFO ); |
||
| 427 | if ($display_progress) echo '#'; // echo a # when trap found |
||
| 428 | |||
| 429 | // get trap objects & source MIB |
||
| 430 | unset($snmptrans); |
||
| 431 | exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
||
| 432 | ' -Td '.$oid,$snmptrans,$retVal); |
||
| 433 | if ($retVal!=0) |
||
| 434 | { |
||
| 435 | $this->logging->log('error executing snmptranslate',ERROR,''); |
||
| 436 | } |
||
| 437 | |||
| 438 | if (!preg_match('/^(.*)::/',$snmptrans[0],$match)) |
||
| 439 | { |
||
| 440 | $this->logging->log('Error getting mib from trap '.$oid.' : ' . $snmptrans[0],1,''); |
||
| 441 | } |
||
| 442 | $trapMib=$match[1]; |
||
| 443 | |||
| 444 | $numLine=1;$trapDesc=''; |
||
| 445 | while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
||
| 446 | if (isset($snmptrans[$numLine])) |
||
| 447 | { |
||
| 448 | $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
||
| 449 | |||
| 450 | while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine])) |
||
| 451 | { |
||
| 452 | $trapDesc.=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]); |
||
| 453 | $numLine++; |
||
| 454 | } |
||
| 455 | if (isset($snmptrans[$numLine])) { |
||
| 456 | $trapDesc.=preg_replace('/".*/','',$snmptrans[$numLine]); |
||
| 457 | $trapDesc=preg_replace('/[\t ]+/',' ',$trapDesc); |
||
| 458 | } |
||
| 459 | |||
| 460 | } |
||
| 461 | $update=$this->update_oid($oid,$trapMib,$name,$type,NULL,NULL,NULL,NULL,$trapDesc); |
||
| 462 | $time_update += microtime(true) - $time_1; $time_1= microtime(true); |
||
| 463 | |||
| 464 | if (($update==0) && ($check_change===false)) |
||
| 465 | { // Trapd didn't change & force check disabled |
||
| 466 | $time_objects += microtime(true) - $time_1; |
||
| 467 | if ($display_progress) echo "C"; |
||
| 468 | continue; |
||
| 469 | } |
||
| 470 | |||
| 471 | $synt=null; |
||
| 472 | foreach ($snmptrans as $line) |
||
| 473 | { |
||
| 474 | if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match)) |
||
| 475 | { |
||
| 476 | $synt=$match[1]; |
||
| 477 | } |
||
| 478 | } |
||
| 479 | if ($synt == null) |
||
| 480 | { |
||
| 481 | //echo "No objects for $trapOID\n"; |
||
| 482 | $time_objects += microtime(true) - $time_1; |
||
| 483 | continue; |
||
| 484 | } |
||
| 485 | //echo "$synt \n"; |
||
| 486 | $trapObjects=array(); |
||
| 487 | while (preg_match('/ *([^ ,]+) *,* */',$synt,$match)) |
||
| 488 | { |
||
| 489 | array_push($trapObjects,$match[1]); |
||
| 490 | $synt=preg_replace('/'.$match[0].'/','',$synt); |
||
| 491 | } |
||
| 492 | |||
| 493 | $this->trap_objects($oid, $trapMib, $trapObjects, false); |
||
| 494 | |||
| 495 | $time_objects += microtime(true) - $time_1; |
||
| 496 | $time_objectsN++; |
||
| 497 | } |
||
| 498 | |||
| 499 | if ($display_progress) |
||
| 500 | { |
||
| 501 | echo "\nNumber of processed traps : $time_num_traps \n"; |
||
| 502 | echo "\nParsing : " . number_format($time_parse1+$time_check1,1) ." sec / " . ($time_parse1N+ $time_check1N) . " occurences\n"; |
||
| 503 | echo "Detecting traps : " . number_format($time_check2+$time_check3,1) . " sec / " . ($time_check2N+$time_check3N) ." occurences\n"; |
||
| 504 | echo "Trap processing ($time_updateN): ".number_format($time_update,1)." sec , "; |
||
| 505 | echo "Objects processing ($time_objectsN) : ".number_format($time_objects,1)." sec \n"; |
||
| 506 | } |
||
| 507 | |||
| 508 | // Timing ends |
||
| 509 | $timeTaken=microtime(true) - $timeTaken; |
||
| 510 | if ($display_progress) |
||
| 511 | { |
||
| 512 | echo "Global time : ".round($timeTaken)." seconds\n"; |
||
| 513 | } |
||
| 518 | } |