Passed
Branch master (608a4b)
by Patrick
48:57 queued 46:05
created

Mib::parse_object()   B

Complexity

Conditions 11
Paths 11

Size

Total Lines 55
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 34
c 1
b 0
f 0
nc 11
nop 1
dl 0
loc 55
rs 7.3166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Trapdirector;
4
5
use Trapdirector\Logging;
6
use Trapdirector\Database;
7
use PDO;
8
use Exception;
9
10
class Mib
11
{
12
    
13
    protected $logging; //< logging class
14
    protected $trapsDB; //< Database class
15
    
16
    public $snmptranslate;
17
    public $snmptranslateDirs;
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->snmptranslateDirs=$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()
50
    {
51
        $db_conn=$this->trapsDB->db_connect_trap();
52
        $this->oidDesc['description']=$db_conn->quote($this->oidDesc['description']);
53
        if (isset($this->dbOidIndex[$this->oidDesc['oid']]))
54
        {
55
            if ($this->dbOidIndex[$this->oidDesc['oid']]['key'] == -1)
56
            { // newly created.
57
                return 0;
58
            }
59
            $oidIndex=$this->dbOidIndex[$this->oidDesc['oid']]['key']; // Get index in dbOidAll
60
            $dbOid=$this->dbOidAll[$oidIndex]; // Get array of element
61
            if ( $this->oidDesc['name'] != $dbOid['name'] ||
62
                $this->oidDesc['mib'] != $dbOid['mib'] ||
63
                $this->oidDesc['type'] !=$dbOid['type']
64
               )
65
            { // Do update
66
                $sql='UPDATE '.$this->trapsDB->dbPrefix.'mib_cache SET '.
67
                    'name = :name , type = :type , mib = :mib , textual_convention = :tc , display_hint = :display_hint'.
68
                    ', syntax = :syntax, type_enum = :type_enum, description = :description '.
69
                    ' WHERE id= :id';
70
                $sqlQuery=$db_conn->prepare($sql);
71
                
72
                $sqlParam=array(
73
                    ':name' => $this->oidDesc['name'],
74
                    ':type' => $this->oidDesc['type'],
75
                    ':mib' => $this->oidDesc['mib'],
76
                    ':tc' =>  $this->oidDesc['textconv']??'null',
77
                    ':display_hint' => $this->oidDesc['dispHint']??'null' ,
78
                    ':syntax' => $this->oidDesc['syntax']==null??'null',
79
                    ':type_enum' => $this->oidDesc['type_enum']??'null',
80
                    ':description' => $this->oidDesc['description']??'null',
81
                    ':id' => $this->dbOidAll[$this->dbOidIndex[$this->oidDesc['oid']]['id']]
82
                );
83
                
84
                if ($sqlQuery->execute($sqlParam) === false) {
85
                    $this->logging->log('Error in query : ' . $sql,ERROR,'');
86
                }
87
                $this->logging->log('Trap updated : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG );
88
                return 1;
89
            }
90
            else
91
            {
92
                $this->logging->log('Trap unchanged : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG );
93
                return 0;
94
            }
95
        }
96
        // create new OID.
97
        
98
        // Insert data
99
        
100
        $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache '.
101
            '(oid, name, type , mib, textual_convention, display_hint '.
102
            ', syntax, type_enum , description ) ' .
103
            'values (:oid, :name , :type ,:mib ,:tc , :display_hint'.
104
            ', :syntax, :type_enum, :description )';
105
        
106
        if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id';
107
        
108
        $sqlQuery=$db_conn->prepare($sql);
109
        
110
        $sqlParam=array(
111
            ':oid' => $this->oidDesc['oid'],
112
            ':name' => $this->oidDesc['name'],
113
            ':type' => $this->oidDesc['type'],
114
            ':mib' => $this->oidDesc['mib'],
115
            ':tc' =>  ($this->oidDesc['textconv']==null)?'null':$this->oidDesc['textconv'] ,
116
            ':display_hint' => ($this->oidDesc['dispHint']==null)?'null':$this->oidDesc['dispHint'] ,
117
            ':syntax' => ($this->oidDesc['syntax']==null)?'null':$this->oidDesc['syntax'],
118
            ':type_enum' => ($this->oidDesc['type_enum']==null)?'null':$this->oidDesc['type_enum'],
119
            ':description' => ($this->oidDesc['description']==null)?'null':$this->oidDesc['description']
120
        );
121
        
122
        if ($sqlQuery->execute($sqlParam) === false) {
123
            $this->logging->log('Error in query : ' . $sql,1,'');
124
        }
125
        
126
        switch ($this->trapsDB->trapDBType)
127
        {
128
            case 'pgsql':
129
                // Get last id to insert oid/values in secondary table
130
                if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) {
131
                    $this->logging->log('Error getting id - pgsql - ',1,'');
132
                }
133
                if (! isset($inserted_id_ret['id'])) {
134
                    $this->logging->log('Error getting id - pgsql - empty.',1,'');
135
                }
136
                $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id_ret['id'];
137
                break;
138
            case 'mysql':
139
                // Get last id to insert oid/values in secondary table
140
                $sql='SELECT LAST_INSERT_ID();';
141
                if (($ret_code=$db_conn->query($sql)) === false) {
142
                    $this->logging->log('Erreur getting id - mysql - ',1,'');
143
                }
144
                
145
                $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()'];
146
                if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
147
                $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id;
148
                break;
149
            default:
150
                $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType,1,'');
151
        }
152
        
153
        // Set as newly created.
154
        $this->dbOidIndex[$this->oidDesc['oid']]['key']=-1;
155
        return 2;
156
    }
157
    
158
/**
159
 * get all objects for a trap.
160
 * @param integer $trapId
161
 * @return array : array of cached objects
162
 */    
163
    private function cache_db_objects($trapId)
164
    {
165
        $dbObjects=array(); // cache of objects for trap in db
166
        $db_conn=$this->trapsDB->db_connect_trap();
167
        // Get all objects
168
        $sql='SELECT * FROM '.$this->trapsDB->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';';
169
        $this->logging->log('SQL query get all traps: '.$sql,DEBUG );
170
        if (($ret_code=$db_conn->query($sql)) === false) {
171
            $this->logging->log('No result in query : ' . $sql,1,'');
172
        }
173
        $dbObjectsRaw=$ret_code->fetchAll();
174
        
175
        foreach ($dbObjectsRaw as $val)
176
        {
177
            $dbObjects[$val['object_id']]=1;
178
        }
179
        return $dbObjects;
180
    }
181
182
/**
183
 * Get object details & mib , returns snmptranslate output
184
 * @param string $object : object name
185
 * @param string $trapmib : mib of trap
186
 * @return NULL|array : null if not found, or output of snmptranslate
187
 */
188
    private function get_object_details($object,$trapmib)
189
    {
190
        $match=$snmptrans=array();
191
        $retVal=0;
192
        $this->oidDesc['mib']=$trapmib;
193
        exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.
194
            ' -On -Td '.$this->oidDesc['mib'].'::'.$object . ' 2>/dev/null',$snmptrans,$retVal);
195
        if ($retVal!=0)
196
        {
197
            // Maybe not trap mib, search with IR
198
            exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.
199
                ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal);
200
            if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match))
201
            { // Not found -> continue with warning
202
                $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,'');
203
                return null;
204
            }
205
            $this->oidDesc['mib']=$match[1];
206
            
207
            // Do the snmptranslate again.
208
            exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.
209
                ' -On -Td '.$this->oidDesc['mib'].'::'.$object,$snmptrans,$retVal);
210
            if ($retVal!=0) {
211
                $this->logging->log('Error finding trap object : '.$this->oidDesc['mib'].'::'.$object,2,'');
212
                return null;
213
            }
214
            
215
        }
216
        return $snmptrans;
217
    }
218
219
/**
220
 * Parse snmptranslate output and set  $this->oidDesc with elements 
221
 * @param array $snmptrans : multi line output of snmptrans
222
 */
223
    private function parse_object($snmptrans)
224
    {
225
        $tmpdesc=''; // For multiline description
226
        $indesc=false; // true if currently inside multiline description
227
        $match=array();
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
                    $this->oidDesc['description'] = $tmpdesc . $match[1];
237
                    $indesc=false;
238
                }
239
                $tmpdesc.=$line;
240
                continue;
241
            }
242
            if (preg_match('/^\.[0-9\.]+$/', $line))
243
            {
244
                $this->oidDesc['oid']=$line;
245
                continue;
246
            }
247
            if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match))
248
            {
249
                $this->oidDesc['syntax']=$match[1];
250
                $this->oidDesc['type_enum']=$match[2];
251
                continue;
252
            }
253
            if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match))
254
            {
255
                $this->oidDesc['syntax']=$match[1];
256
                continue;
257
            }
258
            if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match))
259
            {
260
                $this->oidDesc['dispHint']=$match[1];
261
                continue;
262
            }
263
            if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match))
264
            {
265
                $this->oidDesc['description']=$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
                $this->oidDesc['textconv']=$match[1];
277
                continue;
278
            }
279
        }
280
    }
281
282
    /**
283
     * create or update (with check_existing = true) objects of trap
284
     * @param string $trapOID : trap oid
285
     * @param string $trapmib : mib of trap
286
     * @param array $objects : array of objects name (without MIB)
287
     * @param bool $check_existing : check instead of create
288
     */
289
    public function trap_objects($trapOID,$trapmib,$objects,$check_existing)
290
    {              
291
        $trapId = $this->dbOidIndex[$trapOID]['id']; // Get id of trap
292
        
293
        if ($check_existing === true)
294
        {
295
            $dbObjects=$this->cache_db_objects($trapId);
296
        }
297
        
298
        foreach ($objects as $object)
299
        {
300
            
301
            $this->reset_oidDesc();
302
            
303
            $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...
304
            if ($snmptrans === null) continue; // object not found
305
            
306
            $this->parse_object($snmptrans);
307
308
            $this->oidDesc['name'] = $object;
309
            
310
            $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 );
311
312
            // Update
313
            $this->update_oid();
314
            
315
            if (isset($dbObjects[$this->dbOidIndex[$this->oidDesc['oid']]['id']]))
316
            {   // if link exists, continue
317
                $dbObjects[$this->dbOidIndex[$this->oidDesc['oid']]['id']]=2;
318
                continue;
319
            }
320
            if ($check_existing === true)
321
            {
322
                // TODO : check link trap - objects exists, mark them.
323
            }
324
            // Associate in object table
325
            $db_conn=$this->trapsDB->db_connect_trap();
326
            $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache_trap_object (trap_id,object_id) '.
327
                'values (:trap_id, :object_id)';
328
            $sqlQuery=$db_conn->prepare($sql);
329
            $sqlParam=array(
330
                ':trap_id' => $trapId,
331
                ':object_id' => $this->dbOidIndex[$this->oidDesc['oid']]['id'],
332
            );
333
            
334
            if ($sqlQuery->execute($sqlParam) === false) {
335
                $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$this->oidDesc['oid']]['id'] ,1,'');
336
            }
337
        }
338
        if ($check_existing === true)
339
        {
340
            // TODO : remove link trap - objects that wasn't marked.
341
        }
342
        
343
    }
344
345
    private function reset_oidDesc()
346
    {
347
        $this->oidDesc['oid']=null;
348
        $this->oidDesc['name']=null;
349
        $this->oidDesc['type']=null;
350
        $this->oidDesc['mib']=null;
351
        $this->oidDesc['textconv']=null;
352
        $this->oidDesc['dispHint'] =null;
353
        $this->oidDesc['syntax']=null;
354
        $this->oidDesc['type_enum']=null;
355
        $this->oidDesc['description']=null;
356
    }
357
    
358
    /**
359
     * Fills $this->objectsAll with all mibs from snmptranslate
360
     * @return integer : number of elements 
361
     */
362
    private function load_mibs_snmptranslate()
363
    {
364
        $retVal=0;
365
        // Get all mib objects from all mibs
366
        $snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.' -On -Tto 2>/dev/null';
367
        $this->logging->log('Getting all traps : '.$snmpCommand,DEBUG );
368
        unset($this->objectsAll);
369
        exec($snmpCommand,$this->objectsAll,$retVal);
370
        if ($retVal!=0)
371
        {
372
            $this->logging->log('error executing snmptranslate',ERROR,'');
373
        }
374
        // Count elements to show progress
375
        $numElements=count($this->objectsAll);
376
        $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO );
377
        return $numElements;
378
    }
379
380
    /**
381
     * load all mib objects db in dbOidAll (raw) and index in dbOidIndex
382
     */
383
    private function load_mibs_from_db()
384
    {
385
        // Get all mibs from databse to have a memory index
386
        
387
        $db_conn=$this->trapsDB->db_connect_trap();
388
        
389
        $sql='SELECT * from '.$this->trapsDB->dbPrefix.'mib_cache;';
390
        $this->logging->log('SQL query : '.$sql,DEBUG );
391
        if (($ret_code=$db_conn->query($sql)) === false) {
392
            $this->logging->log('No result in query : ' . $sql,ERROR,'');
393
        }
394
        $this->dbOidAll=$ret_code->fetchAll();
395
        $this->dbOidIndex=array();
396
        // Create the index for db;
397
        foreach($this->dbOidAll as $key=>$val)
398
        {
399
            $this->dbOidIndex[$val['oid']]['key']=$key;
400
            $this->dbOidIndex[$val['oid']]['id']=$val['id'];
401
        }
402
    }
403
404
    /**
405
     * Reset all update timers & count to zero
406
     */
407
    private function reset_update_timers()
408
    {
409
        $this->timing['base_parse_time']=0;
410
        $this->timing['base_check_time']=0;
411
        $this->timing['type0_check_time']=0;
412
        $this->timing['nottrap_time']=0;
413
        $this->timing['update_time']=0;
414
        $this->timing['objects_time']=0;
415
        $this->timing['base_parse_num']=0;
416
        $this->timing['base_check_num']=0;
417
        $this->timing['type0_check_num']=0;
418
        $this->timing['nottrap_num']=0;
419
        $this->timing['update_num']=0;
420
        $this->timing['objects_num']=0;
421
        $this->timing['num_traps']=0;
422
    }
423
424
    /**
425
     * Detect if $this->objectsAll[$curElement] is a trap 
426
     * @param integer $curElement
427
     * @param bool $onlyTraps : set to false to get all and not only traps.
428
     * @return boolean : false if it's a trap , true if not
429
     */
430
    private function detect_trap($curElement,$onlyTraps)
431
    {
432
        // Get oid or pass if not found
433
        if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement]))
434
        {
435
            $this->timing['base_parse_time'] += microtime(true) - $this->timing['base_time'];
436
            $this->timing['base_parse_num'] ++;
437
            return true;
438
        }
439
        $this->oidDesc['oid']=$this->objectsAll[$curElement];
440
        
441
        // get next line
442
        $curElement++;
443
        $match=$snmptrans=array();
444
        if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/',
445
            $this->objectsAll[$curElement],$match))
446
        {
447
            $this->timing['base_check_time'] += microtime(true) - $this->timing['base_time'];
448
            $this->timing['base_check_num']++;
449
            return true;
450
        }
451
        
452
        $this->oidDesc['name']=$match[1]; // Name
453
        $this->oidDesc['type']=$match[2]; // type (21=trap, 0: may be trap, else : not trap
454
        
455
        if ($this->oidDesc['type']==0) // object type=0 : check if v1 trap
456
        {
457
            // Check if next is suboid -> in that case is cannot be a trap
458
            if (preg_match("/^".$this->oidDesc['oid']."/",$this->objectsAll[$curElement+1]))
459
            {
460
                $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time'];
461
                $this->timing['type0_check_num']++;
462
                return true;
463
            }
464
            unset($snmptrans);
465
            $retVal=0;
466
            exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.
467
                ' -Td '.$this->oidDesc['oid'] . ' | grep OBJECTS ',$snmptrans,$retVal);
468
            if ($retVal!=0)
469
            {
470
                $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time'];
471
                $this->timing['type0_check_num']++;
472
                return true;
473
            }
474
            //echo "\n v1 trap found : $this->oidDesc['oid'] \n";
475
            // Force as trap.
476
            $this->oidDesc['type']=21;
477
        }
478
        if ($onlyTraps===true && $this->oidDesc['type']!=21) // if only traps and not a trap, continue
479
        {
480
            $this->timing['nottrap_time'] += microtime(true) - $this->timing['base_time'];
481
            $this->timing['nottrap_num']++;
482
            return true;
483
        }
484
        return false;
485
    }
486
   
487
    
488
    private function get_trap_mib_description()
489
    {
490
        $retVal=0;
491
        $match=$snmptrans=array();
492
        exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.
493
            ' -Td '.$this->oidDesc['oid'],$snmptrans,$retVal);
494
        if ($retVal!=0)
495
        {
496
            $this->logging->log('error executing snmptranslate',ERROR);
497
        }
498
        
499
        if (!preg_match('/^(.*)::/',$snmptrans[0],$match))
500
        {
501
            $this->logging->log('Error getting mib from trap '.$this->oidDesc['oid'].' : ' . $snmptrans[0],ERROR);
502
        }
503
        $this->oidDesc['mib']=$match[1];
504
        
505
        $numLine=1;
506
        while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++;
507
        if (isset($snmptrans[$numLine]))
508
        {
509
            $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]);
510
            
511
            while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine]))
512
            {
513
                $this->oidDesc['description'].=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]);
514
                $numLine++;
515
            }
516
            if (isset($snmptrans[$numLine])) {
517
                $this->oidDesc['description'].=preg_replace('/".*/','',$snmptrans[$numLine]);
518
                $this->oidDesc['description']=preg_replace('/[\t ]+/',' ',$this->oidDesc['description']);
519
            }
520
            
521
        }
522
        return $snmptrans;
523
    }
524
    
525
    /**
526
     * Cache mib in database
527
     * @param boolean $display_progress : Display progress on standard output
528
     * @param boolean $check_change : Force check of trap params & objects
529
     * @param boolean $onlyTraps : only cache traps and objects (true) or all (false)
530
     * @param string $startOID : only cache under startOID (NOT IMPLEMENTED)
531
     */
532
    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

532
    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...
533
    {
534
        // Global Timing
535
        $timeTaken = microtime(true);
536
        
537
        $numElements=$this->load_mibs_snmptranslate(); // Load objectsAll
538
        
539
        $this->load_mibs_from_db(); // Load from db dbOidAll & dbOidIndex
540
        
541
        $step=$basestep=$numElements/10; // output display of % done
542
        $num_step=0;
543
        $timeFiveSec = microtime(true); // Used for display a '.' every <n> seconds
544
        
545
        // Create index for trap objects
546
        $this->trapObjectsIndex=array();
547
        
548
        // detailed timing (time_* vars)
549
        $this->reset_update_timers();
550
        
551
        for ($curElement=0;$curElement < $numElements;$curElement++)
552
        {
553
            $this->timing['base_time']= microtime(true);
554
            if ($display_progress)
555
            {
556
                if ((microtime(true)-$timeFiveSec) > 2)
557
                { // echo a . every 2 sec
558
                    echo '.';
559
                    $timeFiveSec = microtime(true);
560
                }
561
                if ($curElement>$step)
562
                { // display progress
563
                    $num_step++;
564
                    $step+=$basestep;   
565
                    echo "\n" . ($num_step*10). '% : ';
566
                }
567
            }
568
            
569
            $this->reset_oidDesc();
570
            if ($this->detect_trap($curElement,$onlyTraps)===true)
571
            {
572
                continue;
573
            }
574
            
575
            $this->timing['num_traps']++;
576
            
577
            $this->logging->log('Found trap : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],INFO );
578
            if ($display_progress) echo '#'; // echo a # when trap found
579
            
580
            //################################
581
            // get trap objects & source MIB
582
            
583
            $snmptrans=$this->get_trap_mib_description(); // get MIB & description
584
585
            //################################
586
            $update=$this->update_oid(); // Do update of trap.
587
            
588
            $this->timing['update_time'] += microtime(true) - $this->timing['base_time'];
589
            $this->timing['update_num']++;
590
            
591
            $this->timing['base_time']= microtime(true); // Reset to check object time
592
            
593
            if (($update==0) && ($check_change===false))
594
            { // Trapd didn't change & force check disabled
595
                $this->timing['objects_time'] += microtime(true) - $this->timing['base_time'];
596
                if ($display_progress) echo "C";
597
                continue;
598
            }
599
            
600
            $synt=null;
601
            $match=array();
602
            foreach ($snmptrans as $line)
603
            {
604
                if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match))
605
                {
606
                    $synt=$match[1];
607
                }
608
            }
609
            if ($synt == null)
610
            {
611
                //echo "No objects for $trapOID\n";
612
                $this->timing['objects_time'] += microtime(true) - $this->timing['base_time'];
613
                continue;
614
            }
615
            //echo "$synt \n";
616
            $trapObjects=array();
617
            while (preg_match('/ *([^ ,]+) *,* */',$synt,$match))
618
            {
619
                array_push($trapObjects,$match[1]);
620
                $synt=preg_replace('/'.$match[0].'/','',$synt);
621
            }
622
            
623
            $this->trap_objects($this->oidDesc['oid'], $this->oidDesc['mib'], $trapObjects, false);
624
            
625
            $this->timing['objects_time'] += microtime(true) - $this->timing['base_time'];
626
            $this->timing['objects_num']++;
627
        }
628
        
629
        if ($display_progress)
630
        {
631
            echo "\nNumber of processed traps :  ". $this->timing['num_traps'] ."\n";
632
            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";
633
            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";
634
            echo "Trap processing (".$this->timing['update_num']."): ".number_format($this->timing['update_time'],1)." sec , ";
635
            echo "Objects processing (".$this->timing['objects_num'].") : ".number_format($this->timing['objects_time'],1)." sec \n";
636
            
637
            $timeTaken=microtime(true) - $timeTaken;
638
            echo "Global time : ".round($timeTaken)." seconds\n";
639
        }
640
    }
641
    
642
    
643
}