| Total Complexity | 88 |
| Total Lines | 563 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| 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) |
||
| 35 | { |
||
| 36 | $this->logging=$logClass; |
||
| 37 | $this->trapsDB=$dbClass; |
||
| 38 | $this->snmptranslate=$snmptrans; |
||
| 39 | $this->snmptranslate_dirs=$snmptransdir; |
||
| 40 | |||
| 41 | } |
||
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * Update or add an OID to database uses $this->dbOidIndex for mem cache |
||
| 46 | * @param string $oid |
||
| 47 | * @param string $mib |
||
| 48 | * @param string $name |
||
| 49 | * @param string $type |
||
| 50 | * @param string $textConv |
||
| 51 | * @param string $dispHint |
||
| 52 | * @param string $syntax |
||
| 53 | * @param string $type_enum |
||
| 54 | * @param string $description |
||
| 55 | * @return number : 0=unchanged, 1 = changed, 2=created |
||
| 56 | */ |
||
| 57 | public function update_oid($oid,$mib,$name,$type,$textConv,$dispHint,$syntax,$type_enum,$description=NULL) |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * create or update (with check_existing = true) objects of trap |
||
| 171 | * @param string $trapOID : trap oid |
||
| 172 | * @param string $trapmib : mib of trap |
||
| 173 | * @param array $objects : array of objects name (without MIB) |
||
| 174 | * @param bool $check_existing : check instead of create |
||
| 175 | */ |
||
| 176 | public function trap_objects($trapOID,$trapmib,$objects,$check_existing) |
||
| 177 | { |
||
| 178 | $dbObjects=null; // cache of objects for trap in db |
||
| 179 | $db_conn=$this->trapsDB->db_connect_trap(); |
||
| 180 | |||
| 181 | // Get id of trapmib. |
||
| 182 | |||
| 183 | $trapId = $this->dbOidIndex[$trapOID]['id']; |
||
| 184 | if ($check_existing === true) |
||
| 185 | { |
||
| 186 | // Get all objects |
||
| 187 | $sql='SELECT * FROM '.$this->trapsDB->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
||
| 188 | $this->logging->log('SQL query get all traps: '.$sql,DEBUG ); |
||
| 189 | if (($ret_code=$db_conn->query($sql)) === false) { |
||
| 190 | $this->logging->log('No result in query : ' . $sql,1,''); |
||
| 191 | } |
||
| 192 | $dbObjectsRaw=$ret_code->fetchAll(); |
||
| 193 | |||
| 194 | foreach ($dbObjectsRaw as $val) |
||
| 195 | { |
||
| 196 | $dbObjects[$val['object_id']]=1; |
||
| 197 | } |
||
| 198 | } |
||
| 199 | foreach ($objects as $object) |
||
| 200 | { |
||
| 201 | $match=$snmptrans=array(); |
||
| 202 | $retVal=0; |
||
| 203 | $objOid=$objTc=$objDispHint=$objSyntax=$objDesc=$objEnum=NULL; |
||
| 204 | $tmpdesc='';$indesc=false; |
||
| 205 | |||
| 206 | $objMib=$trapmib; |
||
| 207 | exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
||
| 208 | ' -On -Td '.$objMib.'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
||
| 209 | if ($retVal!=0) |
||
| 210 | { |
||
| 211 | // Maybe not trap mib, search with IR |
||
| 212 | exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
||
| 213 | ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
||
| 214 | if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
||
| 215 | { // Not found -> continue with warning |
||
| 216 | $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
||
| 217 | continue; |
||
| 218 | } |
||
| 219 | $objMib=$match[1]; |
||
| 220 | |||
| 221 | // Do the snmptranslate again. |
||
| 222 | exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
||
| 223 | ' -On -Td '.$objMib.'::'.$object,$snmptrans,$retVal); |
||
| 224 | if ($retVal!=0) { |
||
| 225 | $this->logging->log('Error finding trap object : '.$objMib.'::'.$object,2,''); |
||
| 226 | } |
||
| 227 | |||
| 228 | } |
||
| 229 | foreach ($snmptrans as $line) |
||
| 230 | { |
||
| 231 | if ($indesc===true) |
||
| 232 | { |
||
| 233 | $line=preg_replace('/[\t ]+/',' ',$line); |
||
| 234 | if (preg_match('/(.*)"$/', $line,$match)) |
||
| 235 | { |
||
| 236 | $objDesc = $tmpdesc . $match[1]; |
||
| 237 | $indesc=false; |
||
| 238 | } |
||
| 239 | $tmpdesc.=$line; |
||
| 240 | continue; |
||
| 241 | } |
||
| 242 | if (preg_match('/^\.[0-9\.]+$/', $line)) |
||
| 243 | { |
||
| 244 | $objOid=$line; |
||
| 245 | continue; |
||
| 246 | } |
||
| 247 | if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match)) |
||
| 248 | { |
||
| 249 | $objSyntax=$match[1]; |
||
| 250 | $objEnum=$match[2]; |
||
| 251 | continue; |
||
| 252 | } |
||
| 253 | if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match)) |
||
| 254 | { |
||
| 255 | $objSyntax=$match[1]; |
||
| 256 | continue; |
||
| 257 | } |
||
| 258 | if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match)) |
||
| 259 | { |
||
| 260 | $objDispHint=$match[1]; |
||
| 261 | continue; |
||
| 262 | } |
||
| 263 | if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match)) |
||
| 264 | { |
||
| 265 | $objDesc=$match[1]; |
||
| 266 | continue; |
||
| 267 | } |
||
| 268 | if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match)) |
||
| 269 | { |
||
| 270 | $tmpdesc=$match[1]; |
||
| 271 | $indesc=true; |
||
| 272 | continue; |
||
| 273 | } |
||
| 274 | if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match)) |
||
| 275 | { |
||
| 276 | $objTc=$match[1]; |
||
| 277 | continue; |
||
| 278 | } |
||
| 279 | } |
||
| 280 | $this->logging->log("Adding trap $object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc",DEBUG ); |
||
| 281 | //echo "$object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc / $objDesc\n"; |
||
| 282 | // Update |
||
| 283 | $this->update_oid($objOid, $objMib, $object, '3', $objTc, $objDispHint, $objSyntax, $objEnum,$objDesc); |
||
| 284 | |||
| 285 | if (isset($dbObjects[$this->dbOidIndex[$objOid]['id']])) |
||
| 286 | { // if link exists, continue |
||
| 287 | $dbObjects[$this->dbOidIndex[$objOid]['id']]=2; |
||
| 288 | continue; |
||
| 289 | } |
||
| 290 | if ($check_existing === true) |
||
| 291 | { |
||
| 292 | // TODO : check link trap - objects exists, mark them. |
||
| 293 | } |
||
| 294 | // Associate in object table |
||
| 295 | $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache_trap_object (trap_id,object_id) '. |
||
| 296 | 'values (:trap_id, :object_id)'; |
||
| 297 | $sqlQuery=$db_conn->prepare($sql); |
||
| 298 | $sqlParam=array( |
||
| 299 | ':trap_id' => $trapId, |
||
| 300 | ':object_id' => $this->dbOidIndex[$objOid]['id'], |
||
| 301 | ); |
||
| 302 | |||
| 303 | if ($sqlQuery->execute($sqlParam) === false) { |
||
| 304 | $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$objOid]['id'] ,1,''); |
||
| 305 | } |
||
| 306 | } |
||
| 307 | if ($check_existing === true) |
||
| 308 | { |
||
| 309 | // TODO : remove link trap - objects that wasn't marked. |
||
| 310 | } |
||
| 311 | |||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Fills $this->objectsAll with all mibs from snmptranslate |
||
| 316 | * @return integer : number of elements |
||
| 317 | */ |
||
| 318 | private function load_mibs_snmptranslate() |
||
| 319 | { |
||
| 320 | $retVal=0; |
||
| 321 | // Get all mib objects from all mibs |
||
| 322 | $snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.' -On -Tto 2>/dev/null'; |
||
| 323 | $this->logging->log('Getting all traps : '.$snmpCommand,DEBUG ); |
||
| 324 | unset($this->objectsAll); |
||
| 325 | exec($snmpCommand,$this->objectsAll,$retVal); |
||
| 326 | if ($retVal!=0) |
||
| 327 | { |
||
| 328 | $this->logging->log('error executing snmptranslate',ERROR,''); |
||
| 329 | } |
||
| 330 | // Count elements to show progress |
||
| 331 | $numElements=count($this->objectsAll); |
||
| 332 | $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO ); |
||
| 333 | return $numElements; |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * load all mib objects db in dbOidAll (raw) and index in dbOidIndex |
||
| 338 | */ |
||
| 339 | private function load_mibs_from_db() |
||
| 340 | { |
||
| 341 | // Get all mibs from databse to have a memory index |
||
| 342 | |||
| 343 | $db_conn=$this->trapsDB->db_connect_trap(); |
||
| 344 | |||
| 345 | $sql='SELECT * from '.$this->trapsDB->dbPrefix.'mib_cache;'; |
||
| 346 | $this->logging->log('SQL query : '.$sql,DEBUG ); |
||
| 347 | if (($ret_code=$db_conn->query($sql)) === false) { |
||
| 348 | $this->logging->log('No result in query : ' . $sql,ERROR,''); |
||
| 349 | } |
||
| 350 | $this->dbOidAll=$ret_code->fetchAll(); |
||
| 351 | $this->dbOidIndex=array(); |
||
| 352 | // Create the index for db; |
||
| 353 | foreach($this->dbOidAll as $key=>$val) |
||
| 354 | { |
||
| 355 | $this->dbOidIndex[$val['oid']]['key']=$key; |
||
| 356 | $this->dbOidIndex[$val['oid']]['id']=$val['id']; |
||
| 357 | } |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Reset all update timers & count to zero |
||
| 362 | */ |
||
| 363 | private function reset_update_timers() |
||
| 364 | { |
||
| 365 | $this->timing['base_parse_time']=$this->timing['base_check_time']=0; |
||
| 366 | $this->timing['type0_check_time']=$this->timing['nottrap_time']=0; |
||
| 367 | $this->timing['update_time']=$this->timing['objects_time']=0; |
||
| 368 | $this->timing['base_parse_num']=$this->timing['base_check_num']=0; |
||
| 369 | $this->timing['type0_check_num']=$this->timing['nottrap_num']=0; |
||
| 370 | $this->timing['update_num']=$this->timing['objects_num']=0; |
||
| 371 | $this->timing['num_traps']=0; |
||
| 372 | } |
||
| 373 | |||
| 374 | private function detect_trap($curElement,$onlyTraps,&$name,&$type,&$oid) |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Cache mib in database |
||
| 433 | * @param boolean $display_progress : Display progress on standard output |
||
| 434 | * @param boolean $check_change : Force check of trap params & objects |
||
| 435 | * @param boolean $onlyTraps : only cache traps and objects (true) or all (false) |
||
| 436 | * @param string $startOID : only cache under startOID (NOT IMPLEMENTED) |
||
| 437 | */ |
||
| 438 | public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1') |
||
| 573 | } |
||
| 574 | |||
| 575 | } |
||
| 576 | |||
| 577 | |||
| 578 | } |