From 47f3dd55fa07f0fa53e85485136e48ed90b25712 Mon Sep 17 00:00:00 2001 From: zarzuelo Date: Thu, 15 Nov 2012 15:20:48 +0000 Subject: [PATCH] 2012-11-15 Sergio Martin * lib/PandoraFMS/Core.pm lib/PandoraFMS/DataServer.pm: Added support for new agent data in the XML as custom_id, url_address and custom fields git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@7150 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_server/ChangeLog | 6 +++ pandora_server/lib/PandoraFMS/Core.pm | 27 ++++++++---- pandora_server/lib/PandoraFMS/DataServer.pm | 46 ++++++++++++++++++--- 3 files changed, 64 insertions(+), 15 deletions(-) diff --git a/pandora_server/ChangeLog b/pandora_server/ChangeLog index 9f50e95347..3a61fc29a7 100644 --- a/pandora_server/ChangeLog +++ b/pandora_server/ChangeLog @@ -1,3 +1,9 @@ +2012-11-15 Sergio Martin + + * lib/PandoraFMS/Core.pm + lib/PandoraFMS/DataServer.pm: Added support for new agent data in + the XML as custom_id, url_address and custom fields + 2012-11-15 Ramon Novoa * util/pandora_db.pl: Try to update module status count for all agents. diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index ca2f1fc1ea..458e6b7208 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -1869,7 +1869,7 @@ sub pandora_update_server ($$$$$;$$) { } ########################################################################## -=head2 C<< pandora_update_agent (I<$pa_config>, I<$agent_timestamp>, I<$agent_id>, I<$os_version>, I<$agent_version>, I<$agent_interval>, I<$dbh>, [I<$timezone_offset>], [I<$longitude>], [I<$latitude>], [I<$altitude>], [I<$position_description>]) [I<$parent_agent_name>]) >> +=head2 C<< pandora_update_agent (I<$pa_config>, I<$agent_timestamp>, I<$agent_id>, I<$os_version>, I<$agent_version>, I<$agent_interval>, I<$dbh>, [I<$timezone_offset>], [I<$longitude>], [I<$latitude>], [I<$altitude>], [I<$position_description>], [I<$parent_agent_name>]) >> Update last contact, timezone fields in B and current position (this can affect B and B). If the I<$parent_agent_id> is @@ -2235,18 +2235,27 @@ sub pandora_create_group ($$$$$$$$$) { } ########################################################################## -=head2 C<< pandora_create_agent (I<$pa_config>, I<$server_name>, I<$agent_name>, I<$address>, I<$group_id>, I<$parent_id>, I<$os_id>, I<$description>, I<$interval>, I<$dbh>, [I<$timezone_offset>], [I<$longitude>], [I<$latitude>], [I<$altitude>], [I<$position_description>]) >> +=head2 C<< pandora_create_agent (I<$pa_config>, I<$server_name>, I<$agent_name>, I<$address>, I<$group_id>, I<$parent_id>, I<$os_id>, I<$description>, I<$interval>, I<$dbh>, [I<$timezone_offset>], [I<$longitude>], [I<$latitude>], [I<$altitude>], [I<$position_description>], [I<$custom_id>], [I<$url_address>]) >> Create a new entry in B optionaly with position information =cut ########################################################################## -sub pandora_create_agent ($$$$$$$$$$;$$$$$) { +sub pandora_create_agent ($$$$$$$$$$;$$$$$$$) { my ($pa_config, $server_name, $agent_name, $address, $group_id, $parent_id, $os_id, $description, $interval, $dbh, $timezone_offset, - $longitude, $latitude, $altitude, $position_description) = @_; + $longitude, $latitude, $altitude, $position_description, + $custom_id, $url_address) = @_; + if (!defined($custom_id)) { + $custom_id = ''; + } + + if (!defined($url_address)) { + $url_address = ''; + } + if (!defined($group_id)) { $group_id = $pa_config->{'autocreate_group'}; @@ -2257,13 +2266,13 @@ sub pandora_create_agent ($$$$$$$$$$;$$$$$) { my $agent_id; # Test if the optional positional parameters are defined or GIS is disabled if (!defined ($timezone_offset) ) { - $agent_id = db_insert ($dbh, 'id_agente', 'INSERT INTO tagente (nombre, direccion, comentarios, id_grupo, id_os, server_name, intervalo, id_parent, modo) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, 1)', safe_input($agent_name), $address, $description, $group_id, $os_id, $server_name, $interval, $parent_id); + $agent_id = db_insert ($dbh, 'id_agente', 'INSERT INTO tagente (nombre, direccion, comentarios, id_grupo, id_os, server_name, intervalo, id_parent, modo, custom_id, url_address) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, 1, ?, ?)', safe_input($agent_name), $address, $description, $group_id, $os_id, $server_name, $interval, $parent_id, $custom_id, $url_address); } else { $agent_id = db_insert ($dbh, 'id_agente', 'INSERT INTO tagente (nombre, direccion, comentarios, id_grupo, id_os, server_name, intervalo, id_parent, - timezone_offset, modo ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 1)', safe_input($agent_name), $address, - $description, $group_id, $os_id, $server_name, $interval, $parent_id, $timezone_offset); + timezone_offset, modo, custom_id, url_address) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 1, ?, ?)', safe_input($agent_name), $address, + $description, $group_id, $os_id, $server_name, $interval, $parent_id, $timezone_offset, $custom_id, $url_address); } if (defined ($longitude) && defined ($latitude ) && $pa_config->{'activate_gis'} == 1 ) { if (!defined($altitude)) { @@ -2273,7 +2282,7 @@ sub pandora_create_agent ($$$$$$$$$$;$$$$$) { my $utimestamp = time (); my $timestamp = strftime ("%Y-%m-%d %H:%M:%S", localtime ($utimestamp)); - save_agent_position($pa_config, $longitude, $latitude, $altitude, $agent_id, $dbh, $timestamp,$position_description) ; + save_agent_position($pa_config, $longitude, $latitude, $altitude, $agent_id, $dbh, $timestamp, $position_description) ; } logger ($pa_config, "Server '$server_name' CREATED agent '$agent_name' address '$address'.", 10); diff --git a/pandora_server/lib/PandoraFMS/DataServer.pm b/pandora_server/lib/PandoraFMS/DataServer.pm index f4068de896..1816f7e4aa 100644 --- a/pandora_server/lib/PandoraFMS/DataServer.pm +++ b/pandora_server/lib/PandoraFMS/DataServer.pm @@ -179,9 +179,10 @@ sub data_consumer ($$) { sub process_xml_data ($$$$$) { my ($pa_config, $file_name, $data, $server_id, $dbh) = @_; - my ($agent_name, $agent_version, $timestamp, $interval, $os_version, $timezone_offset) = + my ($agent_name, $agent_version, $timestamp, $interval, $os_version, $timezone_offset, $custom_id, $url_address) = ($data->{'agent_name'}, $data->{'version'}, $data->{'timestamp'}, - $data->{'interval'}, $data->{'os_version'}, $data->{'timezone_offset'}); + $data->{'interval'}, $data->{'os_version'}, $data->{'timezone_offset'}, + $data->{'custom_id'}, $data->{'url_address'}); # Timezone offset must be an integer beween -12 and +12 if (!defined($timezone_offset) || $timezone_offset !~ /[-+]?[0-9,11,12]/) { @@ -330,18 +331,51 @@ sub process_xml_data ($$$$$) { # Create the agent if ($valid_position_data == 1 && $pa_config->{'activate_gis'} != 0 ) { logger($pa_config, "Creating agent $agent_name at long: $longitude lat: $latitude alt: $altitude", 5); - $agent_id = pandora_create_agent($pa_config, $pa_config->{'servername'}, $agent_name, $address, $group_id, $parent_id, $os, - $description, $interval, $dbh, $timezone_offset, $longitude, $latitude, $altitude, $position_description); } else { # Ignore agent positional data logger($pa_config, "Creating agent $agent_name", 5); - $agent_id = pandora_create_agent($pa_config, $pa_config->{'servername'}, $agent_name, $address, $group_id, $parent_id, $os, - $description, $interval, $dbh, $timezone_offset); + $longitude = undef; + $latitude = undef; + $altitude = undef; + $position_description = undef; } + + $agent_id = pandora_create_agent($pa_config, $pa_config->{'servername'}, $agent_name, $address, $group_id, $parent_id, $os, + $description, $interval, $dbh, $timezone_offset, $longitude, $latitude, $altitude, $position_description, $custom_id, $url_address); + if (! defined ($agent_id)) { $AgentSem->up (); return; } + + # Process custom fields + if(defined($data->{'custom_fields'})) { + foreach my $custom_fields (@{$data->{'custom_fields'}}) { + foreach my $custom_field (@{$custom_fields->{'field'}}) { + my $cf_name = get_tag_value ($custom_field, 'name', ''); + logger($pa_config, "Processing custom field '" . $cf_name . "'", 10); + + # Check if the custom field exists + my $custom_field_info = get_db_single_row ($dbh, 'SELECT * FROM tagent_custom_fields WHERE name = ?', safe_input($cf_name)); + + # If it exists add the value to the agent + if (defined ($custom_field_info)) { + my $cf_value = get_tag_value ($custom_field, 'value', ''); + + my $field_agent; + + $field_agent->{'id_agent'} = $agent_id; + $field_agent->{'id_field'} = $custom_field_info->{'id_field'}; + $field_agent->{'description'} = $cf_value; + + db_process_insert($dbh, 'id_field', 'tagent_custom_data', $field_agent); + } + else { + logger($pa_config, "The custom field '" . $cf_name . "' does not exist. Discarded from XML", 5); + } + } + } + } } $AgentSem->up ();