Passed
Push — master ( f89732...ddbf74 )
by Patrick
02:13
created

Mib::getLogging()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Trapdirector;
4
5
6
use PDO;
7
use Exception;
8
9
/**
10
 * MIB queries and update
11
 * 
12
 * @license GPL
13
 * @author Patrick Proy
14
 * @package trapdirector
15
 * @subpackage Processing
16
 *
17
 */
18
class Mib
19
{
20
    use \MibDatabase;
0 ignored issues
show
Bug introduced by
The trait MibDatabase requires the property $dbPrefix which is not provided by Trapdirector\Mib.
Loading history...
21
    
22
    /** @var Logging $logging logging class */
23
    protected $logging;
24
    /** @var Database $trapsDB Database class */
25
    protected $trapsDB;
26
    
27
    /** @var string $snmptranslate */
28
    public $snmptranslate;
29
    /** @var string $snmptranslateDirs */
30
    public $snmptranslateDirs;
31
    
32
    private $dbOidAll; //< All oid in database;
33
    private $dbOidIndex; //< Index of oid in dbOidAll
34
    private $objectsAll; //< output lines of snmptranslate list
35
    private $trapObjectsIndex; //< array of traps objects (as OID)
36
    
37
    private $oidDesc=array(); //< $oid,$mib,$name,$type,$textConv,$dispHint,$syntax,$type_enum,$description=NULL
38
39
    // Timing vars for update
40
    private $timing=array();
41
    
42
    /**
43
     * Setup Mib Class
44
     * @param Logging $logClass : where to log
45
     * @param Database $dbClass : Database
46
     */
47
    function __construct($logClass,$dbClass,$snmptrans,$snmptransdir)
48
    {
49
        $this->logging=$logClass;
50
        $this->trapsDB=$dbClass;
51
        $this->snmptranslate=$snmptrans;
52
        $this->snmptranslateDirs=$snmptransdir;
53
54
    }
55
    
56
    /**
57
     * @return \Trapdirector\Logging
58
     */
59
    public function getLogging()
60
    {
61
        return $this->logging;
62
    }
63
64
    /**
65
     * @return \Trapdirector\Database
66
     */
67
    public function getTrapsDB()
68
    {
69
        return $this->trapsDB;
70
    }
71
72
73
74
    /**
75
     * Create object in DB with object in dbOidIndex
76
     * @return number : 0=unchanged, 1 = changed, 2=created
77
     */
78
    private function update_oid_create()
79
    {
80
        // Insert data
81
        
82
        $db_conn=$this->trapsDB->db_connect_trap();
83
        $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache '.
84
            '(oid, name, type , mib, textual_convention, display_hint '.
85
            ', syntax, type_enum , description ) ' .
86
            'values (:oid, :name , :type ,:mib ,:tc , :display_hint'.
87
            ', :syntax, :type_enum, :description )';
88
        
89
        if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id';
90
        
91
        $sqlQuery=$db_conn->prepare($sql);
92
        
93
        $sqlParam=array(
94
            ':oid' => $this->oidDesc['oid'],
95
            ':name' => $this->oidDesc['name'],
96
            ':type' => $this->oidDesc['type'],
97
            ':mib' => $this->oidDesc['mib'],
98
            ':tc' =>  $this->oidDesc['textconv']??'null',
99
            ':display_hint' => $this->oidDesc['dispHint']??'null',
100
            ':syntax' => $this->oidDesc['syntax']??'null',
101
            ':type_enum' => $this->oidDesc['type_enum']??'null',
102
            ':description' => $this->oidDesc['description']??'null'
103
        );
104
        
105
        if ($sqlQuery->execute($sqlParam) === false) {
106
            $this->logging->log('Error in query : ' . $sql,1,'');
107
        }
108
        
109
        switch ($this->trapsDB->trapDBType)
110
        {
111
            case 'pgsql':
112
                // Get last id to insert oid/values in secondary table
113
                if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) {
114
                    $this->logging->log('Error getting id - pgsql - ',1,'');
115
                }
116
                if (! isset($inserted_id_ret['id'])) {
117
                    $this->logging->log('Error getting id - pgsql - empty.',ERROR);
118
                    return 0;
119
                }
120
                $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id_ret['id'];
121
                break;
122
            case 'mysql':
123
                // Get last id to insert oid/values in secondary table
124
                $sql='SELECT LAST_INSERT_ID();';
125
                if (($ret_code=$db_conn->query($sql)) === false) {
126
                    $this->logging->log('Erreur getting id - mysql - ',ERROR);
127
                    return 0;
128
                }
129
                
130
                $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()'];
131
                if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
132
                $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id;
133
                break;
134
            default:
135
                $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType,ERROR);
136
                return 0;
137
        }
138
        
139
        // Set as newly created.
140
        $this->dbOidIndex[$this->oidDesc['oid']]['key']=-1;
141
        return 2;
142
    }
143
    
144
    /**
145
     * Update or add an OID to database uses $this->dbOidIndex for mem cache
146
     * and $this->oidDesc doe data
147
     * @return number : 0=unchanged, 1 = changed, 2=created
148
     */
149
    public function update_oid()
150
    {
151
        $db_conn=$this->trapsDB->db_connect_trap();
152
        // Quote description.
153
        $this->oidDesc['description']=$db_conn->quote($this->oidDesc['description']);
154
        
155
        if (isset($this->dbOidIndex[$this->oidDesc['oid']]))
156
        { // oid exists in db, so update
157
            return $this->update_oid_update();
158
        }
159
        // create new OID.
160
        return $this->update_oid_create();
161
162
    }
163
    
164
/**
165
 * get all objects for a trap.
166
 * @param integer $trapId
167
 * @return array : array of cached objects
168
 */    
169
    private function cache_db_objects($trapId)
170
    {
171
        $dbObjects=array(); // cache of objects for trap in db
172
        $db_conn=$this->trapsDB->db_connect_trap();
173
        // Get all objects
174
        $sql='SELECT * FROM '.$this->trapsDB->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';';
175
        $this->logging->log('SQL query get all traps: '.$sql,DEBUG );
176
        if (($ret_code=$db_conn->query($sql)) === false) {
177
            $this->logging->log('No result in query : ' . $sql,1,'');
178
        }
179
        $dbObjectsRaw=$ret_code->fetchAll();
180
        
181
        foreach ($dbObjectsRaw as $val)
182
        {
183
            $dbObjects[$val['object_id']]=1;
184
        }
185
        return $dbObjects;
186
    }
187
188
/**
189
 * Get object details & mib , returns snmptranslate output
190
 * @param string $object : object name
191
 * @param string $trapmib : mib of trap
192
 * @return NULL|array : null if not found, or output of snmptranslate
193
 */
194
    private function get_object_details($object,$trapmib)
195
    {
196
        $match=$snmptrans=array();
197
        $retVal=0;
198
        $this->oidDesc['mib']=$trapmib;
199
        exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.
200
            ' -On -Td '.$this->oidDesc['mib'].'::'.$object . ' 2>/dev/null',$snmptrans,$retVal);
201
        if ($retVal!=0)
202
        {
203
            // Maybe not trap mib, search with IR
204
            exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.
205
                ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal);
206
            if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match))
207
            { // Not found -> continue with warning
208
                $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,'');
209
                return null;
210
            }
211
            $this->oidDesc['mib']=$match[1];
212
            
213
            // Do the snmptranslate again.
214
            exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.
215
                ' -On -Td '.$this->oidDesc['mib'].'::'.$object,$snmptrans,$retVal);
216
            if ($retVal!=0) {
217
                $this->logging->log('Error finding trap object : '.$this->oidDesc['mib'].'::'.$object,2,'');
218
                return null;
219
            }
220
            
221
        }
222
        return $snmptrans;
223
    }
224
225
/**
226
 * Parse snmptranslate output and set  $this->oidDesc with elements 
227
 * @param array $snmptrans : multi line output of snmptrans
228
 */
229
    private function parse_object($snmptrans)
230
    {
231
        $tmpdesc=''; // For multiline description
232
        $indesc=false; // true if currently inside multiline description
233
        $match=array();
234
        
235
        foreach ($snmptrans as $line)
236
        {
237
            if ($indesc===true)
238
            {
239
                $line=preg_replace('/[\t ]+/',' ',$line);
240
                if (preg_match('/(.*)"$/', $line,$match))
241
                {
242
                    $this->oidDesc['description'] = $tmpdesc . $match[1];
243
                    $indesc=false;
244
                }
245
                $tmpdesc.=$line;
246
                continue;
247
            }
248
            if (preg_match('/^\.[0-9\.]+$/', $line))
249
            {
250
                $this->oidDesc['oid']=$line;
251
                continue;
252
            }
253
            if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match))
254
            {
255
                $this->oidDesc['syntax']=$match[1];
256
                $this->oidDesc['type_enum']=$match[2];
257
                continue;
258
            }
259
            if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match))
260
            {
261
                $this->oidDesc['syntax']=$match[1];
262
                continue;
263
            }
264
            if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match))
265
            {
266
                $this->oidDesc['dispHint']=$match[1];
267
                continue;
268
            }
269
            if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match))
270
            {
271
                $this->oidDesc['description']=$match[1];
272
                continue;
273
            }
274
            if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match))
275
            {
276
                $tmpdesc=$match[1];
277
                $indesc=true;
278
                continue;
279
            }
280
            if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match))
281
            {
282
                $this->oidDesc['textconv']=$match[1];
283
                continue;
284
            }
285
        }
286
    }
287
288
    /**
289
     * create or update (with check_existing = true) objects of trap
290
     * @param string $trapOID : trap oid
291
     * @param string $trapmib : mib of trap
292
     * @param array $objects : array of objects name (without MIB)
293
     * @param bool $check_existing : check instead of create
294
     */
295
    public function trap_objects($trapOID,$trapmib,$objects,$check_existing)
296
    {              
297
        $trapId = $this->dbOidIndex[$trapOID]['id']; // Get id of trap
298
        
299
        if ($check_existing === true)
300
        {
301
            $dbObjects=$this->cache_db_objects($trapId);
302
        }
303
        
304
        foreach ($objects as $object)
305
        {
306
            
307
            $this->reset_oidDesc();
308
            
309
            $snmptrans=$this->get_object_details($object, $trapmib); // Get object mib & details
1 ignored issue
show
Bug introduced by
Are you sure the assignment to $snmptrans is correct as $this->get_object_details($object, $trapmib) targeting Trapdirector\Mib::get_object_details() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

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.

Loading history...
310
            if ($snmptrans === null) continue; // object not found
311
            
312
            $this->parse_object($snmptrans);
313
314
            $this->oidDesc['name'] = $object;
315
            
316
            $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 );
317
318
            // Update
319
            $this->update_oid();
320
            
321
            if (isset($dbObjects[$this->dbOidIndex[$this->oidDesc['oid']]['id']]))
322
            {   // if link exists, continue
323
                $dbObjects[$this->dbOidIndex[$this->oidDesc['oid']]['id']]=2;
324
                continue;
325
            }
326
            if ($check_existing === true)
327
            {
328
                // TODO : check link trap - objects exists, mark them.
329
            }
330
            // Associate in object table
331
            $db_conn=$this->trapsDB->db_connect_trap();
332
            $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache_trap_object (trap_id,object_id) '.
333
                'values (:trap_id, :object_id)';
334
            $sqlQuery=$db_conn->prepare($sql);
335
            $sqlParam=array(
336
                ':trap_id' => $trapId,
337
                ':object_id' => $this->dbOidIndex[$this->oidDesc['oid']]['id'],
338
            );
339
            
340
            if ($sqlQuery->execute($sqlParam) === false) {
341
                $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$this->oidDesc['oid']]['id'] ,1,'');
342
            }
343
        }
344
        if ($check_existing === true)
345
        {
346
            // TODO : remove link trap - objects that wasn't marked.
347
        }
348
        
349
    }
350
351
    private function reset_oidDesc()
352
    {
353
        $this->oidDesc['oid']=null;
354
        $this->oidDesc['name']=null;
355
        $this->oidDesc['type']=null;
356
        $this->oidDesc['mib']=null;
357
        $this->oidDesc['textconv']=null;
358
        $this->oidDesc['dispHint'] =null;
359
        $this->oidDesc['syntax']=null;
360
        $this->oidDesc['type_enum']=null;
361
        $this->oidDesc['description']=null;
362
    }
363
    
364
    /**
365
     * Fills $this->objectsAll with all mibs from snmptranslate
366
     * @return integer : number of elements 
367
     */
368
    private function load_mibs_snmptranslate()
369
    {
370
        $retVal=0;
371
        // Get all mib objects from all mibs
372
        $snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.' -On -Tto 2>/dev/null';
373
        $this->logging->log('Getting all traps : '.$snmpCommand,DEBUG );
374
        unset($this->objectsAll);
375
        exec($snmpCommand,$this->objectsAll,$retVal);
376
        if ($retVal!=0)
377
        {
378
            $this->logging->log('error executing snmptranslate',ERROR,'');
379
        }
380
        // Count elements to show progress
381
        $numElements=count($this->objectsAll);
382
        $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO );
383
        return $numElements;
384
    }
385
386
    /**
387
     * load all mib objects db in dbOidAll (raw) and index in dbOidIndex
388
     */
389
    private function load_mibs_from_db()
390
    {
391
        // Get all mibs from databse to have a memory index
392
        
393
        $db_conn=$this->trapsDB->db_connect_trap();
394
        
395
        $sql='SELECT * from '.$this->trapsDB->dbPrefix.'mib_cache;';
396
        $this->logging->log('SQL query : '.$sql,DEBUG );
397
        if (($ret_code=$db_conn->query($sql)) === false) {
398
            $this->logging->log('No result in query : ' . $sql,ERROR,'');
399
        }
400
        $this->dbOidAll=$ret_code->fetchAll();
401
        $this->dbOidIndex=array();
402
        // Create the index for db;
403
        foreach($this->dbOidAll as $key=>$val)
404
        {
405
            $this->dbOidIndex[$val['oid']]['key']=$key;
406
            $this->dbOidIndex[$val['oid']]['id']=$val['id'];
407
        }
408
    }
409
410
    /**
411
     * Reset all update timers & count to zero
412
     */
413
    private function reset_update_timers()
414
    {
415
        $this->timing['base_parse_time']=0;
416
        $this->timing['base_check_time']=0;
417
        $this->timing['type0_check_time']=0;
418
        $this->timing['nottrap_time']=0;
419
        $this->timing['update_time']=0;
420
        $this->timing['objects_time']=0;
421
        $this->timing['base_parse_num']=0;
422
        $this->timing['base_check_num']=0;
423
        $this->timing['type0_check_num']=0;
424
        $this->timing['nottrap_num']=0;
425
        $this->timing['update_num']=0;
426
        $this->timing['objects_num']=0;
427
        $this->timing['num_traps']=0;
428
    }
429
430
    /**
431
     * Detect if $this->objectsAll[$curElement] is a trap 
432
     * @param integer $curElement
433
     * @param bool $onlyTraps : set to false to get all and not only traps.
434
     * @return boolean : false if it's a trap , true if not
435
     */
436
    private function detect_trap($curElement,$onlyTraps)
437
    {
438
        // Get oid or pass if not found
439
        if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement]))
440
        {
441
            $this->timing['base_parse_time'] += microtime(true) - $this->timing['base_time'];
442
            $this->timing['base_parse_num'] ++;
443
            return true;
444
        }
445
        $this->oidDesc['oid']=$this->objectsAll[$curElement];
446
        
447
        // get next line
448
        $curElement++;
449
        $match=$snmptrans=array();
450
        if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/',
451
            $this->objectsAll[$curElement],$match))
452
        {
453
            $this->timing['base_check_time'] += microtime(true) - $this->timing['base_time'];
454
            $this->timing['base_check_num']++;
455
            return true;
456
        }
457
        
458
        $this->oidDesc['name']=$match[1]; // Name
459
        $this->oidDesc['type']=$match[2]; // type (21=trap, 0: may be trap, else : not trap
460
        
461
        if ($this->oidDesc['type']==0) // object type=0 : check if v1 trap
462
        {
463
            // Check if next is suboid -> in that case is cannot be a trap
464
            if (preg_match("/^".$this->oidDesc['oid']."/",$this->objectsAll[$curElement+1]))
465
            {
466
                $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time'];
467
                $this->timing['type0_check_num']++;
468
                return true;
469
            }
470
            unset($snmptrans);
471
            $retVal=0;
472
            exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.
473
                ' -Td '.$this->oidDesc['oid'] . ' | grep OBJECTS ',$snmptrans,$retVal);
474
            if ($retVal!=0)
475
            {
476
                $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time'];
477
                $this->timing['type0_check_num']++;
478
                return true;
479
            }
480
            //echo "\n v1 trap found : $this->oidDesc['oid'] \n";
481
            // Force as trap.
482
            $this->oidDesc['type']=21;
483
        }
484
        if ($onlyTraps===true && $this->oidDesc['type']!=21) // if only traps and not a trap, continue
485
        {
486
            $this->timing['nottrap_time'] += microtime(true) - $this->timing['base_time'];
487
            $this->timing['nottrap_num']++;
488
            return true;
489
        }
490
        return false;
491
    }
492
   
493
    /**
494
     * get_trap_mib_description
495
     * @return array|null : array of snmptranslate output or null on error
496
    **/
497
    private function get_trap_mib_description()
498
    {
499
        $retVal=0;
500
        $match=$snmptrans=array();
501
        exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.
502
            ' -Td '.$this->oidDesc['oid'],$snmptrans,$retVal);
503
        if ($retVal!=0)
504
        {
505
            $this->logging->log('error executing snmptranslate',ERROR);
506
            return $snmptrans;
507
        }
508
        
509
        if (!preg_match('/^(.*)::/',$snmptrans[0],$match))
510
        {
511
            $this->logging->log('Error getting mib from trap '.$this->oidDesc['oid'].' : ' . $snmptrans[0],ERROR);
512
            return $snmptrans;
513
        }
514
        $this->oidDesc['mib']=$match[1];
515
        
516
        $numLine=1;
517
        while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++;
518
        if (isset($snmptrans[$numLine]))
519
        {
520
            $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]);
521
            
522
            while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine]))
523
            {
524
                $this->oidDesc['description'].=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]);
525
                $numLine++;
526
            }
527
            if (isset($snmptrans[$numLine])) {
528
                $this->oidDesc['description'].=preg_replace('/".*/','',$snmptrans[$numLine]);
529
                $this->oidDesc['description']=preg_replace('/[\t ]+/',' ',$this->oidDesc['description']);
530
            }
531
            
532
        }
533
        return $snmptrans;
534
    }
535
536
    /**
537
     * Get trap objects
538
     * @param array $snmptrans : output of snmptranslate for TrapModuleConfig
539
     * @return array|null : array of objects or null if not found
540
    **/
541
    private function get_trap_objects($snmptrans)
542
    {
543
        $objectName=null;
544
        $match=array();
545
        foreach ($snmptrans as $line)
546
        {
547
            if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match))
548
            {
549
                $objectName=$match[1];
550
            }
551
        }
552
        if ($objectName == null)
553
        {
554
            $this->logging->log('No objects for ' . $this->oidDesc['oid'],DEBUG);
555
            $this->timing['objects_time'] += microtime(true) - $this->timing['base_time'];
556
            return null;
557
        }
558
        
559
        $trapObjects=array();
560
        while (preg_match('/ *([^ ,]+) *,* */',$objectName,$match))
561
        {
562
            array_push($trapObjects,$match[1]);
563
            $objectName=preg_replace('/'.$match[0].'/','',$objectName);
564
        }
565
        return $trapObjects;
566
    }
567
    
568
    /**
569
     * Cache mib in database
570
     * @param boolean $display_progress : Display progress on standard output
571
     * @param boolean $check_change : Force check of trap params & objects
572
     * @param boolean $onlyTraps : only cache traps and objects (true) or all (false)
573
     * @param string $startOID : only cache under startOID (NOT IMPLEMENTED)
574
     */
575
    public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1')
0 ignored issues
show
Unused Code introduced by
The parameter $startOID is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

575
    public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,/** @scrutinizer ignore-unused */ $startOID='.1')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
576
    {
577
        // Global Timing
578
        $timeTaken = microtime(true);
579
        
580
        $numElements=$this->load_mibs_snmptranslate(); // Load objectsAll
581
        
582
        $this->load_mibs_from_db(); // Load from db dbOidAll & dbOidIndex
583
        
584
        $step=$basestep=$numElements/10; // output display of % done
585
        $num_step=0;
586
        $timeFiveSec = microtime(true); // Used for display a '.' every <n> seconds
587
        
588
        // Create index for trap objects
589
        $this->trapObjectsIndex=array();
590
        
591
        // detailed timing (time_* vars)
592
        $this->reset_update_timers();
593
        
594
        for ($curElement=0;$curElement < $numElements;$curElement++)
595
        {
596
            $this->timing['base_time']= microtime(true);
597
            if ($display_progress)
598
            {
599
                if ((microtime(true)-$timeFiveSec) > 2)
600
                { // echo a . every 2 sec
601
                    echo '.';
602
                    $timeFiveSec = microtime(true);
603
                }
604
                if ($curElement>$step)
605
                { // display progress
606
                    $num_step++;
607
                    $step+=$basestep;   
608
                    echo "\n" . ($num_step*10). '% : ';
609
                }
610
            }
611
            
612
            $this->reset_oidDesc();
613
            if ($this->detect_trap($curElement,$onlyTraps)===true)
614
            {
615
                continue;
616
            }
617
            
618
            $this->timing['num_traps']++;
619
            
620
            $this->logging->log('Found trap : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],INFO );
621
            if ($display_progress) echo '#'; // echo a # when trap found
622
623
            // get trap objects & source MIB
624
            
625
            $snmptrans=$this->get_trap_mib_description(); // get MIB & description
626
627
628
            $update=$this->update_oid(); // Do update of trap.
629
            
630
            $this->timing['update_time'] += microtime(true) - $this->timing['base_time'];
631
            $this->timing['update_num']++;
632
            
633
            $this->timing['base_time']= microtime(true); // Reset to check object time
634
            
635
            if (($update==0) && ($check_change===false))
636
            { // Trapd didn't change & force check disabled
637
                $this->timing['objects_time'] += microtime(true) - $this->timing['base_time'];
638
                if ($display_progress) echo "C";
639
                continue;
640
            }
641
            
642
            $trapObjects=$this->get_trap_objects($snmptrans); // Get trap objects from snmptranslate output            
643
            if ($trapObjects == null)
644
            {
645
                continue;
646
            }
647
           
648
            $this->trap_objects($this->oidDesc['oid'], $this->oidDesc['mib'], $trapObjects, false);
649
            
650
            $this->timing['objects_time'] += microtime(true) - $this->timing['base_time'];
651
            $this->timing['objects_num']++;
652
        }
653
        
654
        if ($display_progress)
655
        {
656
            echo "\nNumber of processed traps :  ". $this->timing['num_traps'] ."\n";
657
            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";
658
            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";
659
            echo "Trap processing (".$this->timing['update_num']."): ".number_format($this->timing['update_time'],1)." sec , ";
660
            echo "Objects processing (".$this->timing['objects_num'].") : ".number_format($this->timing['objects_time'],1)." sec \n";
661
            
662
            $timeTaken=microtime(true) - $timeTaken;
663
            echo "Global time : ".round($timeTaken)." seconds\n";
664
        }
665
    }
666
    
667
    
668
}