Passed
Push — master ( d03ea5...4fd4ae )
by Patrick
02:40
created
library/Trapdirector/TrapsProcess/Mib.php 1 patch
Spacing   +116 added lines, -116 removed lines patch added patch discarded remove patch
@@ -32,7 +32,7 @@  discard block
 block discarded – undo
32 32
      * @param Logging $logClass : where to log
33 33
      * @param Database $dbClass : Database
34 34
      */
35
-    function __construct($logClass,$dbClass,$snmptrans,$snmptransdir)
35
+    function __construct($logClass, $dbClass, $snmptrans, $snmptransdir)
36 36
     {
37 37
         $this->logging=$logClass;
38 38
         $this->trapsDB=$dbClass;
@@ -56,9 +56,9 @@  discard block
 block discarded – undo
56 56
         }
57 57
         $oidIndex=$this->dbOidIndex[$this->oidDesc['oid']]['key']; // Get index in dbOidAll
58 58
         $dbOid=$this->dbOidAll[$oidIndex]; // Get array of element
59
-        if ( $this->oidDesc['name'] != $dbOid['name'] ||
59
+        if ($this->oidDesc['name'] != $dbOid['name'] ||
60 60
             $this->oidDesc['mib'] != $dbOid['mib'] ||
61
-            $this->oidDesc['type'] !=$dbOid['type']
61
+            $this->oidDesc['type'] != $dbOid['type']
62 62
             )
63 63
         { // Do update
64 64
             $sql='UPDATE '.$this->trapsDB->dbPrefix.'mib_cache SET '.
@@ -72,22 +72,22 @@  discard block
 block discarded – undo
72 72
                 ':type' => $this->oidDesc['type'],
73 73
                 ':mib' => $this->oidDesc['mib'],
74 74
                 ':tc' =>  $this->oidDesc['textconv']??'null',
75
-                ':display_hint' => $this->oidDesc['dispHint']??'null' ,
76
-                ':syntax' => $this->oidDesc['syntax']==null??'null',
75
+                ':display_hint' => $this->oidDesc['dispHint']??'null',
76
+                ':syntax' => $this->oidDesc['syntax'] == null??'null',
77 77
                 ':type_enum' => $this->oidDesc['type_enum']??'null',
78 78
                 ':description' => $this->oidDesc['description']??'null',
79 79
                 ':id' => $this->dbOidAll[$this->dbOidIndex[$this->oidDesc['oid']]['id']]
80 80
             );
81 81
             
82 82
             if ($sqlQuery->execute($sqlParam) === false) {
83
-                $this->logging->log('Error in query : ' . $sql,ERROR,'');
83
+                $this->logging->log('Error in query : '.$sql, ERROR, '');
84 84
             }
85
-            $this->logging->log('Trap updated : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG );
85
+            $this->logging->log('Trap updated : '.$this->oidDesc['name'].' / OID : '.$this->oidDesc['oid'], DEBUG);
86 86
             return 1;
87 87
         }
88 88
         else
89 89
         {
90
-            $this->logging->log('Trap unchanged : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG );
90
+            $this->logging->log('Trap unchanged : '.$this->oidDesc['name'].' / OID : '.$this->oidDesc['oid'], DEBUG);
91 91
             return 0;
92 92
         }
93 93
     }
@@ -103,11 +103,11 @@  discard block
 block discarded – undo
103 103
         $db_conn=$this->trapsDB->db_connect_trap();
104 104
         $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache '.
105 105
             '(oid, name, type , mib, textual_convention, display_hint '.
106
-            ', syntax, type_enum , description ) ' .
106
+            ', syntax, type_enum , description ) '.
107 107
             'values (:oid, :name , :type ,:mib ,:tc , :display_hint'.
108 108
             ', :syntax, :type_enum, :description )';
109 109
         
110
-        if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id';
110
+        if ($this->trapsDB->trapDBType == 'pgsql') $sql.='RETURNING id';
111 111
         
112 112
         $sqlQuery=$db_conn->prepare($sql);
113 113
         
@@ -116,15 +116,15 @@  discard block
 block discarded – undo
116 116
             ':name' => $this->oidDesc['name'],
117 117
             ':type' => $this->oidDesc['type'],
118 118
             ':mib' => $this->oidDesc['mib'],
119
-            ':tc' =>  ($this->oidDesc['textconv']==null)?'null':$this->oidDesc['textconv'] ,
120
-            ':display_hint' => ($this->oidDesc['dispHint']==null)?'null':$this->oidDesc['dispHint'] ,
121
-            ':syntax' => ($this->oidDesc['syntax']==null)?'null':$this->oidDesc['syntax'],
122
-            ':type_enum' => ($this->oidDesc['type_enum']==null)?'null':$this->oidDesc['type_enum'],
123
-            ':description' => ($this->oidDesc['description']==null)?'null':$this->oidDesc['description']
119
+            ':tc' =>  ($this->oidDesc['textconv'] == null) ? 'null' : $this->oidDesc['textconv'],
120
+            ':display_hint' => ($this->oidDesc['dispHint'] == null) ? 'null' : $this->oidDesc['dispHint'],
121
+            ':syntax' => ($this->oidDesc['syntax'] == null) ? 'null' : $this->oidDesc['syntax'],
122
+            ':type_enum' => ($this->oidDesc['type_enum'] == null) ? 'null' : $this->oidDesc['type_enum'],
123
+            ':description' => ($this->oidDesc['description'] == null) ? 'null' : $this->oidDesc['description']
124 124
         );
125 125
         
126 126
         if ($sqlQuery->execute($sqlParam) === false) {
127
-            $this->logging->log('Error in query : ' . $sql,1,'');
127
+            $this->logging->log('Error in query : '.$sql, 1, '');
128 128
         }
129 129
         
130 130
         switch ($this->trapsDB->trapDBType)
@@ -132,10 +132,10 @@  discard block
 block discarded – undo
132 132
             case 'pgsql':
133 133
                 // Get last id to insert oid/values in secondary table
134 134
                 if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) {
135
-                    $this->logging->log('Error getting id - pgsql - ',1,'');
135
+                    $this->logging->log('Error getting id - pgsql - ', 1, '');
136 136
                 }
137
-                if (! isset($inserted_id_ret['id'])) {
138
-                    $this->logging->log('Error getting id - pgsql - empty.',1,'');
137
+                if (!isset($inserted_id_ret['id'])) {
138
+                    $this->logging->log('Error getting id - pgsql - empty.', 1, '');
139 139
                 }
140 140
                 $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id_ret['id'];
141 141
                 break;
@@ -143,15 +143,15 @@  discard block
 block discarded – undo
143 143
                 // Get last id to insert oid/values in secondary table
144 144
                 $sql='SELECT LAST_INSERT_ID();';
145 145
                 if (($ret_code=$db_conn->query($sql)) === false) {
146
-                    $this->logging->log('Erreur getting id - mysql - ',1,'');
146
+                    $this->logging->log('Erreur getting id - mysql - ', 1, '');
147 147
                 }
148 148
                 
149 149
                 $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()'];
150
-                if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
150
+                if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
151 151
                 $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id;
152 152
                 break;
153 153
             default:
154
-                $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType,1,'');
154
+                $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType, 1, '');
155 155
         }
156 156
         
157 157
         // Set as newly created.
@@ -190,9 +190,9 @@  discard block
 block discarded – undo
190 190
         $db_conn=$this->trapsDB->db_connect_trap();
191 191
         // Get all objects
192 192
         $sql='SELECT * FROM '.$this->trapsDB->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';';
193
-        $this->logging->log('SQL query get all traps: '.$sql,DEBUG );
193
+        $this->logging->log('SQL query get all traps: '.$sql, DEBUG);
194 194
         if (($ret_code=$db_conn->query($sql)) === false) {
195
-            $this->logging->log('No result in query : ' . $sql,1,'');
195
+            $this->logging->log('No result in query : '.$sql, 1, '');
196 196
         }
197 197
         $dbObjectsRaw=$ret_code->fetchAll();
198 198
         
@@ -209,30 +209,30 @@  discard block
 block discarded – undo
209 209
  * @param string $trapmib : mib of trap
210 210
  * @return NULL|array : null if not found, or output of snmptranslate
211 211
  */
212
-    private function get_object_details($object,$trapmib)
212
+    private function get_object_details($object, $trapmib)
213 213
     {
214 214
         $match=$snmptrans=array();
215 215
         $retVal=0;
216 216
         $this->oidDesc['mib']=$trapmib;
217
-        exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.
218
-            ' -On -Td '.$this->oidDesc['mib'].'::'.$object . ' 2>/dev/null',$snmptrans,$retVal);
219
-        if ($retVal!=0)
217
+        exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslateDirs.
218
+            ' -On -Td '.$this->oidDesc['mib'].'::'.$object.' 2>/dev/null', $snmptrans, $retVal);
219
+        if ($retVal != 0)
220 220
         {
221 221
             // Maybe not trap mib, search with IR
222
-            exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.
223
-                ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal);
224
-            if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match))
222
+            exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslateDirs.
223
+                ' -IR '.$object.' 2>/dev/null', $snmptrans, $retVal);
224
+            if ($retVal != 0 || !preg_match('/(.*)::(.*)/', $snmptrans[0], $match))
225 225
             { // Not found -> continue with warning
226
-                $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,'');
226
+                $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object, 2, '');
227 227
                 return null;
228 228
             }
229 229
             $this->oidDesc['mib']=$match[1];
230 230
             
231 231
             // Do the snmptranslate again.
232
-            exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.
233
-                ' -On -Td '.$this->oidDesc['mib'].'::'.$object,$snmptrans,$retVal);
234
-            if ($retVal!=0) {
235
-                $this->logging->log('Error finding trap object : '.$this->oidDesc['mib'].'::'.$object,2,'');
232
+            exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslateDirs.
233
+                ' -On -Td '.$this->oidDesc['mib'].'::'.$object, $snmptrans, $retVal);
234
+            if ($retVal != 0) {
235
+                $this->logging->log('Error finding trap object : '.$this->oidDesc['mib'].'::'.$object, 2, '');
236 236
                 return null;
237 237
             }
238 238
             
@@ -252,12 +252,12 @@  discard block
 block discarded – undo
252 252
         
253 253
         foreach ($snmptrans as $line)
254 254
         {
255
-            if ($indesc===true)
255
+            if ($indesc === true)
256 256
             {
257
-                $line=preg_replace('/[\t ]+/',' ',$line);
258
-                if (preg_match('/(.*)"$/', $line,$match))
257
+                $line=preg_replace('/[\t ]+/', ' ', $line);
258
+                if (preg_match('/(.*)"$/', $line, $match))
259 259
                 {
260
-                    $this->oidDesc['description'] = $tmpdesc . $match[1];
260
+                    $this->oidDesc['description']=$tmpdesc.$match[1];
261 261
                     $indesc=false;
262 262
                 }
263 263
                 $tmpdesc.=$line;
@@ -268,34 +268,34 @@  discard block
 block discarded – undo
268 268
                 $this->oidDesc['oid']=$line;
269 269
                 continue;
270 270
             }
271
-            if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match))
271
+            if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/', $line, $match))
272 272
             {
273 273
                 $this->oidDesc['syntax']=$match[1];
274 274
                 $this->oidDesc['type_enum']=$match[2];
275 275
                 continue;
276 276
             }
277
-            if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match))
277
+            if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/', $line, $match))
278 278
             {
279 279
                 $this->oidDesc['syntax']=$match[1];
280 280
                 continue;
281 281
             }
282
-            if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match))
282
+            if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/', $line, $match))
283 283
             {
284 284
                 $this->oidDesc['dispHint']=$match[1];
285 285
                 continue;
286 286
             }
287
-            if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match))
287
+            if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/', $line, $match))
288 288
             {
289 289
                 $this->oidDesc['description']=$match[1];
290 290
                 continue;
291 291
             }
292
-            if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match))
292
+            if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/', $line, $match))
293 293
             {
294 294
                 $tmpdesc=$match[1];
295 295
                 $indesc=true;
296 296
                 continue;
297 297
             }
298
-            if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match))
298
+            if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/', $line, $match))
299 299
             {
300 300
                 $this->oidDesc['textconv']=$match[1];
301 301
                 continue;
@@ -310,9 +310,9 @@  discard block
 block discarded – undo
310 310
      * @param array $objects : array of objects name (without MIB)
311 311
      * @param bool $check_existing : check instead of create
312 312
      */
313
-    public function trap_objects($trapOID,$trapmib,$objects,$check_existing)
313
+    public function trap_objects($trapOID, $trapmib, $objects, $check_existing)
314 314
     {              
315
-        $trapId = $this->dbOidIndex[$trapOID]['id']; // Get id of trap
315
+        $trapId=$this->dbOidIndex[$trapOID]['id']; // Get id of trap
316 316
         
317 317
         if ($check_existing === true)
318 318
         {
@@ -329,9 +329,9 @@  discard block
 block discarded – undo
329 329
             
330 330
             $this->parse_object($snmptrans);
331 331
 
332
-            $this->oidDesc['name'] = $object;
332
+            $this->oidDesc['name']=$object;
333 333
             
334
-            $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 );
334
+            $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);
335 335
 
336 336
             // Update
337 337
             $this->update_oid();
@@ -356,7 +356,7 @@  discard block
 block discarded – undo
356 356
             );
357 357
             
358 358
             if ($sqlQuery->execute($sqlParam) === false) {
359
-                $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$this->oidDesc['oid']]['id'] ,1,'');
359
+                $this->logging->log('Error adding trap object : '.$sql.' / '.$trapId.'/'.$this->dbOidIndex[$this->oidDesc['oid']]['id'], 1, '');
360 360
             }
361 361
         }
362 362
         if ($check_existing === true)
@@ -373,7 +373,7 @@  discard block
 block discarded – undo
373 373
         $this->oidDesc['type']=null;
374 374
         $this->oidDesc['mib']=null;
375 375
         $this->oidDesc['textconv']=null;
376
-        $this->oidDesc['dispHint'] =null;
376
+        $this->oidDesc['dispHint']=null;
377 377
         $this->oidDesc['syntax']=null;
378 378
         $this->oidDesc['type_enum']=null;
379 379
         $this->oidDesc['description']=null;
@@ -387,17 +387,17 @@  discard block
 block discarded – undo
387 387
     {
388 388
         $retVal=0;
389 389
         // Get all mib objects from all mibs
390
-        $snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.' -On -Tto 2>/dev/null';
391
-        $this->logging->log('Getting all traps : '.$snmpCommand,DEBUG );
390
+        $snmpCommand=$this->snmptranslate.' -m ALL -M +'.$this->snmptranslateDirs.' -On -Tto 2>/dev/null';
391
+        $this->logging->log('Getting all traps : '.$snmpCommand, DEBUG);
392 392
         unset($this->objectsAll);
393
-        exec($snmpCommand,$this->objectsAll,$retVal);
394
-        if ($retVal!=0)
393
+        exec($snmpCommand, $this->objectsAll, $retVal);
394
+        if ($retVal != 0)
395 395
         {
396
-            $this->logging->log('error executing snmptranslate',ERROR,'');
396
+            $this->logging->log('error executing snmptranslate', ERROR, '');
397 397
         }
398 398
         // Count elements to show progress
399 399
         $numElements=count($this->objectsAll);
400
-        $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO );
400
+        $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements, INFO);
401 401
         return $numElements;
402 402
     }
403 403
 
@@ -411,14 +411,14 @@  discard block
 block discarded – undo
411 411
         $db_conn=$this->trapsDB->db_connect_trap();
412 412
         
413 413
         $sql='SELECT * from '.$this->trapsDB->dbPrefix.'mib_cache;';
414
-        $this->logging->log('SQL query : '.$sql,DEBUG );
414
+        $this->logging->log('SQL query : '.$sql, DEBUG);
415 415
         if (($ret_code=$db_conn->query($sql)) === false) {
416
-            $this->logging->log('No result in query : ' . $sql,ERROR,'');
416
+            $this->logging->log('No result in query : '.$sql, ERROR, '');
417 417
         }
418 418
         $this->dbOidAll=$ret_code->fetchAll();
419 419
         $this->dbOidIndex=array();
420 420
         // Create the index for db;
421
-        foreach($this->dbOidAll as $key=>$val)
421
+        foreach ($this->dbOidAll as $key=>$val)
422 422
         {
423 423
             $this->dbOidIndex[$val['oid']]['key']=$key;
424 424
             $this->dbOidIndex[$val['oid']]['id']=$val['id'];
@@ -451,13 +451,13 @@  discard block
 block discarded – undo
451 451
      * @param bool $onlyTraps : set to false to get all and not only traps.
452 452
      * @return boolean : false if it's a trap , true if not
453 453
      */
454
-    private function detect_trap($curElement,$onlyTraps)
454
+    private function detect_trap($curElement, $onlyTraps)
455 455
     {
456 456
         // Get oid or pass if not found
457
-        if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement]))
457
+        if (!preg_match('/^\.[0-9\.]+$/', $this->objectsAll[$curElement]))
458 458
         {
459
-            $this->timing['base_parse_time'] += microtime(true) - $this->timing['base_time'];
460
-            $this->timing['base_parse_num'] ++;
459
+            $this->timing['base_parse_time']+=microtime(true) - $this->timing['base_time'];
460
+            $this->timing['base_parse_num']++;
461 461
             return true;
462 462
         }
463 463
         $this->oidDesc['oid']=$this->objectsAll[$curElement];
@@ -466,9 +466,9 @@  discard block
 block discarded – undo
466 466
         $curElement++;
467 467
         $match=$snmptrans=array();
468 468
         if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/',
469
-            $this->objectsAll[$curElement],$match))
469
+            $this->objectsAll[$curElement], $match))
470 470
         {
471
-            $this->timing['base_check_time'] += microtime(true) - $this->timing['base_time'];
471
+            $this->timing['base_check_time']+=microtime(true) - $this->timing['base_time'];
472 472
             $this->timing['base_check_num']++;
473 473
             return true;
474 474
         }
@@ -476,22 +476,22 @@  discard block
 block discarded – undo
476 476
         $this->oidDesc['name']=$match[1]; // Name
477 477
         $this->oidDesc['type']=$match[2]; // type (21=trap, 0: may be trap, else : not trap
478 478
         
479
-        if ($this->oidDesc['type']==0) // object type=0 : check if v1 trap
479
+        if ($this->oidDesc['type'] == 0) // object type=0 : check if v1 trap
480 480
         {
481 481
             // Check if next is suboid -> in that case is cannot be a trap
482
-            if (preg_match("/^".$this->oidDesc['oid']."/",$this->objectsAll[$curElement+1]))
482
+            if (preg_match("/^".$this->oidDesc['oid']."/", $this->objectsAll[$curElement + 1]))
483 483
             {
484
-                $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time'];
484
+                $this->timing['type0_check_time']+=microtime(true) - $this->timing['base_time'];
485 485
                 $this->timing['type0_check_num']++;
486 486
                 return true;
487 487
             }
488 488
             unset($snmptrans);
489 489
             $retVal=0;
490
-            exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.
491
-                ' -Td '.$this->oidDesc['oid'] . ' | grep OBJECTS ',$snmptrans,$retVal);
492
-            if ($retVal!=0)
490
+            exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslateDirs.
491
+                ' -Td '.$this->oidDesc['oid'].' | grep OBJECTS ', $snmptrans, $retVal);
492
+            if ($retVal != 0)
493 493
             {
494
-                $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time'];
494
+                $this->timing['type0_check_time']+=microtime(true) - $this->timing['base_time'];
495 495
                 $this->timing['type0_check_num']++;
496 496
                 return true;
497 497
             }
@@ -499,9 +499,9 @@  discard block
 block discarded – undo
499 499
             // Force as trap.
500 500
             $this->oidDesc['type']=21;
501 501
         }
502
-        if ($onlyTraps===true && $this->oidDesc['type']!=21) // if only traps and not a trap, continue
502
+        if ($onlyTraps === true && $this->oidDesc['type'] != 21) // if only traps and not a trap, continue
503 503
         {
504
-            $this->timing['nottrap_time'] += microtime(true) - $this->timing['base_time'];
504
+            $this->timing['nottrap_time']+=microtime(true) - $this->timing['base_time'];
505 505
             $this->timing['nottrap_num']++;
506 506
             return true;
507 507
         }
@@ -516,35 +516,35 @@  discard block
 block discarded – undo
516 516
     {
517 517
         $retVal=0;
518 518
         $match=$snmptrans=array();
519
-        exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.
520
-            ' -Td '.$this->oidDesc['oid'],$snmptrans,$retVal);
521
-        if ($retVal!=0)
519
+        exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslateDirs.
520
+            ' -Td '.$this->oidDesc['oid'], $snmptrans, $retVal);
521
+        if ($retVal != 0)
522 522
         {
523
-            $this->logging->log('error executing snmptranslate',ERROR);
523
+            $this->logging->log('error executing snmptranslate', ERROR);
524 524
             return $snmptrans;
525 525
         }
526 526
         
527
-        if (!preg_match('/^(.*)::/',$snmptrans[0],$match))
527
+        if (!preg_match('/^(.*)::/', $snmptrans[0], $match))
528 528
         {
529
-            $this->logging->log('Error getting mib from trap '.$this->oidDesc['oid'].' : ' . $snmptrans[0],ERROR);
529
+            $this->logging->log('Error getting mib from trap '.$this->oidDesc['oid'].' : '.$snmptrans[0], ERROR);
530 530
             return $snmptrans;
531 531
         }
532 532
         $this->oidDesc['mib']=$match[1];
533 533
         
534 534
         $numLine=1;
535
-        while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++;
535
+        while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/', $snmptrans[$numLine], $match)) $numLine++;
536 536
         if (isset($snmptrans[$numLine]))
537 537
         {
538
-            $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]);
538
+            $snmptrans[$numLine]=preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/', '', $snmptrans[$numLine]);
539 539
             
540
-            while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine]))
540
+            while (isset($snmptrans[$numLine]) && !preg_match('/"/', $snmptrans[$numLine]))
541 541
             {
542
-                $this->oidDesc['description'].=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]);
542
+                $this->oidDesc['description'].=preg_replace('/[\t ]+/', ' ', $snmptrans[$numLine]);
543 543
                 $numLine++;
544 544
             }
545 545
             if (isset($snmptrans[$numLine])) {
546
-                $this->oidDesc['description'].=preg_replace('/".*/','',$snmptrans[$numLine]);
547
-                $this->oidDesc['description']=preg_replace('/[\t ]+/',' ',$this->oidDesc['description']);
546
+                $this->oidDesc['description'].=preg_replace('/".*/', '', $snmptrans[$numLine]);
547
+                $this->oidDesc['description']=preg_replace('/[\t ]+/', ' ', $this->oidDesc['description']);
548 548
             }
549 549
             
550 550
         }
@@ -562,23 +562,23 @@  discard block
 block discarded – undo
562 562
         $match=array();
563 563
         foreach ($snmptrans as $line)
564 564
         {
565
-            if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match))
565
+            if (preg_match('/OBJECTS.*\{([^\}]+)\}/', $line, $match))
566 566
             {
567 567
                 $objectName=$match[1];
568 568
             }
569 569
         }
570 570
         if ($objectName == null)
571 571
         {
572
-            $this->logging->log('No objects for ' . $this->oidDesc['oid'],DEBUG);
573
-            $this->timing['objects_time'] += microtime(true) - $this->timing['base_time'];
572
+            $this->logging->log('No objects for '.$this->oidDesc['oid'], DEBUG);
573
+            $this->timing['objects_time']+=microtime(true) - $this->timing['base_time'];
574 574
             return null;
575 575
         }
576 576
         
577 577
         $trapObjects=array();
578
-        while (preg_match('/ *([^ ,]+) *,* */',$objectName,$match))
578
+        while (preg_match('/ *([^ ,]+) *,* */', $objectName, $match))
579 579
         {
580
-            array_push($trapObjects,$match[1]);
581
-            $objectName=preg_replace('/'.$match[0].'/','',$objectName);
580
+            array_push($trapObjects, $match[1]);
581
+            $objectName=preg_replace('/'.$match[0].'/', '', $objectName);
582 582
         }
583 583
         return $trapObjects;
584 584
     }
@@ -590,18 +590,18 @@  discard block
 block discarded – undo
590 590
      * @param boolean $onlyTraps : only cache traps and objects (true) or all (false)
591 591
      * @param string $startOID : only cache under startOID (NOT IMPLEMENTED)
592 592
      */
593
-    public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1')
593
+    public function update_mib_database($display_progress=false, $check_change=false, $onlyTraps=true, $startOID='.1')
594 594
     {
595 595
         // Global Timing
596
-        $timeTaken = microtime(true);
596
+        $timeTaken=microtime(true);
597 597
         
598 598
         $numElements=$this->load_mibs_snmptranslate(); // Load objectsAll
599 599
         
600 600
         $this->load_mibs_from_db(); // Load from db dbOidAll & dbOidIndex
601 601
         
602
-        $step=$basestep=$numElements/10; // output display of % done
602
+        $step=$basestep=$numElements / 10; // output display of % done
603 603
         $num_step=0;
604
-        $timeFiveSec = microtime(true); // Used for display a '.' every <n> seconds
604
+        $timeFiveSec=microtime(true); // Used for display a '.' every <n> seconds
605 605
         
606 606
         // Create index for trap objects
607 607
         $this->trapObjectsIndex=array();
@@ -609,33 +609,33 @@  discard block
 block discarded – undo
609 609
         // detailed timing (time_* vars)
610 610
         $this->reset_update_timers();
611 611
         
612
-        for ($curElement=0;$curElement < $numElements;$curElement++)
612
+        for ($curElement=0; $curElement < $numElements; $curElement++)
613 613
         {
614
-            $this->timing['base_time']= microtime(true);
614
+            $this->timing['base_time']=microtime(true);
615 615
             if ($display_progress)
616 616
             {
617
-                if ((microtime(true)-$timeFiveSec) > 2)
617
+                if ((microtime(true) - $timeFiveSec) > 2)
618 618
                 { // echo a . every 2 sec
619 619
                     echo '.';
620
-                    $timeFiveSec = microtime(true);
620
+                    $timeFiveSec=microtime(true);
621 621
                 }
622
-                if ($curElement>$step)
622
+                if ($curElement > $step)
623 623
                 { // display progress
624 624
                     $num_step++;
625 625
                     $step+=$basestep;   
626
-                    echo "\n" . ($num_step*10). '% : ';
626
+                    echo "\n".($num_step * 10).'% : ';
627 627
                 }
628 628
             }
629 629
             
630 630
             $this->reset_oidDesc();
631
-            if ($this->detect_trap($curElement,$onlyTraps)===true)
631
+            if ($this->detect_trap($curElement, $onlyTraps) === true)
632 632
             {
633 633
                 continue;
634 634
             }
635 635
             
636 636
             $this->timing['num_traps']++;
637 637
             
638
-            $this->logging->log('Found trap : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],INFO );
638
+            $this->logging->log('Found trap : '.$this->oidDesc['name'].' / OID : '.$this->oidDesc['oid'], INFO);
639 639
             if ($display_progress) echo '#'; // echo a # when trap found
640 640
 
641 641
             // get trap objects & source MIB
@@ -645,14 +645,14 @@  discard block
 block discarded – undo
645 645
 
646 646
             $update=$this->update_oid(); // Do update of trap.
647 647
             
648
-            $this->timing['update_time'] += microtime(true) - $this->timing['base_time'];
648
+            $this->timing['update_time']+=microtime(true) - $this->timing['base_time'];
649 649
             $this->timing['update_num']++;
650 650
             
651
-            $this->timing['base_time']= microtime(true); // Reset to check object time
651
+            $this->timing['base_time']=microtime(true); // Reset to check object time
652 652
             
653
-            if (($update==0) && ($check_change===false))
653
+            if (($update == 0) && ($check_change === false))
654 654
             { // Trapd didn't change & force check disabled
655
-                $this->timing['objects_time'] += microtime(true) - $this->timing['base_time'];
655
+                $this->timing['objects_time']+=microtime(true) - $this->timing['base_time'];
656 656
                 if ($display_progress) echo "C";
657 657
                 continue;
658 658
             }
@@ -665,17 +665,17 @@  discard block
 block discarded – undo
665 665
            
666 666
             $this->trap_objects($this->oidDesc['oid'], $this->oidDesc['mib'], $trapObjects, false);
667 667
             
668
-            $this->timing['objects_time'] += microtime(true) - $this->timing['base_time'];
668
+            $this->timing['objects_time']+=microtime(true) - $this->timing['base_time'];
669 669
             $this->timing['objects_num']++;
670 670
         }
671 671
         
672 672
         if ($display_progress)
673 673
         {
674
-            echo "\nNumber of processed traps :  ". $this->timing['num_traps'] ."\n";
675
-            echo "\nParsing : " . number_format($this->timing['base_parse_time']+$this->timing['base_check_time'],1) ." sec / " . ($this->timing['base_parse_num']+ $this->timing['base_check_num'])  . " occurences\n";
676
-            echo "Detecting traps : " . number_format($this->timing['type0_check_time']+$this->timing['nottrap_time'],1) . " sec / " . ($this->timing['type0_check_num']+$this->timing['nottrap_num']) ." occurences\n";
677
-            echo "Trap processing (".$this->timing['update_num']."): ".number_format($this->timing['update_time'],1)." sec , ";
678
-            echo "Objects processing (".$this->timing['objects_num'].") : ".number_format($this->timing['objects_time'],1)." sec \n";
674
+            echo "\nNumber of processed traps :  ".$this->timing['num_traps']."\n";
675
+            echo "\nParsing : ".number_format($this->timing['base_parse_time'] + $this->timing['base_check_time'], 1)." sec / ".($this->timing['base_parse_num'] + $this->timing['base_check_num'])." occurences\n";
676
+            echo "Detecting traps : ".number_format($this->timing['type0_check_time'] + $this->timing['nottrap_time'], 1)." sec / ".($this->timing['type0_check_num'] + $this->timing['nottrap_num'])." occurences\n";
677
+            echo "Trap processing (".$this->timing['update_num']."): ".number_format($this->timing['update_time'], 1)." sec , ";
678
+            echo "Objects processing (".$this->timing['objects_num'].") : ".number_format($this->timing['objects_time'], 1)." sec \n";
679 679
             
680 680
             $timeTaken=microtime(true) - $timeTaken;
681 681
             echo "Global time : ".round($timeTaken)." seconds\n";
Please login to merge, or discard this patch.
library/Trapdirector/Config/MIBLoader.php 1 patch
Spacing   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -21,7 +21,7 @@  discard block
 block discarded – undo
21 21
 	 * @param string $db current database
22 22
 	 * @param TrapModuleConfig $config TrapModuleConfig class instance
23 23
 	 */
24
-	public function __construct($snmptranslate,$snmptranslate_dirs,$db,$config)
24
+	public function __construct($snmptranslate, $snmptranslate_dirs, $db, $config)
25 25
 	{
26 26
 		$this->snmptranslate=$snmptranslate;
27 27
 		$this->snmptranslate_dirs=$snmptranslate_dirs;
@@ -38,19 +38,19 @@  discard block
 block discarded – undo
38 38
 	
39 39
 	public function getMIBList()
40 40
 	{
41
-		$dbconn = $this->db->getConnection();
41
+		$dbconn=$this->db->getConnection();
42 42
 		$query=$dbconn->select()
43 43
 				->distinct()
44 44
 				->from(
45 45
 					$this->config->getMIBCacheTableName(),
46 46
 					array('mib' => 'mib'))
47 47
 				->where("type = '21'")
48
-				->order('mib ASC');				;
48
+				->order('mib ASC'); ;
49 49
 		$names=$dbconn->fetchAll($query);
50 50
 		$mib=array();
51
-		foreach($names as $val)
51
+		foreach ($names as $val)
52 52
 		{
53
-			array_push($mib,$val->mib);
53
+			array_push($mib, $val->mib);
54 54
 		}
55 55
 		return $mib;
56 56
 		
@@ -64,12 +64,12 @@  discard block
 block discarded – undo
64 64
 	public function getTrapList($mib)
65 65
 	{
66 66
 		$traps=array();
67
-		$dbconn = $this->db->getConnection();
67
+		$dbconn=$this->db->getConnection();
68 68
 		$query=$dbconn->select()
69 69
 				->from(
70 70
 					$this->config->getMIBCacheTableName(),
71 71
 				    array('name' => 'name', 'oid' => 'oid', 'description' => 'description'))
72
-				->where("mib = '".$mib."' AND type='21'") ;
72
+				->where("mib = '".$mib."' AND type='21'");
73 73
 		$names=$dbconn->fetchAll($query);
74 74
 		foreach ($names as $val)
75 75
 		{
@@ -87,19 +87,19 @@  discard block
 block discarded – undo
87 87
 		$objects=array();
88 88
 		
89 89
 		// Get trap id in DB
90
-		$dbconn = $this->db->getConnection();
90
+		$dbconn=$this->db->getConnection();
91 91
 		$query=$dbconn->select()
92 92
 				->from(
93 93
 					$this->config->getMIBCacheTableName(),
94 94
 					array('id' => 'id'))
95
-				->where("oid = '".$trap."'") ;
95
+				->where("oid = '".$trap."'");
96 96
 		$id=$dbconn->fetchRow($query);
97
-		if ( ($id == null) || ! property_exists($id,'id') ) return null;
97
+		if (($id == null) || !property_exists($id, 'id')) return null;
98 98
 		
99 99
 		$query=$dbconn->select()
100 100
 		        ->from(
101 101
 		            array('c' => $this->config->getMIBCacheTableName()),
102
-		            array('name' => 'c.name','mib' => 'c.mib','oid' => 'c.oid','type_enum'=>'c.type_enum',
102
+		            array('name' => 'c.name', 'mib' => 'c.mib', 'oid' => 'c.oid', 'type_enum'=>'c.type_enum',
103 103
 		                'type' => 'c.syntax', 'text_conv' => 'c.textual_convention', 'disp' => 'display_hint',
104 104
 		                'description' => 'c.description'))
105 105
 		        ->join(
@@ -107,7 +107,7 @@  discard block
 block discarded – undo
107 107
 		            'o.trap_id='.$id->id )
108 108
 		        ->where("o.object_id = c.id");
109 109
 		$listObjects=$dbconn->fetchAll($query);
110
-		if ( count($listObjects)==0 ) return null;
110
+		if (count($listObjects) == 0) return null;
111 111
 		
112 112
 		foreach ($listObjects as $val)
113 113
 		{
@@ -128,14 +128,14 @@  discard block
 block discarded – undo
128 128
 	*/
129 129
 	public function translateOID($oid)
130 130
 	{
131
-	    if (!preg_match('/^\./',$oid)) $oid = '.' . $oid; // Add a leading '.'
132
-		$retArray=array('oid' => $oid, 'mib' => null, 'name'=>null,'type'=>null);
133
-		$dbconn = $this->db->getConnection();
131
+	    if (!preg_match('/^\./', $oid)) $oid='.'.$oid; // Add a leading '.'
132
+		$retArray=array('oid' => $oid, 'mib' => null, 'name'=>null, 'type'=>null);
133
+		$dbconn=$this->db->getConnection();
134 134
 
135 135
 		$query=$dbconn->select()
136 136
 				->from(
137 137
 					array('o' => $this->config->getMIBCacheTableName()),
138
-					array('mib'=>'o.mib','name' => 'o.name','type'=>'o.syntax',
138
+					array('mib'=>'o.mib', 'name' => 'o.name', 'type'=>'o.syntax',
139 139
 					    'type_enum'=>'o.type_enum', 'description'=>'o.description'))
140 140
 				->where('o.oid=\''.$oid.'\'');
141 141
 		$object=$dbconn->fetchRow($query);
@@ -151,18 +151,18 @@  discard block
 block discarded – undo
151 151
 		
152 152
 		// Try to get oid name from snmptranslate
153 153
 		$matches=array();
154
-		$translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.
154
+		$translate=exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs.
155 155
 		    ' '.$oid);
156
-		$ret_code=preg_match('/(.*)::(.*)/',$translate,$matches);
157
-		if ($ret_code===0 || $ret_code===false) {
156
+		$ret_code=preg_match('/(.*)::(.*)/', $translate, $matches);
157
+		if ($ret_code === 0 || $ret_code === false) {
158 158
 			return null;
159 159
 		} 
160 160
 		$retArray['mib']=$matches[1];
161 161
 		$retArray['name']=$matches[2];
162 162
 		
163
-		$translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.' -Td -On ' . $matches[0] .
163
+		$translate=exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs.' -Td -On '.$matches[0].
164 164
 			" | grep SYNTAX | sed 's/SYNTAX[[:blank:]]*//'");
165
-		if (preg_match('/(.*)\{(.*)\}/',$translate,$matches))
165
+		if (preg_match('/(.*)\{(.*)\}/', $translate, $matches))
166 166
 		{
167 167
 		    $retArray['type']=$matches[1];
168 168
 		    $retArray['type_enum']=$matches[2];
@@ -197,9 +197,9 @@  discard block
 block discarded – undo
197 197
 	 * @param string $type filter by type (21=trap)
198 198
 	 * @return number number of entries in db.
199 199
 	 */
200
-	public function countObjects($mib=null,$type=null)
200
+	public function countObjects($mib=null, $type=null)
201 201
 	{
202
-		$dbconn = $this->db->getConnection();
202
+		$dbconn=$this->db->getConnection();
203 203
 		$query=$dbconn->select()
204 204
 				->from(
205 205
 					$this->config->getMIBCacheTableName(),
@@ -207,11 +207,11 @@  discard block
 block discarded – undo
207 207
 		$where=null;
208 208
 		if ($mib !== null)
209 209
 		{
210
-			$where ="mib = '$mib' ";
210
+			$where="mib = '$mib' ";
211 211
 		}
212 212
 		if ($type !== null)
213 213
 		{
214
-			$where=($where !== null)?' AND ':'';
214
+			$where=($where !== null) ? ' AND ' : '';
215 215
 			$where.="type='$type'";
216 216
 		}
217 217
 		if ($where !== null)
@@ -227,10 +227,10 @@  discard block
 block discarded – undo
227 227
 	 * @param integer $id
228 228
 	 * @return array trap details
229 229
 	 */
230
-	public function getTrapDetails($oid=null,$id=null)
230
+	public function getTrapDetails($oid=null, $id=null)
231 231
 	{	    
232 232
 	    // Get trap id in DB
233
-	    if ($oid===null)
233
+	    if ($oid === null)
234 234
 	    {
235 235
 	        $where="c.id = '$id'";
236 236
 	    }
@@ -241,7 +241,7 @@  discard block
 block discarded – undo
241 241
 	    $query=$this->db->getConnection()->select()
242 242
            ->from(
243 243
             array('c' => $this->config->getMIBCacheTableName()),
244
-            array('name' => 'c.name','mib' => 'c.mib','oid' => 'c.oid','type_enum'=>'c.type_enum',
244
+            array('name' => 'c.name', 'mib' => 'c.mib', 'oid' => 'c.oid', 'type_enum'=>'c.type_enum',
245 245
                 'type' => 'c.syntax', 'text_conv' => 'c.textual_convention', 'disp' => 'display_hint',
246 246
                 'description' => 'c.description'))
247 247
             ->where($where);
Please login to merge, or discard this patch.