Passed
Push — master ( 0baeb0...d4017c )
by Tony
19:18 queued 08:55
created

includes/discovery/cisco-sla.inc.php (1 issue)

1
<?php
2
3
use LibreNMS\Config;
4
use LibreNMS\Util\IP;
5
6
if (Config::get('enable_sla') && $device['os_group'] == 'cisco') {
7
    $slas = snmp_walk($device, 'ciscoRttMonMIB.ciscoRttMonObjects.rttMonCtrl', '-Osq', '+CISCO-RTTMON-MIB');
8
9
    $sla_table = array();
10
    foreach (explode("\n", $slas) as $sla) {
11
        $key_val = explode(' ', $sla, 2);
12
        if (count($key_val) != 2) {
13
            $key_val[] = '';
14
        }
15
16
        $key   = $key_val[0];
17
        $value = $key_val[1];
18
19
        $prop_id = explode('.', $key);
20
        if ((count($prop_id) != 2) || !ctype_digit($prop_id[1])) {
21
            continue;
22
        }
23
24
        $property = $prop_id[0];
25
        $id       = intval($prop_id[1]);
26
27
        $sla_table[$id][$property] = trim($value);
28
    }
29
30
    // var_dump($sla_table);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
31
    // Get existing SLAs
32
    $existing_slas = dbFetchColumn('SELECT `sla_id` FROM `slas` WHERE `device_id` = :device_id AND `deleted` = 0', array('device_id' => $device['device_id']));
33
34
    foreach ($sla_table as $sla_nr => $sla_config) {
35
        $query_data = array(
36
                   'device_id' => $device['device_id'],
37
                   'sla_nr'    => $sla_nr,
38
                  );
39
        $sla_id = dbFetchCell('SELECT `sla_id` FROM `slas` WHERE `device_id` = :device_id AND `sla_nr` = :sla_nr', $query_data);
40
41
        $data = array(
42
                 'device_id' => $device['device_id'],
43
                 'sla_nr'    => $sla_nr,
44
                 'owner'     => $sla_config['rttMonCtrlAdminOwner'],
45
                 'tag'       => $sla_config['rttMonCtrlAdminTag'],
46
                 'rtt_type'  => $sla_config['rttMonCtrlAdminRttType'],
47
                 'status'    => ($sla_config['rttMonCtrlAdminStatus'] == 'active') ? 1 : 0,
48
                 'opstatus'  => ($sla_config['rttMonLatestRttOperSense'] == 'ok') ? 0 : 2,
49
                 'deleted'   => 0,
50
                );
51
52
        // Some fallbacks for when the tag is empty
53
        if (!$data['tag']) {
54
            switch ($data['rtt_type']) {
55
                case 'http':
56
                    $data['tag'] = $sla_config['rttMonEchoAdminURL'];
57
                    break;
58
59
                case 'dns':
60
                    $data['tag'] = $sla_config['rttMonEchoAdminTargetAddressString'];
61
                    break;
62
63
                case 'echo':
64
                    $data['tag'] = IP::fromHexString($sla_config['rttMonEchoAdminTargetAddress'], true);
65
                    break;
66
67
                case 'jitter':
68
                    $data['tag'] = $sla_config['rttMonEchoAdminCodecType'] ." (". preg_replace('/milliseconds/', 'ms', $sla_config['rttMonEchoAdminCodecInterval']) .")";
69
                    break;
70
            }//end switch
71
        }//end if
72
73
        if (!$sla_id) {
74
            $sla_id = dbInsert($data, 'slas');
75
            echo '+';
76
        } else {
77
            // Remove from the list
78
            $existing_slas = array_diff($existing_slas, array($sla_id));
79
80
            dbUpdate($data, 'slas', '`sla_id` = :sla_id', array('sla_id' => $sla_id));
81
            echo '.';
82
        }
83
    }//end foreach
84
85
    // Mark all remaining SLAs as deleted
86
    foreach ($existing_slas as $existing_sla) {
87
        dbUpdate(array('deleted' => 1), 'slas', '`sla_id` = :sla_id', array('sla_id' => $existing_sla));
88
        echo '-';
89
    }
90
91
    echo "\n";
92
}
93