Passed
Push — master ( 608a4b...d03ea5 )
by Patrick
03:11
created

Mib::get_trap_objects()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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

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