Created by 1024) { $size = round(($size*100)/1024)/100; $times++; } return "$size " . $names[$times]; } //function humanreadable($size) /* Convert a month number to a month name */ function monthname($no) { $names = array(1 => 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'); return $names[$no]; } //function monthname($no) /* Convert year and month number to a string useful for rrdtool */ function monthstartend($year, $month) { $start = mktime(0, 0, 0, $month, 1, $year); if($month==12) $end = mktime(0, 0, 0, 1, 1, $year+1); else $end = mktime(0, 0, 0, $month+1, 1, $year); return " -s $start -e $end "; } /* Output HTML for a year */ function showyear($year, $months) { $sumyear = array(); $sumquater = array(); printf("

Year: %s

\n", $year); printf("\n"); for($quater=1; $quater<=4; $quater++) { /* month */ for($i=($quater-1)*3+1; $i<=($quater-1)*3+3; $i++) { printf("\n", $_SERVER['SCRIPT_NAME'], $_GET['name'], $year, $i, monthname($i), $i); } /* quater */ printf("\n", $quater); } /* year */ printf("\n"); printf("\n"); for($quater=1; $quater<=4; $quater++) { /* month */ for($i=($quater-1)*3+1; $i<=($quater-1)*3+3; $i++) { printf("\n", humanreadable($months[$i]['in'])); $sumyear['in'] += $months[$i]['in']; $sumquater[$quater]['in'] += $months[$i]['in']; } /* quater */ printf("\n", humanreadable($sumquater[$quater]['in'])); } /* year */ printf("\n", humanreadable($sumyear['in'])); printf("\n"); for($quater=1; $quater<=4; $quater++) { /* month */ for($i=($quater-1)*3+1; $i<=($quater-1)*3+3; $i++) { printf("\n", humanreadable($months[$i]['out'])); $sumyear['out'] += $months[$i]['out']; $sumquater[$quater]['out'] += $months[$i]['out']; } /* quater */ printf("\n", humanreadable($sumquater[$quater]['out'])); } /* year */ printf("\n", humanreadable($sumyear['out'])); printf("\n"); for($quater=1; $quater<=4; $quater++) { /* month */ for($i=($quater-1)*3+1; $i<=($quater-1)*3+3; $i++) { printf("\n", humanreadable(max($months[$i]['in'], $months[$i]['out']))); } /* quater */ printf("\n", humanreadable(max($sumquater[$quater]['in'], $sumquater[$quater]['out']))); } /* year */ printf("\n", humanreadable(max($sumyear['in'], $sumyear['out']))); printf("\n"); for($quater=1; $quater<=4; $quater++) { /* month */ for($i=($quater-1)*3+1; $i<=($quater-1)*3+3; $i++) { printf("\n", humanreadable($months[$i]['in'] + $months[$i]['out'])); } /* quater */ printf("\n", humanreadable($sumquater[$quater]['in'] + $sumquater[$quater]['out'])); } /* year */ printf("\n", humanreadable($sumyear['in'] + $sumyear['out'])); printf("
%s (%s)Quater %sYear
In%s%s%s
Out%s%s%s
Max%s%s%s
Sum%s%s%s
\n"); } //function showyear($year, $months) /* Output HTML for a month */ function showmonth($year, $month, $days) { $summonth = array(); $daysinmonth = date("t", mktime(0, 0, 0, $month, 1, $year)); printf("

Month: %s %s

\n", $year, monthname($month)); for($j=1; $j<=2; $j++) { if($j==1) { $start = 1; $end = 16; } else { $start = 17; $end = $daysinmonth; } printf("\n"); for($i=$start; $i<=$end; $i++) { printf("\n", $i); } if($j==2) { printf("\n"); } printf("\n"); for($i=$start; $i<=$end; $i++) { printf("\n", humanreadable($days[$i]['in'])); $summonth['in'] += $days[$i]['in']; } if($j==2) { printf("\n", humanreadable($summonth['in'])); } printf("\n"); for($i=$start; $i<=$end; $i++) { printf("\n", humanreadable($days[$i]['out'])); $summonth['out'] += $days[$i]['out']; } if($j==2) { printf("\n", humanreadable($summonth['out'])); } printf("\n"); for($i=$start; $i<=$end; $i++) { printf("\n", humanreadable(max($days[$i]['in'], $days[$i]['out']))); } if($j==2) { printf("\n", humanreadable(max($summonth['in'], $summonth['out']))); } printf("\n"); for($i=$start; $i<=$end; $i++) { printf("\n", humanreadable($days[$i]['in'] + $days[$i]['out'])); } if($j==2) { printf("\n", humanreadable($summonth['in'] + $summonth['out'])); } printf("
%sMonth
In%s%s
Out%s%s
Max%s%s
Sum%s%s
\n"); } //for($j=1; $j<=2; $j++) } //showmonth($year, $month, $days) /***************************************************************************\ * All the rest * \***************************************************************************/ /* Find legalnames */ $legalnames = array(); if($dirhandler = @opendir($dir)) { while(($filename = readdir($dirhandler)) !== false) { if(ereg("$extension$", $filename)) { $filename = substr($filename, 0, -strlen($extension)); if(!in_array($filename, $exclude)) { $legalnames[] = $filename; } } } closedir($dirhandler); } /* If a device have been chosen */ if(isset($_GET['name'])) { /* If the device name is valid */ if(validname($_GET['name'])) { /* If the script should generate a picture */ if(isset($_GET['picture'])) { $name = filename($_GET['name']); header("content-type: image/png"); $rrdcommand = "$rrdcommand graph - -v 'Bytes/s' -b 1024 -w 390 DEF:avgin=$name:ds0:AVERAGE AREA:avgin#00CC00:'Traffic in' DEF:avgout=$name:ds1:AVERAGE LINE2:avgout#0000FF:'Traffic out'"; /* Last day */ if($_GET['period']=='day') { $rrdcommand .= ' -t "Traffic the last day" -s -86400'; } /* Last week */ else if($_GET['period']=='week') { $rrdcommand .= ' -t "Traffic the last week" -s -604800'; } /* Last month */ else if($_GET['period']=='month') { $rrdcommand .= ' -t "Traffic the last month" -s -2678400'; } /* Last year */ else if($_GET['period']=='year') { $rrdcommand .= ' -t "Traffic the last year" -s -31622400'; } /* If year and month is supplied, then generate picture for that month */ else if(is_numeric($_GET['year']) && is_numeric($_GET['month'])) { $name = monthname($_GET['month']) . ' ' . $_GET['year']; $rrdcommand .= " -t 'Traffic for $name' " . monthstartend($_GET['year'], $_GET['month']); $rrdcommand .= " -x DAY:1:WEEK:1:DAY:1:86400:%d "; } echo `$rrdcommand`; } //if(isset($_GET['picture'])) /* If year and month is supplied, then generate page for that month */ else if(is_numeric($_GET['year']) && is_numeric($_GET['month'])) { echo top($_GET['name']); $name = monthname($_GET['month']) . ' ' . $_GET['year']; printf("\"%s\"", $_SERVER['SCRIPT_NAME'], $_GET['name'], $_GET['year'], $_GET['month'], $name); $lastdate = 0; $days = array(); /* Get statistics for the selected month */ if($fp = popen("$rrdcommand fetch " . filename($_GET['name']) . " AVERAGE -r 864000 ".monthstartend($_GET['year'], $_GET['month']), 'r')) { fgets($fp, 4096); while(!feof($fp)) { $line = trim(fgets($fp, 4096)); if($line != '') { list($date, $in, $out) = split(' ', $line); list($date) = split(':', $date); if($lastdate != 0) { if(!is_numeric($in)) $in = 0; if(!is_numeric($out)) $out = 0; $in = $in*($date-$lastdate); $out = $out*($date-$lastdate); if($_GET['month'] == date('n', $lastdate) && $_GET['year'] == date('Y', $lastdate)) { $day = date('j', $lastdate); $days[$day]['in'] += $in; $days[$day]['out'] += $out; } } //if($lastdate != 0) $lastdate = $date; } //if($line != '') } //while(!feof($fp)) showmonth($_GET['year'], $_GET['month'], $days); pclose($fp); } //if($fp = popen($test, 'r')) echo bottom(); } /* Else generate main device page */ else { echo top($_GET['name']); /* Find out when the database was last updated */ if($fp = popen("$rrdcommand info " . filename($_GET['name']), 'r')) { $key = ''; while(!feof($fp)) { list($key, $value) = split(' = ', trim(fgets($fp, 4096))); if($key == 'last_update') { printf("Last updated: %s
\n", date("Y-m-d H:i:s", $value)); break; } } pclose($fp); } printf("\"Dayly\"", $_SERVER['SCRIPT_NAME'], $_GET['name']); printf("\"Weekly\"", $_SERVER['SCRIPT_NAME'], $_GET['name']); printf("\"Monthly\"", $_SERVER['SCRIPT_NAME'], $_GET['name']); printf("\"Yearly\"\n", $_SERVER['SCRIPT_NAME'], $_GET['name']); $lastdate = 0; $months = array(); /* Get statistics for the last two year */ if($fp = popen("$rrdcommand fetch " . filename($_GET['name']) . " AVERAGE -s -63331200 -e +31622400", 'r')) { fgets($fp, 4096); while(!feof($fp)) { $line = trim(fgets($fp, 4096)); if($line != '') { list($date, $in, $out) = split(' ', $line); list($date) = split(':', $date); if($lastdate != 0) { if(!is_numeric($in)) $in = 0; if(!is_numeric($out)) $out = 0; $in = $in*($date-$lastdate); $out = $out*($date-$lastdate); $year = date('Y', $lastdate); $month = date('n', $lastdate); $months[$year][$month]['in'] += $in; $months[$year][$month]['out'] += $out; } //if($lastdate != 0) $lastdate = $date; } //if($line != '') } //while(!feof($fp)) $year = date('Y'); showyear($year, $months[$year]); $year = date('Y')-1; showyear($year, $months[$year]); pclose($fp); } //if($fp = popen($test, 'r')) echo bottom(); } //else } //if(validname($_GET['name'])) /* If device name has been provided, but it is not valid */ else { printf("Don't do that"); } //else } //if(isset($_GET['name'])) /* If device name has been given, show the main page */ else { echo top('All devices'); foreach($legalnames as $name) { printf("%s
\n", $_SERVER['SCRIPT_NAME'], $name, $name); } echo bottom(); } //else ?>