Merge branch 'ent-8732-giss-13230-aplicar-reglas-de-autoconfiguracion-de-forma-programada' into 'develop'

Ent 8732 giss 13230 aplicar reglas de autoconfiguracion de forma programada

See merge request artica/pandorafms!5011
This commit is contained in:
Diego Muñoz-Reja 2022-07-14 10:35:52 +00:00
commit 0f770edc13
4 changed files with 95 additions and 2 deletions

View File

@ -23,4 +23,17 @@ CREATE TABLE IF NOT EXISTS `tuser_task_scheduled` (
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=UTF8MB4;
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;

View File

@ -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;

View File

@ -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,67 @@ 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) {
eval {{
local $SIG{__DIE__};
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'
);
# 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 '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) ||
($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 ($pa_config->{'autoconfigure_agents_threshold'});
}
db_disconnect($dbh);
}
########################################################################################
# Additional tasks executed periodically by the Pandora FMS Server
########################################################################################

View File

@ -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);
}