diff --git a/pandora_server/ChangeLog b/pandora_server/ChangeLog index 46ebf1265e..39eb4ff13c 100644 --- a/pandora_server/ChangeLog +++ b/pandora_server/ChangeLog @@ -1,3 +1,16 @@ +2009-01-15 Ramon Novoa + + * lib/PandoraFMS/DB.pm: Partially rewrote the alert core to support the + new DB schemas. Compound alerts have been temporarily disabled. + Rewrote alert macro substitution code. + + * lib/PandoraFMS/Tools.pm, + bin/pandora_snmpconsole: Aesthetic changes to enterprise code. + + * lib/PandoraFMS/Config.pm, + bin/pandora_network: Added support for UDP multicast status reports + (enterprise version). + 2009-01-14 Sancho Lerena * DB.pm: Solved an invalid reference to hash variable affecting to diff --git a/pandora_server/bin/pandora_network b/pandora_server/bin/pandora_network index 996a7a8de1..bcf0e09ab9 100755 --- a/pandora_server/bin/pandora_network +++ b/pandora_server/bin/pandora_network @@ -37,6 +37,7 @@ use threads::shared; use PandoraFMS::Config; use PandoraFMS::Tools; use PandoraFMS::DB; +enterprise_load (); # FLUSH in each IO (only for debug, very slooow) # ENABLED in DEBUGMODE @@ -108,6 +109,7 @@ my $dbh = DBI->connect("DBI:mysql:$dbname:$dbhost:3306", while (1) { pandora_serverkeepaliver (\%pa_config, 1, $dbh); + enterprise_hook('mcast_status_report', [\%pa_config, $dbh]); threads->yield; sleep ($pa_config{"server_threshold"}); } diff --git a/pandora_server/bin/pandora_snmpconsole b/pandora_server/bin/pandora_snmpconsole index 13ce0ffe72..02a9182489 100755 --- a/pandora_server/bin/pandora_snmpconsole +++ b/pandora_server/bin/pandora_snmpconsole @@ -30,7 +30,7 @@ use Time::HiRes; # For high precission timedate functions (Net::Ping) use PandoraFMS::Config; use PandoraFMS::Tools; use PandoraFMS::DB; -PandoraFMS::Tools::enterprise_load (); +enterprise_load (); # FLUSH in each IO (only for debug, very slooow) # ENABLED in DEBUGMODE @@ -118,7 +118,7 @@ sub pandora_snmptrapd { # open database, only ONCE. We pass reference to DBI handler ($dbh) to all subprocess my $dbh = DBI->connect("DBI:mysql:$pa_config->{'dbname'}:$pa_config->{'dbhost'}:3306",$pa_config->{'dbuser'}, $pa_config->{'dbpass'}, { RaiseError => 1, AutoCommit => 1 }); - my $trap2agent = PandoraFMS::Tools::enterprise_hook('snmp_get_trap2module', [$dbh]); + my $trap2agent = enterprise_hook('snmp_get_trap2agent', [$dbh]); # Main loop for reading file while ( 1 ){ @@ -158,7 +158,7 @@ sub pandora_snmptrapd { $custom_value=$type_desc; # Bug fixed, 080108 by anonymous } - if (! defined(PandoraFMS::Tools::enterprise_hook ('snmp_insert_trap', [$pa_config, $source, $oid, $type, $value, $custom_oid, $custom_value, $custom_type, $timestamp, $dbh]))) { + if (! defined(enterprise_hook ('snmp_insert_trap', [$pa_config, $source, $oid, $type, $value, $custom_oid, $custom_value, $custom_type, $timestamp, $dbh]))) { $sql_insert = "INSERT INTO ttrap (timestamp, source, oid, type, value, oid_custom, value_custom, type_custom) VALUES ('$timestamp', '$source', '$oid', $type, '$value', '$custom_oid', '$custom_value', '$custom_type')"; logger ($pa_config,"Received SNMP Trap from $source", 4); eval { @@ -171,7 +171,7 @@ sub pandora_snmptrapd { logger ($pa_config, "[ERROR] SQL Errorcode: $@", 2); } } - PandoraFMS::Tools::enterprise_hook ('snmp_trap2agent', [defined($trap2agent) ? $trap2agent : 0, $pa_config, $source, $oid, $value, $custom_oid, $custom_value, $timestamp, $dbh]); + enterprise_hook ('snmp_trap2agent', [defined($trap2agent) ? $trap2agent : 0, $pa_config, $source, $oid, $value, $custom_oid, $custom_value, $timestamp, $dbh]); } } sleep ($pa_config{'server_threshold'}); diff --git a/pandora_server/lib/PandoraFMS/Config.pm b/pandora_server/lib/PandoraFMS/Config.pm index c3fde45499..815da52b65 100644 --- a/pandora_server/lib/PandoraFMS/Config.pm +++ b/pandora_server/lib/PandoraFMS/Config.pm @@ -203,6 +203,10 @@ sub pandora_loadconfig { # max log size (bytes) $pa_config->{'max_log_size'} = 1048576; # 1MB by default + # Multicast status report + $pa_config->{'mcast_group'} = '226.1.1.2'; + $pa_config->{'mcast_port'} = '2000'; + # Check for UID0 if ($pa_config->{"quiet"} != 0){ if ($> == 0){ @@ -413,6 +417,12 @@ sub pandora_loadconfig { } elsif ($parametro =~ m/^max_log_size\s([0-9]*)/i) { $pa_config->{'max_log_size'}= clean_blank($1); + } + elsif ($parametro =~ m/^mcast_group\s([0-9\.]*)/i) { + $pa_config->{'mcast_group'}= clean_blank($1); + } + elsif ($parametro =~ m/^mcast_port\s([0-9]*)/i) { + $pa_config->{'mcast_port'}= clean_blank($1); } } # end of loop for parameter # diff --git a/pandora_server/lib/PandoraFMS/DB.pm b/pandora_server/lib/PandoraFMS/DB.pm index 10b26220c2..e029fa9144 100644 --- a/pandora_server/lib/PandoraFMS/DB.pm +++ b/pandora_server/lib/PandoraFMS/DB.pm @@ -29,6 +29,7 @@ use HTML::Entities; use POSIX qw(strtod); use PandoraFMS::Tools; +enterprise_load (); require Exporter; @@ -79,16 +80,32 @@ our @EXPORT = qw( get_db_value get_db_free_row + get_db_all_rows get_db_free_field db_insert - db_update - db_delete + db_do ); # Spanish translation note: # 'Crea' in spanish means 'create' # 'Dame' in spanish means 'give' +########################################################################## +## SUB subst_alert_macros (string, macros) { +## Searches string for macros and substitutes them with their values. +########################################################################## + +sub subst_alert_macros ($\%) { + my $string = $_[0]; + my %macros = %{$_[1]}; + + while ((my $macro, my $value) = each (%macros)) { + $string =~ s/($macro)/$value/ig; + } + + return $string; +} + ########################################################################## ## SUB pandora_generate_alerts ## (paconfig, timestamp, agent_name, $id_agent, id_agent_module, @@ -113,17 +130,13 @@ sub pandora_generate_alerts (%$$$$$$$$) { } # Get enabled alerts associated with this module - my $query_alert = "SELECT * FROM talerta_agente_modulo WHERE - id_agente_modulo = '$id_agent_module' AND disable = 0"; - my $handle_alert = $dbh->prepare($query_alert); - - $handle_alert->execute; - if ($handle_alert->rows == 0) { - return; - } - - while (my $alert_data = $handle_alert->fetchrow_hashref()) { + my @alerts = get_db_all_rows ("SELECT talert_template_modules.id as id_template_module, talert_template_modules.*, talert_templates.* + FROM talert_template_modules, talert_templates + WHERE talert_template_modules.id_alert_template = talert_templates.id + AND id_agent_module = $id_agent_module + AND disabled = 0", $dbh); + foreach my $alert_data (@alerts) { my $rc = pandora_evaluate_alert($pa_config, $timestamp, $alert_data, $module_data, $id_module_type, $dbh); pandora_process_alert ($pa_config, $timestamp, $rc, $agent_name, @@ -132,13 +145,11 @@ sub pandora_generate_alerts (%$$$$$$$$) { # Evaluate compound alerts even if the alert status did not change in # case the compound alert does not recover - pandora_generate_compound_alerts ($pa_config, $timestamp, - $agent_name, $id_agent, - $alert_data->{'id_aam'}, - $id_group, 0, $dbh); + #pandora_generate_compound_alerts ($pa_config, $timestamp, + # $agent_name, $id_agent, + # $alert_data->{'id_aam'}, + # $id_group, 0, $dbh); } - - $handle_alert->finish(); } ########################################################################## @@ -178,8 +189,7 @@ sub pandora_evaluate_alert (%$%$$$) { } # Check time threshold - my $last_fired_date = ParseDate($alert_data->{'last_fired'}); - my $limit_date = DateCalc ($last_fired_date, "+ " . + my $limit_date = DateCalc (ParseDateString("epoch " . $alert_data->{'last_reference'}), "+ " . $alert_data->{'time_threshold'} . " seconds", \$err); my $date = ParseDate($timestamp); @@ -204,26 +214,33 @@ sub pandora_evaluate_alert (%$%$$$) { } # Check for valid data - if ($id_module_type == 3 || - $id_module_type == 10 || - $id_module_type == 17) { - if ($module_data !~ m/$alert_data->{'alert_text'}/i) { - return $status; - } + if ($alert_data->{'type'} eq "min" && $module_data >= $alert_data->{'min_value'}) { + return $status; } - elsif ($id_module_type == -1) { - if (pandora_evaluate_compound_alert($pa_config, - $alert_data->{'id_aam'}, - $dbh) == 0) { - return $status - } + elsif ($alert_data->{'type'} eq "max" && $module_data <= $alert_data->{'max_value'}) { + return $status; } - else { - if ($module_data <= $alert_data->{'dis_max'} && - $module_data >= $alert_data->{'dis_min'}) { - return $status; - } + elsif ($alert_data->{'type'} eq "max_min" + && $module_data >= $alert_data->{'min_value'} + && $module_data <= $alert_data->{'max_value'}) { + return $status; } + elsif ($alert_data->{'type'} eq "equal" && $module_data == $alert_data->{'value'}) { + return $status; + } + elsif ($alert_data->{'type'} eq "not_equal" && $module_data != $alert_data->{'value'}) { + return $status; + } + elsif ($alert_data->{'type'} eq "regex" && $module_data !~ m/$alert_data->{'alert_text'}/i) { + return $status; + } + + #if ($id_module_type == -1) { + #if (pandora_evaluate_compound_alert($pa_config, + # $alert_data->{'id_aam'}, + # $dbh) == 0) { + # return $status + #} # Check min and max alert limits if (($alert_data->{'internal_counter'} < $alert_data->{'min_alerts'}) || @@ -261,15 +278,18 @@ sub pandora_process_alert (%$$$$$%$$) { if ($rc == 3) { # Update alert status - $dbh->do("UPDATE talerta_agente_modulo SET times_fired = 0, - internal_counter = 0 WHERE id_aam = " . - $alert_data->{'id_aam'}); + db_do("UPDATE talert_template_modules SET times_fired = 0, + internal_counter = 0 WHERE id = " . + $alert_data->{'id_template_module'}, $dbh); # Generate an event pandora_event ($pa_config, "Alert ceased (" . $alert_data->{'descripcion'} . ")", $id_group, - $id_agent, $alert_data->{'priority'}, $alert_data->{'id_aam'}, $alert_data->{'id_agente_modulo'}, + $id_agent, $alert_data->{'priority'}, $alert_data->{'id_template_module'}, $alert_data->{'id_agent_module'}, "alert_recovered", $dbh); + + # Send a status change report + enterprise_hook('pandora_mcast_change_report', [$pa_config, $alert_data->{'id_agent_module'}, $timestamp, 'OK', $dbh]); return; } @@ -277,47 +297,62 @@ sub pandora_process_alert (%$$$$$%$$) { if ($rc == 4) { # Update alert status - $dbh->do("UPDATE talerta_agente_modulo SET times_fired = 0, - internal_counter = 0 WHERE id_aam = " . - $alert_data->{'id_aam'}); + db_do("UPDATE talert_template_modules SET times_fired = 0, + internal_counter = 0 WHERE id = " . + $alert_data->{'id_template_module'}, $dbh); execute_alert ($pa_config, $alert_data, $id_agent, $id_group, $agent_name, $module_data, 0, $dbh); + # Send a status change report + enterprise_hook('pandora_mcast_change_report', [$pa_config, $alert_data->{'id_agent_module'}, $timestamp, 'OK', $dbh]); return; } + # Get current date + my $date_db = &UnixDate("today","%s"); + # Increment internal counter if ($rc == 2) { + my $new_interval = ""; + + # Start a new interval + if ($alert_data->{'internal_counter'} == 0) { + $new_interval = ", last_reference = $date_db"; + } # Update alert status $alert_data->{'internal_counter'} += 1; # Do not increment times_fired, but set it in case the alert was reset - $dbh->do("UPDATE talerta_agente_modulo SET times_fired = " . + db_do("UPDATE talert_template_modules SET times_fired = " . $alert_data->{'times_fired'} . ", internal_counter = " . - $alert_data->{'internal_counter'} . " WHERE id_aam = " . - $alert_data->{'id_aam'}); + $alert_data->{'internal_counter'} . $new_interval . + " WHERE id = " . $alert_data->{'id_template_module'}, $dbh); return; } # Execute if ($rc == 0) { + my $new_interval = ""; - # Get current date - my $date_db = &UnixDate("today","%Y-%m-%d %H:%M:%S"); + # Start a new interval + if ($alert_data->{'internal_counter'} == 0) { + $new_interval .= ", last_reference = $date_db"; + } # Update alert status - $alert_data->{'times_fired'} += 1; $alert_data->{'internal_counter'} += 1; - $dbh->do("UPDATE talerta_agente_modulo SET times_fired = " . - $alert_data->{'times_fired'} . ", last_fired = - '$date_db', internal_counter = " . - $alert_data->{'internal_counter'} . " WHERE id_aam = " . - $alert_data->{'id_aam'}); + + db_do("UPDATE talert_template_modules SET times_fired = " . + $alert_data->{'times_fired'} . ", last_fired = $date_db, internal_counter = " . + $alert_data->{'internal_counter'} . $new_interval . " WHERE id = " . $alert_data->{'id_template_module'}, $dbh); execute_alert ($pa_config, $alert_data, $id_agent, $id_group, $agent_name, $module_data, 1, $dbh); + + # Send a status change report + enterprise_hook('pandora_mcast_change_report', [$pa_config, $alert_data->{'id_agent_module'}, $timestamp, 'ERR', $dbh]); return; } } @@ -470,110 +505,96 @@ sub pandora_generate_compound_alerts (%$$$$$$$) { sub execute_alert (%$$$$$$$$$$$$$$$) { my $pa_config = $_[0]; - my $data_alert = $_[1]; + my $alert = $_[1]; my $id_agent = $_[2]; my $id_group = $_[3]; my $agent = $_[4]; my $data = $_[5]; - my $alert_mode = $_[6]; # 0 is recovery, 1 is normal + my $alert_mode = $_[6]; # 0 recovery, 1 normal my $dbh = $_[7]; - - # Some variable init - - my $create_event = 1; - my $command = ""; - my $alert_name = ""; - my $field1; - my $field2; - my $field3; - my $id_alert = $data_alert->{'id_alerta'}; - my $id_agent_module = $data_alert->{'id_agente_modulo'}; - my $timestamp = &UnixDate ("today", "%Y-%m-%d %H:%M:%S"); # string timestamp - my $alert_description = $data_alert->{'descripcion'}; - # Compound only - if ($id_alert == 1){ + # Get active actions/commands + my @actions = get_db_all_rows ("SELECT * FROM talert_template_module_actions, talert_actions, talert_commands + WHERE talert_template_module_actions.id_alert_action = talert_actions.id + AND talert_actions.id_alert_command = talert_commands.id + AND talert_template_module_actions.id_alert_template_module = " . + $alert->{'id_template_module'} . + " AND ((fires_min = 0 AND fires_max = 0) + OR (" . $alert->{'times_fired'} . " >= fires_min AND " . $alert->{'times_fired'} . " <= fires_max))", $dbh); + + if ($#actions < 0) { return; } - - if ($alert_mode == 1){ - $field1 = $data_alert->{'al_campo1'}; - $field2 = $data_alert->{'al_campo2'}; - $field3 = $data_alert->{'al_campo3'}; - } else { - $field1 = $data_alert->{'al_campo1'}; - # Patch for adding [RECOVER] on f2/f3 if blank. Submitted by Kato Atsushi - $field2 = $data_alert->{'al_f2_recovery'} || "[RECOVER]" . $data_alert->{'al_campo2'}; - $field3 = $data_alert->{'al_f3_recovery'} || "[RECOVER]" . $data_alert->{'al_campo3'}; - # End of patch - } - # Get values for commandline, reading from talerta. - my $query_idag = "SELECT * FROM talerta WHERE id_alerta = '$id_alert'"; - my $idag = $dbh->prepare($query_idag); - $idag ->execute; - my @datarow; - if ($idag->rows != 0) { - while (@datarow = $idag->fetchrow_array()) { - $command = $datarow[2]; - $alert_name = $datarow[1]; + # Execute actions + foreach my $action (@actions) { + $field1 = $action->{'field1'} ne "" ? $action->{'field1'} : $alert->{'field1'}; + $field2 = $action->{'field2'} ne "" ? $action->{'field2'} : $alert->{'field2'}; + $field3 = $action->{'field3'} ne "" ? $action->{'field3'} : $alert->{'field3'}; + + # Recovery fields, thanks to Kato Atsushi + if ($alert_mode == 0){ + $field2 = $alert->{'field2_recovery'} ne "" ? $alert->{'field2_recovery'} : "[RECOVER]" . $field2; + $field2 = $alert->{'field3_recovery'} ne "" ? $alert->{'field2_recovery'} : "[RECOVER]" . $field3; } - } - $idag->finish(); - - logger($pa_config, "Alert ($alert_name) TRIGGERED for $agent",2); - if ($id_alert > 4) { # Skip internal alerts - $command =~ s/_field1_/"$field1"/ig; - $command =~ s/_field2_/"$field2"/ig; - $command =~ s/_field3_/"$field3"/ig; - $command =~ s/_agent_/$agent/ig; - $command =~ s/_timestamp_/$timestamp/ig; - $command =~ s/_data_/$data/ig; - # Clean up some "tricky" characters - $command = decode_entities($command); - # EXECUTING COMMAND !!! - eval { - my $exit_value = system ($command); - $exit_value = $? >> 8; # Shift 8 bits to get a "classic" errorlevel - if ($exit_value != 0) { - logger($pa_config, "Executed command for triggered alert '$alert_name' had errors (errorlevel =! 0) ",1); - logger($pa_config, "Executed command was $command ",5); + + # Alert macros + my %macros = (_field1_ => $field1, + _field2_ => $field2, + _field3_ => $field3, + _agent_ => $agent, + _timestamp_ => &UnixDate ("today", "%Y-%m-%d %H:%M:%S"), + _data_ => $data, + _alert_description_ => $alert->{'descripcion'}, + _alert_threshold_ => $alert->{'time_threshold'}, + _alert_times_fired_ => $alert->{'times_fired'}, + _module_name_ => $module_name, + ); + + logger($pa_config, "Alert (" . $alert->{'name'} . ") executed for agent $agent", 2); + + # User defined alerts + if ($action->{'internal'} == 0) { + $command = subst_alert_macros ($action->{'command'}, %macros); + $command = decode_entities($command); + eval { + system ($command); + $rc = $? >> 8; # Shift 8 bits to get a "classic" errorlevel + if ($rc != 0) { + logger($pa_config, "Executed command for alert " . $alert->{'name'} . " returned with errorlevel $rc", 1); + } + }; + + if ($@){ + logger($pa_config, "Error $@ executing command $command", 0); } - }; - if ($@){ - logger($pa_config, "WARNING: Alert command don't return from execution. ( $command )", 0 ); - logger($pa_config, "ERROR Code: $@",2); - } - } elsif ($id_alert == 3) { # id_alerta = 3, is a internal system audit - logger($pa_config, "Internal audit lauch for agent name $agent",3); - $field1 =~ s/_agent_/$agent/ig; - $field1 =~ s/_timestamp_/$timestamp/ig; - $field1 =~ s/_data_/$data/ig; - pandora_audit ($pa_config, $field1, $agent, "Alert ($alert_description)", $dbh); - $create_event = 0; - } elsif ($id_alert == 2) { # email - - $field2 =~ s/_agent_/$agent/ig; - $field2 =~ s/_timestamp_/$timestamp/ig; - $field2 =~ s/_data_/$data/ig; - - $field3 =~ s/_agent_/$agent/ig; - $field3 =~ s/_timestamp_/$timestamp/ig; - $field3 =~ s/_data_/$data/ig; - - pandora_sendmail ( $pa_config, $field1, $field2, $field3); - } elsif ($id_alert == 4) { # internal event - $create_event = 1; - } - - if ($create_event == 1){ - my $evt_descripcion = "Alert fired ($alert_description)"; - if ($alert_mode == 0){ # recovery - pandora_event ($pa_config, $evt_descripcion, $id_group, $id_agent, $data_alert->{'priority'}, $data_alert->{'id_aam'}, - $data_alert->{'id_agente_modulo'}, 'alert_recovered', $dbh); + # Internal Audit + } elsif ($action->{'name'} eq "Internal Audit") { + logger($pa_config, "Internal audit for agent $agent", 3); + $field1 = subst_alert_macros ($field1, %macros); + pandora_audit ($pa_config, $field1, $agent, "Alert ($alert_description)", $dbh); + + # Return without creating an event + return; + # Email + } elsif ($action->{'name'} eq "eMail") { + $field2 = subst_alert_macros ($field2, %macros); + $field3 = subst_alert_macros ($field3, %macros); + pandora_sendmail ($pa_config, $field1, $field2, $field3); + # Internal event + } elsif ($action->{'name'} eq "Pandora FMS Event") { + # Unknown } else { - pandora_event ($pa_config, $evt_descripcion, $id_group, $id_agent, $data_alert->{'priority'}, $data_alert->{'id_aam'}, - $data_alert->{'id_agente_modulo'}, 'alert_fired', $dbh); + return; + } + + # Create an event + if ($alert_mode == 0){ + pandora_event ($pa_config, "Alert recovered (" . $alert->{'description'} . ")", $id_group, $id_agent, $alert->{'priority'}, $alert->{'id_template_module'}, + $alert->{'id_agent_module'}, 'alert_recovered', $dbh); + } else { + pandora_event ($pa_config, "Alert fired (" . $alert->{'description'} . ")", $id_group, $id_agent, $alert->{'priority'}, $alert->{'id_template_module'}, + $alert->{'id_agent_module'}, 'alert_fired', $dbh); } } } @@ -747,11 +768,11 @@ sub pandora_writestate (%$$$$$$$) { if ($event_type eq "going_down_warning"){ # Clean up and system mark all active CRITICAL events for this module - db_update ("UPDATE tevento SET estado=1 WHERE id_agentmodule = ".$module_data->{'id_agente_modulo'}." AND event_type = 'going_up_critical'", $dbh); + db_do ("UPDATE tevento SET estado=1 WHERE id_agentmodule = ".$module_data->{'id_agente_modulo'}." AND event_type = 'going_up_critical'", $dbh); } elsif ($event_type eq "going_down_normal"){ # Clean up and system mark all active WARNING and CRITICAL events for this module - db_update ("UPDATE tevento SET estado=1 WHERE id_agentmodule = ".$module_data->{'id_agente_modulo'}." AND (event_type = 'going_up_warning' OR event_type = 'going_down_warning' OR event_type = 'going_up_critical')", $dbh); + db_do ("UPDATE tevento SET estado=1 WHERE id_agentmodule = ".$module_data->{'id_agente_modulo'}." AND (event_type = 'going_up_warning' OR event_type = 'going_down_warning' OR event_type = 'going_up_critical')", $dbh); } } } @@ -763,7 +784,7 @@ sub pandora_writestate (%$$$$$$$) { $query_act = "UPDATE tagente_estado SET utimestamp = $utimestamp, datos = $datos, last_status = " . $data_status->{'last_status'} . ", status_changes = " . $data_status->{'status_changes'} . ", timestamp = '$timestamp', estado = ".$data_status->{'estado'}.", id_agente = $module_data->{'id_agente'}, current_interval = $module_interval, running_by = $id_server, last_execution_try = $utimestamp " . $needs_update_sql . " WHERE id_agente_modulo = ". $module_data->{'id_agente_modulo'}; } - db_update ($query_act, $dbh); + db_do ($query_act, $dbh); } ########################################################################## @@ -1511,7 +1532,7 @@ sub pandora_lastagentcontact (%$$$$$$) { } logger( $pa_config, "pandora_lastagentcontact: Updating Agent last contact data for $nombre_agente",6); logger( $pa_config, "pandora_lastagentcontact: SQL Query: ".$query, 10); - db_update ($query, $dbh); + db_do ($query, $dbh); } @@ -2161,6 +2182,30 @@ sub get_db_free_row ($$) { return -1; } +# --------------------------------------------------------------- +# Free SQL sentence. Return all rows as a hash array. +# --------------------------------------------------------------- + +sub get_db_all_rows ($$) { + my $condition = $_[0]; + my $dbh = $_[1]; + my @result; + + my $s_idag = $dbh->prepare($condition); + if (!$s_idag->execute) { + return @result; + } + + if ($s_idag->rows == 0) { + return @result; + } + + while (my $row = $s_idag->fetchrow_hashref()) { + push (@result, $row); + } + $s_idag->finish(); + return @result; +} # --------------------------------------------------------------- # Insert SQL sentence. Returns ID of row inserted @@ -2174,31 +2219,17 @@ sub db_insert ($$) { return $dbh->{'mysql_insertid'}; } - # --------------------------------------------------------------- -# UPDATE SQL sentence. +# Generic SQL sentence. # --------------------------------------------------------------- -sub db_update ($$) { +sub db_do ($$) { my $query= $_[0]; my $dbh = $_[1]; $dbh->do($query); } -# --------------------------------------------------------------- -# DELETE SQL sentence. -# --------------------------------------------------------------- - -sub db_delete ($$) { - my $query= $_[0]; - my $dbh = $_[1]; - - $dbh->do($query); -} - - - ########################################################################## # SUB pandora_create_agent (pa_config, dbh, target_ip, target_ip_id, # id_group, network_server_assigned, name, id_os) diff --git a/pandora_server/lib/PandoraFMS/Tools.pm b/pandora_server/lib/PandoraFMS/Tools.pm index cac7e477fe..2cba1caff6 100644 --- a/pandora_server/lib/PandoraFMS/Tools.pm +++ b/pandora_server/lib/PandoraFMS/Tools.pm @@ -41,6 +41,8 @@ our @EXPORT = qw( pandora_sendmail pandora_get_os pandora_trash_ascii + enterprise_hook + enterprise_load ); ##########################################################################