Now the agents retrieved by group can be served with the key serialized when using the metaconsole

This commit is contained in:
Alejandro Gallardo Escobar 2015-08-05 10:36:35 +02:00
parent 9c8aef62dd
commit d14e250ad8
2 changed files with 31 additions and 16 deletions

View File

@ -38,20 +38,22 @@ if ($get_agents_group) {
$id_group = (int) get_parameter('id_group', -1); $id_group = (int) get_parameter('id_group', -1);
$mode = (string) get_parameter('mode', 'json'); $mode = (string) get_parameter('mode', 'json');
$id_server = (int) get_parameter('id_server', 0); $id_server = (int) get_parameter('id_server', 0);
$serialized = (bool) get_parameter('serialized');
$return = array(); $return = array();
if ($id_group != -1) { if ($id_group != -1) {
$filter = array(); $filter = array();
if (defined('METACONSOLE')) { if (is_metaconsole() && !empty($id_server)) {
$filter['id_server'] = $id_server; $filter['id_server'] = $id_server;
} }
$return = agents_get_group_agents($id_group, $filter, "none"); $return = agents_get_group_agents($id_group, $filter, "none", false, false, $serialized);
} }
switch ($mode) { switch ($mode) {
case 'json': case 'json':
default:
echo json_encode($return); echo json_encode($return);
break; break;
} }
@ -59,7 +61,7 @@ if ($get_agents_group) {
return; return;
} }
if ($search_agents && ((!defined('METACONSOLE')) || $force_local)) { if ($search_agents && (!is_metaconsole() || $force_local)) {
$id_agent = (int) get_parameter('id_agent'); $id_agent = (int) get_parameter('id_agent');
$string = (string) get_parameter('q'); /* q is what autocomplete plugin gives */ $string = (string) get_parameter('q'); /* q is what autocomplete plugin gives */
@ -166,7 +168,7 @@ if ($search_agents && ((!defined('METACONSOLE')) || $force_local)) {
echo json_encode($data); echo json_encode($data);
return; return;
} }
elseif ($search_agents && ($config['metaconsole'] == 1) && defined('METACONSOLE')) { elseif ($search_agents && is_metaconsole()) {
$id_agent = (int) get_parameter ('id_agent'); $id_agent = (int) get_parameter ('id_agent');
$string = (string) get_parameter ('q'); /* q is what autocomplete plugin gives */ $string = (string) get_parameter ('q'); /* q is what autocomplete plugin gives */

View File

@ -757,11 +757,13 @@ function agents_common_modules ($id_agent, $filter = false, $indexed = true, $ge
* @param string $case Which case to return the agentname as (lower, upper, none) * @param string $case Which case to return the agentname as (lower, upper, none)
* @param boolean $noACL jump the ACL test. * @param boolean $noACL jump the ACL test.
* @param boolean $childGroups The flag to get agents in the child group of group parent passed. By default false. * @param boolean $childGroups The flag to get agents in the child group of group parent passed. By default false.
* @param boolean $serialized Only in metaconsole. Return the key as <server id><SEPARATOR><agent id>. By default false.
* @param string $separator Only in metaconsole. Separator for the serialized data. By default |.
* *
* @return array An array with all agents in the group or an empty array * @return array An array with all agents in the group or an empty array
*/ */
function agents_get_group_agents ($id_group = 0, $search = false, function agents_get_group_agents ($id_group = 0, $search = false,
$case = "lower", $noACL = false, $childGroups = false) { $case = "lower", $noACL = false, $childGroups = false, $serialized = false, $separator = '|') {
global $config; global $config;
@ -776,8 +778,6 @@ function agents_get_group_agents ($id_group = 0, $search = false,
} }
} }
if ($childGroups) { if ($childGroups) {
if (is_array($id_group)) { if (is_array($id_group)) {
foreach ($id_group as $parent) { foreach ($id_group as $parent) {
@ -860,7 +860,7 @@ function agents_get_group_agents ($id_group = 0, $search = false,
unset($search['status']); unset($search['status']);
} }
if (defined('METACONSOLE') && isset($search['id_server'])) { if (is_metaconsole() && isset($search['id_server'])) {
$filter['id_tmetaconsole_setup'] = $search['id_server']; $filter['id_tmetaconsole_setup'] = $search['id_server'];
if ($filter['id_tmetaconsole_setup'] == 0) { if ($filter['id_tmetaconsole_setup'] == 0) {
@ -882,41 +882,54 @@ function agents_get_group_agents ($id_group = 0, $search = false,
$filter['order'] = 'nombre'; $filter['order'] = 'nombre';
if (defined('METACONSOLE')) { if (is_metaconsole()) {
$table_name = 'tmetaconsole_agent'; $table_name = 'tmetaconsole_agent';
$fields = array( $fields = array(
'id_tagente AS id_agente', 'nombre' 'id_tagente AS id_agente',
'nombre',
'id_tmetaconsole_setup AS id_server'
); );
} }
else { else {
$table_name = 'tagente'; $table_name = 'tagente';
$fields = array( $fields = array(
'id_agente', 'nombre' 'id_agente',
'nombre'
); );
} }
$result = db_get_all_rows_filter($table_name, $filter, $fields); $result = db_get_all_rows_filter($table_name, $filter, $fields);
if ($result === false) if ($result === false)
return array (); //Return an empty array return array (); //Return an empty array
$agents = array (); $agents = array ();
foreach ($result as $row) { foreach ($result as $row) {
if (!isset($row["id_agente"]) || !isset($row["nombre"]))
continue;
if ($serialized && isset($row["id_server"])) {
$key = $row["id_server"] . $separator . $row["id_agente"];
}
else {
$key = $row["id_agente"];
}
switch ($case) { switch ($case) {
case "lower": case "lower":
$agents[$row["id_agente"]] = mb_strtolower ($row["nombre"], "UTF-8"); $value = mb_strtolower ($row["nombre"], "UTF-8");
break; break;
case "upper": case "upper":
$agents[$row["id_agente"]] = mb_strtoupper ($row["nombre"], "UTF-8"); $value = mb_strtoupper ($row["nombre"], "UTF-8");
break; break;
default: default:
$agents[$row["id_agente"]] = $row["nombre"]; $value = $row["nombre"];
break; break;
} }
$agents[$key] = $value;
} }
return ($agents); return ($agents);
} }