Issues (2963)

includes/polling/applications/rrdcached.inc.php (1 issue)

1
<?php
2
/**
3
 * rrdcached.inc.php
4
 *
5
 * rrdcached application polling module
6
 * Capable of collecting stats from the agent or via direct connection
7
 * Only the default tcp port is supported, and unix sockets only work on localhost
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
21
 *
22
 * @link       https://www.librenms.org
23
 *
24
 * @copyright  2016 Tony Murray
25
 * @author     Tony Murray <[email protected]>
26
 */
27
28
use LibreNMS\RRD\RrdDefinition;
29
30
echo ' rrdcached';
31
32
$data = '';
33
$name = 'rrdcached';
34
$app_id = $app['app_id'];
35
36
if ($agent_data['app'][$name]) {
37
    $data = $agent_data['app'][$name];
38
} else {
39
    d_echo("\nNo Agent Data. Attempting to connect directly to the rrdcached server " . $device['hostname'] . ":42217\n");
40
41
    $sock = fsockopen($device['hostname'], 42217, $errno, $errstr, 5);
42
43
    if (! $sock) {
44
        d_echo("\nNo Socket to rrdcached server " . $device['hostname'] . ":42217 try to get rrdcached from SNMP\n");
45
        $oid = '.1.3.6.1.4.1.8072.1.3.2.3.1.2.9.114.114.100.99.97.99.104.101.100';
46
        $result = snmp_get($device, $oid, '-Oqv');
47
        $data = trim($result, '"');
0 ignored issues
show
It seems like $result can also be of type false; however, parameter $string of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
        $data = trim(/** @scrutinizer ignore-type */ $result, '"');
Loading history...
48
        $data = str_replace("<<<rrdcached>>>\n", '', $data);
49
    }
50
    if (strlen($data) < 100) {
51
        $socket = \LibreNMS\Config::get('rrdcached');
52
        if (substr($socket, 0, 6) == 'unix:/') {
53
            $socket_file = substr($socket, 5);
54
            if (file_exists($socket_file)) {
55
                $sock = fsockopen('unix://' . $socket_file);
56
            }
57
        }
58
        d_echo("\nNo SnmpData " . $device['hostname'] . ' fallback to local rrdcached unix://' . $socket_file . "\n");
59
    }
60
    if ($sock) {
61
        fwrite($sock, "STATS\n");
62
        $max = -1;
63
        $count = 0;
64
        while ($max == -1 || $count < $max) {
65
            $data .= fgets($sock, 128);
66
            if ($max == -1) {
67
                $tmp_max = explode(' ', $data);
68
                $max = $tmp_max[0] + 1;
69
            }
70
            $count++;
71
        }
72
        fclose($sock);
73
    } elseif (strlen($data) < 100) {
74
        d_echo("ERROR: $errno - $errstr\n");
75
    }
76
}
77
78
$rrd_name = ['app', $name, $app_id];
79
$rrd_def = RrdDefinition::make()
80
    ->addDataset('queue_length', 'GAUGE', 0)
81
    ->addDataset('updates_received', 'COUNTER', 0)
82
    ->addDataset('flushes_received', 'COUNTER', 0)
83
    ->addDataset('updates_written', 'COUNTER', 0)
84
    ->addDataset('data_sets_written', 'COUNTER', 0)
85
    ->addDataset('tree_nodes_number', 'GAUGE', 0)
86
    ->addDataset('tree_depth', 'GAUGE', 0)
87
    ->addDataset('journal_bytes', 'COUNTER', 0)
88
    ->addDataset('journal_rotate', 'COUNTER', 0);
89
90
$fields = [];
91
foreach (explode("\n", $data) as $line) {
92
    $split = explode(': ', $line);
93
    if (count($split) == 2) {
94
        $ds = strtolower(preg_replace('/[A-Z]/', '_$0', lcfirst($split[0])));
95
        $fields[$ds] = $split[1];
96
    }
97
}
98
99
$tags = compact('name', 'app_id', 'rrd_name', 'rrd_def');
100
data_update($device, 'app', $tags, $fields);
101
update_application($app, $data, $fields);
102
103
unset($data, $rrd_name, $rrd_def, $fields, $tags);
104