implement demo data load

This commit is contained in:
alejandro.campos@artica.es 2023-11-29 16:57:38 +01:00
parent 6f96087d90
commit a290203847
2 changed files with 169 additions and 163 deletions

View File

@ -50,7 +50,7 @@ if ($create_data === false) {
$adv_options_is_enabled = get_parameter('toggle_adv_opts', 0);
$days_hist_data = (int) get_parameter('days_hist_data', 15);
$interval = get_parameter('interval', 300);
$service_agent_id = (int) get_parameter('service_agent_id', 0);
$service_agent_name = get_parameter('service_agent_name', 'demo-global-agent-1');
// Map directory and demo item ID.
$dir_item_id_map = [
@ -74,7 +74,7 @@ $enabled_items = [
$generate_hist = (int) get_parameter('enable_history', $def_value);
$plugin_agent = (int) get_parameter('plugin_agent', 0);
$plugin_agent = get_parameter('plugin_agent', 'demo-global-agent-1');
$traps_target_ip = get_parameter('traps_target_ip', '127.0.0.1');
$traps_community = get_parameter('traps_community', 'public');
$tentacle_target_ip = get_parameter('tentacle_target_ip', '127.0.0.1');
@ -340,22 +340,22 @@ if ($display_loading === true || $running_create === true || $running_delete) {
)
);
$params = [];
$params['return'] = true;
$params['show_helptip'] = true;
$params['print_hidden_input_idagent'] = true;
$params['hidden_input_idagent_id'] = 'hidden-service_agent_id';
$params['hidden_input_idagent_name'] = 'service_agent_id';
$params['input_name'] = 'agent_alias';
$params['value'] = '';
$params['javascript_function_action_after_select'] = 'active_button_add_agent';
$params['javascript_is_function_select'] = true;
$params['disabled_javascript_on_blur_function'] = false;
$table_adv->data['row4'][] = html_print_label_input_block(
__('Services agent name').ui_print_help_tip(__('If not set, %s will be used as the default agent', 'demo-global-agent-1'), true),
ui_print_agent_autocomplete_input($params),
['div_class' => 'w300px']
html_print_input_text(
'service_agent_name',
$service_agent_name,
'',
50,
255,
true,
false,
false,
'',
'w300px'
)
);
}
@ -409,22 +409,20 @@ if ($display_loading === true || $running_create === true || $running_delete) {
)
);
$params = [];
$params['return'] = true;
$params['show_helptip'] = true;
$params['print_hidden_input_idagent'] = true;
$params['hidden_input_idagent_id'] = 'hidden-plugin_agent';
$params['hidden_input_idagent_name'] = 'plugin_agent';
$params['input_name'] = 'plugin_agent_alias';
$params['value'] = '';
$params['javascript_function_action_after_select'] = 'active_button_add_agent';
$params['javascript_is_function_select'] = true;
$params['disabled_javascript_on_blur_function'] = false;
$table_adv->data['row10'][] = html_print_label_input_block(
__('Demo data plugin agent').ui_print_help_tip(__('If not set, %s will be used as the default agent', 'demo-global-agent-1'), true),
ui_print_agent_autocomplete_input($params),
['div_class' => 'w300px']
__('Demo data plugin agent'),
html_print_input_text(
'plugin_agent',
$plugin_agent,
'',
50,
255,
true,
false,
false,
'',
'w300px'
)
);
$table_adv->data['row11'][] = html_print_label_input_block(
@ -676,13 +674,13 @@ if ($display_loading === true || $running_create === true || $running_delete) {
var agents_str = '<?php echo __('agents'); ?>';
var display_progress_bar_cr = <?php echo (int) $running_create; ?>;
console.log("dpb ",display_progress_bar_cr);
if (display_progress_bar_cr == 1) {
init_progress_bar('create');
}
var display_progress_bar_del = <?php echo (int) $running_delete; ?>;
console.log("dpbdel ",display_progress_bar_del);
if (display_progress_bar_del == 1) {
init_progress_bar('cleanup');
}
@ -729,8 +727,6 @@ if ($display_loading === true || $running_create === true || $running_delete) {
// Creation operation must be done via AJAX in order to be able to run the operations in background
// and keep it running even if we quit the page.
if (create_data == true) {
console.log("CREATE");
init_progress_bar('create');
var params = {};
@ -747,9 +743,8 @@ if ($display_loading === true || $running_create === true || $running_delete) {
params["tentacle_target_ip"] = "<?php echo $tentacle_target_ip; ?>";
params["tentacle_port"] = <?php echo $tentacle_port; ?>;
params["tentacle_extra_options"] = "<?php echo $tentacle_extra_options; ?>";
params["service_agent_id"] = "<?php echo $service_agent_id; ?>";
console.log("params");
console.log(params);
params["service_agent_name"] = "<?php echo $service_agent_name; ?>";
jQuery.ajax({
data: params,
type: "POST",
@ -761,7 +756,6 @@ if ($display_loading === true || $running_create === true || $running_delete) {
// Delete operation must be done via AJAX in order to be able to run the operations in background
// and keep it running even if we quit the page.
if (delete_data == true) {
console.log("DELETE");
/// $("#table-demo-row2").show();
init_progress_bar('cleanup');
@ -814,8 +808,7 @@ if ($display_loading === true || $running_create === true || $running_delete) {
}
params["page"] = "include/ajax/demo_data.ajax";
params["id_queue"] = id_queue;
console.log("ANTES DE AJAX");
console.log(params);
jQuery.ajax({
data: params,
type: "POST",
@ -840,13 +833,14 @@ console.log(params);
if (operation == 'create') {
var status_data = data?.demo_data_load_status;
console.log("----> ",status_data.checked_items);
status_data.checked_items?.forEach(function(item_id) {
if (items_checked.includes(item_id)) {
return;
}
if (typeof status_data.errors[item_id] !== 'undefined'
if (typeof status_data !== 'undefined'
&& typeof status_data.errors !== 'undefined'
&& typeof status_data.errors[item_id] !== 'undefined'
&& status_data.errors[item_id].length > 0
) {
status_data.errors[item_id].forEach(function(error_msg) {
@ -905,7 +899,6 @@ console.log(params);
});
// Append the new item to the corresponding error-list ul.
console.log("qewdfefdsfs"+error_msg);
$('#load-info li[data-item-id="' + item_id + '"] .error-list').append(error_list_item);
}
</script>

View File

@ -62,17 +62,16 @@ if ($action === 'create_demo_data') {
$demodata_directory = $config['homedir'].'/extras/demodata/';
$service_agent_id = (int) get_parameter('service_agent_id', 0);
$adv_options_is_enabled = (bool) get_parameter('adv_options_is_enabled', false);
if ($adv_options_is_enabled === true) {
$service_agent_name = get_parameter('service_agent_name', 'demo-global-agent-1');
$enabled_items = get_parameter('enabled_items');
$history_is_enabled = (bool) $enabled_items['enable_history'];
$days_hist_data = (int) get_parameter('days_hist_data', 15);
$interval = (int) get_parameter('interval', 300);
// Plugin values.
$plugin_agent_id = (int) get_parameter('plugin_agent', 0);
$plugin_agent_name = get_parameter('plugin_agent', 'demo-global-agent-1');
$traps_target_ip = (string) get_parameter('traps_target_ip', '');
$traps_community = (string) get_parameter('traps_community', '');
@ -84,6 +83,9 @@ if ($action === 'create_demo_data') {
$enabled_directories[] = 'agents';
} else {
$enabled_directories = $directories;
$service_agent_name = 'demo-global-agent-1';
$plugin_agent_name = 'demo-global-agent-1';
}
if (enterprise_installed() === false) {
@ -140,7 +142,7 @@ if ($action === 'create_demo_data') {
///$iter_agents_created = 0;
// Get first server: general value for all created modules. .
$server_name = db_get_value('name' ,'tserver', 'id_server', 1);
$server_name = db_get_value('name', 'tserver', 'id_server', 1);
// Traverse agent ini files and create agents.
foreach ($parsed_ini['agents'] as $ini_agent_data) {
@ -247,9 +249,10 @@ if ($action === 'create_demo_data') {
// Create agents (remaining number of agents to reach number specified by user).
for ($i = 0; $i < min($iter_agents_to_create, $max_agents_to_limit); $i++) {
$agent_base_host_address = explode('/', $address_network)[0];
$next_ip_address = ($agents_last_ip[$agent_data['agent_alias']] !== null) ? $agents_last_ip[$agent_data['agent_alias']] : $agent_base_host_address;
$curr_ip_address = ($agents_last_ip[$agent_data['agent_alias']] !== null) ? $agents_last_ip[$agent_data['agent_alias']] : $address_network;
$next_ip_address = calculateNextHostAddress($curr_ip_address);
$host_address = explode('/', $next_ip_address)[0];
$agents_last_ip[$agent_data['agent_alias']] = $next_ip_address;
$os_version = current($os_versions);
next($os_versions);
@ -305,7 +308,7 @@ if ($action === 'create_demo_data') {
}
$agents_created_count[$agent_data['agent_alias']]++;
$agents_last_ip[$agent_data['agent_alias']] = calculateNextHostAddress($next_ip_address);
$iter_agents_created++;
// Create agent modules.
@ -409,9 +412,10 @@ if ($action === 'create_demo_data') {
$back_periods = 1;
if ($adv_options_is_enabled === true && $history_is_enabled === true) {
$back_periods = $days_hist_data;
$back_periods = round(($days_hist_data * SECONDS_1DAY) / $interval);
}
$utimestamp = time();
// Generate back_periods amounts of tagente_datos rows each period_mins minutes back in time.
@ -441,24 +445,9 @@ if ($action === 'create_demo_data') {
'utimestamp' => $utimestamp,
];
$created_data_id = db_process_sql_insert('tagente_datos', $agent_data_values);
$created_data_res = db_process_sql_insert('tagente_datos', $agent_data_values);
if ($created_data_id > 0) {
// Register created demo item in tdemo_data.
$values = [
'item_id' => $created_data_id,
'table_name' => 'tagente_datos',
];
$result = (bool) db_process_sql_insert('tdemo_data', $values);
if ($result === false) {
// Rollback demo item creation if could not be registered in tdemo_data.
db_process_sql_delete('tagente_datos', ['id_agente_modulo' => $created_data_id]);
} else {
continue;
}
} else {
if ($created_data_res === false) {
continue;
}
@ -503,8 +492,6 @@ if ($action === 'create_demo_data') {
}
if ($adv_options_is_enabled === true && $history_is_enabled === true) {
// Get 00:00 timestamp and substract 24 hours interval.
$utimestamp = strtotime(date('Y-m-d', $utimestamp));
$utimestamp -= $interval;
}
}
@ -660,26 +647,7 @@ if ($action === 'create_demo_data') {
'timestamp' => $current_date_time,
];
$created_inventory_data = db_process_sql_insert('tagente_datos_inventory', $inventory_data_values);
if ($created_inventory_data > 0) {
// Register created inventory data element in tdemo_data.
$values = [
'item_id' => $created_inventory_data,
'table_name' => 'tagente_datos_inventory',
];
$result = (bool) db_process_sql_insert('tdemo_data', $values);
if ($result === false) {
// Rollback inventory module if could not be registered in tdemo_data.
db_process_sql_delete(
'tagente_datos_inventory',
['id_agent_module_inventory' => $created_inventory_data]
);
continue;
}
}
db_process_sql_insert('tagente_datos_inventory', $inventory_data_values);
}
// Create traps.
@ -688,9 +656,10 @@ if ($action === 'create_demo_data') {
$back_periods = 1;
if ($adv_options_is_enabled === true && $history_is_enabled === true) {
$back_periods = $days_hist_data;
$back_periods = round(($days_hist_data * SECONDS_1DAY) / $interval);
}
$utimestamp = time();
for ($p = 0; $p < $back_periods; $p++) {
@ -786,12 +755,8 @@ if ($action === 'create_demo_data') {
}
if ($adv_options_is_enabled === true && $history_is_enabled === true) {
$date_time->setTime(0, 0, 0);
$date_time->sub(new DateInterval("PT{$interval}S"));
$current_date_time = $date_time->format('Y-m-d H:i:s');
// Get 00:00 timestamp and substract 24 hours interval.
$utimestamp = strtotime(date('Y-m-d', $utimestamp));
$utimestamp -= $interval;
}
}
@ -833,35 +798,33 @@ if ($action === 'create_demo_data') {
continue;
}
if ($service_agent_id === 0) {
// Check whether services default agent exists in the system. Try to get default agent if not.
$matched_agents = agents_get_agents(
['nombre' => 'demo-global-agent-1'],
['id_agente'],
'AR',
[
'field' => 'nombre',
'order' => 'ASC',
],
false,
0,
false,
false,
false
// Check whether services default agent exists in the system. Try to get default agent if not.
$matched_agents = agents_get_agents(
['nombre' => $service_agent_name],
['id_agente'],
'AR',
[
'field' => 'nombre',
'order' => 'ASC',
],
false,
0,
false,
false,
false
);
$matched_agent = $matched_agents[0]['id_agente'];
if (isset($matched_agent) === true && $matched_agent > 0) {
$service_agent_id = $matched_agent;
} else {
// Skip element creation if agent does not exist.
register_error(
DEMO_SERVICE,
__('Error in %s: the specified services agent does not exist in the system: %s. Skipping service creation', $filename, $service_agent_name)
);
$matched_agent = $matched_agents[0]['id_agente'];
if (isset($matched_agent) === true && $matched_agent > 0) {
$service_agent_id = $matched_agent;
} else {
// Skip element creation if agent does not exist.
register_error(
DEMO_SERVICE,
__('Error in %s: no agent was specified for the service and default services agent does not exist in the system: demo-global-agent-1. Skipping service creation', $filename)
);
continue;
}
continue;
}
$id_group = get_group_or_create_demo_group($service_data['group']);
@ -1085,7 +1048,7 @@ if ($action === 'create_demo_data') {
if (in_array($element_type, ['agent', 'module']) === true) {
// Get agent ID and module ID.
$matched_agents = agents_get_agents(
['nombre' => $items_array['agent_name']],
['nombre' => io_safe_input($items_array['agent_name'])],
['id_agente'],
'AR',
[
@ -1751,6 +1714,8 @@ if ($action === 'create_demo_data') {
$item_values = [];
$item_values['id_report'] = $created_report_id;
$item_values['name'] = $items_array['name'];
$item_values['type'] = $items_array['type'];
if (isset($items_array['agent_name']) === true) {
if (isset($items_array['module']) === false
@ -1812,7 +1777,7 @@ if ($action === 'create_demo_data') {
}
if (isset($items_array['graph_name']) === true && is_string($items_array['graph_name']) === true) {
$id_custom_graph = reset(custom_graphs_search('', $items_array['graph_name']))['id_graph'];
$id_custom_graph = reset(custom_graphs_search('', io_safe_input($items_array['graph_name'])))['id_graph'];
if ($id_custom_graph > 0) {
$item_values['id_gs'] = $id_custom_graph;
@ -2046,6 +2011,17 @@ if ($action === 'create_demo_data') {
}
if ($items_array['type'] === 'module_graph') {
if (isset($items_array['image']) === false
|| is_string($items_array['image']) === false
) {
// The above fields are required for this item.
register_error(
DEMO_VISUAL_CONSOLE,
__('Error in %s: image field must be specified for module_graph item type. Skipping creation of item with index %d', $filename, ($item_access_idx - 1))
);
continue;
}
if (isset($items_array['agent_name']) === true) {
$matched_agents = agents_get_agents(
['nombre' => $items_array['agent_name']],
@ -2092,6 +2068,17 @@ if ($action === 'create_demo_data') {
}
if ($items_array['type'] === 'custom_graph') {
if (isset($items_array['image']) === false
|| is_string($items_array['image']) === false
) {
// The above fields are required for this item.
register_error(
DEMO_VISUAL_CONSOLE,
__('Error in %s: image field must be specified for custom_graph item type. Skipping creation of item with index %d', $filename, ($item_access_idx - 1))
);
continue;
}
if (isset($items_array['graph_name']) === true
&& is_string($items_array['graph_name']) === true
) {
@ -2372,7 +2359,7 @@ if ($action === 'create_demo_data') {
}
// Try to get graph and skip if not exists.
$id_graph = db_get_value('id_graph', 'tgraph', 'name', $items_array['graph_name']);
$id_graph = db_get_value('id_graph', 'tgraph', 'name', io_safe_input($items_array['graph_name']));
if (!($id_graph > 0)) {
continue;
@ -2414,7 +2401,7 @@ if ($action === 'create_demo_data') {
continue;
}
$id_report = reports_get_reports(['name' => $items_array['report_name']], ['id_report'])[0]['id_report'];
$id_report = reports_get_reports(['name' => io_safe_input($items_array['report_name'])], ['id_report'])[0]['id_report'];
if (!($id_report > 0)) {
continue;
@ -2435,7 +2422,7 @@ if ($action === 'create_demo_data') {
continue;
}
$id_map = db_get_value('id', 'tmap', 'name', $items_array['map_name']);
$id_map = db_get_value('id', 'tmap', 'name', io_safe_input($items_array['map_name']));
if (!($id_map > 0)) {
continue;
@ -2483,10 +2470,10 @@ if ($action === 'create_demo_data') {
$item_height = $items_array['height'];
$position_data = [
'x' => (isset($items_array['x']) === false) ? "$item_x" : "0",
'y' => (isset($items_array['y']) === false) ? "$item_y" : "0",
'width' => (isset($items_array['width']) === false) ? "$item_width" : "4",
'height' => (isset($items_array['height']) === false) ? "$item_height" : "4",
'x' => (isset($items_array['x']) === true) ? "$item_x" : "0",
'y' => (isset($items_array['y']) === true) ? "$item_y" : "0",
'width' => (isset($items_array['width']) === true) ? "$item_width" : "4",
'height' => (isset($items_array['height']) === true) ? "$item_height" : "4",
];
$element_values = [
@ -2495,8 +2482,8 @@ if ($action === 'create_demo_data') {
'order' => $order,
'id_dashboard' => $created_id,
'id_widget' => $type_id,
'prop_width' => 0.32,
'prop_height' => 0.32,
'prop_width' => $items_array['width'],
'prop_height' => $items_array['height'],
];
$id = db_process_sql_insert('twidget_dashboard', $element_values);
@ -2527,35 +2514,34 @@ if ($action === 'create_demo_data') {
// Register plugin.
$quit = false;
if (!($plugin_agent_id > 0)) {
// Check whether plugin agent exists in the system. Try to get default agent if not.
$matched_agents = agents_get_agents(
['nombre' => 'demo-global-agent-1'],
['id_agente'],
'AR',
[
'field' => 'nombre',
'order' => 'ASC',
],
false,
0,
false,
false,
false
// Check whether plugin agent exists in the system. Try to get default agent if not.
$matched_agents = agents_get_agents(
['nombre' => $plugin_agent_name],
['id_agente'],
'AR',
[
'field' => 'nombre',
'order' => 'ASC',
],
false,
0,
false,
false,
false
);
$matched_agent = $matched_agents[0]['id_agente'];
if (isset($matched_agent) === true && $matched_agent > 0) {
$plugin_agent_id = $matched_agent;
} else {
// Skip element creation if agent does not exist.
register_error(
DEMO_PLUGIN,
__('Error in plugin creation: the specified agent for the plugin does not exist in the system: %s. Skipping plugin creation', $filename, $plugin_agent_name)
);
$matched_agent = $matched_agents[0]['id_agente'];
if (isset($matched_agent) === true && $matched_agent > 0) {
$plugin_agent_id = $matched_agent;
} else {
// Skip element creation if agent does not exist.
register_error(
DEMO_PLUGIN,
__('Error in plugin creation: no agent was specified for the plugin and default agent does not exist in the system: demo-global-agent-1. Skipping plugin creation', $filename)
);
$quit = true;
}
$quit = true;
}
if ($quit === false) {
@ -2651,6 +2637,34 @@ if ($action === 'cleanup_demo_data') {
$demo_items = db_get_all_rows_in_table('tdemo_data');
$module_items = array_filter(
$demo_items,
function ($item) {
return ($item['table_name'] === 'tagente_modulo');
}
);
$inventory_module_items = array_filter(
$demo_items,
function ($item) {
return ($item['table_name'] === 'tagent_module_inventory');
}
);
foreach ($inventory_module_items as $item) {
db_process_sql_delete(
'tagente_datos_inventory',
['id_agent_module_inventory' => $item['item_id']]
);
}
foreach ($module_items as $item) {
db_process_sql_delete(
'tagente_datos',
['id_agente_modulo' => $item['item_id']]
);
}
foreach ($demo_items as $item) {
$table_id_field_dict = [
'tconfig_os' => 'id_os',
@ -2659,7 +2673,6 @@ if ($action === 'cleanup_demo_data') {
'tagente_modulo' => 'id_agente_modulo',
'tmodule_inventory' => 'id_module_inventory',
'tagent_module_inventory' => 'id_agent_module_inventory',
'tagente_datos_inventory' => 'id_agent_module_inventory',
'tgraph' => 'id_graph',
'tmap' => 'id',
'treport' => 'id_report',
@ -2667,7 +2680,6 @@ if ($action === 'cleanup_demo_data') {
'tservice' => 'id',
'tservice_element' => 'id',
'ttrap' => 'id_trap',
'tagente_datos' => 'id_agente_modulo',
'titem' => 'id',
'tgraph_source' => 'id_gs',
'twidget_dashboard' => 'id',
@ -2676,6 +2688,7 @@ if ($action === 'cleanup_demo_data') {
'tlayout_data' => 'id',
'tagente_estado' => 'id_agente_estado',
'trel_item' => 'id',
'tplugin' => 'id',
];
$table_id_field = $table_id_field_dict[$item['table_name']];