From 994c76a39ba59a1f5a210a7da59bb8989274dca3 Mon Sep 17 00:00:00 2001 From: Daniel Maya Date: Fri, 8 Jul 2022 11:02:58 +0200 Subject: [PATCH 1/4] #8732 Added autoconfiguration scheduled --- pandora_console/extras/mr/56.sql | 16 ++++++++++++++++ pandora_console/pandoradb.sql | 12 ++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 pandora_console/extras/mr/56.sql diff --git a/pandora_console/extras/mr/56.sql b/pandora_console/extras/mr/56.sql new file mode 100644 index 0000000000..47cc9b2220 --- /dev/null +++ b/pandora_console/extras/mr/56.sql @@ -0,0 +1,16 @@ +START TRANSACTION; + +ALTER TABLE `tautoconfig` ADD COLUMN `type_execution` VARCHAR(100) NOT NULL DEFAULT 'start'; +ALTER TABLE `tautoconfig` ADD COLUMN `type_periodicity` VARCHAR(100) NOT NULL DEFAULT 'weekly'; +ALTER TABLE `tautoconfig` ADD COLUMN `monday` TINYINT DEFAULT 0; +ALTER TABLE `tautoconfig` ADD COLUMN `tuesday` TINYINT DEFAULT 0; +ALTER TABLE `tautoconfig` ADD COLUMN `wednesday` TINYINT DEFAULT 0; +ALTER TABLE `tautoconfig` ADD COLUMN `thursday` TINYINT DEFAULT 0; +ALTER TABLE `tautoconfig` ADD COLUMN `friday` TINYINT DEFAULT 0; +ALTER TABLE `tautoconfig` ADD COLUMN `saturday` TINYINT DEFAULT 0; +ALTER TABLE `tautoconfig` ADD COLUMN `sunday` TINYINT DEFAULT 0; +ALTER TABLE `tautoconfig` ADD COLUMN `periodically_day_from` INT UNSIGNED DEFAULT NULL; +ALTER TABLE `tautoconfig` ADD COLUMN `periodically_time_from` time NULL DEFAULT NULL; +ALTER TABLE `tautoconfig` ADD COLUMN `executed` TINYINT UNSIGNED NOT NULL DEFAULT 0; + +COMMIT; diff --git a/pandora_console/pandoradb.sql b/pandora_console/pandoradb.sql index 145cabd447..7fd6995477 100644 --- a/pandora_console/pandoradb.sql +++ b/pandora_console/pandoradb.sql @@ -3575,6 +3575,18 @@ CREATE TABLE IF NOT EXISTS `tautoconfig` ( `order` INT NOT NULL DEFAULT 0, `description` TEXT, `disabled` TINYINT DEFAULT 0, + `type_execution` VARCHAR(100) NOT NULL DEFAULT 'start', + `type_periodicity` VARCHAR(100) NOT NULL DEFAULT 'weekly', + `monday` TINYINT DEFAULT 0, + `tuesday` TINYINT DEFAULT 0, + `wednesday` TINYINT DEFAULT 0, + `thursday` TINYINT DEFAULT 0, + `friday` TINYINT DEFAULT 0, + `saturday` TINYINT DEFAULT 0, + `sunday` TINYINT DEFAULT 0, + `periodically_day_from` INT UNSIGNED DEFAULT NULL, + `periodically_time_from` time NULL DEFAULT NULL, + `executed` TINYINT UNSIGNED NOT NULL DEFAULT 0, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=UTF8MB4; From 26cfde6687f462b812caddec6ea0de851446376a Mon Sep 17 00:00:00 2001 From: Daniel Maya Date: Tue, 12 Jul 2022 12:37:22 +0200 Subject: [PATCH 2/4] #8732 Added autoconfiguration scheduled 2 --- pandora_server/bin/pandora_server | 57 +++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/pandora_server/bin/pandora_server b/pandora_server/bin/pandora_server index 14ae4914db..80bb67ec5e 100755 --- a/pandora_server/bin/pandora_server +++ b/pandora_server/bin/pandora_server @@ -139,9 +139,12 @@ sub pandora_startup () { # Start the policy queue thread. start_server_thread(\&pandora_process_policy_queue, [\%Config]) if ($Config{'__enterprise_enabled'} == 1 && $Config{'policy_manager'} == 1); - + + # Start agent autoconfiguration thread. + start_server_thread(\&pandora_agent_autoconfiguration_scheduled, [\%Config]) if ($Config{'__enterprise_enabled'} == 1 && $Config{'autoconfigure_agents'} == 1); + pandora_audit (\%Config, $Config{'rb_product_name'} . ' Server Daemon starting', 'SYSTEM', 'System', $DBH); - + # Load servers if (!is_metaconsole(\%Config)) { pandora_reset_server (\%Config, $DBH); @@ -308,6 +311,56 @@ sub pandora_stop_netflow_daemon () { return 0; } +sub pandora_agent_autoconfiguration_scheduled($) { + my $pa_config = shift; + + my %pa_config = %{$pa_config}; + + my $dbh = db_connect ($pa_config{'dbengine'}, $pa_config{'dbname'}, $pa_config{'dbhost'}, $pa_config{'dbport'}, + $pa_config{'dbuser'}, $pa_config{'dbpass'}); + + while ($THRRUN == 1) { + my @autoconfig = get_db_rows ( + $dbh, + 'SELECT *, DATE_FORMAT(DATE_ADD(periodically_time_from, INTERVAL 5 MINUTE), "%H:%i:%S") AS time_minutes FROM tautoconfig + WHERE executed = 0 AND type_execution LIKE "scheduled" AND disabled = 0' + ); + + # Get current time. + my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time()); + my $time = sprintf ("%.2d:%.2d:%.2d", $hour, $min, $sec); + + foreach my $conf (@autoconfig) { + if (($conf->{'type_periodicity'} eq 'monthly' && $conf->{'periodically_day_from'} eq $mday) || + ($conf->{'type_periodicity'} eq 'weekly' && (($conf->{'sunday'} eq 1 && $wday eq 0) || + ($conf->{'monday'} eq 1 && $wday eq 1) || ($conf->{'tuesday'} eq 1 && $wday eq 2) || + ($conf->{'wednesday'} eq 1 && $wday eq 3) || ($conf->{'thursday'} eq 1 && $wday eq 4) || + ($conf->{'friday'} eq 1 && $wday eq 5) || ($conf->{'saturday'} eq 1 && $wday eq 6))) + ) { + if ($time ge $conf->{'periodically_time_from'} && $time le $conf->{'time_minutes'}) { + # Get agents. + my @agents = get_db_rows( + $dbh, + 'SELECT id_agente, alias, id_grupo, id_os, os_version, direccion, nombre AS agent_name FROM tagente + WHERE `disabled` = 0' + ); + + foreach my $agent (@agents) { + my $match = enterprise_hook('autoconf_evaluate_rules', [$pa_config, $dbh, $agent, $conf->{'id'}, $agent->{'id_agente'}, 1]); + if (defined($match) && $match > 0) { + enterprise_hook('autoconf_execute_actions', [$pa_config, $dbh, $agent->{'id_agente'}, $agent, $conf->{'id'}]); + } + } + } + } + } + + sleep (300); + } + + db_disconnect($dbh); +} + ######################################################################################## # Additional tasks executed periodically by the Pandora FMS Server ######################################################################################## From 8ffb247703085d395a5e1df5d721e874f84dc3ef Mon Sep 17 00:00:00 2001 From: Daniel Maya Date: Wed, 13 Jul 2022 13:04:15 +0200 Subject: [PATCH 3/4] #8732 Added autoconfiguration scheduled 3 --- pandora_server/bin/pandora_server | 66 ++++++++++++++----------- pandora_server/lib/PandoraFMS/Config.pm | 4 ++ 2 files changed, 42 insertions(+), 28 deletions(-) diff --git a/pandora_server/bin/pandora_server b/pandora_server/bin/pandora_server index 80bb67ec5e..d3b33cbfbc 100755 --- a/pandora_server/bin/pandora_server +++ b/pandora_server/bin/pandora_server @@ -320,42 +320,52 @@ sub pandora_agent_autoconfiguration_scheduled($) { $pa_config{'dbuser'}, $pa_config{'dbpass'}); while ($THRRUN == 1) { - my @autoconfig = get_db_rows ( - $dbh, - 'SELECT *, DATE_FORMAT(DATE_ADD(periodically_time_from, INTERVAL 5 MINUTE), "%H:%i:%S") AS time_minutes FROM tautoconfig - WHERE executed = 0 AND type_execution LIKE "scheduled" AND disabled = 0' - ); + eval {{ + local $SIG{__DIE__}; - # Get current time. - my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time()); - my $time = sprintf ("%.2d:%.2d:%.2d", $hour, $min, $sec); + my @autoconfig = get_db_rows ( + $dbh, + 'SELECT *, DATE_FORMAT(DATE_ADD(periodically_time_from, INTERVAL ' . $pa_config->{'autoconfigure_agents_threshold'} . ' SECOND), "%H:%i:%S") AS time_minutes + FROM tautoconfig WHERE executed = 0 AND type_execution LIKE "scheduled" AND disabled = 0' + ); - foreach my $conf (@autoconfig) { - if (($conf->{'type_periodicity'} eq 'monthly' && $conf->{'periodically_day_from'} eq $mday) || - ($conf->{'type_periodicity'} eq 'weekly' && (($conf->{'sunday'} eq 1 && $wday eq 0) || - ($conf->{'monday'} eq 1 && $wday eq 1) || ($conf->{'tuesday'} eq 1 && $wday eq 2) || - ($conf->{'wednesday'} eq 1 && $wday eq 3) || ($conf->{'thursday'} eq 1 && $wday eq 4) || - ($conf->{'friday'} eq 1 && $wday eq 5) || ($conf->{'saturday'} eq 1 && $wday eq 6))) - ) { - if ($time ge $conf->{'periodically_time_from'} && $time le $conf->{'time_minutes'}) { - # Get agents. - my @agents = get_db_rows( - $dbh, - 'SELECT id_agente, alias, id_grupo, id_os, os_version, direccion, nombre AS agent_name FROM tagente - WHERE `disabled` = 0' - ); + # Get current time. + my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time()); + my $time = sprintf ("%.2d:%.2d:%.2d", $hour, $min, $sec); - foreach my $agent (@agents) { - my $match = enterprise_hook('autoconf_evaluate_rules', [$pa_config, $dbh, $agent, $conf->{'id'}, $agent->{'id_agente'}, 1]); - if (defined($match) && $match > 0) { - enterprise_hook('autoconf_execute_actions', [$pa_config, $dbh, $agent->{'id_agente'}, $agent, $conf->{'id'}]); + foreach my $conf (@autoconfig) { + if (($conf->{'type_periodicity'} eq 'monthly' && $conf->{'periodically_day_from'} eq $mday) || + ($conf->{'type_periodicity'} eq 'weekly' && (($conf->{'sunday'} eq 1 && $wday eq 0) || + ($conf->{'monday'} eq 1 && $wday eq 1) || ($conf->{'tuesday'} eq 1 && $wday eq 2) || + ($conf->{'wednesday'} eq 1 && $wday eq 3) || ($conf->{'thursday'} eq 1 && $wday eq 4) || + ($conf->{'friday'} eq 1 && $wday eq 5) || ($conf->{'saturday'} eq 1 && $wday eq 6))) + ) { + if ($time ge $conf->{'periodically_time_from'} && $time le $conf->{'time_minutes'}) { + # Update executed. + db_process_update ($dbh, 'tautoconfig', {'executed' => 1}, {'id' => $conf->{'id'}}); + # Get agents. + my @agents = get_db_rows( + $dbh, + 'SELECT id_agente, alias, id_grupo, id_os, os_version, direccion, nombre AS agent_name FROM tagente + WHERE `disabled` = 0' + ); + + foreach my $agent (@agents) { + # Check if the agent matches the rules. + my $match = enterprise_hook('autoconf_evaluate_rules', [$pa_config, $dbh, $agent, $conf->{'id'}, $agent->{'id_agente'}, 1]); + if (defined($match) && $match > 0) { + enterprise_hook('autoconf_execute_actions', [$pa_config, $dbh, $agent->{'id_agente'}, $agent, $conf->{'id'}]); + } } + + # Update executed. + db_process_update ($dbh, 'tautoconfig', {'executed' => 0}, {'id' => $conf->{'id'}}); } } } - } + }}; - sleep (300); + sleep ($pa_config->{'autoconfigure_agents_threshold'}); } db_disconnect($dbh); diff --git a/pandora_server/lib/PandoraFMS/Config.pm b/pandora_server/lib/PandoraFMS/Config.pm index 94c7d34225..ae1ea28bcc 100644 --- a/pandora_server/lib/PandoraFMS/Config.pm +++ b/pandora_server/lib/PandoraFMS/Config.pm @@ -539,6 +539,7 @@ sub pandora_load_config { $pa_config->{"provisioning_cache_interval"} = 300; # 7.0 720 $pa_config->{"autoconfigure_agents"} = 1; # 7.0 725 + $pa_config->{"autoconfigure_agents_threshold"} = 300; #7.0 764 $pa_config->{'snmp_extlog'} = ""; # 7.0 726 @@ -1265,6 +1266,9 @@ sub pandora_load_config { elsif ($parametro =~ m/^autoconfigure_agents\s+([0-1])/i){ $pa_config->{'autoconfigure_agents'}= clean_blank($1); } + elsif ($parametro =~ m/^autoconfigure_agents_threshold\s+([0-1])/i){ + $pa_config->{'autoconfigure_agents_threshold'}= clean_blank($1); + } elsif ($parametro =~ m/^snmp_extlog\s(.*)/i) { $pa_config->{'snmp_extlog'} = clean_blank($1); } From 8096ffebc1956bc71fec6e1062fcdba041958e25 Mon Sep 17 00:00:00 2001 From: Daniel Maya Date: Thu, 14 Jul 2022 10:10:25 +0200 Subject: [PATCH 4/4] #8732 Added autoconfiguration scheduled 4 --- pandora_server/bin/pandora_server | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandora_server/bin/pandora_server b/pandora_server/bin/pandora_server index d3b33cbfbc..ce091872e2 100755 --- a/pandora_server/bin/pandora_server +++ b/pandora_server/bin/pandora_server @@ -334,7 +334,8 @@ sub pandora_agent_autoconfiguration_scheduled($) { my $time = sprintf ("%.2d:%.2d:%.2d", $hour, $min, $sec); foreach my $conf (@autoconfig) { - if (($conf->{'type_periodicity'} eq 'monthly' && $conf->{'periodically_day_from'} eq $mday) || + if (($conf->{'type_periodicity'} eq 'daily') || + ($conf->{'type_periodicity'} eq 'monthly' && $conf->{'periodically_day_from'} eq $mday) || ($conf->{'type_periodicity'} eq 'weekly' && (($conf->{'sunday'} eq 1 && $wday eq 0) || ($conf->{'monday'} eq 1 && $wday eq 1) || ($conf->{'tuesday'} eq 1 && $wday eq 2) || ($conf->{'wednesday'} eq 1 && $wday eq 3) || ($conf->{'thursday'} eq 1 && $wday eq 4) ||