mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Graphing cleanup (#14492)
* WIP * Fixes and cleanups * Move parseAtTime to Time util class * lint fixes, explicitly define variables * Style fixes
This commit is contained in:
@@ -91,10 +91,10 @@ function api_get_graph(array $vars)
|
||||
$graph = Graph::get($vars);
|
||||
|
||||
if ($vars['output'] === 'base64') {
|
||||
return api_success(['image' => $graph->base64(), 'content-type' => $graph->imageType()], 'image');
|
||||
return api_success(['image' => $graph->base64(), 'content-type' => $graph->contentType()], 'image');
|
||||
}
|
||||
|
||||
return response($graph->data(), 200, ['Content-Type' => $graph->imageType()]);
|
||||
return response($graph->data, 200, ['Content-Type' => $graph->contentType()]);
|
||||
} catch (\LibreNMS\Exceptions\RrdGraphException $e) {
|
||||
return api_error(500, $e->getMessage());
|
||||
}
|
||||
|
@@ -989,17 +989,6 @@ function eventlog_severity($eventlog_severity)
|
||||
}
|
||||
} // end eventlog_severity
|
||||
|
||||
/**
|
||||
* Get the http content type of the image
|
||||
*
|
||||
* @param string $type svg or png
|
||||
* @return string
|
||||
*/
|
||||
function get_image_type(string $type)
|
||||
{
|
||||
return \LibreNMS\Util\Graph::imageType($type);
|
||||
}
|
||||
|
||||
function get_oxidized_nodes_list()
|
||||
{
|
||||
$context = stream_context_create([
|
||||
@@ -1045,36 +1034,6 @@ function generate_stacked_graphs($transparency = '88')
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse AT time spec, does not handle the entire spec.
|
||||
*
|
||||
* @param string|int $time
|
||||
* @return int
|
||||
*/
|
||||
function parse_at_time($time)
|
||||
{
|
||||
if (is_numeric($time)) {
|
||||
return $time < 0 ? time() + $time : intval($time);
|
||||
}
|
||||
|
||||
if (preg_match('/^[+-]\d+[hdmy]$/', $time)) {
|
||||
$units = [
|
||||
'm' => 60,
|
||||
'h' => 3600,
|
||||
'd' => 86400,
|
||||
'y' => 31557600,
|
||||
];
|
||||
$value = substr($time, 1, -1);
|
||||
$unit = substr($time, -1);
|
||||
|
||||
$offset = ($time[0] == '-' ? -1 : 1) * $units[$unit] * $value;
|
||||
|
||||
return time() + $offset;
|
||||
}
|
||||
|
||||
return (int) strtotime($time);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sysname of a device with a html line break prepended.
|
||||
* if the device has an empty sysname it will return device's hostname instead
|
||||
|
@@ -1,119 +1,7 @@
|
||||
<?php
|
||||
|
||||
use LibreNMS\Config;
|
||||
use LibreNMS\Util\Clean;
|
||||
|
||||
if (isset($_GET['from']) && $_GET['from']) {
|
||||
$from = parse_at_time($_GET['from']);
|
||||
}
|
||||
|
||||
if (isset($_GET['to']) && $_GET['to']) {
|
||||
$to = parse_at_time($_GET['to']);
|
||||
}
|
||||
|
||||
if (isset($_GET['width']) && $_GET['width']) {
|
||||
$width = (int) $_GET['width'];
|
||||
}
|
||||
|
||||
if (isset($_GET['height']) && $_GET['height']) {
|
||||
$height = (int) $_GET['height'];
|
||||
}
|
||||
|
||||
if (! empty($_GET['inverse'])) {
|
||||
$in = 'out';
|
||||
$out = 'in';
|
||||
$inverse = true;
|
||||
} else {
|
||||
$in = 'in';
|
||||
$out = 'out';
|
||||
}
|
||||
|
||||
if (isset($_GET['legend']) && $_GET['legend'] == 'no') {
|
||||
$rrd_options .= ' -g';
|
||||
}
|
||||
|
||||
if (isset($_GET['nototal'])) {
|
||||
$nototal = ((bool) $_GET['nototal']);
|
||||
} else {
|
||||
$nototal = true;
|
||||
}
|
||||
|
||||
if (isset($_GET['nodetails'])) {
|
||||
$nodetails = ((bool) $_GET['nodetails']);
|
||||
} else {
|
||||
$nodetails = false;
|
||||
}
|
||||
|
||||
if (isset($_GET['noagg'])) {
|
||||
$noagg = ((bool) $_GET['noagg']);
|
||||
} else {
|
||||
$noagg = true;
|
||||
}
|
||||
|
||||
if (isset($_GET['title']) && $_GET['title'] == 'yes') {
|
||||
$rrd_options .= " --title='" . $graph_title . "' ";
|
||||
}
|
||||
|
||||
if (isset($_GET['graph_title'])) {
|
||||
$rrd_options .= " --title='" . Clean::alphaDashSpace($_GET['graph_title']) . "' ";
|
||||
}
|
||||
|
||||
if (! isset($scale_min) && ! isset($scale_max)) {
|
||||
$rrd_options .= ' --alt-autoscale-max';
|
||||
}
|
||||
|
||||
if (! isset($scale_min) && ! isset($scale_max) && ! isset($norigid)) {
|
||||
$rrd_options .= ' --rigid';
|
||||
}
|
||||
|
||||
if (isset($scale_min)) {
|
||||
$rrd_options .= " -l $scale_min";
|
||||
}
|
||||
|
||||
if (isset($scale_max)) {
|
||||
$rrd_options .= " -u $scale_max";
|
||||
}
|
||||
|
||||
if (isset($scale_rigid)) {
|
||||
$rrd_options .= ' -r';
|
||||
}
|
||||
|
||||
if (! isset($float_precision)) {
|
||||
$float_precision = 2;
|
||||
}
|
||||
|
||||
$rrd_options .= ' -E --start ' . $from . ' --end ' . $to . ' --width ' . $width . ' --height ' . $height . ' ';
|
||||
|
||||
if (Config::get('applied_site_style') == 'dark') {
|
||||
$rrd_options .= \LibreNMS\Config::get('rrdgraph_def_text_dark') . ' -c FONT#' . ltrim(\LibreNMS\Config::get('rrdgraph_def_text_color_dark'), '#');
|
||||
} else {
|
||||
$rrd_options .= \LibreNMS\Config::get('rrdgraph_def_text') . ' -c FONT#' . ltrim(\LibreNMS\Config::get('rrdgraph_def_text_color'), '#');
|
||||
}
|
||||
|
||||
if (! empty($_GET['bg'])) {
|
||||
$rrd_options .= ' -c CANVAS#' . Clean::alphaDash($_GET['bg']) . ' ';
|
||||
}
|
||||
|
||||
if (! empty($_GET['bbg'])) {
|
||||
$rrd_options .= ' -c BACK#' . Clean::alphaDash($_GET['bbg']) . ' ';
|
||||
}
|
||||
|
||||
if (! empty($_GET['font'])) {
|
||||
$rrd_options .= ' -c FONT#' . Clean::alphaDash($_GET['font']) . ' ';
|
||||
}
|
||||
|
||||
if ($height < '99') {
|
||||
$rrd_options .= ' --only-graph';
|
||||
}
|
||||
|
||||
if ($width <= '300') {
|
||||
$rrd_options .= ' --font LEGEND:7:' . \LibreNMS\Config::get('mono_font') . ' --font AXIS:6:' . \LibreNMS\Config::get('mono_font');
|
||||
} else {
|
||||
$rrd_options .= ' --font LEGEND:8:' . \LibreNMS\Config::get('mono_font') . ' --font AXIS:7:' . \LibreNMS\Config::get('mono_font');
|
||||
}
|
||||
|
||||
$rrd_options .= ' --font-render-mode normal';
|
||||
|
||||
if (isset($_GET['absolute']) && $_GET['absolute'] == '1') {
|
||||
$rrd_options .= ' --full-size-mode';
|
||||
}
|
||||
// populate legacy global variables
|
||||
$graph_params->scale_max = isset($scale_max) ? (int) $scale_max : $graph_params->scale_max;
|
||||
$graph_params->scale_min = isset($scale_min) ? (int) $scale_min : $graph_params->scale_min;
|
||||
$graph_params->scale_rigid = $scale_rigid ?? $graph_params->scale_rigid;
|
||||
$graph_params->title = $graph_title ?? $graph_params->title;
|
||||
|
@@ -1,37 +1,41 @@
|
||||
<?php
|
||||
|
||||
use LibreNMS\Config;
|
||||
use LibreNMS\Data\Graphing\GraphParameters;
|
||||
use LibreNMS\Enum\ImageFormat;
|
||||
|
||||
global $debug;
|
||||
|
||||
[$type, $subtype] = extract_graph_type($vars['type']);
|
||||
|
||||
if (isset($vars['device'])) {
|
||||
$device = is_numeric($vars['device'])
|
||||
? device_by_id_cache($vars['device'])
|
||||
: device_by_name($vars['device']);
|
||||
DeviceCache::setPrimary($device['device_id']);
|
||||
}
|
||||
|
||||
// FIXME -- remove these
|
||||
$width = $vars['width'] ?? 400;
|
||||
$height = $vars['height'] ?? round($width / 3);
|
||||
$title = $vars['title'] ?? '';
|
||||
$vertical = $vars['vertical'] ?? '';
|
||||
$legend = $vars['legend'] ?? false;
|
||||
$output = (! empty($vars['output']) ? $vars['output'] : 'default');
|
||||
$from = empty($vars['from']) ? Config::get('time.day') : parse_at_time($vars['from']);
|
||||
$to = empty($vars['to']) ? Config::get('time.now') : parse_at_time($vars['to']);
|
||||
$period = ($to - $from);
|
||||
$prev_from = ($from - $period);
|
||||
|
||||
$graph_image_type = $vars['graph_type'] ?? Config::get('webui.graph_type');
|
||||
// variables for included graphs
|
||||
$graph_params = new GraphParameters($vars);
|
||||
// set php variables for legacy graphs
|
||||
$type = $graph_params->type;
|
||||
$subtype = $graph_params->subtype;
|
||||
$height = $graph_params->height;
|
||||
$width = $graph_params->width;
|
||||
$from = $graph_params->from;
|
||||
$to = $graph_params->to;
|
||||
$period = $graph_params->period;
|
||||
$prev_from = $graph_params->prev_from;
|
||||
$inverse = $graph_params->inverse;
|
||||
$in = $graph_params->in;
|
||||
$out = $graph_params->out;
|
||||
$float_precision = $graph_params->float_precision;
|
||||
$title = $graph_params->visible('title');
|
||||
$nototal = ! $graph_params->visible('total');
|
||||
$nodetails = ! $graph_params->visible('details');
|
||||
$noagg = ! $graph_params->visible('aggregate');
|
||||
$rrd_options = '';
|
||||
|
||||
require Config::get('install_dir') . "/includes/html/graphs/$type/auth.inc.php";
|
||||
|
||||
//set default graph title
|
||||
$graph_title = format_hostname($device);
|
||||
|
||||
if ($auth && is_customoid_graph($type, $subtype)) {
|
||||
$unit = $vars['unit'];
|
||||
include Config::get('install_dir') . '/includes/html/graphs/customoid/customoid.inc.php';
|
||||
@@ -49,12 +53,7 @@ if ($auth === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($graph_image_type === 'svg') {
|
||||
$rrd_options .= ' --imgformat=SVG';
|
||||
if ($width < 350) {
|
||||
$rrd_options .= ' -m 0.75 -R light';
|
||||
}
|
||||
}
|
||||
$rrd_options = $graph_params . ' ' . $rrd_options;
|
||||
|
||||
// command output requested
|
||||
if (! empty($command_only)) {
|
||||
@@ -88,9 +87,9 @@ try {
|
||||
|
||||
// output the graph
|
||||
if (\LibreNMS\Util\Debug::isEnabled()) {
|
||||
echo '<img src="data:' . get_image_type($graph_image_type) . ';base64,' . base64_encode($image_data) . '" alt="graph" />';
|
||||
echo '<img src="data:' . ImageFormat::forGraph()->contentType() . ';base64,' . base64_encode($image_data) . '" alt="graph" />';
|
||||
} else {
|
||||
header('Content-type: ' . get_image_type(Config::get('webui.graph_type')));
|
||||
header('Content-type: ' . ImageFormat::forGraph()->contentType());
|
||||
echo $output === 'base64' ? base64_encode($image_data) : $image_data;
|
||||
}
|
||||
} catch (\LibreNMS\Exceptions\RrdGraphException $e) {
|
||||
|
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use LibreNMS\Config;
|
||||
use LibreNMS\Util\Time;
|
||||
|
||||
unset($vars['page']);
|
||||
|
||||
@@ -14,8 +15,8 @@ if (session('widescreen')) {
|
||||
$thumb_width = 113;
|
||||
}
|
||||
|
||||
$vars['from'] = parse_at_time($vars['from'] ?? '') ?: Config::get('time.day');
|
||||
$vars['to'] = parse_at_time($vars['to'] ?? '') ?: Config::get('time.now');
|
||||
$vars['from'] = Time::parseAt($vars['from'] ?? '') ?: Config::get('time.day');
|
||||
$vars['to'] = Time::parseAt($vars['to'] ?? '') ?: Config::get('time.now');
|
||||
|
||||
preg_match('/^(?P<type>[A-Za-z0-9]+)_(?P<subtype>.+)/', $vars['type'], $graphtype);
|
||||
|
||||
@@ -188,6 +189,7 @@ if (! $auth) {
|
||||
}
|
||||
|
||||
if (! empty($vars['showcommand'])) {
|
||||
$vars = $graph_array;
|
||||
$_GET = $graph_array;
|
||||
$command_only = 1;
|
||||
|
||||
|
Reference in New Issue
Block a user