-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdevice-info.php
executable file
·89 lines (78 loc) · 2.92 KB
/
device-info.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/php
<?php
/**
* device-info.php
*
* Get stats on device
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2016 Tony Murray
* @author Tony Murray <[email protected]>
*/
require 'dibi/dibi.php';
require 'functions.php';
require 'definitions.php';
require 'config.php';
$config['cache'] = false; // never cache this script's queries
$options = getopt('ho:l:s:i:d:v');
if (empty($options) || isset($options['h'])) {
echo <<<EOL
\e[1mFilter Options:\e[0m
-o <os> Return all entries where OS is \e[1mequal\e[0m to this value
-d <sysDescr> Return all entries where sysDescr \e[1mcontains\e[0m this value
-i <sysObjectID> Return all entries where sysObjectID \e[1mstarts with\e[0m this value
-l <limit> Limit the number of results, default 30
\e[1mOutput Options:\e[0m
-s <field> Only show this field to improve grouping. Valid fields: os, sysObjectID, sysDescr
-v Display debug output. Can be added multiple times to increase verbosity
EOL;
exit;
}
$verbose = isset($options['v']) ? count((array)$options['v']) : 0;
$os = isset($options['o']) ? $options['o'] : null;
$limit = isset($options['l']) ? $options['l'] : 30;
$sort = isset($options['s']) ? $options['s'] : '';
$object_id = isset($options['i']) ? $options['i'] : null;
$descr = isset($options['d']) ? $options['d'] : null;
// fetch data
$results = getDeviceInfo($os, $object_id, $descr, $sort, $limit);
// normalize sysObjectID a bit
$results = array_map(function ($entry) {
$entry['sysObjectID'] = str_replace('enterprises', '.1.3.6.1.4.1', trim($entry['sysObjectID'], '"'));
return $entry;
}, $results);
// determine column sizes
$os_size = array_reduce($results, function($max, $entry) {
return max($max, strlen($entry['os']));
}, 2);
$soid_size = array_reduce($results, function($max, $entry) {
return max($max, strlen($entry['sysObjectID']));
}, 11);
// the line format
$mask = "|%5.5s|%-{$os_size}.{$os_size}s|%-{$soid_size}.{$soid_size}s| %s |\n";
// print header
printf($mask, 'Total', 'OS', 'sysObjectID', 'sysDescr');
// print results
foreach ($results as $result) {
printf(
$mask,
$result['total'],
$result['os'],
$result['sysObjectID'],
isset($result['sysDescr']) ? $result['sysDescr'] : ''
);
}