2012-02-16 12:19:40 +00:00
< ? php
2014-01-13 17:43:58 +00:00
// MYSQL Check - FIXME
// 1 UNKNOWN
2015-07-13 20:10:26 +02:00
/*
* Observium Network Management and Monitoring System
2012-02-16 12:19:40 +00:00
* Copyright ( C ) 2006 - 2012 , Observium Developers - http :// www . observium . org
*
* 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 .
*
* See COPYING for more details .
*/
2015-07-13 20:10:26 +02:00
if ( ! isset ( $debug )) {
// Not called from within discovery, let's load up the necessary stuff.
include 'includes/defaults.inc.php' ;
include 'config.php' ;
include 'includes/definitions.inc.php' ;
include 'includes/functions.php' ;
$options = getopt ( 'd' );
if ( isset ( $options [ 'd' ])) {
$debug = true ;
}
else {
$debug = false ;
}
2012-02-16 23:18:49 +00:00
}
2015-07-22 07:52:58 -04:00
if ( ! dbGetLock ( 'schema_update' )) {
echo " Schema update already in progress. Exiting " ;
exit ( 1 );
2015-07-22 08:09:19 -04:00
} //end if
do {
sleep ( 1 );
} while ( @ dbFetchCell ( 'SELECT COUNT(*) FROM `devices` WHERE NOT IS_FREE_LOCK(CONCAT("polling.", device_id)) OR NOT IS_FREE_LOCK(CONCAT("queued.", device_id)) OR NOT IS_FREE_LOCK(CONCAT("discovering.", device_id))' ) > 0 );
2015-07-22 07:52:58 -04:00
2012-02-19 18:41:53 +00:00
$insert = 0 ;
2015-07-13 20:10:26 +02:00
if ( $db_rev = @ dbFetchCell ( 'SELECT version FROM `dbSchema` ORDER BY version DESC LIMIT 1' )) {
}
else {
$db_rev = 0 ;
$insert = 1 ;
2012-02-16 12:19:40 +00:00
}
2015-07-13 20:10:26 +02:00
// For transition from old system
if ( $old_rev = @ dbFetchCell ( 'SELECT revision FROM `dbSchema`' )) {
echo " -- Transitioning from old revision-based schema to database version system \n " ;
$db_rev = 6 ;
2012-02-16 12:19:40 +00:00
2015-07-13 20:10:26 +02:00
if ( $old_rev <= 1000 ) {
$db_rev = 1 ;
}
2012-02-16 12:19:40 +00:00
2015-07-13 20:10:26 +02:00
if ( $old_rev <= 1435 ) {
$db_rev = 2 ;
}
if ( $old_rev <= 2245 ) {
$db_rev = 3 ;
}
if ( $old_rev <= 2804 ) {
$db_rev = 4 ;
}
if ( $old_rev <= 2827 ) {
$db_rev = 5 ;
}
$insert = 1 ;
} //end if
2012-02-16 12:19:40 +00:00
$updating = 0 ;
2015-07-13 20:10:26 +02:00
$include_dir_regexp = '/\.sql$/' ;
2012-02-16 12:19:40 +00:00
2015-07-13 20:10:26 +02:00
if ( $handle = opendir ( $config [ 'install_dir' ] . '/sql-schema' )) {
while ( false !== ( $file = readdir ( $handle ))) {
if ( filetype ( $config [ 'install_dir' ] . '/sql-schema/' . $file ) == 'file' && preg_match ( $include_dir_regexp , $file )) {
$filelist [] = $file ;
}
2012-02-16 12:19:40 +00:00
}
2015-07-13 20:10:26 +02:00
closedir ( $handle );
2012-02-16 12:19:40 +00:00
}
asort ( $filelist );
2015-07-13 20:10:26 +02:00
foreach ( $filelist as $file ) {
list ( $filename , $extension ) = explode ( '.' , $file , 2 );
if ( $filename > $db_rev ) {
if ( ! $updating ) {
echo " -- Updating database schema \n " ;
}
echo sprintf ( '%03d' , $db_rev ) . ' -> ' . sprintf ( '%03d' , $filename ) . ' ...' ;
$err = 0 ;
if ( $fd = @ fopen ( $config [ 'install_dir' ] . '/sql-schema/' . $file , 'r' )) {
$data = fread ( $fd , 4096 );
while ( ! feof ( $fd )) {
$data .= fread ( $fd , 4096 );
}
foreach ( explode ( " \n " , $data ) as $line ) {
if ( trim ( $line )) {
2015-08-20 16:06:44 +02:00
d_echo ( " $line \n " );
2015-07-13 20:10:26 +02:00
if ( $line [ 0 ] != '#' ) {
2015-08-08 19:27:18 +00:00
if ( $config [ 'db' ][ 'extension' ] == 'mysqli' ) {
2015-08-12 19:55:37 +00:00
$update = mysqli_query ( $database_link , $line );
2015-08-08 19:27:18 +00:00
}
else {
2015-08-12 19:55:37 +00:00
$update = mysql_query ( $line );
2015-08-08 19:27:18 +00:00
}
2015-07-13 20:10:26 +02:00
if ( ! $update ) {
$err ++ ;
if ( $debug ) {
2015-08-08 19:27:18 +00:00
if ( $config [ 'db' ][ 'extension' ] == 'mysqli' ) {
echo mysqli_error ( $database_link ) . " \n " ;
}
else {
echo mysql_error () . " \n " ;
}
2015-07-13 20:10:26 +02:00
}
}
}
}
}
2012-02-16 12:19:40 +00:00
2015-07-13 20:10:26 +02:00
if ( $db_rev < 5 ) {
echo " done. \n " ;
}
else {
echo " done ( $err errors). \n " ;
2012-02-19 18:41:53 +00:00
}
2012-02-16 12:19:40 +00:00
}
2015-07-13 20:10:26 +02:00
else {
echo " Could not open file! \n " ;
} //end if
$updating ++ ;
$db_rev = $filename ;
} //end if
} //end foreach
if ( $updating ) {
if ( $insert ) {
dbInsert ( array ( 'version' => $db_rev ), 'dbSchema' );
2012-02-16 12:19:40 +00:00
}
2015-07-13 20:10:26 +02:00
else {
dbUpdate ( array ( 'version' => $db_rev ), 'dbSchema' );
2012-02-16 12:19:40 +00:00
}
2015-07-13 20:10:26 +02:00
echo " -- Done \n " ;
2012-02-16 12:19:40 +00:00
}