From b184efd4d566c19fa1a42ac1929873810e4b51e7 Mon Sep 17 00:00:00 2001 From: guruevi Date: Thu, 20 Nov 2008 14:09:26 +0000 Subject: [PATCH] 2008-11-20 Evi Vanoost * reporting/fgraph.php: Updated for some faster processing and new functions. * operation/events/events.php, operation/agentes/estado_alertas, include/functions_reporting.php, godmode/reporting/reporting_bulder.php, godmode/reporting/map_builder.php, godmode/db/db_info.php: Function name updates * include/functions_html.php: Small style update * include/functions_db.php: get_agents_in_group is now get_group_agents. Accepts array and specified case. get_modules_in_agent is now get_agentmodules and accepts arrays of agents as well as which details to select. Deprecated dame_nombre_agente in favor of get_agent_name which accepts case. Fixed get_agent_addresses for use in select boxes. Added filter to get_agentmodule_count * godmode/db/db_purge.php: Fixed queries and style updates * godmode/agentes/configurar_agente.php: Fixed IP select box * godmode/agentes/agent_manager.php: Style update and fixed a bug git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@1250 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_console/ChangeLog | 28 +++ .../godmode/agentes/agent_manager.php | 223 +++++++----------- .../godmode/agentes/configurar_agente.php | 10 +- pandora_console/godmode/db/db_info.php | 2 +- pandora_console/godmode/db/db_purge.php | 52 ++-- .../godmode/reporting/map_builder.php | 11 +- .../godmode/reporting/reporting_builder.php | 9 +- pandora_console/include/functions_db.php | 154 +++++++++--- pandora_console/include/functions_html.php | 7 +- .../include/functions_reporting.php | 30 +-- .../operation/agentes/estado_alertas.php | 8 +- pandora_console/operation/events/events.php | 2 +- pandora_console/reporting/fgraph.php | 11 +- 13 files changed, 292 insertions(+), 255 deletions(-) diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog index cc5b5db048..c9d35aac61 100644 --- a/pandora_console/ChangeLog +++ b/pandora_console/ChangeLog @@ -1,3 +1,31 @@ +2008-11-20 Evi Vanoost + + * reporting/fgraph.php: Updated for some faster processing + and new functions. + + * operation/events/events.php, + operation/agentes/estado_alertas, + include/functions_reporting.php, + godmode/reporting/reporting_bulder.php, + godmode/reporting/map_builder.php, godmode/db/db_info.php: + Function name updates + + * include/functions_html.php: Small style update + + * include/functions_db.php: get_agents_in_group is now + get_group_agents. Accepts array and specified case. + get_modules_in_agent is now get_agentmodules and accepts arrays + of agents as well as which details to select. Deprecated + dame_nombre_agente in favor of get_agent_name which accepts case. + Fixed get_agent_addresses for use in select boxes. Added filter + to get_agentmodule_count + + * godmode/db/db_purge.php: Fixed queries and style updates + + * godmode/agentes/configurar_agente.php: Fixed IP select box + + * godmode/agentes/agent_manager.php: Style update and fixed a bug + 2008-11-19 Jorge Gonzalez * pandoradb_data.sql: Added Arab and Turkish translations to the diff --git a/pandora_console/godmode/agentes/agent_manager.php b/pandora_console/godmode/agentes/agent_manager.php index 68d73bde56..a16c9937f3 100644 --- a/pandora_console/godmode/agentes/agent_manager.php +++ b/pandora_console/godmode/agentes/agent_manager.php @@ -19,8 +19,9 @@ // ======================== // AGENT GENERAL DATA FORM // ======================== + // Load global vars -require ('include/config.php'); +require_once ('include/config.php'); enterprise_include ('godmode/agentes/agent_manager.php'); @@ -30,190 +31,136 @@ if (! give_acl ($config['id_user'], 0, "AW")) { audit_db ($config['id_user'], $REMOTE_ADDR, "ACL Violation", "Trying to access agent manager"); require ("general/noaccess.php"); - return; + exit; } echo "

".__('Agent configuration'); -if (isset($_GET["create_agent"])){ +if (isset($_GET["create_agent"])) { $create_agent = 1; echo " > ".__('Create agent'); } else { echo " > ".__('Update agent'); } echo "

"; -echo "
"; +echo '
 
'; // Agent remote configuration editor -$agent_md5 = md5($nombre_agente, FALSE); -if (isset($_GET["disk_conf"])){ +$agent_md5 = md5 ($nombre_agente, FALSE); +$filename['md5'] = $config["remote_config"] . "/" . $agent_md5 . ".md5"; +$filename['conf'] = $config["remote_config"] . "/" . $agent_md5 . ".conf"; + +if (isset ($_GET["disk_conf"])) { require ("agent_disk_conf_editor.php"); exit; } // Agent remote configuration DELETE -if (isset($_GET["disk_conf_delete"])){ - $agent_md5 = md5($nombre_agente, FALSE); - $file_name = $config["remote_config"] . "/" . $agent_md5 . ".conf"; - unlink ($file_name); - $file_name = $config["remote_config"] . "/" . $agent_md5 . ".md5"; - unlink ($file_name); +if (isset($_GET["disk_conf_delete"])) { + //TODO: Get this working on computers where the Pandora server(s) are not on the webserver + //TODO: Get a remote_config editor working in the open version + unlink ($filename['md5']); + unlink ($filename['conf']); } -echo '
'; -echo ''; -echo ""; -echo '"; + $table->data[1][1] .= '    '; + + $ip_all = get_agent_addresses ($id_agente); + + $table->data[1][1] .= print_select ($ip_all, "address_list", $direccion_agente, '', '', 0, true); + $table->data[1][1] .= print_checkbox ("delete_ip", 1, false, true).__('Delete selected'); } -echo '"; -echo ''; -echo ''; -echo '
'.__('Agent name').' '.__('The Agent\'s name must be the same as the one defined at the Console').''; -print_input_text ('agente', $nombre_agente, '', 30, 100); +echo ''; + +$table->width = 650; +$table->cellpadding = 4; +$table->cellspacing = 4; +$table->class = "databox_color"; + +$table->head = array (); +$table->data = array (); + +$table->data[0][0] = ''.__('Agent name').''.print_help_tip (__("The agent's name must be the same as the one defined at the console"), true); +$table->data[0][1] = print_input_text ('agente', $nombre_agente, '', 30, 100,true); if (isset ($id_agente) && $id_agente != "") { - echo " - - "; -} -// Remote configuration available -if (file_exists ($config["remote_config"] . "/" . $agent_md5 . ".md5")) { - echo " - - "; - echo ' '.__('You can remotely edit this agent configuration').''; + $table->data[0][1] .= ' + '; } -echo '
'; -echo ''.__('IP Address').''; -echo ''; -print_input_text ('direccion', $direccion_agente, '', 16, 100); +// Remote configuration available +if (file_exists ($filename['md5'])) { + $table->data[0][1] .= ' + '.print_help_tip (__('You can remotely edit this agent configuration'), true); +} + +$table->data[1][0] = ''.__('IP Address').''; +$table->data[1][1] = print_input_text ('direccion', $direccion_agente, '', 16, 100, true); if ($create_agent != 1) { - echo "    "; - - echo '"; - - echo " ".__('Delete selected'); - echo "
'.__('Parent').''; -echo ''; -print_select_from_sql ('SELECT id_agente, nombre FROM tagente ORDER BY nombre', - 'id_parent', $id_parent, '', 'None', '0'); +$groups = get_user_groups ($config["id_user"]); +$agents = get_group_agents (array_keys ($groups)); -echo '
'.__('Group').''; -echo ''; -print_select_from_sql ('SELECT id_grupo, nombre FROM tgrupo ORDER BY nombre', - 'grupo', $grupo, '', '', ''); +$table->data[2][0] = ''.__('Parent').''; +$table->data[2][1] = print_select ($agents, 'id_parent', $id_parent, '', get_agent_name ($id_parent, "lower"), $id_parent, true, false, false); //I use get_agent_name because the user might not have rights to the current parent -echo "
"; -echo "".__('Interval')."'; +$table->data[3][0] = ''.__('Group').''; +$table->data[3][1] = print_select ($groups, 'grupo', $grupo, '', '', 0, true, false, false); -echo '
'.__('OS').''; -print_select_from_sql ('SELECT id_os, name FROM tconfig_os ORDER BY name', - 'id_os', $id_os, '', '', ''); +$table->data[4][0] = ''.__('Interval').''; +$table->data[4][1] = print_input_text ('intervalo', $intervalo, '', 16, 100, true); + +$table->data[5][0] = ''.__('OS').''; +$table->data[5][1] = print_select_from_sql ('SELECT id_os, name FROM tconfig_os ORDER BY name', 'id_os', $id_os, '', '', '0', true); // Network server -echo '
'.__('Network Server').''; -echo ' '.__('You must select a Network Server for the Agent, so it can work properly with this kind of modules').''; -echo ''; -$none = ''; -$none_value = ''; -if ($id_network_server == 0) { - $none = __('None'); - $none_value = 0; -} -print_select_from_sql ('SELECT id_server, name FROM tserver WHERE network_server = 1 ORDER BY name', - 'network_server', $id_network_server, '', $none, $none_value); +$table->data[6][0] = ''.__('Network Server').''.print_help_tip (__('You must select a Network Server for the Agent, so it can work properly with this kind of modules'), true); +$table->data[6][1] = print_select_from_sql ('SELECT id_server, name FROM tserver WHERE network_server = 1 ORDER BY name', 'network_server', $id_network_server, '', '', 0, true); -// Plugin Server -echo '
'.__('Plugin Server').''; -echo ' '.__('You must select a Plugin Server for the Agent, so it can work properly with this kind of modules').''; -echo ''; -$none_str = __('None'); -$none = ''; -$none_value = ''; -if ($id_plugin_server == 0) { - $none = $none_str; - $none_value = 0; -} -print_select_from_sql ('SELECT id_server, name FROM tserver WHERE plugin_server = 1 ORDER BY name', - 'plugin_server', $id_plugin_server, '', $none, $none_value); +// Plugin server +$table->data[7][0] = ''.__('Plugin Server').''.print_help_tip (__('You must select a Plugin Server for the Agent, so it can work properly with this kind of modules'), true); +$table->data[7][1] = print_select_from_sql ('SELECT id_server, name FROM tserver WHERE plugin_server = 1 ORDER BY name', 'plugin_server', $id_plugin_server, '', '', 0, true); // WMI Server -echo '
'.__('WMI Server').''; -echo ' '.__('You must select a WMI Server for the Agent, so it can work properly with this kind of modules').''; -echo ''; -$none = ''; -$none_value = ''; -if ($id_wmi_server == 0) { - $none = $none_str; - $none_value = 0; -} -print_select_from_sql ('SELECT id_server, name FROM tserver WHERE wmi_server = 1 ORDER BY name', - 'wmi_server', $id_wmi_server, '', $none, $none_value); +$table->data[8][0] = ''.__('WMI Server').''.print_help_tip (__('You must select a WMI Server for the Agent, so it can work properly with this kind of modules'), true); +$table->data[8][1] = print_select_from_sql ('SELECT id_server, name FROM tserver WHERE wmi_server = 1 ORDER BY name', 'wmi_server', $id_wmi_server, '', '', 0, true); // Prediction Server -echo '
'.__('Prediction Server').''; -echo ' '.__('You must select a Prediction Server for the Agent, so it can work properly with this kind of modules').''; -echo ''; -$none = ''; -$none_value = ''; -if ($id_prediction_server == 0) { - $none = $none_str; - $none_value = 0; -} -print_select_from_sql ('SELECT id_server, name FROM tserver WHERE prediction_server = 1 ORDER BY name', - 'prediction_server', $id_prediction_server, '', $none, $none_value); +$table->data[9][0] = ''.__('Prediction Server').''.print_help_tip (__('You must select a Prediction Server for the Agent, so it can work properly with this kind of modules'), true); +$table->data[9][1] = print_select_from_sql ('SELECT id_server, name FROM tserver WHERE prediction_server = 1 ORDER BY name', 'prediction_server', $id_prediction_server, '', '', 0, true); enterprise_hook ('inventory_server'); // Description -echo '
'; -echo __('Description'); -echo ''; -print_input_text ('comentarios', $comentarios, '', 45, 255); +$table->data[10][0] = ''.__('Description').''; +$table->data[10][1] = print_input_text ('comentarios', $comentarios, '', 45, 255, true); -// Learn mode / Normal mode -echo '
'; -echo __('Module definition'); -pandora_help("module_definition"); -echo ''; -echo __('Learning mode'); -print_radio_button_extended ("modo", 1, '', $modo, false, '', 'style="margin-right: 40px;"'); -echo __('Normal mode'); -print_radio_button_extended ("modo", 0, '', $modo, false, '', 'style="margin-right: 40px;"'); +// Learn mode / Normal mode +$table->data[11][0] = ''.__('Module definition').''.pandora_help("module_definition", true); +$table->data[11][1] = __('Learning mode').' '.print_radio_button_extended ("modo", 1, '', $modo, false, '', 'style="margin-right: 40px;"', true); +$table->data[11][1] .= __('Normal mode').' '.print_radio_button_extended ("modo", 0, '', $modo, false, '', 'style="margin-right: 40px;"', true); // Status (Disabled / Enabled) -echo '
'.__('Status').''; -echo ''; -echo __('Disabled'); -print_radio_button_extended ("disabled", 1, '', $disabled, false, '', 'style="margin-right: 40px;"'); -echo __('Active'); -print_radio_button_extended ("disabled", 0, '', $disabled, false, '', 'style="margin-right: 40px;"'); +$table->data[12][0] = ''.__('Status').''; +$table->data[12][1] = __('Disabled').' '.print_radio_button_extended ("disabled", 1, '', $disabled, false, '', 'style="margin-right: 40px;"', true); +$table->data[12][1] .= __('Active').' '.print_radio_button_extended ("disabled", 0, '', $disabled, false, '', 'style="margin-right: 40px;"', true); // Remote configuration -echo '
'.__('Remote configuration').''; -echo ''; -$filename = $config["remote_config"] . "/" . $agent_md5 . ".md5"; -if (file_exists($filename)){ - echo date("F d Y H:i:s.", fileatime($filename)); +$table->data[13][0] = ''.__('Remote configuration').''; + + +if (file_exists ($filename['md5'])) { + $table->data[13][1] = date ("F d Y H:i:s.", fileatime ($filename['md5'])); // Delete remote configuration - echo ""; + $table->data[13][1] .= ''; } else { - echo ''.__('Not available').''; + $table->data[13][1] = ''.__('Not available').''; } -echo '
'; +print_table ($table); +unset ($table); + +echo '
'; if ($create_agent == 1) { print_submit_button (__('Create'), 'crtbutton', false, 'class="sub wand"'); print_input_hidden ('create_agent', 1); @@ -222,7 +169,5 @@ if ($create_agent == 1) { print_input_hidden ('update_agent', 1); print_input_hidden ('id_agente', $id_agente); } - -echo "
"; - +echo ''; ?> diff --git a/pandora_console/godmode/agentes/configurar_agente.php b/pandora_console/godmode/agentes/configurar_agente.php index bc57994857..4d864278e9 100644 --- a/pandora_console/godmode/agentes/configurar_agente.php +++ b/pandora_console/godmode/agentes/configurar_agente.php @@ -37,7 +37,7 @@ if (! give_acl($config["id_user"], $group, "AW")) { // Get passed variables -$tab = get_parameter_get ("tab","main"); +$tab = get_parameter_get ("tab", "main"); $form_moduletype = get_parameter_post ("form_moduletype"); $form_alerttype = get_parameter ("form_alerttype"); $moduletype = get_parameter_get ("moduletype"); @@ -490,6 +490,14 @@ if (isset($_POST["update_agent"])) { // if modified some agent paramenter $id_agente = (int) get_parameter_post ("id_agente", 0); $nombre_agente = (string) get_parameter_post ("agente"); $direccion_agente = (string) get_parameter_post ("direccion"); + $address_list = (string) get_parameter_post ("address_list"); + if ($address_list != $direccion_agente && $direccion_agente == get_agent_address ($id_agente) && $address_list != get_agent_address ($id_agente)) { + //If we selected another IP in the drop down list to be 'primary': + // a) field is not the same as selectbox + // b) field has not changed from current IP + // c) selectbox is not the current IP + $direccion_agente = $address_list; + } $grupo = (int) get_parameter_post ("grupo", 0); $intervalo = (int) get_parameter_post ("intervalo", 300); $comentarios = (string) get_parameter_post ("comentarios"); diff --git a/pandora_console/godmode/db/db_info.php b/pandora_console/godmode/db/db_info.php index c1c6c6f6d1..459674b00c 100644 --- a/pandora_console/godmode/db/db_info.php +++ b/pandora_console/godmode/db/db_info.php @@ -82,7 +82,7 @@ foreach ($result as $row) { $data[0] = ''.dame_nombre_agente ($row["id_agente"]).''; //Second row is a number of modules for the agent - $data[1] = count (get_modules_in_agent ($row["id_agente"])); + $data[1] = get_agentmodule_count ($row["id_agente"]); //Then the number of data packets for the agent $data[2] = $row["count"]; diff --git a/pandora_console/godmode/db/db_purge.php b/pandora_console/godmode/db/db_purge.php index d4dacc8a29..47966aff54 100644 --- a/pandora_console/godmode/db/db_purge.php +++ b/pandora_console/godmode/db/db_purge.php @@ -31,7 +31,7 @@ if (! give_acl ($config['id_user'], 0, "DM")) { //id_agent = -1: None selected; id_agent = 0: All if (isset ($_POST["agent"])){ - $id_agent = (int) get_parameter_post ("agent",-1); //Default to none selected + $id_agent = (int) get_parameter_post ("agent", -1); //Default to none selected } else { $id_agent = -1; } @@ -78,13 +78,13 @@ $data["total"] = 0; # Purge data using dates if (isset($_POST["purgedb"])) { - $from_date = get_parameter_post ("date_purge",0); //0: No time selected + $from_date = get_parameter_post ("date_purge", 0); //0: No time selected if ($id_agent > 0) { echo __('Purge task launched for agent')." ".dame_nombre_agente ($id_agent)." :: ".__('Data older than')." ".human_time_description ($from_date); echo "

".__('Please be patient. This operation can take a long time depending on the amount of modules.')."

"; - $sql = sprintf ("SELECT id_agente_modulo FROM tagente_modulo WHERE id_agente = %d",$id_agent); - $result=get_db_all_rows_sql($sql); + $sql = sprintf ("SELECT id_agente_modulo FROM tagente_modulo WHERE id_agente = %d", $id_agent); + $result=get_db_all_rows_sql ($sql); if (empty ($result)) { $result = array (); } @@ -98,13 +98,13 @@ if (isset($_POST["purgedb"])) { echo "
"; flush (); //Flush here in case there are errors and the script dies, at least we know where we ended set_time_limit (); //Reset the time limit just in case - $sql = sprintf("DELETE FROM `tagente_datos` WHERE `id_agente_modulo` = '%d' AND `utimestamp` < '%d'",$row["id_agente_modulo"],$from_date); + $sql = sprintf ("DELETE FROM `tagente_datos` WHERE `id_agente_modulo` = %d AND `utimestamp` < %d",$row["id_agente_modulo"],$from_date); if (process_sql ($sql) === false) $errors++; - $sql = sprintf("DELETE FROM `tagente_datos_inc` WHERE `id_agente_modulo` = '%d' AND `utimestamp` < '%d'",$row["id_agente_modulo"],$from_date); + $sql = sprintf ("DELETE FROM `tagente_datos_inc` WHERE `id_agente_modulo` = %d AND `utimestamp` < %d",$row["id_agente_modulo"],$from_date); if (process_sql ($sql) === false) $errors++; - $sql = sprintf("DELETE FROM `tagente_datos_string` WHERE `id_agente_modulo` = '%d' AND `utimestamp` < '%d'",$row["id_agente_modulo"],$from_date); + $sql = sprintf ("DELETE FROM `tagente_datos_string` WHERE `id_agente_modulo` = %d AND `utimestamp` < %d",$row["id_agente_modulo"],$from_date); if (process_sql ($sql) === false) $errors++; } @@ -120,39 +120,29 @@ if (isset($_POST["purgedb"])) { //All agents echo __('Deleting records for all agents'); flush (); - //ob_flush(); - $query = sprintf("DELETE FROM `tagente_datos` WHERE `utimestamp` < '%d'",$from_date); + $query = sprintf ("DELETE FROM `tagente_datos` WHERE `utimestamp` < %d",$from_date); process_sql ($query); - $query = sprintf("DELETE FROM `tagente_datos_inc` WHERE `utimestamp` < '%d'",$from_date); + $query = sprintf ("DELETE FROM `tagente_datos_inc` WHERE `utimestamp` < %d",$from_date); process_sql ($query); - $query = sprintf("DELETE FROM `tagente_datos_string` WHERE `utimestamp` < '%d'",$from_date); + $query = sprintf ("DELETE FROM `tagente_datos_string` WHERE `utimestamp` < %d",$from_date); process_sql ($query); } echo "

"; } # Select Agent for further operations. -echo '
- -
'; - +$agents = get_group_agents (1, true); $agents[-1] = __('Choose agent'); $agents[0] = __('All agents'); -$result = get_agents_in_group (1); -if ($result === false) - $result = array(); - -foreach ($result as $row) { - $agents[$row["id_agente"]] = $row["nombre"]; -} - -print_select ($agents, "agent", $id_agent, "", "", "", false, false, false); +echo ''; +echo '
'; +print_select ($agents, "agent", $id_agent, "this.form.submit();", "", "", false, false, false); print_help_tip (__("Select the agent you want information about")); - -echo '
'; +echo '

'; +echo '
'; if ($id_agent > 0) { $title = __('Information on agent').' '.dame_nombre_agente ($id_agent).' '.__('in the database'); @@ -161,10 +151,12 @@ if ($id_agent > 0) { } echo "

".$title."

"; -flush (); -$query = ""; +flush (); //Flush before we do some SQL stuff if ($id_agent > 0) { //If the agent is not All or Not selected - $query = sprintf (" AND id_agente_modulo = ANY(SELECT id_agente_modulo FROM tagente_modulo WHERE id_agente = '%d' ",$id_agent); + $modules = get_agentmodules ($id_agent); + sprintf ("AND id_agente_modulo IN(%s)", implode (",", array_keys ($modules))); +} else { + $query = ""; } $data["1day"] = get_db_sql (sprintf ("SELECT COUNT(id_agente_datos) FROM tagente_datos WHERE utimestamp > %d %s", $time["1day"], $query)); diff --git a/pandora_console/godmode/reporting/map_builder.php b/pandora_console/godmode/reporting/map_builder.php index ba49b13493..508d91d9dd 100644 --- a/pandora_console/godmode/reporting/map_builder.php +++ b/pandora_console/godmode/reporting/map_builder.php @@ -369,15 +369,8 @@ if (! $edit_layout && ! $id_layout) { $intervals[1440] = __('Two Months'); $intervals[4320] = __('Six Months'); - $all_agents = get_agents_in_group ($id_group); - $agents = array (); - if ($all_agents !== false) { - foreach ($all_agents as $agent) { - $agents[$agent['id_agente']] = strtolower($agent['nombre']); - } - asort($agents); - } - + $agents = get_group_agents ($id_group); + echo '
'; echo '

'.__('Map element editor').'

'; echo __('Drag an element here to edit the properties'); diff --git a/pandora_console/godmode/reporting/reporting_builder.php b/pandora_console/godmode/reporting/reporting_builder.php index 947a849fcc..07f3393582 100644 --- a/pandora_console/godmode/reporting/reporting_builder.php +++ b/pandora_console/godmode/reporting/reporting_builder.php @@ -202,14 +202,7 @@ if ($id_report) { $report_id_group = $report['id_group']; } -$all_agents = get_agents_in_group ($report_id_group); -$agents = array (); -if ($all_agents !== false) { - foreach ($all_agents as $agent) { - $agents[$agent['id_agente']] = strtolower($agent['nombre']); - } - asort ($agents); -} +$agents = get_group_agents ($report_id_group); if ($edit_sla_report_content) { /* Edit SLA report form */ diff --git a/pandora_console/include/functions_db.php b/pandora_console/include/functions_db.php index b2f0613ab3..dfd47ae8eb 100644 --- a/pandora_console/include/functions_db.php +++ b/pandora_console/include/functions_db.php @@ -190,44 +190,100 @@ function give_disabled_group ($id_group) { } /** - * Get all the agents in a group. + * Get all the agents within a group(s). * * @param id_group Group id or a comma delimited list of id_groups or an array * of ID's * * @param disabled Add disabled agents to agents. Default: False. + * + * @param case Which case to return the agentname as (lower, upper, none) * - * @return An array with all agents in the group. + * @return An array with all agents in the group or an empty array */ -function get_agents_in_group ($id_group, $disabled = false) { - if (is_array ($id_group)) //If id_group is an array, then - $id_group = implode (",", $id_group); +function get_group_agents ($id_group, $disabled = false, $case = "lower") { + $id_group = safe_int ($id_group, 1); + + if (is_array ($id_group)) { + //If id_group is an array, then + if (in_array (1, $id_group)) { + //If All is included in the group list, just select All + $id_group = 1; + } else { + //If All is not included, select what we need + $id_group = implode (",", $id_group); + } + } /* 'All' group must return all agents */ - if ($id_group == 1) { - if ($disabled) { - return get_db_all_rows_in_table ('tagente', 'nombre'); - } else { - return get_db_all_rows_field_filter ('tagente', 'disabled', 0, 'nombre'); - } - } elseif ($disabled && $id_group != 1) { - $sql = sprintf ("SELECT * FROM tagente WHERE id_grupo IN (%s) ORDER BY nombre",$id_group); - return get_db_all_rows_sql ($sql); - } else { - $sql = sprintf ("SELECT * FROM tagente WHERE id_grupo IN (%s) AND disabled = 0 ORDER BY nombre",$id_group); - return get_db_all_rows_sql ($sql); + $search = ''; + if (!empty ($id_group) && $id_group > 1) { + $search .= sprintf (' WHERE id_grupo IN (%s)', $id_group); } + if ($disabled !== false) { + $search .= (($search == '') ? ' WHERE' : ' AND' ).' disabled = 0'; + } + + $sql = sprintf ("SELECT id_agente, nombre FROM tagente%s ORDER BY nombre", $search); + $result = get_db_all_rows_sql ($sql); + + if ($result === false) + return array (); //Return an empty array + + $agents = array (); + foreach ($result as $row) { + switch ($case) { + case "lower": + $agents[$row["id_agente"]] = mb_strtolower ($row["nombre"],"UTF-8"); + break; + case "upper": + $agents[$row["id_agente"]] = mb_strtoupper ($row["nombre"],"UTF-8"); + break; + default: + $agents[$row["id_agente"]] = $row["nombre"]; + } + } + return ($agents); } /** * Get all the modules in an agent. * * @param $id_agent Agent id + * @param $details Array, comma delimited list or singular value of rows to select. If nothing is specified, nombre will be selected * - * @return An array with all modules in the agent. + * @return An array with all modules in the agent. If multiple rows are selected, they will be in an array */ -function get_modules_in_agent ($id_agent) { - return get_db_all_rows_field_filter ('tagente_modulo', 'id_agente', (int) $id_agent); +function get_agentmodules ($id_agent, $details = false) { + $id_agent = safe_int ($id_agent, 1); + + if (empty ($id_agent)) { + $filter = ''; + } elseif (is_array ($id_agent)) { + $filter = sprintf (' WHERE id_agente IN (%s)', implode (",",$id_agent)); + } else { + $filter = sprintf (' WHERE id_agente = %d', $id_agent); + } + + if (empty ($details)) { + $details = "nombre"; + } + + $sql = "SELECT id_agente_modulo,".implode (",", (array) $details)." FROM tagente_modulo".$filter." ORDER BY nombre"; + $result = get_db_all_rows_sql ($sql); //cast as array, that way a false will be converted into an array + if (empty ($result)) { + $result = array (); + } + $modules = array (); + + foreach ($result as $row) { + if (is_array ($details)) { + $modules[$row["id_agente_modulo"]] = $row; //Just stack the information in array by ID + } else { + $modules[$row["id_agente_modulo"]] = $row[$details]; + } + } + return $modules; } /** @@ -361,14 +417,36 @@ function return_event_description ($id_event) { } /** - * Get name of an agent. + * DEPRECATED: Use get_agent_name instead * * @param id_agent Agent id. * * @return Name of the given agent. */ function dame_nombre_agente ($id_agent) { - return (string) get_db_value ('nombre', 'tagente', 'id_agente', (int) $id_agent); + return get_agent_name ($id_agent, "none"); +} + +/** + * Get name of an agent. + * + * @param id_agent Agent id. + * @param case Case (upper, lower, none) + * + * @return Name of the given agent. + */ +function get_agent_name ($id_agent, $case = "upper") { + $agent = (string) get_db_value ('nombre', 'tagente', 'id_agente', (int) $id_agent); + switch ($case) { + case "upper": + return mb_strtoupper ($agent,"UTF-8"); + break; + case "lower": + return mb_strtolower ($agent,"UTF-8"); + break; + default: + return ($agent); + } } /** @@ -662,12 +740,10 @@ function get_alert_fires_in_period ($id_agent_module, $period, $date = 0) { */ function get_alerts_in_group ($id_group) { $alerts = array (); - $agents = get_agents_in_group ($id_group); - if (empty ($agents)) - return $alerts; + $agents = get_group_agents ($id_group, false, "none"); - foreach ($agents as $agent) { - $agent_alerts = get_alerts_in_agent ($agent["id_agente"]); + foreach ($agents as $agent_id => $agent_name) { + $agent_alerts = get_alerts_in_agent ($agent_id); $alerts = array_merge ($alerts, $agent_alerts); } @@ -1287,7 +1363,7 @@ function agent_delete_address ($id_agent, $ip_address) { if (get_agent_address ($id_agent) == $ip_address) { $new_ips = get_agent_addresses ($id_agent); // Change main address in agent to first one in the list - $query = sprintf ("UPDATE tagente SET `direccion` = '%s' WHERE id_agente = %d", $new_ips[0], $id_agent); + $query = sprintf ("UPDATE tagente SET `direccion` = '%s' WHERE id_agente = %d", current ($new_ips), $id_agent); process_sql ($query); } } @@ -1324,7 +1400,7 @@ function get_agent_with_ip ($ip_address) { * * @param id_agent Agent id * - * @return Array with the IP address of the given agent. + * @return Array with the IP address of the given agent or an empty array. */ function get_agent_addresses ($id_agent) { $sql = sprintf ("SELECT ip FROM taddress_agent, taddress @@ -1339,7 +1415,7 @@ function get_agent_addresses ($id_agent) { $ret_arr = array (); foreach ($ips as $row) { - $ret_arr[] = $row["ip"]; + $ret_arr[$row["ip"]] = $row["ip"]; } return $ret_arr; @@ -2365,11 +2441,23 @@ function get_server_info ($id_server = -1) { /** * This function will return the number of all agent modules in the database * + * @param integer or array of integers with agent(s). Leave empty to select everything + * * @return integer with the number of agent modules * - * TODO: Filter? Implement when necessary **/ -function get_agentmodule_count () { - return (int) get_db_sql ("SELECT COUNT(*) FROM tagente_modulo"); +function get_agentmodule_count ($id_agent = 0) { + $id_agent = safe_int ($id_agent); //Make sure we're all int's and filter out bad stuff + if (empty ($id_agent) || $id_agent < 1) { + //If the array proved empty or the agent is less than 1 (eg. -1) + $filter = ''; + } elseif (is_array ($id_agent)) { + //If it's an array of agents, flatten the aray + $filter = sprintf (" WHERE id_agente IN (%s)", implode (",",$id_agent)); + } else { + $filter = sprintf (" WHERE id_agente = %d", $id_agent); + } + + return (int) get_db_sql ("SELECT COUNT(*) FROM tagente_modulo".$filter); } ?> diff --git a/pandora_console/include/functions_html.php b/pandora_console/include/functions_html.php index 2fec66ef97..f4755b6a0c 100644 --- a/pandora_console/include/functions_html.php +++ b/pandora_console/include/functions_html.php @@ -113,12 +113,7 @@ function print_select_from_sql ($sql, $name, $selected = '', $script = '', $noth $fields[$row[0]] = $row[1]; } - $output = print_select ($fields, $name, $selected, $script, $nothing, $nothing_value, true, $multiple, $sort); - - if ($return) - return $output; - - echo $output; + return print_select ($fields, $name, $selected, $script, $nothing, $nothing_value, $return, $multiple, $sort); } /** diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php index b75f26bcbd..22a8d06815 100644 --- a/pandora_console/include/functions_reporting.php +++ b/pandora_console/include/functions_reporting.php @@ -221,7 +221,7 @@ function get_group_stats ($id_group) { } //End module check } //End foreach module - $data["total_agents"] = count (get_agents_in_group ($groups)); + $data["total_agents"] = count (get_group_agents ($groups, false, "none")); $data["total_checks"] = $data["data_checks"] + $data["monitor_checks"]; $data["total_ok"] = $data["data_ok"] + $data["monitor_ok"]; $data["total_alerts"] = $data["data_alerts"] + $data["monitor_alerts"]; @@ -496,7 +496,7 @@ function get_monitors_down_reporting_table ($monitors_down) { $data = array (); foreach ($monitors as $monitor) { if (! isset ($data[0])) - $data[0] = dame_nombre_agente ($id_agent); + $data[0] = get_agent_name ($id_agent); else $data[0] = ''; if ($monitor['descripcion'] != '') { @@ -520,12 +520,12 @@ function get_monitors_down_reporting_table ($monitors_down) { * @param $return Flag to return or echo the report (by default). */ function general_group_reporting ($id_group, $return = false) { - $output = ''; - $agents = get_agents_in_group ($id_group); - $output .= ''.__('Agents in group').': '.sizeof ($agents).'
'; + $agents = get_group_agents ($id_group, false, "none"); + $output = ''.__('Agents in group').': '.count ($agents).'
'; - if (!$return) + if ($return === false) echo $output; + return $output; } @@ -619,7 +619,7 @@ function get_agent_monitors_reporting_table ($id_agent, $period = 0, $date = 0) function get_agent_modules_reporting_table ($id_agent, $period = 0, $date = 0) { $table->data = array (); $n_a_string = __('N/A').'(*)'; - $modules = get_modules_in_agent ($id_agent); + $modules = get_agentmodules ($id_agent, array ("nombre", "descripcion")); if ($modules === false) $modules = array(); $data = array (); @@ -693,20 +693,16 @@ function get_agent_detailed_reporting ($id_agent, $period = 0, $date = 0, $retur * @param $return Flag to return or echo the report (by default). */ function get_agents_detailed_reporting ($id_group, $period = 0, $date = 0, $return = false) { - $output = ''; - $agents = get_agents_in_group ($id_group); + $agents = get_group_agents ($id_group, false, "none"); - foreach ($agents as $agent) { - $output .= get_agent_detailed_reporting ($agent['id_agente'], $period, $date, true); - if (!$return) { - echo $output; - $output = ''; - flush (); - } + $output = ''; + foreach ($agents as $agent_id => $agent_name) { + $output .= get_agent_detailed_reporting ($agent_id, $period, $date, true); } - if (!$return) + if ($return === false) echo $output; + return $output; } diff --git a/pandora_console/operation/agentes/estado_alertas.php b/pandora_console/operation/agentes/estado_alertas.php index b50df4fbe5..9d88d36dee 100644 --- a/pandora_console/operation/agentes/estado_alertas.php +++ b/pandora_console/operation/agentes/estado_alertas.php @@ -144,10 +144,10 @@ if (isset($_GET["id_agente"])){ // Agent group selector if ($ag_group > 1) { - $result = get_agents_in_group ($ag_group); + $result = get_group_agents ($ag_group, false, "upper"); } else { //Fields is an array with all the groups the user has access to - $result = get_agents_in_group (array_keys ($fields)); + $result = get_group_agents (array_keys ($fields), false, "upper"); } $color = 1; @@ -174,9 +174,7 @@ if (isset($_GET["id_agente"])){ } //This result is the array with agents - foreach ($result as $row) { - $id_agente = $row["id_agente"]; - $nombre_agente = strtoupper ($row["nombre"]); + foreach ($result as $id_agente => $nombre_agente) { $result_alerts = get_alerts_in_agent ($id_agente); if ($result_alerts === false) diff --git a/pandora_console/operation/events/events.php b/pandora_console/operation/events/events.php index 189b3f1886..87797de87d 100644 --- a/pandora_console/operation/events/events.php +++ b/pandora_console/operation/events/events.php @@ -161,7 +161,7 @@ if (dame_admin ($config["id_user"])) { foreach ($result as $id_row) { $name_for_combo = ""; if ($id_row[0] > 0) - $name_for_combo = substr (strtolower (dame_nombre_agente ($id_row[0])),0,20); + $name_for_combo = substr (get_agent_name ($id_row[0], "lower"),0,20); if ($name_for_combo != "") $agents[$id_row[0]] = $name_for_combo; } diff --git a/pandora_console/reporting/fgraph.php b/pandora_console/reporting/fgraph.php index a95348d3a7..951af7cbac 100644 --- a/pandora_console/reporting/fgraph.php +++ b/pandora_console/reporting/fgraph.php @@ -1207,8 +1207,8 @@ function grafico_db_agentes_modulos($width, $height) { $result = array(); foreach ($result as $row) { - $data[] = count (get_modules_in_agent ($row["id_agente"])); - $legend[] = dame_nombre_agente ($row["id_agente"]); + $data[] = get_agentmodule_count ($row["id_agente"]); + $legend[] = get_agent_name ($row["id_agente"], "lower"); } generic_bar_graph ($width, $height, $data, $legend); @@ -1370,7 +1370,7 @@ function grafico_eventos_grupo ($width = 300, $height = 200, $url = "") { $legend[] = "SYSTEM (".$row["count"].")"; } else { //Other events - $legend[] = substr (dame_nombre_agente ($row["id_agente"]), 0, 15)." (".$row["count"].")"; + $legend[] = substr (get_agent_name ($row["id_agente"], "lower"), 0, 15)." (".$row["count"].")"; } } } @@ -1434,7 +1434,7 @@ function grafico_db_agentes_paquetes ($width = 380, $height = 300) { foreach ($result as $row) { $data[] = $row["count"]; - $legend[] = dame_nombre_agente ($row["id_agente"]); + $legend[] = get_agent_name ($row["id_agente"], "lower"); } generic_bar_graph ($width, $height, $data, $legend); @@ -1447,7 +1447,8 @@ function grafico_db_agentes_purge ($id_agent, $width, $height) { $id_agent = -1; $query = ""; } else { - $query = sprintf (" AND id_agente_modulo = ANY(SELECT id_agente_modulo FROM tagente_modulo WHERE id_agente = '%d' ",$id_agent); + $modules = get_agentmodules ($id_agent); + $query = sprintf (" AND id_agente_modulo IN (%s)", implode (",", array_keys ($modules))); } // All data (now)