diff --git a/pandora_console/extras/mr/62.sql b/pandora_console/extras/mr/62.sql new file mode 100644 index 0000000000..c2a309eb32 --- /dev/null +++ b/pandora_console/extras/mr/62.sql @@ -0,0 +1,23 @@ +START TRANSACTION; + +CREATE TABLE IF NOT EXISTS `tmonitor_filter` ( + `id_filter` INT UNSIGNED NOT NULL AUTO_INCREMENT, + `id_name` VARCHAR(600) NOT NULL, + `id_group_filter` INT NOT NULL DEFAULT 0, + `ag_group` INT NOT NULL DEFAULT 0, + `recursion` TEXT, + `status` INT NOT NULL DEFAULT -1, + `ag_modulename` TEXT, + `ag_freestring` TEXT, + `tag_filter` TEXT, + `moduletype` TEXT, + `module_option` INT DEFAULT 1, + `modulegroup` INT NOT NULL DEFAULT -1, + `min_hours_status` TEXT, + `datatype` TEXT, + `not_condition` TEXT, + `ag_custom_fields` TEXT, + PRIMARY KEY (`id_filter`) +) ENGINE=InnoDB DEFAULT CHARSET=UTF8MB4; + +COMMIT; diff --git a/pandora_console/include/ajax/module.php b/pandora_console/include/ajax/module.php index 61720a99a4..03eb150573 100755 --- a/pandora_console/include/ajax/module.php +++ b/pandora_console/include/ajax/module.php @@ -66,6 +66,12 @@ if (check_login()) { 0 ); + $load_filter_modal = get_parameter('load_filter_modal', 0); + $save_filter_modal = get_parameter('save_filter_modal', 0); + $get_monitor_filters = get_parameter('get_monitor_filters', 0); + $save_monitor_filter = get_parameter('save_monitor_filter', 0); + $update_monitor_filter = get_parameter('update_monitor_filter', 0); + if ($get_agent_modules_json_by_name === true) { $agent_name = get_parameter('agent_name'); @@ -1629,4 +1635,534 @@ if (check_login()) { return; } + + // Saves an event filter. + if ($save_monitor_filter) { + $values = []; + $values['id_name'] = get_parameter('id_name'); + $values['id_group_filter'] = get_parameter('id_group_filter'); + $values['ag_group'] = get_parameter('ag_group'); + $values['modulegroup'] = get_parameter('modulegroup'); + $values['recursion'] = get_parameter('recursion'); + $values['status'] = get_parameter('status'); + $values['ag_modulename'] = get_parameter('ag_modulename'); + $values['ag_freestring'] = get_parameter('ag_freestring'); + $values['tag_filter'] = json_encode(get_parameter('tag_filter')); + $values['moduletype'] = get_parameter('moduletype'); + $values['module_option'] = get_parameter('module_option'); + $values['min_hours_status'] = get_parameter('min_hours_status'); + $values['datatype'] = get_parameter('datatype'); + $values['not_condition'] = get_parameter('not_condition'); + $values['ag_custom_fields'] = get_parameter('ag_custom_fields'); + + $exists = (bool) db_get_value_filter( + 'id_filter', + 'tmonitor_filter', + $values + ); + + if ($exists === true) { + echo 'duplicate'; + } else { + $result = db_process_sql_insert('tmonitor_filter', $values); + + if ($result === false) { + echo 'error'; + } else { + echo $result; + } + } + } + + if ($update_monitor_filter) { + $values = []; + $id = get_parameter('id'); + + $values['ag_group'] = get_parameter('ag_group'); + $values['modulegroup'] = get_parameter('modulegroup'); + $values['recursion'] = get_parameter('recursion'); + $values['status'] = get_parameter('status'); + $values['ag_modulename'] = get_parameter('ag_modulename'); + $values['ag_freestring'] = get_parameter('ag_freestring'); + $values['tag_filter'] = json_encode(get_parameter('tag_filter')); + $values['moduletype'] = get_parameter('moduletype'); + $values['module_option'] = get_parameter('module_option'); + $values['min_hours_status'] = get_parameter('min_hours_status'); + $values['datatype'] = get_parameter('datatype'); + $values['not_condition'] = get_parameter('not_condition'); + $values['ag_custom_fields'] = get_parameter('ag_custom_fields'); + + $result = db_process_sql_update( + 'tmonitor_filter', + $values, + ['id_filter' => $id] + ); + + if ($result === false) { + echo 'error'; + } else { + echo 'ok'; + } + } + + if ($get_monitor_filters) { + $sql = 'SELECT id_filter, id_name FROM tmonitor_filter'; + + $monitor_filters = db_get_all_rows_sql($sql); + + $result = []; + + if ($monitor_filters !== false) { + foreach ($monitor_filters as $monitor_filter) { + $result[$monitor_filter['id_filter']] = $monitor_filter['id_name']; + } + } + + echo io_json_mb_encode($result); + } + + if ((int) $load_filter_modal === 1) { + $user_groups = users_get_groups( + $config['id_user'], + 'AR', + users_can_manage_group_all(), + true + ); + + $sql = 'SELECT id_filter, id_name + FROM tmonitor_filter + WHERE id_group_filter IN ('.implode(',', array_keys($user_groups)).')'; + + $event_filters = db_get_all_rows_sql($sql); + + $filters = []; + foreach ($event_filters as $event_filter) { + $filters[$event_filter['id_filter']] = $event_filter['id_name']; + } + + echo '
'; + echo '
'; + + $table = new StdClass; + $table->id = 'load_filter_form'; + $table->width = '100%'; + $table->cellspacing = 4; + $table->cellpadding = 4; + $table->class = 'databox'; + if (is_metaconsole()) { + $table->cellspacing = 0; + $table->cellpadding = 0; + $table->class = 'databox filters'; + } + + $table->styleTable = 'font-weight: bold; color: #555; text-align:left;'; + $filter_id_width = '200px'; + if (is_metaconsole()) { + $filter_id_width = '150px'; + } + + $data = []; + $table->rowid[3] = 'update_filter_row1'; + $data[0] = __('Load filter').$jump; + $data[0] .= html_print_select( + $filters, + 'filter_id', + $current, + '', + __('None'), + 0, + true, + false, + true, + '', + false, + 'margin-left:5px; width:'.$filter_id_width.';' + ); + $data[1] = html_print_submit_button( + __('Load filter'), + 'load_filter', + false, + 'class="sub upd"', + true + ); + $data[1] .= html_print_input_hidden('load_filter', 1, true); + $table->data[] = $data; + $table->rowclass[] = ''; + + html_print_table($table); + echo '
'; + echo '
'; + ?> + + + '; + if (check_acl($config['id_user'], 0, 'EW') + || check_acl($config['id_user'], 0, 'EM') + ) { + echo '
'; + $table = new StdClass; + $table->id = 'save_filter_form'; + $table->width = '100%'; + $table->cellspacing = 4; + $table->cellpadding = 4; + $table->class = 'databox'; + if (is_metaconsole()) { + $table->class = 'databox filters'; + $table->cellspacing = 0; + $table->cellpadding = 0; + } + + $table->styleTable = 'font-weight: bold; text-align:left;'; + if (!is_metaconsole()) { + $table->style[0] = 'width: 50%; width:50%;'; + } + + $data = []; + $table->rowid[0] = 'update_save_selector'; + $data[0] = html_print_radio_button( + 'filter_mode', + 'new', + '', + true, + true + ).__('New filter').''; + + $data[1] = html_print_radio_button( + 'filter_mode', + 'update', + '', + false, + true + ).__('Update filter').''; + + $table->data[] = $data; + $table->rowclass[] = ''; + + $data = []; + $table->rowid[1] = 'save_filter_row1'; + $data[0] = __('Filter name').$jump; + $data[0] .= html_print_input_text('id_name', '', '', 15, 255, true); + if (is_metaconsole()) { + $data[1] = __('Save in Group').$jump; + } else { + $data[1] = __('Filter group').$jump; + } + + $user_groups_array = users_get_groups_for_select( + $config['id_user'], + 'EW', + users_can_manage_group_all(), + true + ); + + $data[1] .= html_print_select( + $user_groups_array, + 'id_group_filter_dialog', + $id_group_filter, + '', + '', + 0, + true, + false, + false, + 'w130' + ); + + $table->data[] = $data; + $table->rowclass[] = ''; + + $data = []; + $table->rowid[2] = 'save_filter_row2'; + + $table->data[] = $data; + $table->rowclass[] = ''; + + $data = []; + $table->rowid[3] = 'update_filter_row1'; + $data[0] = __('Overwrite filter').$jump; + + $sql = 'SELECT id_filter, id_name FROM tmonitor_filter'; + $monitor_filters = db_get_all_rows_sql($sql); + + $_filters_update = []; + + if ($monitor_filters !== false) { + foreach ($monitor_filters as $monitor_filter) { + $_filters_update[$monitor_filter['id_filter']] = $monitor_filter['id_name']; + } + } + + $data[0] .= html_print_select( + $_filters_update, + 'overwrite_filter', + '', + '', + '', + 0, + true + ); + $data[1] = html_print_submit_button( + __('Update filter'), + 'update_filter', + false, + 'class="sub upd" onclick="save_update_filter();"', + true + ); + + $table->data[] = $data; + $table->rowclass[] = ''; + + html_print_table($table); + echo '
'; + echo html_print_submit_button( + __('Save filter'), + 'save_filter', + false, + 'class="sub upd float-right" onclick="save_new_filter();"', + true + ); + echo '
'; + } else { + include 'general/noaccess.php'; + } + + echo ''; + ?> + + 0) { + $user_groups_fl = users_get_groups( + $config['id_user'], + 'AR', + users_can_manage_group_all(), + true + ); + + $sql = sprintf( + 'SELECT id_filter, id_name + FROM tmonitor_filter + WHERE id_filter = %d AND id_group_filter IN (%s)', + $load_filter_id, + implode(',', array_keys($user_groups_fl)) + ); + + $loaded_filter = db_get_row_sql($sql); +} + +if ($loaded_filter['id_filter'] > 0) { + $query_filter['id_filter'] = $load_filter_id; + $filter = db_get_row_filter('tmonitor_filter', $query_filter, false); + if ($filter !== false) { + $ag_group = $filter['ag_group']; + $recursion = $filter['recursion']; + $status = $filter['status']; + $modulegroup = $filter['modulegroup']; + $ag_modulename = $filter['ag_modulename']; + $ag_freestring = $filter['ag_freestring']; + $tag_filter = $filter['tag_filter']; + $moduletype = $filter['moduletype']; + $module_option = $filter['module_option']; + $min_hours_status = $filter['min_hours_status']; + $datatype = $filter['datatype']; + $not_condition = $filter['not_condition']; + $ag_custom_fields = $filter['ag_custom_fields']; + + if ($not_condition === 'false') { + $not_condition = ''; + } + + if ($not_condition !== '') { + $is_none = 'None'; + $not_condition = 'NOT'; + } + + if ($not_condition !== '') { + $condition_query = '!='; + } + + if (is_array($tag_filter) === false) { + $tag_filter = json_decode($tag_filter, true); + } + if ($tag_filter === '') { + $tag_filter = [0 => 0]; + } + + if (is_array($ag_custom_fields) === false) { + $ag_custom_fields = json_decode(io_safe_output($ag_custom_fields), true); + } + } +} + // Agent group selector. if (!$is_metaconsole) { if ($ag_group > 0 && check_acl($config['id_user'], $ag_group, 'AR')) { @@ -370,19 +436,21 @@ if (!empty($ag_custom_fields)) { } } +$all_tags = in_array(0, $tag_filter); + // Filter by tag. -if ($tag_filter !== 0) { - if ($is_metaconsole) { - $sql_conditions .= ' AND tagente_modulo.id_agente_modulo IN ( - SELECT ttag_module.id_agente_modulo - FROM ttag_module - WHERE ttag_module.id_tag '.$not_condition.' IN ('.$tag_filter.'))'; - } else { - $sql_conditions .= ' AND tagente_modulo.id_agente_modulo IN ( - SELECT ttag_module.id_agente_modulo - FROM ttag_module - WHERE ttag_module.id_tag '.$condition_query.' '.$tag_filter.')'; +if ($all_tags === false) { + $sql_conditions .= ' AND tagente_modulo.id_agente_modulo IN ( + SELECT ttag_module.id_agente_modulo + FROM ttag_module + WHERE 1=1'; + + if ($all_tags === false) { + $sql_conditions .= ' AND ttag_module.id_tag '.$not_condition.' IN ('.implode(',', $tag_filter).'))'; } +} else if ($not_condition === 'NOT') { + // Match nothing if not condition has been selected along with all tags selected (none). + $sql_conditions .= ' AND 0=0'; } @@ -475,13 +543,14 @@ $table->data[0][1] .= html_print_select_groups( false, $not_condition ); + $table->data[0][1] .= '
'; $table->data[0][1] .= html_print_input( [ 'type' => 'checkbox', 'name' => 'recursion', 'return' => true, - 'checked' => $recursion, + 'checked' => ($recursion === true || $recursion === 'true' || $recursion === '1') ? 'checked' : false, 'value' => 1, ] ); @@ -556,13 +625,13 @@ if (empty($tags)) { } else { $table->data[1][5] = html_print_select( $tags, - 'tag_filter', + 'tag_filter[]', $tag_filter, '', - __($is_none), - '', + __('All'), + 0, + true, true, - false, true, '', false, @@ -666,7 +735,6 @@ $table2->data[1][0] = 'data[1][0] .= '>'.__('Data type').''; $table2->data[1][1] .= '
'; - switch ($moduletype) { case 1: $sql = sprintf( @@ -771,7 +839,7 @@ $table2->data[1][3] = html_print_div( 'type' => 'switch', 'name' => 'not_condition', 'return' => false, - 'checked' => $check_not_condition, + 'checked' => ($check_not_condition === true || $check_not_condition === 'true' || $check_not_condition === '1') ? 'checked' : false, 'value' => 'NOT', 'id' => 'not_condition_switch', 'onclick' => 'changeNotConditionStatus(this)', @@ -855,9 +923,30 @@ $table->data[3][0] = ui_toggle( 'white_table_graph' ); -$table->colspan[4][0] = 7; +$table->colspan[4][0] = 2; $table->cellstyle[4][0] = 'padding-top: 0px;'; -$table->data[4][0] = html_print_submit_button( +$table->data[4][0] = html_print_button( + __('Load filter'), + 'load-filter', + false, + '', + 'class="float-left margin-right-2 sub config"', + true +); + +$table->cellstyle[4][0] .= 'padding-top: 0px;'; +$table->data[4][0] .= html_print_button( + __('Save filter'), + 'save-filter', + false, + '', + 'class="float-left margin-right-2 sub wand"', + true +); + +$table->colspan[4][2] = 5; +$table->cellstyle[4][2] = 'padding-top: 0px;'; +$table->data[4][2] = html_print_submit_button( __('Show'), 'uptbutton', false, @@ -2017,16 +2106,76 @@ if (!empty($result)) { } - // End Build List Result. - echo "
"; +// End Build List Result. +echo "
"; - enterprise_hook('close_meta_frame'); +// Load filter div for dialog. +echo ''; +echo ''; - ui_require_javascript_file('pandora_modules'); +enterprise_hook('close_meta_frame'); + +ui_require_javascript_file('pandora_modules'); ?>