Clean deleted agents and modules from dashboards, reports, etc

This commit is contained in:
Calvo 2023-06-23 15:06:31 +02:00
parent 40ea25eeea
commit 7a3da419bd

View File

@ -764,51 +764,55 @@ sub pandora_checkdb_integrity {
# Delete orphan data form deleted agents.
my @agents_ids = get_db_rows ($dbh, 'SELECT id_agente, alias FROM tagente');
my $agents_id = '0';
my $agents_alias;
foreach my $id (@agents_ids) {
$agents_id .= ','.$id->{'id_agente'};
$agents_alias .= ','.$id->{'alias'};
}
if(defined($agents_id) && $agents_id ne '0') {
# Delete orphan data from visual console.
log_message ('INTEGRITY', "Deleting orphan visual console items.");
db_do ($dbh, 'DELETE FROM tlayout_data WHERE id_agent NOT IN (?)', $agents_id);
db_do ($dbh, 'DELETE FROM tlayout_data WHERE id_agente_modulo NOT IN (SELECT id_agente_modulo FROM tagente_modulo)');
# Clearl orphan data from dashboards
log_message ('INTEGRITY', "Deleting orphan dahsboard items.");
my $where_condition;
my $index ;
foreach my $agent_id (@agents_ids) {
$where_condition .= 'options NOT LIKE ("%\\"agentid\\":\\"'.$agent_id->{'id_agente'}.'\\"%")';
if($agent_id == @agents_ids[-1]) {
if($agent_id == $agents_ids[-1]) {
last;
}
$where_condition .= ' AND ';
}
db_do ($dbh, 'UPDATE twidget_dashboard set options = NULL WHERE '.$where_condition);
# Delete orphan report items.
db_do ($dbh, 'DELETE FROM treport_content WHERE id_agent != 0 AND id_agent NOT IN (?)', $agents_id);
$where_condition = '';
my @modules = get_db_rows($dbh, 'SELECT id_agente_modulo FROM tagente_modulo');
foreach my $id_agente_modulo (@modules) {
print Dumper($id_agente_modulo);
$where_condition .= 'options NOT LIKE ("%\\"moduleId\\":\\"'.$id_agente_modulo->{'id_agente_modulo'}.'\\"%")';
if($id_agente_modulo == $modules[-1]) {
last;
}
$where_condition .= ' AND ';
}
# Delete orphan tevent alert rules
db_do ($dbh, 'DELETE FROM tevent_rule WHERE agent IS NOT NULL AND agent != "" AND agent NOT IN (?) AND operator_agent = ?', $agents_alias, '==');
db_do ($dbh, 'DELETE FROM tevent_rule WHERE log_agent IS NOT NULL AND log_agent != "" AND log_agent NOT IN (?) AND operator_log_agent = ?', $agents_alias, '==');
db_do ($dbh, 'UPDATE twidget_dashboard set options = NULL WHERE '.$where_condition);
# Delete orphan data from favorite agents
log_message ('INTEGRITY', "Deleting orphan favories items.");
db_do ($dbh, 'DELETE FROM tfavmenu_user WHERE section = "Agents" AND id_element NOT IN (?)', $agents_id);
# Delete orphan data from tservices.
db_do ($dbh, 'DELETE FROM tservice_element WHERE id_agent NOT IN (?)', $agents_id);
# Delete orphan data from gis maps
log_message ('INTEGRITY', "Deleting orphan GIS data.");
db_do ($dbh, 'DELETE FROM tgis_data_history WHERE tagente_id_agente NOT IN (?)', $agents_id);
# Delete agents from policies
db_do ($dbh, 'DELETE FROM tpolicy_agents WHERE id_agent NOT IN (?)', $agents_id);
# Delete orphan tnetwork maps data
log_message ('INTEGRITY', "Deleting orphan networkmaps data.");
db_do ($dbh, 'DELETE FROM titem WHERE source_data NOT IN (?)', $agents_id);
db_do ($dbh, 'DELETE FROM trel_item WHERE id_parent_source_data NOT IN (?) OR id_child_source_data NOT IN (?)', $agents_id, $agents_id);
}
# Check enterprise tables
enterprise_hook ('pandora_checkdb_integrity_enterprise', [$conf, $dbh]);
}