diff --git a/pandora_console/include/functions_alerts.php b/pandora_console/include/functions_alerts.php index f4bd91645e..2566e265be 100644 --- a/pandora_console/include/functions_alerts.php +++ b/pandora_console/include/functions_alerts.php @@ -2830,3 +2830,83 @@ function alerts_ui_update_or_create_actions($update=true) $update ? __('Could not be updated') : __('Could not be created') ); } + + +/** + * Retrieve all agent_modules with configured alerts filtered by group. + * + * @param integer|null $id_grupo Filter by group. + * @param boolean $recursion Filter by group recursive. + * + * @return array With agent module ids. + */ +function alerts_get_agent_modules( + ?int $id_grupo, + bool $recursion=false +) : array { + if ($id_grupo === null) { + $agent_modules = db_get_all_rows_sql( + 'SELECT distinct(atm.id_agent_module) + FROM talert_template_modules atm + INNER JOIN tagente_modulo am + ON am.id_agente_modulo = atm.id_agent_module + WHERE atm.disabled = 0' + ); + } else if ($recursion !== true) { + $sql = sprintf( + 'SELECT distinct(atm.id_agent_module) + FROM talert_template_modules atm + INNER JOIN tagente_modulo am + ON am.id_agente_modulo = atm.id_agent_module + INNER JOIN tagente ta + ON am.id_agente = ta.id_agente + LEFT JOIN tagent_secondary_group tasg + ON tasg.id_agent = ta.id_agente + WHERE atm.disabled = 0 + AND (tasg.id_group = %d + OR ta.id_grupo = %d) + ', + $id_grupo, + $id_grupo + ); + $agent_modules = db_get_all_rows_sql($sql); + } else { + $groups = groups_get_children($id_grupo, true); + if (empty($groups) === false) { + $groups = array_reduce( + $groups, + function ($carry, $item) { + $carry[] = $item['id_grupo']; + return $carry; + }, + [$id_grupo] + ); + + $sql = sprintf( + 'SELECT distinct(atm.id_agent_module) + FROM talert_template_modules atm + INNER JOIN tagente_modulo am + ON am.id_agente_modulo = atm.id_agent_module + INNER JOIN tagente ta + ON am.id_agente = ta.id_agente + LEFT JOIN tagent_secondary_group tasg + ON tasg.id_agent = ta.id_agente + WHERE atm.disabled = 0 + AND (tasg.id_group IN (%s) + OR ta.id_grupo IN (%s)) + ', + implode(',', $groups), + implode(',', $groups) + ); + } + + $agent_modules = db_get_all_rows_sql($sql); + } + + if ($agent_modules === false) { + return []; + } + + return $agent_modules; + +} diff --git a/pandora_console/include/functions_events.php b/pandora_console/include/functions_events.php index 4247a1868a..7a337fbdab 100644 --- a/pandora_console/include/functions_events.php +++ b/pandora_console/include/functions_events.php @@ -707,7 +707,9 @@ function events_get_all( } $count = false; - if (!is_array($fields) && $fields == 'count') { + if (is_array($fields) === false && $fields === 'count' + || (is_array($fields) === true && $fields[0] === 'count') + ) { $fields = ['te.*']; $count = true; } else if (!is_array($fields)) { @@ -877,9 +879,10 @@ function events_get_all( $groups ); - if (!$propagate) { + if (!$propagate && isset($groups)) { $sql_filters[] = sprintf( ' AND (te.id_grupo = %d OR tasg.id_group = %d)', + $groups, $groups ); } else { @@ -1063,6 +1066,7 @@ function events_get_all( } } + $_tmp = ''; foreach ($tags as $id_tag) { if (!isset($tags_names[$id_tag])) { $tags_names[$id_tag] = tags_get_name($id_tag); @@ -1304,6 +1308,7 @@ function events_get_all( $tgrupo_join = 'LEFT'; $tgrupo_join_filters = []; + if (isset($groups) && (is_array($groups) || $groups > 0) @@ -1311,17 +1316,21 @@ function events_get_all( $tgrupo_join = 'INNER'; if (is_array($groups)) { $tgrupo_join_filters[] = sprintf( - ' AND (tg.id_grupo IN (%s) OR tasg.id_group IN (%s))', + ' (te.id_grupo = tg.id_grupo AND tg.id_grupo IN (%s)) + OR (tg.id_grupo = tasg.id_group AND tasg.id_group IN (%s))', join(', ', $groups), join(', ', $groups) ); } else { $tgrupo_join_filters[] = sprintf( - ' AND (tg.id_grupo = %s OR tasg.id_group = %s)', + ' (te.id_grupo = tg.id_grupo AND tg.id_grupo = %s) + OR (tg.id_grupo = tasg.id_group AND tasg.id_group = %s)', $groups, $groups ); } + } else { + $tgrupo_join_filters[] = ' te.id_grupo = tg.id_grupo'; } $server_join = ''; @@ -1376,8 +1385,7 @@ function events_get_all( %s %s %s JOIN tgrupo tg - ON te.id_grupo = tg.id_grupo - %s + ON %s %s WHERE 1=1 %s @@ -1567,7 +1575,9 @@ function events_get_events_no_grouped( $table = events_get_events_table($meta, $history); - $sql = 'SELECT * FROM '.$table.' te WHERE 1=1 '.$sql_post; + $sql = 'SELECT * FROM '.$table.' te '; + $sql .= events_get_secondary_groups_left_join($table); + $sql .= $sql_post; $events = db_get_all_rows_sql($sql, $history_db); @@ -1942,7 +1952,7 @@ function events_change_status( * * @param mixed $id_event Event ID or array of events. * @param string $new_owner Id_user of the new owner. If is false, the current - * owner will be setted. + * owner will be set, if empty, will be cleaned. * @param boolean $force Flag to force the change or not (not force is * change only when it hasn't owner). * @param boolean $meta Metaconsole mode flag. @@ -1982,11 +1992,10 @@ function events_change_owner( return false; } - // If no new_owner is provided, the current user will be the owner - // * #2250: Comment this lines because if possible selected None owner. - // if (empty($new_owner)) { - // $new_owner = $config['id_user']; - // } + if ($new_owner === false) { + $new_owner = $config['id_user']; + } + // Only generate comment when is forced (sometimes is owner changes when // comment). if ($force) { @@ -1995,7 +2004,9 @@ function events_change_owner( '', 'Change owner to '.$new_owner, $meta, - $history + $history, + true, + false ); } @@ -2051,13 +2062,14 @@ function events_get_events_table($meta, $history) /** * Comment events in a transresponse * - * @param mixed $id_event Event ID or array of events. - * @param string $comment Comment to be registered. - * @param string $action Action performed with comment. By default just add - * a comment. - * @param boolean $meta Flag of metaconsole mode. - * @param boolean $history Flag of history mode. - * @param boolean $similars Similars. + * @param mixed $id_event Event ID or array of events. + * @param string $comment Comment to be registered. + * @param string $action Action performed with comment. By default just add + * a comment. + * @param boolean $meta Flag of metaconsole mode. + * @param boolean $history Flag of history mode. + * @param boolean $similars Similars. + * @param boolean $update_owner Update owner. * * @return boolean Whether or not it was successful */ @@ -2067,7 +2079,8 @@ function events_comment( $action='Added comment', $meta=false, $history=false, - $similars=true + $similars=true, + $update_owner=true ) { global $config; @@ -2095,8 +2108,10 @@ function events_comment( return false; } - // If the event hasn't owner, assign the user as owner. - events_change_owner($id_event); + if ($update_owner) { + // If the event hasn't owner, assign the user as owner. + events_change_owner($id_event); + } // Get the current event comments. $first_event = $id_event; @@ -2975,9 +2990,13 @@ function events_get_agent( if ($events_group) { $sql_where .= sprintf( - ' AND id_grupo IN (%s) AND utimestamp > %d - AND utimestamp <= %d ', - implode(',', $id_group), + ' INNER JOIN tgrupo tg + ON (te.id_grupo = tg.id_grupo AND tg.id_grupo = %s) + OR (tg.id_grupo = tasg.id_group AND tasg.id_group = %s) + WHERE utimestamp > %d + AND utimestamp <= %d ', + join(',', $id_group), + join(',', $id_group), $datelimit, $date ); @@ -5013,20 +5032,26 @@ function events_get_count_events_by_agent( $tevento = 'tevento'; $sql = sprintf( - 'SELECT id_agente, - (SELECT t2.alias - FROM %s t2 - WHERE t2.id_agente = t3.id_agente) AS agent_name, - COUNT(*) AS count - FROM %s t3 + 'SELECT + ta.id_agente, + ta.alias as agent_name, + count(*) as count + FROM %s te + %s + INNER JOIN %s ta + ON te.id_agente = ta.id_agente + INNER JOIN tgrupo tg + ON (te.id_grupo = tg.id_grupo AND tg.id_grupo IN (%s)) + OR (tg.id_grupo = tasg.id_group AND tasg.id_group IN (%s)) WHERE utimestamp > %d AND utimestamp <= %d - AND id_grupo IN (%s) - GROUP BY id_agente', - $tagente, + GROUP BY ta.id_agente', $tevento, + events_get_secondary_groups_left_join($tevento), + $tagente, + implode(',', $id_group), + implode(',', $id_group), $datelimit, $date, - implode(',', $id_group), $sql_where ); @@ -5077,8 +5102,10 @@ function events_get_count_events_validated_by_user( $dbmeta=false ) { global $config; + $tevento = 'tevento'; + // Group. - $sql_filter = ' AND 1=1 '; + $tgroup_join = ''; if (isset($filter['id_group'])) { $id_group = groups_safe_acl($config['id_user'], $filter['id_group'], 'AR'); @@ -5087,7 +5114,15 @@ function events_get_count_events_validated_by_user( return false; } - $sql_filter .= sprintf(' AND id_grupo IN (%s) ', implode(',', $id_group)); + $tgroup_join = sprintf( + '%s + INNER JOIN tgrupo tg + ON (te.id_grupo = tg.id_grupo AND tg.id_grupo IN (%s)) + OR (tg.id_grupo = tasg.id_group AND tasg.id_group IN (%s))', + events_get_secondary_groups_left_join($tevento), + implode(',', $id_group), + implode(',', $id_group) + ); } if (!empty($filter['id_agent'])) { @@ -5186,24 +5221,29 @@ function events_get_count_events_validated_by_user( $sql_where .= ' AND (evento LIKE "%'.io_safe_input($filter_event_filter_search).'%" OR id_evento LIKE "%'.io_safe_input($filter_event_filter_search).'%")'; } - $tevento = 'tevento'; - $sql = sprintf( - 'SELECT id_usuario, - (SELECT t2.fullname - FROM tusuario t2 - WHERE t2.id_user = t3.id_usuario) AS user_name, - COUNT(*) AS count - FROM %s t3 - WHERE utimestamp > %d AND utimestamp <= %d - %s %s - GROUP BY id_usuario', + 'SELECT + te.id_usuario, + tu.fullname as user_name, + count(*) as count + FROM %s te + %s + LEFT JOIN tusuario tu + ON te.owner_user = tu.id_user + WHERE + te.utimestamp > %d AND te.utimestamp <= %d + AND te.estado = %d + %s + GROUP BY te.id_usuario', $tevento, + $tgroup_join, $datelimit, $date, + EVENT_VALIDATE, $sql_filter, $sql_where ); + $rows = db_get_all_rows_sql($sql); if ($rows == false) { @@ -5214,7 +5254,7 @@ function events_get_count_events_validated_by_user( foreach ($rows as $row) { $user_name = $row['user_name']; if (empty($row['user_name'])) { - $user_name = __('Unknown'); + $user_name = __('Validated but not assigned'); } $return[$user_name] = $row['count']; @@ -5252,7 +5292,10 @@ function events_get_count_events_by_criticity( ) { global $config; - $sql_filter = ' AND 1=1 '; + $tevento = 'tevento'; + + $sql_filter = ''; + $tgroup_join = ''; if (isset($filter['id_group'])) { $id_group = groups_safe_acl($config['id_user'], $filter['id_group'], 'AR'); @@ -5261,7 +5304,15 @@ function events_get_count_events_by_criticity( return false; } - $sql_filter .= sprintf(' AND id_grupo IN (%s) ', implode(',', $id_group)); + $tgroup_join = sprintf( + '%s + INNER JOIN tgrupo tg + ON (te.id_grupo = tg.id_grupo AND tg.id_grupo IN (%s)) + OR (tg.id_grupo = tasg.id_group AND tasg.id_group IN (%s))', + events_get_secondary_groups_left_join($tevento), + implode(',', $id_group), + implode(',', $id_group) + ); } if (!empty($filter['id_agent'])) { @@ -5361,16 +5412,19 @@ function events_get_count_events_by_criticity( $sql_where .= ' AND (evento LIKE "%'.io_safe_input($filter_event_filter_search).'%" OR id_evento LIKE "%'.io_safe_input($filter_event_filter_search).'%")'; } - $tevento = 'tevento'; - $sql = sprintf( - 'SELECT criticity, - COUNT(*) AS count - FROM %s - WHERE utimestamp > %d AND utimestamp <= %d - %s %s - GROUP BY criticity', + 'SELECT + te.criticity, + count(*) as count + FROM %s te + %s + WHERE + te.utimestamp > %d AND te.utimestamp <= %d + %s + %s + GROUP BY te.id_usuario', $tevento, + $tgroup_join, $datelimit, $date, $sql_filter, @@ -5419,23 +5473,26 @@ function events_get_count_events_validated( $dbmeta=false ) { global $config; + $tevento = 'tevento'; // Group. - $sql_filter = ' 1=1 '; + $sql_filter = ''; + $tgroup_join = ''; if (isset($filter['id_group'])) { - $id_group = groups_safe_acl( - $config['id_user'], - $filter['id_group'], - 'AR' - ); + $id_group = groups_safe_acl($config['id_user'], $filter['id_group'], 'AR'); if (empty($id_group)) { // An empty array means the user doesn't have access. return false; } - $sql_filter .= sprintf( - ' AND id_grupo IN (%s) ', + $tgroup_join = sprintf( + '%s + INNER JOIN tgrupo tg + ON (te.id_grupo = tg.id_grupo AND tg.id_grupo IN (%s)) + OR (tg.id_grupo = tasg.id_group AND tasg.id_group IN (%s))', + events_get_secondary_groups_left_join($tevento), + implode(',', $id_group), implode(',', $id_group) ); } @@ -5566,9 +5623,24 @@ function events_get_count_events_validated( $sql_where .= ' AND (evento LIKE "%'.io_safe_input($filter_event_filter_search).'%" OR id_evento LIKE "%'.io_safe_input($filter_event_filter_search).'%")'; } - $tevento = 'tevento'; - - $sql = sprintf('SELECT estado, COUNT(*) AS count FROM %s WHERE %s %s GROUP BY estado', $tevento, $sql_filter, $sql_where); + $sql = sprintf( + 'SELECT + te.estado, + count(*) as count + FROM %s te + %s + WHERE + te.utimestamp > %d AND te.utimestamp <= %d + %s + %s + GROUP BY te.id_usuario', + $tevento, + $tgroup_join, + $datelimit, + $date, + $sql_filter, + $sql_where + ); $rows = db_get_all_rows_sql($sql); diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php index 9eed30c03d..3c530f3115 100755 --- a/pandora_console/include/functions_reporting.php +++ b/pandora_console/include/functions_reporting.php @@ -3910,30 +3910,10 @@ function reporting_alert_report_group($report, $content) $return['description'] = $content['description']; $return['date'] = reporting_get_date_text($report, $content); - if ($content['id_group'] == 0) { - $agent_modules = db_get_all_rows_sql( - ' - SELECT distinct(id_agent_module) - FROM talert_template_modules - WHERE disabled = 0 - AND id_agent_module IN ( - SELECT id_agente_modulo - FROM tagente_modulo)' - ); - } else { - $agent_modules = db_get_all_rows_sql( - ' - SELECT distinct(id_agent_module) - FROM talert_template_modules - WHERE disabled = 0 - AND id_agent_module IN ( - SELECT id_agente_modulo - FROM tagente_modulo - WHERE id_agente IN ( - SELECT id_agente - FROM tagente WHERE id_grupo = '.$content['id_group'].'))' - ); - } + $agent_modules = alerts_get_agent_modules( + $content['id_group'], + $content['recursion'] + ); if (empty($alerts)) { $alerts = []; diff --git a/pandora_console/include/lib/Event.php b/pandora_console/include/lib/Event.php new file mode 100644 index 0000000000..d86288a00a --- /dev/null +++ b/pandora_console/include/lib/Event.php @@ -0,0 +1,176 @@ + $id_group]); + } else { + // Empty skel. + parent::__construct($table); + } + + } + + + /** + * Retrieves all events matching given filters. + * + * @param array $fields Fields to retrieve. + * @param array $filter Filter. + * @param integer $offset Offset. + * @param integer $limit Limit. + * @param string $order Order (asc or desc). + * @param string $sort_field Sort field. + * @param boolean $history Search history. + * @param boolean $return_sql Return sql or execute it. + * @param string $having Having. + * + * @return array|string|falsse Found events or SQL query or error. + */ + public static function search( + array $fields, + array $filter, + ?int $offset=null, + ?int $limit=null, + ?string $order=null, + ?string $sort_field=null, + bool $history=false, + bool $return_sql=false, + string $having='' + ):array { + return \events_get_all( + $fields, + $filter, + $offset, + $limit, + $order, + $sort_field, + $history, + $return_sql, + $having + ); + } + + + /** + * Saves current group definition to database. + * + * @return mixed Affected rows of false in case of error. + * @throws \Exception On error. + */ + public function save() + { + global $config; + + if (isset($config['centralized_management']) === true + && $config['centralized_management'] > 0 + ) { + throw new \Exception( + get_class($this).' error, cannot be modified while centralized management environment.' + ); + } + + if ($this->id_evento === null) { + // New. + return db_process_sql_insert( + 'tgrupo', + $this->fields + ); + } else if ($this->fields['id_evento'] > 0) { + // Update. + return db_process_sql_update( + 'tgrupo', + $this->fields, + ['id_evento' => $this->fields['id_evento']] + ); + } + + return false; + } + + + /** + * Return error message to target. + * + * @param string $msg Error message. + * + * @return void + */ + public static function error(string $msg) + { + echo json_encode(['error' => $msg]); + } + + + /** + * Verifies target method is allowed to be called using AJAX call. + * + * @param string $method Method to be invoked via AJAX. + * + * @return boolean Available (true), or not (false). + */ + public static function ajaxMethod(string $method):bool + { + return in_array($method, self::$ajaxMethods) === true; + } + + +}