From faa29e0c03e427ca18489a3bd5a6a5b300b4ae2c Mon Sep 17 00:00:00 2001 From: mdtrooper Date: Thu, 8 Aug 2013 09:00:34 +0000 Subject: [PATCH] 2013-08-08 Miguel de Dios * include/functions_agents.php, include/functions_alerts.php, operation/agentes/alerts_status.php: improved the function "get_group_alerts" to use in the places that there were a foreach loop with all agents for any group. Fixes: #2060 git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@8641 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_console/ChangeLog | 9 ++ pandora_console/include/functions_agents.php | 2 +- pandora_console/include/functions_alerts.php | 149 ++++++++++++++++-- .../operation/agentes/alerts_status.php | 53 +++++-- 4 files changed, 189 insertions(+), 24 deletions(-) diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog index ab87a826fd..afe2b49a25 100644 --- a/pandora_console/ChangeLog +++ b/pandora_console/ChangeLog @@ -1,3 +1,12 @@ +2013-08-08 Miguel de Dios + + * include/functions_agents.php, include/functions_alerts.php, + operation/agentes/alerts_status.php: improved the function + "get_group_alerts" to use in the places that there were a foreach + loop with all agents for any group. + + Fixes: #2060 + 2013-08-07 Miguel de Dios * godmode/agentes/modificar_agente.php, diff --git a/pandora_console/include/functions_agents.php b/pandora_console/include/functions_agents.php index 20e47526a3..3f4837107e 100644 --- a/pandora_console/include/functions_agents.php +++ b/pandora_console/include/functions_agents.php @@ -218,7 +218,7 @@ function agents_get_alerts_simple ($id_agent = false, $filter = '', $options = f INNER JOIN talert_templates t4 ON talert_template_modules.id_alert_template = t4.id WHERE id_agent_module in (%s) %s %s %s", - $selectText, $subQuery, $where, $filter, $orderbyText); + $selectText, $subQuery, $where, $filter, $orderbyText); $alerts = db_get_all_rows_sql ($sql); if ($alerts === false) diff --git a/pandora_console/include/functions_alerts.php b/pandora_console/include/functions_alerts.php index 549cba8a32..11ae8c9e12 100644 --- a/pandora_console/include/functions_alerts.php +++ b/pandora_console/include/functions_alerts.php @@ -1522,24 +1522,147 @@ function get_alert_fires_in_period ($id_alert_module, $period, $date = 0) { * * @return array An array with alerts dictionaries defined in a group. */ -function get_group_alerts ($id_group) { +function get_group_alerts($id_group, $filter = '', $options = false, + $where = '', $allModules = false, $orderby = false, + $idGroup = false, $count = false) { + global $config; - require_once ($config["homedir"] . '/include/functions_agents.php'); - $alerts = array ('simple' => array()); - $agents = agents_get_group_agents ($id_group, false, "none"); - - foreach ($agents as $agent_id => $agent_name) { - $agent_alerts = agents_get_alerts ($agent_id); - - - if (!empty($agent_alerts['simple'])) - $alerts['simple'] = array_merge ($alerts['simple'], - $agent_alerts['simple']); + if (is_array($filter)) { + $disabled = $filter['disabled']; + if (isset($filter['standby'])) { + $filter = ' AND talert_template_modules.standby = "'.$filter['standby'].'"'; + } + else { + $filter = ''; + } + } + else { + $filter = ''; + $disabled = $filter; } - return $alerts; + switch ($disabled) { + case "notfired": + $filter .= ' AND times_fired = 0 AND talert_template_modules.disabled = 0'; + break; + case "fired": + $filter .= ' AND times_fired > 0 AND talert_template_modules.disabled = 0'; + break; + case "disabled": + $filter .= ' AND talert_template_modules.disabled = 1'; + break; + case "all_enabled": + $filter .= ' AND talert_template_modules.disabled = 0'; + break; + default: + $filter .= ''; + break; + } + + if (is_array ($options)) { + $filter .= db_format_array_where_clause_sql ($options); + } + + + if ($id_group !== false) { + $where_tags = tags_get_acl_tags($config['id_user'], + $idGroup, 'AR', 'module_condition', 'AND', 'tagente_modulo'); + + if ($id_group != 0) { + if (is_array($id_group)) { + if (in_array(0, $id_group)) { + $id_group = 0; + } + } + + if (is_array($id_group)) { + if (empty($id_group)) { + $subQuery = 'SELECT id_agente_modulo + FROM tagente_modulo + WHERE 1 = 0'; + } + else { + $subQuery = 'SELECT id_agente_modulo + FROM tagente_modulo + WHERE delete_pending = 0 + AND id_agente IN (SELECT id_agente + FROM tagente + WHERE + id_grupo IN (' . implode(',', $id_group) . '))'; + } + } + else { + $subQuery = 'SELECT id_agente_modulo + FROM tagente_modulo + WHERE delete_pending = 0 + AND id_agente IN (SELECT id_agente + FROM tagente WHERE id_grupo = ' . $idGroup . ')'; + } + } + else { + //ALL GROUP + $subQuery = 'SELECT id_agente_modulo + FROM tagente_modulo WHERE delete_pending = 0'; + } + + // If there are any errors add imposible condition + if (in_array($where_tags, array(ERR_WRONG_PARAMETERS, ERR_ACL))) { + $subQuery .= ' AND 1 = 0'; + } + else { + $subQuery .= $where_tags; + } + } + else { + if ($allModules) + $disabled = ''; + else + $disabled = 'WHERE disabled = 0'; + + $subQuery = 'SELECT id_agente_modulo + FROM tagente_modulo ' . $disabled; + } + + $orderbyText = ''; + if ($orderby !== false) { + if (is_array($orderby)) { + $orderbyText = sprintf("ORDER BY %s", $orderby['field'], $orderby['order']); + } + else { + $orderbyText = sprintf("ORDER BY %s", $orderby); + } + } + + $selectText = 'talert_template_modules.*, t2.nombre AS agent_module_name, t3.nombre AS agent_name, t4.name AS template_name'; + if ($count !== false) { + $selectText = 'COUNT(talert_template_modules.id) AS count'; + } + + + + $sql = sprintf ("SELECT %s + FROM talert_template_modules + INNER JOIN tagente_modulo t2 + ON talert_template_modules.id_agent_module = t2.id_agente_modulo + INNER JOIN tagente t3 + ON t2.id_agente = t3.id_agente + INNER JOIN talert_templates t4 + ON talert_template_modules.id_alert_template = t4.id + WHERE id_agent_module in (%s) %s %s %s", + $selectText, $subQuery, $where, $filter, $orderbyText); + $alerts = db_get_all_rows_sql ($sql); + + if ($alerts === false) + return array (); + + if ($count !== false) { + return $alerts[0]['count']; + } + else { + return $alerts; + } } /** diff --git a/pandora_console/operation/agentes/alerts_status.php b/pandora_console/operation/agentes/alerts_status.php index 635686b825..060f275717 100644 --- a/pandora_console/operation/agentes/alerts_status.php +++ b/pandora_console/operation/agentes/alerts_status.php @@ -76,7 +76,7 @@ if ($idAgent != 0) { $is_extra = enterprise_hook('policies_is_agent_extra_policy', array($id_agente)); - if($is_extra === ENTERPRISE_NOT_HOOK) { + if ($is_extra === ENTERPRISE_NOT_HOOK) { $is_extra = false; } @@ -86,7 +86,6 @@ if ($idAgent != 0) { exit; } - $agents = array($idAgent); $idGroup = false; $print_agent = false; @@ -276,18 +275,52 @@ else { if (defined('METACONSOLE')) { require_once ($config['homedir'] . '/enterprise/meta/include/functions_alerts_meta.php'); - - $alerts['alerts_simple'] = alerts_meta_get_alerts ($agents, - $filter_alert, $options_simple, $whereAlertSimple, false, false, $idGroup); + if ($idAgent != 0) { + $alerts['alerts_simple'] = alerts_meta_get_alerts ($agents, + $filter_alert, $options_simple, $whereAlertSimple, false, false, + $idGroup); + + + $countAlertsSimple = alerts_meta_get_alerts ($agents, $filter_alert, + false, $whereAlertSimple, false, false, $idGroup, true); + } + else { + $id_groups = array_keys( + users_get_groups($config["id_user"], 'AR', false)); + + $alerts['alerts_simple'] = alerts_meta_get_group_alerts($id_groups, + $filter_alert, $options_simple, $whereAlertSimple, false, + false, $idGroup); + + $countAlertsSimple = alerts_meta_get_group_alerts($id_groups, + $filter_alert, false, $whereAlertSimple, false, false, + $idGroup, true); + } } else { - $alerts['alerts_simple'] = agents_get_alerts_simple ($agents, - $filter_alert, $options_simple, $whereAlertSimple, false, false, $idGroup); + if ($idAgent != 0) { + $alerts['alerts_simple'] = agents_get_alerts_simple ($idAgent, + $filter_alert, $options_simple, $whereAlertSimple, false, false, + $idGroup); + + $countAlertsSimple = agents_get_alerts_simple ($idAgent, + $filter_alert, false, $whereAlertSimple, false, false, + $idGroup, true); + } + else { + $id_groups = array_keys( + users_get_groups($config["id_user"], 'AR', false)); + + $alerts['alerts_simple'] = get_group_alerts($id_groups, + $filter_alert, $options_simple, $whereAlertSimple, false, + false, $idGroup); + + $countAlertsSimple = get_group_alerts($id_groups, + $filter_alert, false, $whereAlertSimple, false, false, + $idGroup, true); + } } -$countAlertsSimple = agents_get_alerts_simple ($agents, $filter_alert, - false, $whereAlertSimple, false, false, $idGroup, true); - if ($tab != null) { $url = $url.'&tab='.$tab; }