<?php
#
# 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>
#


// initiate the database connection
function init_db() {
	global $dbh, $db_host, $db_name, $db_user, $db_pass;
	try {
		if ($database = 'mysql') {
			$dbh = new PDO('mysql:host='.$db_host.';dbname='.$db_name, $db_user, $db_pass);
		} else if ($database = 'postgresql') {
			$dbh = new PDO('pgsql:host='.$db_host.';dbname='.$db_name, $db_user, $db_pass);
		}
		$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
	} catch (PDOException $e) {
		print "Error!: " . $e->getMessage() . "<br/>";
		die();
	}
}

// return the number of reporting probes
function get_num_probes() {
	global $dbh;
	try {
		$q = $dbh->query("SELECT COUNT(FlowSrc) FROM FlowSources;");
		$r = $q->fetch();
	} catch (PDOException $e) {
		print "Error!: " . $e->getMessage() . "<br/>";
		die();
	}

	return $r[0];
}

// return the number of archived flows
function get_num_flows() {
	global $dbh;
	try {
		$q = $dbh->query("SELECT COUNT(FlowSrc) FROM Flows;");
		$r = $q->fetch();
	} catch (PDOException $e) {
		print "Error!: " . $e->getMessage() . "<br/>";
		die();
	}

	return $r[0];
}

// return the number of systems in the databse
function get_num_systems() {
	global $dbh;
	try {
		$q = $dbh->query("SELECT COUNT(DISTINCT IPSrc) FROM Flows;");
		$r = $q->fetch();
	} catch (PDOException $e) {
		print "Error!: " . $e->getMessage() . "<br/>";
		die();
	}

	return $r[0];
}

// return the oldest flow in the db as a unix timestamp
function get_oldest_flow_ts() {
	global $dbh;
	try {
		$q = $dbh->query("SELECT MIN(TimeStart) FROM Flows;");
		$r = $q->fetch();
	} catch (PDOException $e) {
		print "Error!: " . $e->getMessage() . "<br/>";
		die();
	}

	return $r[0];
}

// return the oldest flow in the db as a unix timestamp
function get_newest_flow_ts() {
	global $dbh;
	try {
		$q = $dbh->query("SELECT MAX(TimeStop) FROM Flows;");
		$r = $q->fetch();
	} catch (PDOException $e) {
		print "Error!: " . $e->getMessage() . "<br/>";
		die();
	}

	return $r[0];
}

// return the total traffic in the archive
function get_bytes() {
	global $dbh;
	try {
		$q = $dbh->query("SELECT SUM(Bytes) FROM Flows;");
		$r = $q->fetch();
	} catch (PDOException $e) {
		print "Error!: " . $e->getMessage() . "<br/>";
		die();
	}

	return $r[0];
}

// return the total traffic in the archive
function get_pkts() {
	global $dbh;
	try {
		$q = $dbh->query("SELECT SUM(Pakets) FROM Flows;");
		$r = $q->fetch();
	} catch (PDOException $e) {
		print "Error!: " . $e->getMessage() . "<br/>";
		die();
	}

	return $r[0];
}

// return the database size
function get_db_size() {
	global $dbh;
	try {
		$size = 0;
		foreach($dbh->query("SHOW TABLE STATUS") as $row) {
			$size += $row['Data_length'] + $row['Index_length'];
		}
	} catch (PDOException $e) {
		print "Error!: " . $e->getMessage() . "<br/>";
		die();
	}
	return $size;
}

// return existing FlowProbes
function get_probes() {
	global $dbh;
	$probes = array();
	try {
		foreach($dbh->query("SELECT FlowSrc, Hostname FROM FlowSources") as $row) {
			// No hostname found in DB, retrieve it from the DNS
			$ip = long2ip($row['FlowSrc']);
			if (empty($row['Hostname'])) {
				$row['Hostname'] = gethostbyaddr($ip);
			}
			$probes[$ip] = $row['Hostname'];
		}
	} catch (PDOException $e) {
		print "Error!: " . $e->getMessage() . "<br/>";
		die();
	}
	return $probes;
}

// check if a probe definition is valid
// expects an ip address
function isvalid_probe($ip) {
	global $dbh;
	$probes = array();
	try {
		$q = $dbh->prepare("SELECT FlowSrc FROM FlowSources WHERE FlowSrc = ?;");
		$q->bindParam(1, sprintf("%u", ip2long($ip)));
		$q->execute();
		$r = $q->fetch();
	} catch (PDOException $e) {
		print "Error!: " . $e->getMessage() . "<br/>";
		die();
	}
	if ($ip == long2ip($r[0])) {
		return True;
	}
	return False;
}

//$dbh = '';
//init_db();
