Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

101 lines
3.5 KiB
PHP
Raw Permalink Normal View History

2016-06-08 20:40:51 -05:00
<?php
2016-06-09 00:40:04 -05:00
/**
* rrdcached.inc.php
*
* rrdcached application polling module
* Capable of collecting stats from the agent or via direct connection
* Only the default tcp port is supported, and unix sockets only work on localhost
*
* 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
2021-02-09 00:29:04 +01:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2016-06-09 00:40:04 -05:00
*
2021-02-09 00:29:04 +01:00
* @link https://www.librenms.org
2021-09-10 20:09:53 +02:00
*
2016-06-09 00:40:04 -05:00
* @copyright 2016 Tony Murray
* @author Tony Murray <[email protected]>
*/
2016-06-08 20:40:51 -05:00
use LibreNMS\RRD\RrdDefinition;
2016-06-08 20:40:51 -05:00
$data = '';
$name = 'rrdcached';
2016-06-08 20:40:51 -05:00
if ($agent_data['app'][$name]) {
$data = $agent_data['app'][$name];
2016-06-08 20:40:51 -05:00
} else {
2016-06-09 00:40:04 -05:00
d_echo("\nNo Agent Data. Attempting to connect directly to the rrdcached server " . $device['hostname'] . ":42217\n");
2016-06-08 20:40:51 -05:00
$sock = fsockopen($device['hostname'], 42217, $errno, $errstr, 5);
2020-09-21 15:43:38 +02:00
if (! $sock) {
d_echo("\nNo Socket to rrdcached server " . $device['hostname'] . ":42217 try to get rrdcached from SNMP\n");
$oid = '.1.3.6.1.4.1.8072.1.3.2.3.1.2.9.114.114.100.99.97.99.104.101.100';
$result = snmp_get($device, $oid, '-Oqv');
$data = trim($result, '"');
$data = str_replace("<<<rrdcached>>>\n", '', $data);
}
if (strlen($data) < 100) {
$socket = \LibreNMS\Config::get('rrdcached');
2020-04-03 03:50:39 +02:00
if (substr($socket, 0, 6) == 'unix:/') {
$socket_file = substr($socket, 5);
if (file_exists($socket_file)) {
$sock = fsockopen('unix://' . $socket_file);
}
2016-06-08 20:40:51 -05:00
}
d_echo("\nNo SnmpData " . $device['hostname'] . ' fallback to local rrdcached unix://' . $socket_file . "\n");
2016-06-08 20:40:51 -05:00
}
2016-06-09 00:40:04 -05:00
if ($sock) {
2016-06-08 20:40:51 -05:00
fwrite($sock, "STATS\n");
$max = -1;
$count = 0;
while ($max == -1 || $count < $max) {
$data .= fgets($sock, 128);
if ($max == -1) {
$tmp_max = explode(' ', $data);
2020-09-21 15:43:38 +02:00
$max = $tmp_max[0] + 1;
2016-06-08 20:40:51 -05:00
}
$count++;
}
fclose($sock);
} elseif (strlen($data) < 100) {
2016-06-08 20:40:51 -05:00
d_echo("ERROR: $errno - $errstr\n");
}
}
$rrd_name = ['app', $name, $app->app_id];
$rrd_def = RrdDefinition::make()
->addDataset('queue_length', 'GAUGE', 0)
->addDataset('updates_received', 'COUNTER', 0)
->addDataset('flushes_received', 'COUNTER', 0)
->addDataset('updates_written', 'COUNTER', 0)
->addDataset('data_sets_written', 'COUNTER', 0)
->addDataset('tree_nodes_number', 'GAUGE', 0)
->addDataset('tree_depth', 'GAUGE', 0)
->addDataset('journal_bytes', 'COUNTER', 0)
->addDataset('journal_rotate', 'COUNTER', 0);
2016-06-08 20:40:51 -05:00
2020-09-21 15:43:38 +02:00
$fields = [];
2016-06-08 20:40:51 -05:00
foreach (explode("\n", $data) as $line) {
$split = explode(': ', $line);
if (count($split) == 2) {
$ds = strtolower(preg_replace('/[A-Z]/', '_$0', lcfirst($split[0])));
$fields[$ds] = $split[1];
2016-06-08 20:40:51 -05:00
}
}
$tags = compact('name', 'app_id', 'rrd_name', 'rrd_def');
data_update($device, 'app', $tags, $fields);
update_application($app, $data, $fields);
2016-06-08 20:40:51 -05:00
unset($data, $rrd_name, $rrd_def, $fields, $tags);