From 7f0b676e0f03e91c6a51d5e5ce017f62f7f003fb Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Mon, 18 Feb 2019 19:50:13 +0100 Subject: [PATCH] Discovery tasks. Extended events. Autoconfigure detected agents. Former-commit-id: da2282082606c970dbace453193eb0a7e4089731 --- .../godmode/wizards/HostDevices.class.php | 4 +++ pandora_server/lib/PandoraFMS/Core.pm | 32 ++++++++++++++++--- .../lib/PandoraFMS/DiscoveryServer.pm | 17 ++++++++-- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/pandora_console/godmode/wizards/HostDevices.class.php b/pandora_console/godmode/wizards/HostDevices.class.php index c537ae3c8d..bf4eb8f73d 100755 --- a/pandora_console/godmode/wizards/HostDevices.class.php +++ b/pandora_console/godmode/wizards/HostDevices.class.php @@ -305,6 +305,9 @@ class HostDevices extends Wizard } $id_network_profile = get_parameter('id_network_profile', null); + $autoconf_enabled = get_parameter_switch( + 'autoconfiguration_enabled' + ); $snmp_enabled = get_parameter_switch('snmp_enabled'); $os_detect = get_parameter_switch('os_detect'); $parent_detection = get_parameter_switch('parent_detection'); @@ -329,6 +332,7 @@ class HostDevices extends Wizard $this->task['snmp_community'] = $community; } + $this->task['autoconfiguration_enabled'] = $autoconf_enabled; $this->task['id_network_profile'] = $id_network_profile; $this->task['snmp_enabled'] = $snmp_enabled; $this->task['os_detect'] = $os_detect; diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index ba9ddbeae8..84f16b5362 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -179,6 +179,7 @@ our @EXPORT = qw( pandora_evaluate_alert pandora_evaluate_snmp_alerts pandora_event + pandora_extended_event pandora_execute_alert pandora_execute_action pandora_exec_forced_alerts @@ -3270,11 +3271,11 @@ sub pandora_event ($$$$$$$$$$;$$$$$$$$$$$) { # Create the event logger($pa_config, "Generating event '$evento' for agent ID $id_agente module ID $id_agentmodule.", 10); - db_do ($dbh, 'INSERT INTO ' . $event_table . ' (id_agente, id_grupo, evento, timestamp, estado, utimestamp, event_type, id_agentmodule, id_alert_am, criticity, user_comment, tags, source, id_extra, id_usuario, critical_instructions, warning_instructions, unknown_instructions, ack_utimestamp, custom_data, data, module_status) + my $event_id = db_insert ($dbh, 'id_evento','INSERT INTO ' . $event_table . ' (id_agente, id_grupo, evento, timestamp, estado, utimestamp, event_type, id_agentmodule, id_alert_am, criticity, user_comment, tags, source, id_extra, id_usuario, critical_instructions, warning_instructions, unknown_instructions, ack_utimestamp, custom_data, data, module_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', $id_agente, $id_grupo, safe_input ($evento), $timestamp, $event_status, $utimestamp, $event_type, $id_agentmodule, $id_alert_am, $severity, $comment, $module_tags, $source, $id_extra, $user_name, $critical_instructions, $warning_instructions, $unknown_instructions, $ack_utimestamp, $custom_data, $module_data, $module_status); - + # Do not write to the event file - return if ($pa_config->{'event_file'} eq ''); + return $event_id if ($pa_config->{'event_file'} eq ''); # Add a header when the event file is created my $header = undef; @@ -3285,7 +3286,7 @@ sub pandora_event ($$$$$$$$$$;$$$$$$$$$$$) { # Open the event file for writing if (! open (EVENT_FILE, '>>' . $pa_config->{'event_file'})) { logger($pa_config, "Error opening event file " . $pa_config->{'event_file'} . ": $!", 10); - return; + return $event_id; } # Resolve ids @@ -3308,6 +3309,29 @@ sub pandora_event ($$$$$$$$$$;$$$$$$$$$$$) { print EVENT_FILE "$agent_name,".safe_output($group_name)."," . safe_output ($evento) . ",$timestamp,$event_status,$utimestamp,$event_type,".safe_output($module_name).",".safe_output($alert_name).",$severity,".safe_output($comment).",".safe_output($module_tags).",$source,$id_extra,$user_name,".safe_output($critical_instructions).",".safe_output($warning_instructions).",".safe_output($unknown_instructions).",$ack_utimestamp\n"; close (EVENT_FILE); + + return $event_id; +} + +########################################################################## +=head2 C<< pandora_extended_event (I<$pa_config>, I<$dbh>, I<$event_id>, I<$description>) >> + +Creates an extended event linked to an existing main event id. + +=cut +########################################################################## +sub pandora_extended_event($$$$) { + my ($pa_config, $dbh, $event_id, $description) = @_; + + return unless defined($event_id) && "$event_id" ne "" && $event_id > 0; + + return db_do( + $dbh, + 'INSERT INTO tevent_extended (id_evento, utimestamp, description) VALUES (?,?,?)', + $event_id, + time(), + safe_input($description) + ); } ########################################################################## diff --git a/pandora_server/lib/PandoraFMS/DiscoveryServer.pm b/pandora_server/lib/PandoraFMS/DiscoveryServer.pm index b6423a8506..8911d353e3 100644 --- a/pandora_server/lib/PandoraFMS/DiscoveryServer.pm +++ b/pandora_server/lib/PandoraFMS/DiscoveryServer.pm @@ -163,6 +163,8 @@ sub data_consumer ($$) { @auth_strings = split(/,/, safe_output($task->{'auth_strings'})); } + my $main_event = pandora_event($pa_config, "[Discovery] Execution summary",$task->{'id_group'}, 0, 0, 0, 0, 'system', 0, $dbh); + my $recon = new PandoraFMS::Recon::Base( communities => \@communities, dbh => $dbh, @@ -190,7 +192,8 @@ sub data_consumer ($$) { vlan_cache_enabled => $task->{'vlan_enabled'}, wmi_enabled => $task->{'wmi_enabled'}, auth_strings_array => \@auth_strings, - autoconfigure_agent => $task->{'autoconfiguration_enabled'} + autoconfiguration_enabled => $task->{'autoconfiguration_enabled'}, + main_event_id => $main_event, %{$pa_config} ); @@ -451,10 +454,18 @@ sub PandoraFMS::Recon::Base::create_agent($$) { if (defined($self->{'autoconfiguration_enabled'}) && $self->{'autoconfiguration_enabled'} == 1) { my $agent_data = PandoraFMS::DB::get_db_single_row($self->{'dbh'}, 'SELECT * FROM tagente WHERE id_agente = ?', $agent_id); # Update agent configuration once, after create agent. - enterprise_hook('autoconfigure_agent', [$self->{'pa_config'}, $host_name, $agent_id, $agent_data, $self->{'dbh'}]); + enterprise_hook('autoconfigure_agent', [$self->{'pa_config'}, $host_name, $agent_id, $agent_data, $self->{'dbh'}, $agent_id]); + } + + if (defined($self->{'main_event_id'})) { + my $addresses_str = join(',', safe_output($self->get_addresses($device))); + pandora_extended_event( + $self->{'pa_config'}, $self->{'dbh'}, $self->{'main_event_id'}, + "[Discovery] New " . safe_output($self->get_device_type($device)) . " found " . $host_name . " (" . $addresses_str . ") Agent $agent_id." + ); + } - pandora_event($self->{'pa_config'}, "[RECON] New " . safe_output($self->get_device_type($device)) . " found (" . join(',', safe_output($self->get_addresses($device))) . ").", $self->{'group_id'}, $agent_id, 2, 0, 0, 'recon_host_detected', 0, $self->{'dbh'}); $agent_learning = 1; # Create network profile modules for the agent