diff --git a/pandora_console/include/ajax/custom_fields.php b/pandora_console/include/ajax/custom_fields.php index 0cb9626e7f..f67d7971bc 100644 --- a/pandora_console/include/ajax/custom_fields.php +++ b/pandora_console/include/ajax/custom_fields.php @@ -1,17 +1,33 @@ $value) { - $values_insert[] = '('.$value['id_server'].', '.$value['id_agente'].", '".$value['description']."', ".$value['status'].')'; + $values_insert[] = '('.$value['id_server'].', '.$value['id_agente'].", '".$value['description']."', '".$value['critical_count']."', '".$value['warning_count']."', '".$value['unknown_count']."', '".$value['notinit_count']."', '".$value['normal_count']."', '".$value['total_count']."', ".$value['status'].')'; } $values_insert_implode = implode(',', $values_insert); $query_insert = 'INSERT INTO temp_custom_fields VALUES '.$values_insert_implode; db_process_sql($query_insert); - // search table for alias, custom field data, server_name, direction + // Search table for alias, custom field data, server_name, direction. $search_query = ''; if ($search['value'] != '') { $search_query = ' AND (tma.alias LIKE "%'.$search['value'].'%"'; @@ -95,7 +134,61 @@ if (check_login()) { $search_query .= ' OR temp.name_custom_fields LIKE "%'.$search['value'].'%" ) '; } - // query all fields result + // Search for status module. + $status_agent_search = ''; + if (isset($id_status) === true && is_array($id_status) === true) { + if (in_array(-1, $id_status) === false) { + if (in_array(AGENT_MODULE_STATUS_NOT_NORMAL, $id_status) === false) { + $status_agent_search = ' AND temp.status IN ('.implode(',', $id_status).')'; + } else { + // Not normal statuses. + $status_agent_search = ' AND temp.status IN (1,2,3,4,5)'; + } + } + } + + // Search for status module. + $status_module_search = ''; + if (isset($module_status) === true && is_array($module_status) === true) { + if (in_array(-1, $module_status) === false) { + if (in_array(AGENT_MODULE_STATUS_NOT_NORMAL, $module_status) === false) { + if (count($module_status) > 0) { + $status_module_search = ' AND ( '; + foreach ($module_status as $key => $value) { + $status_module_search .= ($key != 0) ? ' OR (' : ' ('; + switch ($value) { + default: + case AGENT_STATUS_NORMAL: + $status_module_search .= ' temp.critical_count = 0 AND temp.warning_count = 0 AND temp.unknown_count = 0 AND temp.notinit_count <> temp.total_count ) '; + break; + case AGENT_STATUS_CRITICAL: + $status_module_search .= ' temp.critical_count > 0) '; + break; + + case AGENT_STATUS_WARNING: + $status_module_search .= ' temp.critical_count = 0 AND temp.warning_count > 0) '; + break; + + case AGENT_STATUS_UNKNOWN: + $status_module_search .= ' temp.critical_count = 0 AND temp.warning_count = 0 AND temp.unknown_count > 0) '; + break; + + case AGENT_STATUS_NOT_INIT: + $status_module_search .= ' temp.total_count = temp.notinit_count) '; + break; + } + } + + $status_module_search .= ' ) '; + } + } else { + // Not normal. + $status_module_search = ' AND NOT ( temp.critical_count = 0 AND temp.warning_count = 0 AND temp.unknown_count = 0 AND temp.notinit_count <> temp.total_count)'; + } + } + } + + // Query all fields result. $query = sprintf( 'SELECT tma.id_agente, @@ -112,10 +205,14 @@ if (check_login()) { AND temp.id_server = tma.id_tmetaconsole_setup WHERE tma.disabled = 0 %s + %s + %s %s LIMIT %d OFFSET %d ', $search_query, + $status_agent_search, + $status_module_search, $order_by, $length, $start @@ -123,23 +220,27 @@ if (check_login()) { $result = db_get_all_rows_sql($query); - // query count + // Query count. $query_count = sprintf( 'SELECT COUNT(tma.id_agente) AS `count` - FROM tmetaconsole_agent tma - INNER JOIN temp_custom_fields temp - ON temp.id_agent = tma.id_tagente - AND temp.id_server = tma.id_tmetaconsole_setup - WHERE tma.disabled = 0 - %s - ', - $search_query + FROM tmetaconsole_agent tma + INNER JOIN temp_custom_fields temp + ON temp.id_agent = tma.id_tagente + AND temp.id_server = tma.id_tmetaconsole_setup + WHERE tma.disabled = 0 + %s + %s + %s + ', + $search_query, + $status_agent_search, + $status_module_search ); $count = db_get_sql($query_count); - // for link nodes. + // For link nodes. $array_nodes = metaconsole_get_connections(); if (isset($array_nodes) && is_array($array_nodes)) { $hash_array_nodes = []; @@ -158,20 +259,20 @@ if (check_login()) { $user_rot13 = str_rot13($config['id_user']); $hashdata = $user.$pwd; $hashdata = md5($hashdata); - $url_hash = '&'.'loginhash=auto&'.'loginhash_data='.$hashdata.'&'.'loginhash_user='.$user_rot13; + $url_hash = '&loginhash=auto&loginhash_data='.$hashdata.'&loginhash_user='.$user_rot13; $hash_array_nodes[$server['id']]['hashurl'] = $url_hash; $hash_array_nodes[$server['id']]['server_url'] = $server['server_url']; } } - // prepare rows for table dinamic + // Prepare rows for table dinamic. $data = []; foreach ($result as $values) { $image_status = agents_get_image_status($values['status']); - // link nodes - $agent_link = ''; + // Link nodes. + $agent_link = ''; $agent_alias = ui_print_truncate_text( $values['alias'], @@ -225,7 +326,7 @@ if (check_login()) { $name_where = " AND tam.nombre LIKE '%".$module_search."%'"; } - // filter by status module + // Filter by status module. $and_module_status = ''; if (is_array($module_status)) { if (!in_array(-1, $module_status)) { @@ -260,7 +361,7 @@ if (check_login()) { $and_module_status .= ' ) '; } } else { - // not normal + // Not normal. $and_module_status = 'AND tae.estado <> 0 AND tae.estado <> 300 '; } } @@ -314,7 +415,12 @@ if (check_login()) { && $value['id_tipo_modulo'] != 23 && $value['id_tipo_modulo'] != 33 ) { - $table_modules->data[$key][1] = remove_right_zeros(number_format($value['datos'], $config['graph_precision'])); + $table_modules->data[$key][1] = remove_right_zeros( + number_format( + $value['datos'], + $config['graph_precision'] + ) + ); } else { $table_modules->data[$key][1] = $value['datos']; } @@ -329,7 +435,10 @@ if (check_login()) { ); $table_modules->data[$key][3] = $value['current_interval']; - $table_modules->data[$key][4] = ui_print_timestamp($value['utimestamp'], true); + $table_modules->data[$key][4] = ui_print_timestamp( + $value['utimestamp'], + true + ); switch ($value['estado']) { case 0: case 300: @@ -398,7 +507,7 @@ if (check_login()) { } } - // status agents from tagente + // Status agents from tagente. $sql_info_agents = 'SELECT * fROM tagente WHERE id_agente ='.$id_agent; $info_agents = db_get_row_sql($sql_info_agents); $status_agent = agents_get_status_from_counts($info_agents); @@ -463,7 +572,13 @@ if (check_login()) { false ); - $table->data[0][3] = html_print_submit_button(__('Load filter'), 'load_filter', false, 'class="sub upd"', true); + $table->data[0][3] = html_print_submit_button( + __('Load filter'), + 'load_filter', + false, + 'class="sub upd"', + true + ); echo "
"; html_print_table($table); @@ -474,7 +589,12 @@ if (check_login()) { } if ($append_tab_filter) { - $filters = json_decode(io_safe_output(get_parameter('filters', '')), true); + $filters = json_decode( + io_safe_output( + get_parameter('filters', '') + ), + true + ); $table = new StdClass; $table->id = 'save_filter_form'; @@ -485,7 +605,14 @@ if (check_login()) { if ($filters['id'] == 'extended_create_filter') { echo "
"; $table->data[0][0] = __('Filter name'); - $table->data[0][1] = html_print_input_text('id_name', '', '', 15, 255, true); + $table->data[0][1] = html_print_input_text( + 'id_name', + '', + '', + 15, + 255, + true + ); $table->data[1][0] = __('Group'); $table->data[1][1] = html_print_select_groups( @@ -510,7 +637,13 @@ if (check_login()) { ); $table->rowspan[0][2] = 2; - $table->data[0][2] = html_print_submit_button(__('Create filter'), 'create_filter', false, 'class="sub upd"', true); + $table->data[0][2] = html_print_submit_button( + __('Create filter'), + 'create_filter', + false, + 'class="sub upd"', + true + ); } else { echo "
"; echo "
"; @@ -552,8 +685,20 @@ if (check_login()) { false ); - $table->data[0][2] = html_print_submit_button(__('Delete filter'), 'delete_filter', false, 'class="sub upd"', true); - $table->data[1][2] = html_print_submit_button(__('Update filter'), 'update_filter', false, 'class="sub upd"', true); + $table->data[0][2] = html_print_submit_button( + __('Delete filter'), + 'delete_filter', + false, + 'class="sub upd"', + true + ); + $table->data[1][2] = html_print_submit_button( + __('Update filter'), + 'update_filter', + false, + 'class="sub upd"', + true + ); } html_print_table($table); @@ -561,17 +706,20 @@ if (check_login()) { } if ($create_filter_cf) { - // initialize result + // Initialize result. $result_array = []; $result_array['error'] = 0; $result_array['msg'] = ''; - // initialize vars - $filters = json_decode(io_safe_output(get_parameter('filters', '')), true); + // Initialize vars. + $filters = json_decode( + io_safe_output(get_parameter('filters', '')), + true + ); $name_filter = get_parameter('name_filter', ''); $group_search = get_parameter('group_search', 0); - // check that the name is not empty + // Check that the name is not empty. if ($name_filter == '') { $result_array['error'] = 1; $result_array['msg'] = ui_print_error_message( @@ -596,7 +744,7 @@ if (check_login()) { return; } - // check custom field is not empty + // Check custom field is not empty. if ($filters['id_custom_fields'] == '') { $result_array['error'] = 1; $result_array['msg'] = ui_print_error_message( @@ -608,13 +756,15 @@ if (check_login()) { return; } - // insert + // Insert. $values = []; $values['name'] = $name_filter; $values['group_search'] = $group_search; $values['id_group'] = $filters['group']; $values['id_custom_field'] = $filters['id_custom_fields']; - $values['id_custom_fields_data'] = json_encode($filters['id_custom_fields_data']); + $values['id_custom_fields_data'] = json_encode( + $filters['id_custom_fields_data'] + ); $values['id_status'] = json_encode($filters['id_status']); $values['module_search'] = $filters['module_search']; $values['module_status'] = json_encode($filters['module_status']); @@ -622,7 +772,7 @@ if (check_login()) { $insert = db_process_sql_insert('tagent_custom_fields_filter', $values); - // check error insert + // Check error insert. if ($insert) { $result_array['error'] = 0; $result_array['msg'] = ui_print_success_message( @@ -644,17 +794,17 @@ if (check_login()) { } if ($update_filter_cf) { - // initialize result + // Initialize result. $result_array = []; $result_array['error'] = 0; $result_array['msg'] = ''; - // initialize vars + // Initialize vars. $filters = json_decode(io_safe_output(get_parameter('filters', '')), true); $id_filter = get_parameter('id_filter', ''); $group_search = get_parameter('group_search', 0); - // check selected filter + // Check selected filter. if ($id_filter == -1) { $result_array['error'] = 1; $result_array['msg'] = ui_print_error_message( @@ -666,11 +816,11 @@ if (check_login()) { return; } - // array condition update + // Array condition update. $condition = []; $condition['id'] = $id_filter; - // check selected custom fields + // Check selected custom fields. if ($filters['id_custom_fields'] == '') { $result_array['error'] = 1; $result_array['msg'] = ui_print_error_message( @@ -682,7 +832,7 @@ if (check_login()) { return; } - // array values update + // Array values update. $values = []; $values['id_group'] = $filters['group']; $values['group_search'] = $group_search; @@ -693,10 +843,10 @@ if (check_login()) { $values['module_status'] = json_encode($filters['module_status']); $values['recursion'] = $filters['recursion']; - // update + // Update. $update = db_process_sql_update('tagent_custom_fields_filter', $values, $condition); - // check error insert + // Check error insert. if ($update) { $result_array['error'] = 0; $result_array['msg'] = ui_print_success_message( @@ -718,16 +868,16 @@ if (check_login()) { } if ($delete_filter_cf) { - // Initialize result + // Initialize result. $result_array = []; $result_array['error'] = 0; $result_array['msg'] = ''; - // Initialize vars + // Initialize vars. $filters = json_decode(io_safe_output(get_parameter('filters', '')), true); $id_filter = get_parameter('id_filter', ''); - // Check selected filter + // Check selected filter. if ($id_filter == -1) { $result_array['error'] = 1; $result_array['msg'] = ui_print_error_message( @@ -739,14 +889,14 @@ if (check_login()) { return; } - // Array condition update + // Array condition update. $condition = []; $condition['id'] = $id_filter; - // Delete + // Delete. $delete = db_process_sql_delete('tagent_custom_fields_filter', $condition); - // Check error insert + // Check error insert. if ($delete) { $result_array['error'] = 0; $result_array['msg'] = ui_print_success_message( diff --git a/pandora_console/include/functions_custom_fields.php b/pandora_console/include/functions_custom_fields.php index 7e28b1dcd2..d6d4bd1c45 100644 --- a/pandora_console/include/functions_custom_fields.php +++ b/pandora_console/include/functions_custom_fields.php @@ -1,16 +1,31 @@ 0 AND tae.estado <> 300 '; $empty_agents_count = ''; } } } - // filters module + // Filters module. if ($filters['module_search']) { $and_module_search = 'AND tam.nombre LIKE "%'.$filters['module_search'].'%"'; $empty_agents_count = ''; } - // filter group and check ACL groups + // Filter group and check ACL groups. $groups_and = ''; if (!users_can_manage_group_all('AR')) { if ($filters['group']) { @@ -338,7 +359,7 @@ function agent_counters_custom_fields($filters) } if ($filters['group']) { - // recursion check acl + // Recursion check acl. if ($filters['recursion']) { $recursion_groups = groups_get_id_recursive($filters['group'], true); if (!users_can_manage_group_all('AR')) { @@ -362,26 +383,26 @@ function agent_counters_custom_fields($filters) } } - // filter custom data + // Filter custom data. $custom_data_and = ''; if (!in_array(-1, $filters['id_custom_fields_data'])) { $custom_data_array = implode("', '", $filters['id_custom_fields_data']); $custom_data_and = "AND tcd.description IN ('".$custom_data_array."')"; } - // filter custom name + // Filter custom name. $custom_field_name = $filters['id_custom_fields']; if (is_metaconsole()) { $metaconsole_connections = metaconsole_get_connection_names(); - // For all nodes + // For all nodes. if (isset($metaconsole_connections) && is_array($metaconsole_connections)) { $result_meta = []; $data = []; foreach ($metaconsole_connections as $metaconsole) { - // Get server connection data + // Get server connection data. $server_data = metaconsole_get_connection($metaconsole); - // Establishes connection + // Establishes connection. if (metaconsole_load_external_db($server_data) !== NOERR) { continue; } @@ -470,7 +491,13 @@ function agent_counters_custom_fields($filters) WHEN ta.total_count = ta.notinit_count THEN 5 ELSE 0 - END) AS `status` + END) AS `status`, + ta.critical_count, + ta.warning_count, + ta.unknown_count, + ta.notinit_count, + ta.normal_count, + ta.total_count FROM tagente ta LEFT JOIN tagent_secondary_group tasg ON ta.id_agente = tasg.id_agent @@ -510,7 +537,7 @@ function agent_counters_custom_fields($filters) } $data = array_merge($data, $node_result); - // Restore connection to root node + // Restore connection to root node. metaconsole_restore_db(); } } @@ -519,7 +546,7 @@ function agent_counters_custom_fields($filters) $array_data = []; if (isset($result_meta) && is_array($result_meta)) { - // initialize counters + // Initialize counters. $final_result['counters_total'] = [ 't_m_normal' => 0, 't_m_critical' => 0, @@ -538,7 +565,7 @@ function agent_counters_custom_fields($filters) foreach ($result_meta as $k => $nodo) { if (isset($nodo) && is_array($nodo)) { foreach ($nodo as $key => $value) { - // Sum counters total + // Sum counters total. $final_result['counters_total']['t_m_normal'] += $value['m_normal']; $final_result['counters_total']['t_m_critical'] += $value['m_critical']; $final_result['counters_total']['t_m_warning'] += $value['m_warning']; @@ -553,7 +580,7 @@ function agent_counters_custom_fields($filters) $final_result['counters_total']['t_a_not_init'] += $value['a_not_init']; $final_result['counters_total']['t_a_agents'] += $value['a_agents']; - // Sum counters for data + // Sum counters for data. $array_data[$value['name_data']]['m_normal'] += $value['m_normal']; $array_data[$value['name_data']]['m_critical'] += $value['m_critical']; $array_data[$value['name_data']]['m_warning'] += $value['m_warning']; @@ -576,7 +603,7 @@ function agent_counters_custom_fields($filters) $final_result['indexed_descriptions'] = $data; } else { - // TODO + // TODO. $final_result = false; } @@ -586,7 +613,7 @@ function agent_counters_custom_fields($filters) function get_filters_custom_fields_view($id=0, $for_select=false, $name='') { - // filter group and check ACL groups + // Filter group and check ACL groups. $groups_and = ''; if (!users_can_manage_group_all()) { $user_groups = array_keys(users_get_groups(false, 'AR', false));