Hello, I happen to come accross this nice php script/snippet used for displaying the amount of bandwidth your web server is using by reading your log files. Now the only issue im having with it is that it uses the log files to determin how much bandwidth iv been using but the log files are set to be deleted every 2 days to make room for the new ones. Now the php script reads from 2 log files, the current day log file and the day before it. Seeing as how my web server software uses log file names EXYYMMDD.LOG (YY=year; MM=month; DD=day)I dont know how I would have the script work without editing it everyday. I dont know php that good so I cant add in anything to update it on its own. So i will post the script here and I hope someone can help me with this issue/problem.
The log file names take the form EXYYMMDD.LOG (YY=year; MM=month; DD=day) and are stored in the same directory as the the web server software executable.
<?
# CONFIGURE THIS:
$todaylog = "/home/ivan/logs/access_log"; # today's logfile
$yesterdaylog = "/home/ivan/logs/access_log.1"; # yesterday's rotated log
# REMOVE OR COMMENT THIS LINE:
print "edit the configuration section inside this file."; exit;
error_reporting(1);
$fd = fopen("$todaylog","r");
while ($y = fgets($fd,1024)) { $linecount++; }
$y = NULL;
fclose($fd);
$fd = fopen("$todaylog","r");
while ($x = fgets($fd,1024)) {
if($count == 0) {
# get the first timestamp
list( , , , $date) = explode(" ", $x);
$date = str_replace("[", "", $date);
list($day, $month, $year) = explode("/", $date, 3);
list($year, $hour, $minute, $second) = explode(":", $year, 4);
$formdate = "$day $month $year $hour:$minute:$second";
$starttime = strtotime("$formdate");
}
if($count == ($linecount - 1)) {
# get the last timestamp
list( , , , $date) = explode(" ", $x);
$edate = str_replace("[", "", $date);
list($day, $month, $year) = explode("/", $edate, 3);
list($year, $hour, $minute, $second) = explode(":", $year, 4);
$eformdate = "$day $month $year $hour:$minute:$second";
$endtime = strtotime("$eformdate");
}
list( , , , , , , , , , $size) = explode(" ", $x, 10);
if($size != "-") { $total = ($total + $size); }
$count++;
}
fclose($fd);
$fd = fopen("$yesterdaylog","r");
while ($x = fgets($fd,1024)) {
list( , , , , , , , , , $size) = explode(" ", $x, 10);
if($size != "-") { $ytotal = ($ytotal + $size); }
}
fclose($fd);
$change = ($total / $ytotal);
$change = (($change * 100) - 100);
$logduration = ($endtime - $starttime);
$totalsecs = 86400; # total seconds in a day
$timeofdaycovered = ($totalsecs / $logduration);
$actualchange = ($change * $timeofdaycovered);
$change = round($change, 1);
$actualchange = round($actualchange, 1);
$percentofday = (100 / $timeofdaycovered);
$percentofday = round($percentofday, 1);
if($change > 0) { $change = "+$change"; }
if($actualchange > 0) { $actualchange = "+$actualchange"; }
$ytotal = ($ytotal / 1024);
$total = ($total / 1024);
$total = round($total, 1);
$ytotal = round($ytotal, 1);
print "Yesterday's bandwidth: <b>$ytotal</b> KBytes<br>\n";
print "Today's bandwidth so far: <b>$total</b> KBytes<br>\n";
print "Change in bandwidth: <b>${change}%</b><br>\n";
print "Time covered in today's log: <b>$logduration</b> seconds, (<b>${percentofday}%</b> of day) <br>\n";
print "Estimated change in bandwidth (by midnight): <b>${actualchange}%</b>";
?>