Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Send collected data to graphite server #6201

Merged
merged 14 commits into from Mar 22, 2017
23 changes: 23 additions & 0 deletions doc/Extensions/Graphite.md
@@ -0,0 +1,23 @@
source: Extensions/Graphite.md
# Enabling support for Graphite.

This module sends all metrics to a remote graphite service. You need something like Grafana for graphing.

### What you don't get
- Pretty graphs, this is why at present you need Grafana. You need to build your own graphs within Grafana.

RRD will continue to function as normal so LibreNMS itself should continue to function as normal.

### Configuration
```php
$config['graphite']['enable'] = true;
$config['graphite']['host'] = 'your.graphite.server';
$config['graphite']['port'] = 2003; // this defaults to 2003 and is usually not needed
$config['graphite']['prefix'] = 'your.metric.prefix';
```

Your metric path can be prefixed if required, otherwise the metric path for Graphite will be in the form of
`hostname.measurement.fieldname`, interfaces will be stored as `hostname.ports.ifName.fieldname`.

The same data then stored within rrd will be sent to Graphite and recorded. You can then create graphs within Grafana
to display the information you need.
1 change: 1 addition & 0 deletions includes/datastore.inc.php
Expand Up @@ -67,4 +67,5 @@ function data_update($device, $measurement, $tags, $fields)

rrdtool_data_update($device, $measurement, $tags, $fields);
influx_update($device, $measurement, rrd_array_filter($tags), $fields);
graphite_update($device, $measurement, $tags, $fields);
} // data_update
33 changes: 33 additions & 0 deletions includes/graphite.inc.php
@@ -0,0 +1,33 @@
<?php

/*
* LibreNMS
*
* Copyright (c) 2017 Falk Stern <https://github.com/fstern/ >
*
* 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. Please see LICENSE.txt at the top level of
* the source code distribution for details.
*/

function graphite_update($device, $measurement, $tags, $fields)
{
global $graphite, $config;
if ($graphite !== false) {
$timestamp = time();
$graphite_prefix = $config['graphite']['prefix'];
// metrics will be built as prefix.hostname.measurement.field value timestamp
// metric fields can not contain . as this is used by graphite as a field separator
$hostname = preg_replace('/\./', '_', $device['hostname']);
$measurement = preg_replace(array('/\./', '/\//'), '_', $measurement);
$measurement = preg_replace('/\|/', '.', $measurement);
foreach ($fields as $k => $v) {
$metric = implode(".", array_filter(array($graphite_prefix, $hostname, $measurement, $k)));
$line = implode(" ", array($metric, $v, $timestamp));
d_echo("Sending $line\n");
fwrite($graphite, $line . "\n");
}
}
}
1 change: 1 addition & 0 deletions includes/init.php
Expand Up @@ -47,6 +47,7 @@
require $install_dir . '/includes/dbFacile.php';
require $install_dir . '/includes/rrdtool.inc.php';
require $install_dir . '/includes/influxdb.inc.php';
require $install_dir . '/includes/graphite.inc.php';
require $install_dir . '/includes/datastore.inc.php';
require $install_dir . '/includes/billing.php';
require $install_dir . '/includes/syslog.php';
Expand Down
1 change: 1 addition & 0 deletions includes/polling/ports.inc.php
Expand Up @@ -753,6 +753,7 @@
$fields['ifOutOctets_rate'] = $port['ifOutOctets_rate'];

influx_update($device, 'ports', rrd_array_filter($tags), $fields);
graphite_update($device, 'ports|' . $ifName, $tags, $fields);

// End Update IF-MIB
// Update PAgP
Expand Down
19 changes: 19 additions & 0 deletions poller.php
Expand Up @@ -110,12 +110,27 @@
$config['noinfluxdb'] = true;
}

if (isset($options['g'])) {
$config['nographite'] = true;
}

if ($config['noinfluxdb'] !== true && $config['influxdb']['enable'] === true) {
$influxdb = influxdb_connect();
} else {
$influxdb = false;
}

if ($config['nographite'] !== true && $config['graphite']['enable'] === true) {
if (isset($config['graphite']['port'])) {
$graphite_port = $config['graphite']['port'];
} else {
$graphite_port = '2003';
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set the default in includes/defaults.inc.php. This entire isset piece is then not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I've moved it to includes/defaults.inc.php

$graphite = fsockopen($config['graphite']['host'], $graphite_port);
} else {
$graphite = false;
}

rrdtool_initialize();

echo "Starting polling run:\n\n";
Expand All @@ -141,6 +156,10 @@
$poller_run = ($poller_end - $poller_start);
$poller_time = substr($poller_run, 0, 5);

if ($graphite !== false) {
fclose($graphite);
}

if ($polled_devices) {
dbInsert(array('type' => 'poll', 'doing' => $doing, 'start' => $poller_start, 'duration' => $poller_time, 'devices' => $polled_devices, 'poller' => $config['distributed_poller_name'] ), 'perf_times');
}
Expand Down