From c7cf81f5d040853b99d0321e1b47676bb4742655 Mon Sep 17 00:00:00 2001 From: esanchezm Date: Mon, 4 May 2009 10:10:19 +0000 Subject: [PATCH] =?UTF-8?q?2009-05-04=20=20Esteban=20S=C3=A1nchez=20=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * include/functions_agents.php: Added create_agent(), * include/functions_db.php: Renamed dame_agente_id() to get_agent_id (). Added process_sql_begin(), process_sql_rollback() and process_sql_commit() and use MySQL functions directly because the Pandora cache was avoiding more than one execution. * include/functions_incidents.php, include/functions_messages.php, godmode/agentes/massive_delete_agents.php, godmode/agentes/massive_delete_alerts.php, godmode/agentes/massive_delete_modules.php, godmode/agentes/massive_edit_modules.php, godmode/db/db_purge.php, include/functions_events.php: Use new transaction functions. * include/functions_modules.php: Sanitize some values. Added create_agent_module_from_network_component(), create_agent_module() and get_network_component(). * godmode/agentes/configurar_agente.php: Use get_agent_id() and transaction functions. * reporting/fgraph.php, operation/reporting/reporting_viewer.php: Use get_agent_id(). * godmode/setup/file_manager.php: Removed unnecesary variable. * operation/agentes/ver_agente.php: Replaced exit with return on ajax operation. * include/config_process.php: Updated build number. * include/functions_network_profiles.php: Fixed documentation. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@1672 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_console/ChangeLog | 36 ++++++ .../godmode/agentes/configurar_agente.php | 18 ++- .../godmode/agentes/massive_delete_agents.php | 9 +- .../godmode/agentes/massive_delete_alerts.php | 9 +- .../agentes/massive_delete_modules.php | 9 +- .../godmode/agentes/massive_edit_modules.php | 8 +- pandora_console/godmode/db/db_purge.php | 9 +- .../godmode/setup/file_manager.php | 2 +- pandora_console/include/config_process.php | 2 +- pandora_console/include/functions_agents.php | 75 +++++++++++ pandora_console/include/functions_db.php | 59 ++++++--- pandora_console/include/functions_events.php | 20 ++- .../include/functions_incidents.php | 35 ++---- .../include/functions_messages.php | 9 +- pandora_console/include/functions_modules.php | 118 +++++++++++++++++- .../include/functions_network_profiles.php | 6 +- .../operation/agentes/ver_agente.php | 8 +- .../operation/reporting/reporting_viewer.php | 2 +- pandora_console/reporting/fgraph.php | 6 +- 19 files changed, 326 insertions(+), 114 deletions(-) diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog index dc88d1f3e1..6146adc87f 100644 --- a/pandora_console/ChangeLog +++ b/pandora_console/ChangeLog @@ -1,3 +1,39 @@ +2009-05-04 Esteban Sánchez + + * include/functions_agents.php: Added create_agent(), + + * include/functions_db.php: Renamed dame_agente_id() to get_agent_id (). + Added process_sql_begin(), process_sql_rollback() and process_sql_commit() + and use MySQL functions directly because the Pandora cache was avoiding + more than one execution. + + * include/functions_incidents.php, include/functions_messages.php, + godmode/agentes/massive_delete_agents.php, + godmode/agentes/massive_delete_alerts.php, + godmode/agentes/massive_delete_modules.php, + godmode/agentes/massive_edit_modules.php, + godmode/db/db_purge.php, + include/functions_events.php: Use new transaction functions. + + * include/functions_modules.php: Sanitize some values. Added + create_agent_module_from_network_component(), create_agent_module() + and get_network_component(). + + * godmode/agentes/configurar_agente.php: Use get_agent_id() and transaction + functions. + + * reporting/fgraph.php, operation/reporting/reporting_viewer.php: Use + get_agent_id(). + + * godmode/setup/file_manager.php: Removed unnecesary variable. + + * operation/agentes/ver_agente.php: Replaced exit with return on ajax + operation. + + * include/config_process.php: Updated build number. + + * include/functions_network_profiles.php: Fixed documentation. + 2009-05-02 Sancho Lerena * extensions/dbmanager.php: Fixed problems with quotes. diff --git a/pandora_console/godmode/agentes/configurar_agente.php b/pandora_console/godmode/agentes/configurar_agente.php index 4327dc8963..40a56bc7db 100644 --- a/pandora_console/godmode/agentes/configurar_agente.php +++ b/pandora_console/godmode/agentes/configurar_agente.php @@ -112,7 +112,7 @@ if ($create_agent) { $direccion_agente = (string) get_parameter_post ("direccion"); $grupo = (int) get_parameter_post ("grupo"); $intervalo = (string) get_parameter_post ("intervalo", 300); - $comentarios = (string)get_parameter_post ("comentarios"); + $comentarios = (string) get_parameter_post ("comentarios"); $modo = (int) get_parameter_post ("modo"); $id_parent = (int) get_parameter_post ("id_parent"); $server_name = (string) get_parameter_post ("server_name"); @@ -124,7 +124,7 @@ if ($create_agent) { if ($nombre_agente == "") { $agent_creation_error = __('No agent name specified'); $agent_created_ok = 0; - } elseif (dame_agente_id ($nombre_agente) > 0) { + } elseif (get_agent_id ($nombre_agente)) { $agent_creation_error = __('There is already an agent in the database with this name'); $agent_created_ok = 0; } else { @@ -281,12 +281,12 @@ if (isset($_POST["update_agent"])) { // if modified some agent paramenter $server_name = (string) get_parameter_post ("server_name"); $id_parent = (int) get_parameter_post ("id_parent", 0); $custom_id = (string) get_parameter_post ("custom_id", ""); - + //Verify if there is another agent with the same name but different ID if ($nombre_agente == "") { echo '

'.__('No agent name specified').'

'; //If there is an agent with the same name, but a different ID - } elseif (dame_agente_id ($nombre_agente) > 0 && dame_agente_id ($nombre_agente) != $id_agente) { + } elseif (get_agent_id ($nombre_agente) != $id_agente) { echo '

'.__('There is already an agent in the database with this name').'

'; } else { //If different IP is specified than previous, add the IP @@ -538,8 +538,7 @@ if (isset ($_GET["delete_module"])){ // DELETE agent module ! //Init transaction $error = 0; - process_sql ("SET AUTOCOMMIT=0;"); - process_sql ("START TRANSACTION;"); + process_sql_begin (); // First delete from tagente_modulo -> if not successful, increment // error @@ -556,14 +555,11 @@ if (isset ($_GET["delete_module"])){ // DELETE agent module ! //Check for errors if ($error != 0) { echo '

'.__('There was a problem deleting the module').'

'; - process_sql ("ROLLBACK;"); + process_sql_rollback (); } else { echo '

'.__('Module deleted successfully').'

'; - process_sql ("COMMIT;"); + process_sql_commit (); } - - //End transaction - process_sql ("SET AUTOCOMMIT=1;"); } // ----------------------------------- diff --git a/pandora_console/godmode/agentes/massive_delete_agents.php b/pandora_console/godmode/agentes/massive_delete_agents.php index 73f9d59569..f0db3b7fa3 100644 --- a/pandora_console/godmode/agentes/massive_delete_agents.php +++ b/pandora_console/godmode/agentes/massive_delete_agents.php @@ -43,8 +43,7 @@ function process_manage_delete ($id_agents) { $copy_modules = (bool) get_parameter ('copy_modules'); $copy_alerts = (bool) get_parameter ('copy_alerts'); - process_sql ('SET AUTOCOMMIT = 0'); - process_sql ('START TRANSACTION'); + process_sql_begin (); $error = false; foreach ($id_agents as $id_agent) { @@ -56,13 +55,11 @@ function process_manage_delete ($id_agents) { if (! $success) { echo '

'.__('There was an error deleting the agent, the operation has been cancelled').'

'; echo '

'.__('Could not delete agent').' '.get_agent_name ($id_agent).'

'; - process_sql ('ROLLBACK'); + process_sql_rollback (); } else { echo '

'.__('Successfully deleted').'

'; - process_sql ('COMMIT'); + process_sql_commit (); } - - process_sql ('SET AUTOCOMMIT = 1'); } $id_group = (int) get_parameter ('id_group'); diff --git a/pandora_console/godmode/agentes/massive_delete_alerts.php b/pandora_console/godmode/agentes/massive_delete_alerts.php index 9a85d9a966..549e1b1eb4 100644 --- a/pandora_console/godmode/agentes/massive_delete_alerts.php +++ b/pandora_console/godmode/agentes/massive_delete_alerts.php @@ -37,8 +37,7 @@ function process_manage_delete ($id_alerts) { return false; } - process_sql ('SET AUTOCOMMIT = 0'); - process_sql ('START TRANSACTION'); + process_sql_begin (); foreach ($id_alerts as $id_alert) { $success = delete_alert_agent_module ($id_alert); @@ -49,13 +48,11 @@ function process_manage_delete ($id_alerts) { if (! $success) { echo '

'.__('There was an error deleting the alert, the operation has been cancelled').'

'; echo '

'.__('Could not delete alert').' '.get_agentmodule_name ($id_module).'

'; - process_sql ('ROLLBACK'); + process_sql_rollback (); } else { echo '

'.__('Successfully deleted').'

'; - process_sql ('COMMIT'); + process_sql_commit (); } - - process_sql ('SET AUTOCOMMIT = 1'); } $id_group = (int) get_parameter ('id_group'); diff --git a/pandora_console/godmode/agentes/massive_delete_modules.php b/pandora_console/godmode/agentes/massive_delete_modules.php index e85ee1c326..3ae1a70500 100644 --- a/pandora_console/godmode/agentes/massive_delete_modules.php +++ b/pandora_console/godmode/agentes/massive_delete_modules.php @@ -37,8 +37,7 @@ function process_manage_delete ($id_modules) { return false; } - process_sql ('SET AUTOCOMMIT = 0'); - process_sql ('START TRANSACTION'); + process_sql_begin (); foreach ($id_modules as $id_module) { $success = delete_agent_module ($id_module); @@ -49,13 +48,11 @@ function process_manage_delete ($id_modules) { if (! $success) { echo '

'.__('There was an error deleting the module, the operation has been cancelled').'

'; echo '

'.__('Could not delete module').' '.get_agentmodule_name ($id_module).'

'; - process_sql ('ROLLBACK'); + process_sql_rollback (); } else { echo '

'.__('Successfully deleted').'

'; - process_sql ('COMMIT'); + process_sql_commit (); } - - process_sql ('SET AUTOCOMMIT = 1'); } $id_group = (int) get_parameter ('id_group'); diff --git a/pandora_console/godmode/agentes/massive_edit_modules.php b/pandora_console/godmode/agentes/massive_edit_modules.php index facfb11eb3..ba19b24691 100644 --- a/pandora_console/godmode/agentes/massive_edit_modules.php +++ b/pandora_console/godmode/agentes/massive_edit_modules.php @@ -52,8 +52,8 @@ function process_manage_edit ($module_name) { 'nombre' => $module_name), array ('id_agente_modulo')); - process_sql ('SET AUTOCOMMIT = 0'); - process_sql ('START TRANSACTION'); + process_sql_begin (); + if ($modules === false) return false; foreach ($modules as $module) { @@ -61,8 +61,8 @@ function process_manage_edit ($module_name) { } echo '

'.__('Successfully updated').'

'; - process_sql ('COMMIT'); - process_sql ('SET AUTOCOMMIT = 1'); + + process_sql_commit (); } $module_type = (int) get_parameter ('module_type'); diff --git a/pandora_console/godmode/db/db_purge.php b/pandora_console/godmode/db/db_purge.php index 2b0f70f4df..a8d321de7b 100644 --- a/pandora_console/godmode/db/db_purge.php +++ b/pandora_console/godmode/db/db_purge.php @@ -82,8 +82,8 @@ if (isset($_POST["purgedb"])) { } //Made it in a transaction so it gets done all at once. - process_sql ("SET AUTOCOMMIT=0;"); - process_sql ("START TRANSACTION;"); //We start a transaction for consistency + process_sql_begin (); + $errors = 0; foreach ($result as $row) { echo __('Deleting records for module')." ".get_agentmodule_name ($row["id_agente_modulo"]); @@ -102,12 +102,11 @@ if (isset($_POST["purgedb"])) { } if ($errors > 0) { - process_sql ("ROLLBACK;"); //If we have errors, rollback + process_sql_rollback (); } else { - process_sql ("COMMIT;"); //Otherwise commit + process_sql_commit (); } - process_sql ("SET AUTOCOMMIT=1;"); //Set autocommit back to 1 } else { //All agents echo __('Deleting records for all agents'); diff --git a/pandora_console/godmode/setup/file_manager.php b/pandora_console/godmode/setup/file_manager.php index f61c625d61..db9b120a7b 100644 --- a/pandora_console/godmode/setup/file_manager.php +++ b/pandora_console/godmode/setup/file_manager.php @@ -122,7 +122,7 @@ if (! is_file_manager_writable_dir ($real_directory)) { echo '

'; } else { $table->data[1][0] = __('Upload file'); - $table->data[1][1] = print_input_file ('file', true, $options = false); + $table->data[1][1] = print_input_file ('file', true, false); $table->data[1][2] = print_submit_button (__('Go'), 'go', false, 'class="sub next"', true); $table->data[1][2] .= print_input_hidden ('upload_file', 1, true); diff --git a/pandora_console/include/config_process.php b/pandora_console/include/config_process.php index e15ea15fd9..4bc74e2616 100644 --- a/pandora_console/include/config_process.php +++ b/pandora_console/include/config_process.php @@ -17,7 +17,7 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. //Pandora Version -$build_version = 'PC090414'; +$build_version = 'PC090504'; $pandora_version = 'v3.0-dev'; $config['start_time'] = microtime (true); diff --git a/pandora_console/include/functions_agents.php b/pandora_console/include/functions_agents.php index 2afad3c03f..1c8c291c7f 100644 --- a/pandora_console/include/functions_agents.php +++ b/pandora_console/include/functions_agents.php @@ -17,6 +17,81 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +/** + * Creates an agent + * + * @param string Agent name. + * @param string Group to be included. + * @param int Agent interval + * @param string Agent IP + * + * @return int New agent id if created. False if it could not be created. + */ +function create_agent ($name, $id_group, $interval, $ip_address, $values = false) { + if (empty ($name)) + return false; + if (empty ($name)) + return false; + if (empty ($ip_address)) + return false; + $interval = safe_int ($interval, 300); + if (empty ($interval)) + return false; + if (! is_array ($values)) + $values = array (); + $values['nombre'] = $name; + $values['id_grupo'] = $id_group; + $values['intervalo'] = $interval; + $values['direccion'] = $ip_address; + + process_sql_begin (); + + $id_agent = process_sql_insert ('tagente', $values); + if ($id_agent === false) { + process_sql_rollback (); + return false; + } + + // Create address for this agent in taddress + agent_add_address ($id_agent, $ip_address); + + // Create special module agent_keepalive + $id_agent_module = process_sql_insert ('tagente_modulo', + array ('nombre' => 'agent_keepalive', + 'id_agente' => $id_agent, + 'id_tipo_modulo' => 100, + 'descripcion' => __('Agent keepalive monitor'), + 'id_modulo' => 1, + 'min_warning' => 0, + 'max_warning' => 1)); + + if ($id_agent_module === false) { + process_sql_rollback (); + return false; + } + + $result = process_sql_insert ('tagente_estado', + array ('id_agente_modulo' => $id_agent_module, + 'datos' => '', + 'timestamp' => 0, + 'estado' => 0, + 'id_agente' => $id_agent, + 'last_try' => 0, + 'utimestamp' => 0, + 'current_interval' => 0, + 'running_by' => 0, + 'last_execution_try' => 0)); + + if ($result === false) { + process_sql_rollback (); + return false; + } + + process_sql_commit (); + + return $id_agent; +} + /** * Get all the simple alerts of an agent. * diff --git a/pandora_console/include/functions_db.php b/pandora_console/include/functions_db.php index 4ee6b889ce..cffb975435 100644 --- a/pandora_console/include/functions_db.php +++ b/pandora_console/include/functions_db.php @@ -524,7 +524,7 @@ function dame_grupo_icono ($id_group) { * * @return int Id from the agent of the given name. */ -function dame_agente_id ($agent_name) { +function get_agent_id ($agent_name) { return (int) get_db_value ('id_agente', 'tagente', 'nombre', $agent_name); } @@ -539,14 +539,15 @@ function dame_agente_id ($agent_name) { 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); + case "upper": + return mb_strtoupper ($agent,"UTF-8"); + break; + case "lower": + return mb_strtolower ($agent,"UTF-8"); + break; + case "none": + default: + return ($agent); } } @@ -1843,6 +1844,7 @@ function process_sql ($sql, $rettype = "affected_rows") { mysql_free_result ($result); } } + if (! empty ($retval)) return $retval; //Return false, check with === or !== @@ -2568,11 +2570,7 @@ function delete_agent ($id_agents) { $id_agents = (array) $id_agents; //Start transaction - process_sql ("SET AUTOCOMMIT = 0;"); - $trerr = process_sql ("START TRANSACTION;"); - - if ($trerr === false) - return false; + process_sql_begin (); foreach ($id_agents as $id_agent) { $id_agent = (int) $id_agent; //Cast as integer @@ -2582,8 +2580,7 @@ function delete_agent ($id_agents) { /* Check for deletion permissions */ $id_group = get_agent_group ($id_agent); if (! give_acl ($config['id_user'], $id_group, "AW")) { - process_sql ("ROLLBACK;"); - process_sql ("SET AUTOCOMMIT = 1;"); + process_sql_rollback (); return false; } @@ -2662,12 +2659,10 @@ function delete_agent ($id_agents) { } if ($error) { - process_sql ("ROLLBACK;"); - process_sql ("SET AUTOCOMMIT = 1;"); + process_sql_rollback (); return false; } else { - process_sql ("COMMIT;"); - process_sql ("SET AUTOCOMMIT = 1;"); + process_sql_commit (); return true; } } @@ -3065,6 +3060,30 @@ function process_sql_delete ($table, $where, $where_join = 'AND') { return process_sql ($query); } +/** + * Starts a database transaction. + */ +function process_sql_begin () { + mysql_query ('SET AUTOCOMMIT = 0'); + mysql_query ('START TRANSACTION'); +} + +/** + * Commits a database transaction. + */ +function process_sql_commit () { + mysql_query ('COMMIT'); + mysql_query ('SET AUTOCOMMIT = 0'); +} + +/** + * Rollbacks a database transaction. + */ +function process_sql_rollback () { + mysql_query ('ROLLBACK'); + mysql_query ('SET AUTOCOMMIT = 0'); +} + /** * Get all the users belonging to a group. * diff --git a/pandora_console/include/functions_events.php b/pandora_console/include/functions_events.php index 50d22bb973..ccd60eb953 100644 --- a/pandora_console/include/functions_events.php +++ b/pandora_console/include/functions_events.php @@ -87,8 +87,7 @@ function delete_event ($id_event, $similar = true) { } } - process_sql ("SET AUTOCOMMIT = 0;"); - process_sql ("START TRANSACTION;"); + process_sql_begin (); $errors = 0; foreach ($id_event as $event) { @@ -104,18 +103,17 @@ function delete_event ($id_event, $similar = true) { } $errors++; + break; } if ($errors > 1) { - process_sql ("ROLLBACK;"); - process_sql ("SET AUTOCOMMIT = 1;"); + process_sql_rollback (); return false; } else { foreach ($id_event as $event) { audit_db ($config["id_user"], $config["remote_addr"], "Event deleted", "Deleted event #".$event); } - process_sql ("COMMIT;"); - process_sql ("SET AUTOCOMMIT = 1;"); + process_sql_commit (); return true; } } @@ -141,8 +139,7 @@ function validate_event ($id_event, $similars = true) { } } - process_sql ("SET AUTOCOMMIT = 0;"); - process_sql ("START TRANSACTION;"); + process_sql_begin (); $errors = 0; foreach ($id_event as $event) { @@ -158,18 +155,17 @@ function validate_event ($id_event, $similars = true) { } $errors++; + break; } if ($errors > 1) { - process_sql ("ROLLBACK;"); - process_sql ("SET AUTOCOMMIT = 1;"); + process_sql_rollback (); return false; } else { foreach ($id_event as $event) { audit_db ($config["id_user"], $config["remote_addr"], "Event validated", "Validated event #".$event); } - process_sql ("COMMIT;"); - process_sql ("SET AUTOCOMMIT = 1;"); + process_sql_commit (); return true; } } diff --git a/pandora_console/include/functions_incidents.php b/pandora_console/include/functions_incidents.php index c42624e924..333761a7b0 100644 --- a/pandora_console/include/functions_incidents.php +++ b/pandora_console/include/functions_incidents.php @@ -232,8 +232,7 @@ function delete_incidents ($id_incident) { $errors = 0; //Start transaction - process_sql ("SET AUTOCOMMIT = 0;"); - process_sql ("START TRANSACTION;"); + process_sql_begin (); foreach ($ids as $id_inc) { //Delete incident @@ -259,14 +258,12 @@ function delete_incidents ($id_incident) { if ($errors > 0) { //This will also rollback the audit log - process_sql ("ROLLBACK;"); - process_sql ("SET AUTOCOMMIT = 1;"); + process_sql_rollback (); return false; - } else { - process_sql ("COMMIT;"); - process_sql ("SET AUTOCOMMIT = 1;"); - return true; } + process_sql_commit (); + + return true; } /** @@ -283,26 +280,23 @@ function delete_incidents_note ($id_note, $transact = true) { //Start transaction if ($transact == true){ - process_sql ("SET AUTOCOMMIT = 0;"); - process_sql ("START TRANSACTION;"); + process_sql_begin (); + process_sql_commit (); } //Delete notes foreach ($id_note as $id) { - $sql = sprintf ("DELETE FROM tnota WHERE id_nota = %d", $id); - $ret = process_sql ($sql); + $ret = process_sql_delete ('tnota', array ('id_nota' => $id)); if ($ret === false) { $errors++; } } if ($transact == true && $errors > 0) { - process_sql ("ROLLBACK;"); - process_sql ("SET AUTOCOMMIT = 1;"); + process_sql_rollback (); return false; } elseif ($transact == true) { - process_sql ("COMMIT;"); - process_sql ("SET AUTOCOMMIT = 1;"); + process_sql_commit (); return true; } elseif ($errors > 0) { return false; @@ -327,8 +321,7 @@ function delete_incidents_attach ($id_attach, $transact = true) { //Start transaction if ($transact == true) { - process_sql ("SET AUTOCOMMIT = 0;"); - process_sql ("START TRANSACTION;"); + process_sql_begin (); } //Delete attachment @@ -343,12 +336,10 @@ function delete_incidents_attach ($id_attach, $transact = true) { } if ($transact == true && $errors > 0) { - process_sql ("ROLLBACK;"); - process_sql ("SET AUTOCOMMIT = 1;"); + process_sql_rollback (); return false; } elseif ($transact == true) { - process_sql ("COMMIT;"); - process_sql ("SET AUTOCOMMIT = 1;"); + process_sql_commit (); return true; } elseif ($errors > 0) { return false; diff --git a/pandora_console/include/functions_messages.php b/pandora_console/include/functions_messages.php index 70e8675f65..67d513d422 100644 --- a/pandora_console/include/functions_messages.php +++ b/pandora_console/include/functions_messages.php @@ -76,22 +76,19 @@ function create_message_group ($usuario_origen, $dest_group, $subject, $mensaje) } //Start transaction so that if it fails somewhere along the way, we roll back - process_sql ("SET AUTOCOMMIT = 0;"); - process_sql ("START TRANSACTION;"); + process_sql_begin (); foreach ($group_users as $user) { $return = create_message ($usuario_origen, get_user_id ($user), $subject, $mensaje); if ($return === false) { //Error sending message, rollback and return false - process_sql ("ROLLBACK;"); - process_sql ("SET AUTOCOMMIT = 1;"); + process_sql_rollback (); return false; } } //We got here, so we can commit - if this function gets extended, make sure to do SQL above these lines - process_sql ("COMMIT;"); - process_sql ("SET AUTOCOMMIT = 1;"); + process_sql_commit (); return true; } diff --git a/pandora_console/include/functions_modules.php b/pandora_console/include/functions_modules.php index b7975e2c94..154e2b4919 100644 --- a/pandora_console/include/functions_modules.php +++ b/pandora_console/include/functions_modules.php @@ -95,7 +95,7 @@ function get_network_components ($id_module, $filter = false, $fields = false) { if (!is_array ($filter)) $filter = array (); - $filter['id_modulo'] = $id_module; + $filter['id_modulo'] = (int) $id_module; $components = get_db_all_rows_filter ('tnetwork_component', $filter, $fields); if ($components === false) @@ -156,6 +156,74 @@ function get_network_component_groups ($id_module_components = 0) { return $retval; } +/** + * Get a network component. + * + * @param int Component id to be fetched. + * @param array Extra filter. + * @param array Fields to be fetched. + * + * @return array A network component matching id and filter. + */ +function get_network_component ($id_network_component, $filter = false, $fields = false) { + if (empty ($id_network_component)) + return false; + if (! is_array ($filter)) + $filter = array (); + $filter['id_nc'] = (int) $id_network_component; + + return get_db_row_filter ('tnetwork_component', $filter, $fields); +} + +/** + * Creates a module in an agent from a network component. + * + * @param int Component id to be created. + * @param int Agent id to create module in. + * + * @return array New agent module id if created. False if could not be created + */ +function create_agent_module_from_network_component ($id_network_component, $id_agent) { + if (! user_access_to_agent ($id_agent, 'AW')) + return false; + $component = get_network_component ($id_network_component, + false, + array ('name', + 'description AS descripcion', + 'type AS id_tipo_modulo', + 'max', + 'min', + 'module_interval', + 'tcp_port', + 'tcp_send', + 'tcp_rcv', + 'snmp_community', + 'snmp_oid', + 'id_module_group', + 'id_modulo', + 'plugin_user', + 'plugin_pass', + 'plugin_parameter', + 'max_timeout', + 'history_data', + 'min_warning', + 'max_warning', + 'min_critical', + 'max_critical', + 'min_ff_event')); + if (empty ($component)) + return false; + $values = $component; + $len = count ($values) / 2; + for ($i = 0; $i < $len; $i++) + unset ($values[$i]); + $name = $values['name']; + unset ($values['name']); + $values['ip_target'] = get_agent_address ($id_agent); + + return create_agent_module ($id_agent, $name, $values); +} + /** * Get the name of a network components group. * @@ -206,7 +274,51 @@ function update_agent_module ($id, $values) { if (! is_array ($values)) return false; - return (bool) process_sql_update ('tagente_modulo', $values, - array ('id_agente_modulo' => $id)); + return (@process_sql_update ('tagente_modulo', $values, + array ('id_agente_modulo' => (int) $id)) !== false); +} + +/** + * Creates a module in an agent. + * + * @param int Agent id. + * @param int Module name id. + * @param array Extra values for the module. + * + * @return New module id if the module was created. False if not. + */ +function create_agent_module ($id_agent, $name, $values = false) { + if (empty ($id_agent) || ! user_access_to_agent ($id_agent, 'AW')) + return false; + if (empty ($name)) + return false; + if (! is_array ($values)) + $values = array (); + $values['nombre'] = $name; + $values['id_agente'] = (int) $id_agent; + + $id_agent_module = process_sql_insert ('tagente_modulo', $values); + + if ($id_agent_module === false) + return false; + + $result = process_sql_insert ('tagente_estado', + array ('id_agente_modulo' => $id_agent_module, + 'datos' => 0, + 'timestamp' => '0000-00-00 00:00:00', + 'estado' => 0, + 'id_agente' => (int) $id_agent, + 'utimestamp' => 0, + 'status_changes' => 0, + 'last_status' => 0 + )); + + if ($result === false) { + process_sql_delete ('tagente_modulo', + array ('id_agente_modulo' => $id_agent_module)); + return false; + } + + return $id_agent_module; } ?> diff --git a/pandora_console/include/functions_network_profiles.php b/pandora_console/include/functions_network_profiles.php index acbe48343f..b7deaf9082 100644 --- a/pandora_console/include/functions_network_profiles.php +++ b/pandora_console/include/functions_network_profiles.php @@ -18,13 +18,13 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. /** - * Get a custom user report. + * Get a network profile. * - * @param int Report id to get. + * @param int Profile id to get. * @param array Extra filter. * @param array Fields to get. * - * @return Report with the given id. False if not available or readable. + * @return Profile with the given id. False if not available or readable. */ function get_network_profile ($id_network_profile, $filter = false, $fields = false) { global $config; diff --git a/pandora_console/operation/agentes/ver_agente.php b/pandora_console/operation/agentes/ver_agente.php index be11e33abb..88584efdb4 100644 --- a/pandora_console/operation/agentes/ver_agente.php +++ b/pandora_console/operation/agentes/ver_agente.php @@ -34,7 +34,7 @@ if (is_ajax ()) { $agent = get_db_row ('tagente', 'id_agente', $id_agent); echo json_encode ($agent); - exit (); + return; } if ($get_agent_modules_json) { @@ -52,7 +52,7 @@ if (is_ajax ()) { ($filter != '' ? $filter : false), $indexed); echo json_encode ($agent_modules); - exit (); + return; } if ($get_agent_status_tooltip) { @@ -139,10 +139,10 @@ if (is_ajax ()) { echo ''; } - exit (); + return; } - exit (); + return; } $id_agente = (int) get_parameter ("id_agente", 0); diff --git a/pandora_console/operation/reporting/reporting_viewer.php b/pandora_console/operation/reporting/reporting_viewer.php index 46f66a975f..e7048f7ab3 100644 --- a/pandora_console/operation/reporting/reporting_viewer.php +++ b/pandora_console/operation/reporting/reporting_viewer.php @@ -262,7 +262,7 @@ foreach ($contents as $content) { break; case 4: case 'event_report': - $id_agent = dame_agente_id ($agent_name); + $id_agent = get_agent_id ($agent_name); $data = array (); $data[0] = "

".__('Event report')."

"; $data[1] = "

".human_time_description ($content['period'])."

"; diff --git a/pandora_console/reporting/fgraph.php b/pandora_console/reporting/fgraph.php index b098190ba6..94759723f0 100644 --- a/pandora_console/reporting/fgraph.php +++ b/pandora_console/reporting/fgraph.php @@ -122,7 +122,7 @@ function graphic_combined_module ($module_list, $weight_list, $period, $width, $ for ($i = 0; $i < $module_number; $i++) { $id_agente_modulo = $module_list[$i]; $nombre_agente = get_agentmodule_agent_name ($id_agente_modulo); - $id_agente = dame_agente_id ($nombre_agente); + $id_agente = get_agent_id ($nombre_agente); $nombre_modulo = get_agentmodule_name ($id_agente_modulo); $module_list_name[$i] = substr ($nombre_agente, 0, 9)." / ".substr ($nombre_modulo, 0, 20); for ($j = 0; $j <= $resolution; $j++) { @@ -282,7 +282,7 @@ function grafico_modulo_sparse ($id_agente_modulo, $period, $show_event, $interval = (int) ($period / $resolution); // Each interval is $interval seconds length $nombre_agente = get_agentmodule_agent_name ($id_agente_modulo); - $id_agente = dame_agente_id ($nombre_agente); + $id_agente = get_agent_id ($nombre_agente); $nombre_modulo = get_agentmodule_name ($id_agente_modulo); // Init tables @@ -901,7 +901,7 @@ function grafico_modulo_boolean ($id_agente_modulo, $period, $show_event, $datelimit = $date - $period; // limit date $interval = (int) ($period / $resolution); // Each interval is $interval seconds length $nombre_agente = get_agentmodule_agent_name ($id_agente_modulo); - $id_agente = dame_agente_id ($nombre_agente); + $id_agente = get_agent_id ($nombre_agente); $nombre_modulo = get_agentmodule_name ($id_agente_modulo); if ($show_event == 1)