Issues (2963)

scripts/collect-port-polling.php (1 issue)

1
#!/usr/bin/env php
2
<?php
3
4
use Illuminate\Support\Str;
5
use LibreNMS\Config;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Config. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use LibreNMS\Util\Debug;
7
8
$install_dir = realpath(__DIR__ . '/..');
9
chdir($install_dir);
10
11
$init_modules = [];
12
require $install_dir . '/includes/init.php';
13
$options = getopt('dh:e:', ['help']);
14
15
Config::set('rrd.enable', false);
16
Config::set('influxdb.enable', false);
17
Config::set('nographite', true);
18
19
function print_help()
20
{
21
    echo "-h <device id> | <device hostname wildcard>  Poll single device, wildcard hostname, or comma separated list\n";
22
    echo "-e <percentage>                              Enable/disable selected ports polling for devices which would benefit <percentage> from a change\n";
23
    echo "\n";
24
}
25
26
if (isset($options['d'])) {
27
    Debug::set();
28
}
29
30
if (isset($options['help'])) {
31
    print_help();
32
    exit(0);
33
}
34
35
$where = '';
36
$params = [];
37
if (isset($options['h'])) {
38
    if (is_numeric($options['h'])) {
39
        $where = 'AND `device_id` = ?';
40
        $params = [$options['h']];
41
    } elseif (Str::contains($options['h'], ',')) {
42
        $device_ids = array_map('trim', explode(',', $options['h']));
43
        $device_ids = array_filter($device_ids, 'is_numeric');
44
        $where = 'AND `device_id` in ' . dbGenPlaceholders(count($device_ids));
45
        $params = $device_ids;
46
    } else {
47
        $where = 'AND `hostname` LIKE ?';
48
        $params = [str_replace('*', '%', $options['h'])];
49
    }
50
}
51
$devices = dbFetchRows("SELECT * FROM `devices` WHERE status = 1 AND disabled = 0 $where ORDER BY `hostname` ASC", $params);
52
53
if (isset($options['e'])) {
54
    if (! is_numeric($options['e']) || $options['e'] < 0) {
55
        print_help();
56
        exit(1);
57
    }
58
    $enable_sel_value = $options['e'];
59
}
60
61
echo 'Full Polling: ';
62
Config::set('polling.selected_ports', false);
63
foreach ($devices as $index => $device) {
64
    echo $device['device_id'] . ' ';
65
    if (! Debug::isEnabled()) {
66
        ob_start();
67
    }
68
69
    $port_test_start = microtime(true);
70
    include $install_dir . '/includes/polling/ports.inc.php';
71
    $devices[$index]['full_time_sec'] = microtime(true) - $port_test_start;
72
    ob_end_clean();
73
}
74
echo PHP_EOL;
75
76
Config::set('polling.selected_ports', true);
77
echo 'Selective Polling: ';
78
foreach ($devices as $index => $device) {
79
    echo $device['device_id'] . ' ';
80
    if (! Debug::isEnabled()) {
81
        ob_start();
82
    }
83
84
    $port_test_start = microtime(true);
85
    include $install_dir . '/includes/polling/ports.inc.php';
86
    $devices[$index]['selective_time_sec'] = microtime(true) - $port_test_start;
87
    ob_end_clean();
88
}
89
echo PHP_EOL;
90
91
// collect port counts
92
$inactive_sql = "`deleted` = 1 OR `ifAdminStatus` != 'up' OR `disabled` = 1";
93
$set_count = 0;
94
foreach ($devices as &$device) {
95
    $count = dbFetchCell('SELECT COUNT(*) FROM `ports` WHERE `device_id`=?', [$device['device_id']]);
96
    $inactive = dbFetchCell(
97
        "SELECT COUNT(*) FROM `ports` WHERE `device_id`=? AND ($inactive_sql)",
98
        [$device['device_id']]
99
    );
100
101
    $device['port_count'] = $count;
102
    $device['inactive_ratio'] = ($inactive == 0 ? 0 : ($inactive / $count));
103
    $device['diff_sec'] = $device['selective_time_sec'] - $device['full_time_sec'];
104
    $device['diff_perc'] = ($device['diff_sec'] / $device['full_time_sec']) * 100;
105
106
    // $enable_sel_value is negative and we want to enable it for all devices with an even lower value.
107
    // It also has to save more than 1 s, or we might enable it for devices with i.e. 100ms vs 50ms, which isn't needed.
108
    $device['set'] = 'none';
109
    if (isset($enable_sel_value) && $device['diff_perc'] < ($enable_sel_value * -1) && $device['diff_sec'] < -1) {
110
        set_dev_attrib($device, 'selected_ports', 'true');
111
        $device['set'] = 'true';
112
        $set_count++;
113
    }
114
    if (isset($enable_sel_value) && $device['diff_perc'] > $enable_sel_value && $device['diff_sec'] > 1) {
115
        set_dev_attrib($device, 'selected_ports', 'false');
116
        $device['set'] = 'false';
117
        $set_count++;
118
    }
119
}
120
unset($device);  // will edit the wrong thing after using $device by reference
121
122
// print out the results
123
$stats = [
124
    'device_id',
125
    'os',
126
    'port_count',
127
    'inactive_ratio',
128
    'full_time',
129
    'selective_time',
130
    'diff',
131
    'diff',
132
    'set',
133
];
134
135
echo PHP_EOL;
136
$header = "| %9.9s | %-11.11s | %10.10s | %14.14s | %10.10s | %14.14s | %8.10s | %5.9s | %5.5s |\n";
137
call_user_func_array('printf', array_merge([$header], $stats));
138
139
$mask = "| %9.9s | %-11.11s | %10.10s | %14.3f | %9.3fs | %13.3fs | %s%+7.3fs\e[0m | %s%+4.0f%%\e[0m | %5.5s |\n";
140
foreach ($devices as $device) {
141
    $diff_color = ($device['diff_sec'] > 0 ? "\033[0;31m" : "\033[0;32m");
142
    printf(
143
        $mask,
144
        $device['device_id'],
145
        $device['os'],
146
        $device['port_count'],
147
        $device['inactive_ratio'],
148
        $device['full_time_sec'],
149
        $device['selective_time_sec'],
150
        $diff_color,
151
        $device['diff_sec'],
152
        $diff_color,
153
        $device['diff_perc'],
154
        $device['set']
155
    );
156
}
157
158
$total_ports = array_sum(array_column($devices, 'port_count'));
159
$inactive_ratio = array_sum(array_column($devices, 'inactive_ratio')) / count($devices);
160
$total_full_time = array_sum(array_column($devices, 'full_time_sec'));
161
$total_selective_time = array_sum(array_column($devices, 'selective_time_sec'));
162
$difference = $total_selective_time - $total_full_time;
163
$difference_perc = ($difference / $total_full_time) * 100;
164
$total_diff_color = ($difference > 0 ? "\033[0;31m" : "\033[0;32m");
165
166
printf(
167
    $mask,
168
    'Totals:',
169
    '',
170
    $total_ports,
171
    $inactive_ratio,
172
    $total_full_time,
173
    $total_selective_time,
174
    $total_diff_color,
175
    $difference,
176
    $total_diff_color,
177
    $difference_perc,
178
    $set_count
179
);
180