connect();
$agent = new Agent((int) $array_id[1]);
if ($agent->hasRemoteConf() === true) {
$cont++;
}
$node->disconnect();
} catch (\Exception $e) {
// Unexistent agent.
$cont = 0;
$node->disconnect();
}
} else {
try {
$agent = new Agent((int) $array_id[1]);
if ($agent->hasRemoteConf() === true) {
$cont++;
}
} catch (\Exception $e) {
// Unexistent agent.
$cont = 0;
}
}
}
echo $cont;
return;
}
if ($groups_secondary_selected === true) {
$groups = get_parameter('groups', []);
$groups_selected = get_parameter('groups_selected', []);
$user_groups = users_get_groups($config['user'], 'AR', false);
$ret = [];
foreach ($user_groups as $id_gr => $name_group) {
if (in_array($id_gr, $groups) === false) {
$ret[$id_gr] = $name_group;
}
}
echo json_encode($ret);
return;
}
}
$update_agents = get_parameter('update_agents', 0);
$recursion = get_parameter('recursion');
if ($update_agents) {
$values = [];
if ((int) get_parameter('group', '') !== -1) {
$values['id_grupo'] = get_parameter('group');
}
if (!(get_parameter('interval_select') === -1
&& empty(get_parameter('interval_text')))
) {
if (get_parameter('interval') != -2) {
$values['intervalo'] = get_parameter('interval');
}
}
if (get_parameter('id_os', '') != -1) {
$values['id_os'] = get_parameter('id_os');
}
if (get_parameter('id_parent', '') != '') {
$values['id_parent'] = get_parameter('id_agent_parent', 0);
}
if (get_parameter('server_name', '') != -1) {
$values['server_name'] = get_parameter('server_name');
}
if (get_parameter('description', '') != '') {
$values['comentarios'] = get_parameter('description');
}
if (get_parameter('mode', '') != -1) {
$values['modo'] = get_parameter('mode');
}
if (get_parameter('disabled', '') != -1) {
$values['disabled'] = get_parameter('disabled');
}
if (get_parameter('icon_path', '') != '') {
$values['icon_path'] = get_parameter('icon_path');
}
if (get_parameter('update_gis_data', -1) != -1) {
$values['update_gis_data'] = get_parameter('update_gis_data');
}
if (get_parameter('custom_id', '') != '') {
$values['custom_id'] = get_parameter('custom_id');
}
if (get_parameter('cascade_protection', -1) != -1) {
$values['cascade_protection'] = get_parameter('cascade_protection');
}
if (get_parameter('cascade_protection_module', -1) != -1) {
$values['cascade_protection_module'] = get_parameter('cascade_protection_module');
}
if (get_parameter('delete_conf', 0) != 0) {
$values['delete_conf'] = get_parameter('delete_conf');
}
if (get_parameter('quiet_select', -1) != -1) {
$values['quiet'] = get_parameter('quiet_select');
}
if (get_parameter('safe_mode_change', -1) == 1 && get_parameter('safe_mode_module', '') != '') {
// Get the module name.
$values['safe_mode_module'] = get_parameter('safe_mode_module');
} else if (get_parameter('safe_mode_change', -1) == 0) {
// Disabled Safe Operation Mode.
$values['safe_mode_module'] = '0';
}
$secondary_groups_added = (array) get_parameter(
'secondary_groups_added',
[]
);
$secondary_groups_removed = (array) get_parameter(
'secondary_groups_removed',
[]
);
$fields = db_get_all_fields_in_table('tagent_custom_fields');
if ($fields === false) {
$fields = [];
}
$id_agents = get_parameter('id_agents', false);
if (!$id_agents) {
ui_print_error_message(__('No agents selected'));
$id_agents = [];
} else {
if (empty($values) && empty($fields)) {
ui_print_error_message(__('No values changed'));
$id_agents = [];
}
}
$id_module_safe = [];
if (is_metaconsole() === false) {
// Get the id_agente_modulo to update the 'safe_operation_mode' field.
if (isset($values['safe_mode_module']) === true
&& ($values['safe_mode_module'] != '0')
) {
foreach ($id_agents as $id_agent) {
$id_module_safe[$id_agent] = db_get_value_filter(
'id_agente_modulo',
'tagente_modulo',
[
'id_agente' => $id_agent,
'nombre' => $values['safe_mode_module'],
]
);
}
}
}
// CONF FILE DELETION.
if (isset($values['delete_conf']) === true) {
unset($values['delete_conf']);
$n_deleted = 0;
foreach ($id_agents as $id_agent) {
$agent_md5 = md5(agents_get_name($id_agent));
@unlink(
$config['remote_config'].'/md5/'.$agent_md5.'.md5'
);
$result = @unlink(
$config['remote_config'].'/conf/'.$agent_md5.'.conf'
);
$n_deleted += (int) $result;
}
if ($n_deleted > 0) {
db_pandora_audit(
AUDIT_LOG_MASSIVE_MANAGEMENT,
'Delete conf file '.$id_agent
);
} else {
db_pandora_audit(
AUDIT_LOG_MASSIVE_MANAGEMENT,
'Try to delete conf file '.$id_agent
);
}
ui_print_result_message(
$n_deleted > 0,
__('Configuration files deleted successfully').'('.$n_deleted.')',
__('Configuration files cannot be deleted')
);
}
if (empty($values) === true
&& empty($fields) === true
) {
$id_agents = [];
}
$result = [];
foreach ($id_agents as $id_agent) {
if (is_metaconsole() === true) {
$array_id = explode('|', $id_agent);
try {
$node = new Node((int) $array_id[0]);
$node->connect();
$result[$id_agent] = edit_massive_agent(
(int) $array_id[1],
$values,
$id_module_safe,
$fields,
$secondary_groups_added,
$secondary_groups_removed
);
$agents_values = agents_get_agent((int) $array_id[1]);
$node->disconnect();
if (empty($values) === false) {
update_agents_in_metaconsole(
(int) $array_id[1],
$values,
$agents_values
);
}
} catch (\Exception $e) {
// Unexistent agent.
$result = [];
$node->disconnect();
}
} else {
try {
$result[$id_agent] = edit_massive_agent(
$id_agent,
$values,
$id_module_safe,
$fields,
$secondary_groups_added,
$secondary_groups_removed
);
} catch (\Exception $e) {
// Unexistent agent.
$result = [];
}
}
}
$ret = [];
foreach ($result as $id_agent => $item) {
if ($item['db'] !== false) {
$ret['db']['edited'] += 1;
$ret['db']['edited_agent'][] = $id_agent;
} else {
$ret['db']['failed'] += 1;
$ret['db']['failed_agent'][] = $id_agent;
}
if (isset($item['fields']) === true
&& empty($item['fields']) === false
) {
foreach ($item['fields'] as $kfield => $vfield) {
if ($vfield !== false) {
$ret['fields'][$id_agent]['edited'] += 1;
$ret['fields'][$id_agent]['edited_field'][] = $kfield;
} else {
$ret['fields'][$id_agent]['failed'] += 1;
$ret['fields'][$id_agent]['failed_field'][] = $kfield;
}
}
}
if (isset($item['secondary']) === true
&& empty($item['secondary']) === false
) {
foreach ($item['secondary'] as $type_action => $values_secondary) {
foreach ($values_secondary as $kgr => $vgr) {
if ($vgr !== false) {
$ret['secondary'][$type_action][$id_agent]['edited'] += 1;
$ret['secondary'][$type_action][$id_agent]['edited_gr'][] = $kgr;
} else {
$ret['secondary'][$type_action][$id_agent]['failed'] += 1;
$ret['secondary'][$type_action][$id_agent]['failed_gr'][] = $kgr;
}
}
}
}
}
foreach ($ret as $type => $ret_val) {
switch ($type) {
case 'db':
if (isset($ret_val['edited']) === true
&& $ret_val['edited'] > 0
) {
ui_print_success_message(
__(
'Agents updated successfully (%d)',
$ret_val['edited'],
implode(
',',
$ret_val['edited_agent']
)
)
);
}
if (isset($ret_val['failed']) === true
&& $ret_val['failed'] > 0
) {
ui_print_error_message(
__(
'Agents cannot be updated (%d), ids (%s)',
$ret_val['failed'],
implode(',', $ret_val['failed_agent'])
)
);
}
break;
case 'fields':
$str = '';
foreach ($ret_val as $kag => $vag) {
if (isset($vag['failed']) === true
&& $vag['failed'] > 0
) {
$str .= __(
'Agent ID: %s cannot be updated custom fields (%s)',
$kag,
implode(',', $vag['failed_field'])
).'
';
}
}
if (empty($str) === false) {
ui_print_error_message($str);
}
break;
case 'secondary':
$str = '';
foreach ($ret_val as $type => $values_secondary) {
foreach ($values_secondary as $kag => $vag) {
if (isset($vag['failed']) === true
&& $vag['failed'] > 0
) {
$str .= __(
'Agent ID: %s cannot be updated %s secondary groups (%s)',
$kag,
$type,
implode(',', $vag['failed_gr'])
).'
';
}
}
}
if (empty($str) === false) {
ui_print_error_message($str);
}
break;
default:
// Not posible.
break;
}
}
}
/**
* Edit massive agent.
*
* @param integer $id_agent
* @param array $values
* @param array $id_module_safe
* @param array $fields
* @param array $secondary_groups_added
* @param array $secondary_groups_removed
* @return void
*/
function edit_massive_agent(
int $id_agent,
array $values,
array $id_module_safe,
array $fields,
array $secondary_groups_added,
array $secondary_groups_removed
) {
global $config;
$result = false;
if (empty($values) === false) {
$agent = new Agent($id_agent);
$disabled_old = $agent->disabled();
foreach ($values as $key => $value) {
$agent->{$key}($value);
}
if (is_metaconsole() === false) {
// Get the id_agent_module for this agent to update the 'safe_operation_mode' field.
if (isset($values['safe_mode_module']) === true
&& ($values['safe_mode_module'] != '0')
) {
$values['safe_mode_module'] = $id_module_safe[$id_agent];
}
}
$result['db'] = $agent->save();
if (is_metaconsole() === false) {
if ($result['db'] !== false
&& (bool) $config['metaconsole_agent_cache'] === true
) {
// Force an update of the agent cache.
$agent->updateFromCache();
}
}
if ($disabled_old !== $values['disabled']) {
// Validate alerts for disabled agents.
if ($values['disabled'] == 1) {
alerts_validate_alert_agent($id_agent);
}
}
}
$info = [];
// Update Custom Fields.
if (isset($fields) === true
&& empty($fields) === false
) {
foreach ($fields as $field) {
$info[$field['id_field']] = $field['name'];
$value = get_parameter('customvalue_'.$field['id_field']);
if (empty($value) === false) {
$key = $field['id_field'];
$old_value = db_get_all_rows_filter(
'tagent_custom_data',
[
'id_agent' => $id_agent,
'id_field' => $key,
]
);
if ($old_value === false) {
// Create custom field if not exist.
$result['fields'][$field['id_field']] = db_process_sql_insert(
'tagent_custom_data',
[
'id_field' => $key,
'id_agent' => $id_agent,
'description' => $value,
]
);
} else {
if ($old_value[0]['description'] !== $value) {
$result['fields'][$field['id_field']] = db_process_sql_update(
'tagent_custom_data',
['description' => $value],
[
'id_field' => $key,
'id_agent' => $id_agent,
]
);
}
}
}
}
}
// Create or Remove the secondary groups.
if (empty($secondary_groups_added) === false
|| empty($secondary_groups_removed) === false
) {
$result['secondary'] = enterprise_hook(
'agents_update_secondary_groups',
[
$id_agent,
$secondary_groups_added,
$secondary_groups_removed,
true,
]
);
}
if ($result['db'] !== false) {
db_pandora_audit(
AUDIT_LOG_MASSIVE_MANAGEMENT,
'Update agent '.$id_agent,
false,
false,
json_encode($info)
);
} else {
if (isset($id_agent) === true) {
db_pandora_audit(
AUDIT_LOG_MASSIVE_MANAGEMENT,
'Try to update agent '.$id_agent,
false,
false,
json_encode($info)
);
}
}
return $result;
}
$url = 'index.php?sec=gmassive&sec2=godmode/massive/massive_operations&option=edit_agents';
if (is_metaconsole() === true) {
$url = 'index.php?sec=advanced&sec2=advanced/massive_operations&tab=massive_agents&pure=0&option=edit_agents';
}
echo '