From d137e0d798a84eecb436c92fdc67509ee6cf8f9a Mon Sep 17 00:00:00 2001 From: "alejandro.campos@artica.es" Date: Tue, 29 Mar 2022 18:09:19 +0200 Subject: [PATCH 1/3] implemented custom data filter in event view --- pandora_console/extras/mr/53.sql | 3 + .../pandoradb_migrate_6.0_to_759.mysql.sql | 2 + .../godmode/events/custom_events.php | 1 + .../godmode/events/event_edit_filter.php | 71 ++++++++++------ pandora_console/include/ajax/events.php | 8 +- pandora_console/include/functions_events.php | 29 +++++++ .../operation/events/events.build_table.php | 9 ++- pandora_console/operation/events/events.php | 81 +++++++++++++++++++ .../operation/events/events_list.php | 13 ++- .../operation/events/export_csv.php | 2 +- pandora_console/pandoradb.sql | 2 + 11 files changed, 191 insertions(+), 30 deletions(-) diff --git a/pandora_console/extras/mr/53.sql b/pandora_console/extras/mr/53.sql index dfb7976a91..0540d5dfe7 100644 --- a/pandora_console/extras/mr/53.sql +++ b/pandora_console/extras/mr/53.sql @@ -2,6 +2,9 @@ START TRANSACTION; ALTER TABLE `tipam_vlan` ADD COLUMN `custom_id` bigint(20) unsigned DEFAULT NULL; +ALTER TABLE `tevent_filter` ADD COLUMN `custom_data` VARCHAR(500) DEFAULT ''; +ALTER TABLE `tevent_filter` ADD COLUMN `custom_data_filter_type` TINYINT UNSIGNED DEFAULT 0; + UPDATE `tuser_task` SET `parameters` = 'a:3:{i:0;a:2:{s:11:"description";s:11:"Description";s:4:"type";s:4:"text";}i:1;a:3:{s:11:"description";s:20:"Save to disk in path";s:4:"type";s:6:"string";s:13:"default_value";s:21:"_%_ATTACHMENT_PATH_%_";}i:2;a:3:{s:11:"description";s:14:"Active backups";s:4:"type";s:6:"number";s:13:"default_value";i:3;}}' WHERE `function_name` = 'cron_task_do_backup'; CREATE TABLE IF NOT EXISTS `tbackup` ( diff --git a/pandora_console/extras/pandoradb_migrate_6.0_to_759.mysql.sql b/pandora_console/extras/pandoradb_migrate_6.0_to_759.mysql.sql index 535182d147..41bdb0e6e2 100644 --- a/pandora_console/extras/pandoradb_migrate_6.0_to_759.mysql.sql +++ b/pandora_console/extras/pandoradb_migrate_6.0_to_759.mysql.sql @@ -1543,6 +1543,8 @@ ALTER TABLE `tevent_filter` MODIFY COLUMN `severity` text NOT NULL; ALTER TABLE tevent_filter ADD COLUMN `server_id` int(10) NOT NULL default 0; ALTER TABLE `tevent_filter` ADD COLUMN `time_from` TIME NULL; ALTER TABLE `tevent_filter` ADD COLUMN `time_to` TIME NULL; +ALTER TABLE `tevent_filter` ADD COLUMN `custom_data` VARCHAR(500) DEFAULT ''; +ALTER TABLE `tevent_filter` ADD COLUMN `custom_data_filter_type` TINYINT UNSIGNED DEFAULT 0; -- --------------------------------------------------------------------- -- Table `tusuario` diff --git a/pandora_console/godmode/events/custom_events.php b/pandora_console/godmode/events/custom_events.php index a53657ffce..fb5dce8830 100644 --- a/pandora_console/godmode/events/custom_events.php +++ b/pandora_console/godmode/events/custom_events.php @@ -116,6 +116,7 @@ $fields_available['data'] = __('Data'); $fields_available['module_status'] = __('Module Status'); $fields_available['mini_severity'] = __('Severity mini'); $fields_available['module_custom_id'] = __('Module custom ID'); +$fields_available['custom_data'] = __('Custom data'); // Remove fields already selected. diff --git a/pandora_console/godmode/events/event_edit_filter.php b/pandora_console/godmode/events/event_edit_filter.php index b52242f166..eebb4e213c 100644 --- a/pandora_console/godmode/events/event_edit_filter.php +++ b/pandora_console/godmode/events/event_edit_filter.php @@ -89,6 +89,8 @@ if ($id) { $tag_without_base64 = base64_encode($tag_without_json_clean); $filter_only_alert = $filter['filter_only_alert']; + $custom_data = $filter['custom_data']; + $custom_data_filter_type = $filter['custom_data_filter_type']; if ($id_agent_module != 0) { $text_module = modules_get_agentmodule_name($id_agent_module); @@ -165,30 +167,34 @@ if ($update || $create) { $tag_without_json = io_safe_input(base64_decode($tag_without_base64)); $filter_only_alert = get_parameter('filter_only_alert', ''); + $custom_data = get_parameter('custom_data', ''); + $custom_data_filter_type = get_parameter('custom_data_filter_type', ''); $values = [ - 'id_name' => $id_name, - 'id_group_filter' => $id_group_filter, - 'id_group' => $id_group, - 'event_type' => $event_type, - 'severity' => $severity, - 'status' => $status, - 'search' => $search, - 'text_agent' => $text_agent, - 'id_agent_module' => $id_agent_module, - 'id_agent' => $id_agent, - 'pagination' => $pagination, - 'event_view_hr' => $event_view_hr, - 'id_user_ack' => $id_user_ack, - 'group_rep' => $group_rep, - 'tag_with' => $tag_with_json, - 'tag_without' => $tag_without_json, - 'date_from' => $date_from, - 'date_to' => $date_to, - 'source' => $source, - 'id_extra' => $id_extra, - 'user_comment' => $user_comment, - 'filter_only_alert' => $filter_only_alert, + 'id_name' => $id_name, + 'id_group_filter' => $id_group_filter, + 'id_group' => $id_group, + 'event_type' => $event_type, + 'severity' => $severity, + 'status' => $status, + 'search' => $search, + 'text_agent' => $text_agent, + 'id_agent_module' => $id_agent_module, + 'id_agent' => $id_agent, + 'pagination' => $pagination, + 'event_view_hr' => $event_view_hr, + 'id_user_ack' => $id_user_ack, + 'group_rep' => $group_rep, + 'tag_with' => $tag_with_json, + 'tag_without' => $tag_without_json, + 'date_from' => $date_from, + 'date_to' => $date_to, + 'source' => $source, + 'id_extra' => $id_extra, + 'user_comment' => $user_comment, + 'filter_only_alert' => $filter_only_alert, + 'custom_data' => $custom_data, + 'custom_data_filter_type' => $custom_data_filter_type, ]; $severity = explode(',', $severity); @@ -627,9 +633,26 @@ $table->data[23][1] = html_print_input_text('id_extra', $id_extra, '', 11, 255, $table->data[24][0] = ''.__('Comment').''; $table->data[24][1] = html_print_input_text('user_comment', $user_comment, '', 35, 255, true); +$table->data[25][0] = ''.__('Custom data filter type').''; +$table->data[25][1] = html_print_select( + [ + '0' => __('Filter custom data by name field'), + '1' => __('Filter custom data by value field'), + ], + 'custom_data_filter_type', + $custom_data_filter_type, + '', + false, + '', + true +); + +$table->data[26][0] = ''.__('Custom data').''; +$table->data[26][1] = html_print_input_text('custom_data', $custom_data, '', 35, 255, true); + if (is_metaconsole()) { - $table->data[25][0] = ''.__('Id souce event').''; - $table->data[25][1] = html_print_input_text( + $table->data[27][0] = ''.__('Id souce event').''; + $table->data[27][1] = html_print_input_text( 'id_source_event', $id_source_event, '', diff --git a/pandora_console/include/ajax/events.php b/pandora_console/include/ajax/events.php index e8a0d5831a..71e0783c51 100644 --- a/pandora_console/include/ajax/events.php +++ b/pandora_console/include/ajax/events.php @@ -290,6 +290,8 @@ if ($save_event_filter) { $values['id_extra'] = get_parameter('id_extra'); $values['user_comment'] = get_parameter('user_comment'); $values['id_source_event'] = get_parameter('id_source_event'); + $values['custom_data'] = get_parameter('custom_data'); + $values['custom_data_filter_type'] = get_parameter('custom_data_filter_type'); if (is_metaconsole()) { $values['server_id'] = get_parameter('server_id'); @@ -344,6 +346,8 @@ if ($update_event_filter) { $values['id_extra'] = get_parameter('id_extra'); $values['user_comment'] = get_parameter('user_comment'); $values['id_source_event'] = get_parameter('id_source_event'); + $values['custom_data'] = get_parameter('custom_data'); + $values['custom_data_filter_type'] = get_parameter('custom_data_filter_type'); if (is_metaconsole() === true) { $values['server_id'] = get_parameter('server_id'); @@ -815,7 +819,9 @@ function save_new_filter() { "id_extra": $("#text-id_extra").val(), "user_comment": $("#text-user_comment").val(), "id_source_event": $("#text-id_source_event").val(), - "server_id": $("#server_id").val() + "server_id": $("#server_id").val(), + "custom_data": $("#text-custom_data").val(), + "custom_data_filter_type": $("#custom_data_filter_type").val() }, function (data) { $("#info_box").hide(); diff --git a/pandora_console/include/functions_events.php b/pandora_console/include/functions_events.php index 1eb5a1e34b..986fdab80c 100644 --- a/pandora_console/include/functions_events.php +++ b/pandora_console/include/functions_events.php @@ -210,6 +210,7 @@ function events_get_all_fields() $columns['data'] = __('Data'); $columns['module_status'] = __('Module status'); $columns['module_custom_id'] = __('Module custom id'); + $columns['custom_data'] = __('Custom data'); return $columns; } @@ -310,6 +311,9 @@ function events_get_column_name($field, $table_alias=false) case 'direccion': return __('Agent IP'); + case 'custom_data': + return __('Custom data'); + default: return __($field); } @@ -1118,6 +1122,23 @@ function events_get_all( ); } + // Custom data. + if (empty($filter['custom_data']) === false) { + if ($filter['custom_data_filter_type'] === '1') { + $sql_filters[] = sprintf( + ' AND JSON_VALID(custom_data) = 1 AND JSON_EXTRACT(custom_data, "$.*") LIKE lower("%%%s%%") ', + $filter['custom_data'], + $filter['custom_data'] + ); + } else { + $sql_filters[] = sprintf( + ' AND JSON_VALID(custom_data) = 1 AND JSON_KEYS(custom_data) REGEXP "%s" ', + $filter['custom_data'], + $filter['custom_data'] + ); + } + } + // Validated or in process by. if (empty($filter['id_user_ack']) === false) { $sql_filters[] = sprintf( @@ -5139,6 +5160,14 @@ function events_page_general($event) $data[1] = ''.__('N/A').''; } + $data = []; + $data[0] = __('Custom data'); + if ($event['custom_data'] != '') { + $data[1] = $event['custom_data']; + } else { + $data[1] = ''.__('N/A').''; + } + $table_general->data[] = $data; $table_data = $table_general->data; diff --git a/pandora_console/operation/events/events.build_table.php b/pandora_console/operation/events/events.build_table.php index b241cbcf16..1653f7ab7a 100644 --- a/pandora_console/operation/events/events.build_table.php +++ b/pandora_console/operation/events/events.build_table.php @@ -134,7 +134,7 @@ if ($group_rep == 2) { echo '
'.__('No events').'
'; } } else { - // fields that the user has selected to show + // Fields that the user has selected to show. if ($meta) { $show_fields = events_meta_get_custom_fields_user(); } else { @@ -345,6 +345,13 @@ if ($group_rep == 2) { $i++; } + + if ($fields === 'custom_data') { + $table->head[$i] = __('Custom data'); + $table->align[$i] = 'left'; + + $i++; + } } if (in_array('estado', $show_fields)) { diff --git a/pandora_console/operation/events/events.php b/pandora_console/operation/events/events.php index e29b4cbc16..5f3a733318 100644 --- a/pandora_console/operation/events/events.php +++ b/pandora_console/operation/events/events.php @@ -226,6 +226,16 @@ $server_id = get_parameter( $filter['id_server_meta'] ); +$custom_data_filter_type = get_parameter( + 'filter[custom_data_filter_type]', + $filter['custom_data_filter_type'] +); + +$custom_data = get_parameter( + 'filter[custom_data]', + $filter['custom_data'] +); + if (is_metaconsole() === true) { // Connect to node database. $id_node = $server_id; @@ -530,6 +540,8 @@ if ($loaded_filter !== false && $from_event_graph != 1 && !isset($fb64)) { $user_comment = $filter['user_comment']; $id_source_event = ($filter['id_source_event'] ?? ''); $server_id = $filter['server_id']; + $custom_data = $filter['custom_data']; + $custom_data_filter_type = $filter['custom_data_filter_type']; } } @@ -1451,6 +1463,54 @@ $adv_inputs[] = html_print_div( true ); +// Custom data filter type. +$custom_data_filter_type_input = html_print_select( + [ + '0' => __('Filter custom data by field name'), + '1' => __('Filter custom data by field value'), + ], + 'custom_data_filter_type', + $custom_data_filter_type, + '', + false, + -1, + true +); + +$adv_inputs[] = html_print_div( + [ + 'class' => 'filter_input', + 'content' => sprintf( + '%s', + __('Custom data filter'), + $custom_data_filter_type_input + ), + ], + true +); + +// Custom data. +$custom_data_input = html_print_input_text( + 'custom_data', + $custom_data, + '', + 5, + 255, + true +); + +$adv_inputs[] = html_print_div( + [ + 'class' => 'filter_input', + 'content' => sprintf( + '%s', + __('Custom data search'), + $custom_data_input + ), + ], + true +); + // Tags. if (is_metaconsole() === true) { $data = '
'.__('Events with following tags').''.html_print_table($tabletags_with, true).'
'; @@ -2259,6 +2319,27 @@ function process_datatables_item(item) { /* Module name */ item.id_agentmodule = item.module_name; + + if (item.custom_data !== '') { + var custom_data_str = ''; + + var item_custom_data_obj = (function(json_str) { + try { + return JSON.parse(json_str); + } catch (err) { + return false; + } + })(item.custom_data); + + if (item_custom_data_obj !== false) { + for (const [attr_name, val] of Object.entries(item_custom_data_obj)) { + custom_data_str += attr_name + ' = ' + val + '
'; + } + item.custom_data = custom_data_str; + } else { + item.custom_data = ''; + } + } } /* Datatables auxiliary functions ends */ diff --git a/pandora_console/operation/events/events_list.php b/pandora_console/operation/events/events_list.php index d5dbc2a13c..7cc90a56e7 100644 --- a/pandora_console/operation/events/events_list.php +++ b/pandora_console/operation/events/events_list.php @@ -127,6 +127,8 @@ if (is_ajax()) { $values['source'] = get_parameter('source'); $values['id_extra'] = get_parameter('id_extra'); $values['user_comment'] = get_parameter('user_comment'); + $values['custom_data'] = get_parameter('custom_data'); + $values['custom_data_filter_type'] = get_parameter('custom_data_filter_type'); if (is_metaconsole()) { $values['id_source_event'] = get_parameter('id_source_event'); @@ -182,6 +184,8 @@ if (is_ajax()) { $values['source'] = get_parameter('source'); $values['id_extra'] = get_parameter('id_extra'); $values['user_comment'] = get_parameter('user_comment'); + $values['custom_data'] = get_parameter('custom_data'); + $values['custom_data_filter_type'] = get_parameter('custom_data_filter_type'); if (is_metaconsole()) { $values['id_source_event'] = get_parameter('id_source_event'); @@ -1567,8 +1571,9 @@ $(document).ready( function() { "id_extra": $("#text-id_extra").val(), "user_comment": $("#text-user_comment").val(), "id_source_event": $("#text-id_source_event").val(), - "server_id" : $("#server_id").val() - + "server_id" : $("#server_id").val(), + "custom_data" : $("#text-custom_data").val(), + "custom_data_filter_type" : $("#custom_data_filter_type").val() }, function (data) { $(".info_box").hide(); @@ -1670,7 +1675,9 @@ $(document).ready( function() { "id_extra": $("#text-id_extra").val(), "user_comment": $("#text-user_comment").val(), "id_source_event": $("#text-id_source_event").val(), - "server_id" : $("#server_id").val() + "server_id" : $("#server_id").val(), + "custom_data" : $("#text-custom_data").val() + "custom_data_filter_type" : $("#custom_data_filter_type").val() }, function (data) { $(".info_box").hide(); diff --git a/pandora_console/operation/events/export_csv.php b/pandora_console/operation/events/export_csv.php index 4a7b22c944..4a4df7a11c 100644 --- a/pandora_console/operation/events/export_csv.php +++ b/pandora_console/operation/events/export_csv.php @@ -226,7 +226,7 @@ try { if ($custom_data_array !== null) { array_walk( $custom_data_array, - function (&$value, $field) { + function (&$value, $field) use ($separator) { if (is_array($value) === true) { $value = '['.implode($separator, $value).']'; } diff --git a/pandora_console/pandoradb.sql b/pandora_console/pandoradb.sql index e9ef437992..36668e3a09 100644 --- a/pandora_console/pandoradb.sql +++ b/pandora_console/pandoradb.sql @@ -1251,6 +1251,8 @@ CREATE TABLE IF NOT EXISTS `tevent_filter` ( `server_id` INT NOT NULL DEFAULT 0, `time_from` TIME NULL, `time_to` TIME NULL, + `custom_data` VARCHAR(500) DEFAULT '', + `custom_data_filter_type` TINYINT UNSIGNED DEFAULT 0, PRIMARY KEY (`id_filter`) ) ENGINE=InnoDB DEFAULT CHARSET=UTF8MB4; From 7bfa78a3d51ca5a013cef90ee3754205db5762d8 Mon Sep 17 00:00:00 2001 From: "alejandro.campos@artica.es" Date: Wed, 30 Mar 2022 10:01:37 +0200 Subject: [PATCH 2/3] minor fix --- pandora_console/include/functions_events.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/pandora_console/include/functions_events.php b/pandora_console/include/functions_events.php index 986fdab80c..c666b54b32 100644 --- a/pandora_console/include/functions_events.php +++ b/pandora_console/include/functions_events.php @@ -5160,14 +5160,6 @@ function events_page_general($event) $data[1] = ''.__('N/A').''; } - $data = []; - $data[0] = __('Custom data'); - if ($event['custom_data'] != '') { - $data[1] = $event['custom_data']; - } else { - $data[1] = ''.__('N/A').''; - } - $table_general->data[] = $data; $table_data = $table_general->data; From b7aca968a37dd68325d05d117ed22bcdcc023505 Mon Sep 17 00:00:00 2001 From: "alejandro.campos@artica.es" Date: Fri, 1 Apr 2022 14:06:51 +0200 Subject: [PATCH 3/3] minor change --- pandora_console/include/ajax/events.php | 4 +++- pandora_console/include/functions_events.php | 12 ++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/pandora_console/include/ajax/events.php b/pandora_console/include/ajax/events.php index 71e0783c51..a218a6d25e 100644 --- a/pandora_console/include/ajax/events.php +++ b/pandora_console/include/ajax/events.php @@ -894,7 +894,9 @@ function save_update_filter() { "id_extra": $("#text-id_extra").val(), "user_comment": $("#text-user_comment").val(), "id_source_event": $("#text-id_source_event").val(), - "server_id": $("#server_id").val() + "server_id": $("#server_id").val(), + "custom_data": $("#text-custom_data").val(), + "custom_data_filter_type": $("#custom_data_filter_type").val() }, function (data) { diff --git a/pandora_console/include/functions_events.php b/pandora_console/include/functions_events.php index c666b54b32..38752bfb72 100644 --- a/pandora_console/include/functions_events.php +++ b/pandora_console/include/functions_events.php @@ -1126,15 +1126,15 @@ function events_get_all( if (empty($filter['custom_data']) === false) { if ($filter['custom_data_filter_type'] === '1') { $sql_filters[] = sprintf( - ' AND JSON_VALID(custom_data) = 1 AND JSON_EXTRACT(custom_data, "$.*") LIKE lower("%%%s%%") ', - $filter['custom_data'], - $filter['custom_data'] + ' AND JSON_VALID(custom_data) = 1 + AND (JSON_EXTRACT(custom_data, "$.*") LIKE lower("%%%s%%") COLLATE utf8mb4_0900_ai_ci) ', + io_safe_output($filter['custom_data']) ); } else { $sql_filters[] = sprintf( - ' AND JSON_VALID(custom_data) = 1 AND JSON_KEYS(custom_data) REGEXP "%s" ', - $filter['custom_data'], - $filter['custom_data'] + ' AND JSON_VALID(custom_data) = 1 + AND (JSON_SEARCH(JSON_KEYS(custom_data), "all", lower("%%%s%%") COLLATE utf8mb4_0900_ai_ci) IS NOT NULL) ', + io_safe_output($filter['custom_data']) ); } }