Delete old data from tplanned_downtime.

This commit is contained in:
Ramon Novoa 2022-09-08 10:14:44 +02:00
parent 3524cdac6c
commit 6d153dee54
1 changed files with 39 additions and 9 deletions

View File

@ -40,6 +40,9 @@ my $version = "7.0NG.764 Build 220907";
# Pandora server configuration
my %conf;
# History DB configuration
my $h_conf;
# Long operations are divided in XX steps for performance
my $BIG_OPERATION_STEP = 100; # 100 is default
@ -73,8 +76,8 @@ sub log_message ($$;$) {
########################################################################
# Delete old data from the database.
########################################################################
sub pandora_purgedb ($$) {
my ($conf, $dbh) = @_;
sub pandora_purgedb ($$$) {
my ($conf, $dbh, $h_conf) = @_;
# 1) Obtain last value for date limit
# 2) Delete all elements below date limit
@ -141,13 +144,13 @@ sub pandora_purgedb ($$) {
# Delete sessions data
pandora_delete_old_session_data (\%conf, $dbh, $ulimit_timestamp);
# Delete old inventory data
}
else {
log_message ('PURGE', 'days_purge is set to 0. Old data will not be deleted.');
}
pandora_delete_old_tplanned_downtime(\%conf, $dbh, $h_conf);
# String data deletion
if (!defined($conf->{'_string_purge'})){
$conf->{'_string_purge'} = 7;
@ -1022,6 +1025,33 @@ sub pandora_delete_old_export_data {
};
}
##############################################################################
# Delete old data from tplanned_downtime.
##############################################################################
sub pandora_delete_old_tplanned_downtime {
my ($conf, $dbh, $h_conf) = @_;
# Use the configuration from the history DB if available, which should be
# less restrictive.
my $days_purge = $conf->{'_days_purge'};
if (defined($h_conf) &&
defined($h_conf->{'_days_purge'}) &&
$h_conf->{'_days_purge'} > 0) {
$days_purge = $h_conf->{'_days_purge'};
}
# _days_purge was not configured.
return unless $days_purge > 0;
my $ulimit_timestamp = time() - (86400 * $days_purge);
log_message ('PURGE', "Deleting data older than $days_purge days from tplanned_downtime.");
db_do($dbh, "DELETE FROM tplanned_downtime
WHERE type_execution = 'once'
AND date_to < ?", $ulimit_timestamp);
}
##############################################################################
# Delete old session data.
##############################################################################
@ -1112,13 +1142,13 @@ sub pandoradb_history ($$) {
###############################################################################
# Main
###############################################################################
sub pandoradb_main ($$$;$) {
my ($conf, $dbh, $history_dbh) = @_;
sub pandoradb_main {
my ($conf, $dbh, $h_conf, $history_dbh) = @_;
log_message ('', "Starting at ". strftime ("%Y-%m-%d %H:%M:%S", localtime()) . "\n");
# Purge
pandora_purgedb ($conf, $dbh);
pandora_purgedb ($conf, $dbh, $h_conf);
# Consistency check
pandora_checkdb_consistency ($conf, $dbh);
@ -1221,6 +1251,7 @@ if (defined($conf{'_history_db_enabled'}) && $conf{'_history_db_enabled'} eq '1'
eval {
$conf{'encryption_key'} = enterprise_hook('pandora_get_encryption_key', [\%conf, $conf{'encryption_passphrase'}]);
$history_dbh = db_connect ($conf{'dbengine'}, $conf{'_history_db_name'}, $conf{'_history_db_host'}, $conf{'_history_db_port'}, $conf{'_history_db_user'}, pandora_output_password(\%conf, $conf{'_history_db_pass'}));
$h_conf = pandoradb_load_history_conf($history_dbh);
};
if ($@) {
if (is_offline(\%conf)) {
@ -1269,12 +1300,11 @@ if ($lock == 0) {
}
# Main
pandoradb_main(\%conf, $dbh, $history_dbh);
pandoradb_main(\%conf, $dbh, $h_conf, $history_dbh);
# history_dbh is unset in pandoradb_main if not in use.
if (defined($history_dbh)) {
log_message('', " [>] DB Tool running on historical database.\n");
my $h_conf = pandoradb_load_history_conf($history_dbh);
# Keep base settings.
$h_conf->{'_onlypurge'} = $conf{'_onlypurge'};