wip reports alert actions

This commit is contained in:
Daniel Barbero Martin 2021-12-01 15:15:45 +01:00
parent e3d6d52c14
commit a02c8a7988
6 changed files with 641 additions and 242 deletions

View File

@ -1702,30 +1702,12 @@ $class = 'databox filters';
<td class="bolder"><?php echo __('Agents'); ?></td> <td class="bolder"><?php echo __('Agents'); ?></td>
<td> <td>
<?php <?php
$all_agent_log = agents_get_agents(['id_grupo' => $group], ['id_agente', 'alias']); $all_agents = agents_get_agents_selected($group);
foreach ($all_agent_log as $key => $value) {
$agents2[$value['id_agente']] = $value['alias'];
}
if ((empty($agents2)) || $agents2 == -1) {
$agents = [];
}
$agents_select = [];
if (is_array($id_agents) || is_object($id_agents)) {
foreach ($id_agents as $id) {
foreach ($agents2 as $key => $a) {
if ($key == (int) $id) {
$agents_select[$key] = $key;
}
}
}
}
html_print_select( html_print_select(
$agents2, $all_agents,
'id_agents2[]', 'id_agents2[]',
$agents_select, $id_agents,
$script = '', $script = '',
'', '',
0, 0,
@ -1789,39 +1771,20 @@ $class = 'databox filters';
<?php <?php
if (empty($id_agents) === true) { if (empty($id_agents) === true) {
$all_modules = []; $all_modules = [];
$modules_select = []; $idAgentModule = [];
} else { } else {
$all_modules = db_get_all_rows_sql( $all_modules = get_modules_agents(
sprintf( $modulegroup,
'SELECT nombre as `name`, $id_agents,
id_agente_modulo as id !$selection_a_m,
FROM tagente_modulo true
WHERE id_agente IN (%s)',
implode(',', array_values($id_agents))
)
);
$all_modules = array_reduce(
$all_modules,
function ($carry, $item) {
$carry[$item['id']] = $item['name'];
return $carry;
},
[]
);
$all_modules = array_unique($all_modules);
$modules_select = get_same_modules(
array_values($id_agents),
array_values($idAgentModule)
); );
} }
html_print_select( html_print_select(
$all_modules, $all_modules,
'module[]', 'module[]',
$modules_select, $idAgentModule,
$script = '', $script = '',
'', '',
0, 0,
@ -1830,7 +1793,7 @@ $class = 'databox filters';
true, true,
'', '',
false, false,
'min-width: 500px; max-height: 100px', 'min-width: 500px; max-width: 500px; max-height: 100px',
false, false,
false, false,
false, false,
@ -4593,7 +4556,8 @@ $(document).ready (function () {
"get_modules_group_json" : 1, "get_modules_group_json" : 1,
"id_module_group" : this.value, "id_module_group" : this.value,
"id_agents" : $("#id_agents2").val(), "id_agents" : $("#id_agents2").val(),
"selection" : $("#selection_agent_module").val() "selection" : $("#selection_agent_module").val(),
"select_mode": 1
}, },
function (data, status) { function (data, status) {
$("#module").html(''); $("#module").html('');
@ -4618,7 +4582,8 @@ $(document).ready (function () {
"get_modules_group_json" : 1, "get_modules_group_json" : 1,
"selection" : $("#selection_agent_module").val(), "selection" : $("#selection_agent_module").val(),
"id_module_group" : $("#combo_modulegroup").val(), "id_module_group" : $("#combo_modulegroup").val(),
"id_agents" : $("#id_agents2").val() "id_agents" : $("#id_agents2").val(),
"select_mode": 1
}, },
function (data, status) { function (data, status) {
$("#module").html(''); $("#module").html('');
@ -4645,7 +4610,8 @@ $(document).ready (function () {
"get_modules_group_json" : 1, "get_modules_group_json" : 1,
"id_module_group" : $("#combo_modulegroup").val(), "id_module_group" : $("#combo_modulegroup").val(),
"id_agents" : $("#id_agents2").val(), "id_agents" : $("#id_agents2").val(),
"selection" : $("#selection_agent_module").val() "selection" : $("#selection_agent_module").val(),
"select_mode": 1
}, },
function (data, status) { function (data, status) {
$("#module").html(''); $("#module").html('');

View File

@ -1689,7 +1689,7 @@ switch ($action) {
true true
); );
$es['module'] = get_same_modules( $es['module'] = get_same_modules_all(
$agents_to_report, $agents_to_report,
$modules_to_report $modules_to_report
); );
@ -1721,7 +1721,7 @@ switch ($action) {
true true
); );
$es['module'] = get_same_modules( $es['module'] = get_same_modules_all(
$agents_to_report, $agents_to_report,
$modules_to_report $modules_to_report
); );
@ -2489,7 +2489,7 @@ switch ($action) {
true true
); );
$es['module'] = get_same_modules( $es['module'] = get_same_modules_all(
$agents_to_report, $agents_to_report,
$modules_to_report $modules_to_report
); );
@ -2522,7 +2522,7 @@ switch ($action) {
true true
); );
$es['module'] = get_same_modules( $es['module'] = get_same_modules_all(
$agents_to_report, $agents_to_report,
$modules_to_report $modules_to_report
); );

View File

@ -733,6 +733,63 @@ function agents_get_agents(
} }
function agents_get_agents_selected($group)
{
if (is_metaconsole() === true) {
$all = agents_get_agents(
['id_grupo' => $group],
[
'id_tagente',
'id_tmetaconsole_setup',
'id_agente',
'alias',
],
'AR',
[
'field' => 'alias',
'order' => 'ASC',
],
false,
0,
true
);
$all = array_reduce(
$all,
function ($carry, $item) {
$carry[$item['id_tmetaconsole_setup'].'|'.$item['id_tagente']] = $item['alias'];
return $carry;
},
[]
);
} else {
$all = agents_get_agents(
['id_grupo' => $group],
[
'id_agente',
'alias',
],
'AR',
[
'field' => 'alias',
'order' => 'ASC',
]
);
$all = array_reduce(
$all,
function ($carry, $item) {
$carry[$item['id_agente']] = $item['alias'];
return $carry;
},
[]
);
}
return $all;
}
/** /**
* Get all the alerts of an agent, simple and combined. * Get all the alerts of an agent, simple and combined.
* *
@ -1662,27 +1719,67 @@ function agents_get_alias_array($array_ids)
return []; return [];
} }
$sql = sprintf( if ((bool) is_metaconsole() === true) {
'SELECT id_agente as id, alias as `name` $agents = array_reduce(
FROM tagente $array_ids,
WHERE id_agente IN (%s)', function ($carry, $item) {
implode(',', $array_ids) $explode = explode('|', $item);
);
$result = db_get_all_rows_sql($sql); $carry[$explode[0]][] = $explode[1];
return $carry;
}
);
if ($result === false) {
$result = []; $result = [];
} foreach ($agents as $tserver => $id_agents) {
$sql = sprintf(
'SELECT id_tagente as id, alias as `name`
FROM tmetaconsole_agent
WHERE id_tagente IN (%s) AND id_tmetaconsole_setup = %d',
implode(',', $id_agents),
$tserver
);
$result = array_reduce( $data_server = db_get_all_rows_sql($sql);
$result,
function ($carry, $item) { if ($data_server === false) {
$carry[$item['id']] = $item['name']; $data_server = [];
return $carry; }
},
[] $data_server = array_reduce(
); $data_server,
function ($carry, $item) {
$carry[$item['id']] = $item['name'];
return $carry;
},
[]
);
$result[$tserver] = $data_server;
}
} else {
$sql = sprintf(
'SELECT id_agente as id, alias as `name`
FROM tagente
WHERE id_agente IN (%s)',
implode(',', $array_ids)
);
$result = db_get_all_rows_sql($sql);
if ($result === false) {
$result = [];
}
$result = array_reduce(
$result,
function ($carry, $item) {
$carry[$item['id']] = $item['name'];
return $carry;
},
[]
);
}
return $result; return $result;
} }

View File

@ -2887,12 +2887,18 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false)
hd(5); hd(5);
$table = 'tevento';
if (is_metaconsole() === true) {
$table = 'tmetaconsole_event';
}
$filter_date = ''; $filter_date = '';
if (isset($filters['period']) === true if (isset($filters['period']) === true
&& empty($filters['period']) === false && empty($filters['period']) === false
) { ) {
$filter_date = sprintf( $filter_date = sprintf(
'AND tevento.utimestamp > %d', 'AND %s.utimestamp > %d',
$table,
(time() - $filters['period']) (time() - $filters['period'])
); );
} }
@ -2902,7 +2908,8 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false)
&& empty($filters['group']) === false && empty($filters['group']) === false
) { ) {
$filter_group = sprintf( $filter_group = sprintf(
'AND tevento.id_grupo = %d', 'AND %s.id_grupo = %d',
$table,
$filters['group'] $filters['group']
); );
} }
@ -2911,30 +2918,98 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false)
if (isset($filters['agents']) === true if (isset($filters['agents']) === true
&& empty($filters['agents']) === false && empty($filters['agents']) === false
) { ) {
$filter_agents = sprintf( if (is_metaconsole() === true) {
'AND tevento.id_agente IN (%s)', $agents = array_reduce(
implode(',', $filters['agents']) $filters['agents'],
); function ($carry, $item) {
$explode = explode('|', $item);
$carry[$explode[0]][] = $explode[1];
return $carry;
}
);
$filter_agents .= ' AND ( ';
$i = 0;
foreach ($agents as $tserver => $agent) {
if ($i !== 0) {
$filter_agents .= ' OR ';
}
$filter_agents .= sprintf(
'( %s.id_agente IN (%s) AND %s.server_id = %d )',
$table,
implode(',', $agent),
$table,
(int) $tserver
);
$i++;
}
$filter_agents .= ' )';
} else {
$filter_agents = sprintf(
'AND %s.id_agente IN (%s)',
$table,
implode(',', $filters['agents'])
);
}
} }
$filter_modules = ''; $filter_modules = '';
if (isset($filters['modules']) === true if (isset($filters['modules']) === true
&& empty($filters['modules']) === false && empty($filters['modules']) === false
) { ) {
$filter_modules = sprintf( if (is_metaconsole() === true) {
'AND tevento.id_agentmodule IN (%s)', $modules = array_reduce(
implode(',', $filters['modules']) $filters['modules'],
); function ($carry, $item) {
$explode = explode('|', $item);
$carry[$explode[0]][] = $explode[1];
return $carry;
}
);
$filter_modules .= ' AND ( ';
$i = 0;
foreach ($modules as $tserver => $module) {
if ($i !== 0) {
$filter_modules .= ' OR ';
}
$filter_modules .= sprintf(
'( %s.id_agentmodule IN (%s) AND %s.server_id = %d )',
$table,
implode(',', $module),
$table,
(int) $tserver
);
$i++;
}
$filter_modules .= ' )';
} else {
$filter_modules = sprintf(
'AND %s.id_agentmodule IN (%s)',
$table,
implode(',', $filters['modules'])
);
}
} }
$filter_templates = ''; $filter_templates = '';
if (isset($filters['templates']) === true if (isset($filters['templates']) === true
&& empty($filters['templates']) === false && empty($filters['templates']) === false
) { ) {
$filter_templates = sprintf( if (is_metaconsole() === false) {
'AND talert_template_modules.id_alert_template IN (%s)', $filter_templates = sprintf(
implode(',', $filters['templates']) 'AND talert_template_modules.id_alert_template IN (%s)',
); implode(',', $filters['templates'])
);
}
} }
$actions_names = alerts_get_actions_names($filters['actions'], true); $actions_names = alerts_get_actions_names($filters['actions'], true);
@ -2954,12 +3029,14 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false)
} }
$filter_actions .= sprintf( $filter_actions .= sprintf(
"JSON_CONTAINS(tevento.custom_data, '\"%s\"', '\$.actions')", "JSON_CONTAINS(%s.custom_data, '\"%s\"', '\$.actions')",
$table,
io_safe_output($name_action) io_safe_output($name_action)
); );
$fields_actions[$name_action] = sprintf( $fields_actions[$name_action] = sprintf(
"SUM(JSON_CONTAINS(tevento.custom_data, '\"%s\"', '\$.actions')) as '%s'", "SUM(JSON_CONTAINS(%s.custom_data, '\"%s\"', '\$.actions')) as '%s'",
$table,
io_safe_output($name_action), io_safe_output($name_action),
io_safe_output($name_action) io_safe_output($name_action)
); );
@ -2971,7 +3048,8 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false)
} else { } else {
foreach ($actions_names as $name_action) { foreach ($actions_names as $name_action) {
$fields[] = sprintf( $fields[] = sprintf(
"SUM(JSON_CONTAINS(tevento.custom_data, '\"%s\"', '\$.actions')) as '%s'", "SUM(JSON_CONTAINS(%s.custom_data, '\"%s\"', '\$.actions')) as '%s'",
$table,
io_safe_output($name_action), io_safe_output($name_action),
io_safe_output($name_action) io_safe_output($name_action)
); );
@ -2987,35 +3065,49 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false)
} }
$names_search = []; $names_search = [];
$names_server = [];
if (isset($groupsBy['group_by']) === true) { if (isset($groupsBy['group_by']) === true) {
switch ($groupsBy['group_by']) { switch ($groupsBy['group_by']) {
case 'module': case 'module':
$fields[] = 'tevento.id_agentmodule as module'; $fields[] = $table.'.id_agentmodule as module';
$group_array[] = 'tevento.id_agentmodule'; $group_array[] = $table.'.id_agentmodule';
$names_search = modules_get_agentmodule_name_array( $names_search = modules_get_agentmodule_name_array(
array_values($filters['modules']) array_values($filters['modules'])
); );
if (is_metaconsole() === true && $total === false) {
$fields[] = $table.'.server_id as server';
$group_array[] = $table.'.server_id';
$names_server = metaconsole_get_names();
}
break; break;
case 'template': case 'template':
$fields[] = 'talert_template_modules.id_alert_template as template'; if (is_metaconsole() === false) {
$group_array[] = 'talert_template_modules.id_alert_template'; $fields[] = 'talert_template_modules.id_alert_template as template';
$names_search = alerts_get_templates_name_array( $group_array[] = 'talert_template_modules.id_alert_template';
array_values($filters['templates']) $names_search = alerts_get_templates_name_array(
); array_values($filters['templates'])
);
}
break; break;
case 'agent': case 'agent':
$fields[] = 'tevento.id_agente as agent'; $fields[] = $table.'.id_agente as agent';
$group_array[] = 'tevento.id_agente'; $group_array[] = $table.'.id_agente';
$names_search = agents_get_alias_array( $names_search = agents_get_alias_array(
array_values($filters['agents']) array_values($filters['agents'])
); );
if (is_metaconsole() === true && $total === false) {
$fields[] = $table.'.server_id as server';
$group_array[] = $table.'.server_id';
$names_server = metaconsole_get_names();
}
break; break;
case 'group': case 'group':
$fields[] = 'tevento.id_grupo as `group`'; $fields[] = $table.'.id_grupo as `group`';
$group_array[] = 'tevento.id_grupo'; $group_array[] = $table.'.id_grupo';
$names_search = users_get_groups($config['user'], 'AR', false); $names_search = users_get_groups($config['user'], 'AR', false);
break; break;
@ -3030,7 +3122,8 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false)
&& empty($groupsBy['lapse']) === false && empty($groupsBy['lapse']) === false
) { ) {
$fields[] = sprintf( $fields[] = sprintf(
'tevento.utimestamp AS Period' '%s.utimestamp AS Period',
$table
); );
$group_array[] = 'period'; $group_array[] = 'period';
} }
@ -3041,16 +3134,22 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false)
$group_by = sprintf(' GROUP BY %s', implode(", \n", $group_array)); $group_by = sprintf(' GROUP BY %s', implode(", \n", $group_array));
} }
$innerJoin = '';
if (is_metaconsole() === false) {
$innerJoin = sprintf(
'INNER JOIN talert_template_modules
ON talert_template_modules.id = %s.id_alert_am',
$table
);
}
$query = sprintf( $query = sprintf(
'SELECT 'SELECT
%s %s
FROM tevento FROM %s
INNER JOIN talert_template_modules %s
ON talert_template_modules.id = tevento.id_alert_am
INNER JOIN talert_templates
ON talert_templates.id = talert_template_modules.id_alert_template
WHERE custom_data != "" WHERE custom_data != ""
AND tevento.event_type="alert_fired" AND %s.event_type="alert_fired"
%s %s
%s %s
%s %s
@ -3059,6 +3158,9 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false)
%s %s
%s', %s',
implode(", \n", $fields), implode(", \n", $fields),
$table,
$innerJoin,
$table,
$filter_date, $filter_date,
$filter_group, $filter_group,
$filter_agents, $filter_agents,
@ -3080,10 +3182,23 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false)
$data, $data,
function ($carry, $item) use ($groupsBy) { function ($carry, $item) use ($groupsBy) {
$period = (isset($item['Period']) === true) ? (int) $item['Period'] : 0; $period = (isset($item['Period']) === true) ? (int) $item['Period'] : 0;
$grby = $item[$groupsBy['group_by']]; if (is_metaconsole() === true
unset($item['Period']); && ($groupsBy['group_by'] === 'agent'
unset($item[$groupsBy['group_by']]); || $groupsBy['group_by'] === 'module')
$carry[$period][$grby] = $item; ) {
$grby = $item[$groupsBy['group_by']];
$server = $item['server'];
unset($item['Period']);
unset($item[$groupsBy['group_by']]);
unset($item['server']);
$carry[$period][$server][$grby] = $item;
} else {
$grby = $item[$groupsBy['group_by']];
unset($item['Period']);
unset($item[$groupsBy['group_by']]);
$carry[$period][$grby] = $item;
}
return $carry; return $carry;
}, },
[] []
@ -3095,13 +3210,20 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false)
) { ) {
$tend = time(); $tend = time();
$tstart = ($tend - (int) $filters['period']); $tstart = ($tend - (int) $filters['period']);
for ($current_time = $tstart; $current_time <= $tend; ($current_time += $groupsBy['lapse'])) { for ($current_time = $tstart; $current_time < $tend; ($current_time += $groupsBy['lapse'])) {
$intervals[] = (int) $current_time; $intervals[] = (int) $current_time;
} }
} }
$first_element = reset($data); $first_element = reset($data);
$first_element = reset($first_element); $first_element = reset($first_element);
if (is_metaconsole() === true
&& ($groupsBy['group_by'] === 'agent'
|| $groupsBy['group_by'] === 'module')
) {
$first_element = reset($first_element);
}
$clone = []; $clone = [];
foreach ($first_element as $key_clone => $value_clone) { foreach ($first_element as $key_clone => $value_clone) {
$clone[$key_clone] = 0; $clone[$key_clone] = 0;
@ -3110,13 +3232,30 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false)
$result = []; $result = [];
if (empty($intervals) === true) { if (empty($intervals) === true) {
foreach ($data as $period => $array_data) { foreach ($data as $period => $array_data) {
foreach ($names_search as $id => $name) { if (is_metaconsole() === true
if (isset($array_data[$id]) === true) { && ($groupsBy['group_by'] === 'agent'
$result[$period][$id] = $array_data[$id]; || $groupsBy['group_by'] === 'module')
$result[$period][$id][$groupsBy['group_by']] = $name; ) {
} else { foreach ($names_search as $server => $names) {
$clone[$groupsBy['group_by']] = $name; foreach ($names as $id => $name) {
$result[$period][$id] = $clone; if (isset($array_data[$server][$id]) === true) {
$result[$period][$server.'|'.$id] = $array_data[$server][$id];
$result[$period][$server.'|'.$id][$groupsBy['group_by']] = $name;
} else {
$clone[$groupsBy['group_by']] = $name;
$result[$period][$server.'|'.$id] = $clone;
}
}
}
} else {
foreach ($names_search as $id => $name) {
if (isset($array_data[$id]) === true) {
$result[$period][$id] = $array_data[$id];
$result[$period][$id][$groupsBy['group_by']] = $name;
} else {
$clone[$groupsBy['group_by']] = $name;
$result[$period][$id] = $clone;
}
} }
} }
} }
@ -3125,21 +3264,54 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false)
foreach ($intervals as $interval) { foreach ($intervals as $interval) {
$start_interval = $interval; $start_interval = $interval;
$end_interval = ($interval + $period_lapse); $end_interval = ($interval + $period_lapse);
foreach ($names_search as $id => $name) {
$result[$start_interval][$id] = $clone; if (is_metaconsole() === true
$result[$start_interval][$id][$groupsBy['group_by']] = $name; && ($groupsBy['group_by'] === 'agent'
|| $groupsBy['group_by'] === 'module')
) {
foreach ($names_search as $server => $names) {
foreach ($names as $id => $name) {
$result_name = $names_server[$server].' &raquo; '.$name;
$result[$start_interval][$server.'|'.$id] = $clone;
$result[$start_interval][$server.'|'.$id][$groupsBy['group_by']] = $result_name;
}
}
} else {
foreach ($names_search as $id => $name) {
$result[$start_interval][$id] = $clone;
$result[$start_interval][$id][$groupsBy['group_by']] = $name;
}
} }
foreach ($data as $period => $array_data) { foreach ($data as $period => $array_data) {
$period_time = (int) $period; $period_time = (int) $period;
if ($start_interval < $period_time && $period_time <= $end_interval) { if ($start_interval < $period_time && $period_time <= $end_interval) {
foreach ($array_data as $id_data => $value_data) { if (is_metaconsole() === true
foreach ($value_data as $key_data => $v) { && ($groupsBy['group_by'] === 'agent'
if ($key_data !== $groupsBy['group_by']) { || $groupsBy['group_by'] === 'module')
if (isset($result[$start_interval][$id_data][$key_data])) { ) {
$result[$start_interval][$id_data][$key_data] += $v; foreach ($array_data as $server => $datas) {
} else { foreach ($datas as $id_data => $value_data) {
$result[$start_interval][$id_data][$key_data] = $v; foreach ($value_data as $key_data => $v) {
if ($key_data !== $groupsBy['group_by']) {
if (isset($result[$start_interval][$server.'|'.$id_data][$key_data])) {
$result[$start_interval][$server.'|'.$id_data][$key_data] += $v;
} else {
$result[$start_interval][$server.'|'.$id_data][$key_data] = $v;
}
}
}
}
}
} else {
foreach ($array_data as $id_data => $value_data) {
foreach ($value_data as $key_data => $v) {
if ($key_data !== $groupsBy['group_by']) {
if (isset($result[$start_interval][$id_data][$key_data])) {
$result[$start_interval][$id_data][$key_data] += $v;
} else {
$result[$start_interval][$id_data][$key_data] = $v;
}
} }
} }
} }

View File

@ -26,6 +26,8 @@
* ============================================================================ * ============================================================================
*/ */
use PandoraFMS\Enterprise\Metaconsole\Node;
// Begin. // Begin.
require_once $config['homedir'].'/include/functions_agents.php'; require_once $config['homedir'].'/include/functions_agents.php';
require_once $config['homedir'].'/include/functions_users.php'; require_once $config['homedir'].'/include/functions_users.php';
@ -1448,6 +1450,51 @@ function modules_get_agentmodule_name_array($array_ids)
return []; return [];
} }
if ((bool) is_metaconsole() === true) {
$modules = array_reduce(
$array_ids,
function ($carry, $item) {
$explode = explode('|', $item);
$carry[$explode[0]][] = $explode[1];
return $carry;
}
);
$result = [];
foreach ($modules as $tserver => $id_modules) {
if (metaconsole_connect(null, $tserver) == NOERR) {
$result_modules = modules_get_agentmodule_name_array_data(
$id_modules
);
$result[$tserver] = $result_modules;
metaconsole_restore_db();
}
}
} else {
$result = modules_get_agentmodule_name_array_data(
$array_ids
);
}
return $result;
}
/**
* Data names.
*
* @param array $array_ids Ids.
*
* @return array
*/
function modules_get_agentmodule_name_array_data($array_ids)
{
if (is_array($array_ids) === false || empty($array_ids) === true) {
return [];
}
$sql = sprintf( $sql = sprintf(
'SELECT id_agente_modulo as id, nombre as `name` 'SELECT id_agente_modulo as id, nombre as `name`
FROM tagente_modulo FROM tagente_modulo
@ -3508,6 +3555,145 @@ function modules_get_agentmodule_mininterval_no_async($id_agent)
} }
function get_modules_agents($id_module_group, $id_agents, $selection, $select_mode=true)
{
if ((bool) is_metaconsole() === true) {
if ($select_mode === true) {
$agents = array_reduce(
$id_agents,
function ($carry, $item) {
$explode = explode('|', $item);
$carry[$explode[0]][] = $explode[1];
return $carry;
}
);
} else {
if (count($id_agents) > 0) {
$rows = db_get_all_rows_sql(
sprintf(
'SELECT `id_agente`, `id_tagente`, `id_tmetaconsole_setup`
FROM `tmetaconsole_agent`
WHERE `id_agente` IN (%s)',
implode(',', $id_agents)
)
);
} else {
$rows = [];
}
$agents = array_reduce(
$rows,
function ($carry, $item) {
if ($carry[$item['id_tmetaconsole_setup']] === null) {
$carry[$item['id_tmetaconsole_setup']] = [];
}
$carry[$item['id_tmetaconsole_setup']][] = $item['id_tagente'];
return $carry;
},
[]
);
}
$modules = [];
foreach ($agents as $tserver => $id_agents) {
if (metaconsole_connect(null, $tserver) == NOERR) {
$modules[$tserver] = select_modules_for_agent_group(
$id_module_group,
$id_agents,
$selection,
false,
false,
true
);
metaconsole_restore_db();
}
}
if (!$selection) {
// Common modules.
$final_modules = [];
$nodes_consulted = count($modules);
foreach ($modules as $tserver => $mods) {
foreach ($mods as $module) {
if ($final_modules[$module['nombre']] === null) {
$final_modules[$module['nombre']] = 0;
}
$final_modules[$module['nombre']]++;
}
}
$modules = [];
foreach ($final_modules as $module_name => $occurrences) {
if ($occurrences === $nodes_consulted) {
// Module already present in ALL nodes.
$modules[] = [
'id_agente_modulo' => $module_name,
'nombre' => $module_name,
];
}
}
} else {
// All modules.
$return = [];
$nodes = [];
foreach ($agents as $tserver => $id_agents) {
try {
$nodes[$tserver] = new Node($tserver);
} catch (Exception $e) {
hd($e);
}
$return = array_reduce(
$modules[$tserver],
function ($carry, $item) use ($tserver, $nodes) {
$t = [];
foreach ($item as $k => $v) {
$t[$k] = $v;
}
$t['id_node'] = $tserver;
if ($nodes[$tserver] !== null) {
$t['nombre'] = io_safe_output(
$nodes[$tserver]->server_name().' &raquo; '.$t['nombre']
);
}
$carry[] = $t;
return $carry;
},
$return
);
}
$modules = $return;
}
$modules = array_reduce(
$modules,
function ($carry, $item) {
$carry[$item['id_node'].'|'.$item['id_agente_modulo']] = $item['nombre'];
return $carry;
},
[]
);
} else {
$modules = select_modules_for_agent_group(
$id_module_group,
$id_agents,
$selection,
false
);
}
return $modules;
}
/** /**
* List all modules in agents selection. * List all modules in agents selection.
* *
@ -3522,7 +3708,9 @@ function get_same_modules($agents, $modules)
return []; return [];
} }
$name_modules = modules_get_agentmodule_name_array(array_values($modules)); $name_modules = modules_get_agentmodule_name_array_data(
array_values($modules)
);
$sql = sprintf( $sql = sprintf(
'SELECT id_agente_modulo as id, 'SELECT id_agente_modulo as id,
@ -3557,6 +3745,84 @@ function get_same_modules($agents, $modules)
} }
/**
* List all modules in agents selection to metaconsole or node.
*
* @param array $agents Agents ids array.
* @param array $modules Modules ids array.
*
* @return array List modules [server|id_module, ...].
*/
function get_same_modules_all($agents, $modules, $select_mode=true)
{
if (is_array($agents) === false || empty($agents) === true) {
return [];
}
if (is_metaconsole() === true) {
$modules = array_reduce(
$modules,
function ($carry, $item) {
$explode = explode('|', $item);
$carry[$explode[0]][] = $explode[1];
return $carry;
}
);
if ($select_mode === true) {
$agents = array_reduce(
$agents,
function ($carry, $item) {
$explode = explode('|', $item);
$carry[$explode[0]][] = $explode[1];
return $carry;
}
);
} else {
$rows = db_get_all_rows_sql(
sprintf(
'SELECT `id_agente`, `id_tagente`, `id_tmetaconsole_setup`
FROM `tmetaconsole_agent`
WHERE `id_agente` IN (%s)',
implode(',', $agents)
)
);
$agents = array_reduce(
$rows,
function ($carry, $item) {
if ($carry[$item['id_tmetaconsole_setup']] === null) {
$carry[$item['id_tmetaconsole_setup']] = [];
}
$carry[$item['id_tmetaconsole_setup']][] = $item['id_tagente'];
return $carry;
},
[]
);
}
$result = [];
foreach ($agents as $tserver => $id_agents) {
if (metaconsole_connect(null, $tserver) == NOERR) {
$same_modules = get_same_modules($id_agents, $modules[$tserver]);
foreach ($same_modules as $id_module) {
$result[] = $tserver.'|'.$id_module;
}
metaconsole_restore_db();
}
}
} else {
$result = get_same_modules($agents, $modules);
}
return $result;
}
function get_hierachy_modules_tree($modules) function get_hierachy_modules_tree($modules)
{ {
$new_modules = []; $new_modules = [];

View File

@ -192,127 +192,25 @@ if (is_ajax()) {
return; return;
} }
if ($get_modules_group_json) { if ($get_modules_group_json === true) {
$id_group = (int) get_parameter('id_module_group', 0); $id_group = (int) get_parameter('id_module_group', 0);
$id_agents = get_parameter('id_agents', null); $id_agents = get_parameter('id_agents', null);
$selection = get_parameter('selection'); $selection = get_parameter('selection');
$select_mode = (bool) get_parameter('select_mode', 0);
if ($id_agents === null) { if ($id_agents === null) {
echo '[]'; echo '[]';
return; return;
} }
if ((bool) is_metaconsole() === true) { $modules = get_modules_agents(
if (count($id_agents) > 0) { $id_group,
$rows = db_get_all_rows_sql( $id_agents,
sprintf( $selection,
'SELECT `id_agente`, `id_tagente`, `id_tmetaconsole_setup` $select_mode
FROM `tmetaconsole_agent` );
WHERE `id_agente` IN (%s)', echo json_encode($modules);
implode(',', $id_agents) return;
)
);
} else {
$rows = [];
}
$agents = array_reduce(
$rows,
function ($carry, $item) {
if ($carry[$item['id_tmetaconsole_setup']] === null) {
$carry[$item['id_tmetaconsole_setup']] = [];
}
$carry[$item['id_tmetaconsole_setup']][] = $item['id_tagente'];
return $carry;
},
[]
);
$modules = [];
foreach ($agents as $tserver => $id_agents) {
if (metaconsole_connect(null, $tserver) == NOERR) {
$modules[$tserver] = select_modules_for_agent_group(
$id_group,
$id_agents,
$selection,
false,
false,
true
);
metaconsole_restore_db();
}
}
if (!$selection) {
// Common modules.
$final_modules = [];
$nodes_consulted = count($modules);
foreach ($modules as $tserver => $mods) {
foreach ($mods as $module) {
if ($final_modules[$module['nombre']] === null) {
$final_modules[$module['nombre']] = 0;
}
$final_modules[$module['nombre']]++;
}
}
$modules = [];
foreach ($final_modules as $module_name => $occurrences) {
if ($occurrences === $nodes_consulted) {
// Module already present in ALL nodes.
$modules[] = [
'id_agente_modulo' => $module_name,
'nombre' => $module_name,
];
}
}
} else {
// All modules.
$return = [];
$nodes = [];
foreach ($agents as $tserver => $id_agents) {
try {
$nodes[$tserver] = new Node($tserver);
} catch (Exception $e) {
hd($e);
}
$return = array_reduce(
$modules[$tserver],
function ($carry, $item) use ($tserver, $nodes) {
$t = [];
foreach ($item as $k => $v) {
$t[$k] = $v;
}
$t['id_node'] = $tserver;
if ($nodes[$tserver] !== null) {
$t['nombre'] = io_safe_output(
$nodes[$tserver]->server_name().' &raquo; '.$t['nombre']
);
}
$carry[] = $t;
return $carry;
},
$return
);
}
$modules = $return;
}
echo json_encode($modules);
} else {
$modules = select_modules_for_agent_group($id_group, $id_agents, $selection, false);
echo json_encode($modules);
}
} }
if ($filter_modules_group_json) { if ($filter_modules_group_json) {