#! /usr/bin/perl -w
################################################################################
# Copyright 2005-2011 MERETHIS
# Centreon is developped by : Julien Mathis and Romain Le Merlus under
# GPL Licence 2.0.
# 
# 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.
# 
# 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, see <http://www.gnu.org/licenses>.
# 
# Linking this program statically or dynamically with other modules is making a 
# combined work based on this program. Thus, the terms and conditions of the GNU 
# General Public License cover the whole combination.
# 
# As a special exception, the copyright holders of this program give MERETHIS 
# permission to link this program with independent modules to produce an executable, 
# regardless of the license terms of these independent modules, and to copy and 
# distribute the resulting executable under terms of MERETHIS choice, provided that 
# MERETHIS also meet, for each linked independent module, the terms  and conditions 
# of the license of that module. An independent module is a module which is not 
# derived from this program. If you modify this program, you may extend this 
# exception to your version of the program, but you are not obliged to do so. If you
# do not wish to do so, delete this exception statement from your version.
# 
# For more information : contact@centreon.com
# 
# SVN : $URL$
# SVN : $Id$
#
####################################################################################
#
# Script init
#
# This is a purge script for people who use centreon in large environment. Sometimes 
# purge managed with centstorage is not possible due to laency on MySQL Server.
# This purge stop daemon ans after finishing purge restart deamon. 
#

use strict;
use warnings;
use DBI;
use POSIX ":sys_wait_h";

my $installedPath = "/DBA/centreon/2.4.5/";

my $LOG = "/DBA/centreon/2.4.5/log/centreon-purge.log";
my $PID = "/DBA/centreon/2.4.5/var/run/centstorage.pid";

# Init Globals
use vars qw($debug $LOG %status $generalcounter);
use vars qw($mysql_user $mysql_passwd $mysql_host $mysql_database_oreon $mysql_database_ods $mysql_database_ndo);
use vars qw($con_oreon $con_ods);

$debug = 0;

# Include Configuration Data
require "/DBA/centreon/2.4.5/etc/conf.pm";

sub writeLogFile($){
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time());
    open (LOG, ">> ".$LOG) || print "can't write $LOG: $!";
   
   	# Add initial 0 if value is under 10
    $hour = "0".$hour if ($hour < 10);
    $min = "0".$min if ($min < 10);
    $sec = "0".$sec if ($sec < 10);
   	
    print LOG "$mday/".($mon+1)."/".($year+1900)." $hour:$min:$sec - ".$_[0]."\n";
    close LOG or warn $!;
}

# checking if pid file exists.
if (-x $PID){
	writeLogFile("centreonPurge already runnig. can't launch again....\n");
	exit(2);
}

# Require Libs
require $installedPath."lib/misc.pm";
require $installedPath."lib/purge.pm";

# 
# Functions
#

sub CheckMySQLConnexion(){
    while ((!defined($con_oreon) || !$con_oreon->ping) && (!defined($con_ods) || !$con_ods->ping)){
	if (!defined($con_oreon) || !$con_oreon->ping) {
	    $con_oreon = DBI->connect("DBI:mysql:database=".$mysql_database_oreon.";host=".$mysql_host, $mysql_user, $mysql_passwd, {'RaiseError' => 0, 'PrintError' => 0, 'AutoCommit' => 1});
	    if (!defined($con_oreon)) {
		writeLogFile("Error when connecting to database : " . $DBI::errstr . "\n");
		sleep(2);
	    }
	} else {
	    sleep(2);
	    undef($con_oreon);
	    $con_oreon = DBI->connect("DBI:mysql:database=".$mysql_database_oreon.";host=".$mysql_host, $mysql_user, $mysql_passwd, {'RaiseError' => 0, 'PrintError' => 0, 'AutoCommit' => 1});
	}
	if (!defined($con_ods) || !$con_ods->ping) {
	    $con_ods = DBI->connect("DBI:mysql:database=".$mysql_database_ods.";host=".$mysql_host, $mysql_user, $mysql_passwd, {'RaiseError' => 0, 'PrintError' => 0, 'AutoCommit' => 1});
	    if (!defined($con_ods)) {
		writeLogFile("Error when connecting to database : " . $DBI::errstr . "\n");
		sleep(2);
	    }
	} else {
	    sleep(2);
	    undef($con_ods);
	    $con_ods = DBI->connect("DBI:mysql:database=".$mysql_database_ods.";host=".$mysql_host, $mysql_user, $mysql_passwd, {'RaiseError' => 0, 'PrintError' => 0, 'AutoCommit' => 1});	
	}
    }
}

sub CreateConnexionForCentstorage(){
    my $strgcnx;
    while (!defined($strgcnx) || !$strgcnx->ping){
	if (!defined($strgcnx)) {
	    $strgcnx = DBI->connect("DBI:mysql:database=".$mysql_database_ods.";host=".$mysql_host, $mysql_user, $mysql_passwd, {'RaiseError' => 0, 'PrintError' => 0, 'AutoCommit' => 1});
	    if (!defined($strgcnx)) {
		writeLogFile("Error when connecting to database : ".$DBI::errstr."\n");
		sleep(5);
	    }
	}
    }
    return $strgcnx;
}


########################################
# Purge MySQL data
########################################

sub purgeMysqlData(){
    my $con_ods = CreateConnexionForCentstorage();
    my $sth2 = $con_ods->prepare("SELECT len_storage_mysql FROM config");
    writeLogFile("Error when getting len_storage_mysql properties : ".$sth2->errstr."\n")if (!$sth2->execute());
    my $data = $sth2->fetchrow_hashref();
    if (defined($data->{'len_storage_mysql'}) && $data->{'len_storage_mysql'} ne 0) {
		my $delete_limit = time() - 60 * 60 * 24 * $data->{'len_storage_mysql'};
		if ($debug) {
			writeLogFile("Debug : DELETE FROM data_bin WHERE ctime < '".$delete_limit."'");
		}
		$sth2 = $con_ods->prepare("DELETE FROM data_bin WHERE ctime < '".$delete_limit."'");
		writeLogFile("Error when purging Mysql data  : ".$sth2->errstr."\n")if (!$sth2->execute());
    }
    $sth2->finish();
    undef($sth2);
    $con_ods->disconnect();
}

sub CreateConnexionForOreon(){
    my $oreoncnx;
    while (!defined($oreoncnx) || !$oreoncnx->ping){
		if (!defined($oreoncnx)) {
		    $oreoncnx = DBI->connect("DBI:mysql:database=".$mysql_database_oreon.";host=".$mysql_host, $mysql_user, $mysql_passwd, {'RaiseError' => 0, 'PrintError' => 0, 'AutoCommit' => 1});
		    if (!defined($oreoncnx)) {
				writeLogFile("Error when connecting to database : ".$DBI::errstr."\n");
				sleep(5);
		    }
		}
    }
    return $oreoncnx;
}

# Get if purge is activ

sub getPurgeConfig($){
    
    # Get connexion
    my $con = $_[0];
    
    my $sth = $con->prepare("SELECT autodelete_rrd_db FROM config");
    if (!$sth->execute) {
		writeLogFile("Error - getPurgeConfig :" . $sth->errstr . "\n");
    }
    my $data = $sth->fetchrow_hashref();
    undef($sth);
    return $data->{'autodelete_rrd_db'};
}

# Get repository of RRDTool db

sub getStorageDir($){
    
    # Get connexion
    my $con = $_[0];
    
    my $sth = $con->prepare("SELECT RRDdatabase_path FROM config");
    if (!$sth->execute) {
		writeLogFile("Error - getStorageDir : " . $sth->errstr . "\n");
    }
    my $data = $sth->fetchrow_hashref();
    undef($sth);
    return $data->{'RRDdatabase_path'};
}

#
# main Script steps
#

# data Bin purge
writeLogFile("Begin centstorage.data_bin purge");

# Purge All bin data
purgeMysqlData();

writeLogFile("Finishing centstorage.data_bin purge");

