Skip to content

Commit

Permalink
feature: Added ability to output graphs as svg (#5959)
Browse files Browse the repository at this point in the history
* feature: Added ability to output graphs as svg

* Update graph.inc.php

* added ability to gzip various files for web servers
  • Loading branch information
laf authored and murrant committed Feb 22, 2017
1 parent 1c9cbc4 commit a869168
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 12 deletions.
2 changes: 2 additions & 0 deletions doc/Installation/Installation-CentOS-7-Nginx.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ server {
index index.php;
access_log /opt/librenms/logs/access_log;
error_log /opt/librenms/logs/error_log;
gzip on;
gzip_types text/css application/x-javascript text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
location / {
try_files $uri $uri/ @librenms;
}
Expand Down
2 changes: 2 additions & 0 deletions doc/Installation/Installation-Ubuntu-1404-Nginx.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ server {
index index.php;
access_log /opt/librenms/logs/access_log;
error_log /opt/librenms/logs/error_log;
gzip on;
gzip_types text/css application/x-javascript text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
location / {
try_files $uri $uri/ @librenms;
}
Expand Down
2 changes: 2 additions & 0 deletions doc/Installation/Installation-Ubuntu-1604-Nginx.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ server {
index index.php;
access_log /opt/librenms/logs/access_log;
error_log /opt/librenms/logs/error_log;
gzip on;
gzip_types text/css application/x-javascript text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
location / {
try_files $uri $uri/ @librenms;
}
Expand Down
3 changes: 3 additions & 0 deletions html/.htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

Options FollowSymlinks Multiviews

AddType image/svg+xml .svg
AddOutputFilterByType DEFLATE image/svg+xml

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
Expand Down
6 changes: 3 additions & 3 deletions html/includes/api_functions.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function get_graph_by_port_hostname()
$vars['height'] = $_GET['height'] ?: 300;
$auth = '1';
$vars['id'] = dbFetchCell("SELECT `P`.`port_id` FROM `ports` AS `P` JOIN `devices` AS `D` ON `P`.`device_id` = `D`.`device_id` WHERE `D`.`hostname`=? AND `P`.`$port`=? AND `deleted` = 0 LIMIT 1", array($hostname, $vars['port']));
$app->response->headers->set('Content-Type', 'image/png');
$app->response->headers->set(set_image_type());
rrdtool_initialize(false);
include 'includes/graphs/graph.inc.php';
rrdtool_close();
Expand Down Expand Up @@ -149,7 +149,7 @@ function get_graph_generic_by_hostname()
$vars['height'] = $_GET['height'] ?: 300;
$auth = '1';
$vars['device'] = dbFetchCell('SELECT `D`.`device_id` FROM `devices` AS `D` WHERE `D`.`hostname`=?', array($hostname));
$app->response->headers->set('Content-Type', 'image/png');
$app->response->headers->set(set_image_type());
rrdtool_initialize(false);
include 'includes/graphs/graph.inc.php';
rrdtool_close();
Expand Down Expand Up @@ -514,7 +514,7 @@ function get_graph_by_portgroup()
unset($seperator);
$vars['type'] = 'multiport_bits_separate';
$vars['id'] = $if_list;
$app->response->headers->set('Content-Type', 'image/png');
$app->response->headers->set(set_image_type());
rrdtool_initialize(false);
include 'includes/graphs/graph.inc.php';
rrdtool_close();
Expand Down
14 changes: 14 additions & 0 deletions html/includes/functions.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -1419,3 +1419,17 @@ function eventlog_severity($eventlog_severity)
break;
}
} // end eventlog_severity

/**
*
*/
function set_image_type()
{
global $config;

if ($config['webui']['graph_type'] === 'svg') {
return header('Content-type: image/svg+xml');
} else {
return header('Content-type: image/png');
}
}
4 changes: 2 additions & 2 deletions html/includes/graphs/common.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
}

if ($_GET['width']) {
$width = mres($_GET['width']);
$width = mres($vars['width']);
}

if ($config['trim_tobias']) {
$width += 12;
}

if ($_GET['height']) {
$height = mres($_GET['height']);
$height = mres($vars['height']);
}

if ($_GET['inverse']) {
Expand Down
12 changes: 8 additions & 4 deletions html/includes/graphs/graph.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

$prev_from = ($from - $period);

$graphfile = $config['temp_dir'].'/'.strgen().'.png';
$graphfile = $config['temp_dir'].'/'.strgen();

$type = $graphtype['type'];
$subtype = $graphtype['subtype'];
Expand Down Expand Up @@ -102,6 +102,10 @@ function graph_error($string)
}
} else {
// $rrd_options .= " HRULE:0#999999";
if ($config['webui']['graph_type'] === 'svg') {
$rrd_options .= " --imgformat=SVG -m 0.75";
}

if ($no_file) {
if ($width < 200) {
graph_error('No RRD');
Expand All @@ -125,8 +129,8 @@ function graph_error($string)

if (is_file($graphfile)) {
if (!$debug) {
header('Content-type: image/png');
if ($config['trim_tobias']) {
set_image_type();
if ($config['trim_tobias'] && $config['webui']['graph_type'] !== 'svg') {
list($w, $h, $type, $attr) = getimagesize($graphfile);
$src_im = imagecreatefrompng($graphfile);
$src_x = '0';
Expand Down Expand Up @@ -156,7 +160,7 @@ function graph_error($string)
}
} else {
echo `ls -l $graphfile`;
echo '<img src="'.data_uri($graphfile, 'image/png').'" alt="graph" />';
echo '<img src="'.data_uri($graphfile, 'image/svg+xml').'" alt="graph" />';
}
unlink($graphfile);
} else {
Expand Down
14 changes: 11 additions & 3 deletions html/pages/settings/webui.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@
);

$graph_conf = array(
array('name' => 'webui.min_graph_height',
'descr' => 'Set the minimum graph height',
'type' => 'text',
array('name' => 'webui.min_graph_height',
'descr' => 'Set the minimum graph height',
'type' => 'text',
),
array('name' => 'webui.graph_type',
'descr' => 'Set the graph type',
'type' => 'select',
'options' => array(
'png' => 'png',
'svg' => 'svg',
),
),
);

Expand Down
1 change: 1 addition & 0 deletions sql-schema/166.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO config (config_name,config_value,config_default,config_descr,config_group,config_group_order,config_sub_group,config_sub_group_order,config_hidden,config_disabled) values ('webui.graph_type','png','png','Set the default graph type','webui',0,'graph',0,'0','0');

0 comments on commit a869168

Please sign in to comment.