* * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * @author S. McAvoy * @package mrtgtu * @copyright 2011 * @version 0.9.1b * * Installation * -------------------- * * Just edit the LOG_DIR constant below and place this script and the * accompanying style.css files in a directory that is accessible by your * http server. * **************************************************************************/ define('LOG_DIR', '../MRTG/'); /*************************************************************************** * No need to edit anything below this line unless you want to hack. */ define('VERSION', '0.9.1b'); define('PACKAGE_NAME', 'mrtgtu'); /** * Scales the number of bytes into kilo, mega, giga, etc. * @param int $num The number of bits or bytes to scale. * @param string $suffix * @param bool $metric If true, results will be scaled in base 10 insted of * base 2. * @param int $precision The number of decimal places to keep. * @return string Number of bytes scaled in appropriate units (eg. M G). */ function auto_scale($num, $suffix, $metric=false, $precision=2) { $unit_map = array("K", "M", "G", "T", "E"); $unit = 0; $denominator = ($metric) ? 1000.0 : 1024.0; while (($num /= $denominator) > $denominator) $unit++; return round($num, $precision) . ' ' . $unit_map[$unit] . $suffix; } /** * Converts bytes to bits. * @param int $bytes The number of bytes to convert to bits. * @return int The number of bits in the given bytes. */ function to_bits($bytes) { return $bytes * 8; } /** * Retrieves the names of the log files from the given directory. * @param string $dir the director containing MRTG log files. * @return array absolute path names for MRTG log files which were found. */ function get_log_filenames($dir) { $file = ''; $files = array(); if (!is_dir($dir)) exit("The directory '$dir' does not exist."); $h = opendir($dir); if (!$h) exit("Unable to open directory '$dir'."); while ($file = readdir($h)) if (preg_match('/\.log$/', $file)) $files[] = $dir . '/' . $file; closedir($h); sort($files, SORT_STRING); return $files; } /** * Parses an MRTG log file and extracts the relevant sections. * @param int $time_start The UNIX timestamp to start from. * @param int $time_stop The UNIX timestamp to stop at. * @return array */ function parse_log_file($time_start, $time_stop, $filename) { $results = array(); $fd = fopen($filename, "r"); if (!$fd) exit("Unable to open log file '$filename'."); while (!feof($fd)) { $line = fgets($fd); $cols = explode(" ", $line); // We only want the relevant sections in the log file. if ($cols[0] >= $time_start && $cols[0] <= $time_stop) $results[] = $cols; } fclose($fd); return $results; } /** * Parses an array of results generated by 
parse_log_file
. * @param int $bytes_in The variable to store the amount of ingress traffic. * @param int $bytes_out The variable to store the amount of egress traffic. * @param array $results An array generated by 
parse_log_file
. */ function parse_results(&$bytes_in, &$bytes_out, $results) { $t = 0; $dt = 0; $prev_time = 0; $bytes_in = 0; $bytes_out = 0; for ($i=count($results)-1; $i>=0; $i--) { if ($i == count($results)-1) { $prev_time = $results[$i][0]; } else { $t = $results[$i][0]; $dt = $t - $prev_time; $bytes_in += $dt * $results[$i][1]; $bytes_out += $dt * $results[$i][2]; $prev_time = $t; } } } /** * Creates a UNIX timestamp for a given year, month, and day. * The day is assumed to start at 00:00:00 hours. * @param int $year * @param int $month * @param int $day * @return int A UNIX timestamp. */ function mktime_start($year, $month, $day) { return strtotime($year.'-'.$month.'-'.$day.' 00:00:00'); } /** * Creates a UNIX timestamp for a given year, month, and day. * The day is assumed to end at 23:59:59 hours. * @param int $year * @param int $month * @param int $day * @return int A UNIX timestamp. */ function mktime_stop($year, $month, $day) { return strtotime($year.'-'.$month.'-'.$day.' 23:59:59'); } /** * Outputs an HTML table containing traffic data for a given MRTG log file. * @param int $time_start The UNIX timestamp of the start time. * @param int $time_stop The UNIX timestamp of the end time. * @param string $logfile The absolute path of the MRTG log to analyze. * @css_id int A unique numeric value to append to the DIV's ID property. */ function prt_traffic($time_start, $time_stop, $logfile, $css_id) { $dt = $time_stop - $time_start; $bytes_in = 0; $bytes_out = 0; $bits_in = 0; $bits_out = 0; $hostname = ''; $ip = ''; $results = null; // Parse the logfile and determine total traffic consumed. $results = parse_log_file($time_start, $time_stop, $logfile); parse_results($bytes_in, $bytes_out, $results); // Convert bytes to bits $bits_in = to_bits($bytes_in); $bits_out = to_bits($bytes_out); // Print the traffic table for this host. print('
'."\n". '
' . "\n" . '
File: ' . basename($logfile) . '
' . "\n" . '
' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n". '' . "\n" . '' . "\n". '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '
Ingress Traffic Egress Traffic
Bits InAvg. Bits InBytes InAvg. Bytes In Bits OutAvg. Bits OutBytes OutAvg. Bytes Out
' . auto_scale($bits_in, 'b') . '' . auto_scale($bits_in/$dt, 'b/s') . '' . auto_scale($bytes_in, 'B') . '' . auto_scale($bytes_in/$dt, 'b/s') . ' ' . auto_scale($bits_out, 'b') . '' . auto_scale($bits_out/$dt, 'b/s') . '' . auto_scale($bytes_out, 'B') . '' . auto_scale($bytes_out/$dt, 'B/s') . '
' . "\n" . '
'."\n"); } function clean_date_input($date) { return preg_replace('/[^0-9]/', '', $date); } /******************************** * Process the form data */ $_now = time(); $input = array(); // If the form has been submitted, grab the date components // otherwise use default values. if (isset($_GET['_submit']) && $_GET['_submit'] == 1) { $input['start_year'] = clean_date_input($_GET['start_year']); $input['start_month'] = clean_date_input($_GET['start_month']); $input['start_date'] = clean_date_input($_GET['start_date']); $input['stop_year'] = clean_date_input($_GET['stop_year']); $input['stop_month'] = clean_date_input($_GET['stop_month']); $input['stop_date'] = clean_date_input($_GET['stop_date']); } else { // Use the current month for default values if the page // is loaded without populated parameters. $input['start_year'] = date('Y', $_now); $input['start_month'] = date('m', $_now); $input['start_date'] = '01'; $input['stop_year'] = date('Y', $_now); $input['stop_month'] = date('m', $_now); $input['stop_date'] = date('t', $_now); // Last day of current month. } // Create the start and stop timestamps that we'll use when scanning // throught the MRTG log files. $start_time = mktime_start($input['start_year'], $input['start_month'], $input['start_date']); $stop_time = mktime_stop($input['stop_year'], $input['stop_month'], $input['stop_date']); // Set a friendly date format to be used in the page title. $start_title = date('Y-m-d H:i:s', $start_time); $stop_title = date('Y-m-d H:i:s', $stop_time); ?> '."\n"); ?> Total Bandwidth Consumption from <?php print($start_title . ' until ' . $stop_title); ?>

MRTG Traffic Utilization v

From: - - To: - - Enter dates as: yyyy-mm-dd

Report for to .

No log files were found. Have you configured the LOG_DIR value?
\n"); } ?>