Skip to content

Commit

Permalink
Fix array/hash lookup errors when data is missing
Browse files Browse the repository at this point in the history
Say you mispell your iface name in the config, or try to target a device that
vnstat is not configured to monitor, you will get lots of the following:

PHP Warning:  Illegal string offset 'rx' in /home/joe/public_html/vnstat/index.php on line 104
PHP Warning:  Illegal string offset 'tx' in /home/joe/public_html/vnstat/index.php on line 105

This checks if vnstat gives an error. If it does, it leaves the $hour/$month/$day
variables empty rather than making them 'nodata'.

In places where data is expected (in the graphs and table generation) the
time variables are now checked against being empty rather than 'nodata' which further
fixes warnings like the following:

PHP Notice:  Undefined index: totalrx in /home/joe/public_html/vnstat-php-frontend/index.php on line 86
PHP Notice:  Undefined index: totalrxk in /home/joe/public_html/vnstat-php-frontend/index.php on line 86
PHP Notice:  Undefined index: totaltx in /home/joe/public_html/vnstat-php-frontend/index.php on line 87
PHP Notice:  Undefined index: totaltxk in /home/joe/public_html/vnstat-php-frontend/index.php on line 87
  • Loading branch information
jrgp committed Feb 19, 2015
1 parent caf5b2f commit 6b0b63f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 33 deletions.
6 changes: 3 additions & 3 deletions graph.php
Expand Up @@ -105,7 +105,7 @@ function draw_border()
function draw_grid($x_ticks, $y_ticks)
{
global $im, $cl, $iw, $ih, $xlm, $xrm, $ytm, $ybm;
$x_step = ($iw - $xlm - $xrm) / $x_ticks;
$x_step = ($iw - $xlm - $xrm) / ($x_ticks ?: 1);
$y_step = ($ih - $ytm - $ybm) / $y_ticks;

$depth = 10;//($x_step / 8) + 4;
Expand Down Expand Up @@ -139,7 +139,7 @@ function draw_data($data)
$unit = 'K';
$offset = 0;
$gr_h = $ih - $ytm - $ybm;
$x_step = ($iw - $xlm - $xrm) / $x_ticks;
$x_step = ($iw - $xlm - $xrm) / ($x_ticks ?: 1);
$y_step = ($ih - $ytm - $ybm) / $y_ticks;
$bar_w = ($x_step / 2) ;

Expand Down Expand Up @@ -184,7 +184,7 @@ function draw_data($data)
imagesetthickness($im, 1);
$sf = ($prescale * $y_scale * $y_ticks) / $gr_h;

if ($data[0] == 'nodata')
if (count($data) == 0)
{
$text = T('no data available');
$bbox = imagettfbbox(10, 0, GRAPH_FONT, $text);
Expand Down
6 changes: 3 additions & 3 deletions graph_svg.php
Expand Up @@ -165,7 +165,7 @@ function draw_border()
function draw_grid($x_ticks, $y_ticks)
{
global $cl, $iw, $ih, $xlm, $xrm, $ytm, $ybm;
$x_step = ($iw - $xlm - $xrm) / $x_ticks;
$x_step = ($iw - $xlm - $xrm) / ($x_ticks ?: 1);
$y_step = ($ih - $ytm - $ybm) / $y_ticks;

$depth = 12*SVG_DEPTH_SCALING;
Expand Down Expand Up @@ -203,7 +203,7 @@ function draw_data($data)
$unit = 'K';
$offset = 0;
$gr_h = $ih - $ytm - $ybm;
$x_step = ($iw - $xlm - $xrm) / $x_ticks;
$x_step = ($iw - $xlm - $xrm) / ($x_ticks ?: 1);
$y_step = ($ih - $ytm - $ybm) / $y_ticks;
$bar_w = ($x_step / 2) ;

Expand Down Expand Up @@ -247,7 +247,7 @@ function draw_data($data)
//
$sf = ($prescale * $y_scale * $y_ticks) / $gr_h;

if ($data[0] == 'nodata')
if (count($data) == 0)
{
$text = 'no data available';
svg_text($iw/2, $ytm + 80, $text, array( 'stroke' => $cl['text']['rgb'], 'fill' => $cl['text']['rgb'], 'stroke-width' => 0, 'font-family' => SVG_FONT, 'font-size' => '16pt', 'text-anchor' => 'middle') );
Expand Down
43 changes: 24 additions & 19 deletions index.php
Expand Up @@ -89,25 +89,30 @@ function write_summary()
//
// build array for write_data_table
//
$sum[0]['act'] = 1;
$sum[0]['label'] = T('This hour');
$sum[0]['rx'] = $hour[0]['rx'];
$sum[0]['tx'] = $hour[0]['tx'];

$sum[1]['act'] = 1;
$sum[1]['label'] = T('This day');
$sum[1]['rx'] = $day[0]['rx'];
$sum[1]['tx'] = $day[0]['tx'];

$sum[2]['act'] = 1;
$sum[2]['label'] = T('This month');
$sum[2]['rx'] = $month[0]['rx'];
$sum[2]['tx'] = $month[0]['tx'];

$sum[3]['act'] = 1;
$sum[3]['label'] = T('All time');
$sum[3]['rx'] = $trx;
$sum[3]['tx'] = $ttx;

$sum = array();

if (count($day) > 0 && count($hour) > 0 && count($month) > 0) {
$sum[0]['act'] = 1;
$sum[0]['label'] = T('This hour');
$sum[0]['rx'] = $hour[0]['rx'];
$sum[0]['tx'] = $hour[0]['tx'];

$sum[1]['act'] = 1;
$sum[1]['label'] = T('This day');
$sum[1]['rx'] = $day[0]['rx'];
$sum[1]['tx'] = $day[0]['tx'];

$sum[2]['act'] = 1;
$sum[2]['label'] = T('This month');
$sum[2]['rx'] = $month[0]['rx'];
$sum[2]['tx'] = $month[0]['tx'];

$sum[3]['act'] = 1;
$sum[3]['label'] = T('All time');
$sum[3]['rx'] = $trx;
$sum[3]['tx'] = $ttx;
}

write_data_table(T('Summary'), $sum);
print "<br/>\n";
Expand Down
13 changes: 5 additions & 8 deletions vnstat.php
Expand Up @@ -123,6 +123,10 @@ function get_vnstat_data($use_label=true)
$month = array();
$top = array();

if (strpos($vnstat_data[0], 'Error') !== false) {
return;
}

//
// extract data
//
Expand Down Expand Up @@ -199,16 +203,9 @@ function get_vnstat_data($use_label=true)
$summary[$d[0]] = isset($d[1]) ? $d[1] : '';
}
}
if (count($day) == 0)
$day[0] = 'nodata';
rsort($day);

if (count($month) == 0)
$month[0] = 'nodata';
rsort($day);
rsort($month);

if (count($hour) == 0)
$hour[0] = 'nodata';
rsort($hour);
}
?>

0 comments on commit 6b0b63f

Please sign in to comment.