From 7a3da419bd0efd2c97103bd45365cdf1c26953a2 Mon Sep 17 00:00:00 2001 From: Calvo Date: Fri, 23 Jun 2023 15:06:31 +0200 Subject: [PATCH] Clean deleted agents and modules from dashboards, reports, etc --- pandora_server/util/pandora_db.pl | 36 +++++++++++++++++-------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/pandora_server/util/pandora_db.pl b/pandora_server/util/pandora_db.pl index 48ea46f7fe..d8d61eb7df 100755 --- a/pandora_server/util/pandora_db.pl +++ b/pandora_server/util/pandora_db.pl @@ -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]); }