Private GIT

Skip to content
Snippets Groups Projects
Commit 9d69baf3 authored by Kevin Duret's avatar Kevin Duret
Browse files

add cron for centstorage purge

parent a65c55dd
No related branches found
No related tags found
No related merge requests found
......@@ -45,12 +45,12 @@ $debugLevel = 0;
require_once "DB.php";
require_once __DIR__."../config/centreon.config.php";
require_once __DIR__."/../config/centreon.config.php";
require_once __DIR__."../www/class/centreon-partition/options.class.php";
require_once __DIR__."../www/class/centreon-partition/partEngine.class.php";
require_once __DIR__."../www/class/centreon-partition/mysqlTable.class.php";
require_once __DIR__."../www/class/centreon-partition/config.class.php";
require_once __DIR__."/../www/class/centreon-partition/options.class.php";
require_once __DIR__."/../www/class/centreon-partition/partEngine.class.php";
require_once __DIR__."/../www/class/centreon-partition/mysqlTable.class.php";
require_once __DIR__."/../www/class/centreon-partition/config.class.php";
$options = new Options();
if ($options->isMissingOptions()) {
......
#!/usr/bin/env perl
################################################################################
# Copyright 2005-2015 Centreon
# 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 Centreon
# 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 Centreon choice, provided that
# Centreon 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.
#
####################################################################################
use warnings;
use centreon::script::centstorage_purge;
centreon::script::centstorage_purge->new()->run();
__END__
=head1 NAME
centstorage_purge - a script to keep the centstorage database clean.
=head1 SYNOPSIS
logAnalyser [options]
=head1 OPTIONS
=over 8
=item B<--config>
Specify the path to the main configuration file (default: /etc/centreon/conf.pm).
=item B<--logfile>
Write log messages into this file instead of the standard output.
=item B<--help>
Print a brief help message and exits.
=back
=head1 DESCRIPTION
B<centstorage_purge> will delete records from the database (databin,
log and ?? tables) depending on the retention policy defined in
Centreon.
=cut
<?php
/*
* Copyright 2005-2016 Centreon
* 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 Centreon
* 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 Centreon choice, provided that
* Centreon 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
*
*/
require_once(realpath(dirname(__FILE__) . "/../www/class/centreonPurgeEngine.class.php"));
echo "[" . date(DATE_RFC822) . "] PURGE STARTED\n";
try {
$engine = new CentreonPurgeEngine();
$engine->purge();
} catch (\Exception $e) {
echo "[" . date(DATE_RFC822) . "] " . $e->getMessage();
exit(1);
}
echo "[" . date(DATE_RFC822) . "] PURGE COMPLETED\n";
exit(0);
################################################################################
# Copyright 2005-2013 Centreon
# 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 Centreon
# 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 Centreon choice, provided that
# Centreon 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.
#
#
####################################################################################
package centreon::script::centstorage_purge;
use strict;
use warnings;
use centreon::script;
use centreon::common::lock;
use base qw(centreon::script);
sub new {
my $class = shift;
my $self = $class->SUPER::new("centstorage_purge",
centreon_db_conn => 1,
centstorage_db_conn => 1
);
bless $self, $class;
return $self;
}
sub read_config {
my $self = shift;
my ($status, $sth) = $self->{csdb}->query(<<"EOQ");
SELECT len_storage_mysql,archive_retention,reporting_retention, len_storage_downtimes, len_storage_comments
FROM config
EOQ
die "Failed to retrieve configuration from database" if $status == -1;
$self->{config} = $sth->fetchrow_hashref();
}
sub run {
my $self = shift;
$self->SUPER::run();
$self->read_config();
if (defined $self->{config}->{len_storage_mysql} && $self->{config}->{len_storage_mysql} != 0) {
my $delete_limit = time() - 60 * 60 * 24 * $self->{config}->{len_storage_mysql};
$self->{logger}->writeLogInfo("Purging centstorage.data_bin table...");
$self->{csdb}->do("DELETE FROM data_bin WHERE ctime < $delete_limit");
$self->{logger}->writeLogInfo("Done");
}
if (defined($self->{config}->{archive_retention}) && $self->{config}->{archive_retention} != 0) {
my $last_log = time() - ($self->{config}->{archive_retention} * 24 * 60 * 60);
$self->{logger}->writeLogInfo("Purging centstorage.logs table...");
$self->{csdb}->do("DELETE FROM `logs` WHERE `ctime` < $last_log");
if ($@) {
$self->{logger}->writeLogError("Failed: $@");
} else {
$self->{logger}->writeLogInfo("Done");
}
}
if (defined($self->{config}->{reporting_retention}) && $self->{config}->{reporting_retention} != 0) {
my $last_log = time() - ($self->{config}->{reporting_retention} * 24 * 60 * 60);
$self->{logger}->writeLogInfo("Purging log archive tables...");
$self->{csdb}->do("DELETE FROM `log_archive_host` WHERE `date_end` < $last_log");
$self->{csdb}->do("DELETE FROM `log_archive_service` WHERE `date_end` < $last_log");
$self->{logger}->writeLogInfo("Done");
}
if (defined $self->{config}->{len_storage_downtimes} && $self->{config}->{len_storage_downtimes} != 0) {
my $delete_limit = time() - 60 * 60 * 24 * $self->{config}->{len_storage_downtimes};
$self->{logger}->writeLogInfo("Purging centstorage.downtimes table...");
$self->{csdb}->do("DELETE FROM downtimes WHERE (actual_end_time is not null and actual_end_time < $delete_limit) OR (deletion_time is not null and deletion_time < $delete_limit)");
$self->{logger}->writeLogInfo("Done");
}
if (defined $self->{config}->{len_storage_comments} && $self->{config}->{len_storage_comments} != 0) {
my $delete_limit = time() - 60 * 60 * 24 * $self->{config}->{len_storage_comments};
$self->{logger}->writeLogInfo("Purging centstorage.comments table...");
$self->{csdb}->do("DELETE FROM comments WHERE (deletion_time is not null and deletion_time < $delete_limit) OR (expire_time < $delete_limit AND expire_time <> 0)");
$self->{logger}->writeLogInfo("Done");
}
# Put to_delete in index_data
$self->{logger}->writeLogInfo("Purging centstorage.index_data table...");
$self->{csdb}->do("UPDATE index_data SET to_delete = '1' WHERE
ISNULL((SELECT 1 FROM " . $self->{centreon_config}->{centreon_db} . ".hostgroup_relation hr, " . $self->{centreon_config}->{centreon_db} . ".host_service_relation hsr WHERE hr.host_host_id = index_data.host_id AND hr.hostgroup_hg_id = hsr.hostgroup_hg_id AND hsr.service_service_id = index_data.service_id LIMIT 1)) AND
ISNULL((SELECT 1 FROM " . $self->{centreon_config}->{centreon_db} . ".host_service_relation hsr WHERE hsr.host_host_id = index_data.host_id AND hsr.service_service_id = index_data.service_id LIMIT 1))
");
$self->{logger}->writeLogInfo("Done");
}
1;
......@@ -124,7 +124,7 @@ class CentreonPurgeEngine
$DBRESULT = $this->dbCentstorage->query($query);
if (PEAR::isError($DBRESULT)) {
throw new Exception('Cannot get retention information');
throw new Exception('Cannot get partition information');
}
while ($row = $DBRESULT->fetchRow()) {
......@@ -138,15 +138,19 @@ class CentreonPurgeEngine
{
foreach ($this->tablesToPurge as $table => $parameters) {
if ($parameters['retention'] > 0) {
echo "[" . date(DATE_RFC822) . "] Purging table " . $table . "...\n";
if ($parameters['is_partitioned']) {
$this->purgeParts($table);
} else {
$this->purgeOldData($table);
}
echo "[" . date(DATE_RFC822) . "] Table " . $table . " purged\n";
}
}
echo "[" . date(DATE_RFC822) . "] Purging index_data...\n";
$this->purgeIndexData();
echo "[" . date(DATE_RFC822) . "] index_data purged\n";
}
/**
......@@ -175,6 +179,7 @@ class CentreonPurgeEngine
throw new Exception("Error : Cannot drop partition " . $row["PARTITION_NAME"] . " of table "
. $table . ", " . $DBRESULT2->getDebugInfo() . "\n");
}
echo "[" . date(DATE_RFC822) . "] Partition " . $row["PARTITION_NAME"] . " deleted\n";
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment