mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
This reverts commit 2833d935e0.
This commit is contained in:
@@ -28,11 +28,9 @@ use Illuminate\Support\Str;
|
|||||||
use LibreNMS\Config;
|
use LibreNMS\Config;
|
||||||
use LibreNMS\Data\Measure\Measurement;
|
use LibreNMS\Data\Measure\Measurement;
|
||||||
use LibreNMS\Exceptions\FileExistsException;
|
use LibreNMS\Exceptions\FileExistsException;
|
||||||
use LibreNMS\Exceptions\RrdGraphException;
|
|
||||||
use LibreNMS\Proc;
|
use LibreNMS\Proc;
|
||||||
use LibreNMS\Util\Rewrite;
|
use LibreNMS\Util\Rewrite;
|
||||||
use Log;
|
use Log;
|
||||||
use Symfony\Component\Process\Process;
|
|
||||||
|
|
||||||
class Rrd extends BaseDatastore
|
class Rrd extends BaseDatastore
|
||||||
{
|
{
|
||||||
@@ -547,26 +545,25 @@ class Rrd extends BaseDatastore
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a graph file at $graph_file using $options
|
* Generates a graph file at $graph_file using $options
|
||||||
* Graphs are a single command per run, so this just runs rrdtool
|
* Opens its own rrdtool pipe.
|
||||||
*
|
*
|
||||||
* @param string $options
|
* @param string $graph_file
|
||||||
* @return string
|
* @param string $options
|
||||||
* @throws \LibreNMS\Exceptions\FileExistsException
|
* @return string|int
|
||||||
* @throws \LibreNMS\Exceptions\RrdGraphException
|
|
||||||
*/
|
*/
|
||||||
public function graph(string $options): string
|
public function graph($graph_file, $options)
|
||||||
{
|
{
|
||||||
$cmd = $this->buildCommand('graph', '-', $options);
|
if ($this->init(false)) {
|
||||||
$process = Process::fromShellCommandline(Config::get('rrdtool') . ' ' . $cmd);
|
$cmd = $this->buildCommand('graph', $graph_file, $options);
|
||||||
$process->setTimeout(300);
|
|
||||||
$process->setIdleTimeout(300);
|
|
||||||
$process->run();
|
|
||||||
|
|
||||||
if (! $process->isSuccessful()) {
|
$output = implode($this->sync_process->sendCommand($cmd));
|
||||||
throw new RrdGraphException($process->getErrorOutput(), $process->getExitCode(), $process->getOutput());
|
|
||||||
|
d_echo("<p>$cmd</p>\n<p>command returned ($output)</p>");
|
||||||
|
|
||||||
|
return $output;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $process->getOutput();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __destruct()
|
public function __destruct()
|
||||||
|
|||||||
@@ -1,44 +0,0 @@
|
|||||||
<?php
|
|
||||||
/*
|
|
||||||
* RrdGraphException.php
|
|
||||||
*
|
|
||||||
* Exception generated when there is an error creating the graph image
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*
|
|
||||||
* @package LibreNMS
|
|
||||||
* @link http://librenms.org
|
|
||||||
* @copyright 2021 Tony Murray
|
|
||||||
* @author Tony Murray <murraytony@gmail.com>
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace LibreNMS\Exceptions;
|
|
||||||
|
|
||||||
use Exception;
|
|
||||||
|
|
||||||
class RrdGraphException extends Exception
|
|
||||||
{
|
|
||||||
protected $image_output;
|
|
||||||
|
|
||||||
public function __construct($error, $exit_code, $image_output)
|
|
||||||
{
|
|
||||||
parent::__construct($error, $exit_code);
|
|
||||||
$this->image_output = $image_output;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getImage()
|
|
||||||
{
|
|
||||||
return $this->image_output;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -81,7 +81,8 @@ function api_get_graph(array $vars)
|
|||||||
{
|
{
|
||||||
global $dur; // Needed for callback within graph code
|
global $dur; // Needed for callback within graph code
|
||||||
|
|
||||||
$auth = true;
|
$auth = '1';
|
||||||
|
$base64_output = '';
|
||||||
|
|
||||||
// prevent ugly error for undefined graphs from being passed to the user
|
// prevent ugly error for undefined graphs from being passed to the user
|
||||||
[$type, $subtype] = extract_graph_type($vars['type']);
|
[$type, $subtype] = extract_graph_type($vars['type']);
|
||||||
@@ -98,10 +99,10 @@ function api_get_graph(array $vars)
|
|||||||
ob_end_clean();
|
ob_end_clean();
|
||||||
|
|
||||||
if ($vars['output'] === 'base64') {
|
if ($vars['output'] === 'base64') {
|
||||||
return api_success(['image' => $image, 'content-type' => get_image_type(Config::get('webui.graph_type'))], 'image');
|
return api_success(['image' => $base64_output, 'content-type' => get_image_type()], 'image');
|
||||||
}
|
}
|
||||||
|
|
||||||
return response($image, 200, ['Content-Type' => get_image_type(Config::get('webui.graph_type'))]);
|
return response($image, 200, ['Content-Type' => get_image_type()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
function check_bill_permission($bill_id, $callback)
|
function check_bill_permission($bill_id, $callback)
|
||||||
|
|||||||
@@ -46,6 +46,14 @@ function var_get($v)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function data_uri($file, $mime)
|
||||||
|
{
|
||||||
|
$contents = file_get_contents($file);
|
||||||
|
$base64 = base64_encode($contents);
|
||||||
|
|
||||||
|
return 'data:' . $mime . ';base64,' . $base64;
|
||||||
|
}//end data_uri()
|
||||||
|
|
||||||
function toner2colour($descr, $percent)
|
function toner2colour($descr, $percent)
|
||||||
{
|
{
|
||||||
$colour = \LibreNMS\Util\Colors::percentage(100 - $percent, null);
|
$colour = \LibreNMS\Util\Colors::percentage(100 - $percent, null);
|
||||||
@@ -456,30 +464,19 @@ function graph_error($text, $color = [128, 0, 0])
|
|||||||
{
|
{
|
||||||
global $vars, $debug;
|
global $vars, $debug;
|
||||||
|
|
||||||
$type = Config::get('webui.graph_type');
|
|
||||||
if (! $debug) {
|
if (! $debug) {
|
||||||
header('Content-type: ' . get_image_type($type));
|
set_image_type();
|
||||||
}
|
}
|
||||||
|
|
||||||
$width = (int) ($vars['width'] ?? 150);
|
$width = $vars['width'] ?? 150;
|
||||||
$height = (int) ($vars['height'] ?? 60);
|
$height = $vars['height'] ?? 60;
|
||||||
|
|
||||||
if ($type === 'svg') {
|
if (Config::get('webui.graph_type') === 'svg') {
|
||||||
$rgb = implode(', ', $color);
|
$rgb = implode(', ', $color);
|
||||||
echo <<<SVG
|
$font_size = 20;
|
||||||
<svg xmlns="http://www.w3.org/2000/svg"
|
$svg_x = 100;
|
||||||
xmlns:xhtml="http://www.w3.org/1999/xhtml"
|
$svg_y = min($font_size, $width ? (($height / $width) * $svg_x) : 1);
|
||||||
viewBox="0 0 $width $height"
|
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>";
|
||||||
preserveAspectRatio="xMinYMin">
|
|
||||||
<foreignObject x="0" y="0" width="$width" height="$height" transform="translate(0,0)">
|
|
||||||
<xhtml:div style="display:table; width:{$width}px; height:{$height}px; overflow:hidden;">
|
|
||||||
<xhtml:div style="display:table-cell; vertical-align:middle;">
|
|
||||||
<xhtml:div style="color:rgb($rgb); text-align:center; font-family:sans-serif; font-size:0.6em;">$text</xhtml:div>
|
|
||||||
</xhtml:div>
|
|
||||||
</xhtml:div>
|
|
||||||
</foreignObject>
|
|
||||||
</svg>
|
|
||||||
SVG;
|
|
||||||
} else {
|
} else {
|
||||||
$img = imagecreate($width, $height);
|
$img = imagecreate($width, $height);
|
||||||
imagecolorallocatealpha($img, 255, 255, 255, 127); // transparent background
|
imagecolorallocatealpha($img, 255, 255, 255, 127); // transparent background
|
||||||
@@ -1022,14 +1019,18 @@ function eventlog_severity($eventlog_severity)
|
|||||||
}
|
}
|
||||||
} // end eventlog_severity
|
} // end eventlog_severity
|
||||||
|
|
||||||
/**
|
function set_image_type()
|
||||||
* Get the http content type of the image
|
|
||||||
* @param string $type svg or png
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
function get_image_type(string $type)
|
|
||||||
{
|
{
|
||||||
return $type === 'svg' ? 'image/svg+xml' : 'image/png';
|
header('Content-type: ' . get_image_type());
|
||||||
|
}
|
||||||
|
|
||||||
|
function get_image_type()
|
||||||
|
{
|
||||||
|
if (Config::get('webui.graph_type') === 'svg') {
|
||||||
|
return 'image/svg+xml';
|
||||||
|
} else {
|
||||||
|
return 'image/png';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_oxidized_nodes_list()
|
function get_oxidized_nodes_list()
|
||||||
|
|||||||
@@ -38,8 +38,8 @@ foreach (dbFetchRows('SELECT * FROM `sensors` WHERE `sensor_class` = ? AND `devi
|
|||||||
}//end switch
|
}//end switch
|
||||||
|
|
||||||
$sensor_descr_fixed = \LibreNMS\Data\Store\Rrd::fixedSafeDescr($sensor['sensor_descr'], 12);
|
$sensor_descr_fixed = \LibreNMS\Data\Store\Rrd::fixedSafeDescr($sensor['sensor_descr'], 12);
|
||||||
$rrd_filename = get_sensor_rrd($device, $sensor);
|
$rrd_file = get_sensor_rrd($device, $sensor);
|
||||||
$rrd_options .= " DEF:sensor{$sensor['sensor_id']}=$rrd_filename:sensor:AVERAGE";
|
$rrd_options .= " DEF:sensor{$sensor['sensor_id']}=$rrd_file:sensor:AVERAGE";
|
||||||
$rrd_options .= " LINE1:sensor{$sensor['sensor_id']}#$colour:'$sensor_descr_fixed'";
|
$rrd_options .= " LINE1:sensor{$sensor['sensor_id']}#$colour:'$sensor_descr_fixed'";
|
||||||
$rrd_options .= " GPRINT:sensor{$sensor['sensor_id']}:LAST:%5.1lf$unit";
|
$rrd_options .= " GPRINT:sensor{$sensor['sensor_id']}:LAST:%5.1lf$unit";
|
||||||
$rrd_options .= " GPRINT:sensor{$sensor['sensor_id']}:MIN:%5.1lf$unit";
|
$rrd_options .= " GPRINT:sensor{$sensor['sensor_id']}:MIN:%5.1lf$unit";
|
||||||
|
|||||||
@@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
use LibreNMS\Config;
|
use LibreNMS\Config;
|
||||||
|
|
||||||
global $debug;
|
|
||||||
|
|
||||||
// Push $_GET into $vars to be compatible with web interface naming
|
// Push $_GET into $vars to be compatible with web interface naming
|
||||||
foreach ($_GET as $name => $value) {
|
foreach ($_GET as $name => $value) {
|
||||||
$vars[$name] = $value;
|
$vars[$name] = $value;
|
||||||
@@ -26,12 +24,13 @@ $legend = $vars['legend'];
|
|||||||
$output = (! empty($vars['output']) ? $vars['output'] : 'default');
|
$output = (! empty($vars['output']) ? $vars['output'] : 'default');
|
||||||
$from = parse_at_time($_GET['from']) ?: Config::get('time.day');
|
$from = parse_at_time($_GET['from']) ?: Config::get('time.day');
|
||||||
$to = parse_at_time($_GET['to']) ?: Config::get('time.now');
|
$to = parse_at_time($_GET['to']) ?: Config::get('time.now');
|
||||||
|
$graph_type = (isset($vars['graph_type']) ? $vars['graph_type'] : Config::get('webui.graph_type'));
|
||||||
|
|
||||||
$period = ($to - $from);
|
$period = ($to - $from);
|
||||||
|
$base64_output = '';
|
||||||
$prev_from = ($from - $period);
|
$prev_from = ($from - $period);
|
||||||
|
|
||||||
$graph_image_type = $vars['graph_type'] ?? Config::get('webui.graph_type');
|
$graphfile = Config::get('temp_dir') . '/' . strgen();
|
||||||
$rrd_options = '';
|
|
||||||
$auth = false;
|
|
||||||
|
|
||||||
require Config::get('install_dir') . "/includes/html/graphs/$type/auth.inc.php";
|
require Config::get('install_dir') . "/includes/html/graphs/$type/auth.inc.php";
|
||||||
|
|
||||||
@@ -48,75 +47,61 @@ if ($auth && is_customoid_graph($type, $subtype)) {
|
|||||||
// Graph Template Missing");
|
// Graph Template Missing");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! empty($error_msg)) {
|
if ($error_msg) {
|
||||||
// We have an error :(
|
// We have an error :(
|
||||||
graph_error($error_msg);
|
graph_error($graph_error);
|
||||||
|
} elseif ($auth === null) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($auth === null) {
|
|
||||||
// We are unauthenticated :(
|
// We are unauthenticated :(
|
||||||
graph_error($width < 200 ? 'No Auth' : 'No Authorization');
|
graph_error($width < 200 ? 'No Auth' : 'No Authorization');
|
||||||
|
} else {
|
||||||
return;
|
// $rrd_options .= " HRULE:0#999999";
|
||||||
}
|
if ($graph_type === 'svg') {
|
||||||
|
$rrd_options .= ' --imgformat=SVG';
|
||||||
if ($graph_image_type === 'svg') {
|
if ($width < 350) {
|
||||||
$rrd_options .= ' --imgformat=SVG';
|
$rrd_options .= ' -m 0.75 -R light';
|
||||||
if ($width < 350) {
|
}
|
||||||
$rrd_options .= ' -m 0.75 -R light';
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// command output requested
|
if ($command_only) {
|
||||||
if (! empty($command_only)) {
|
echo "<div class='infobox'>";
|
||||||
echo "<div class='infobox'>";
|
echo "<p style='font-size: 16px; font-weight: bold;'>RRDTool Command</p>";
|
||||||
echo "<p style='font-size: 16px; font-weight: bold;'>RRDTool Command</p>";
|
echo "<pre class='rrd-pre'>";
|
||||||
echo "<pre class='rrd-pre'>";
|
echo 'rrdtool ' . Rrd::buildCommand('graph', $graphfile, $rrd_options);
|
||||||
echo 'rrdtool ' . Rrd::buildCommand('graph', '-', $rrd_options);
|
echo '</pre>';
|
||||||
echo '</pre>';
|
$return = Rrd::graph($graphfile, $rrd_options);
|
||||||
try {
|
|
||||||
Rrd::graph($rrd_options);
|
|
||||||
} catch (\LibreNMS\Exceptions\RrdGraphException $e) {
|
|
||||||
echo "<p style='font-size: 16px; font-weight: bold;'>RRDTool Output</p>";
|
echo "<p style='font-size: 16px; font-weight: bold;'>RRDTool Output</p>";
|
||||||
echo "<pre class='rrd-pre'>";
|
echo "<pre class='rrd-pre'>";
|
||||||
echo $e->getMessage();
|
echo "$return";
|
||||||
echo '</pre>';
|
echo '</pre>';
|
||||||
}
|
unlink($graphfile);
|
||||||
echo '</div>';
|
echo '</div>';
|
||||||
|
} elseif ($no_file) {
|
||||||
return;
|
graph_error($width < 200 ? 'No Data' : 'No Data file');
|
||||||
}
|
} elseif ($rrd_options) {
|
||||||
|
Rrd::graph($graphfile, $rrd_options);
|
||||||
// graph sent file not found flag
|
d_echo($rrd_cmd);
|
||||||
if (! empty($no_file)) {
|
if (is_file($graphfile)) {
|
||||||
graph_error($width < 200 ? 'No Data' : 'No Data file ' . $no_file);
|
if (! $debug) {
|
||||||
|
set_image_type();
|
||||||
return;
|
if ($output === 'base64') {
|
||||||
}
|
$imagedata = file_get_contents($graphfile);
|
||||||
|
$base64_output = base64_encode($imagedata);
|
||||||
if (empty($rrd_options)) {
|
} else {
|
||||||
graph_error($width < 200 ? 'Def Error' : 'Graph Definition Error');
|
$fd = fopen($graphfile, 'r');
|
||||||
|
fpassthru($fd);
|
||||||
return;
|
fclose($fd);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
// Generating the graph!
|
echo `ls -l $graphfile`;
|
||||||
try {
|
echo '<img src="' . data_uri($graphfile, 'image/svg+xml') . '" alt="graph" />';
|
||||||
$image_data = Rrd::graph($rrd_options);
|
}
|
||||||
|
unlink($graphfile);
|
||||||
// output the graph
|
} elseif (isset($rrd_filename) && ! Rrd::checkRrdExists($rrd_filename)) {
|
||||||
if ($debug) {
|
graph_error($width < 200 ? 'No Data' : 'No Data file');
|
||||||
echo '<img src="data:' . get_image_type($graph_image_type) . ';base64,' . base64_encode($image_data) . '" alt="graph" />';
|
} else {
|
||||||
|
graph_error($width < 200 ? 'Draw Error' : 'Error Drawing Graph');
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
header('Content-type: ' . get_image_type(Config::get('webui.graph_type')));
|
graph_error($width < 200 ? 'Def Error' : 'Graph Definition Error');
|
||||||
echo $output === 'base64' ? base64_encode($image_data) : $image_data;
|
|
||||||
}
|
|
||||||
} catch (\LibreNMS\Exceptions\RrdGraphException $e) {
|
|
||||||
if (isset($rrd_filename) && ! Rrd::checkRrdExists($rrd_filename)) {
|
|
||||||
graph_error($width < 200 ? 'No Data' : 'No Data file ' . basename($rrd_filename));
|
|
||||||
} else {
|
|
||||||
graph_error($width < 200 ? 'Draw Error' : 'Error Drawing Graph: ' . $e->getMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,8 +92,8 @@ if (! $auth) {
|
|||||||
$link_array['page'] = 'graphs';
|
$link_array['page'] = 'graphs';
|
||||||
$link = \LibreNMS\Util\Url::generate($link_array);
|
$link = \LibreNMS\Util\Url::generate($link_array);
|
||||||
|
|
||||||
echo '<td style="text-align: center;">';
|
echo '<td align=center>';
|
||||||
echo '<b>' . $text . '</b>';
|
echo '<b>' . $text . '</b><br>';
|
||||||
echo '<a href="' . $link . '">';
|
echo '<a href="' . $link . '">';
|
||||||
echo \LibreNMS\Util\Url::lazyGraphTag($graph_array);
|
echo \LibreNMS\Util\Url::lazyGraphTag($graph_array);
|
||||||
echo '</a>';
|
echo '</a>';
|
||||||
|
|||||||
Reference in New Issue
Block a user