Output image for graphs with no data (#11865)

* Output image for graphs with no data

* add font

* Fix graph error, try to detect rrd missing

* centralize graph_error
use it for graph_text_and_exit()

* Add SVG, right now the text can get a little big...

* fix style
This commit is contained in:
Tony Murray
2021-01-13 07:23:47 -06:00
committed by GitHub
parent ed41e6eedc
commit b07b81021a
7 changed files with 98 additions and 118 deletions

BIN
html/fonts/DejaVuSans.ttf Normal file

Binary file not shown.

View File

@@ -591,6 +591,62 @@ function generate_port_thumbnail($port)
return generate_port_image($port);
}//end generate_port_thumbnail()
/**
* Create image to output text instead of a graph.
*
* @param string $text
* @param int[] $color
*/
function graph_error($text, $color = [128, 0, 0])
{
global $vars, $debug;
if (! $debug) {
set_image_type();
}
$width = $vars['width'] ?? 150;
$height = $vars['height'] ?? 60;
if (Config::get('webui.graph_type') === 'svg') {
$rgb = implode(', ', $color);
$font_size = 20;
$svg_x = 100;
$svg_y = min($font_size, $width ? (($height / $width) * $svg_x) : 1);
echo "<svg viewBox=\"0 0 $svg_x $svg_y\" xmlns=\"http://www.w3.org/2000/svg\"><text x=\"50%\" y=\"50%\" dominant-baseline=\"middle\" text-anchor=\"middle\" style=\"font-family: sans-serif; fill: rgb($rgb);\">$text</text></svg>";
} else {
$img = imagecreate($width, $height);
imagecolorallocatealpha($img, 255, 255, 255, 127); // transparent background
$px = ((imagesx($img) - 7.5 * strlen($text)) / 2);
$font = $width < 200 ? 3 : 5;
imagestring($img, $font, $px, ($height / 2 - 8), $text, imagecolorallocate($img, ...$color));
// Output the image
imagepng($img);
imagedestroy($img);
}
}
/**
* Output message to user in image format.
*
* @param string $text string to display
*/
function graph_text_and_exit($text)
{
global $vars;
if ($vars['showcommand'] == 'yes') {
echo $text;
return;
}
graph_error($text, [13, 21, 210]);
exit;
}
function print_port_thumbnail($args)
{
echo generate_port_link($args, generate_port_image($args));

View File

@@ -15,10 +15,6 @@ if ($_GET['width']) {
$width = (int) $_GET['width'];
}
if (\LibreNMS\Config::get('trim_tobias')) {
$width += 12;
}
if ($_GET['height']) {
$height = (int) $_GET['height'];
}

View File

@@ -4,7 +4,13 @@
$ds_in = 'INOCTETS';
$ds_out = 'OUTOCTETS';
foreach (dbFetchRows('SELECT * FROM `ports` WHERE `device_id` = ? AND `disabled` = 0 AND `deleted` = 0', [$device['device_id']]) as $port) {
$ports = dbFetchRows('SELECT * FROM `ports` WHERE `device_id` = ? AND `disabled` = 0 AND `deleted` = 0', [$device['device_id']]);
if (empty($ports)) {
graph_text_and_exit('No Ports');
}
foreach ($ports as $port) {
$ignore = 0;
if (is_array(\LibreNMS\Config::get('device_traffic_iftype'))) {
foreach (\LibreNMS\Config::get('device_traffic_iftype') as $iftype) {

View File

@@ -2,6 +2,10 @@
$procs = dbFetchRows('SELECT * FROM `processors` where `device_id` = ?', [$device['device_id']]);
if (empty($procs)) {
graph_text_and_exit('No Processors');
}
if (\LibreNMS\Config::getOsSetting($device['os'], 'processor_stacked')) {
include 'includes/html/graphs/device/processor_stack.inc.php';
} else {

View File

@@ -8,7 +8,13 @@ require 'includes/html/graphs/common.inc.php';
$iter = '1';
$rrd_options .= " COMMENT:' Size Used % Used\\l'";
foreach (dbFetchRows('SELECT * FROM storage where device_id = ?', [$device['device_id']]) as $storage) {
$storages = dbFetchRows('SELECT * FROM storage where device_id = ?', [$device['device_id']]);
if (empty($storages)) {
graph_text_and_exit('No Storage');
}
foreach ($storages as $storage) {
// FIXME generic colour function
if ($iter == '1') {
$colour = 'CC0000';

View File

@@ -47,53 +47,12 @@ if ($auth && is_customoid_graph($type, $subtype)) {
// Graph Template Missing");
}
function graph_error($string)
{
global $vars, $debug, $graphfile;
$vars['bg'] = 'FFBBBB';
include 'includes/html/graphs/common.inc.php';
$rrd_options .= ' HRULE:0#555555';
$rrd_options .= ' --title=' . escapeshellarg($string);
rrdtool_graph($graphfile, $rrd_options);
if ($height > '99') {
shell_exec($rrd_cmd);
d_echo('<pre>' . $rrd_cmd . '</pre>');
if (is_file($graphfile) && ! $debug) {
header('Content-type: image/png');
$fd = fopen($graphfile, 'r');
fpassthru($fd);
fclose($fd);
unlink($graphfile);
}
} else {
if (! $debug) {
header('Content-type: image/png');
}
$im = imagecreate($width, $height);
$px = ((imagesx($im) - 7.5 * strlen($string)) / 2);
imagestring($im, 3, $px, ($height / 2 - 8), $string, imagecolorallocate($im, 128, 0, 0));
imagepng($im);
imagedestroy($im);
}
}
if ($error_msg) {
// We have an error :(
graph_error($graph_error);
} elseif ($auth === null) {
// We are unauthenticated :(
if ($width < 200) {
graph_error('No Auth');
} else {
graph_error('No Authorisation');
}
graph_error($width < 200 ? 'No Auth' : 'No Authorization');
} else {
// $rrd_options .= " HRULE:0#999999";
if ($graph_type === 'svg') {
@@ -103,13 +62,7 @@ if ($error_msg) {
}
}
if ($no_file) {
if ($width < 200) {
graph_error('No RRD');
} else {
graph_error('Missing RRD Datafile');
}
} elseif ($command_only) {
if ($command_only) {
echo "<div class='infobox'>";
echo "<p style='font-size: 16px; font-weight: bold;'>RRDTool Command</p>";
echo "<pre class='rrd-pre'>";
@@ -122,47 +75,14 @@ if ($error_msg) {
echo '</pre>';
unlink($graphfile);
echo '</div>';
} else {
if ($rrd_options) {
} elseif ($no_file) {
graph_error($width < 200 ? 'No Data' : 'No Data file');
} elseif ($rrd_options) {
rrdtool_graph($graphfile, $rrd_options);
d_echo($rrd_cmd);
if (is_file($graphfile)) {
if (! $debug) {
set_image_type();
if (Config::get('trim_tobias') && $graph_type !== 'svg') {
[$w, $h, $type, $attr] = getimagesize($graphfile);
$src_im = imagecreatefrompng($graphfile);
$src_x = '0';
// begin x
$src_y = '0';
// begin y
$src_w = ($w - 12);
// width
$src_h = $h;
// height
$dst_x = '0';
// destination x
$dst_y = '0';
// destination y
$dst_im = imagecreatetruecolor($src_w, $src_h);
imagesavealpha($dst_im, true);
$white = imagecolorallocate($dst_im, 255, 255, 255);
$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);
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 {
if ($output === 'base64') {
$imagedata = file_get_contents($graphfile);
$base64_output = base64_encode($imagedata);
@@ -171,25 +91,17 @@ if ($error_msg) {
fpassthru($fd);
fclose($fd);
}
}
} else {
echo `ls -l $graphfile`;
echo '<img src="' . data_uri($graphfile, 'image/svg+xml') . '" alt="graph" />';
}
unlink($graphfile);
} elseif (isset($rrd_filename) && ! Rrd::checkRrdExists($rrd_filename)) {
graph_error($width < 200 ? 'No Data' : 'No Data file');
} else {
if ($width < 200) {
graph_error('Draw Error');
} else {
graph_error('Error Drawing Graph');
}
graph_error($width < 200 ? 'Draw Error' : 'Error Drawing Graph');
}
} else {
if ($width < 200) {
graph_error('Def Error');
} else {
graph_error('Graph Definition Error');
}
}
graph_error($width < 200 ? 'Def Error' : 'Graph Definition Error');
}
}