mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
improved bandwidth and billing graphs
git-svn-id: http://www.observium.org/svn/observer/trunk@2746 61d68cd4-352d-0410-923a-c4978735b2b8
This commit is contained in:
@@ -59,32 +59,8 @@
|
||||
$overuse_data = array();
|
||||
$ticklabels = array();
|
||||
|
||||
function humanReadable($value, $title = false, $max = false, $decimals = 3) {
|
||||
$suffix = array("Bytes", "Kilobytes", "Megabytes", "Gigabytes", "Terabytes", "Petabytes");
|
||||
$max = (($max == false) ? count($suffix) : $max);
|
||||
$i = 0;
|
||||
while($value >= 1024 && ($i < $max)) {
|
||||
$value /= 1024;
|
||||
$i++;
|
||||
}
|
||||
if ($title == "suffix") {
|
||||
$tmp = $i;
|
||||
$res = $suffix[$tmp];
|
||||
} elseif ($title == "count") {
|
||||
$res = $i;
|
||||
} else {
|
||||
$res = round($value, $decimals);
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
function formatScale($value, $max, $decimal) {
|
||||
$res = array();
|
||||
foreach($value as $item) {
|
||||
$tmp = humanReadable($item, false, $max, $decimal);
|
||||
array_push($res, $tmp);
|
||||
}
|
||||
return $res;
|
||||
function format_si_short($val) {
|
||||
return format_si($val, "1");
|
||||
}
|
||||
|
||||
if ($imgtype == "historical") {
|
||||
@@ -93,12 +69,13 @@
|
||||
$datefrom = strftime("%e %b %Y", strtotime($data['bill_datefrom']));
|
||||
$dateto = strftime("%e %b %Y", strtotime($data['bill_dateto']));
|
||||
$datelabel = $datefrom."\n".$dateto;
|
||||
$traf['in'] = round(substr(formatStorage($data['traf_in'] * 1024 * 1024), 0, -2));
|
||||
$traf['out'] = round(substr(formatStorage($data['traf_out'] * 1024 * 1024), 0, -2));
|
||||
$traf['total'] = round(substr(formatStorage($data['traf_total'] * 1024 * 1024), 0, -2));
|
||||
$traf['in'] = $data['traf_in'];
|
||||
$traf['out'] = $data['traf_out'];
|
||||
$traf['total'] = $data['traf_total'];
|
||||
|
||||
if ($data['bill_type'] == "Quota") {
|
||||
$traf['allowed']= round(substr(formatStorage($data['bill_allowed'] * 1024 * 1024), 0, -2));
|
||||
$traf['overuse']= round(substr(formatStorage($data['bill_overuse'] * 1024 * 1024), 0, -2));
|
||||
$traf['allowed']= $data['bill_allowed'];
|
||||
$traf['overuse']= $data['bill_overuse'];
|
||||
} else {
|
||||
$traf['allowed']= "0";
|
||||
$traf['overuse']= "0";
|
||||
@@ -127,8 +104,6 @@
|
||||
$graph_name = "Historical bandwidth over the last 12 billing periods";
|
||||
} else {
|
||||
$data = array();
|
||||
$max = 9999999999999999999999999999999999999999999;
|
||||
$min_data = array("in"=>$max, "out"=>$max, "tot"=>$max);
|
||||
$average = 0;
|
||||
if ($imgtype == "day") {
|
||||
foreach(dbFetch("SELECT DISTINCT UNIX_TIMESTAMP(timestamp) as timestamp, SUM(delta) as traf_total, SUM(in_delta) as traf_in, SUM(out_delta) as traf_out FROM bill_data WHERE `bill_id` = ? AND `timestamp` >= FROM_UNIXTIME(?) AND `timestamp` <= FROM_UNIXTIME(?) GROUP BY DATE(timestamp) ORDER BY timestamp ASC", array($bill_id, $start, $end)) as $data) {
|
||||
@@ -140,14 +115,8 @@
|
||||
array_push($in_data, $traf['in']);
|
||||
array_push($out_data, $traf['out']);
|
||||
array_push($tot_data, $traf['total']);
|
||||
// $min_data['in'] = (($traf['in'] < $min_data['in']) ? $traf['in'] : $min_data['in']);
|
||||
// $min_data['out']= (($traf['out'] < $min_data['out']) ? $traf['out'] : $min_data['out']);
|
||||
// $min_data['tot']= (($traf['total'] < $min_data['tot']) ? $traf['total'] : $min_data['tot']);
|
||||
$average += $data['traf_total'];
|
||||
}
|
||||
$min_data['in'] = min($in_data);
|
||||
$min_data['out'] = min($out_data);
|
||||
$min_data['tot'] = min($tot_data);
|
||||
$ave_count = count($tot_data);
|
||||
if ($imgbill != false) {
|
||||
$days = strftime("%e", date($end - $start)) - $ave_count - 1;
|
||||
@@ -168,24 +137,15 @@
|
||||
array_push($in_data, $traf['in']);
|
||||
array_push($out_data, $traf['out']);
|
||||
array_push($tot_data, $traf['total']);
|
||||
// $min_data['in'] = (($traf['in'] < $min_data['in']) ? $traf['in'] : $min_data['in']);
|
||||
// $min_data['out']= (($traf['out'] < $min_data['out']) ? $traf['out'] : $min_data['out']);
|
||||
// $min_data['tot']= (($traf['tot'] < $min_data['tot']) ? $traf['tot'] : $min_data['tot']);
|
||||
$average += $data['traf_total'];
|
||||
}
|
||||
$min_data['in'] = min($in_data);
|
||||
$min_data['out'] = min($out_data);
|
||||
$min_data['tot'] = min($tot_data);
|
||||
$ave_count = count($tot_data);
|
||||
}
|
||||
$decimal = 0;
|
||||
$max = min($min_data);
|
||||
$yaxistitle = humanReadable($max, "suffix");
|
||||
$max = humanReadable($max, "count");
|
||||
$in_data = formatScale($in_data, $max, $decimal);
|
||||
$out_data = formatScale($out_data, $max, $decimal);
|
||||
$tot_data = formatScale($tot_data, $max, $decimal);
|
||||
$average = humanReadable(($average / $ave_count), false, $max, $decimal);
|
||||
# $in_data = formatScale($in_data, $max, $decimal);
|
||||
# $out_data = formatScale($out_data, $max, $decimal);
|
||||
# $tot_data = formatScale($tot_data, $max, $decimal);
|
||||
$average = $average / $ave_count;
|
||||
for ($x=0;$x<=count($tot_data);$x++) {
|
||||
array_push($ave_data, $average);
|
||||
}
|
||||
@@ -216,7 +176,8 @@
|
||||
$graph->yaxis->SetFont(FF_FONT1);
|
||||
$graph->yaxis->SetTitleMargin(50);
|
||||
$graph->yaxis->title->SetFont(FF_FONT1, FS_NORMAL, 10);
|
||||
$graph->yaxis->title->Set($yaxistitle);
|
||||
$graph->yaxis->title->Set("Bytes Transferred");
|
||||
$graph->yaxis->SetLabelFormatCallback("format_si");
|
||||
$graph->ygrid->SetFill(true,'#EFEFEF@0.5','#FFFFFF@0.5');
|
||||
|
||||
|
||||
@@ -225,6 +186,8 @@
|
||||
$barplot_tot->SetLegend("Traffic total");
|
||||
$barplot_tot->SetColor("#d5d5d5");
|
||||
$barplot_tot->SetFillColor("#d5d5d5@0.5");
|
||||
$barplot_tot->value->Show();
|
||||
$barplot_tot->value->SetFormatCallback('format_si_short');
|
||||
|
||||
$barplot_in = new BarPlot($in_data);
|
||||
$barplot_in->SetLegend("Traffic In");
|
||||
@@ -238,6 +201,7 @@
|
||||
$barplot_out->SetFillColor('lightblue@0.4');
|
||||
$barplot_out->SetWeight(1);
|
||||
|
||||
|
||||
if ($imgtype == "historical") {
|
||||
$barplot_over = new BarPlot($overuse_data);
|
||||
$barplot_over->SetLegend("Traffic Overusage");
|
||||
@@ -250,14 +214,14 @@
|
||||
$lineplot_allow->SetColor('black');
|
||||
$lineplot_allow->SetWeight(1);
|
||||
|
||||
$gbplot = new GroupBarPlot(array($barplot_in, $barplot_out, $barplot_tot, $barplot_over));
|
||||
$gbplot = new GroupBarPlot(array($barplot_in, $barplot_tot, $barplot_out, $barplot_over));
|
||||
} else {
|
||||
$lineplot_allow = new LinePlot($ave_data);
|
||||
$lineplot_allow->SetLegend("Average per day");
|
||||
$lineplot_allow->SetColor('black');
|
||||
$lineplot_allow->SetWeight(1);
|
||||
|
||||
$gbplot = new GroupBarPlot(array($barplot_in, $barplot_out, $barplot_tot));
|
||||
$gbplot = new GroupBarPlot(array($barplot_in, $barplot_tot, $barplot_out));
|
||||
}
|
||||
|
||||
$graph->Add($gbplot);
|
||||
|
@@ -77,8 +77,8 @@ if ($count <= 1) { $count = 2; }
|
||||
#$count = round($counttot / 260, 0);
|
||||
#if ($count <= 1) { $count = 2; }
|
||||
|
||||
$max = dbFetchCell("SELECT delta FROM bill_data WHERE bill_id = ? AND `timestamp` >= FROM_UNIXTIME( ? ) AND `timestamp` <= FROM_UNIXTIME( ? ) ORDER BY delta DESC LIMIT 0,1", array($bill_id, $start, $end));
|
||||
if ($max > 1000000) { $div = "1000000"; $yaxis = "Mbit/sec"; } else { $div = "1000"; $yaxis = "Kbit/sec"; }
|
||||
#$max = dbFetchCell("SELECT delta FROM bill_data WHERE bill_id = ? AND `timestamp` >= FROM_UNIXTIME( ? ) AND `timestamp` <= FROM_UNIXTIME( ? ) ORDER BY delta DESC LIMIT 0,1", array($bill_id, $start, $end));
|
||||
#if ($max > 1000000) { $div = "1000000"; $yaxis = "Mbit/sec"; } else { $div = "1000"; $yaxis = "Kbit/sec"; }
|
||||
|
||||
$i = '0';
|
||||
|
||||
@@ -90,8 +90,8 @@ foreach (dbFetch("SELECT *, UNIX_TIMESTAMP(timestamp) AS formatted_date FROM bil
|
||||
@$period = $row['period'];
|
||||
@$in_delta = $row['in_delta'];
|
||||
@$out_delta = $row['out_delta'];
|
||||
@$in_value = round($in_delta * 8 / $period / $div, 2);
|
||||
@$out_value = round($out_delta * 8 / $period / $div, 2);
|
||||
@$in_value = round($in_delta * 8 / $period, 2);
|
||||
@$out_value = round($out_delta * 8 / $period, 2);
|
||||
|
||||
@$last = $timestamp;
|
||||
|
||||
@@ -101,17 +101,17 @@ foreach (dbFetch("SELECT *, UNIX_TIMESTAMP(timestamp) AS formatted_date FROM bil
|
||||
|
||||
if ($iter == $count)
|
||||
{
|
||||
$out_data[$i] = round($iter_out * 8 / $iter_period / $div, 2);
|
||||
$out_data[$i] = round($iter_out * 8 / $iter_period, 2);
|
||||
$out_data_inv[$i] = $out_data[$i] * -1;
|
||||
$in_data[$i] = round($iter_in * 8 / $iter_period / $div, 2);
|
||||
$in_data[$i] = round($iter_in * 8 / $iter_period, 2);
|
||||
$tot_data[$i] = $out_data[$i] + $in_data[$i];
|
||||
$tot_data_inv[$i] = $tot_data[$i] * -1;
|
||||
|
||||
if ($tot_data[$i] > $max_value) { $max_value = $tot_data[$i]; }
|
||||
|
||||
$ticks[$i] = $timestamp;
|
||||
$per_data[$i] = $rate_95th / $div;
|
||||
$ave_data[$i] = $rate_average / $div;
|
||||
$per_data[$i] = $rate_95th;
|
||||
$ave_data[$i] = $rate_average;
|
||||
$iter = "1";
|
||||
$i++;
|
||||
unset($iter_out, $iter_in, $iter_period);
|
||||
@@ -152,7 +152,10 @@ $graph->xaxis->SetTitleMargin(30);
|
||||
#$graph->xaxis->HideFirstTickLabel();
|
||||
#$graph->yaxis->scale->SetAutoMin(1);
|
||||
$graph->xaxis->title->Set($type);
|
||||
$graph->yaxis->title->Set($yaxis);
|
||||
$graph->yaxis->title->Set("Bits per second");
|
||||
$graph->yaxis->SetLabelFormatCallback("format_si");
|
||||
|
||||
|
||||
|
||||
function TimeCallback($aVal) {
|
||||
global $dur;
|
||||
|
@@ -434,24 +434,32 @@ function formatStorage($rate, $round = '2')
|
||||
return $rate;
|
||||
}
|
||||
|
||||
function format_si($rate)
|
||||
function format_si($rate, $round = 2)
|
||||
{
|
||||
if($rate < "0")
|
||||
{
|
||||
$neg = 1;
|
||||
$rate = $rate * -1;
|
||||
}
|
||||
|
||||
if ($rate >= "0.1")
|
||||
{
|
||||
$sizes = Array('', 'k', 'M', 'G', 'T', 'P', 'E');
|
||||
$round = Array('2','2','2','2','2','2','2','2','2');
|
||||
# $round = Array('2','2','2','2','2','2','2','2','2');
|
||||
$ext = $sizes[0];
|
||||
for ($i = 1; (($i < count($sizes)) && ($rate >= 1000)); $i++) { $rate = $rate / 1000; $ext = $sizes[$i]; }
|
||||
}
|
||||
else
|
||||
{
|
||||
$sizes = Array('', 'm', 'u', 'n');
|
||||
$round = Array('2','2','2','2');
|
||||
# $round = Array('2','2','2','2');
|
||||
$ext = $sizes[0];
|
||||
for ($i = 1; (($i < count($sizes)) && ($rate != 0) && ($rate <= 0.1)); $i++) { $rate = $rate * 1000; $ext = $sizes[$i]; }
|
||||
}
|
||||
|
||||
return round($rate, $round[$i]).$ext;
|
||||
if($neg) { $rate = $rate * -1; }
|
||||
|
||||
return round($rate, $round).$ext;
|
||||
}
|
||||
|
||||
function format_bi($size, $round = '2')
|
||||
|
Reference in New Issue
Block a user