diff --git a/pandora_console/include/functions_api.php b/pandora_console/include/functions_api.php index e4f2bee4e2..b93561255e 100644 --- a/pandora_console/include/functions_api.php +++ b/pandora_console/include/functions_api.php @@ -35,6 +35,8 @@ enterprise_include_once('include/functions_modules.php'); enterprise_include_once('include/functions_clusters.php'); enterprise_include_once('include/functions_alerts.php'); +use PandoraFMS\Enterprise\Cluster; + /** * Parse the "other" parameter. @@ -110,7 +112,7 @@ function returnError($typeError, $returnType='string') $returnType, [ 'type' => 'string', - 'data' => __('Id does not exist in BD.'), + 'data' => __('Id does not exist in database.'), ] ); break; @@ -13887,8 +13889,10 @@ function api_set_add_cluster_agent($thrash1, $thrash2, $other, $thrash3) $array_json = json_decode(base64_decode(io_safe_output($other['data'][0])), true); if (!empty($array_json)) { foreach ($array_json as $key => $element) { - $check_cluster_group = clusters_get_group($element['id']); - if ((!check_acl($config['id_user'], $check_cluster_group, 'AW')) + $clusters = Cluster::search(['id' => $element['id']]); + $cluster = $clusters[0]; + + if ((!check_acl($config['id_user'], $cluster['group'], 'AW')) || (!agents_check_access_agent($element['id_agent'], 'AW')) ) { continue; @@ -13918,8 +13922,10 @@ function api_set_add_cluster_item($thrash1, $thrash2, $other, $thrash3) $array_json = json_decode(base64_decode(io_safe_output($other['data'][0])), true); if (is_array($array_json)) { foreach ($array_json as $key => $element) { - $cluster_group = clusters_get_group($element['id']); - if (!check_acl($config['id_user'], $cluster_group, 'AW')) { + $clusters = Cluster::search(['id' => $element['id_cluster']]); + $cluster = $clusters[0]; + + if (!check_acl($config['id_user'], $cluster['group'], 'AW')) { continue; } @@ -14048,8 +14054,10 @@ function api_set_delete_cluster($id, $thrash1, $thrast2, $thrash3) return; } - $cluster_group = clusters_get_group($id); - if (!check_acl($config['id_user'], $cluster_group, 'AD')) { + $clusters = Cluster::search(['id' => $id]); + $cluster = $clusters[0]; + + if (!check_acl($config['id_user'], $cluster['group'], 'AD')) { returnError('error_set_delete_cluster', __('The user cannot access to the cluster')); return; } @@ -14066,7 +14074,7 @@ function api_set_delete_cluster($id, $thrash1, $thrast2, $thrash3) $tcluster_items_delete = db_process_sql('delete from tcluster_item where id_cluster = '.$id); $tcluster_agents_delete = db_process_sql('delete from tcluster_agent where id_cluster = '.$id); $tcluster_delete = db_process_sql('delete from tcluster where id = '.$id); - $tcluster_agent_delete = agents_delete_agent($temp_id_cluster[0]['id_agent']); + $tcluster_agent_delete = $temp_id_cluster[0]['id_agent'] !== null ? agents_delete_agent($temp_id_cluster[0]['id_agent']) : 0; if (($tcluster_modules_delete + $tcluster_items_delete + $tcluster_agents_delete + $tcluster_delete + $tcluster_agent_delete) == 0) { returnError('error_delete', 'Error in delete operation.'); @@ -14089,44 +14097,32 @@ function api_set_delete_cluster_agents($thrash1, $thrast2, $other, $thrash3) $target_agents = json_decode(base64_decode(io_safe_output($other['data'][0])), true); - $cluster_group = clusters_get_group($id_agent); - if (!users_is_admin($config['id_user'])) { - if (!$cluster_group - || (!check_acl($config['id_user'], $cluster_group, 'AW')) - || (!agents_check_access_agent($id_agent, 'AW')) - ) { - returnError('error_set_delete_cluster_agent', __('The user cannot access to the cluster')); - return; - } - } + $deleted_counter = 0; - $n_agents_deleted = 0; - $n_agents = 0; + foreach ($target_agents as $ta) { + $clusters = Cluster::search(['id' => $ta['id']]); + $cluster = $clusters[0]; - if (is_array($target_agents)) { - $target_clusters = []; - foreach ($target_agents as $data) { - $n_agents++; - if (!isset($target_clusters[$data['id']])) { - $target_clusters[$data['id']] = []; - } - - array_push($target_clusters[$data['id']], $data['id_agent']); - } - - foreach ($target_clusters as $id_cluster => $id_agent_array) { - $rs = cluster_delete_agents($id_cluster, $id_agent_array); - if ($rs !== false) { - $n_agents_deleted += $rs; + // $cluster_group = clusters_get_group($id_agent); + if (!users_is_admin($config['id_user'])) { + if (!$cluster['group'] + || (!check_acl($config['id_user'], $cluster['group'], 'AW')) + || (!agents_check_access_agent($ta['id_agent'], 'AW')) + ) { + returnError('error_set_delete_cluster_agent', __('The user cannot access to the cluster')); + return; } } + + $cluster = new Cluster($ta['id']); + $deleted = $cluster->removeMember($ta['id_agent']); + + if ($deleted === true) { + $deleted_counter++; + } } - if ($n_agents > $n_agents_deleted) { - returnError('error_delete', 'Error in delete operation.'); - } else { - returnData('string', ['type' => 'string', 'data' => $n_agents_deleted]); - } + returnData('string', ['type' => 'string', 'data' => $deleted_counter]); } @@ -14139,8 +14135,10 @@ function api_set_delete_cluster_item($id, $thrash1, $thrast2, $thrast3) return; } - $cluster_group = clusters_get_group($id); - if (!check_acl($config['id_user'], $cluster_group, 'AD')) { + $clusters = Cluster::search(['id' => $id]); + $cluster = $clusters[0]; + + if (!check_acl($config['id_user'], $cluster['group'], 'AD')) { returnError('error_set_delete_cluster_item', __('The user cannot access to the cluster')); return; } @@ -14314,8 +14312,10 @@ function api_get_cluster_status($id_cluster, $trash1, $trash2, $returnType) return; } - $cluster_group = clusters_get_group($id_cluster); - if (!check_acl($config['id_user'], $cluster_group, 'AR')) { + $clusters = Cluster::search(['id' => $id_cluster]); + $cluster = $clusters[0]; + + if (!check_acl($config['id_user'], $cluster['group'], 'AR')) { returnError('error_get_cluster_status', __('The user cannot access to the cluster')); return; } @@ -14346,21 +14346,23 @@ function api_get_cluster_id_by_name($cluster_name, $trash1, $trash2, $returnType return; } - $value = cluster_get_id_by_name($cluster_name); - if (($value === false) || ($value === null)) { + $clusters = Cluster::search(['name' => $cluster_name]); + + if ($clusters === false) { returnError('id_not_found', $returnType); return; } - $cluster_group = clusters_get_group($value); - if (!check_acl($config['id_user'], $cluster_group, 'AR')) { + $cluster = $clusters[0]; + + if (!check_acl($config['id_user'], $cluster['group'], 'AR')) { returnError('error_get_cluster_status', __('The user cannot access to the cluster')); return; } $data = [ 'type' => 'string', - 'data' => $value, + 'data' => $cluster['id'], ]; returnData($returnType, $data); @@ -14375,13 +14377,27 @@ function api_get_agents_id_name_by_cluster_id($cluster_id, $trash1, $trash2, $re return; } - $cluster_group = clusters_get_group($cluster_id); - if (!check_acl($config['id_user'], $cluster_group, 'AR')) { + $clusters = Cluster::search(['id' => $cluster_id]); + + if ($clusters === false) { + returnError('id_not_found', $returnType); + } + + $cluster = $clusters[0]; + + if (!check_acl($config['id_user'], $cluster['group'], 'AR')) { returnError('error_get_cluster_status', __('The user cannot access to the cluster')); return; } - $all_agents = cluster_get_agents_id_name_by_cluster_id($cluster_id); + $cluster = new Cluster($cluster_id); + $cluster_agents = $cluster->getMembers(); + + $all_agents = []; + + foreach ($cluster_agents as $ca) { + $all_agents[$ca->id_agente()] = $ca->name(); + } if ($all_agents !== false) { $data = [ @@ -14404,18 +14420,23 @@ function api_get_agents_id_name_by_cluster_name($cluster_name, $trash1, $trash2, return; } - $value = cluster_get_id_by_name($cluster_name); - if (($value === false) || ($value === null)) { - returnError('id_not_found', $returnType); - } + $clusters = Cluster::search(['name' => $cluster_name]); - $cluster_group = clusters_get_group($value); - if (!check_acl($config['id_user'], $cluster_group, 'AR')) { + $cluster = $clusters[0]; + + if (!check_acl($config['id_user'], $cluster['group'], 'AR')) { returnError('error_get_cluster_status', __('The user cannot access to the cluster')); return; } - $all_agents = cluster_get_agents_id_name_by_cluster_id($value); + $cluster = new Cluster($cluster['id']); + $cluster_agents = $cluster->getMembers(); + + $all_agents = []; + + foreach ($cluster_agents as $ca) { + $all_agents[$ca->id_agente()] = $ca->name(); + } if (count($all_agents) > 0 and $all_agents !== false) { $data = [ @@ -14478,13 +14499,35 @@ function api_get_modules_id_name_by_cluster_id($cluster_id) return; } - $cluster_group = clusters_get_group($cluster_id); - if (!check_acl($config['id_user'], $cluster_group, 'AR')) { + $clusters = Cluster::search(['id' => $cluster_id]); + + if ($clusters === false) { + returnError('error_clusters', 'Id does not exist in database.'); + } + + $cluster = $clusters[0]; + + if (!check_acl($config['id_user'], $cluster['group'], 'AR')) { returnError('error_get_cluster_status', __('The user cannot access to the cluster')); return; } - $all_modules = cluster_get_modules_id_name_by_cluster_id($cluster_id); + $cluster_type = $cluster['cluster_type'] == 'AA' ? MODULE_PREDICTION_CLUSTER_AA : MODULE_PREDICTION_CLUSTER_AP; + + $cluster = new Cluster($cluster_id); + $cluster_modules = $cluster->getItems($cluster_type); + + $all_modules = []; + + foreach ($cluster_modules as $cm) { + $module_obj = $cm->getModule(); + $module_values = $module_obj->toArray(); + + $id_agent_module = $module_values['id_agente_modulo']; + $module_name = $module_values['nombre']; + + $all_modules[$id_agent_module] = $module_name; + } if (count($all_modules) > 0 and $all_modules !== false) { $data = [ @@ -14508,18 +14551,30 @@ function api_get_modules_id_name_by_cluster_name($cluster_name) return; } - $value = cluster_get_id_by_name($cluster_name); - if (($value === false) || ($value === null)) { - returnError('id_not_found', $returnType); - } + $clusters = Cluster::search(['name' => $cluster_name]); + $cluster = $clusters[0]; - $cluster_group = clusters_get_group($value); - if (!check_acl($config['id_user'], $cluster_group, 'AR')) { + if (!check_acl($config['id_user'], $cluster['group'], 'AR')) { returnError('error_get_cluster_status', __('The user cannot access to the cluster')); return; } - $all_modules = cluster_get_modules_id_name_by_cluster_id($value); + $cluster_type = $cluster['cluster_type'] == 'AA' ? MODULE_PREDICTION_CLUSTER_AA : MODULE_PREDICTION_CLUSTER_AP; + + $cluster = new Cluster($cluster['id']); + $cluster_modules = $cluster->getItems($cluster_type); + + $all_modules = []; + + foreach ($cluster_modules as $cm) { + $module_obj = $cm->getModule(); + $module_values = $module_obj->toArray(); + + $id_agent_module = $module_values['id_agente_modulo']; + $module_name = $module_values['nombre']; + + $all_modules[$id_agent_module] = $module_name; + } if (count($all_modules) > 0 and $all_modules !== false) { $data = [ @@ -14706,13 +14761,61 @@ function api_get_cluster_items($cluster_id) return; } - $cluster_group = clusters_get_group($cluster_id); - if (!check_acl($config['id_user'], $cluster_group, 'AR')) { + $clusters = Cluster::search(['id' => $cluster_id]); + $cluster = $clusters[0]; + + if (!check_acl($config['id_user'], $cluster['group'], 'AR')) { returnError('error_get_cluster_status', __('The user cannot access to the cluster')); return; } - $all_items = cluster_get_items($cluster_id); + $cluster = new Cluster($cluster_id); + $cluster_modules_AA = $cluster->getItems(MODULE_PREDICTION_CLUSTER_AA); + $cluster_modules_AP = $cluster->getItems(MODULE_PREDICTION_CLUSTER_AP); + $cluster_modules = array_merge($cluster_modules_AA, $cluster_modules_AP); + + $cluster_items = []; + + foreach ($cluster_modules as $cm) { + $cluster_module_values = $cm->toArray(); + + $module_obj = $cm->getModule(); + $module_values = $module_obj->toArray(); + + $cluster_items[] = [ + 'id' => $cluster_module_values['id'], + 'name' => $cluster_module_values['name'], + 'id_agente_modulo' => $module_values['id_agente_modulo'], + 'item_type' => $cluster_module_values['item_type'], + 'critical_limit' => $cluster_module_values['critical_limit'], + 'warning_limit' => $cluster_module_values['warning_limit'], + 'is_critical' => $cluster_module_values['is_critical'], + ]; + } + + $cluster_items_2 = []; + + foreach ($cluster_items as $key => $value) { + $cluster_items_2[$cluster_items[$key]['id']]['name'] = $cluster_items[$key]['name']; + $cluster_items_2[$cluster_items[$key]['id']]['id_agente_modulo'] = $cluster_items[$key]['id_agente_modulo']; + $cluster_items_2[$cluster_items[$key]['id']]['type'] = $cluster_items[$key]['item_type']; + + if ($cluster_items_2[$cluster_items[$key]['id']]['type'] == 'AA') { + $cluster_items_2[$cluster_items[$key]['id']]['pcrit'] = $cluster_items[$key]['critical_limit']; + } else if ($cluster_items_2[$cluster_items[$key]['id']]['type'] == 'AP') { + $cluster_items_2[$cluster_items[$key]['id']]['pcrit'] = $cluster_items[$key]['is_critical']; + } + + if ($cluster_items_2[$cluster_items[$key]['id']]['type'] == 'AA') { + $cluster_items_2[$cluster_items[$key]['id']]['pwarn'] = $cluster_items[$key]['warning_limit']; + } else if ($cluster_items_2[$cluster_items[$key]['id']]['type'] == 'AP') { + $cluster_items_2[$cluster_items[$key]['id']]['pwarn'] = null; + } + + unset($cluster_items_2[$key]['id']); + } + + $all_items = $cluster_items_2; if (count($all_items) > 0 and $all_items !== false) { $data = [