<?php
ini_set("display_errors", "on");
ini_set("display_startup_errors", "on");
#
# Copyright (C) 2008 Red Hat, Inc.
# Author: Andreas Thienemann <athienem@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library General Public License as published by
# the Free Software Foundation; version 2 only
#
# 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 Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# Copyright 2004, 2005 Red Hat, Inc.
#
# AUTHOR: Andreas Thienemann <athienem@redhat.com>
#

require_once('config.inc.php');
require_once('common.inc.php');
require_once('db_handler.inc.php');

// Initiate the databaseconnection
$dbh = '';
init_db();

// The main interface/welcome screen
if (!array_key_exists('action', $_REQUEST) OR (array_key_exists('action', $_REQUEST) && $_REQUEST['action'] == 'probe_select' && empty($_REQUEST['probe']))) {
	print(html_header($title = 'FlowMon '.$prog_vers.' - running on '.$_SERVER['HTTP_HOST']));
	print(page_header($title = 'FlowMon overview'));

	print('<div style="margin-left: 30px; margin-right: 30px;"><h1>Network Flow Monitor</h1></div>');
	$num_flows = get_num_flows();
	$num_systems = get_num_systems();
	print('<div style="margin: 30px; height: 175px; width: 40%; text-align: left; float: left;">
		<h2>Database Statistics</h2>
		<p><table>
			<tr><td>Number of reporting FlowProbes:</td><td>'.get_num_probes().'<td></tr>
			<tr><td>Number of archived Flows:</td><td>'.$num_flows.'<td></tr>
			<tr><td>Number of tracked Systems:</td><td>'.$num_systems.'<td></tr>
			<tr><td>First Flow occurence:</td><td>'.strftime($date_fmt_str, get_oldest_flow_ts()).'<td></tr>
			<tr><td>Most recent Flow occurence:</td><td>'.strftime($date_fmt_str, get_newest_flow_ts()).'<td></tr>
			<tr><td>Database Size:</td><td>'.round((get_db_size() / 1024 / 1024), 2).' MB<td></tr>
		</table></p>
		</div>');
	$bytes = get_bytes();
	$pkts = get_pkts();
	print('<div style="margin: 30px; height: 175px; width: 40%; text-align: left; float: right;">
		<h2>Network Statistics</h2>
		<p><table>
			<tr><td>Total Bytes tracked</td><td>'.round((get_bytes() / 1024 / 1024), 2).' MB<td></tr>
			<tr><td>Total Pakets tracked</td><td>'.get_pkts().'<td></tr>
			<tr><td>Average Paketsize</td><td>'.@round(($bytes / $pkts), 0).' Bytes<td></tr>
			<tr><td>Average Traffic per system</td><td>'.@round(($bytes / $num_systems / 1024 / 1024), 2).' MB<td></tr>
			<tr><td>Average Traffic per flow</td><td>'.@round(($bytes / $num_flows / 1024 / 1024), 2).' MB<td></tr>
		</table></p>
		</div>');
//	print('<div style="clear: left;"></div>');
	print('<div style="margin-left: 30px; margin-right: 30px; clear: right; text-align: center;"><h2>Detailed traffic graphs</h2></div>');
	print('<div style="margin-left: auto; margin-right: auto; width: 400px; height: 100px;">');
	print('<form action="'.$_SERVER['SCRIPT_NAME'].'" method="GET"><select name="probe" size=1>');
		print('<option value="" selected="selected">Please select a FlowProbe to chart</option>');
	foreach(get_probes() as $probe => $hostname) {
		print('<option value="'.$probe.'">'.$hostname.' ('.$probe.')</option>');
	}
	print('</select><input type="hidden" name="action" value="probe_select"><input type="submit" value="Graph!"></form>');
	print('</div>');
	print(html_footer());


// We have selected a probe, present with detailes form to get the chart parameters
} else if (array_key_exists('action', $_REQUEST) && $_REQUEST['action'] == 'probe_select') {
	print(html_header($title = 'FlowMon '.$prog_vers.' - running on '.$_SERVER['HTTP_HOST'], $calendar = True));
	print(page_header($title = 'FlowMon Chart Parameters'));
	print('<div style="margin: 30px auto 30px auto; height: 350px; width: 40%; text-align: left;">
		<h1> Statistics</h1>
                <form action="'.$_SERVER['SCRIPT_NAME'].'" method="GET">
		<table>
		<tr><td>Probe</td><td><input type="hidden" name="probe" value="'.$_REQUEST['probe'].'" />'.gethostbyaddr(htmlspecialchars($_REQUEST['probe'])).' ('.htmlspecialchars($_REQUEST['probe']).')</td></tr>
		<tr><td>Start Date</td><td><input type="text" name="startdate" value="'.strftime('%Y-%m-%d').'" /></td></tr>
		<tr><td>Start Time</td><td><input type="text" name="starttime" value="'.strftime('%H:%M', (time() - 3600)).'" /></td></tr>
		<tr><td>End Date</td><td><input type="text" name="enddate" value="'.strftime('%Y-%m-%d').'" /></td></tr>
		<tr><td>End Time</td><td><input type="text" name="endtime" value="'.strftime('%H:%M').'" /></td></tr>
		<tr><td>Chart Style</td><td><select name="style" size=1>');
		foreach ($chart_styles as $style=>$file) {
			print('<option>'.$style.'</option>');
		}
	print('</select></td></tr>
		<tr><td colspan="2" style="text-align: right;"><input type="hidden" name="action" value="chart" /><input type="submit" value="Chart" /></form></td></tr>
		<tr><td colspan="2"><hr></td></tr>
		<tr><td><form action="'.$_SERVER['SCRIPT_NAME'].'" method="GET">Chart Style</td><td><select name="style" size=1>');
		foreach ($chart_styles as $style=>$file) {
			print('<option>'.$style.'</option>');
		}
		print('</select></td></tr>
		<tr><td><input type="hidden" name="probe" value="'.$_REQUEST['probe'].'" /><select name="timeframe"><option value="1800">Past 30 minutes</option><option value="3600">Past hour</option><option value="7200">Past 2 hours</option></select></td><td style="text-align: right;"><input type="hidden" name="action" value="rollingchart" /><input type="submit" value="Rolling Chart" /></td></tr>
		</table>
		</div>');

	print(html_footer());


// We got everything needed to chart the requested data.
// Do it!
} else if (array_key_exists('action', $_REQUEST) && $_REQUEST['action'] == 'chart') {
	print(html_header($title = 'FlowMon '.$prog_vers.' - running on '.$_SERVER['HTTP_HOST'], $calendar = True));
	print(page_header($title = 'FlowMon Traffic Chart'));
	print('<div style="margin: 30px auto 30px auto; width: 850px; text-align: left;">
		<h1> Statistics</h1>');

	// Import the Chart-Plugin
	if (array_key_exists($_REQUEST['style'], $chart_styles)) {
		require_once('charts/'.$chart_styles[$_REQUEST['style']]);
	}

	// Sanitize user input
	$starttime = strtotime($_REQUEST['startdate'].' '.$_REQUEST['starttime']);
	$endtime = strtotime($_REQUEST['enddate'].' '.$_REQUEST['endtime']);
	if (!isvalid_probe($_REQUEST['probe'])) {
		die("Invalid Probe");
	}

	$file = chart_prepare($_REQUEST['probe'], $starttime, $endtime);

	print('<img src="'.$file.'" />');

	print('	</div>');

	print(html_footer());

// Rolling chart requested
} else if (array_key_exists('action', $_REQUEST) && $_REQUEST['action'] == 'rollingchart') {
	print(html_header($title = 'FlowMon '.$prog_vers.' - running on '.$_SERVER['HTTP_HOST'], $calendar = False, $refresh = 300));
	print(page_header($title = 'FlowMon Rolling Traffic Chart'));
	print('<div style="margin: 30px auto 30px auto; width: 850px; text-align: left;">
		<h1>Rolling Statistics (updated every 5min)</h1>');

	// Import the Chart-Plugin
	if (array_key_exists($_REQUEST['style'], $chart_styles)) {
		require_once('charts/'.$chart_styles[$_REQUEST['style']]);
	}

	// Sanitize user input
	if (!isvalid_probe($_REQUEST['probe'])) {
		die("Invalid Probe");
	}

	$file = chart_prepare($_REQUEST['probe'], False, False, $_REQUEST['timeframe']);

	print('<img src="'.$file.'" />');

	print('	</div>');

	print(html_footer());


}
