| Total Complexity | 89 |
| Total Lines | 642 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 2 | 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 |
||
| 11 | class Mib |
||
| 12 | { |
||
| 13 | |||
| 14 | protected $logging; //< logging class |
||
| 15 | protected $trapsDB; //< Database class |
||
| 16 | |||
| 17 | public $snmptranslate; |
||
| 18 | public $snmptranslateDirs; |
||
| 19 | |||
| 20 | private $dbOidAll; //< All oid in database; |
||
| 21 | private $dbOidIndex; //< Index of oid in dbOidAll |
||
| 22 | private $objectsAll; //< output lines of snmptranslate list |
||
| 23 | private $trapObjectsIndex; //< array of traps objects (as OID) |
||
| 24 | |||
| 25 | private $oidDesc=array(); //< $oid,$mib,$name,$type,$textConv,$dispHint,$syntax,$type_enum,$description=NULL |
||
| 26 | |||
| 27 | // Timing vars for update |
||
| 28 | private $timing=array(); |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Setup Mib Class |
||
| 32 | * @param Logging $logClass : where to log |
||
| 33 | * @param Database $dbClass : Database |
||
| 34 | */ |
||
| 35 | function __construct($logClass,$dbClass,$snmptrans,$snmptransdir) |
||
| 36 | { |
||
| 37 | $this->logging=$logClass; |
||
| 38 | $this->trapsDB=$dbClass; |
||
| 39 | $this->snmptranslate=$snmptrans; |
||
| 40 | $this->snmptranslateDirs=$snmptransdir; |
||
| 41 | |||
| 42 | } |
||
| 43 | |||
| 44 | |||
| 45 | /** |
||
| 46 | * Update or add an OID to database uses $this->dbOidIndex for mem cache |
||
| 47 | * and $this->oidDesc doe data |
||
| 48 | * @return number : 0=unchanged, 1 = changed, 2=created |
||
| 49 | */ |
||
| 50 | public function update_oid() |
||
| 51 | { |
||
| 52 | $db_conn=$this->trapsDB->db_connect_trap(); |
||
| 53 | $this->oidDesc['description']=$db_conn->quote($this->oidDesc['description']); |
||
| 54 | if (isset($this->dbOidIndex[$this->oidDesc['oid']])) |
||
| 55 | { |
||
| 56 | if ($this->dbOidIndex[$this->oidDesc['oid']]['key'] == -1) |
||
| 57 | { // newly created. |
||
| 58 | return 0; |
||
| 59 | } |
||
| 60 | $oidIndex=$this->dbOidIndex[$this->oidDesc['oid']]['key']; // Get index in dbOidAll |
||
| 61 | $dbOid=$this->dbOidAll[$oidIndex]; // Get array of element |
||
| 62 | if ( $this->oidDesc['name'] != $dbOid['name'] || |
||
| 63 | $this->oidDesc['mib'] != $dbOid['mib'] || |
||
| 64 | $this->oidDesc['type'] !=$dbOid['type'] |
||
| 65 | ) |
||
| 66 | { // Do update |
||
| 67 | $sql='UPDATE '.$this->trapsDB->dbPrefix.'mib_cache SET '. |
||
| 68 | 'name = :name , type = :type , mib = :mib , textual_convention = :tc , display_hint = :display_hint'. |
||
| 69 | ', syntax = :syntax, type_enum = :type_enum, description = :description '. |
||
| 70 | ' WHERE id= :id'; |
||
| 71 | $sqlQuery=$db_conn->prepare($sql); |
||
| 72 | |||
| 73 | $sqlParam=array( |
||
| 74 | ':name' => $this->oidDesc['name'], |
||
| 75 | ':type' => $this->oidDesc['type'], |
||
| 76 | ':mib' => $this->oidDesc['mib'], |
||
| 77 | ':tc' => $this->oidDesc['textconv']??'null', |
||
| 78 | ':display_hint' => $this->oidDesc['dispHint']??'null' , |
||
| 79 | ':syntax' => $this->oidDesc['syntax']==null??'null', |
||
| 80 | ':type_enum' => $this->oidDesc['type_enum']??'null', |
||
| 81 | ':description' => $this->oidDesc['description']??'null', |
||
| 82 | ':id' => $this->dbOidAll[$this->dbOidIndex[$this->oidDesc['oid']]['id']] |
||
| 83 | ); |
||
| 84 | |||
| 85 | if ($sqlQuery->execute($sqlParam) === false) { |
||
| 86 | $this->logging->log('Error in query : ' . $sql,ERROR,''); |
||
| 87 | } |
||
| 88 | $this->logging->log('Trap updated : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
||
| 89 | return 1; |
||
| 90 | } |
||
| 91 | else |
||
| 92 | { |
||
| 93 | $this->logging->log('Trap unchanged : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
||
| 94 | return 0; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | // create new OID. |
||
| 98 | |||
| 99 | // Insert data |
||
| 100 | |||
| 101 | $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache '. |
||
| 102 | '(oid, name, type , mib, textual_convention, display_hint '. |
||
| 103 | ', syntax, type_enum , description ) ' . |
||
| 104 | 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
||
| 105 | ', :syntax, :type_enum, :description )'; |
||
| 106 | |||
| 107 | if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
||
| 108 | |||
| 109 | $sqlQuery=$db_conn->prepare($sql); |
||
| 110 | |||
| 111 | $sqlParam=array( |
||
| 112 | ':oid' => $this->oidDesc['oid'], |
||
| 113 | ':name' => $this->oidDesc['name'], |
||
| 114 | ':type' => $this->oidDesc['type'], |
||
| 115 | ':mib' => $this->oidDesc['mib'], |
||
| 116 | ':tc' => ($this->oidDesc['textconv']==null)?'null':$this->oidDesc['textconv'] , |
||
| 117 | ':display_hint' => ($this->oidDesc['dispHint']==null)?'null':$this->oidDesc['dispHint'] , |
||
| 118 | ':syntax' => ($this->oidDesc['syntax']==null)?'null':$this->oidDesc['syntax'], |
||
| 119 | ':type_enum' => ($this->oidDesc['type_enum']==null)?'null':$this->oidDesc['type_enum'], |
||
| 120 | ':description' => ($this->oidDesc['description']==null)?'null':$this->oidDesc['description'] |
||
| 121 | ); |
||
| 122 | |||
| 123 | if ($sqlQuery->execute($sqlParam) === false) { |
||
| 124 | $this->logging->log('Error in query : ' . $sql,1,''); |
||
| 125 | } |
||
| 126 | |||
| 127 | switch ($this->trapsDB->trapDBType) |
||
| 128 | { |
||
| 129 | case 'pgsql': |
||
| 130 | // Get last id to insert oid/values in secondary table |
||
| 131 | if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
||
| 132 | $this->logging->log('Error getting id - pgsql - ',1,''); |
||
| 133 | } |
||
| 134 | if (! isset($inserted_id_ret['id'])) { |
||
| 135 | $this->logging->log('Error getting id - pgsql - empty.',1,''); |
||
| 136 | } |
||
| 137 | $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id_ret['id']; |
||
| 138 | break; |
||
| 139 | case 'mysql': |
||
| 140 | // Get last id to insert oid/values in secondary table |
||
| 141 | $sql='SELECT LAST_INSERT_ID();'; |
||
| 142 | if (($ret_code=$db_conn->query($sql)) === false) { |
||
| 143 | $this->logging->log('Erreur getting id - mysql - ',1,''); |
||
| 144 | } |
||
| 145 | |||
| 146 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
||
| 147 | if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
||
| 148 | $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id; |
||
| 149 | break; |
||
| 150 | default: |
||
| 151 | $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType,1,''); |
||
| 152 | } |
||
| 153 | |||
| 154 | // Set as newly created. |
||
| 155 | $this->dbOidIndex[$this->oidDesc['oid']]['key']=-1; |
||
| 156 | return 2; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * get all objects for a trap. |
||
| 161 | * @param integer $trapId |
||
| 162 | * @return array : array of cached objects |
||
| 163 | */ |
||
| 164 | private function cache_db_objects($trapId) |
||
| 165 | { |
||
| 166 | $dbObjects=array(); // cache of objects for trap in db |
||
| 167 | $db_conn=$this->trapsDB->db_connect_trap(); |
||
| 168 | // Get all objects |
||
| 169 | $sql='SELECT * FROM '.$this->trapsDB->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
||
| 170 | $this->logging->log('SQL query get all traps: '.$sql,DEBUG ); |
||
| 171 | if (($ret_code=$db_conn->query($sql)) === false) { |
||
| 172 | $this->logging->log('No result in query : ' . $sql,1,''); |
||
| 173 | } |
||
| 174 | $dbObjectsRaw=$ret_code->fetchAll(); |
||
| 175 | |||
| 176 | foreach ($dbObjectsRaw as $val) |
||
| 177 | { |
||
| 178 | $dbObjects[$val['object_id']]=1; |
||
| 179 | } |
||
| 180 | return $dbObjects; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Get object details & mib , returns snmptranslate output |
||
| 185 | * @param string $object : object name |
||
| 186 | * @param string $trapmib : mib of trap |
||
| 187 | * @return NULL|array : null if not found, or output of snmptranslate |
||
| 188 | */ |
||
| 189 | private function get_object_details($object,$trapmib) |
||
| 190 | { |
||
| 191 | $match=$snmptrans=array(); |
||
| 192 | $retVal=0; |
||
| 193 | $this->oidDesc['mib']=$trapmib; |
||
| 194 | exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
||
| 195 | ' -On -Td '.$this->oidDesc['mib'].'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
||
| 196 | if ($retVal!=0) |
||
| 197 | { |
||
| 198 | // Maybe not trap mib, search with IR |
||
| 199 | exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
||
| 200 | ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
||
| 201 | if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
||
| 202 | { // Not found -> continue with warning |
||
| 203 | $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
||
| 204 | return null; |
||
| 205 | } |
||
| 206 | $this->oidDesc['mib']=$match[1]; |
||
| 207 | |||
| 208 | // Do the snmptranslate again. |
||
| 209 | exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
||
| 210 | ' -On -Td '.$this->oidDesc['mib'].'::'.$object,$snmptrans,$retVal); |
||
| 211 | if ($retVal!=0) { |
||
| 212 | $this->logging->log('Error finding trap object : '.$this->oidDesc['mib'].'::'.$object,2,''); |
||
| 213 | return null; |
||
| 214 | } |
||
| 215 | |||
| 216 | } |
||
| 217 | return $snmptrans; |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Parse snmptranslate output and set $this->oidDesc with elements |
||
| 222 | * @param array $snmptrans : multi line output of snmptrans |
||
| 223 | */ |
||
| 224 | private function parse_object($snmptrans) |
||
| 225 | { |
||
| 226 | $tmpdesc=''; // For multiline description |
||
| 227 | $indesc=false; // true if currently inside multiline description |
||
| 228 | $match=array(); |
||
| 229 | |||
| 230 | foreach ($snmptrans as $line) |
||
| 231 | { |
||
| 232 | if ($indesc===true) |
||
| 233 | { |
||
| 234 | $line=preg_replace('/[\t ]+/',' ',$line); |
||
| 235 | if (preg_match('/(.*)"$/', $line,$match)) |
||
| 236 | { |
||
| 237 | $this->oidDesc['description'] = $tmpdesc . $match[1]; |
||
| 238 | $indesc=false; |
||
| 239 | } |
||
| 240 | $tmpdesc.=$line; |
||
| 241 | continue; |
||
| 242 | } |
||
| 243 | if (preg_match('/^\.[0-9\.]+$/', $line)) |
||
| 244 | { |
||
| 245 | $this->oidDesc['oid']=$line; |
||
| 246 | continue; |
||
| 247 | } |
||
| 248 | if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match)) |
||
| 249 | { |
||
| 250 | $this->oidDesc['syntax']=$match[1]; |
||
| 251 | $this->oidDesc['type_enum']=$match[2]; |
||
| 252 | continue; |
||
| 253 | } |
||
| 254 | if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match)) |
||
| 255 | { |
||
| 256 | $this->oidDesc['syntax']=$match[1]; |
||
| 257 | continue; |
||
| 258 | } |
||
| 259 | if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match)) |
||
| 260 | { |
||
| 261 | $this->oidDesc['dispHint']=$match[1]; |
||
| 262 | continue; |
||
| 263 | } |
||
| 264 | if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match)) |
||
| 265 | { |
||
| 266 | $this->oidDesc['description']=$match[1]; |
||
| 267 | continue; |
||
| 268 | } |
||
| 269 | if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match)) |
||
| 270 | { |
||
| 271 | $tmpdesc=$match[1]; |
||
| 272 | $indesc=true; |
||
| 273 | continue; |
||
| 274 | } |
||
| 275 | if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match)) |
||
| 276 | { |
||
| 277 | $this->oidDesc['textconv']=$match[1]; |
||
| 278 | continue; |
||
| 279 | } |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * create or update (with check_existing = true) objects of trap |
||
| 285 | * @param string $trapOID : trap oid |
||
| 286 | * @param string $trapmib : mib of trap |
||
| 287 | * @param array $objects : array of objects name (without MIB) |
||
| 288 | * @param bool $check_existing : check instead of create |
||
| 289 | */ |
||
| 290 | public function trap_objects($trapOID,$trapmib,$objects,$check_existing) |
||
| 291 | { |
||
| 292 | $trapId = $this->dbOidIndex[$trapOID]['id']; // Get id of trap |
||
| 293 | |||
| 294 | if ($check_existing === true) |
||
| 295 | { |
||
| 296 | $dbObjects=$this->cache_db_objects($trapId); |
||
| 297 | } |
||
| 298 | |||
| 299 | foreach ($objects as $object) |
||
| 300 | { |
||
| 301 | |||
| 302 | $this->reset_oidDesc(); |
||
| 303 | |||
| 304 | $snmptrans=$this->get_object_details($object, $trapmib); // Get object mib & details |
||
|
1 ignored issue
–
show
|
|||
| 305 | if ($snmptrans === null) continue; // object not found |
||
| 306 | |||
| 307 | $this->parse_object($snmptrans); |
||
| 308 | |||
| 309 | $this->oidDesc['name'] = $object; |
||
| 310 | |||
| 311 | $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 ); |
||
| 312 | |||
| 313 | // Update |
||
| 314 | $this->update_oid(); |
||
| 315 | |||
| 316 | if (isset($dbObjects[$this->dbOidIndex[$this->oidDesc['oid']]['id']])) |
||
| 317 | { // if link exists, continue |
||
| 318 | $dbObjects[$this->dbOidIndex[$this->oidDesc['oid']]['id']]=2; |
||
| 319 | continue; |
||
| 320 | } |
||
| 321 | if ($check_existing === true) |
||
| 322 | { |
||
| 323 | // TODO : check link trap - objects exists, mark them. |
||
| 324 | } |
||
| 325 | // Associate in object table |
||
| 326 | $db_conn=$this->trapsDB->db_connect_trap(); |
||
| 327 | $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache_trap_object (trap_id,object_id) '. |
||
| 328 | 'values (:trap_id, :object_id)'; |
||
| 329 | $sqlQuery=$db_conn->prepare($sql); |
||
| 330 | $sqlParam=array( |
||
| 331 | ':trap_id' => $trapId, |
||
| 332 | ':object_id' => $this->dbOidIndex[$this->oidDesc['oid']]['id'], |
||
| 333 | ); |
||
| 334 | |||
| 335 | if ($sqlQuery->execute($sqlParam) === false) { |
||
| 336 | $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$this->oidDesc['oid']]['id'] ,1,''); |
||
| 337 | } |
||
| 338 | } |
||
| 339 | if ($check_existing === true) |
||
| 340 | { |
||
| 341 | // TODO : remove link trap - objects that wasn't marked. |
||
| 342 | } |
||
| 343 | |||
| 344 | } |
||
| 345 | |||
| 346 | private function reset_oidDesc() |
||
| 347 | { |
||
| 348 | $this->oidDesc['oid']=null; |
||
| 349 | $this->oidDesc['name']=null; |
||
| 350 | $this->oidDesc['type']=null; |
||
| 351 | $this->oidDesc['mib']=null; |
||
| 352 | $this->oidDesc['textconv']=null; |
||
| 353 | $this->oidDesc['dispHint'] =null; |
||
| 354 | $this->oidDesc['syntax']=null; |
||
| 355 | $this->oidDesc['type_enum']=null; |
||
| 356 | $this->oidDesc['description']=null; |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Fills $this->objectsAll with all mibs from snmptranslate |
||
| 361 | * @return integer : number of elements |
||
| 362 | */ |
||
| 363 | private function load_mibs_snmptranslate() |
||
| 364 | { |
||
| 365 | $retVal=0; |
||
| 366 | // Get all mib objects from all mibs |
||
| 367 | $snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.' -On -Tto 2>/dev/null'; |
||
| 368 | $this->logging->log('Getting all traps : '.$snmpCommand,DEBUG ); |
||
| 369 | unset($this->objectsAll); |
||
| 370 | exec($snmpCommand,$this->objectsAll,$retVal); |
||
| 371 | if ($retVal!=0) |
||
| 372 | { |
||
| 373 | $this->logging->log('error executing snmptranslate',ERROR,''); |
||
| 374 | } |
||
| 375 | // Count elements to show progress |
||
| 376 | $numElements=count($this->objectsAll); |
||
| 377 | $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO ); |
||
| 378 | return $numElements; |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * load all mib objects db in dbOidAll (raw) and index in dbOidIndex |
||
| 383 | */ |
||
| 384 | private function load_mibs_from_db() |
||
| 385 | { |
||
| 386 | // Get all mibs from databse to have a memory index |
||
| 387 | |||
| 388 | $db_conn=$this->trapsDB->db_connect_trap(); |
||
| 389 | |||
| 390 | $sql='SELECT * from '.$this->trapsDB->dbPrefix.'mib_cache;'; |
||
| 391 | $this->logging->log('SQL query : '.$sql,DEBUG ); |
||
| 392 | if (($ret_code=$db_conn->query($sql)) === false) { |
||
| 393 | $this->logging->log('No result in query : ' . $sql,ERROR,''); |
||
| 394 | } |
||
| 395 | $this->dbOidAll=$ret_code->fetchAll(); |
||
| 396 | $this->dbOidIndex=array(); |
||
| 397 | // Create the index for db; |
||
| 398 | foreach($this->dbOidAll as $key=>$val) |
||
| 399 | { |
||
| 400 | $this->dbOidIndex[$val['oid']]['key']=$key; |
||
| 401 | $this->dbOidIndex[$val['oid']]['id']=$val['id']; |
||
| 402 | } |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Reset all update timers & count to zero |
||
| 407 | */ |
||
| 408 | private function reset_update_timers() |
||
| 409 | { |
||
| 410 | $this->timing['base_parse_time']=0; |
||
| 411 | $this->timing['base_check_time']=0; |
||
| 412 | $this->timing['type0_check_time']=0; |
||
| 413 | $this->timing['nottrap_time']=0; |
||
| 414 | $this->timing['update_time']=0; |
||
| 415 | $this->timing['objects_time']=0; |
||
| 416 | $this->timing['base_parse_num']=0; |
||
| 417 | $this->timing['base_check_num']=0; |
||
| 418 | $this->timing['type0_check_num']=0; |
||
| 419 | $this->timing['nottrap_num']=0; |
||
| 420 | $this->timing['update_num']=0; |
||
| 421 | $this->timing['objects_num']=0; |
||
| 422 | $this->timing['num_traps']=0; |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Detect if $this->objectsAll[$curElement] is a trap |
||
| 427 | * @param integer $curElement |
||
| 428 | * @param bool $onlyTraps : set to false to get all and not only traps. |
||
| 429 | * @return boolean : false if it's a trap , true if not |
||
| 430 | */ |
||
| 431 | private function detect_trap($curElement,$onlyTraps) |
||
| 432 | { |
||
| 433 | // Get oid or pass if not found |
||
| 434 | if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement])) |
||
| 435 | { |
||
| 436 | $this->timing['base_parse_time'] += microtime(true) - $this->timing['base_time']; |
||
| 437 | $this->timing['base_parse_num'] ++; |
||
| 438 | return true; |
||
| 439 | } |
||
| 440 | $this->oidDesc['oid']=$this->objectsAll[$curElement]; |
||
| 441 | |||
| 442 | // get next line |
||
| 443 | $curElement++; |
||
| 444 | $match=$snmptrans=array(); |
||
| 445 | if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/', |
||
| 446 | $this->objectsAll[$curElement],$match)) |
||
| 447 | { |
||
| 448 | $this->timing['base_check_time'] += microtime(true) - $this->timing['base_time']; |
||
| 449 | $this->timing['base_check_num']++; |
||
| 450 | return true; |
||
| 451 | } |
||
| 452 | |||
| 453 | $this->oidDesc['name']=$match[1]; // Name |
||
| 454 | $this->oidDesc['type']=$match[2]; // type (21=trap, 0: may be trap, else : not trap |
||
| 455 | |||
| 456 | if ($this->oidDesc['type']==0) // object type=0 : check if v1 trap |
||
| 457 | { |
||
| 458 | // Check if next is suboid -> in that case is cannot be a trap |
||
| 459 | if (preg_match("/^".$this->oidDesc['oid']."/",$this->objectsAll[$curElement+1])) |
||
| 460 | { |
||
| 461 | $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time']; |
||
| 462 | $this->timing['type0_check_num']++; |
||
| 463 | return true; |
||
| 464 | } |
||
| 465 | unset($snmptrans); |
||
| 466 | $retVal=0; |
||
| 467 | exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
||
| 468 | ' -Td '.$this->oidDesc['oid'] . ' | grep OBJECTS ',$snmptrans,$retVal); |
||
| 469 | if ($retVal!=0) |
||
| 470 | { |
||
| 471 | $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time']; |
||
| 472 | $this->timing['type0_check_num']++; |
||
| 473 | return true; |
||
| 474 | } |
||
| 475 | //echo "\n v1 trap found : $this->oidDesc['oid'] \n"; |
||
| 476 | // Force as trap. |
||
| 477 | $this->oidDesc['type']=21; |
||
| 478 | } |
||
| 479 | if ($onlyTraps===true && $this->oidDesc['type']!=21) // if only traps and not a trap, continue |
||
| 480 | { |
||
| 481 | $this->timing['nottrap_time'] += microtime(true) - $this->timing['base_time']; |
||
| 482 | $this->timing['nottrap_num']++; |
||
| 483 | return true; |
||
| 484 | } |
||
| 485 | return false; |
||
| 486 | } |
||
| 487 | |||
| 488 | private function get_trap_mib_description() |
||
| 523 | } |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Get trap objects |
||
| 527 | * @param string $snmptrans : output of snmptranslate for TrapModuleConfig |
||
| 528 | * @return array|null : array of objects or null if not found |
||
| 529 | **/ |
||
| 530 | private function get_trap_objects($snmptrans) |
||
| 531 | { |
||
| 532 | $objectName=null; |
||
| 533 | $match=array(); |
||
| 534 | foreach ($snmptrans as $line) |
||
| 535 | { |
||
| 536 | if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match)) |
||
| 537 | { |
||
| 538 | $objectName=$match[1]; |
||
| 539 | } |
||
| 540 | } |
||
| 541 | if ($objectName == null) |
||
| 542 | { |
||
| 543 | $this->logging->log('No objects for ' . $this->oidDesc['oid'],DEBUG); |
||
| 544 | $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
||
| 545 | return null; |
||
| 546 | } |
||
| 547 | |||
| 548 | $trapObjects=array(); |
||
| 549 | while (preg_match('/ *([^ ,]+) *,* */',$objectName,$match)) |
||
| 550 | { |
||
| 551 | array_push($trapObjects,$match[1]); |
||
| 552 | $objectName=preg_replace('/'.$match[0].'/','',$objectName); |
||
| 553 | } |
||
| 554 | return $trapObjects; |
||
| 555 | } |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Cache mib in database |
||
| 559 | * @param boolean $display_progress : Display progress on standard output |
||
| 560 | * @param boolean $check_change : Force check of trap params & objects |
||
| 561 | * @param boolean $onlyTraps : only cache traps and objects (true) or all (false) |
||
| 562 | * @param string $startOID : only cache under startOID (NOT IMPLEMENTED) |
||
| 563 | */ |
||
| 564 | public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1') |
||
| 653 | } |
||
| 654 | } |
||
| 655 | |||
| 656 | |||
| 657 | } |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.