mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
api: Add option to output graphs as base64 (#8535)
* Allow 'option' param in graph endpoint * Update lint and update docs * Uncomment api response * Add imagedestroy on 'tobias_trim' section * Add base64 to port endpoints also
This commit is contained in:
committed by
Tony Murray
parent
c828114639
commit
ff0063b515
@@ -396,6 +396,7 @@ Input:
|
||||
- to: This is the date you would like the graph to end - See http://oss.oetiker.ch/rrdtool/doc/rrdgraph.en.html for more information.
|
||||
- width: The graph width, defaults to 1075.
|
||||
- height: The graph height, defaults to 300.
|
||||
- output: Set how the graph should be outputted (base64, display), defaults to display.
|
||||
|
||||
Example:
|
||||
```curl
|
||||
|
||||
@@ -140,6 +140,7 @@ function get_graph_by_port_hostname()
|
||||
$vars = array();
|
||||
$vars['port'] = urldecode($router['ifname']);
|
||||
$vars['type'] = $router['type'] ?: 'port_bits';
|
||||
$vars['output'] = $_GET['output'] ?: 'display';
|
||||
if (!empty($_GET['from'])) {
|
||||
$vars['from'] = $_GET['from'];
|
||||
}
|
||||
@@ -164,6 +165,9 @@ function get_graph_by_port_hostname()
|
||||
rrdtool_initialize(false);
|
||||
include 'includes/graphs/graph.inc.php';
|
||||
rrdtool_close();
|
||||
if ($vars['output'] === 'base64') {
|
||||
api_success(['image' => $base64_output, 'content-type' => get_image_type()], 'image');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -212,6 +216,7 @@ function get_graph_generic_by_hostname()
|
||||
$sensor_id = $router['sensor_id'] ?: null;
|
||||
$vars = array();
|
||||
$vars['type'] = $router['type'] ?: 'device_uptime';
|
||||
$vars['output'] = $_GET['output'] ?: 'display';
|
||||
if (isset($sensor_id)) {
|
||||
$vars['id'] = $sensor_id;
|
||||
if (str_contains($vars['type'], '_wireless')) {
|
||||
@@ -244,6 +249,10 @@ function get_graph_generic_by_hostname()
|
||||
rrdtool_initialize(false);
|
||||
include 'includes/graphs/graph.inc.php';
|
||||
rrdtool_close();
|
||||
|
||||
if ($vars['output'] === 'base64') {
|
||||
api_success(['image' => $base64_output, 'content-type' => get_image_type()], 'image');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -628,6 +637,7 @@ function get_graph_by_portgroup()
|
||||
$group = $router['group'] ?: '';
|
||||
$id = $router['id'] ?: '';
|
||||
$vars = array();
|
||||
$vars['output'] = $_GET['output'] ?: 'display';
|
||||
if (!empty($_GET['from'])) {
|
||||
$vars['from'] = $_GET['from'];
|
||||
}
|
||||
@@ -662,6 +672,9 @@ function get_graph_by_portgroup()
|
||||
rrdtool_initialize(false);
|
||||
include 'includes/graphs/graph.inc.php';
|
||||
rrdtool_close();
|
||||
if ($vars['output'] === 'base64') {
|
||||
api_success(['image' => $base64_output, 'content-type' => get_image_type()], 'image');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ $height = $vars['height'];
|
||||
$title = $vars['title'];
|
||||
$vertical = $vars['vertical'];
|
||||
$legend = $vars['legend'];
|
||||
|
||||
$output = (!empty($vars['output']) ? $vars['output'] : 'default');
|
||||
$from = (isset($vars['from']) ? $vars['from'] : time() - 60 * 60 * 24);
|
||||
$to = (isset($vars['to']) ? $vars['to'] : time());
|
||||
|
||||
@@ -28,7 +28,7 @@ if ($from < 0) {
|
||||
}
|
||||
|
||||
$period = ($to - $from);
|
||||
|
||||
$base64_output = '';
|
||||
$prev_from = ($from - $period);
|
||||
|
||||
$graphfile = $config['temp_dir'].'/'.strgen();
|
||||
@@ -132,7 +132,6 @@ if ($error_msg) {
|
||||
if ($rrd_options) {
|
||||
rrdtool_graph($graphfile, $rrd_options);
|
||||
d_echo($rrd_cmd);
|
||||
|
||||
if (is_file($graphfile)) {
|
||||
if (!$debug) {
|
||||
set_image_type();
|
||||
@@ -157,12 +156,32 @@ if ($error_msg) {
|
||||
$trans_colour = imagecolorallocatealpha($dst_im, 0, 0, 0, 127);
|
||||
imagefill($dst_im, 0, 0, $trans_colour);
|
||||
imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
|
||||
imagepng($dst_im);
|
||||
imagedestroy($dst_im);
|
||||
if ($output === 'base64') {
|
||||
ob_start();
|
||||
imagepng($png);
|
||||
$imagedata = ob_get_contents();
|
||||
imagedestroy($png);
|
||||
ob_end_clean();
|
||||
|
||||
$base64_output = base64_encode($imagedata);
|
||||
} else {
|
||||
imagepng($dst_im);
|
||||
imagedestroy($dst_im);
|
||||
}
|
||||
} else {
|
||||
$fd = fopen($graphfile, 'r');
|
||||
fpassthru($fd);
|
||||
fclose($fd);
|
||||
if ($output === 'base64') {
|
||||
$fd = fopen($graphfile, 'r');
|
||||
ob_start();
|
||||
fpassthru($fd);
|
||||
$imagedata = ob_get_contents();
|
||||
fclose($fd);
|
||||
ob_end_clean();
|
||||
$base64_output = base64_encode($imagedata);
|
||||
} else {
|
||||
$fd = fopen($graphfile, 'r');
|
||||
fpassthru($fd);
|
||||
fclose($fd);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
echo `ls -l $graphfile`;
|
||||
|
||||
Reference in New Issue
Block a user