From c800061bc23673dd285242b54f4170b9fa300ac3 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Wed, 4 Jul 2018 18:39:06 +0200 Subject: [PATCH] Autoconfiguration v1 --- pandora_console/extras/mr/18.sql | 10 +++---- .../pandoradb_migrate_6.0_to_7.0.mysql.sql | 10 +++---- pandora_console/pandoradb.sql | 10 +++---- pandora_server/lib/PandoraFMS/Config.pm | 5 ++++ pandora_server/lib/PandoraFMS/DB.pm | 30 +++++++++++++++++++ pandora_server/lib/PandoraFMS/DataServer.pm | 6 ++++ pandora_server/lib/PandoraFMS/PluginTools.pm | 12 ++++++++ 7 files changed, 68 insertions(+), 15 deletions(-) diff --git a/pandora_console/extras/mr/18.sql b/pandora_console/extras/mr/18.sql index e82f940bfb..310e42c1c8 100644 --- a/pandora_console/extras/mr/18.sql +++ b/pandora_console/extras/mr/18.sql @@ -25,8 +25,8 @@ CREATE TABLE `tautoconfig_rules` ( `order_by` int(11) NOT NULL DEFAULT '0', `operator` enum('AND','OR') DEFAULT 'OR', `type` enum('alias','ip-range','group','os','custom-field','script') DEFAULT 'alias', - `value` varchar(100) NOT NULL DEFAULT '', - `custom` varchar(100) NOT NULL DEFAULT '', + `value` text, + `custom` text, PRIMARY KEY (`id`), KEY `id_autoconfig` (`id_autoconfig`), CONSTRAINT `tautoconfig_rules_ibfk_1` FOREIGN KEY (`id_autoconfig`) REFERENCES `tautoconfig` (`id`) ON DELETE CASCADE @@ -37,9 +37,9 @@ CREATE TABLE `tautoconfig_actions` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `id_autoconfig` int(10) unsigned NOT NULL, `order` int(11) NOT NULL DEFAULT '0', - `action_type` enum('set-group', 'set-secondary-group','apply-policy','apply-all-policies','launch-event','launch-alert-action','raw-config') DEFAULT 'launch-event', - `value` varchar(100) NOT NULL DEFAULT '', - `custom` varchar(100) NOT NULL DEFAULT '', + `action_type` enum('set-group', 'set-secondary-group', 'apply-policiy', 'launch-script', 'launch-event', 'launch-alert-action', 'raw-config') DEFAULT 'launch-event', + `value` text, + `custom` text, PRIMARY KEY (`id`), KEY `id_autoconfig` (`id_autoconfig`), CONSTRAINT `tautoconfig_action_ibfk_1` FOREIGN KEY (`id_autoconfig`) REFERENCES `tautoconfig` (`id`) ON DELETE CASCADE diff --git a/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql b/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql index 19add1b91a..233f376410 100644 --- a/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql +++ b/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql @@ -1688,8 +1688,8 @@ CREATE TABLE IF NOT EXISTS `tautoconfig_rules` ( `order_by` int(11) NOT NULL DEFAULT '0', `operator` enum('AND','OR') DEFAULT 'OR', `type` enum('alias','ip-range','group','os','custom-field','script') DEFAULT 'alias', - `value` varchar(100) NOT NULL DEFAULT '', - `custom` varchar(100) NOT NULL DEFAULT '', + `value` text, + `custom` text, PRIMARY KEY (`id`), KEY `id_autoconfig` (`id_autoconfig`), CONSTRAINT `tautoconfig_rules_ibfk_1` FOREIGN KEY (`id_autoconfig`) REFERENCES `tautoconfig` (`id`) ON DELETE CASCADE @@ -1702,9 +1702,9 @@ CREATE TABLE IF NOT EXISTS `tautoconfig_actions` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `id_autoconfig` int(10) unsigned NOT NULL, `order` int(11) NOT NULL DEFAULT '0', - `action_type` enum('set-group', 'set-secondary-group','apply-policy','apply-all-policies','launch-event','launch-alert-action','raw-config') DEFAULT 'launch-event', - `value` varchar(100) NOT NULL DEFAULT '', - `custom` varchar(100) NOT NULL DEFAULT '', + `action_type` enum('set-group', 'set-secondary-group', 'apply-policiy', 'launch-script', 'launch-event', 'launch-alert-action', 'raw-config') DEFAULT 'launch-event', + `value` text, + `custom` text, PRIMARY KEY (`id`), KEY `id_autoconfig` (`id_autoconfig`), CONSTRAINT `tautoconfig_action_ibfk_1` FOREIGN KEY (`id_autoconfig`) REFERENCES `tautoconfig` (`id`) ON DELETE CASCADE diff --git a/pandora_console/pandoradb.sql b/pandora_console/pandoradb.sql index a06099511b..c3e6bdd9c7 100644 --- a/pandora_console/pandoradb.sql +++ b/pandora_console/pandoradb.sql @@ -3215,8 +3215,8 @@ CREATE TABLE IF NOT EXISTS `tautoconfig_rules` ( `order_by` int(11) NOT NULL DEFAULT '0', `operator` enum('AND','OR') DEFAULT 'OR', `type` enum('alias','ip-range','group','os','custom-field','script') DEFAULT 'alias', - `value` varchar(100) NOT NULL DEFAULT '', - `custom` varchar(100) NOT NULL DEFAULT '', + `value` text, + `custom` text, PRIMARY KEY (`id`), KEY `id_autoconfig` (`id_autoconfig`), CONSTRAINT `tautoconfig_rules_ibfk_1` FOREIGN KEY (`id_autoconfig`) REFERENCES `tautoconfig` (`id`) ON DELETE CASCADE @@ -3229,9 +3229,9 @@ CREATE TABLE IF NOT EXISTS `tautoconfig_actions` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `id_autoconfig` int(10) unsigned NOT NULL, `order` int(11) NOT NULL DEFAULT '0', - `action_type` enum('set-group', 'set-secondary-group','apply-policy','apply-all-policies','launch-event','launch-alert-action','raw-config') DEFAULT 'launch-event', - `value` varchar(100) NOT NULL DEFAULT '', - `custom` varchar(100) NOT NULL DEFAULT '', + `action_type` enum('set-group', 'set-secondary-group', 'apply-policiy', 'launch-script', 'launch-event', 'launch-alert-action', 'raw-config') DEFAULT 'launch-event', + `value` text, + `custom` text, PRIMARY KEY (`id`), KEY `id_autoconfig` (`id_autoconfig`), CONSTRAINT `tautoconfig_action_ibfk_1` FOREIGN KEY (`id_autoconfig`) REFERENCES `tautoconfig` (`id`) ON DELETE CASCADE diff --git a/pandora_server/lib/PandoraFMS/Config.pm b/pandora_server/lib/PandoraFMS/Config.pm index afe1c98dfd..88eafe660a 100644 --- a/pandora_server/lib/PandoraFMS/Config.pm +++ b/pandora_server/lib/PandoraFMS/Config.pm @@ -487,6 +487,8 @@ sub pandora_load_config { $pa_config->{"provisioningserver_threads"} = 1; # 7.0 720 $pa_config->{"provisioning_cache_interval"} = 300; # 7.0 720 + $pa_config->{"autoconfigure_agents"} = 1; # 7.0 725 + # Check for UID0 if ($pa_config->{"quiet"} != 0){ if ($> == 0){ @@ -1113,6 +1115,9 @@ sub pandora_load_config { elsif ($parametro =~ m/^provisioning_cache_interval\s+([0-9]*)/i){ $pa_config->{'provisioning_cache_interval'}= clean_blank($1); } + elsif ($parametro =~ m/^autoconfigure_agents\s+([0-1])/i){ + $pa_config->{'autoconfigure_agents'}= clean_blank($1); + } } # end of loop for parameter # # Set to RDBMS' standard port diff --git a/pandora_server/lib/PandoraFMS/DB.pm b/pandora_server/lib/PandoraFMS/DB.pm index 1fe1208d78..417fd9cf5d 100644 --- a/pandora_server/lib/PandoraFMS/DB.pm +++ b/pandora_server/lib/PandoraFMS/DB.pm @@ -50,6 +50,7 @@ our @EXPORT = qw( db_text db_update db_update_get_values + set_update_agent get_action_id get_addr_id get_agent_addr_id @@ -801,6 +802,35 @@ sub get_db_rows_limit ($$$;@) { return @rows; } +########################################################################## +## Updates agent fields using field => value +## Be careful, no filter is done. +########################################################################## +sub set_update_agent { + my ($dbh, $agent_id, $data) = @_; + + return undef unless (defined($agent_id) && $agent_id > 0); + return undef unless (ref($data) eq "HASH"); + + # Build update query + my $query = 'UPDATE tagente SET '; + + my @values; + foreach my $field (keys %{$data}) { + push @values, $data->{$field}; + + $query .= ' ' . $field . ' = ?,'; + } + + chop($query); + + $query .= ' WHERE id_agente = ? '; + push @values, $agent_id; + + return db_update($dbh, $query, @values); +} + + ########################################################################## ## SQL delete with a LIMIT clause. ########################################################################## diff --git a/pandora_server/lib/PandoraFMS/DataServer.pm b/pandora_server/lib/PandoraFMS/DataServer.pm index ef62bab200..f0c05da7a4 100644 --- a/pandora_server/lib/PandoraFMS/DataServer.pm +++ b/pandora_server/lib/PandoraFMS/DataServer.pm @@ -409,6 +409,12 @@ sub process_xml_data ($$$$$) { } } } + + if (defined($pa_config->{'autoconfigure_agents'}) && $pa_config->{'autoconfigure_agents'} == 1) { + # Update agent configuration once, before create agent - MetaConsole port to Node + enterprise_hook('autoconfigure_agent', [$pa_config, $agent_name, $agent_id, $data, $dbh]); + } + } # Get the data of the agent, if fail return diff --git a/pandora_server/lib/PandoraFMS/PluginTools.pm b/pandora_server/lib/PandoraFMS/PluginTools.pm index 3afa5ff898..45ac0a6e93 100644 --- a/pandora_server/lib/PandoraFMS/PluginTools.pm +++ b/pandora_server/lib/PandoraFMS/PluginTools.pm @@ -990,6 +990,18 @@ sub init { return $conf; } +################################################################################ +# Update internal UA timeout +################################################################################ +sub ua_set_timeout { + my ($config, $timeout) = @_; + return unless looks_like_number($timeout) and $timeout > 0; + my $sys = get_sys_environment($config); + + return unless defined($sys->{'ua'}); + $sys->{'ua'}->timeout($timeout); +} + ################################################################################ # initialize plugin (basic) ################################################################################