<?php
/*
Fruity - A Nagios Configuration Tool
Copyright (C) 2005 Groundwork Open Source Solutions

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 2
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

// MySQL Database Connection Configuration
$sitedb_config['host'] = 'localhost';				// Address to the database server
$sitedb_config['username'] = 'root';				// Username to access the database
$sitedb_config['password'] = '';				// Password to access the database
$sitedb_config['database'] = 'fruity';				// The database name
$sitedb_config['dbserv']	= 'mysql';			// The database driver (for now, use mysql)


include_once('includes/adodb/adodb.inc.php');
include_once('output/output.php');
include_once('includes/output.inc');



print_header("Fruity Database Upgrade");

function dbQuery($query) {
	global $dbConnection;
	$result = $dbConnection->Execute($query);
	if($dbConnection->ErrorNo()) {
		print("ERROR: " . $dbConnection->ErrorMsg() . "<br />");
		print("Attempting to run this Query: " . $query . "<br />");
		die();
	}
}

if(!isset($_GET['commit'])) {
	?>
	<blockquote>
	This script will attempt to upgrade your Fruity 1.0 rc1 database to the new schema used in Fruity 1.0 rc2.  This 
	script will only work on databases used in Fruity 1.0 rc1.<br />
	<br />
	<b>WARNING:</b><br />
	This script is not guaranteed.  You should backup your Fruity database before performing this upgrade.
	</blockquote>
	<div align="center">
	<a href="<?=$_SERVER['PHP_SELF'];?>?commit=true">Continue With Upgrade</a>
	</div>
	<?php
}
else {
	// Continue with upgrade
	?>
	<blockquote>
	Creating Database Connection...
	<?php
	$dbServ = $sitedb_config['dbserv'];
	$dbHost = $sitedb_config['host'];
	$dbUsername = $sitedb_config['username'];
	$dbPassword = $sitedb_config['password'];
	$dbDatabase = $sitedb_config['database'];	
	// Setup internal variables
	// Connect database
	$dbConnection = ADONewConnection($dbServ);
	// Do an initial connection
	$dbConnection->PConnect($dbHost, $dbUsername,
						$dbPassword,$dbDatabase);
	if(!$dbConnection->IsConnected()) {
		print("Unable to connect to database.  Please check your configuration.");
		die();
	}
	$dbConnection->SetFetchMode(ADODB_FETCH_ASSOC);
	print("DONE<br />");
	print("Adding Nagios Broker Modules Table...");
	dbQuery("CREATE TABLE `nagios_broker_modules` (
  `module_id` int(11) NOT NULL auto_increment,
  `module_line` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`module_id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;");
	print("DONE<br />");
	print("Setting various interval lengths...");
	dbQuery("ALTER TABLE `nagios_escalations` CHANGE `notification_interval` `notification_interval` INT( 8 ) NULL DEFAULT NULL");
	dbQuery("ALTER TABLE `nagios_host_templates` CHANGE `check_interval` `check_interval` INT( 8 ) NULL DEFAULT NULL");
	dbQuery("ALTER TABLE `nagios_host_templates` CHANGE `freshness_threshold` `freshness_threshold` INT( 8 ) NULL DEFAULT NULL");
	dbQuery("ALTER TABLE `nagios_host_templates` CHANGE `notification_interval` `notification_interval` INT( 8 ) NULL DEFAULT NULL");
	dbQuery("ALTER TABLE `nagios_hosts` CHANGE `check_interval` `check_interval` INT( 8 ) NULL DEFAULT NULL");
	dbQuery("ALTER TABLE `nagios_hosts` CHANGE `freshness_threshold` `freshness_threshold` INT( 8 ) NULL DEFAULT NULL");
	dbQuery("ALTER TABLE `nagios_hosts` CHANGE `notification_interval` `notification_interval` INT( 8 ) NULL DEFAULT NULL");
	dbQuery("ALTER TABLE `nagios_main` CHANGE `service_check_timeout` `service_check_timeout` INT( 8 ) NULL DEFAULT NULL");
	dbQuery("ALTER TABLE `nagios_main` CHANGE `host_check_timeout` `host_check_timeout` INT( 8 ) NULL DEFAULT NULL");
	dbQuery("ALTER TABLE `nagios_main` CHANGE `event_handler_timeout` `event_handler_timeout` INT( 8 ) NULL DEFAULT NULL");
	dbQuery("ALTER TABLE `nagios_main` CHANGE `notification_timeout` `notification_timeout` INT( 8 ) NULL DEFAULT NULL");
	dbQuery("ALTER TABLE `nagios_main` CHANGE `ocsp_timeout` `ocsp_timeout` INT( 8 ) NULL DEFAULT NULL");
	dbQuery("ALTER TABLE `nagios_main` CHANGE `auto_rescheduling_interval` `auto_rescheduling_interval` INT( 8 ) NULL DEFAULT NULL");
	dbQuery("ALTER TABLE `nagios_main` CHANGE `auto_rescheduling_window` `auto_rescheduling_window` INT( 8 ) NULL DEFAULT NULL");
	dbQuery("ALTER TABLE `nagios_main` CHANGE `ochp_timeout` `ochp_timeout` INT( 8 ) NULL DEFAULT NULL");
	dbQuery("ALTER TABLE `nagios_main` CHANGE `host_freshness_check_interval` `host_freshness_check_interval` INT( 8 ) NULL DEFAULT NULL");
	dbQuery("ALTER TABLE `nagios_main` CHANGE `service_freshness_check_interval` `service_freshness_check_interval` INT( 8 ) NULL DEFAULT NULL");
	dbQuery("ALTER TABLE `nagios_service_templates` CHANGE `normal_check_interval` `normal_check_interval` INT( 8 ) NULL DEFAULT NULL");
	dbQuery("ALTER TABLE `nagios_service_templates` CHANGE `freshness_threshold` `freshness_threshold` INT( 8 ) NULL DEFAULT NULL");
	dbQuery("ALTER TABLE `nagios_service_templates` CHANGE `notification_interval` `notification_interval` INT( 8 ) NULL DEFAULT NULL");
	dbQuery("ALTER TABLE `nagios_services` CHANGE `normal_check_interval` `normal_check_interval` INT( 8 ) NULL DEFAULT NULL");
	dbQuery("ALTER TABLE `nagios_services` CHANGE `freshness_threshold` `freshness_threshold` INT( 8 ) NULL DEFAULT NULL");
	dbQuery("ALTER TABLE `nagios_services` CHANGE `notification_interval` `notification_interval` INT( 8 ) NULL DEFAULT NULL");
	print("DONE<br />");
	print("Adding support for perfdata parameters...");
	dbQuery("ALTER TABLE `nagios_main` CHANGE `xpdfile_host_perfdata_file` `host_perfdata_file` varchar( 255 ) NULL DEFAULT NULL");	
	dbQuery("ALTER TABLE `nagios_main` CHANGE `xpdfile_host_perfdata_template` `host_perfdata_template` varchar( 255 ) NULL DEFAULT NULL");	
	dbQuery("ALTER TABLE `nagios_main` CHANGE `xpdfile_service_perfdata_template` `service_perfdata_template` varchar( 255 ) NULL DEFAULT NULL");
	dbQuery("ALTER TABLE `nagios_hosts` ADD `host_perfdata_file_mode` ENUM( 'a', 'w' ) DEFAULT 'a' ;");
	dbQuery("ALTER TABLE `nagios_hosts` ADD `service_perfdata_file_mode` ENUM( 'a', 'w' ) DEFAULT 'a' ;");
	dbQuery("ALTER TABLE `nagios_hosts` ADD `host_perfdata_file_processing_command` int( 11 ) NULL DEFAULT NULL;");
	dbQuery("ALTER TABLE `nagios_hosts` ADD `service_perfdata_file_processing_command` int( 11 ) NULL DEFAULT NULL;");
	dbQuery("ALTER TABLE `nagios_hosts` ADD `host_perfdata_file_processing_interval` int( 5 ) NULL DEFAULT NULL;");
	dbQuery("ALTER TABLE `nagios_hosts` ADD `service_perfdata_file_processing_interval` int( 5 ) NULL DEFAULT NULL;");
	dbQuery("UPDATE nagios_main_desc SET field_name = 'host_perfdata_file' WHERE field_name = 'xpdfile_host_perfdata_file'");
	dbQuery("UPDATE nagios_main_desc SET field_name = 'host_perfdata_template' WHERE field_name = 'xpdfile_host_perfdata_template'");
	dbQuery("UPDATE nagios_main_desc SET field_name = 'service_perfdata_template' WHERE field_name = 'xpdfile_service_perfdata_template'");
	dbQuery("INSERT INTO `nagios_main_desc` VALUES ('service_perfdata_template', 'This directive determines how the service performance data is written to the file. The example template definition show below will cause the performance data to be written to the file in tab-delimited format, with one entry per line (\\t, \\r and \\n are interpreted as tab, carriage return, and newline characters respectively).<br />\r\nxpdfile_service_perfdata_template=$TIMET$\\t$HOSTNAME$\\t$SERVICEDESC$\\t$OUTPUT$\\t$PERFDATA$ ');");
	dbQuery("INSERT INTO `nagios_main_desc` VALUES ('event_broker_options', 'Controls what (if any) data gets sent to the event broker.');");
	print("DONE<br />");
	print("Adding Failure Prediction fields to host/service templates and objects...");
	dbQuery("ALTER TABLE `nagios_host_templates` ADD `failure_prediction_enabled` ENUM( '0', '1' ) NULL ;");
	dbQuery("ALTER TABLE `nagios_hosts` ADD `failure_prediction_enabled` ENUM( '0', '1' ) NULL ;");
	dbQuery("ALTER TABLE `nagios_service_templates` ADD `failure_prediction_enabled` ENUM( '0', '1' ) NULL ;");
	dbQuery("ALTER TABLE `nagios_services` ADD `failure_prediction_enabled` ENUM( '0', '1' ) NULL ;");
	print("DONE<br />");
	print("Changing ocsp_command field in main config...");
	dbQuery("ALTER TABLE `nagios_main` CHANGE `ocsp_command` `ocsp_command` int( 11 ) NULL DEFAULT NULL");
	print("DONE<br />");
	print("Adding Nagios Host Parents table...");
	dbQuery("CREATE TABLE `nagios_host_parents` (
  `child_id` int(11) NOT NULL,
  `parent_id` int(11) NOT NULL
) TYPE=MyISAM;");
	print("DONE<br />");
	?>
	<br />
	<br />
	The Upgrade is now complete.  You can now use Fruity.
	</blockquote>
	<?php
}

print_footer();


?>