Merge branch 'ent-9519-un-widget-tipo-agent-module-view-en-un-dashboard-falla-al-agregar-mas-modulos-del-mismo-agente' into 'develop'

fixed agent/module widget pandora_enterprise#9519

pandora_enterprise#9519

See merge request artica/pandorafms!5118
This commit is contained in:
Jimmy Olano 2022-10-13 15:26:14 +00:00 committed by Ramon Novoa
commit 164ccb0631
6 changed files with 100 additions and 161 deletions

View File

@ -57,11 +57,17 @@ function mysql_connect_db(
$port = $config['dbport'];
}
if ($ssl === null && (bool) $config['dbssl'] === true) {
if ($ssl === null
&& isset($config['dbssl']) === true
&& (bool) $config['dbssl'] === true
) {
$ssl = $config['dbsslcafile'];
}
if ($verify === null && (bool) $config['sslverifyservercert'] === true) {
if ($verify === null
&& isset($config['sslverifyservercert']) === true
&& (bool) $config['sslverifyservercert'] === true
) {
$verify = 'verified';
}

View File

@ -1664,44 +1664,31 @@ function html_print_select_multiple_modules_filtered(array $data):string
]
);
// Show common modules.
$selection = [
0 => __('Show common modules'),
1 => __('Show all modules'),
];
if (true) {
$output .= html_print_input(
[
'label' => __('Only common modules'),
'type' => 'switch',
'value' => 'checked',
'id' => 'filtered-module-show-common-modules-'.$uniqId,
'return' => true,
'onchange' => 'fmModuleChange(\''.$uniqId.'\', '.(int) is_metaconsole().')',
]
);
} else {
$output .= html_print_input(
[
'label' => __('Show common modules'),
'type' => 'select',
'fields' => $selection,
'name' => 'filtered-module-show-common-modules-'.$uniqId,
'return' => true,
'script' => 'fmModuleChange(\''.$uniqId.'\', '.(int) is_metaconsole().')',
]
);
$commonModules = 0;
if (empty($data['mShowCommonModules']) === false) {
$commonModules = 1;
}
$output .= html_print_input(
[
'label' => __('Only common modules'),
'type' => 'switch',
'checked' => $commonModules,
'value' => $commonModules,
'name' => 'filtered-module-show-common-modules-'.$uniqId,
'id' => 'filtered-module-show-common-modules-'.$uniqId,
'return' => true,
'onchange' => 'fmModuleChange(\''.$uniqId.'\', '.(int) is_metaconsole().')',
]
);
if (empty($data['mAgents']) === false
&& empty($data['mModuleGroup'] === false)
) {
$all_modules = get_modules_agents(
$data['mModuleGroup'],
explode(',', $data['mAgents']),
$data['mShowCommonModules'],
!$commonModules,
false,
true
);
@ -1709,10 +1696,18 @@ function html_print_select_multiple_modules_filtered(array $data):string
$all_modules = [];
}
$mModules = $data['mModules'];
if (is_array($data['mModules']) === false) {
$result = explode(((is_metaconsole() === true) ? SEPARATOR_META_MODULE : ','), $data['mModules']);
} else {
$result = $data['mModules'];
$mModules = explode(
',',
$data['mModules']
);
}
$result = [];
// Clean double safe input.
foreach ($mModules as $name) {
$result[] = io_safe_output($name);
}
$output .= html_print_input(
@ -4956,7 +4951,7 @@ function html_print_input($data, $wrapper='div', $input_only=false)
$output = '';
if (($data['label'] ?? false) && $input_only === false) {
$output = '<'.$wrapper.' id="'.$wrapper.'-'.$data['name'].'" ';
$output = '<'.$wrapper.' id="'.$wrapper.'-'.($data['name'] ?? '').'" ';
$output .= ' class="'.($data['input_class'] ?? '').'">';
$output .= '<label '.$style.' class="'.($data['label_class'] ?? '').'">';
$output .= ($data['label'] ?? '');
@ -4977,7 +4972,7 @@ function html_print_input($data, $wrapper='div', $input_only=false)
if (isset($data['wrapper']) === true) {
$output = '<'.$data['wrapper'].' '.$wrapper_attributes.' id="wr_'.$data['name'].'" ';
$output .= ' class="'.$data['input_class'].'">';
$output .= ' class="'.($data['input_class'] ?? '').'">';
}
switch (($data['type'] ?? null)) {
@ -5097,7 +5092,7 @@ function html_print_input($data, $wrapper='div', $input_only=false)
$output .= html_print_input_color(
$data['name'],
$data['value'],
$data['id'],
($data['id'] ?? ''),
((isset($data['class']) === true) ? $data['class'] : false),
((isset($data['return']) === true) ? $data['return'] : false)
);

View File

@ -3656,7 +3656,7 @@ function get_modules_agents($id_module_group, $id_agents, $selection, $select_mo
$id_agents,
$selection,
false,
false,
$useName,
true
);
@ -3684,8 +3684,8 @@ function get_modules_agents($id_module_group, $id_agents, $selection, $select_mo
if ($occurrences === $nodes_consulted) {
// Module already present in ALL nodes.
$modules[] = [
'id_agente_modulo' => $module_name,
'nombre' => $module_name,
'id_agente_modulo' => io_safe_output($module_name),
'nombre' => io_safe_output($module_name),
];
}
}
@ -3730,7 +3730,7 @@ function get_modules_agents($id_module_group, $id_agents, $selection, $select_mo
function ($carry, $item) use ($useName) {
// Only works in select mode.
if ($useName === true) {
$carry[io_safe_input($item['nombre'])] = $item['nombre'];
$carry[$item['id_node'].'|'.$item['nombre']] = $item['nombre'];
} else {
$carry[$item['id_node'].'|'.$item['id_agente_modulo']] = $item['nombre'];
}
@ -3744,6 +3744,8 @@ function get_modules_agents($id_module_group, $id_agents, $selection, $select_mo
$id_module_group,
$id_agents,
$selection,
false,
$useName,
false
);
}

View File

@ -338,22 +338,6 @@ class AgentModuleWidget extends Widget
)
);
if (is_metaconsole() === true) {
$values['mModules'] = implode(
SEPARATOR_META_MODULE,
array_reduce(
$values['mModules'],
function ($carry, $item) {
$d = explode('|', $item);
$carry[] = (isset($d[1]) === true) ? $d[1] : $item;
return $carry;
},
[]
)
);
}
return $values;
}
@ -670,64 +654,34 @@ class AgentModuleWidget extends Widget
return $output;
}
// Extract info all modules selected.
$target_modules = $this->values['mModules'];
if (is_metaconsole() === true) {
$target_modules = explode(
SEPARATOR_META_MODULE,
$this->values['mModules']
);
$reduceAllModules = array_reduce(
$this->values['mModules'],
function ($carry, $item) {
if ($item === null) {
return $carry;
}
$all_modules = $target_modules;
} else {
if (is_array($target_modules) === true
|| is_numeric($target_modules) === true
) {
$target_modules = array_reduce(
$target_modules,
function ($carry, $item) {
$carry[] = io_safe_output($item);
return $carry;
if (is_metaconsole() === true) {
$item = explode('|', $item);
$serverId = $item[0];
$fullname = $item[1];
if ($this->values['mShowCommonModules'] !== 'on') {
$item = explode('&#x20;&raquo;&#x20;', $fullname);
$name = $item[1];
$carry['modules_selected'][$serverId][$name] = null;
$carry['modules'][$name] = null;
} else {
$carry['modules'][$fullname] = null;
}
);
} else {
$carry['modules'][$item] = null;
}
$all_modules = Module::search(
['nombre' => $target_modules]
);
} else {
// From previous definitions.
$all_modules = Module::search(
['id_agente_modulo' => explode(',', $target_modules)]
);
return $carry;
}
}
if ($all_modules !== null) {
if (is_metaconsole() === true
&& $this->values['mShowCommonModules'] === '1'
) {
$reduceAllModules = [];
} else {
$reduceAllModules = array_reduce(
$all_modules,
function ($carry, $item) {
if ($item === null) {
return $carry;
}
if (is_object($item) === true) {
$carry[$item->name()] = null;
} else {
$carry[io_safe_output($item)] = null;
}
return $carry;
}
);
}
}
);
$allModules = $reduceAllModules['modules'];
$visualData = [];
// Extract info agents selected.
$target_agents = explode(',', $this->values['mAgents']);
@ -754,54 +708,33 @@ class AgentModuleWidget extends Widget
$visualData[$agent_id]['agent_alias'] = $agent->alias();
$visualData[$agent_id]['modules'] = [];
if (is_metaconsole() === true
&& $this->values['mShowCommonModules'] === '1'
) {
// MC should connect to nodes and retrieve information
// from targets.
$tmpModules = array_reduce(
$target_modules,
function ($carry, $item) {
// In this case, the modules come with '» ' chain.
$tmpCarry = explode('&raquo;&#x20;', $item);
$carry[trim($tmpCarry[1])] = null;
return $carry;
}
);
$modules = $agent->searchModules(
['nombre' => array_keys($tmpModules)]
);
foreach ($modules as $module) {
if ($module === null) {
$reduceAllModules[] = null;
} else {
$reduceAllModules[$module->name()] = null;
}
}
} else {
if (empty($reduceAllModules) === false) {
if (empty($allModules) === false) {
if (is_metaconsole() === true && $this->values['mShowCommonModules'] !== 'on') {
$modules = $agent->searchModules(
['nombre' => array_keys($reduceAllModules)]
['nombre' => array_keys($reduceAllModules['modules_selected'][$tserver])]
);
} else {
$modules = $agent->searchModules(
['nombre' => array_keys($allModules)]
);
}
}
$visualData[$agent_id]['modules'] = $reduceAllModules;
$visualData[$agent_id]['modules'] = $allModules;
foreach ($modules as $module) {
if ($module === null) {
continue;
}
$key_name_module = $module->name();
if ($this->values['mTypeShow'] === '1') {
$mod = $module->toArray();
$mod['datos'] = $module->lastValue();
$module_last_value = modules_get_agentmodule_data_for_humans($mod);
$visualData[$agent_id]['modules'][$module->name()] = $module_last_value;
$visualData[$agent_id]['modules'][$key_name_module] = $module_last_value;
} else {
$visualData[$agent_id]['modules'][$module->name()] = $module->getStatus()->estado();
$visualData[$agent_id]['modules'][$key_name_module] = $module->getStatus()->estado();
}
}
@ -813,17 +746,9 @@ class AgentModuleWidget extends Widget
}
}
if (empty($reduceAllModules) === false) {
$allModules = array_keys($reduceAllModules);
}
if ($allModules === null) {
$allModules = [];
}
$output = $this->generateViewAgentModule(
$visualData,
$allModules
array_keys($allModules)
);
return $output;

View File

@ -222,9 +222,16 @@ if (is_ajax()) {
$id_group,
$id_agents,
$selection,
$select_mode
$select_mode,
true
);
echo json_encode($modules);
// Clean double safe input.
foreach ($modules as $id => $name) {
$result[$id] = io_safe_output($name);
}
echo json_encode($result);
return;
}

View File

@ -503,8 +503,12 @@ sub execute_command_timeout {
if ($remaining_timeout > 0) {
# Retrieve output from child.
$output = do { local $/; <$RET> };
$output = $output>>8;
$output = do {
no warnings;
local $/;
<$RET>
};
$output = $output>>8 if looks_like_number($output);
}
else {
# Timeout expired.
@ -564,7 +568,7 @@ sub execute_command_block {
} while ((--$retries) > 0);
# Do not continue evaluating block if failed.
last unless ($err_level == 0);
last unless (looks_like_number($err_level) && $err_level == 0);
}
}
@ -599,7 +603,7 @@ sub evaluate_command {
);
# Precondition not satisfied.
return $self->report_command($ref, $err_level) unless ($err_level == 0);
return $self->report_command($ref, $err_level) unless (looks_like_number($err_level) && $err_level == 0);
# Main run.
$err_level = $self->execute_command_block(
@ -609,7 +613,7 @@ sub evaluate_command {
);
# Script not success.
return $self->report_command($ref, $err_level) unless ($err_level == 0);
return $self->report_command($ref, $err_level) unless (looks_like_number($err_level) && $err_level == 0);
# Check postconditions
$err_level = $self->execute_command_block(
@ -701,4 +705,4 @@ sub recv_file {
}
1;
1;