From 41681e0a817aaabf930357543bde94c838d0c40b Mon Sep 17 00:00:00 2001 From: jim-p Date: Wed, 20 Oct 2010 18:37:48 -0400 Subject: Add contributed ifbwstats package from Zorac on the forum. --- config/ifbwstats/ifbwstats.inc | 392 ++++++++++++++++++++++++++++++++++ config/ifbwstats/ifbwstats.sh | 33 +++ config/ifbwstats/ifbwstats.xml | 347 ++++++++++++++++++++++++++++++ config/ifbwstats/ifbwstats_cur.php | 197 +++++++++++++++++ config/ifbwstats/ifbwstats_daemon.php | 352 ++++++++++++++++++++++++++++++ config/ifbwstats/ifbwstats_daily.php | 142 ++++++++++++ config/ifbwstats/ifbwstats_disp.php | 206 ++++++++++++++++++ 7 files changed, 1669 insertions(+) create mode 100644 config/ifbwstats/ifbwstats.inc create mode 100644 config/ifbwstats/ifbwstats.sh create mode 100644 config/ifbwstats/ifbwstats.xml create mode 100644 config/ifbwstats/ifbwstats_cur.php create mode 100644 config/ifbwstats/ifbwstats_daemon.php create mode 100644 config/ifbwstats/ifbwstats_daily.php create mode 100644 config/ifbwstats/ifbwstats_disp.php (limited to 'config') diff --git a/config/ifbwstats/ifbwstats.inc b/config/ifbwstats/ifbwstats.inc new file mode 100644 index 00000000..be07e8cc --- /dev/null +++ b/config/ifbwstats/ifbwstats.inc @@ -0,0 +1,392 @@ += 0) + { + for ($n=0; $n<$fndentry; $n++) $fpwrite .= $fpdata[$n]."\n"; + for ($n=$fndentry+6; $n<$i-1; $n++) $fpwrite .= $fpdata[$n]."\n"; + $fpwrite .= $fpdata[$i-1]; + $fp = fopen('/etc/rc.shutdown',"w") or die("Error Reading File"); + fwrite($fp, $fpwrite); + fclose($fp); + } + + //remove reboot entry + $fp = fopen('/etc/rc.reboot',"r") or die("Error Reading File"); + $data = fread($fp, filesize('/etc/rc.reboot')); + fclose($fp); + $fpdata = explode("\n", $data); + $fpwrite = ''; + $i = count($fpdata); + $fndentry = -1; //found reboot entry + for ($n=0; $n<$i; $n++) if ((strstr($fpdata[$n], '# ifBWStats:'))&&($fndentry < 0)) $fndentry = $n; + if ($fndentry >= 0) + { + for ($n=0; $n<$fndentry; $n++) $fpwrite .= $fpdata[$n]."\n"; + for ($n=$fndentry+6; $n<$i-1; $n++) $fpwrite .= $fpdata[$n]."\n"; + $fpwrite .= $fpdata[$i-1]; + $fp = fopen('/etc/rc.reboot',"w") or die("Error Reading File"); + fwrite($fp, $fpwrite); + fclose($fp); + } + + //remove halt entry + $fp = fopen('/etc/rc.halt',"r") or die("Error Reading File"); + $data = fread($fp, filesize('/etc/rc.halt')); + fclose($fp); + $fpdata = explode("\n", $data); + $fpwrite = ''; + $i = count($fpdata); + $fndentry = -1; //found halt entry + for ($n=0; $n<$i; $n++) if ((strstr($fpdata[$n], '# ifBWStats:'))&&($fndentry < 0)) $fndentry = $n; + if ($fndentry >= 0) + { + for ($n=0; $n<$fndentry; $n++) $fpwrite .= $fpdata[$n]."\n"; + for ($n=$fndentry+6; $n<$i-1; $n++) $fpwrite .= $fpdata[$n]."\n"; + $fpwrite .= $fpdata[$i-1]; + $fp = fopen('/etc/rc.halt',"w") or die("Error Reading File"); + fwrite($fp, $fpwrite); + fclose($fp); + } +} + +function cleanup_data_file ($readfile, $writefile) +{ + //----------------------------------------maintain file incase of errors in the file---------------------------------------- + $wandataall = array(); + $wandataarray = array(); + + //read data file + $fp = fopen($readfile,"r") or die("Error Reading File"); + $data = fread($fp, filesize($readfile)); + fclose($fp); + $wandataall = explode("\n", $data); + $n = count($wandataall); + + //expode data into two dim. array + for ($x=0; $x<$n; $x++) + { + $line = explode('|',$wandataall[$x]); + + // save it by coulmns otherwise it will saved like rows + for ($i=0; $i<=2; $i++) + { + if ($i==0) $wandataarray[$x][$i] = date("Y-m-d", strtotime($line[$i])); + else $wandataarray[$x][$i] = $line[$i]; + } + } + + //sort array into chronological order + sort ($wandataarray); + + $m=$n; + //check for multiple entries, and if so add them + for ($x=0; $x<$n; $x++) + { + if (($wandataarray[$x][0] == $wandataarray[$x+1][0]) && ($x+1<$m)) + { + $wandataarray[$x][1] = $wandataarray[$x][1] + $wandataarray[$x+1][1]; + $wandataarray[$x][2] = $wandataarray[$x][2] + $wandataarray[$x+1][2]; + for ($i = $x+1; $i<$n; $i++) + { + if (!is_null($wandataarray[$i+1][0])) $wandataarray[$i][0]=$wandataarray[$i+1][0]; + if (!is_null($wandataarray[$i+1][1])) $wandataarray[$i][1]=$wandataarray[$i+1][1]; + if (!is_null($wandataarray[$i+1][2])) $wandataarray[$i][2]=$wandataarray[$i+1][2]; + } + $x--; + $m--; + } + } + + //check for missing entries + $nextday=$wandataarray[0][0]; + $x=0; + while ($x<$m) + { + //missing entry between days + if ($nextday != $wandataarray[$x][0]) + { + $m++; + for ($i=$m; $i>$x; $i--) + { + + $wandataarray[$i][0] = $wandataarray[$i-1][0]; + $wandataarray[$i][1] = $wandataarray[$i-1][1]; + $wandataarray[$i][2] = $wandataarray[$i-1][2]; + $wandataarray[$i][3] = $wandataarray[$i-1][3]; + $wandataarray[$i][4] = $wandataarray[$i-1][4]; + } + //fill in 0 bytes transfered for missing days + $wandataarray[$x][0] = $nextday; + $wandataarray[$x][1] = 0; + $wandataarray[$x][2] = 0; + } + $x++; + + //add one day to the date + $nextday = date("Y-m-d", strtotime(date("Y-m-d", strtotime($nextday)) . " +1 day")); + + //echo 'x: '.$x.' m: '.$m.' array date: '.strtotime($wandataarray[$x-1][0]).' current date: '.strtotime(date("Y-m-d")).'
'; + //add missing entries at end of file if required + if (($x==$m)&&(strtotime($wandataarray[$x-1][0] ) < strtotime(date("Y-m-d")))) + { + $wandataarray[$x][0] = $nextday; + $wandataarray[$x][1] = 0; + $wandataarray[$x][2] = 0; + $m++; + } + } + + //put array data into variable for wirting + $writedata = ''; + for ($x=0; $x<$m-1; $x++) $writedata .= $wandataarray[$x][0].'|'.$wandataarray[$x][1].'|'.$wandataarray[$x][2]."\n"; + $writedata .= $wandataarray[$m-1][0].'|'.$wandataarray[$m-1][1].'|'.$wandataarray[$m-1][2]; + + //write data file + $fp = fopen($writefile,"w") or die("Error Reading File"); + fwrite($fp, $writedata); + fclose($fp); + //----------------------------------------end of file maintaince---------------------------------------- +} + + +?> \ No newline at end of file diff --git a/config/ifbwstats/ifbwstats.sh b/config/ifbwstats/ifbwstats.sh new file mode 100644 index 00000000..fb7ea4fd --- /dev/null +++ b/config/ifbwstats/ifbwstats.sh @@ -0,0 +1,33 @@ +#!/bin/sh + +. /etc/rc.subr + +name="ifbwstats" +start_cmd="${name}_start" +stop_cmd="${name}_stop" +restart_cmd="${name}_restart" + +# called by pfSense by rc.start_packages on startup +ifbwstats_start() +{ +# ifBWStats: initialize ifbwstats_daemon.php script +/usr/local/bin/php -q /usr/local/www/ifbwstats_daemon.php & 2>/dev/null +} + +ifbwstats_stop() +{ +################################################################ +# pfSense does not call rc.stop_packages so this is not called +################################################################ +kill -INT `cat /var/run/ifbwstats.lock` +} + +ifbwstats_restart() +{ +kill -INT `cat /var/run/ifbwstats.lock` +sleep 2 +/usr/local/bin/php -q /usr/local/www/ifbwstats_daemon.php & 2>/dev/null +} + +load_rc_config $name +run_rc_command "$1" \ No newline at end of file diff --git a/config/ifbwstats/ifbwstats.xml b/config/ifbwstats/ifbwstats.xml new file mode 100644 index 00000000..948e47b8 --- /dev/null +++ b/config/ifbwstats/ifbwstats.xml @@ -0,0 +1,347 @@ + + + + + + . + All rights reserved. + */ +/* ========================================================================== */ +/* + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + */ +/* ========================================================================== */ + ]]> + + + Interface Bandwidth Stats + ifbwstats + None + Currently there are no FAQ items provided. + 1.0 + ifBWStats: if Bandwidth Stats Settings + /usr/local/pkg/ifbwstats.inc + + + ifBWStats + +
Status
+ /ifbwstats_cur.php +
+ + + Daily + /ifbwstats_cur.php + + + Monthly + /ifbwstats_disp.php + + + Settings + /pkg_edit.php?xml=ifbwstats.xml&id=0 + + + + + /usr/local/www/ + 0755 + http://192.168.0.5/packages/config/ifbwstats/ifbwstats_daily.php + + + /usr/local/www/ + 0755 + http://192.168.0.5/packages/config/ifbwstats/ifbwstats_disp.php + + + /usr/local/www/ + 0755 + http://192.168.0.5/packages/config/ifbwstats/ifbwstats_cur.php + + + /usr/local/www/ + 0777 + http://192.168.0.5/packages/config/ifbwstats/ifbwstats_daemon.php + + + /usr/local/pkg/ + 0755 + http://192.168.0.5/packages/config/ifbwstats/ifbwstats.inc + + + /usr/local/etc/rc.d/ + 0755 + http://192.168.0.5/packages/config/ifbwstats/ifbwstats.sh + + + + firstday + First Day: + This is the first day of your billing period. It can be changed at anytime with no impact on the recorded data. + + select + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + intervalrun + Connection Speed: + Select the maximum speed your connection is capable of in either the up or down direction. This insure the connection is monitored appropriately. + + select + + + + + + + + + + + ifmon + Interface to Monitor: + Monitor all interfaces or only the WAN(internet) interfaces. When this is set to WAN, no stats are recorded for any other interfaces. + + select + + + + + + + + logging + Debug Daemon: + Logging of the Daemon's actions to /tmp/ifbwstats.log. Warning, file is not managed and can become very large. + + select + + + + + + + + kill_php_script(); + start_php_script(); + + + update_status ("Adding RC entries..."); + ifbwstats_add_rc(); + update_status ("Setting default values..."); + ifbwstats_set_var(); + update_status ("Adding CRON entry..."); + ifbwstats_add_cron(); + update_status ("Starting php script..."); + start_php_script(); + + + update_status ("Kill php script..."); + kill_php_script(); + update_status ("Backup data files and remove tmp files..."); + + update_status ("Removing RC entries..."); + ifbwstats_remove_rc(); + update_status ("Removing CRON entry..."); + ifbwstats_remove_cron(); + update_status ("Removing files..."); + + update_status ("Remove settings..."); + ifbwstats_remove_var(); + +
\ No newline at end of file diff --git a/config/ifbwstats/ifbwstats_cur.php b/config/ifbwstats/ifbwstats_cur.php new file mode 100644 index 00000000..6cb4870e --- /dev/null +++ b/config/ifbwstats/ifbwstats_cur.php @@ -0,0 +1,197 @@ +'; +include("fbegin.inc"); +echo '

ifBWStats: Daily Statistics For Current Billing Period

'; + +//find all valid data files for active and inactive interfaces +//assume monitoring all interfaces +$datafilestores = array(); +$n=0; + +//if only monitoring one inteface +if ($config['installedpackages']['ifbwstats']['config'][0]['ifmon'] != 'all') +{ + //dont check conf directory, as if only one interface is being monitored, it must be used and therefore in the tmp dir + if (file_exists('/tmp/ifbwstats-'.$config['installedpackages']['ifbwstats']['config'][0]['ifmon'].'.data')) + { + $datafilestores[$n] = '/tmp/ifbwstats-'.$config['installedpackages']['ifbwstats']['config'][0]['ifmon'].'.data'; + cleanup_data_file ($datafilestores[$n], $datafilestores[$n]); + $n++; + } +} +else +{ + if ($handle = opendir('/tmp')) + { + while (false !== ($file = readdir($handle))) + { + if ((preg_match ("/ifbwstats/", $file))&&(preg_match ("/.data/", $file))) + { + $datafilestores[$n] = '/tmp/'.$file; + cleanup_data_file ($datafilestores[$n], $datafilestores[$n]); + $n++; + } + } + } + + if ($handle = opendir('/cf/conf')) + { + while (false !== ($file = readdir($handle))) + { + $filefound = 'no'; + if ((preg_match ("/ifbwstats-/", $file))&&(preg_match ("/.data/", $file))) + { + for ($i = 0; $i < $n; $i++) if (preg_match ("/$file/", $datafilestores[$i])) $filefound = 'yes'; + if ($filefound == 'no') + { + cleanup_data_file ('/cf/conf/'.$file, '/tmp/'.$file); + $datafilestores[$n] = '/tmp/'.$file; + $n++; + } + } + } + } +} + +//display tabs +echo ''; +echo ''; +echo ''; +echo '
'; +$tab_array[0] = array ("Daily", true, "ifbwstats_cur.php"); +$tab_array[1] = array ("Monthly", false, "ifbwstats_disp.php"); +$tab_array[2] = array ("Settings", false, "pkg_edit.php?xml=ifbwstats.xml"); +display_top_tabs($tab_array); +echo '
'; + +//cycle through all valid data files found +foreach ($datafilestores as $wandataallfile) +{ +//----------------------------------------begin file statistics monthly display---------------------------------------- + //read data file + $fp = fopen($wandataallfile,"r") or die("Error Reading File"); + $data = fread($fp, filesize($wandataallfile)); + fclose($fp); + $wandataall = explode("\n", $data); + $n = count($wandataall); + + $monthintotal = 0; + $monthouttotal = 0; + + $interfacename = str_replace('.data', '', $wandataallfile); + $interfacename = str_replace('/tmp/ifbwstats-', '', $interfacename); + $interfacename = str_replace('/cf/conf/ifbwstats-', '', $interfacename); + + echo '
'; + echo ''; + echo ''; + echo '
'.$interfacename.' Statistics Summary
'; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo '
DateDownloadedUploadedTotal Transfered
'; + echo '
'; + echo '
'; + echo ''; + $htmlcode = ''; + for ($i=0 ; $i < $n ; $i++ ) + { + $dataset=explode("|", $wandataall[$i]); + $dateset=explode("-", $dataset[0]); + $monthintotal = $monthintotal + $dataset[1]; + $monthouttotal = $monthouttotal + $dataset[2]; + $total = round((($dataset[1]+$dataset[2])/1024/1024/1024),2); + $dataset[1] = round(($dataset[1]/1024/1024),2); + $dataset[2] = round(($dataset[2]/1024/1024),2); + + //show daily stats + $htmlcode = $htmlcode.''; + + //if there is no more data, show month + if (($n-1) == $i) + { + $total = round((($monthintotal + $monthouttotal)/1024/1024/1024),2); + $monthintotal = round(($monthintotal/1024/1024/1024),2); + $monthouttotal = round(($monthouttotal/1024/1024/1024),2); + + $htmlcode = $htmlcode.''; + echo $htmlcode; + } + //if at the end of the billing cycle, erase month htlmcode as we only want to show the current month + if ($dateset[2] == ($firstday-1)) + { + $htmlcode = ''; + $monthintotal = 0; + $monthouttotal = 0; + } + + //if the billing cycle starts on the first day of the month, figure out the last day of the previous month, and if appropriate erase htmlcode + if ($firstday == '1') + { + //find the last day of the month + $maxday = date("t", strtotime($dateset[0]."-".$dateset[1]."-".$dateset[2])); + if ($dateset[2]==$maxday) + { + $htmlcode = ''; + $monthintotal = 0; + $monthouttotal = 0; + } + } + } + echo '
'.$dataset[0].''.$dataset[1].'MB'.$dataset[2].'MB'.$total.'GB
Current Month Total↓'.$monthintotal.'GB↑'.$monthouttotal.'GB↕'.$total.'GB
'; + echo '
'; + echo '
'; + echo '
'; + unset ($writedata); +//----------------------------------------end file statistics monthly display---------------------------------------- +} +//end foreach loop +echo '
'; + +include("fend.inc"); +echo ''; +echo ''; +?> \ No newline at end of file diff --git a/config/ifbwstats/ifbwstats_daemon.php b/config/ifbwstats/ifbwstats_daemon.php new file mode 100644 index 00000000..17026fba --- /dev/null +++ b/config/ifbwstats/ifbwstats_daemon.php @@ -0,0 +1,352 @@ + $maxbytesin) || ($outbytes < 0) || ($outbytes > $maxbytesout)) + { + $inbytes = 0; + $outbytes = 0; + if (file_exists($wandatalastfile)) unlink ($wandatalastfile); + } + + $foundfile = 'null'; + if (file_exists($wandataallfile)) $foundfile = $wandataallfile; + else + { + if (file_exists($wandatabackupfile)) $foundfile = $wandatabackupfile; + } + + //if no file is found, create new data, else read file and add to existing data + if ($foundfile == 'null') + { + $wanwritedata = date("Y-m-d").'|'.$inbytes.'|'.$outbytes; + } + else + { + //read data file + $wandataall = explode("\n", file_get_contents($foundfile)); + $n = count($wandataall); + + //if last line of data date matchs current date, add to totals, else add new line + $dataset = explode("|", $wandataall[$n-1]); + if ($dataset[0] == date("Y-m-d")) + { + $dataset[1]=$dataset[1]+$inbytes; + $dataset[2]=$dataset[2]+$outbytes; + $wandataall[$n-1]=$dataset[0].'|'.$dataset[1].'|'.$dataset[2]; + } + else + { + $wandataall[$n] = date("Y-m-d").'|'.$inbytes.'|'.$outbytes; + } + + //number of data entries (days) + $n = count($wandataall); + + //if more than three years worth of data, trim data to 4 years (1460 days) + $start = 0; + if ($n > 1460) $start = $n - 1460; + + //generate file data to write + for ($i=$start ; $i < ($n-1) ; $i++ ) $wanwritedata = $wanwritedata.$wandataall[$i]."\n"; + $wanwritedata = $wanwritedata.$wandataall[$n-1]; + } + + //write data file + $fp = fopen($wandataallfile,"w") or die("Error Reading File"); + fwrite($fp, $wanwritedata); + fclose($fp); + } + else + { + if (file_exists($wandatalastfile)) unlink ($wandatalastfile); + } +} + +pcntl_signal(SIGTERM, 'sig_handler'); +pcntl_signal(SIGINT, 'sig_handler'); +pcntl_signal(SIGUSR1, 'sig_handler'); + +global $config; +global $g; +global $_MYDAEMON_SHOULD_STOP; +global $_MYDAEMON_SHOULD_QUERY; + +//logging -> yes or no +$logging = $config['installedpackages']['ifbwstats']['config'][0]['logging']; + +if ($logging == 'yes') +{ + global $logfile; + $logfile = fopen("/tmp/ifbwstats-daemon.log","a") or die("Error Reading File"); + $parentpid = getmypid(); + fwrite($logfile, date('Y-m-d H:i:s')." Startup - Parent PID is: ".$parentpid."\n"); +} + +if ($g['platform'] == 'cdrom') +{ + fwrite($logfile, date('Y-m-d H:i:s')." Daemon will not run on CD Rom platform, exiting... \n"); + fclose ($logfile); + exit (); +} +if (isLocked()) +{ + if ($logging == 'yes') fwrite($logfile, date('Y-m-d H:i:s')." Daemon is already running, exiting second instance (".getmypid().") \n"); + fclose ($logfile); + exit (); +} + +// endless loop +while (1) +{ + if (isset($_MYDAEMON_SHOULD_STOP) AND $_MYDAEMON_SHOULD_STOP) break; + $pidA = pcntl_fork(); + if($pidA) + { + // parent process runs here + // wait until the child has finished processing then end the script + pcntl_waitpid($pid, $status, WUNTRACED); + //calc seconds to midnight + $sleeptime = (mktime(23, 59, 55, date ('m, d, Y'))) - time(); + //use whichever is less, seconds to midnight or interval, running script just prior to midnight insure accurate daily reporting as standard timing interval could be as great at 50min + if (($sleeptime > $config['installedpackages']['ifbwstats']['config'][0]['intervalrun']) || ($sleeptime < 5)) $sleeptime = $config['installedpackages']['ifbwstats']['config'][0]['intervalrun']; + if ($logging == 'yes') fwrite($logfile, date('Y-m-d H:i:s')." Parent (".$parentpid.") Sleep for: ".$sleeptime."\n"); + for ($i=0; $i<$sleeptime; $i++) + { + if ((isset($_MYDAEMON_SHOULD_QUERY) AND $_MYDAEMON_SHOULD_QUERY) || (isset($_MYDAEMON_SHOULD_STOP) AND $_MYDAEMON_SHOULD_STOP)) + { + $_MYDAEMON_SHOULD_QUERY = false; + break; + } + else sleep (1); + } + } + else + { + //child process runs here + if ($logging == 'yes') fwrite($logfile, date('Y-m-d H:i:s')." Child Process ". getmypid(). " Reading Interfaces... (Parent: ".$parentpid.")\n"); + if ($config['installedpackages']['ifbwstats']['config'][0]['ifmon'] != 'all') interface_query($config['installedpackages']['ifbwstats']['config'][0]['ifmon']); + else + { + foreach ($config[interfaces] as $if => $value) + { + interface_query($if); + } + } + exit (0); + } +} + +//run query one last time +if ($logging == 'yes') fwrite($logfile, date('Y-m-d H:i:s')." Parent Process ". getmypid(). " Reading Interfaces One Last Time... \n"); +if ($config['installedpackages']['ifbwstats']['config'][0]['ifmon'] != 'all') interface_query($config['installedpackages']['ifbwstats']['config'][0]['ifmon']); +else +{ + foreach ($config[interfaces] as $if => $value) + { + interface_query($if); + } +} +// backup data files to conf dir on exit +if ($g['platform'] != 'pfSense') exec ('/etc/rc.conf_mount_rw'); +exec('cp /tmp/ifbwstats-*.data /cf/conf/'); +if ($g['platform'] != 'pfSense') exec ('/etc/rc.conf_mount_ro'); + +if ($logging == 'yes') +{ + fwrite($logfile, date('Y-m-d H:i:s')." Shutdown Parent ".$parentpid." \n"); + fclose ($logfile); +} + +if( file_exists( LOCK_FILE ) ) unlink( LOCK_FILE ); + +exit (0); + +?> + diff --git a/config/ifbwstats/ifbwstats_daily.php b/config/ifbwstats/ifbwstats_daily.php new file mode 100644 index 00000000..1bd29ea2 --- /dev/null +++ b/config/ifbwstats/ifbwstats_daily.php @@ -0,0 +1,142 @@ +'; +include("fbegin.inc"); +echo '

ifBWStats: Daily Statistics For '.$interfacename.'

'; +echo 'Month Ending: '.str_replace('%20', ' ', $queryparts[3]).'

'; + +//display tabs +echo ''; +echo ''; +echo ''; +echo '
'; +$tab_array[0] = array ("Daily", false, "ifbwstats_cur.php"); +$tab_array[1] = array ("Monthly", false, "ifbwstats_disp.php"); +$tab_array[2] = array ("Settings", false, "pkg_edit.php?xml=ifbwstats.xml"); +$tab_array[3] = array ("Archive", true, $url); +display_top_tabs($tab_array); +echo '
'; +echo '
'; + +$foundfile = 'null'; +if (file_exists($queryparts[0])) $foundfile = $queryparts[0]; +if ($foundfile == 'null') +{ + echo 'Sorry, no data file found.'; +} +else +{ + $fp = fopen($foundfile,"r") or die("Error Reading File"); + $data = fread($fp, filesize($foundfile)); + fclose($fp); + $wandataall = explode("\n", $data); + $n = count($wandataall); + + $monthintotal = 0; + $monthouttotal = 0; + $monthdaystart = 0; + + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + + for ($i=$queryparts[1] ; $i < $queryparts[2]+1 ; $i++ ) + { + $dataset=explode("|", $wandataall[$i]); + $dateset=explode("-", $dataset[0]); + $monthintotal = $monthintotal + $dataset[1]; + $monthouttotal = $monthouttotal + $dataset[2]; + $total = round((($dataset[1]+$dataset[2])/1024/1024/1024),2); + $dataset[1] = round(($dataset[1]/1024/1024),2); + $dataset[2] = round(($dataset[2]/1024/1024),2); + + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + } + + $total = round((($monthintotal + $monthouttotal)/1024/1024/1024),2); + $monthintotal = round(($monthintotal/1024/1024/1024),2); + $monthouttotal = round(($monthouttotal/1024/1024/1024),2); + + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo '
DateDownloadedUploadedTotal Transfered
'.$dataset[0].''.$dataset[1].'MB'.$dataset[2].'MB'.$total.'GB
Month Totals↓'.$monthintotal.'GB↑'.$monthouttotal.'GB↕'.$total.'GB
'; +} + +echo '
'; +echo '
'; + +include("fend.inc"); +echo ''; +echo ''; + +?> \ No newline at end of file diff --git a/config/ifbwstats/ifbwstats_disp.php b/config/ifbwstats/ifbwstats_disp.php new file mode 100644 index 00000000..c949b6cb --- /dev/null +++ b/config/ifbwstats/ifbwstats_disp.php @@ -0,0 +1,206 @@ +'; +include("fbegin.inc"); +echo '

ifBWStats: Monthly Statistics

'; + +//find all valid data files for active and inactive interfaces +//assume monitoring all interfaces +$datafilestores = array(); +$n=0; + +//if only monitoring one inteface +if ($config['installedpackages']['ifbwstats']['config'][0]['ifmon'] != 'all') +{ + //dont check conf directory, as if only one interface is being monitored, it must be used and therefore in the tmp dir + if (file_exists('/tmp/ifbwstats-'.$config['installedpackages']['ifbwstats']['config'][0]['ifmon'].'.data')) + { + $datafilestores[$n] = '/tmp/ifbwstats-'.$config['installedpackages']['ifbwstats']['config'][0]['ifmon'].'.data'; + cleanup_data_file ($datafilestores[$n], $datafilestores[$n]); + $n++; + } +} +else +{ + if ($handle = opendir('/tmp')) + { + while (false !== ($file = readdir($handle))) + { + if ((preg_match ("/ifbwstats/", $file))&&(preg_match ("/.data/", $file))) + { + $datafilestores[$n] = '/tmp/'.$file; + cleanup_data_file ($datafilestores[$n], $datafilestores[$n]); + $n++; + } + } + } + + if ($handle = opendir('/cf/conf')) + { + while (false !== ($file = readdir($handle))) + { + $filefound = 'no'; + if ((preg_match ("/ifbwstats-/", $file))&&(preg_match ("/.data/", $file))) + { + for ($i = 0; $i < $n; $i++) if (preg_match ("/$file/", $datafilestores[$i])) $filefound = 'yes'; + if ($filefound == 'no') + { + cleanup_data_file ('/cf/conf/'.$file, '/tmp/'.$file); + $datafilestores[$n] = '/tmp/'.$file; + $n++; + } + } + } + } +} + +//display tabs +echo ''; +echo ''; +echo ''; +echo '
'; +$tab_array[0] = array ("Daily", false, "ifbwstats_cur.php"); +$tab_array[1] = array ("Monthly", true, "ifbwstats_disp.php"); +$tab_array[2] = array ("Settings", false, "pkg_edit.php?xml=ifbwstats.xml"); +display_top_tabs($tab_array); +echo '
'; + +//cycle through all valid data files found +foreach ($datafilestores as $wandataallfile) +{ +//----------------------------------------begin file statistics monthly display---------------------------------------- + //read data file + $fp = fopen($wandataallfile,"r") or die("Error Reading File"); + $data = fread($fp, filesize($wandataallfile)); + fclose($fp); + $wandataall = explode("\n", $data); + $n = count($wandataall); + + $monthintotal = 0; + $monthouttotal = 0; + $monthdaystart = 0; + + $interfacename = str_replace('.data', '', $wandataallfile); + $interfacename = str_replace('/tmp/ifbwstats-', '', $interfacename); + $interfacename = str_replace('/cf/conf/ifbwstats-', '', $interfacename); + + echo '
'; + echo ''; + echo ''; + echo '
'.$interfacename.' Statistics Summary
'; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo '
Month EndingDownloadedUploadedTotal Transfered
'; + echo '
'; + echo '
'; + //echo '
'; + echo ''; + + for ($i=0 ; $i < $n ; $i++ ) + { + $dataset=explode("|", $wandataall[$i]); + $dateset=explode("-", $dataset[0]); + $monthintotal = $monthintotal + $dataset[1]; + $monthouttotal = $monthouttotal + $dataset[2]; + $total = round((($dataset[1]+$dataset[2])/1024/1024/1024),2); + $dataset[1] = round(($dataset[1]/1024/1024),2); + $dataset[2] = round(($dataset[2]/1024/1024),2); + + //show daily stats + /*echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo '';*/ + + //as data file saved by day, determin if a monthy total is required to be shown based on setting 'first day of billing period' + $showmonth = '0'; + + //if at the end of the billing cycle show month totals, 1 to indicate that a full billing period has passed and to show a 'month end' total + if ($dateset[2] == ($firstday-1)) $showmonth = '1'; + //if there is no more data, show month totals, 2 to show 'current month' + if (($n-1) == $i) $showmonth = '2'; + //if the billing cycle starts on the first day of the month, figure out the last day of the previous month, and if appropriate show month totals + if ($firstday == '1') + { + //find the last day of the month + $maxday = date("t", strtotime($dateset[0]."-".$dateset[1]."-".$dateset[2])); + if ($dateset[2]==$maxday) $showmonth = '1'; + } + + if (($showmonth == '1')||($showmonth == '2')) + { + $total = round((($monthintotal + $monthouttotal)/1024/1024/1024),2); + $monthintotal = round(($monthintotal/1024/1024/1024),2); + $monthouttotal = round(($monthouttotal/1024/1024/1024),2); + if ($showmonth == '2') $dataset[0]='Current%20Month'; + + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + $monthdaystart = $i + 1; + $monthintotal = 0; + $monthouttotal = 0; + } + } + echo '
'.$dataset[0].''.$dataset[1].'MB'.$dataset[2].'MB'.$total.'GB
'.str_replace('%20', ' ', $dataset[0]).'↓'.$monthintotal.'GB↑'.$monthouttotal.'GB↕'.$total.'GB
'; + echo '
'; + echo '
'; + echo '
'; + unset ($writedata); +//----------------------------------------end file statistics monthly display---------------------------------------- +} +//end foreach loop +echo '
'; + +include("fend.inc"); +echo ''; +echo ''; +?> \ No newline at end of file -- cgit v1.2.3