diff --git a/pandora_console/include/functions_html.php b/pandora_console/include/functions_html.php index 4a40c742d4..0f339b288d 100644 --- a/pandora_console/include/functions_html.php +++ b/pandora_console/include/functions_html.php @@ -808,6 +808,8 @@ function print_table (&$table, $return = false) { echo $output; } + + /** * Render a radio button input. Extended version, use print_radio_button() to simplify. * diff --git a/pandora_console/operation/agentes/datos_agente.php b/pandora_console/operation/agentes/datos_agente.php index 42ea3a0727..1530d72fe2 100644 --- a/pandora_console/operation/agentes/datos_agente.php +++ b/pandora_console/operation/agentes/datos_agente.php @@ -21,9 +21,8 @@ require_once ("include/config.php"); check_login(); $module_id = get_parameter_get ("id", 0); -$period = get_parameter ("period", 86400); +$period = get_parameter_get ("period", 86400); $group = get_agentmodule_group ($module_id); -$agentId = get_parameter('id_agente'); if (! give_acl ($config['id_user'], $group, "AR") || $module_id == 0) { audit_db ($config['id_user'], $REMOTE_ADDR, "ACL Violation", @@ -36,117 +35,191 @@ if (isset ($_GET["delete"])) { $delete = get_parameter_get ("delete", 0); $sql = sprintf ("DELETE FROM tagente_datos WHERE id_agente_datos = %d", $delete); process_sql ($sql); -} elseif (isset($_GET["delete_text"])) { +} elseif (isset($_GET["delete_log4x"])) { + $delete = get_parameter_get ("delete_log4x", 0); + $sql = sprintf ("DELETE FROM tagente_datos_log4x WHERE id_tagente_datos_log4x = %d", $delete); + process_sql ($sql); +} elseif (isset($_GET["delete_string"])) { $delete = get_parameter_get ("delete_string", 0); $sql = sprintf ("DELETE FROM tagente_datos_string WHERE id_tagente_datos_string = %d", $delete); process_sql ($sql); } -// Different query for string data type -if (preg_match ("/string/", get_moduletype_name (get_agentmodule_type ($module_id)))) { - $sql = sprintf ("SELECT * - FROM tagente_datos_string - WHERE id_agente_modulo = %d AND utimestamp > %d - ORDER BY utimestamp DESC - LIMIT %d OFFSET %d", $module_id, get_system_time () - $period, $config['block_size'], get_parameter ('offset')); - $sqlCount = sprintf ("SELECT COUNT(*) FROM tagente_datos_string WHERE id_agente_modulo = %d AND utimestamp > %d ORDER BY utimestamp DESC", $module_id, get_system_time () - $period); - $string_type = 1; +$table->cellpadding = 3; +$table->cellspacing = 3; +$table->width = 600; +$table->class = "databox"; +$table->head = array (); +$table->data = array (); +$table->align = array (); +$table->size = array (); + + +$moduletype_name = get_moduletype_name (get_agentmodule_type ($module_id)); + +$offset = (int) get_parameter("offset"); +$block_size = (int) $config["block_size"]; + +// The "columns" array is the number(and definition) of columns in the report: +// $columns = array( +// "COLUMN1" => array(ROW_FROM_DB_TABLE, FUNCTION_NAME_TO_FORMAT_THE_DATA, "align"=>COLUMN_ALIGNMENT, "width"=>COLUMN_WIDTH) +// "COLUMN2" => array(ROW_FROM_DB_TABLE, FUNCTION_NAME_TO_FORMAT_THE_DATA, "align"=>COLUMN_ALIGNMENT, "width"=>COLUMN_WIDTH) +// .... +// ) +// +// For each row from the query, and for each column, we'll call the FUNCTION passing as argument +// the value of the ROW. +// +$columns = array (); + +if ($moduletype_name == "log4x") { + $table->width = 1100; + + $sql_body = sprintf ("FROM tagente_datos_log4x WHERE id_agente_modulo = %d AND utimestamp > %d ORDER BY utimestamp DESC", $module_id, get_system_time () - $period); + + $columns = array( + "Delete" => array("id_tagente_datos_log4x", "format_delete_log4x", "align" => "center", "width" => "10px"), + "Timestamp" => array("utimestamp", "format_timestamp", "align" => "center", "width" => "200px"), + "Sev" => array("severity", "format_data", "align" => "center", "width" => "70px"), + "Message" => array("message", "format_verbatim", "align" => "left", "width" => "400px"), + "ST" => array("stacktrace", "format_data", "align" => "left", "width" => "400px") + ); + +} else if (preg_match ("/string/", $moduletype_name)) { + $sql_body = sprintf (" FROM tagente_datos_string WHERE id_agente_modulo = %d AND utimestamp > %d ORDER BY utimestamp DESC", $module_id, get_system_time () - $period); + + $columns = array( + "Delete" => array("id_agente_datos", "format_delete_string", "align" => "center"), + "Timestamp" => array("utimestamp", "format_timestamp", "align" => "center"), + "Data" => array("datos", "format_data", "align" => "center"), + "Time" => array("utimestamp", "format_time", "align" => "center") + ); } else { - $sql = sprintf ("SELECT * - FROM tagente_datos - WHERE id_agente_modulo = %d AND utimestamp > %d - ORDER BY utimestamp DESC - LIMIT %d OFFSET %d", $module_id, get_system_time () - $period, $config['block_size'], get_parameter ('offset')); - $sqlCount = sprintf ("SELECT COUNT(*) FROM tagente_datos WHERE id_agente_modulo = %d AND utimestamp > %d ORDER BY utimestamp DESC", $module_id, get_system_time () - $period); - $string_type = 0; + $sql_body = sprintf (" FROM tagente_datos WHERE id_agente_modulo = %d AND utimestamp > %d ORDER BY utimestamp DESC", $module_id, get_system_time () - $period); + + $columns = array( + "Delete" => array("id_agente_datos", "format_delete", "align" => "center"), + "Timestamp" => array("utimestamp", "format_timestamp", "align" => "center"), + "Data" => array("datos", "format_data", "align" => "center"), + "Time" => array("utimestamp", "format_time", "align" => "center") + ); } -$countData = get_db_value_sql($sqlCount); -$result = get_db_all_rows_sql ($sql, true); +$sql = "SELECT * " . $sql_body; +$sql_count = "SELECT count(*) " . $sql_body; + +$count = get_db_value_sql($sql_count); + +$sql .= " LIMIT " . $offset . "," . $block_size; + +$result = get_db_all_rows_sql ($sql); if ($result === false) { $result = array (); } echo "

".__('Received data from')." ".get_agentmodule_agent_name ($module_id)." / ".get_agentmodule_name ($module_id)."

"; -echo "

" . __("From the last") . " " . human_time_description ($period) ."

"; +echo "

".human_time_description ($period) ."

"; -echo "
"; -echo __("Choose a time from now") . ": "; -$intervals = array (); -$intervals[3600] = human_time_description_raw (3600); // 1 hour -$intervals[86400] = human_time_description_raw (86400); // 1 day -$intervals[604800] = human_time_description_raw (604800); // 1 week -$intervals[2592000] = human_time_description_raw (2592000); // 1 month -echo print_extended_select_for_time ($intervals, 'period', $period, 'this.form.submit();', '', '0', 10) . __(" seconds."); -echo "

"; +$index = 0; +foreach($columns as $col => $attr){ + $table->head[$index] = $col; + + if (isset($attr["align"])) + $table->align[$index] = $attr["align"]; + + if (isset($attr["width"])) + $table->size[$index] = $attr["width"]; -if ($result === false) { - echo '

'.__('There was a problem locating the source of the graph').'

'; + $index++; } -else { - pagination ($countData, false) ; - - echo ' - - - - - - - - '; - $count = 0; - foreach ($result as $row) { - - if (($count % 2) == 0) - $classPairOdd = 'rowPair'; - else - $classPairOdd = 'rowOdd'; - - if ($count > 100) break; - $count++; - - echo(''); - - if (give_acl ($config['id_user'], $group, "AW") ==1) { - if ($string_type == 0) { - echo(''); - } - else { - echo(''); - } - } - else { - echo(''); - } - - // This returns data with absolute user-defined timestamp format - // and numeric by data managed with 2 decimals, and not using Graph format - // (replacing 1000 by K and 1000000 by G, like version 2.x - - echo(''); - if (is_numeric ($row["datos"])) { - echo(''); - } - else { - echo(''); - } - echo(''); + +foreach ($result as $row) { + $data = array (); + + foreach($columns as $col => $attr){ + $data[] = $attr[1] ($row[$attr[0]]); } - echo '
' . __('Delete') . '' . __('Timestamp') . '' . __('Data') . '
- - - - ' . - date ($config["date_format"], $row["utimestamp"]) . - '' . - format_numeric($row["datos"],2) . - '' . - safe_input ($row["datos"]) . - '
'; - - pagination ($countData, false) ; - echo "

" . __('Total') . ' ' . $countData . ' ' . __('Data') . "

"; + + array_push ($table->data, $data); + if (count($table->data) > 200) break; +} + +if (empty ($table->data)) { + echo '

'.__('There was a problem locating the source of the graph').'

'; +} else { + pagination($count); + print_table ($table); + unset ($table); +} + +// +// This are functions to format the data +// + +function format_time($ts) +{ + return print_timestamp ($ts, true, array("prominent" => "comparation")); +} + +function format_data($data) +{ + if (is_numeric ($data)) { + $data = format_numeric($data, 2); + } else { + $data = safe_input ($data); + } + return $data; +} + +function format_verbatim($data) +{ + return "
" . $data . "
"; +} + +function format_timestamp($ts) +{ + global $config; + + // This returns data with absolute user-defined timestamp format + // and numeric by data managed with 2 decimals, and not using Graph format + // (replacing 1000 by K and 1000000 by G, like version 2.x + return date ($config["date_format"], $ts); +} + +function format_delete($id) +{ + global $period, $module_id, $config, $group; + + $txt = ""; + + if (give_acl ($config['id_user'], $group, "AW") ==1) { + $txt = ''; + } + return $txt; +} + +function format_delete_string($id) +{ + global $period, $module_id, $config, $group; + + $txt = ""; + + if (give_acl ($config['id_user'], $group, "AW") ==1) { + $txt = ''; + } + return $txt; +} + +function format_delete_log4x($id) +{ + global $period, $module_id, $config, $group; + + $txt = ""; + + if (give_acl ($config['id_user'], $group, "AW") ==1) { + $txt = ''; + } + return $txt; } ?> diff --git a/pandora_console/operation/agentes/estado_monitores.php b/pandora_console/operation/agentes/estado_monitores.php index 1056fa2f9e..4d50b80e92 100644 --- a/pandora_console/operation/agentes/estado_monitores.php +++ b/pandora_console/operation/agentes/estado_monitores.php @@ -135,7 +135,9 @@ foreach ($modules as $module) { $link ="winopeng('operation/agentes/stat_win.php?type=$graph_type&period=86400&id=".$module["id_agente_modulo"]."&label=".$module["nombre"]."&refresh=600','day_".$win_handle."')"; - $data[6] = ''; + $data[6] = ""; + if ($nombre_tipo_modulo != "log4x") + $data[6] .= ''; $data[6] .= " "; } diff --git a/pandora_console/operation/agentes/estado_ultimopaquete.php b/pandora_console/operation/agentes/estado_ultimopaquete.php index 4c2bd003a3..a9a97233ec 100644 --- a/pandora_console/operation/agentes/estado_ultimopaquete.php +++ b/pandora_console/operation/agentes/estado_ultimopaquete.php @@ -143,7 +143,11 @@ foreach ($modules as $module) { } echo ""; } - if (($module["id_tipo_modulo"] == 100) OR ($module['history_data'] == 0)) { + + if ($module["id_tipo_modulo"] == 30) { + echo " "; + + } else if (($module["id_tipo_modulo"] == 100) OR ($module['history_data'] == 0)) { echo ""; echo substr(safe_output($module["datos"]),0,12); } else { diff --git a/pandora_console/pandoradb.sql b/pandora_console/pandoradb.sql index dfaefe3508..dfffdfdc6c 100644 --- a/pandora_console/pandoradb.sql +++ b/pandora_console/pandoradb.sql @@ -95,6 +95,19 @@ CREATE TABLE IF NOT EXISTS `tagente_datos_string` ( KEY `idx_utimestamp` (`utimestamp`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; +CREATE TABLE IF NOT EXISTS `tagente_datos_log4x` ( + `id_tagente_datos_log4x` bigint(20) unsigned NOT NULL auto_increment, + `id_agente_modulo` int(10) unsigned NOT NULL default '0', + + `severity` text NOT NULL, + `message` text NOT NULL, + `stacktrace` text NOT NULL, + + `utimestamp` int(20) unsigned NOT NULL default 0, + PRIMARY KEY (`id_tagente_datos_log4x`), + KEY `data_log4x_index_1` (`id_agente_modulo`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + -- delete "cambio" not used anymore CREATE TABLE `tagente_estado` ( `id_agente_estado` int(10) unsigned NOT NULL auto_increment, diff --git a/pandora_console/pandoradb_data.sql b/pandora_console/pandoradb_data.sql index 89e0f75977..eed9139641 100644 --- a/pandora_console/pandoradb_data.sql +++ b/pandora_console/pandoradb_data.sql @@ -223,6 +223,9 @@ INSERT INTO `ttipo_modulo` VALUES (21,'async_proc', 7, 'Asyncronous proc data', 'mod_async_proc.png'), (22,'async_data', 6, 'Asyncronous numeric data', 'mod_async_data.png'), (23,'async_string', 8, 'Asyncronous string data', 'mod_async_string.png'), + +(30,'log4x',0,'Log4x','mod_log4x.png'), + (100,'keep_alive',-1,'KeepAlive','mod_keepalive.png'); -- Not yet implemented -- (19,'image_jpg',9,'Image JPG data', 'mod_image_jpg.png'), diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index a488365b0c..314d7946c7 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -73,8 +73,8 @@ our @AlertStatus = ('Execute the alert', 'Do not execute the alert', 'Do not exe ########################################################################## # Generate alerts for a given module. ########################################################################## -sub pandora_generate_alerts ($$$$$$$) { - my ($pa_config, $data, $status, $agent, $module, $utimestamp, $dbh) = @_; +sub pandora_generate_alerts ($$$$$$$;$) { + my ($pa_config, $data, $status, $agent, $module, $utimestamp, $dbh, $extraMacros) = @_; # Do not generate alerts for disabled groups if (is_group_disabled ($dbh, $agent->{'id_grupo'})) { @@ -93,7 +93,7 @@ sub pandora_generate_alerts ($$$$$$$) { $utimestamp, $dbh); pandora_process_alert ($pa_config, $data, $agent, $module, - $alert, $rc, $dbh); + $alert, $rc, $dbh, $extraMacros); # Evaluate compound alerts even if the alert status did not change in # case the compound alert does not recover @@ -196,8 +196,8 @@ sub pandora_evaluate_alert ($$$$$$$) { ########################################################################## # Process an alert given the status returned by pandora_evaluate_alert. ########################################################################## -sub pandora_process_alert ($$$$$$$) { - my ($pa_config, $data, $agent, $module, $alert, $rc, $dbh) = @_; +sub pandora_process_alert ($$$$$$$;$) { + my ($pa_config, $data, $agent, $module, $alert, $rc, $dbh, $extraMacros) = @_; logger ($pa_config, "Processing alert '" . $alert->{'name'} . "' for agent '" . $agent->{'nombre'} . "': " . (defined ($AlertStatus[$rc]) ? $AlertStatus[$rc] : 'Unknown status') . ".", 10); @@ -231,7 +231,7 @@ sub pandora_process_alert ($$$$$$$) { db_do($dbh, 'UPDATE ' . $table . ' SET times_fired = 0, internal_counter = 0 WHERE id = ?', $id); - pandora_execute_alert ($pa_config, $data, $agent, $module, $alert, 0, $dbh); + pandora_execute_alert ($pa_config, $data, $agent, $module, $alert, 0, $dbh, $extraMacros); return; } @@ -272,7 +272,7 @@ sub pandora_process_alert ($$$$$$$) { db_do($dbh, 'UPDATE ' . $table . ' SET times_fired = ?, last_fired = ?, internal_counter = ? ' . $new_interval . ' WHERE id = ?', $alert->{'times_fired'}, $utimestamp, $alert->{'internal_counter'}, $id); - pandora_execute_alert ($pa_config, $data, $agent, $module, $alert, 1, $dbh); + pandora_execute_alert ($pa_config, $data, $agent, $module, $alert, 1, $dbh, $extraMacros); return; } } @@ -362,9 +362,9 @@ sub pandora_generate_compound_alerts ($$$$$$$$) { ########################################################################## # Execute the given alert. ########################################################################## -sub pandora_execute_alert ($$$$$$$) { +sub pandora_execute_alert ($$$$$$$;$) { my ($pa_config, $data, $agent, $module, - $alert, $alert_mode, $dbh) = @_; + $alert, $alert_mode, $dbh, $extraMacros) = @_; logger ($pa_config, "Executing alert '" . $alert->{'name'} . "' for module '" . $module->{'nombre'} . "'.", 10); @@ -408,7 +408,7 @@ sub pandora_execute_alert ($$$$$$$) { # Execute actions foreach my $action (@actions) { - pandora_execute_action ($pa_config, $data, $agent, $alert, $alert_mode, $action, $module, $dbh); + pandora_execute_action ($pa_config, $data, $agent, $alert, $alert_mode, $action, $module, $dbh, $extraMacros); } # Generate an event @@ -422,9 +422,9 @@ sub pandora_execute_alert ($$$$$$$) { ########################################################################## # Execute the given action. ########################################################################## -sub pandora_execute_action ($$$$$$$$) { +sub pandora_execute_action ($$$$$$$$;$) { my ($pa_config, $data, $agent, $alert, - $alert_mode, $action, $module, $dbh) = @_; + $alert_mode, $action, $module, $dbh, $extraMacros) = @_; logger($pa_config, "Executing action '" . $action->{'name'} . "' for alert '". $alert->{'name'} . "' agent '" . (defined ($agent) ? $agent->{'nombre'} : 'N/A') . "'.", 10); @@ -465,7 +465,12 @@ sub pandora_execute_action ($$$$$$$$) { _id_agent_ => (defined ($module)) ? $module->{'id_agente'} : '', _id_alert_ => $alert->{'id'} ); - + + if (defined ($extraMacros)){ + while ((my $macro, my $value) = each (%{$extraMacros})) { + $macros{$macro} = $value; + } + } # User defined alerts if ($action->{'internal'} == 0) { @@ -523,10 +528,10 @@ sub pandora_access_update ($$$) { ########################################################################## # Process Pandora module. ########################################################################## -sub pandora_process_module ($$$$$$$$$) { - my ($pa_config, $data, $agent, $module, $module_type, - $timestamp, $utimestamp, $server_id, $dbh) = @_; - +sub pandora_process_module ($$$$$$$$$;$) { + my ($pa_config, $dataObject, $agent, $module, $module_type, + $timestamp, $utimestamp, $server_id, $dbh, $extraMacros) = @_; + logger($pa_config, "Processing module '" . $module->{'nombre'} . "' for agent " . (defined ($agent) && $agent ne '' ? "'" . $agent->{'nombre'} . "'" : 'ID ' . $module->{'id_agente'}) . ".", 10); # Get module type @@ -550,9 +555,9 @@ sub pandora_process_module ($$$$$$$$$) { } # Process data - my $processed_data = process_data ($data, $module, $module_type, $utimestamp, $dbh); + my $processed_data = process_data ($dataObject, $module, $module_type, $utimestamp, $dbh); if (! defined ($processed_data)) { - logger($pa_config, "Received invalid data '" . $data . "' from agent '" . $agent->{'nombre'} . "' module '" . $module->{'nombre'} . "' agent " . (defined ($agent) ? "'" . $agent->{'nombre'} . "'" : 'ID ' . $module->{'id_agente'}) . ".", 3); + logger($pa_config, "Received invalid data '" . $dataObject . "' from agent '" . $agent->{'nombre'} . "' module '" . $module->{'nombre'} . "' agent " . (defined ($agent) ? "'" . $agent->{'nombre'} . "'" : 'ID ' . $module->{'id_agente'}) . ".", 3); pandora_update_module_on_error ($pa_config, $module->{'id_agente_modulo'}, $dbh); return; } @@ -575,7 +580,7 @@ sub pandora_process_module ($$$$$$$$$) { # Generate alerts if (pandora_inhibit_alerts ($pa_config, $agent, $dbh) == 0) { - pandora_generate_alerts ($pa_config, $processed_data, $status, $agent, $module, $utimestamp, $dbh); + pandora_generate_alerts ($pa_config, $processed_data, $status, $agent, $module, $utimestamp, $dbh, $extraMacros); } #Update module status @@ -609,8 +614,8 @@ sub pandora_process_module ($$$$$$$$$) { $utimestamp, ($save == 1) ? $timestamp : $agent_status->{'last_try'}, $module->{'id_agente_modulo'}); # Save module data - if ($save == 1) { - save_module_data ($processed_data, $module, $module_type, $utimestamp, $dbh); + if ($module_type eq "log4x" || $save == 1) { + save_module_data ($dataObject, $module, $module_type, $utimestamp, $dbh); } } @@ -750,7 +755,7 @@ sub pandora_update_agent ($$$$$$$;$$$$) { # if the the flag update_gis_data is 0 the positional data is ignored. if ($last_agent_info->{'update_gis_data'} == 0){ logger($pa_config, "Agent id $agent_id positional data ignored",10); - db_do($dbh, 'UPDATE tagente SET intervalo = ?, agent_version = ?, ultimo_contacto_remoto = ?, ultimo_contacto = ?, os_version = ?, WHERE id_agente = ?', + db_do($dbh, 'UPDATE tagente SET intervalo = ?, agent_version = ?, ultimo_contacto_remoto = ?, ultimo_contacto = ?, os_version = ? WHERE id_agente = ?', $agent_interval, $agent_version, $agent_timestamp, $timestamp, $os_version, $agent_id); return; } @@ -828,7 +833,8 @@ sub pandora_module_keep_alive ($$$$$) { my $module = get_db_single_row ($dbh, 'SELECT * FROM tagente_modulo WHERE id_agente = ? AND id_tipo_modulo = 100', $id_agent); return unless defined ($module); - pandora_process_module ($pa_config, 1, '', $module, 'keep_alive', '', time(), $server_id, $dbh); + my %data = ('data' => 1); + pandora_process_module ($pa_config, \%data, '', $module, 'keep_alive', '', time(), $server_id, $dbh); } ########################################################################## @@ -923,7 +929,7 @@ sub pandora_create_agent ($$$$$$$$$$$;$$$$) { ########################################################################## # Generate an event. ########################################################################## -sub pandora_event (%$$$$$$$$) { +sub pandora_event ($$$$$$$$$) { my ($pa_config, $evento, $id_grupo, $id_agente, $severity, $id_alert_am, $id_agentmodule, $event_type, $event_status, $dbh) = @_; @@ -1000,9 +1006,10 @@ sub pandora_module_keep_alive_nd { AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo AND ( tagente_estado.utimestamp + (tagente.intervalo * 2) < UNIX_TIMESTAMP())'); + my %data = ('data' => 0); foreach my $module (@modules) { logger($pa_config, "Updating keep_alive module for module '" . $module->{'nombre'} . "' agent ID " . $module->{'id_agente'} . " (agent without data).", 10); - pandora_process_module ($pa_config, 0, '', $module, 'keep_alive', '', time (), 0, $dbh); + pandora_process_module ($pa_config, \%data, '', $module, 'keep_alive', '', time (), 0, $dbh); } } @@ -1167,8 +1174,14 @@ sub subst_alert_macros ($$) { # Process module data. ########################################################################## sub process_data ($$$$$) { - my ($data, $module, $module_type, $utimestamp, $dbh) = @_; + my ($dataObject, $module, $module_type, $utimestamp, $dbh) = @_; + if ($module_type eq "log4x") { + return log4x_get_severity_num($dataObject); + } + + my $data = $dataObject->{'data'}; + # String data if ($module_type =~ m/_string$/) { @@ -1195,7 +1208,11 @@ sub process_data ($$$$$) { $data = process_inc_data ($data, $module, $utimestamp, $dbh); # Not an error, no previous data - return 0 unless defined ($data); + if (!defined($data)){ + $dataObject->{'data'} = 0; + return 0; + } + #return 0 unless defined ($data); } # Post process @@ -1208,6 +1225,7 @@ sub process_data ($$$$$) { # Format data $data = sprintf("%.2f", $data); + $dataObject->{'data'} = $data; return $data; } @@ -1245,6 +1263,30 @@ sub process_inc_data ($$$$) { return ($data - $data_inc->{'datos'}) / ($utimestamp - $data_inc->{'utimestamp'}); } +sub log4x_get_severity_num($) { + my ($dataObject) = @_; + my $data = $dataObject->{'severity'}; + + # The severity is a word, so we need to translate to numbers + + if ($data =~ m/^trace$/i) { + $data = 10; + } elsif ($data =~ m/^debug$/i) { + $data = 20; + } elsif ($data =~ m/^info$/i) { + $data = 30; + } elsif ($data =~ m/^warn$/i) { + $data = 40; + } elsif ($data =~ m/^error$/i) { + $data = 50; + } elsif ($data =~ m/^fatal$/i) { + $data = 60; + } else { + $data = 10; + } + return $data; +} + ########################################################################## # Returns the status of the module: 0 (NORMAL), 1 (CRITICAL), 2 (WARNING). ########################################################################## @@ -1262,6 +1304,15 @@ sub get_module_status ($$) { ($critical_min, $critical_max) = (0, 1); } + if ($module_type eq "log4x") { + if ($critical_min eq $critical_max) { + ($critical_min, $critical_max) = (50, 61); # ERROR - FATAL + } + if ($warning_min eq $warning_max) { + ($warning_min, $warning_max) = (40, 41); # WARN - WARN + } + } + # Critical if ($critical_min ne $critical_max) { return 1 if ($data >= $critical_min && $data < $critical_max); @@ -1352,12 +1403,36 @@ sub generate_status_event ($$$$$$$) { # Saves module data to the DB. ########################################################################## sub save_module_data ($$$$$) { - my ($data, $module, $module_type, $utimestamp, $dbh) = @_; + my ($dataObject, $module, $module_type, $utimestamp, $dbh) = @_; - my $table = ($module_type =~ m/_string/) ? 'tagente_datos_string' : 'tagente_datos'; + if ($module_type eq "log4x") { + # + # + # log4x + # + # + # + # + # + # - db_do($dbh, 'INSERT INTO ' . $table . ' (id_agente_modulo, datos, utimestamp) - VALUES (?, ?, ?)', $module->{'id_agente_modulo'}, $data, $utimestamp); + print "saving log4x data: " . $dataObject->{'message'} . "\n"; + + my $sql = "INSERT INTO tagente_datos_log4x(id_agente_modulo, utimestamp, severity, message, stacktrace) values (?, ?, ?, ?, ?)"; + + db_do($dbh, $sql, + $module->{'id_agente_modulo'}, $utimestamp, + $dataObject->{'severity'}, + $dataObject->{'message'}, + $dataObject->{'stacktrace'} + ); + } else { + my $data = $dataObject->{'data'}; + my $table = ($module_type =~ m/_string/) ? 'tagente_datos_string' : 'tagente_datos'; + + db_do($dbh, 'INSERT INTO ' . $table . ' (id_agente_modulo, datos, utimestamp) + VALUES (?, ?, ?)', $module->{'id_agente_modulo'}, $data, $utimestamp); + } } ########################################################################## @@ -1366,6 +1441,9 @@ sub save_module_data ($$$$$) { sub export_module_data ($$$$$$$) { my ($pa_config, $data, $agent, $module, $module_type, $timestamp, $dbh) = @_; + # TODO: If module is log4x we hope for the best :P + #return if ($module_type == "log4x"); + # Data export is disabled return if ($module->{'id_export'} < 1); diff --git a/pandora_server/lib/PandoraFMS/DB.pm b/pandora_server/lib/PandoraFMS/DB.pm index 7be2839213..86f48d6ed9 100644 --- a/pandora_server/lib/PandoraFMS/DB.pm +++ b/pandora_server/lib/PandoraFMS/DB.pm @@ -33,6 +33,7 @@ our @EXPORT = qw( db_insert get_agent_id get_agent_name + get_module_name get_db_rows get_db_single_row get_db_value @@ -105,6 +106,16 @@ sub get_agent_name ($$) { return get_db_value ($dbh, "SELECT nombre FROM tagente WHERE id_agente = ?", $agent_id); } +########################################################################## +## SUB get_module_name(module_id) +## Return the module name, given "module_id" +########################################################################## +sub get_module_name ($$) { + my ($dbh, $module_id) = @_; + + return get_db_value ($dbh, "SELECT nombre FROM tagente_modulo WHERE id_agente_modulo = ?", $module_id); +} + ########################################################################## ## Returns true if the given group is disabled, false otherwise. ########################################################################## @@ -223,6 +234,8 @@ sub db_insert ($$;@) { sub db_do ($$;@) { my ($dbh, $query, @values) = @_; + #DBI->trace( 3, '/tmp/dbitrace.log' ); + $dbh->do($query, undef, @values); } diff --git a/pandora_server/lib/PandoraFMS/DataServer.pm b/pandora_server/lib/PandoraFMS/DataServer.pm index 9e6010822f..db01efe95c 100644 --- a/pandora_server/lib/PandoraFMS/DataServer.pm +++ b/pandora_server/lib/PandoraFMS/DataServer.pm @@ -321,7 +321,6 @@ sub process_module_data ($$$$$$$$$) { $ModuleSem->down (); my $module = get_db_single_row ($dbh, 'SELECT * FROM tagente_modulo WHERE id_agente = ? AND nombre = ?', $agent->{'id_agente'}, $module_name); if (! defined ($module)) { - # Do not auto create modules if ($pa_config->{'autocreate'} ne '1') { logger($pa_config, "Module '$module_name' not found for agent '$agent_name' and module auto-creation disabled.", 10); @@ -335,7 +334,7 @@ sub process_module_data ($$$$$$$$$) { $ModuleSem->up (); return; } - + # Get the module type my $module_id = get_module_id ($dbh, $module_type); if ($module_id <= 0) { @@ -379,8 +378,46 @@ sub process_module_data ($$$$$$$$$) { } my $utimestamp = timelocal($6, $5, $4, $3, $2 - 1, $1 - 1900); - my $value = get_tag_value ($data, 'data', ''); - pandora_process_module ($pa_config, $value, $agent, $module, $module_type, $timestamp, $utimestamp, $server_id, $dbh); + + #my $value = get_tag_value ($data, 'data', ''); + my $dataObject = get_module_data($data, $agent_name, $module_name, $module_type); + my $extraMacros = get_macros_for_data($data, $agent_name, $module_name, $module_type); + + pandora_process_module ($pa_config, $dataObject, $agent, $module, $module_type, $timestamp, $utimestamp, $server_id, $dbh, $extraMacros); +} + +sub get_module_data($$){ + my ($data, $agent_name, $module_name, $module_type) = @_; + + my %dataObject; + + if ($module_type eq "log4x") { + foreach my $attr ('severity','message', 'stacktrace'){ + $dataObject{$attr} = get_tag_value ($data, $attr, ''); + } + } else { + # Default case + my $value = get_tag_value ($data, 'data', ''); + $dataObject{'data'} = $value; + } + #return $value; + return \%dataObject; +} + +sub get_macros_for_data($$){ + my ($data, $agent_name, $module_name, $module_type) = @_; + + my %macros; + + if ($module_type eq "log4x") { + foreach my $attr ('severity','message', 'stacktrace'){ + my $macro = "_" . $attr . "_"; + $macros{$macro} = get_tag_value ($data, $attr, ''); + } + } else { + } + + return \%macros; } 1; diff --git a/pandora_server/lib/PandoraFMS/NetworkServer.pm b/pandora_server/lib/PandoraFMS/NetworkServer.pm index 5ee1e6bf2f..dbdc7ffe0b 100644 --- a/pandora_server/lib/PandoraFMS/NetworkServer.pm +++ b/pandora_server/lib/PandoraFMS/NetworkServer.pm @@ -277,7 +277,7 @@ sub pandora_query_snmp (%$) { # SNMP v1, v2 and v2c call if ($snmp_version ne '3'){ - $output = `$snmpget_cmd -v $snmp_version -r $snmp_retries -t $snmp_timeout -OUevqt -c $snmp_community $snmp_target $snmp_oid 2>/dev/null`; + $output = `$snmpget_cmd -v $snmp_version -r $snmp_retries -t $snmp_timeout -OUevqt -c '$snmp_community' $snmp_target $snmp_oid 2>/dev/null`; if ($output ne ""){ $module_result = 0; $module_data = $output; @@ -420,7 +420,8 @@ sub exec_network_module ($$$$) { # Is everything goes ok if ($module_result == 0) { - pandora_process_module ($pa_config, $module_data, undef, $module, undef, $timestamp, $utimestamp, $server_id, $dbh); + my %data = ("data" => $module_data); + pandora_process_module ($pa_config, \%data, '', $module, '', $timestamp, $utimestamp, $server_id, $dbh); # Update agent last contact using Pandora version as agent version pandora_update_agent ($pa_config, $timestamp, $id_agente, $pa_config->{'servername'}.'_Net', $pa_config->{'version'}, -1, $dbh); diff --git a/pandora_server/lib/PandoraFMS/PluginServer.pm b/pandora_server/lib/PandoraFMS/PluginServer.pm index b1e818927c..2a629ffffc 100644 --- a/pandora_server/lib/PandoraFMS/PluginServer.pm +++ b/pandora_server/lib/PandoraFMS/PluginServer.pm @@ -166,9 +166,11 @@ sub data_consumer ($$) { # Execute command $command = $pa_config->{'plugin_exec'} . ' ' . $timeout . ' ' . $command; + my $module_data = `$command`; my $ReturnCode = ($? >> 8) & 0xff; + if ($plugin->{'plugin_type'} == 1) { # Get the errorlevel if is a Nagios plugin type (parsing the errorlevel) @@ -206,7 +208,8 @@ sub data_consumer ($$) { my $utimestamp = time (); my $timestamp = strftime ("%Y-%m-%d %H:%M:%S", localtime($utimestamp)); - pandora_process_module ($pa_config, $module_data, undef, $module, undef, $timestamp, $utimestamp, $self->getServerID (), $dbh); + my %data = ("data" => $module_data); + pandora_process_module ($pa_config, \%data, '', $module, '', $timestamp, $utimestamp, $self->getServerID (), $dbh); pandora_update_agent ($pa_config, $timestamp, $module->{'id_agente'}, $pa_config->{'servername'}.'_Plugin', $pa_config->{'version'}, -1, $dbh); } diff --git a/pandora_server/lib/PandoraFMS/PredictionServer.pm b/pandora_server/lib/PandoraFMS/PredictionServer.pm index 373bbb06ce..096a47e957 100644 --- a/pandora_server/lib/PandoraFMS/PredictionServer.pm +++ b/pandora_server/lib/PandoraFMS/PredictionServer.pm @@ -235,7 +235,8 @@ sub exec_prediction_module ($$$$) { $module_data = $average; } - pandora_process_module ($pa_config, $module_data, undef, $agent_module, undef, $timestamp, $utimestamp, $server_id, $dbh); + my %data = ("data" => $module_data); + pandora_process_module ($pa_config, \%data, '', $agent_module, '', $timestamp, $utimestamp, $server_id, $dbh); pandora_update_agent ($pa_config, $timestamp, $agent_module->{'id_agente'}, $pa_config->{'servername'}.'_Prediction', $pa_config->{'version'}, -1, $dbh); } diff --git a/pandora_server/lib/PandoraFMS/WMIServer.pm b/pandora_server/lib/PandoraFMS/WMIServer.pm index b5234d9aab..a6063c27a8 100644 --- a/pandora_server/lib/PandoraFMS/WMIServer.pm +++ b/pandora_server/lib/PandoraFMS/WMIServer.pm @@ -192,7 +192,8 @@ sub data_consumer ($$) { my $utimestamp = time (); my $timestamp = strftime ("%Y-%m-%d %H:%M:%S", localtime($utimestamp)); - pandora_process_module ($pa_config, $module_data, undef, $module, undef, $timestamp, $utimestamp, $self->getServerID (), $dbh); + my %data = ("data" => $module_data); + pandora_process_module ($pa_config, \%data, '', $module, '', $timestamp, $utimestamp, $self->getServerID (), $dbh); pandora_update_agent ($pa_config, $timestamp, $module->{'id_agente'}, $pa_config->{'servername'} . '_WMI', $pa_config->{'version'}, -1, $dbh); }