From 71def9a7fbdb6178effe55a00893993c8a7af097 Mon Sep 17 00:00:00 2001 From: Ramon Novoa Date: Mon, 6 Apr 2015 15:26:26 +0200 Subject: [PATCH 01/16] Implemented an agent cache for the metaconsole. --- pandora_console/include/functions_agents.php | 5 +++- pandora_console/include/functions_config.php | 26 +++++++++++------- pandora_server/bin/pandora_server | 5 +++- pandora_server/lib/PandoraFMS/Config.pm | 3 +++ pandora_server/lib/PandoraFMS/Core.pm | 27 +++++-------------- pandora_server/lib/PandoraFMS/DB.pm | 28 +++++++++++++++----- pandora_server/util/pandora_manage.pl | 10 +++---- 7 files changed, 60 insertions(+), 44 deletions(-) diff --git a/pandora_console/include/functions_agents.php b/pandora_console/include/functions_agents.php index 61bf8d7eae..20d3716049 100644 --- a/pandora_console/include/functions_agents.php +++ b/pandora_console/include/functions_agents.php @@ -21,6 +21,7 @@ require_once($config['homedir'] . "/include/functions_modules.php"); require_once($config['homedir'] . '/include/functions_users.php'); +enterprise_include_once('/include/functions_agents.php'); /** * Check the agent exists in the DB. @@ -1828,7 +1829,9 @@ function agents_delete_agent ($id_agents, $disableACL = false) { db_pandora_audit( "Agent management", "Deleted agent '$agent_name'"); - + // Delete the agent from the metaconsole cache + enterprise_hook('agent_delete_from_cache', array($id_agent)); + /* Break the loop on error */ if ($error) break; diff --git a/pandora_console/include/functions_config.php b/pandora_console/include/functions_config.php index b27bb5eddc..b81b09695b 100644 --- a/pandora_console/include/functions_config.php +++ b/pandora_console/include/functions_config.php @@ -204,21 +204,23 @@ function config_update_config () { if ((int)get_parameter('event_replication') == 1) { if (!config_update_value ('replication_interval', (int)get_parameter('replication_interval'))) $error_update[] = __('Replication interval'); - if (!config_update_value ('replication_dbhost', (string)get_parameter('replication_dbhost'))) - $error_update[] = __('Replication DB host'); - if (!config_update_value ('replication_dbname', (string)get_parameter('replication_dbname'))) - $error_update[] = __('Replication DB database'); - if (!config_update_value ('replication_dbuser', (string)get_parameter('replication_dbuser'))) - $error_update[] = __('Replication DB user'); - if (!config_update_value ('replication_dbpass', io_input_password((string)get_parameter('replication_dbpass')))) - $error_update[] = __('Replication DB password'); - if (!config_update_value ('replication_dbport', (string)get_parameter('replication_dbport'))) - $error_update[] = __('Replication DB port'); if (!config_update_value ('replication_mode', (string)get_parameter('replication_mode'))) $error_update[] = __('Replication mode'); if (!config_update_value ('show_events_in_local', (string)get_parameter('show_events_in_local'))) $error_update[] = __('Show events list in local console (read only)'); } + if (!config_update_value ('replication_dbhost', (string)get_parameter('replication_dbhost'))) + $error_update[] = __('Replication DB host'); + if (!config_update_value ('replication_dbname', (string)get_parameter('replication_dbname'))) + $error_update[] = __('Replication DB database'); + if (!config_update_value ('replication_dbuser', (string)get_parameter('replication_dbuser'))) + $error_update[] = __('Replication DB user'); + if (!config_update_value ('replication_dbpass', io_input_password((string)get_parameter('replication_dbpass')))) + $error_update[] = __('Replication DB password'); + if (!config_update_value ('replication_dbport', (string)get_parameter('replication_dbport'))) + $error_update[] = __('Replication DB port'); + if (!config_update_value ('metaconsole_agent_cache', (int)get_parameter('metaconsole_agent_cache'))) + $error_update[] = __('Metaconsole agent cache'); if (!config_update_value ('log_collector', (bool)get_parameter('log_collector'))) $error_update[] = __('Activate Log Collector'); @@ -796,6 +798,10 @@ function config_process_config () { config_update_value ('replication_mode', "only_validated"); } + if (!isset ($config["metaconsole_agent_cache"])) { + config_update_value ('metaconsole_agent_cache', 0); + } + if (!isset ($config["show_events_in_local"])) { config_update_value ('show_events_in_local', 0); } diff --git a/pandora_server/bin/pandora_server b/pandora_server/bin/pandora_server index 272f75e89b..87fdeb38d6 100755 --- a/pandora_server/bin/pandora_server +++ b/pandora_server/bin/pandora_server @@ -101,6 +101,9 @@ sub pandora_startup () { # Generate the encryption key after reading the passphrase. $Config{"encryption_key"} = enterprise_hook('pandora_get_encryption_key', [\%Config, $Config{"encryption_passphrase"}]); + # Update the agent cache. + enterprise_hook('update_agent_cache', [\%Config, $DBH]) if ($Config{'metaconsole_agent_cache'} == 1); + pandora_audit (\%Config, 'Pandora FMS Server Daemon starting', 'SYSTEM', 'System', $DBH); # Load servers @@ -291,7 +294,7 @@ sub pandora_server_tasks ($) { logger ($pa_config, "Updating module status and fired alert counts for agent " . $agent->{'nombre'}, 10); if ($agent->{'update_module_count'} == 1) { - pandora_update_agent_module_count ($dbh, $agent->{'id_agente'}); + pandora_update_agent_module_count ($pa_config, $dbh, $agent->{'id_agente'}); } if ($agent->{'update_alert_count'} == 1) { diff --git a/pandora_server/lib/PandoraFMS/Config.pm b/pandora_server/lib/PandoraFMS/Config.pm index 2579942056..613383527e 100644 --- a/pandora_server/lib/PandoraFMS/Config.pm +++ b/pandora_server/lib/PandoraFMS/Config.pm @@ -159,6 +159,9 @@ sub pandora_get_sharedconfig ($$) { # Pandora FMS Console's attachment directory $pa_config->{"attachment_dir"} = pandora_get_tconfig_token ($dbh, 'attachment_store', '/var/www/pandora_console/attachment'); + + # Metaconsole agent cache. + $pa_config->{"metaconsole_agent_cache"} = pandora_get_tconfig_token ($dbh, 'metaconsole_agent_cache', 0); } ########################################################################## diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index 850f5da749..62812da7f0 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -2608,7 +2608,7 @@ sub pandora_create_module_from_hash ($$$) { sub pandora_update_module_from_hash ($$$$$) { my ($pa_config, $parameters, $where_column, $where_value, $dbh) = @_; - my $module_id = db_process_update($dbh, 'tagente_modulo', $parameters, $where_column, $where_value); + my $module_id = db_process_update($dbh, 'tagente_modulo', $parameters, {$where_column => $where_value}); return $module_id; } @@ -2618,7 +2618,7 @@ sub pandora_update_module_from_hash ($$$$$) { sub pandora_update_table_from_hash ($$$$$$) { my ($pa_config, $parameters, $where_column, $where_value, $table, $dbh) = @_; - my $module_id = db_process_update($dbh, $table, $parameters, $where_column, $where_value); + my $module_id = db_process_update($dbh, $table, $parameters, {$where_column => $where_value}); return $module_id; } @@ -4663,24 +4663,8 @@ sub pandora_set_event_storm_protection ($) { ########################################################################## # Update the module status count of an agent. ########################################################################## -sub pandora_update_agent_count ($$) { - my ($dbh, $agent_id) = @_; - - db_do ($dbh, 'UPDATE tagente SET update_module_count=0, - normal_count=(SELECT COUNT(*) FROM tagente_modulo, tagente_estado WHERE tagente_modulo.id_agente_modulo=tagente_estado.id_agente_modulo AND tagente_modulo.id_agente=' . $agent_id . ' AND estado=0), - critical_count=(SELECT COUNT(*) FROM tagente_modulo, tagente_estado WHERE tagente_modulo.id_agente_modulo=tagente_estado.id_agente_modulo AND tagente_modulo.id_agente=' . $agent_id . ' AND estado=1), - warning_count=(SELECT COUNT(*) FROM tagente_modulo, tagente_estado WHERE tagente_modulo.id_agente_modulo=tagente_estado.id_agente_modulo AND tagente_modulo.id_agente=' . $agent_id . ' AND estado=2), - unknown_count=(SELECT COUNT(*) FROM tagente_modulo, tagente_estado WHERE tagente_modulo.id_agente_modulo=tagente_estado.id_agente_modulo AND tagente_modulo.id_agente=' . $agent_id . ' AND estado=3), - notinit_count=(SELECT COUNT(*) FROM tagente_modulo, tagente_estado WHERE tagente_modulo.id_agente_modulo=tagente_estado.id_agente_modulo AND tagente_modulo.id_agente=' . $agent_id . ' AND estado=4), - total_count=(SELECT COUNT(*) FROM tagente_modulo, tagente_estado WHERE tagente_modulo.id_agente_modulo=tagente_estado.id_agente_modulo AND tagente_modulo.id_agente=' . $agent_id . - ') WHERE id_agente = ' . $agent_id); -} - -########################################################################## -# Update the module status count of an agent. -########################################################################## -sub pandora_update_agent_module_count ($$) { - my ($dbh, $agent_id) = @_; +sub pandora_update_agent_module_count ($$$) { + my ($pa_config, $dbh, $agent_id) = @_; db_do ($dbh, 'UPDATE tagente SET update_module_count=0, normal_count=(SELECT COUNT(*) FROM tagente_modulo, tagente_estado WHERE tagente_modulo.disabled=0 AND tagente_modulo.id_agente_modulo=tagente_estado.id_agente_modulo AND tagente_modulo.id_agente=' . $agent_id . ' AND estado=0), @@ -4690,6 +4674,9 @@ sub pandora_update_agent_module_count ($$) { notinit_count=(SELECT COUNT(*) FROM tagente_modulo, tagente_estado WHERE tagente_modulo.disabled=0 AND tagente_modulo.id_agente_modulo=tagente_estado.id_agente_modulo AND tagente_modulo.id_agente=' . $agent_id . ' AND estado=4), total_count=(SELECT COUNT(*) FROM tagente_modulo, tagente_estado WHERE tagente_modulo.disabled=0 AND tagente_modulo.id_agente_modulo=tagente_estado.id_agente_modulo AND tagente_modulo.id_agente=' . $agent_id . ') WHERE id_agente = ' . $agent_id); + + # Sync the agent cache every time the module count is updated. + enterprise_hook('update_agent_cache', [$pa_config, $dbh, $agent_id]) if ($pa_config->{'metaconsole_agent_cache'} == 1); } ########################################################################## diff --git a/pandora_server/lib/PandoraFMS/DB.pm b/pandora_server/lib/PandoraFMS/DB.pm index e4c34bb5a9..19b6b686e6 100644 --- a/pandora_server/lib/PandoraFMS/DB.pm +++ b/pandora_server/lib/PandoraFMS/DB.pm @@ -734,17 +734,20 @@ sub db_process_insert($$$$;@) { ######################################################################## ## SQL update. ######################################################################## -sub db_process_update($$$$$;@) { - my ($dbh, $table, $parameters, $where_column, $where_value, @values) = @_; +sub db_process_update($$$$) { + my ($dbh, $table, $parameters, $conditions) = @_; my @columns_array = keys %$parameters; my @values_array = values %$parameters; + my @where_columns = keys %$conditions; + my @where_values = values %$conditions; - if (!defined($table) || $#columns_array == -1) { + if (!defined($table) || $#columns_array == -1 || $#where_columns == -1) { return -1; exit; } + # VALUES... my $fields = ''; for (my $i = 0; $i <= $#values_array; $i++) { if (!defined($values_array[$i])) { @@ -756,12 +759,23 @@ sub db_process_update($$$$$;@) { $fields = $fields . " " . $RDBMS_QUOTE . "$columns_array[$i]" . $RDBMS_QUOTE . " = ?"; } - - push(@values_array, $where_value); - + + # WHERE... + my $where = ''; + for (my $i = 0; $i <= $#where_columns; $i++) { + if (!defined($where_values[$i])) { + $where_values[$i] = ''; + } + if ($i > 0 && $i <= $#where_values) { + $where = $where.' AND '; + } + $where = $where . + " " . $RDBMS_QUOTE . "$where_columns[$i]" . $RDBMS_QUOTE . " = ?"; + } + my $res = db_update ($dbh, "UPDATE $table SET $fields - WHERE $where_column = ?", @values_array); + WHERE $where", @values_array, @where_values); return $res; } diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index b6a93d870f..2e711d13f0 100644 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -497,7 +497,7 @@ sub pandora_validate_event_id ($$$) { sub pandora_update_user_from_hash ($$$$) { my ($parameters, $where_column, $where_value, $dbh) = @_; - my $user_id = db_process_update($dbh, 'tusuario', $parameters, $where_column, $where_value); + my $user_id = db_process_update($dbh, 'tusuario', $parameters, {$where_column => $where_value}); return $user_id; } @@ -507,7 +507,7 @@ sub pandora_update_user_from_hash ($$$$) { sub pandora_update_alert_template_from_hash ($$$$) { my ($parameters, $where_column, $where_value, $dbh) = @_; - my $template_id = db_process_update($dbh, 'talert_templates', $parameters, $where_column, $where_value); + my $template_id = db_process_update($dbh, 'talert_templates', $parameters, {$where_column => $where_value}); return $template_id; } @@ -621,7 +621,7 @@ sub pandora_create_special_day_from_hash ($$$) { sub pandora_update_special_day_from_hash ($$$$) { my ($parameters, $where_column, $where_value, $dbh) = @_; - my $special_day_id = db_process_update($dbh, 'talert_special_days', $parameters, $where_column, $where_value); + my $special_day_id = db_process_update($dbh, 'talert_special_days', $parameters, {$where_column => $where_value}); return $special_day_id; } @@ -3480,7 +3480,7 @@ sub cli_stop_downtime () { my $parameters->{'date_to'} = time; - db_process_update($dbh, 'tplanned_downtime', $parameters, 'id', $downtime_id); + db_process_update($dbh, 'tplanned_downtime', $parameters, {'id' => $downtime_id}); } ############################################################################### @@ -3593,7 +3593,7 @@ sub pandora_get_event_name($$) { sub pandora_update_event_from_hash ($$$$) { my ($parameters, $where_column, $where_value, $dbh) = @_; - my $event_id = db_process_update($dbh, 'tevento', $parameters, $where_column, $where_value); + my $event_id = db_process_update($dbh, 'tevento', $parameters, {$where_column => $where_value}); return $event_id; } From cc1faf5eb9d37d741201e6136a0c39ea1d6b4af1 Mon Sep 17 00:00:00 2001 From: Ramon Novoa Date: Tue, 7 Apr 2015 11:55:18 +0200 Subject: [PATCH 02/16] Added a missing include. --- pandora_console/include/functions_agents.php | 1 + 1 file changed, 1 insertion(+) diff --git a/pandora_console/include/functions_agents.php b/pandora_console/include/functions_agents.php index 9d47f2d672..6541e807e5 100644 --- a/pandora_console/include/functions_agents.php +++ b/pandora_console/include/functions_agents.php @@ -19,6 +19,7 @@ * @subpackage Agents */ +require_once($config['homedir'] . '/include/functions.php'); require_once($config['homedir'] . "/include/functions_modules.php"); require_once($config['homedir'] . '/include/functions_users.php'); enterprise_include_once('/include/functions_agents.php'); From afada892fee76bd57999499656ca3680b5803468 Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Wed, 15 Apr 2015 15:29:07 +0200 Subject: [PATCH 03/16] Modified the 'group_get_data' and 'group_get_group_list' to retrieve the data from the metaconsole's new agent cache table --- pandora_console/include/functions_groups.php | 249 ++++++++++++++++++- 1 file changed, 245 insertions(+), 4 deletions(-) diff --git a/pandora_console/include/functions_groups.php b/pandora_console/include/functions_groups.php index 7c34c33ed0..7e3ad962e7 100644 --- a/pandora_console/include/functions_groups.php +++ b/pandora_console/include/functions_groups.php @@ -2278,10 +2278,10 @@ function group_get_data ($id_user = false, $user_strict = false, $acltags, $retu } if ($user_strict) { - $user_groups_ids = implode(',',array_keys($groups_without_tags)); + $user_groups_ids = implode(',', array_keys($groups_without_tags)); } else { - $user_groups_ids = implode(',',array_keys($acltags)); + $user_groups_ids = implode(',', array_keys($acltags)); } if (!empty($user_groups_ids)) { @@ -2341,10 +2341,251 @@ function group_get_data ($id_user = false, $user_strict = false, $acltags, $retu else { $i = 0; } + + /* + * Agent cache for metaconsole. + * Retrieve the statistic data from the cache table. + */ + if (!$user_strict && defined('METACONSOLE') && !empty($list_groups)) { + $cache_table = 'tmetaconsole_agent'; + + $sql_stats = "SELECT id_grupo, COUNT(id_agente) AS agents_total, + SUM(total_count) AS monitors_total, + SUM(normal_count) AS monitors_ok, + SUM(warning_count) AS monitors_warning, + SUM(critical_count) AS monitors_critical, + SUM(unknown_count) AS monitors_unknown, + SUM(notinit_count) AS monitors_not_init, + SUM(fired_count) AS alerts_fired + FROM $cache_table + WHERE disabled = 0 + AND id_grupo IN ($user_groups_ids) + GROUP BY id_grupo"; + $data_stats = db_get_all_rows_sql($sql_stats); + + $sql_stats_unknown = "SELECT id_grupo, COUNT(id_agente) AS agents_unknown + FROM $cache_table + WHERE disabled = 0 + AND id_grupo IN ($user_groups_ids) + AND critical_count = 0 + AND warning_count = 0 + AND unknown_count > 0 + GROUP BY id_grupo"; + $data_stats_unknown = db_get_all_rows_sql($sql_stats_unknown); + + $sql_stats_not_init = "SELECT id_grupo, COUNT(id_agente) AS agents_not_init + FROM $cache_table + WHERE disabled = 0 + AND id_grupo IN ($user_groups_ids) + AND (total_count = 0 OR total_count = notinit_count) + GROUP BY id_grupo"; + $data_stats_not_init = db_get_all_rows_sql($sql_stats_not_init); + + if ($mode == 'tactical' || $mode == 'tree') { + $sql_stats_ok = "SELECT id_grupo, COUNT(id_agente) AS agents_ok + FROM $cache_table + WHERE disabled = 0 + AND id_grupo IN ($user_groups_ids) + AND critical_count = 0 + AND warning_count = 0 + AND unknown_count = 0 + AND normal_count > 0 + GROUP BY id_grupo"; + $data_stats_ok = db_get_all_rows_sql($sql_stats_ok); + + $sql_stats_warning = "SELECT id_grupo, COUNT(id_agente) AS agents_warning + FROM $cache_table + WHERE disabled = 0 + AND id_grupo IN ($user_groups_ids) + AND critical_count = 0 + AND warning_count > 0 + GROUP BY id_grupo"; + $data_stats_warning = db_get_all_rows_sql($sql_stats_warning); + + $sql_stats_critical = "SELECT id_grupo, COUNT(id_agente) AS agents_critical + FROM $cache_table + WHERE disabled = 0 + AND id_grupo IN ($user_groups_ids) + AND critical_count = 0 + AND warning_count > 0 + GROUP BY id_grupo"; + $data_stats_critical = db_get_all_rows_sql($sql_stats_critical); + } + + $stats_by_group = array(); + if (!empty($data_stats)) { + foreach ($data_stats as $value) { + $group_id = (int) $value['id_grupo']; + + $stats = array(); + $stats['agents_total'] = (int) $value['agents_total']; + $stats['monitors_total'] = (int) $value['monitors_total']; + $stats['monitors_ok'] = (int) $value['monitors_ok']; + $stats['monitors_warning'] = (int) $value['monitors_warning']; + $stats['monitors_critical'] = (int) $value['monitors_critical']; + $stats['monitors_unknown'] = (int) $value['monitors_unknown']; + $stats['monitors_not_init'] = (int) $value['monitors_not_init']; + $stats['alerts_fired'] = (int) $value['alerts_fired']; + $stats_by_group[$group_id] = $stats; + } + + if (!empty($stats_by_group)) { + if (!empty($data_stats_unknown)) { + foreach ($data_stats_unknown as $value) { + $group_id = (int) $value['id_grupo']; + if (isset($stats_by_group[$group_id])) { + $stats_by_group[$group_id]['agents_unknown'] = (int) $value['agents_unknown']; + } + } + } + if (!empty($data_stats_not_init)) { + foreach ($data_stats_not_init as $value) { + $group_id = (int) $value['id_grupo']; + if (isset($stats_by_group[$group_id])) { + $stats_by_group[$group_id]['agents_not_init'] = (int) $value['agents_not_init']; + } + } + } + if (!empty($data_stats_ok)) { + foreach ($data_stats_ok as $value) { + $group_id = (int) $value['id_grupo']; + if (isset($stats_by_group[$group_id])) { + $stats_by_group[$group_id]['agents_ok'] = (int) $value['agents_ok']; + } + } + } + if (!empty($data_stats_warning)) { + foreach ($data_stats_warning as $value) { + $group_id = (int) $value['id_grupo']; + if (isset($stats_by_group[$group_id])) { + $stats_by_group[$group_id]['agents_warning'] = (int) $value['agents_warning']; + } + } + } + if (!empty($data_stats_critical)) { + foreach ($data_stats_critical as $value) { + $group_id = (int) $value['id_grupo']; + if (isset($stats_by_group[$group_id])) { + $stats_by_group[$group_id]['agents_critical'] = (int) $value['agents_critical']; + } + } + } + } + } + } + foreach ($list_groups as $key => $item) { $id = $item['id_grupo']; - if (($config["realtimestats"] == 0) && !$user_strict) { + if (!$user_strict && defined('METACONSOLE')) { // Agent cache + $group_stat = array(); + if (isset($stats_by_group[$id])) + $group_stat = $stats_by_group[$id]; + + $list[$i]['_id_'] = $id; + $list[$i]['_name_'] = $item['nombre']; + $list[$i]['_iconImg_'] = html_print_image ("images/groups_small/" . groups_get_icon($item['id_grupo']).".png", true, array ("style" => 'vertical-align: middle;')); + + if ($mode == 'tree' && !empty($item['parent'])) + $list[$i]['_parent_id_'] = $item['parent']; + + $list[$i]['_agents_unknown_'] = isset($group_stat['agents_unknown']) ? $group_stat['agents_unknown'] : 0; + $list[$i]['_monitors_alerts_fired_'] = isset($group_stat['alerts_fired']) ? $group_stat['alerts_fired'] : 0; + $list[$i]['_total_agents_'] = isset($group_stat['agents_total']) ? $group_stat['agents_total'] : 0; + + // This fields are not in database + $list[$i]['_monitors_ok_'] = isset($group_stat['monitors_ok']) ? $group_stat['monitors_ok'] : 0; + $list[$i]['_monitors_critical_'] = isset($group_stat['monitors_critical']) ? $group_stat['monitors_critical'] : 0; + $list[$i]['_monitors_warning_'] = isset($group_stat['monitors_warning']) ? $group_stat['monitors_warning'] : 0; + $list[$i]['_monitors_unknown_'] = isset($group_stat['monitors_unknown']) ? $group_stat['monitors_unknown'] : 0; + $list[$i]['_monitors_not_init_'] = isset($group_stat['monitors_not_init']) ? $group_stat['monitors_not_init'] : 0; + $list[$i]['_agents_not_init_'] = isset($group_stat['agents_not_init']) ? $group_stat['agents_not_init'] : 0; + + if ($mode == 'tactical' || $mode == 'tree') { + $list[$i]['_agents_ok_'] = isset($group_stat['agents_ok']) ? $group_stat['agents_ok'] : 0; + $list[$i]['_agents_warning_'] = isset($group_stat['agents_warning']) ? $group_stat['agents_warning'] : 0; + $list[$i]['_agents_critical_'] = isset($group_stat['agents_critical']) ? $group_stat['agents_critical'] : 0; + $list[$i]['_monitors_alerts_'] = isset($group_stat['alerts']) ? $group_stat['alerts'] : 0;; + + $list[$i]["_monitor_alerts_fire_count_"] = $group_stat[0]["alerts_fired"]; + $list[$i]["_total_checks_"] = $group_stat[0]["modules"]; + $list[$i]["_total_alerts_"] = $group_stat[0]["alerts"]; + } + if ($mode == 'tactical') { + // Get total count of monitors for this group, except disabled. + $list[$i]["_monitor_checks_"] = $list[$i]["_monitors_not_init_"] + $list[$i]["_monitors_unknown_"] + $list[$i]["_monitors_warning_"] + $list[$i]["_monitors_critical_"] + $list[$i]["_monitors_ok_"]; + + // Calculate not_normal monitors + $list[$i]["_monitor_not_normal_"] = $list[$i]["_monitor_checks_"] - $list[$i]["_monitors_ok_"]; + + if ($list[$i]["_monitor_not_normal_"] > 0 && $list[$i]["_monitor_checks_"] > 0) { + $list[$i]["_monitor_health_"] = format_numeric (100 - ($list[$i]["_monitor_not_normal_"] / ($list[$i]["_monitor_checks_"] / 100)), 1); + } + else { + $list[$i]["_monitor_health_"] = 100; + } + + if ($list[$i]["_monitors_not_init_"] > 0 && $list[$i]["_monitor_checks_"] > 0) { + $list[$i]["_module_sanity_"] = format_numeric (100 - ($list[$i]["_monitors_not_init_"] / ($list[$i]["_monitor_checks_"] / 100)), 1); + } + else { + $list[$i]["_module_sanity_"] = 100; + } + + if (isset($list[$i]["_alerts_"])) { + if ($list[$i]["_monitors_alerts_fired_"] > 0 && $list[$i]["_alerts_"] > 0) { + $list[$i]["_alert_level_"] = format_numeric (100 - ($list[$i]["_monitors_alerts_fired_"] / ($list[$i]["_alerts_"] / 100)), 1); + } + else { + $list[$i]["_alert_level_"] = 100; + } + } + else { + $list[$i]["_alert_level_"] = 100; + $list[$i]["_alerts_"] = 0; + } + + $list[$i]["_monitor_bad_"] = $list[$i]["_monitors_critical_"] + $list[$i]["_monitors_warning_"]; + + if ($list[$i]["_monitor_bad_"] > 0 && $list[$i]["_monitor_checks_"] > 0) { + $list[$i]["_global_health_"] = format_numeric (100 - ($list[$i]["_monitor_bad_"] / ($list[$i]["_monitor_checks_"] / 100)), 1); + } + else { + $list[$i]["_global_health_"] = 100; + } + + $list[$i]["_server_sanity_"] = format_numeric (100 - $list[$i]["_module_sanity_"], 1); + } + + if ($returnAllGroup) { + $list[0]['_agents_unknown_'] += $list[$i]['_agents_unknown_']; + $list[0]['_monitors_alerts_fired_'] += $list[$i]['_monitors_alerts_fired_']; + $list[0]['_total_agents_'] += $list[$i]['_total_agents_']; + $list[0]['_monitors_ok_'] += $list[$i]['_monitors_ok_']; + $list[0]['_monitors_critical_'] += $list[$i]['_monitors_critical_']; + $list[0]['_monitors_warning_'] += $list[$i]['_monitors_warning_']; + $list[0]['_monitors_unknown_'] += $list[$i]['_monitors_unknown_']; + $list[0]['_monitors_not_init_'] += $list[$i]['_monitors_not_init_']; + $list[0]['_agents_not_init_'] += $list[$i]['_agents_not_init_']; + + if ($mode == 'tactical' || $mode == 'tree') { + $list[0]['_agents_ok_'] += $list[$i]['_agents_ok_']; + $list[0]['_agents_warning_'] += $list[$i]['_agents_warning_']; + $list[0]['_agents_critical_'] += $list[$i]['_agents_critical_']; + $list[0]['_monitors_alerts_'] += $list[$i]['_monitors_alerts_']; + } + } + + if ($mode == 'group') { + if (($list[$i]['_agents_unknown_'] == 0) && ($list[$i]['_monitors_alerts_fired_'] == 0) + && ($list[$i]['_total_agents_'] == 0) && ($list[$i]['_monitors_ok_'] == 0) + && ($list[$i]['_monitors_critical_'] == 0) && ($list[$i]['_monitors_warning_'] == 0)) { + unset($list[$i]); + } + } + + } + else if (($config["realtimestats"] == 0) && !$user_strict) { $group_stat = db_get_all_rows_sql ("SELECT * FROM tgroup_stat, tgrupo WHERE tgrupo.id_grupo = tgroup_stat.id_group @@ -2684,7 +2925,7 @@ function group_get_groups_list($id_user = false, $user_strict = false, $access = $acltags = tags_get_user_module_and_tags ($id_user, $access, $user_strict); - if (! defined ('METACONSOLE')) { + if (! $user_strict) { $result_list = group_get_data ($id_user, $user_strict, $acltags, $returnAllGroup, $mode); From a70d751c38a98a4a7882de1d99760e006bce67c8 Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Thu, 16 Apr 2015 13:16:50 +0200 Subject: [PATCH 04/16] Removed the text of the agent search. The text 'All' makes no sense, cause the input searchs for it unnecessarily and the empty texts represents better the lack of filter. --- pandora_console/operation/events/events.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandora_console/operation/events/events.php b/pandora_console/operation/events/events.php index e001e22cae..b2c773a076 100644 --- a/pandora_console/operation/events/events.php +++ b/pandora_console/operation/events/events.php @@ -223,7 +223,7 @@ $date_from = (string)get_parameter('date_from', ''); $date_to = (string)get_parameter('date_to', ''); $server_id = (int)get_parameter('server_id', 0); -$text_agent = (string) get_parameter("text_agent", __("All")); +$text_agent = (string) get_parameter("text_agent"); $id_agent = get_parameter('id_agent', 0); if ($id_agent != 0) { $text_agent = db_get_value('nombre', 'tagente', 'id_agente', $id_agent); From 08dd1832acb4fa19e5279c4c5eed59be7e75e3d2 Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Thu, 16 Apr 2015 13:29:23 +0200 Subject: [PATCH 05/16] Found and removed the async ajax calls (finally!). That was a very bad practice. Also removed the alert, cause after closing it, the blur event was executed again, with another alert that repeated the process. --- pandora_console/include/functions_ui.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pandora_console/include/functions_ui.php b/pandora_console/include/functions_ui.php index 2e9455250c..777d273a5d 100755 --- a/pandora_console/include/functions_ui.php +++ b/pandora_console/include/functions_ui.php @@ -3151,7 +3151,6 @@ function ui_print_agent_autocomplete_input($parameters) { jQuery.ajax ({ data: data_params, - async: false, type: "POST", url: action="' . $javascript_ajax_page . '", timeout: 10000, @@ -3365,15 +3364,12 @@ function ui_print_agent_autocomplete_input($parameters) { jQuery.ajax ({ data: data_params, - async: false, type: "POST", url: action="' . $javascript_ajax_page . '", timeout: 10000, dataType: "json", success: function (data) { if (data.length == 0) { - alert("' . __('Does not exist agent with this name.') . '"); - //Set icon $("#' . $input_id . '") .css("background", From 8826f0c80cd78e9052efd1db84598f51728251cf Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Fri, 17 Apr 2015 16:12:51 +0200 Subject: [PATCH 06/16] Adapted the code to improve the performance of the tree view on the metaconsole with the 'tmetaconsole_agent' --- pandora_console/include/class/Tree.class.php | 306 +++++++++---------- pandora_console/operation/tree.php | 39 +-- 2 files changed, 166 insertions(+), 179 deletions(-) diff --git a/pandora_console/include/class/Tree.class.php b/pandora_console/include/class/Tree.class.php index cdfdb95cd0..14e32454ac 100644 --- a/pandora_console/include/class/Tree.class.php +++ b/pandora_console/include/class/Tree.class.php @@ -351,67 +351,110 @@ class Tree { if (empty($rootID) || $rootID == -1) { if ($this->strictACL) return false; - - $columns = 'tg.id_grupo AS id, tg.nombre AS name, tg.parent, tg.icon, COUNT(DISTINCT(ta.id_agente)) AS total_count'; + + $columns = 'tg.id_grupo AS id, tg.nombre AS name, tg.parent, tg.icon'; $order_fields = 'tg.nombre ASC, tg.id_grupo ASC'; - - // Add the agent counters to the columns - $agent_table = "SELECT COUNT(DISTINCT(ta.id_agente)) - FROM tagente AS ta - LEFT JOIN tagente_modulo AS tam - ON tam.disabled = 0 - AND ta.id_agente = tam.id_agente - $module_search_filter - $module_status_join - WHERE ta.disabled = 0 - AND ta.id_grupo = tg.id_grupo + + if (! defined('METACONSOLE')) { + // Add the agent counters to the columns + $agent_table = "SELECT COUNT(DISTINCT(ta.id_agente)) + FROM tagente AS ta + LEFT JOIN tagente_modulo AS tam + ON tam.disabled = 0 + AND ta.id_agente = tam.id_agente + $module_search_filter + $module_status_join + WHERE ta.disabled = 0 + AND ta.id_grupo = tg.id_grupo + $group_acl + $agent_search_filter + $agent_status_filter"; + $counter_columns = $this->getAgentCounterColumnsSql($agent_table); + if (!empty($counter_columns)) + $columns .= ", $counter_columns"; + + $sql = "SELECT $columns + FROM tgrupo AS tg + LEFT JOIN tagente AS ta + LEFT JOIN tagente_modulo AS tam + ON tam.disabled = 0 + AND ta.id_agente = tam.id_agente + $module_search_filter + $module_status_join + ON ta.disabled = 0 + AND tg.id_grupo = ta.id_grupo $group_acl $agent_search_filter $agent_status_filter - $module_search_filter"; - $counter_columns = $this->getAgentCounterColumnsSql($agent_table); - if (!empty($counter_columns)) - $columns .= ", $counter_columns"; + GROUP BY tg.id_grupo + ORDER BY $order_fields"; + } + // Metaconsole + else { + // Add the agent counters to the columns + $agent_table = "SELECT COUNT(DISTINCT(ta.id_agente)) + FROM tmetaconsole_agent AS ta + WHERE ta.disabled = 0 + AND ta.id_grupo = tg.id_grupo + $group_acl + $agent_search_filter + $agent_status_filter"; + $counter_columns = $this->getAgentCounterColumnsSql($agent_table); + if (!empty($counter_columns)) + $columns .= ", $counter_columns"; + + $sql = "SELECT $columns + FROM tgrupo AS tg + LEFT JOIN tagente AS ta + ON ta.disabled = 0 + AND tg.id_grupo = ta.id_grupo + $group_acl + $agent_search_filter + $agent_status_filter + GROUP BY tg.id_grupo + ORDER BY $order_fields"; + } + } + else { + if (! defined('METACONSOLE') || $this->strictACL) { + $columns = 'ta.id_agente AS id, ta.nombre AS name, + ta.fired_count, ta.normal_count, ta.warning_count, + ta.critical_count, ta.unknown_count, ta.notinit_count, + ta.total_count, ta.quiet'; + $order_fields = 'ta.nombre ASC, ta.id_agente ASC'; - $sql = "SELECT $columns - FROM tgrupo AS tg - LEFT JOIN tagente AS ta - LEFT JOIN tagente_modulo AS tam - ON tam.disabled = 0 - AND ta.id_agente = tam.id_agente - $module_search_filter - $module_status_join - ON ta.disabled = 0 - AND tg.id_grupo = ta.id_grupo + $sql = "SELECT $columns + FROM tagente AS ta + LEFT JOIN tagente_modulo AS tam + ON tam.disabled = 0 + AND ta.id_agente = tam.id_agente + $module_search_filter + $module_status_join + WHERE ta.disabled = 0 + AND ta.id_grupo = $rootID $group_acl $agent_search_filter $agent_status_filter - $module_search_filter - GROUP BY tg.id_grupo - ORDER BY $order_fields"; - } - else { - $columns = 'ta.id_agente AS id, ta.nombre AS name, - ta.fired_count, ta.normal_count, ta.warning_count, - ta.critical_count, ta.unknown_count, ta.notinit_count, - ta.total_count, ta.quiet'; - $order_fields = 'ta.nombre ASC, ta.id_agente ASC'; - - $sql = "SELECT $columns - FROM tagente AS ta - LEFT JOIN tagente_modulo AS tam - ON tam.disabled = 0 - AND ta.id_agente = tam.id_agente - $module_search_filter - $module_status_join - WHERE ta.disabled = 0 - AND ta.id_grupo = $rootID - $group_acl - $agent_search_filter - $agent_status_filter - $module_search_filter - GROUP BY ta.id_agente - ORDER BY $order_fields"; + GROUP BY ta.id_agente + ORDER BY $order_fields"; + } + else { + $columns = 'ta.id_tagente AS id, ta.nombre AS name, + ta.fired_count, ta.normal_count, ta.warning_count, + ta.critical_count, ta.unknown_count, ta.notinit_count, + ta.total_count, ta.quiet, id_tmetaconsole_setup AS server_id'; + $order_fields = 'ta.nombre ASC, ta.id_tagente ASC'; + + $sql = "SELECT $columns + FROM tmetaconsole_agent AS ta + WHERE ta.disabled = 0 + AND ta.id_grupo = $rootID + $group_acl + $agent_search_filter + $agent_status_filter + GROUP BY ta.id_tagente + ORDER BY $order_fields"; + } } break; // Get the modules of an agent @@ -419,7 +462,7 @@ class Tree { $columns = 'tam.id_agente_modulo AS id, tam.nombre AS name, tam.id_tipo_modulo, tam.id_modulo, tae.estado, tae.datos'; $order_fields = 'tam.nombre ASC, tam.id_agente_modulo ASC'; - + $sql = "SELECT $columns FROM tagente_modulo AS tam $module_status_join @@ -1387,8 +1430,12 @@ class Tree { $agent['rootID'] = $this->rootID; $agent['rootType'] = $this->rootType; - if (defined("METACONSOLE") && !empty($server)) - $agent['serverID'] = $server['id']; + if (defined("METACONSOLE")) { + if (isset($agent['server_id'])) + $agent['serverID'] = $agent['server_id']; + else if (!empty($server)) + $agent['serverID'] = $server['id']; + } // Realtime counters for Strict ACL if ($this->strictACL) { @@ -1535,37 +1582,37 @@ class Tree { if (isset($agent['unknown_count'])) $agent['counters']['unknown'] = $agent['unknown_count']; else - $agent['counters']['unknown'] = agents_monitor_unknown($agent['id']); + $agent['counters']['unknown'] = (int) agents_monitor_unknown($agent['id']); if (isset($agent['critical_count'])) $agent['counters']['critical'] = $agent['critical_count']; else - $agent['counters']['critical'] = agents_monitor_critical($agent['id']); + $agent['counters']['critical'] = (int) agents_monitor_critical($agent['id']); if (isset($agent['warning_count'])) $agent['counters']['warning'] = $agent['warning_count']; else - $agent['counters']['warning'] = agents_monitor_warning($agent['id']); + $agent['counters']['warning'] = (int) agents_monitor_warning($agent['id']); if (isset($agent['notinit_count'])) $agent['counters']['not_init'] = $agent['notinit_count']; else - $agent['counters']['not_init'] = agents_monitor_notinit($agent['id']); + $agent['counters']['not_init'] = (int) agents_monitor_notinit($agent['id']); if (isset($agent['normal_count'])) $agent['counters']['ok'] = $agent['normal_count']; else - $agent['counters']['ok'] = agents_monitor_ok($agent['id']); + $agent['counters']['ok'] = (int) agents_monitor_ok($agent['id']); if (isset($agent['total_count'])) $agent['counters']['total'] = $agent['total_count']; else - $agent['counters']['total'] = agents_monitor_total($agent['id']); + $agent['counters']['total'] = (int) agents_monitor_total($agent['id']); if (isset($agent['fired_count'])) $agent['counters']['alerts'] = $agent['fired_count']; else - $agent['counters']['alerts'] = agents_get_alerts_fired($agent['id']); + $agent['counters']['alerts'] = (int) agents_get_alerts_fired($agent['id']); } // Status image @@ -1643,25 +1690,25 @@ class Tree { } } - private static function extractItemWithID ($items, $item_id, $item_type = "group") { + private static function extractItemWithID ($items, $item_id, $item_type = "group", $strictACL = false) { foreach ($items as $item) { if ($item["type"] != $item_type) continue; // Item found - if (! defined("METACONSOLE")) { - if ($item["id"] == $item_id) - return $item; - } - else { + if ($strictACL && defined("METACONSOLE")) { foreach ($item["id"] as $server_id => $id) { if ($id == $item_id) return $item; } } + else { + if ($item["id"] == $item_id) + return $item; + } if ($item["type"] == "group" && !empty($item["children"])) { - $result = self::extractItemWithID($item["children"], $item_id, $item_type); + $result = self::extractItemWithID($item["children"], $item_id, $item_type, $strictACL); // Item found on children if ($result !== false) @@ -1675,7 +1722,7 @@ class Tree { public function getData() { - if (! $this->strictACL) { + if (! defined('METACONSOLE')) { switch ($this->type) { case 'os': $this->getDataOS(); @@ -1699,7 +1746,7 @@ class Tree { $this->getDataExtended(); } } - else { + else if ($this->strictACL) { switch ($this->type) { case 'group': case 'tag': @@ -1710,6 +1757,14 @@ class Tree { break; } } + else { + if ($this->type == 'agent') { + $this->getDataAgent(); + } + else { + $this->getDataGroup(); + } + } } protected function getDataExtended () { @@ -1721,7 +1776,7 @@ class Tree { // Module names if ($this->id == -1) { - + } // Agents else { @@ -1737,19 +1792,13 @@ class Tree { $server = metaconsole_get_servers($this->serverID); if (metaconsole_connect($server) == NOERR) { - db_clean_cache(); - - $newItems = $this->getItems(); - $this->processModules($newItems, $server); - $items = array_merge($items, $newItems); + $items = $this->getItems(); + $this->processModules($items, $server); metaconsole_restore_db(); } } - if (!empty($items)) - usort($items, array("Tree", "cmpSortNames")); - $processed_items = $items; } } @@ -1897,56 +1946,27 @@ class Tree { // Groups if ($this->id == -1) { - if (! defined ('METACONSOLE')) { - $items = $this->getItems(); - - // Build the group hierarchy - foreach ($items as $key => $item) { - if (empty($item['parent'])) { - - unset($items[$key]); - $items_tmp = array(); - $processed_item = $this->getProcessedItem($item, false, $items, $items_tmp, true); - - if (!empty($processed_item) - && isset($processed_item['counters']) - && isset($processed_item['counters']['total']) - && !empty($processed_item['counters']['total'])) - $processed_items[] = $processed_item; - } + + $items = $this->getItems(); + + // Build the group hierarchy + foreach ($items as $key => $item) { + if (empty($item['parent'])) { + + unset($items[$key]); + $items_tmp = array(); + $processed_item = $this->getProcessedItem($item, false, $items, $items_tmp, true); + + if (!empty($processed_item) + && isset($processed_item['counters']) + && isset($processed_item['counters']['total']) + && !empty($processed_item['counters']['total'])) + $processed_items[] = $processed_item; } } - else { - $servers = metaconsole_get_servers(); - - $item_list = array(); - foreach ($servers as $server) { - if (metaconsole_connect($server) != NOERR) - continue; - db_clean_cache(); - - $items = $this->getItems(); - - // Build the group hierarchy - $processed_items = array(); - foreach ($items as $key => $item) { - if (empty($item['parent'])) { - - unset($items[$key]); - $processed_items[] = $this->getProcessedItem($item, $server, $items); - } - } - - $item_list = array_merge($item_list, $processed_items); - - metaconsole_restore_db(); - } - - $processed_items = $this->getMergedItems($item_list); - } // groupID filter. To access the view from tactical views f.e. if (!empty($processed_items) && !empty($this->filter['groupID'])) { - $result = self::extractItemWithID($processed_items, $this->filter['groupID'], "group"); + $result = self::extractItemWithID($processed_items, $this->filter['groupID'], "group", $this->strictACL); if ($result === false) $processed_items = array(); @@ -1956,35 +1976,9 @@ class Tree { } // Agents else { - if (! defined ('METACONSOLE')) { - $items = $this->getItems(); - $this->processAgents($items); - $processed_items = $items; - } - else { - $rootIDs = $this->rootID; - - $items = array(); - foreach ($rootIDs as $serverID => $rootID) { - $server = metaconsole_get_servers($serverID); - if (metaconsole_connect($server) != NOERR) - continue; - db_clean_cache(); - - $this->rootID = $rootID; - $newItems = $this->getItems(); - $this->processAgents($newItems, $server); - $items = array_merge($items, $newItems); - - metaconsole_restore_db(); - } - $this->rootID = $rootIDs; - - if (!empty($items)) - usort($items, array("Tree", "cmpSortNames")); - - $processed_items = $items; - } + $items = $this->getItems(); + $this->processAgents($items); + $processed_items = $items; } $this->tree = $processed_items; diff --git a/pandora_console/operation/tree.php b/pandora_console/operation/tree.php index 12a69aed64..b33f8d88d2 100755 --- a/pandora_console/operation/tree.php +++ b/pandora_console/operation/tree.php @@ -108,13 +108,7 @@ switch ($tab) { break; } -if (defined('METACONSOLE')) { - if ($strict_acl) - $header_sub_title = ''; - - ui_meta_print_header($header_title, $header_sub_title, $tabs); -} -else{ +if (!defined('METACONSOLE')) { if (!$strict_acl) $header_title = $header_title ." - ". $header_sub_title; @@ -153,25 +147,24 @@ $table->rowspan[][count($row)-1] = 2; $table->data[] = $row; -// Module filter -$module_status_arr = array(); -$module_status_arr[-1] = __('All'); //default -$module_status_arr[AGENT_MODULE_STATUS_NORMAL] = __('Normal'); -$module_status_arr[AGENT_MODULE_STATUS_WARNING] = __('Warning'); -$module_status_arr[AGENT_MODULE_STATUS_CRITICAL_BAD] = __('Critical'); -$module_status_arr[AGENT_MODULE_STATUS_UNKNOWN] = __('Unknown'); -$module_status_arr[AGENT_MODULE_STATUS_NOT_INIT] = __('Not init'); +if (!defined('METACONSOLE')) { + // Module filter + $module_status_arr = array(); + $module_status_arr[-1] = __('All'); //default + $module_status_arr[AGENT_MODULE_STATUS_NORMAL] = __('Normal'); + $module_status_arr[AGENT_MODULE_STATUS_WARNING] = __('Warning'); + $module_status_arr[AGENT_MODULE_STATUS_CRITICAL_BAD] = __('Critical'); + $module_status_arr[AGENT_MODULE_STATUS_UNKNOWN] = __('Unknown'); + $module_status_arr[AGENT_MODULE_STATUS_NOT_INIT] = __('Not init'); -$row = array(); -$row[] = __('Module status'); -$row[] = html_print_select($module_status_arr, "status_module", $status_module, '', '', 0, true); -$row[] = __('Search module'); -if (defined('METACONSOLE')) - $row[] = html_print_input_text("search_module", $search_module, '', 70, 30, true); -else + $row = array(); + $row[] = __('Module status'); + $row[] = html_print_select($module_status_arr, "status_module", $status_module, '', '', 0, true); + $row[] = __('Search module'); $row[] = html_print_input_text("search_module", $search_module, '', 40, 30, true); -$table->data[] = $row; + $table->data[] = $row; +} if (defined('METACONSOLE')) { $table->width = "96%"; From 90d5d3cae3c4a4d187ed968446dbf244b33c3d0d Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Tue, 21 Apr 2015 13:58:28 +0200 Subject: [PATCH 07/16] Changed an ajax call from synchronous to asynchronous --- .../include/javascript/pandora_events.js | 1 - .../operation/events/events_list.php | 53 +++++++++---------- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/pandora_console/include/javascript/pandora_events.js b/pandora_console/include/javascript/pandora_events.js index 8d091cfddc..5dfc80c463 100644 --- a/pandora_console/include/javascript/pandora_events.js +++ b/pandora_console/include/javascript/pandora_events.js @@ -159,7 +159,6 @@ function show_response_dialog(event_id, response_id, response) { data: params.join ("&"), type: 'POST', url: action=ajax_file, - async: false, timeout: 10000, dataType: 'html', success: function (data) { diff --git a/pandora_console/operation/events/events_list.php b/pandora_console/operation/events/events_list.php index 0ea6311104..76c6d30f59 100644 --- a/pandora_console/operation/events/events_list.php +++ b/pandora_console/operation/events/events_list.php @@ -17,15 +17,14 @@ // Load global vars global $config; -require_once ($config['homedir']. "/include/functions_events.php"); //Event processing functions -require_once ($config['homedir']. "/include/functions_alerts.php"); //Alerts processing functions -require_once ($config['homedir']. "/include/functions.php"); -require_once($config['homedir'] . "/include/functions_agents.php"); //Agents funtions -require_once($config['homedir'] . "/include/functions_users.php"); //Users functions -require_once ($config['homedir'] . '/include/functions_groups.php'); - -require_once ($config["homedir"] . '/include/functions_graph.php'); -require_once ($config["homedir"] . '/include/functions_tags.php'); +require_once ($config['homedir'] . "/include/functions.php"); +require_once ($config['homedir'] . "/include/functions_events.php"); //Event processing functions +require_once ($config['homedir'] . "/include/functions_alerts.php"); //Alerts processing functions +require_once ($config['homedir'] . "/include/functions_agents.php"); //Agents funtions +require_once ($config['homedir'] . "/include/functions_users.php"); //Users functions +require_once ($config['homedir'] . "/include/functions_groups.php"); +require_once ($config["homedir"] . "/include/functions_graph.php"); +require_once ($config["homedir"] . "/include/functions_tags.php"); check_login (); @@ -36,8 +35,6 @@ if (! check_acl ($config["id_user"], 0, "ER")) { return; } -$strict_user = db_get_value('strict_acl', 'tusuario', 'id_user', $config['id_user']); - if(defined('METACONSOLE')){ $jump = '  '; } @@ -162,7 +159,7 @@ require('events.build_query.php'); $id_name = get_parameter('id_name', ''); -if(!defined("METACONSOLE")) +if (!defined("METACONSOLE")) echo "
"; @@ -174,7 +171,7 @@ $update_pressed = (int) !empty($update_pressed); if ($update_pressed || $open_filter) { $open_filter = true; } -if(!defined("METACONSOLE")){ +if (!defined("METACONSOLE")) { $table = html_get_predefined_table('transparent', 2); $table->styleTable = 'width: 23px; float: right; background: #ECECEC;'; $table->width = '98%'; @@ -275,7 +272,7 @@ $table->width = '98%'; $table->cellspacing = 4; $table->cellpadding = 4; $table->class = 'databox'; -if(defined('METACONSOLE')){ +if (defined('METACONSOLE')) { $table->width = '100%'; $table->cellspacing = 0; $table->cellpadding = 0; @@ -283,7 +280,7 @@ if(defined('METACONSOLE')){ } $table->styleTable = 'font-weight: bold; color: #555; text-align:left;'; -if(!defined("METACONSOLE")) +if (!defined("METACONSOLE")) $table->style[0] = 'width: 50%; width:50%;'; $data = array(); $table->rowid[3] = 'update_filter_row1'; @@ -330,7 +327,7 @@ $tabletags_with->cellspacing = 4; $tabletags_with->cellpadding = 4; $tabletags_with->class = 'noshadow'; $tabletags_with->styleTable = 'border: 0px;'; -if(defined('METACONSOLE')){ +if (defined('METACONSOLE')) { $tabletags_with->width = '100%'; $tabletags_with->class = 'nobady'; $tabletags_with->cellspacing = 0; @@ -340,7 +337,7 @@ if(defined('METACONSOLE')){ $data = array(); -if(!defined("METACONSOLE")) +if (!defined("METACONSOLE")) $data[0] = html_print_select ($tags_select_with, 'select_with', '', '', '', 0, true, true, true, '', false, 'width: 120px; height: 70px;') . '
'; else @@ -365,7 +362,7 @@ $tabletags_without->width = '100%'; $tabletags_without->cellspacing = 4; $tabletags_without->cellpadding = 4; $tabletags_without->class = 'noshadow'; -if(defined('METACONSOLE')){ +if (defined('METACONSOLE')) { $tabletags_without->width = '100%'; $tabletags_without->class = 'nobady'; $tabletags_without->cellspacing = 0; @@ -374,7 +371,7 @@ if(defined('METACONSOLE')){ $tabletags_without->styleTable = 'border: 0px;'; $data = array(); -if(!defined("METACONSOLE")) +if (!defined("METACONSOLE")) $data[0] = html_print_select ($tags_select_without, 'select_without', '', '', '', 0, true, true, true, '', false, 'width: 120px; height: 70px;') . '
'; else @@ -385,7 +382,7 @@ $data[1] = html_print_image('images/darrowright.png', true, array('id' => 'butto $data[1] .= html_print_input_hidden('tag_without', $tag_without_base64, true); $data[1] .= '

' . html_print_image('images/darrowleft.png', true, array('id' => 'button-remove_without', 'style' => 'cursor: pointer;', 'title' => __('Remove'))); -if(!defined("METACONSOLE")) +if (!defined("METACONSOLE")) $data[2] = html_print_select ($tag_without_temp, 'tag_without_temp', array(), '', '', 0, true, true, true, '', false, "width: 120px; height: 70px;"); else @@ -399,11 +396,11 @@ $tabletags_without->rowclass[] = ''; // EVENTS FILTER // Table for filter controls -if(defined('METACONSOLE')){ +if (defined('METACONSOLE')) { $events_filter = '
'; } -else{ +else { $events_filter = ''; } @@ -511,7 +508,7 @@ $table_advanced->data[] = $data; $table_advanced->rowclass[] = ''; $data = array(); -if(defined('METACONSOLE')) +if (defined('METACONSOLE')) { $data[0] = '
' . '' . @@ -526,7 +523,7 @@ if(defined('METACONSOLE')) html_print_table($tabletags_without, true) . '
'; } -else{ +else { $data[0] = '
' . '' . __('Events with following tags') . @@ -550,7 +547,7 @@ $table->width = '100%'; $table->cellspacing = 4; $table->cellpadding = 4; $table->class = 'databox'; -if (defined('METACONSOLE')){ +if (defined('METACONSOLE')) { $table->width = '96%'; $table->class = 'databox_filters'; } @@ -617,7 +614,7 @@ if (check_acl ($config["id_user"], 0, "EW")) { $data[0] .= '' . html_print_image("images/disk.png", true, array("border" => '0', "title" => __('Save filter'), "alt" => __('Save filter'))) . '  '; } -if(defined("METACONSOLE")){ +if (defined("METACONSOLE")) { $data[0] .= '' . html_print_image("images/load.png", true, array("border" => '0', "title" => __('Load filter'), "alt" => __('Load filter'))) . '  '; $data[0] .= '' . @@ -626,7 +623,7 @@ if(defined("METACONSOLE")){ else $data[0] .= '' . html_print_image("images/load.png", true, array("border" => '0', "title" => __('Load filter'), "alt" => __('Load filter'))) . '
'; -if(defined("METACONSOLE")){ +if (defined("METACONSOLE")) { if (empty($id_name)) { $data[0] .= '
[' . __('No filter loaded') . @@ -638,7 +635,7 @@ if(defined("METACONSOLE")){ ']
'; } } -else{ +else { if (empty($id_name)) { $data[0] .= '[' . __('No filter loaded') . From 311fd998118a14d86d3cb1c2112509312dbbadf9 Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Tue, 21 Apr 2015 14:01:10 +0200 Subject: [PATCH 08/16] Improvements on the performance for the metaconsole --- pandora_console/include/ajax/agent.php | 123 ++++------- .../operation/agentes/ver_agente.php | 206 +++++++----------- 2 files changed, 128 insertions(+), 201 deletions(-) diff --git a/pandora_console/include/ajax/agent.php b/pandora_console/include/ajax/agent.php index 73c4660511..c8ad9e3791 100644 --- a/pandora_console/include/ajax/agent.php +++ b/pandora_console/include/ajax/agent.php @@ -185,12 +185,6 @@ if ($search_agents && ((!defined('METACONSOLE')) || $force_local)) { return; } elseif ($search_agents && ($config['metaconsole'] == 1) && defined('METACONSOLE')) { - $servers = db_get_all_rows_sql ("SELECT * - FROM tmetaconsole_setup - WHERE disabled = 0"); - if (!isset($servers)) { - return; - } $id_agent = (int) get_parameter ('id_agent'); $string = (string) get_parameter ('q'); /* q is what autocomplete plugin gives */ @@ -204,7 +198,7 @@ elseif ($search_agents && ($config['metaconsole'] == 1) && defined('METACONSOLE' } } - $filter = array (); + $filter = array(); if ($id_group != -1) { if ($id_group == 0) { @@ -217,90 +211,63 @@ elseif ($search_agents && ($config['metaconsole'] == 1) && defined('METACONSOLE' } } - $data = array(); - foreach ($servers as $server) { - if (metaconsole_load_external_db ($server) != NOERR) { - continue; - } + if (!empty($id_agent)) { + $filter['id_agente'] = $id_agent; + } + + if (!empty($string)) { + $search_filters = array(); //Get agents for only the name. - $filter_agents = $filter; switch ($config['dbtype']) { case "mysql": - $filter_agents[] = '(nombre COLLATE utf8_general_ci LIKE "%'.$string.'%")'; + //Get agents for only the name. + $search_filters[] = "(nombre COLLATE utf8_general_ci LIKE '%$string%')"; + //Get agents for only the address + $search_filters[] = "(direccion LIKE '%$string%')"; + //Get agents for only the description + $search_filters[] = "(comentarios LIKE '%$string%')"; break; case "postgresql": - $filter_agents[] = '(nombre LIKE \'%'.$string.'%\')'; + //Get agents for only the name. + $search_filters[] = "(nombre LIKE '%$string%')"; + //Get agents for only the address + $search_filters[] = "(direccion LIKE '%$string%')"; + //Get agents for only the description + $search_filters[] = "(comentarios LIKE '%$string%')"; break; case "oracle": - $filter_agents[] = '(UPPER(nombre) LIKE UPPER(\'%'.$string.'%\')'; + //Get agents for only the name. + $search_filters[] = "(UPPER(nombre) LIKE UPPER('%$string%')"; + //Get agents for only the address + $search_filters[] = "(UPPER(direccion) LIKE UPPER('%$string%'))"; + //Get agents for only the description + $search_filters[] = "(UPPER(comentarios) LIKE UPPER('%$string%'))"; break; } - $agents = agents_get_agents($filter_agents, array ('id_agente', 'nombre', 'direccion')); - if ($agents !== false) { - foreach ($agents as $agent) { - $data[] = array('id' => $agent['id_agente'], - 'name' => io_safe_output($agent['nombre']), - 'ip' => io_safe_output($agent['direccion']), - 'filter' => 'agent', - 'id_server' => $server['id']); - } - } - //Get agents for only the address - $filter_address = $filter; - switch ($config['dbtype']) { - case "mysql": - $filter_address[] = '(nombre COLLATE utf8_general_ci NOT LIKE "%'.$string.'%" AND direccion LIKE "%'.$string.'%")'; - break; - case "postgresql": - $filter_address[] = '(nombre NOT LIKE \'%'.$string.'%\' AND direccion LIKE \'%'.$string.'%\')'; - break; - case "oracle": - $filter_address[] = '(UPPER(nombre) NOT LIKE UPPER(\'%'.$string.'%\') AND UPPER(direccion) LIKE UPPER(\'%'.$string.'%\'))'; - break; - } - $agents = agents_get_agents($filter_address, array ('id_agente', 'nombre', 'direccion')); - if ($agents !== false) { - foreach ($agents as $agent) { - $data[] = array('id' => $agent['id_agente'], - 'name' => io_safe_output($agent['nombre']), - 'ip' => io_safe_output($agent['direccion']), - 'filter' => 'address', - 'id_server' => $server['id']); - } - } + $search_filters_str = implode($search_filters, ' OR '); - //Get agents for only the description - $filter_description = $filter; - switch ($config['dbtype']) { - case "mysql": - $filter_description[] = - '(nombre COLLATE utf8_general_ci NOT LIKE "%'.$string.'%" AND direccion NOT LIKE "%'.$string.'%" AND comentarios LIKE "%'.$string.'%")'; - break; - case "postgresql": - $filter_description[] = - '(nombre NOT LIKE \'%'.$string.'%\' AND direccion NOT LIKE \'%'.$string.'%\' AND comentarios LIKE \'%'.$string.'%\')'; - break; - case "oracle": - $filter_description[] = - '(UPPER(nombre) NOT LIKE UPPER(\'%'.$string.'%\') AND UPPER(direccion) NOT LIKE UPPER(\'%'.$string.'%\') AND UPPER(comentarios) LIKE UPPER(\'%'.$string.'%\'))'; - break; - } - $agents = agents_get_agents($filter_description, - array ('id_agente', 'nombre', 'direccion')); - if ($agents !== false) { - foreach ($agents as $agent) { - $data[] = array('id' => $agent['id_agente'], - 'name' => io_safe_output($agent['nombre']), - 'ip' => io_safe_output($agent['direccion']), - 'filter' => 'description', - 'id_server' => $server['id']); - } - } - //Restore db connection - metaconsole_restore_db(); + if (!empty($search_filters_str)) + $filter[] = "($search_filters_str)"; + } + $fields = array( + 'id_tagente AS id_agente', 'nombre', + 'direccion', 'id_tmetaconsole_setup AS id_server' + ); + $agents = db_get_all_rows_filter('tmetaconsole_agent', $filter, $fields); + + $data = array(); + + if ($agents !== false) { + foreach ($agents as $agent) { + $data[] = array('id' => $agent['id_agente'], + 'name' => io_safe_output($agent['nombre']), + 'ip' => io_safe_output($agent['direccion']), + 'filter' => 'description', + 'id_server' => $agent['id_server']); + } } echo json_encode($data); diff --git a/pandora_console/operation/agentes/ver_agente.php b/pandora_console/operation/agentes/ver_agente.php index 6f143e1bc1..572348659d 100644 --- a/pandora_console/operation/agentes/ver_agente.php +++ b/pandora_console/operation/agentes/ver_agente.php @@ -423,6 +423,9 @@ if (is_ajax ()) { ($filter != '' ? $filter : false), $indexed); } + if (empty($agent_modules)) + $agent_modules = array(); + foreach ($agent_modules as $key => $module) { $agent_modules[$key]['nombre'] = io_safe_output($module['nombre']); } @@ -438,28 +441,24 @@ if (is_ajax ()) { if ($get_agent_status_tooltip) { $id_agent = (int) get_parameter ('id_agent'); - $metaconsole = (bool)get_parameter('metaconsole', false); - $id_server = (int)get_parameter('id_server', 0); //Metaconsole + $metaconsole = (bool) get_parameter('metaconsole', false); + $id_server = (int) get_parameter('id_server', 0); //Metaconsole $server = null; if ($metaconsole) { - $strict_user = (bool) db_get_value('strict_acl', 'tusuario', 'id_user', $config['id_user']); - $server = db_get_row('tmetaconsole_setup', 'id', $id_server); + $filter = array(); + if (!empty($id_agent)) + $filter['id_tagente'] = $id_agent; + if (!empty($id_server)) + $filter['id_tmetaconsole_setup'] = $id_server; - if (metaconsole_connect($server) != NOERR) { - return; - } - - $agent = db_get_row ('tagente', 'id_agente', $id_agent); - - metaconsole_restore_db(); + $agent = db_get_row_filter('tmetaconsole_agent', $filter); } else { $agent = db_get_row ('tagente', 'id_agente', $id_agent); } - - + if ($agent === false) { return; } echo '

'.$agent['nombre'].'

'; echo ''.__('Main IP').': '.$agent['direccion'].'
'; @@ -474,130 +473,91 @@ if (is_ajax ()) { echo ''.__('Last contact').': '.human_time_comparation($agent['ultimo_contacto']).'
'; echo ''.__('Last remote contact').': '.human_time_comparation($agent['ultimo_contacto_remoto']).'
'; - - # Fix : Only show agents with module with tags of user profile - $_user_tags = tags_get_user_tags($config['id_user'], 'RR'); - $_sql_post = ''; - if (is_array($_user_tags) && !empty($_user_tags)) { + if (!$metaconsole) { + # Fix : Only show agents with module with tags of user profile + $_user_tags = tags_get_user_tags($config['id_user'], 'RR'); - $_tags = implode(',', array_keys($_user_tags)); - - $_sql_post .= ' AND tagente_modulo.id_agente_modulo IN (SELECT a.id_agente_modulo FROM tagente_modulo a, ttag_module b WHERE a.id_agente_modulo=b.id_agente_modulo AND b.id_tag IN (' . $_tags . ')) '; - - } - - $sql = sprintf ('SELECT tagente_modulo.descripcion, - tagente_modulo.nombre - FROM tagente_estado, tagente_modulo - WHERE tagente_modulo.id_agente = %d - AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo - AND tagente_modulo.disabled = 0 - AND tagente_estado.estado = 1', $id_agent); - - $sql .= $_sql_post; - - if ($metaconsole) { - if (metaconsole_connect($server) != NOERR) { - return; + $_sql_post = ''; + if (is_array($_user_tags) && !empty($_user_tags)) { + + $_tags = implode(',', array_keys($_user_tags)); + + $_sql_post .= ' AND tagente_modulo.id_agente_modulo IN (SELECT a.id_agente_modulo FROM tagente_modulo a, ttag_module b WHERE a.id_agente_modulo=b.id_agente_modulo AND b.id_tag IN (' . $_tags . ')) '; + } + $sql = sprintf ('SELECT tagente_modulo.descripcion, + tagente_modulo.nombre + FROM tagente_estado, tagente_modulo + WHERE tagente_modulo.id_agente = %d + AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo + AND tagente_modulo.disabled = 0 + AND tagente_estado.estado = 1', $id_agent); + + $sql .= $_sql_post; + $bad_modules = db_get_all_rows_sql ($sql); - metaconsole_restore_db(); - } - else { - $bad_modules = db_get_all_rows_sql ($sql); - } - - $sql = sprintf ('SELECT COUNT(*) - FROM tagente_modulo - WHERE id_agente = %d - AND disabled = 0', $id_agent); - if ($metaconsole) { - if (metaconsole_connect($server) != NOERR) { - return; - } - + $sql = sprintf ('SELECT COUNT(*) + FROM tagente_modulo + WHERE id_agente = %d + AND disabled = 0', $id_agent); $total_modules = db_get_sql ($sql); - metaconsole_restore_db(); - } - else { - $total_modules = db_get_sql ($sql); - } - - if ($bad_modules === false) - $size_bad_modules = 0; - else - $size_bad_modules = sizeof ($bad_modules); - - // Modules down - if ($size_bad_modules > 0 && (!$metaconsole || !$strict_user)) { - echo ''.__('Monitors down').': '.$size_bad_modules.' / '.$total_modules; - echo '
    '; - foreach ($bad_modules as $module) { - echo '
  • '; - echo ui_print_truncate_text($module['nombre'], 'module_small'); - echo '
  • '; - } - echo '
'; - } - - // Alerts (if present) - $sql = sprintf ('SELECT COUNT(talert_template_modules.id) - FROM talert_template_modules, tagente_modulo, tagente - WHERE tagente.id_agente = %d - AND tagente.disabled = 0 - AND tagente.id_agente = tagente_modulo.id_agente - AND tagente_modulo.disabled = 0 - AND tagente_modulo.id_agente_modulo = talert_template_modules.id_agent_module - AND talert_template_modules.times_fired > 0 ', - $id_agent); - if ($metaconsole) { - if (metaconsole_connect($server) != NOERR) { - return; - } + if ($bad_modules === false) + $size_bad_modules = 0; + else + $size_bad_modules = sizeof ($bad_modules); - $alert_modules = db_get_sql ($sql); - - metaconsole_restore_db(); - } - else { - $alert_modules = db_get_sql ($sql); - } - - if ($alert_modules > 0 && (!$metaconsole || !$strict_user)) { - $sql = sprintf ('SELECT tagente_modulo.nombre, talert_template_modules.last_fired - FROM talert_template_modules, tagente_modulo, tagente - WHERE tagente.id_agente = %d - AND tagente.disabled = 0 - AND tagente.id_agente = tagente_modulo.id_agente - AND tagente_modulo.disabled = 0 - AND tagente_modulo.id_agente_modulo = talert_template_modules.id_agent_module - AND talert_template_modules.times_fired > 0 ', - $id_agent); - if ($metaconsole) { - if (metaconsole_connect($server) != NOERR) { - return; + // Modules down + if ($size_bad_modules > 0) { + echo ''.__('Monitors down').': '.$size_bad_modules.' / '.$total_modules; + echo '
    '; + foreach ($bad_modules as $module) { + echo '
  • '; + echo ui_print_truncate_text($module['nombre'], 'module_small'); + echo '
  • '; } + echo '
'; + } + + // Alerts (if present) + $sql = sprintf ('SELECT COUNT(talert_template_modules.id) + FROM talert_template_modules, tagente_modulo, tagente + WHERE tagente.id_agente = %d + AND tagente.disabled = 0 + AND tagente.id_agente = tagente_modulo.id_agente + AND tagente_modulo.disabled = 0 + AND tagente_modulo.id_agente_modulo = talert_template_modules.id_agent_module + AND talert_template_modules.times_fired > 0 ', + $id_agent); + + $alert_modules = (int) db_get_sql ($sql); + + if ($alert_modules > 0) { + $sql = sprintf ('SELECT tagente_modulo.nombre, talert_template_modules.last_fired + FROM talert_template_modules, tagente_modulo, tagente + WHERE tagente.id_agente = %d + AND tagente.disabled = 0 + AND tagente.id_agente = tagente_modulo.id_agente + AND tagente_modulo.disabled = 0 + AND tagente_modulo.id_agente_modulo = talert_template_modules.id_agent_module + AND talert_template_modules.times_fired > 0 ', + $id_agent); $alerts = db_get_all_rows_sql ($sql); - metaconsole_restore_db(); + echo ''.__('Alerts fired').':'; + echo "
    "; + foreach ($alerts as $alert_item) { + echo '
  • '; + echo ui_print_truncate_text($alert_item['nombre']).' -> '; + echo human_time_comparation($alert_item['last_fired']); + echo '
  • '; + } + echo '
'; } - else { - $alerts = db_get_all_rows_sql ($sql); - } - echo ''.__('Alerts fired').':'; - echo "
    "; - foreach ($alerts as $alert_item) { - echo '
  • '; - echo ui_print_truncate_text($alert_item['nombre']).' -> '; - echo human_time_comparation($alert_item['last_fired']); - echo '
  • '; - } - echo '
'; } return; From 18a58243a4328de377b0667dd1b3a5550e82e47b Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Tue, 21 Apr 2015 14:51:27 +0200 Subject: [PATCH 09/16] Error fixes and performance improvements --- .../godmode/reporting/visual_console_builder.elements.php | 8 +++----- .../godmode/reporting/visual_console_builder.php | 7 ++----- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/pandora_console/godmode/reporting/visual_console_builder.elements.php b/pandora_console/godmode/reporting/visual_console_builder.elements.php index 0809aee4b8..e827e05dbd 100755 --- a/pandora_console/godmode/reporting/visual_console_builder.elements.php +++ b/pandora_console/godmode/reporting/visual_console_builder.elements.php @@ -340,11 +340,9 @@ foreach ($layoutDatas as $layoutData) { $params['javascript_ajax_page'] = '../../ajax.php'; $params['disabled_javascript_on_blur_function'] = true; - $params['print_input_server'] = true; - $params['input_server_id'] = - $params['input_server_name'] = 'id_server_name_' . $idLayoutData; - $params['input_server_value'] = - db_get_value('server_name', 'tmetaconsole_setup', 'id', $layoutData['id_metaconsole']); + $params['print_input_id_server'] = true; + $params['input_id_server_id'] = $params['input_id_server_name'] = 'id_server_id_' . $idLayoutData; + $params['input_id_server_value'] = $layoutData['id_metaconsole']; $params['metaconsole_enabled'] = true; $params['print_hidden_input_idagent'] = true; $params['hidden_input_idagent_name'] = 'id_agent_' . $idLayoutData; diff --git a/pandora_console/godmode/reporting/visual_console_builder.php b/pandora_console/godmode/reporting/visual_console_builder.php index f3ab691db0..df41615261 100755 --- a/pandora_console/godmode/reporting/visual_console_builder.php +++ b/pandora_console/godmode/reporting/visual_console_builder.php @@ -281,11 +281,8 @@ switch ($activeTab) { } $agentName = get_parameter('agent_' . $id, ''); if (defined('METACONSOLE')) { - $values['id_metaconsole'] = db_get_value('id', - 'tmetaconsole_setup', 'server_name', - get_parameter('id_server_name_' . $id, '')); - $values['id_agent'] = - (int)get_parameter('id_agent_' . $id, 0); + $values['id_metaconsole'] = (int) get_parameter('id_server_id_' . $id, ''); + $values['id_agent'] = (int) get_parameter('id_agent_' . $id, 0); } else { $values['id_agent'] = agents_get_agent_id($agentName); From a4431d63a742e27864d3c9acb5f8008507aebf5e Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Thu, 23 Apr 2015 21:12:24 +0200 Subject: [PATCH 10/16] Improved the code maintenance and added support to retrieve agents from the new table of the metaconsole --- pandora_console/include/functions_agents.php | 109 ++++++++----------- 1 file changed, 45 insertions(+), 64 deletions(-) diff --git a/pandora_console/include/functions_agents.php b/pandora_console/include/functions_agents.php index 6541e807e5..052433f26d 100644 --- a/pandora_console/include/functions_agents.php +++ b/pandora_console/include/functions_agents.php @@ -757,13 +757,14 @@ function agents_common_modules ($id_agent, $filter = false, $indexed = true, $ge * @param string $case Which case to return the agentname as (lower, upper, none) * @param boolean $noACL jump the ACL test. * @param boolean $childGroups The flag to get agents in the child group of group parent passed. By default false. - * @param boolean $extra_access The flag to get agents of extra access policies. * * @return array An array with all agents in the group or an empty array */ -function agents_get_group_agents ($id_group = 0, $search = false, $case = "lower", $noACL = false, $childGroups = false, $extra_access = true) { +function agents_get_group_agents ($id_group = 0, $search = false, $case = "lower", $noACL = false, $childGroups = false) { global $config; + $filter = array(); + if (!$noACL) { $id_group = groups_safe_acl($config["id_user"], $id_group, "AR"); @@ -785,43 +786,30 @@ function agents_get_group_agents ($id_group = 0, $search = false, $case = "lower $id_group = array_keys(users_get_groups(false, "AR", true, false, (array)$id_group)); } - if (is_array($id_group)) { - $all_groups = false; - $search_group_sql = sprintf ('id_grupo IN (%s)', implode (",", $id_group)); + if (!empty($id_group)) { + $filter['id_grupo'] = $id_group; } - else if ($id_group == 0) { //All group - $all_groups = true; - $search_group_sql = '1 = 1'; - } - else { - $all_groups = false; - $search_group_sql = sprintf ('id_grupo = %d', $id_group); - } - - $search_sql = '1 = 1'; if ($search === true) { //No added search. Show both disabled and non-disabled } - elseif (is_array ($search)) { + else if (is_array ($search)) { + $filter['disabled'] = 0; if (isset ($search["disabled"])) { - $search_sql .= ' AND disabled = '.($search["disabled"] ? 1 : 0); //Bool, no cleanup necessary + $filter['disabled'] = (int) $search["disabled"]; + + unset ($search["disabled"]); } - else { - $search_sql .= ' AND disabled = 0'; - } - unset ($search["disabled"]); + if (isset ($search["string"])) { $string = io_safe_input ($search["string"]); switch ($config["dbtype"]) { case "mysql": - $search_sql .= ' AND (nombre COLLATE utf8_general_ci LIKE "%'.$string.'%" OR direccion LIKE "%'.$string.'%")'; - break; case "postgresql": - $search_sql .= ' AND (nombre COLLATE utf8_general_ci LIKE \'%'.$string.'%\' OR direccion LIKE \'%'.$string.'%\')'; + $filter[] = "(nombre COLLATE utf8_general_ci LIKE '%$string%' OR direccion LIKE '%$string%')"; break; case "oracle": - $search_sql .= ' AND (UPPER(nombre) LIKE UPPER(\'%'.$string.'%\') OR direccion LIKE upper(\'%'.$string.'%\'))'; + $filter[] = "(UPPER(nombre) LIKE UPPER('%$string%') OR direccion LIKE upper('%$string%'))"; break; } @@ -832,13 +820,11 @@ function agents_get_group_agents ($id_group = 0, $search = false, $case = "lower $name = io_safe_input ($search["name"]); switch ($config["dbtype"]) { case "mysql": - $search_sql .= ' AND nombre COLLATE utf8_general_ci LIKE "' . $name . '" '; - break; case "postgresql": - $search_sql .= ' AND nombre COLLATE utf8_general_ci LIKE \'' . $name . '\' '; + $filter[] = "nombre COLLATE utf8_general_ci LIKE '$name'"; break; case "oracle": - $search_sql .= ' AND UPPER(nombre) LIKE UPPER(\'' . $name . '\') '; + $filter[] = "UPPER(nombre) LIKE UPPER('$name')"; break; } @@ -848,65 +834,60 @@ function agents_get_group_agents ($id_group = 0, $search = false, $case = "lower if (isset($search['status'])) { switch ($search['status']) { case AGENT_STATUS_NORMAL: - $search_sql .= - " AND normal_count = total_count"; + $filter[] = "normal_count = total_count"; break; case AGENT_STATUS_WARNING: - $search_sql .= - " AND critical_count = 0 AND warning_count > 0"; + $filter[] = "(critical_count = 0 AND warning_count > 0)"; break; case AGENT_STATUS_CRITICAL: - $search_sql .= - " AND critical_count > 0"; + $filter[] = "critical_count > 0"; break; case AGENT_STATUS_UNKNOWN: - $search_sql .= - " AND critical_count = 0 AND warning_count = 0 - AND unknown_count > 0"; + $filter[] = "(critical_count = 0 AND warning_count = 0 AND unknown_count > 0)"; break; case AGENT_STATUS_NOT_NORMAL: - $search_sql .= " AND normal_count <> total_count"; + $filter[] = "normal_count <> total_count"; break; case AGENT_STATUS_NOT_INIT: - $search_sql .= " AND notinit_count = total_count"; + $filter[] = "notinit_count = total_count"; break; } unset($search['status']); } + if (defined('METACONSOLE') && isset($search['id_server'])) { + $filter['id_tmetaconsole_setup'] = $search['id_server']; + + unset ($search["id_server"]); + } - if (! empty ($search)) { - $search_sql .= ' AND '.db_format_array_where_clause_sql ($search); + // Add the rest of the filter from the search array + foreach ($search as $key => $value) { + $filter[] = $value; } } else { - $search_sql .= ' AND disabled = 0'; + $filter['disabled'] = 0; } - enterprise_include_once ('include/functions_policies.php'); + $filter['order'] = 'nombre'; - // TODO: CLEAN extra_sql - $extra_sql = ''; - - switch ($config["dbtype"]) { - case "mysql": - case "postgresql": - $sql = sprintf ("SELECT id_agente, nombre - FROM tagente - WHERE (%s %s) AND (%s) - ORDER BY nombre", - $extra_sql, $search_group_sql, $search_sql); - break; - case "oracle": - $sql = sprintf ("SELECT id_agente, nombre - FROM tagente - WHERE (%s %s) AND (%s) - ORDER BY dbms_lob.substr(nombre,4000,1)", - $extra_sql, $search_group_sql, $search_sql); - break; + if (defined('METACONSOLE')) { + $table_name = 'tmetaconsole_agent'; + + $fields = array( + 'id_tagente AS id_agente', 'nombre' + ); + } + else { + $table_name = 'tagente'; + + $fields = array( + 'id_agente', 'nombre' + ); } - $result = db_get_all_rows_sql ($sql); + $result = db_get_all_rows_filter($table_name, $filter, $fields); if ($result === false) return array (); //Return an empty array From 1ae9ee0810e995dc46fad71a659eca35dbb2d309 Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Thu, 23 Apr 2015 21:14:24 +0200 Subject: [PATCH 11/16] Added support to retrieve agents from the new table of the metaconsole --- pandora_console/include/ajax/agent.php | 34 +++++--------------------- 1 file changed, 6 insertions(+), 28 deletions(-) diff --git a/pandora_console/include/ajax/agent.php b/pandora_console/include/ajax/agent.php index c8ad9e3791..e49ed796d4 100644 --- a/pandora_console/include/ajax/agent.php +++ b/pandora_console/include/ajax/agent.php @@ -36,34 +36,12 @@ if ($get_agents_group) { $return = array(); if ($id_group != -1) { - if (defined('METACONSOLE')) { - - if ($id_server == 0) { - $servers = $servers = db_get_all_rows_sql ("SELECT * - FROM tmetaconsole_setup - WHERE disabled = 0"); - } - else { - $servers = db_get_all_rows_sql ("SELECT * - FROM tmetaconsole_setup - WHERE id = " . $id_server . " - AND disabled = 0"); - } - - foreach ($servers as $server) { - if (metaconsole_load_external_db ($server) != NOERR) { - continue; - } - - $return = agents_get_group_agents($id_group); - - //Restore db connection - metaconsole_restore_db(); - } - } - else { - $return = agents_get_group_agents($id_group); - } + $filter = array(); + + if (defined('METACONSOLE')) + $filter['id_server'] = $id_server; + + $return = agents_get_group_agents($id_group, $filter, "none"); } switch ($mode) { From afc54630a8819ddead32d7629825d10a42e0e783 Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Thu, 23 Apr 2015 21:15:13 +0200 Subject: [PATCH 12/16] Improved the code readability and added support to retrieve the agents from the new table of the metaconsole --- .../visual_console_builder.wizard.php | 105 +++++++++--------- 1 file changed, 53 insertions(+), 52 deletions(-) diff --git a/pandora_console/godmode/reporting/visual_console_builder.wizard.php b/pandora_console/godmode/reporting/visual_console_builder.wizard.php index 0a2fcc94f4..ba88c295df 100644 --- a/pandora_console/godmode/reporting/visual_console_builder.wizard.php +++ b/pandora_console/godmode/reporting/visual_console_builder.wizard.php @@ -183,7 +183,7 @@ if (defined('METACONSOLE')) { $table->rowstyle["all_2"] = 'display: none;'; $table->data["all_2"][0] = __('Servers'); $table->data["all_2"][1] = html_meta_print_select_servers(false, - false, 'servers', '', '', '', 0, true); + false, 'servers', '', 'metaconsole_init();', '', 0, true); } @@ -210,8 +210,12 @@ $table->data["all_one_item_per_agent"][1] .= html_print_input_hidden( $table->rowstyle["all_4"] = 'display: none;'; $table->data["all_4"][0] = __('Agents'); -$table->data["all_4"][1] = html_print_select( - agents_get_group_agents(0, false, "none", false, true), + +$agents_list = array(); +if (!defined('METACONSOLE')) + $agents_list = agents_get_group_agents(0, false, "none", false, true); + +$table->data["all_4"][1] = html_print_select($agents_list, 'id_agents[]', 0, false, '', '', true, true); $table->data["all_4"][2] = ' ' . __('Modules') . ''; @@ -272,7 +276,7 @@ else { onsubmit="if (! confirm(\''.__('Are you sure to add many elements\nin visual map?').'\')) return false; else return check_fields();">'; } -if(defined("METACONSOLE")){ +if (defined("METACONSOLE")) { echo "
" . __('Wizard') . "
"; } @@ -296,16 +300,14 @@ echo ''; echo ''; ?>