#!/usr/bin/php
<?
/*
 Network Interface Statistics Collection Agent v2.5
 by Brett Baugh

 This script creates a new installation of the NISCA
 database, making sure the selected database name does
 not already exist, and inserts some bland default
 values in a few tables.
 */
?>

This should take less than two seconds. Please wait... :)

<?
$admin_pw="change";

require_once("/etc/nisca/nisca.conf");
require_once($location["db_config"]);
if (! $persistent_connection=mysql_pconnect($db_host, $db_user, $db_pass)) {
  echo "\nUh oh; MySQL doesn't seem to be running on $db_host, or $db_user can't connect.\n" .
    "Try tweaking something in \"{$location["db_config"]}\" and try again.\n\n" .
    "Death has occurred.\n\n";
  die;
}

if (! mysql_select_db($database, $persistent_connection)) {
  $sq="create database $database";
  dosql($sq);
}

if (! mysql_select_db($database, $persistent_connection)) {
  echo "\nUh oh; the \"$database\" database doesn't seem to exist yet,\n" .
    "and I couldn't create it myself. Please create it by running this command as root:\n\n" .
    "     mysqladmin create $database\n\n" .
    "and then set up user permissions on it as recommended in the documentation.\n\n" .
    "Death has occurred.\n\n";
  die;
}

function dosql($sq) {
  global $persistent_connection;
//  echo "<h3>$sq</h3>\n";
  $res=mysql_query($sq, $persistent_connection);
  if (mysql_error($persistent_connection) != "") {
    echo "<h3>SQL Query Failure!</h3>\n<I>" .
      mysql_error($persistent_connection) . ":</I><P>\n$sq<P>\n";
  }
  return $res;
}

$sq="show tables";
$res=dosql($sq);
if (mysql_num_rows($res) > 0) {
  echo "\nUmmm, sorry, but you already have an installed database named \"$database\".\n" .
    "You probably want to follow the upgrade procedure in the INSTALL file instead.\n" .
    "This script will only work when the above-named database is completely empty.\n\n";
  die;
}

$sq="create table stats(
	if_id mediumint not null,
	r_bytes bigint,
	r_packets bigint,
	r_errors bigint,
	r_dropped bigint,
	t_bytes bigint,
	t_packets bigint,
	t_errors bigint,
	t_dropped bigint,
	stamp int not null,
	primary key (stamp,if_id)
)";
$res=dosql($sq);

$sq="create table stats_ifs(
	id int not null auto_increment primary key,
	hostname varchar(64) not null,
	community varchar(64) not null,
	if_name varchar(64) not null,
	index idx1 (hostname, if_name),
	index idx2 (hostname, community, if_name)
)";
$res=dosql($sq);

$sq="create table reports(
	id int not null auto_increment primary key,
	name varchar(255),
	host_if text,
	bytes varchar(1),
	packets varchar(1),
	errors varchar(1),
	dropped varchar(1),
	reportctl varchar(4),
        in_out varchar(4),
	timeperiod varchar(24),
	time_x smallint,
	f_month tinyint,
	f_day tinyint,
	f_year smallint,
	f_hour tinyint,
	f_min tinyint,
	f_sec tinyint,
	t_month tinyint,
	t_day tinyint,
	t_year smallint,
	t_hour tinyint,
	t_min tinyint,
	t_sec tinyint,
	x smallint,
	y smallint,
	summ_interval smallint,
	summ_units enum('second', 'minute', 'hour', 'month', 'day', 'year'),
	g_bg varchar(6),
	g_text varchar(6),
	g_lborder varchar(6),
	g_dborder varchar(6),
	g_peak varchar(6),
	g_in varchar(6),
	g_out varchar(6),
	g_fmt varchar(1),
	multiple varchar(1),
	multi_int bigint,
	g_bb varchar(3),
	do95 smallint
)";
$res=dosql($sq);

$sq="create table config(
	version varchar(10) not null primary key,
	delay int,
	have_gd enum('y', 'n'),
	ttf_font varchar(128),
	x smallint,
	y smallint,
	graph_background varchar(6),
	graph_text varchar(6),
	graph_border_light varchar(6),
	graph_border_dark varchar(6),
	graph_peak_highlight varchar(6),
	graph_incoming_data varchar(6),
	graph_outgoing_data varchar(6),
	statsfile varchar(128),
	js_list enum('y', 'n'),
	dateformat varchar(30),
	dformat varchar(15),
	tformat varchar(15),
	bitbyte varchar(1),
	mailto varchar(255),
	collect_cmd varchar(255),
	snmp_cmd varchar(255),
	ps_cmd varchar(255),
	graph_fmt varchar(1)
)";
$res=dosql($sq);

$sq="create table interfaces(
	hostname varchar(64) not null,
	if_name varchar(64) not null,
	community varchar(64) not null,
	dynamic enum('y', 'n'),
	snmpv2 enum('y', 'n'),
	alias varchar(255),
	speed bigint,
	hostalias varchar(255),
	method varchar(16),
	cir bigint,
	primary key (hostname,if_name,community)
)";
$res=dosql($sq);

$sq="create table localhost(
	hostname varchar(64) not null,
	if_name varchar(64) not null,
	alias varchar(255),
	speed bigint,
	primary key (hostname, if_name)
)";
$res=dosql($sq);

$sq="create table sessions(
	user varchar(50) not null primary key,
	session_id varchar(128)
)";
$res=dosql($sq);

$sq="create table users(
	user varchar(50) not null primary key,
	pass varchar(50)
)";
$res=dosql($sq);
echo "Database tables created.\n";

$sq="insert into users values('admin', encrypt('$admin_pw'))";
$res=dosql($sq);
echo "Admin user created.\n";

$sq="insert into config values('2.5', 300, 'y', " .
    "'{$location["install"]}/galaxy.ttf', 750, 250, '000000', 'FFFFFF', " .
    "'333333', '999999', 'FF0000', " .
    "'00FF00', 'FFFF00', '/proc/net/dev', 'y', " .
    "'m/d/Y, H:i:s', 'm/d', 'H:i', 'B', 'root@localhost', " .
    "'nohup /usr/sbin/collect > /dev/null &', " .
    "'nohup /usr/sbin/snmp_collect > /dev/null &', " .
    "'ps axw', 'p')";

$res=dosql($sq);
echo "Config table populated.\n\n";

echo "Database creation complete.\n\n";

?>
