| Total Complexity | 82 |
| Total Lines | 576 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | 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 |
||
| 10 | class Mib |
||
| 11 | { |
||
| 12 | |||
| 13 | protected $logging; //< logging class |
||
| 14 | protected $trapsDB; //< Database class |
||
| 15 | |||
| 16 | public $snmptranslate; |
||
| 17 | public $snmptranslate_dirs; |
||
| 18 | |||
| 19 | private $dbOidAll; //< All oid in database; |
||
| 20 | private $dbOidIndex; //< Index of oid in dbOidAll |
||
| 21 | private $objectsAll; //< output lines of snmptranslate list |
||
| 22 | private $trapObjectsIndex; //< array of traps objects (as OID) |
||
| 23 | |||
| 24 | private $oidDesc=array(); //< $oid,$mib,$name,$type,$textConv,$dispHint,$syntax,$type_enum,$description=NULL |
||
| 25 | |||
| 26 | // Timing vars for update |
||
| 27 | private $timing=array(); |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Setup Mib Class |
||
| 31 | * @param Logging $logClass : where to log |
||
| 32 | * @param Database $dbClass : Database |
||
| 33 | */ |
||
| 34 | function __construct($logClass,$dbClass,$snmptrans,$snmptransdir) |
||
| 40 | |||
| 41 | } |
||
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * Update or add an OID to database uses $this->dbOidIndex for mem cache |
||
| 46 | * and $this->oidDesc doe data |
||
| 47 | * @return number : 0=unchanged, 1 = changed, 2=created |
||
| 48 | */ |
||
| 49 | public function update_oid() |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * create or update (with check_existing = true) objects of trap |
||
| 160 | * @param string $trapOID : trap oid |
||
| 161 | * @param string $trapmib : mib of trap |
||
| 162 | * @param array $objects : array of objects name (without MIB) |
||
| 163 | * @param bool $check_existing : check instead of create |
||
| 164 | */ |
||
| 165 | public function trap_objects($trapOID,$trapmib,$objects,$check_existing) |
||
| 166 | { |
||
| 167 | $dbObjects=null; // cache of objects for trap in db |
||
| 168 | $db_conn=$this->trapsDB->db_connect_trap(); |
||
| 169 | |||
| 170 | // Get id of trapmib. |
||
| 171 | |||
| 172 | $trapId = $this->dbOidIndex[$trapOID]['id']; |
||
| 173 | if ($check_existing === true) |
||
| 174 | { |
||
| 175 | // Get all objects |
||
| 176 | $sql='SELECT * FROM '.$this->trapsDB->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
||
| 177 | $this->logging->log('SQL query get all traps: '.$sql,DEBUG ); |
||
| 178 | if (($ret_code=$db_conn->query($sql)) === false) { |
||
| 179 | $this->logging->log('No result in query : ' . $sql,1,''); |
||
| 180 | } |
||
| 181 | $dbObjectsRaw=$ret_code->fetchAll(); |
||
| 182 | |||
| 183 | foreach ($dbObjectsRaw as $val) |
||
| 184 | { |
||
| 185 | $dbObjects[$val['object_id']]=1; |
||
| 186 | } |
||
| 187 | } |
||
| 188 | foreach ($objects as $object) |
||
| 189 | { |
||
| 190 | $match=$snmptrans=array(); |
||
| 191 | $retVal=0; |
||
| 192 | |||
| 193 | $this->reset_oidDesc(); |
||
| 194 | |||
| 195 | |||
| 196 | $tmpdesc=''; // For multiline description |
||
| 197 | $indesc=false; // true if currently inside multiline description |
||
| 198 | |||
| 199 | $this->oidDesc['mib']=$trapmib; |
||
| 200 | exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
||
| 201 | ' -On -Td '.$this->oidDesc['mib'].'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
||
| 202 | if ($retVal!=0) |
||
| 203 | { |
||
| 204 | // Maybe not trap mib, search with IR |
||
| 205 | exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
||
| 206 | ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
||
| 207 | if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
||
| 208 | { // Not found -> continue with warning |
||
| 209 | $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
||
| 210 | continue; |
||
| 211 | } |
||
| 212 | $this->oidDesc['mib']=$match[1]; |
||
| 213 | |||
| 214 | // Do the snmptranslate again. |
||
| 215 | exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
||
| 216 | ' -On -Td '.$this->oidDesc['mib'].'::'.$object,$snmptrans,$retVal); |
||
| 217 | if ($retVal!=0) { |
||
| 218 | $this->logging->log('Error finding trap object : '.$this->oidDesc['mib'].'::'.$object,2,''); |
||
| 219 | } |
||
| 220 | |||
| 221 | } |
||
| 222 | foreach ($snmptrans as $line) |
||
| 223 | { |
||
| 224 | if ($indesc===true) |
||
| 225 | { |
||
| 226 | $line=preg_replace('/[\t ]+/',' ',$line); |
||
| 227 | if (preg_match('/(.*)"$/', $line,$match)) |
||
| 228 | { |
||
| 229 | $this->oidDesc['description'] = $tmpdesc . $match[1]; |
||
| 230 | $indesc=false; |
||
| 231 | } |
||
| 232 | $tmpdesc.=$line; |
||
| 233 | continue; |
||
| 234 | } |
||
| 235 | if (preg_match('/^\.[0-9\.]+$/', $line)) |
||
| 236 | { |
||
| 237 | $this->oidDesc['oid']=$line; |
||
| 238 | continue; |
||
| 239 | } |
||
| 240 | if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match)) |
||
| 241 | { |
||
| 242 | $this->oidDesc['syntax']=$match[1]; |
||
| 243 | $this->oidDesc['type_enum']=$match[2]; |
||
| 244 | continue; |
||
| 245 | } |
||
| 246 | if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match)) |
||
| 247 | { |
||
| 248 | $this->oidDesc['syntax']=$match[1]; |
||
| 249 | continue; |
||
| 250 | } |
||
| 251 | if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match)) |
||
| 252 | { |
||
| 253 | $this->oidDesc['dispHint']=$match[1]; |
||
| 254 | continue; |
||
| 255 | } |
||
| 256 | if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match)) |
||
| 257 | { |
||
| 258 | $this->oidDesc['description']=$match[1]; |
||
| 259 | continue; |
||
| 260 | } |
||
| 261 | if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match)) |
||
| 262 | { |
||
| 263 | $tmpdesc=$match[1]; |
||
| 264 | $indesc=true; |
||
| 265 | continue; |
||
| 266 | } |
||
| 267 | if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match)) |
||
| 268 | { |
||
| 269 | $this->oidDesc['textconv']=$match[1]; |
||
| 270 | continue; |
||
| 271 | } |
||
| 272 | } |
||
| 273 | $this->oidDesc['name'] = $object; |
||
| 274 | $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 ); |
||
| 275 | //echo "$this->oidDesc['name'] : $this->oidDesc['oid'] / $this->oidDesc['syntax'] / $this->oidDesc['type_enum'] / $this->oidDesc['dispHint'] / $this->oidDesc['textconv'] / $this->oidDesc['description']\n"; |
||
| 276 | // Update |
||
| 277 | $this->update_oid(); |
||
| 278 | |||
| 279 | if (isset($dbObjects[$this->dbOidIndex[$this->oidDesc['oid']]['id']])) |
||
| 280 | { // if link exists, continue |
||
| 281 | $dbObjects[$this->dbOidIndex[$this->oidDesc['oid']]['id']]=2; |
||
| 282 | continue; |
||
| 283 | } |
||
| 284 | if ($check_existing === true) |
||
| 285 | { |
||
| 286 | // TODO : check link trap - objects exists, mark them. |
||
| 287 | } |
||
| 288 | // Associate in object table |
||
| 289 | $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache_trap_object (trap_id,object_id) '. |
||
| 290 | 'values (:trap_id, :object_id)'; |
||
| 291 | $sqlQuery=$db_conn->prepare($sql); |
||
| 292 | $sqlParam=array( |
||
| 293 | ':trap_id' => $trapId, |
||
| 294 | ':object_id' => $this->dbOidIndex[$this->oidDesc['oid']]['id'], |
||
| 295 | ); |
||
| 296 | |||
| 297 | if ($sqlQuery->execute($sqlParam) === false) { |
||
| 298 | $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$this->oidDesc['oid']]['id'] ,1,''); |
||
| 299 | } |
||
| 300 | } |
||
| 301 | if ($check_existing === true) |
||
| 302 | { |
||
| 303 | // TODO : remove link trap - objects that wasn't marked. |
||
| 304 | } |
||
| 305 | |||
| 306 | } |
||
| 307 | |||
| 308 | private function reset_oidDesc() |
||
| 309 | { |
||
| 310 | $this->oidDesc['oid']=null; |
||
| 311 | $this->oidDesc['name']=null; |
||
| 312 | $this->oidDesc['type']=null; |
||
| 313 | $this->oidDesc['mib']=null; |
||
| 314 | $this->oidDesc['textconv']=null; |
||
| 315 | $this->oidDesc['dispHint'] =null; |
||
| 316 | $this->oidDesc['syntax']=null; |
||
| 317 | $this->oidDesc['type_enum']=null; |
||
| 318 | $this->oidDesc['description']=null; |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Fills $this->objectsAll with all mibs from snmptranslate |
||
| 323 | * @return integer : number of elements |
||
| 324 | */ |
||
| 325 | private function load_mibs_snmptranslate() |
||
| 326 | { |
||
| 327 | $retVal=0; |
||
| 328 | // Get all mib objects from all mibs |
||
| 329 | $snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.' -On -Tto 2>/dev/null'; |
||
| 330 | $this->logging->log('Getting all traps : '.$snmpCommand,DEBUG ); |
||
| 331 | unset($this->objectsAll); |
||
| 332 | exec($snmpCommand,$this->objectsAll,$retVal); |
||
| 333 | if ($retVal!=0) |
||
| 334 | { |
||
| 335 | $this->logging->log('error executing snmptranslate',ERROR,''); |
||
| 336 | } |
||
| 337 | // Count elements to show progress |
||
| 338 | $numElements=count($this->objectsAll); |
||
| 339 | $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO ); |
||
| 340 | return $numElements; |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * load all mib objects db in dbOidAll (raw) and index in dbOidIndex |
||
| 345 | */ |
||
| 346 | private function load_mibs_from_db() |
||
| 347 | { |
||
| 348 | // Get all mibs from databse to have a memory index |
||
| 349 | |||
| 350 | $db_conn=$this->trapsDB->db_connect_trap(); |
||
| 351 | |||
| 352 | $sql='SELECT * from '.$this->trapsDB->dbPrefix.'mib_cache;'; |
||
| 353 | $this->logging->log('SQL query : '.$sql,DEBUG ); |
||
| 354 | if (($ret_code=$db_conn->query($sql)) === false) { |
||
| 355 | $this->logging->log('No result in query : ' . $sql,ERROR,''); |
||
| 356 | } |
||
| 357 | $this->dbOidAll=$ret_code->fetchAll(); |
||
| 358 | $this->dbOidIndex=array(); |
||
| 359 | // Create the index for db; |
||
| 360 | foreach($this->dbOidAll as $key=>$val) |
||
| 361 | { |
||
| 362 | $this->dbOidIndex[$val['oid']]['key']=$key; |
||
| 363 | $this->dbOidIndex[$val['oid']]['id']=$val['id']; |
||
| 364 | } |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Reset all update timers & count to zero |
||
| 369 | */ |
||
| 370 | private function reset_update_timers() |
||
| 371 | { |
||
| 372 | $this->timing['base_parse_time']=0; |
||
| 373 | $this->timing['base_check_time']=0; |
||
| 374 | $this->timing['type0_check_time']=0; |
||
| 375 | $this->timing['nottrap_time']=0; |
||
| 376 | $this->timing['update_time']=0; |
||
| 377 | $this->timing['objects_time']=0; |
||
| 378 | $this->timing['base_parse_num']=0; |
||
| 379 | $this->timing['base_check_num']=0; |
||
| 380 | $this->timing['type0_check_num']=0; |
||
| 381 | $this->timing['nottrap_num']=0; |
||
| 382 | $this->timing['update_num']=0; |
||
| 383 | $this->timing['objects_num']=0; |
||
| 384 | $this->timing['num_traps']=0; |
||
| 385 | } |
||
| 386 | |||
| 387 | private function detect_trap($curElement,$onlyTraps) |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Cache mib in database |
||
| 446 | * @param boolean $display_progress : Display progress on standard output |
||
| 447 | * @param boolean $check_change : Force check of trap params & objects |
||
| 448 | * @param boolean $onlyTraps : only cache traps and objects (true) or all (false) |
||
| 449 | * @param string $startOID : only cache under startOID (NOT IMPLEMENTED) |
||
| 450 | */ |
||
| 451 | public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1') |
||
| 586 | } |
||
| 587 | } |
||
| 588 | |||
| 589 | |||
| 590 | } |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.