implemented demo data load

This commit is contained in:
alejandro.campos@artica.es 2023-11-10 11:24:34 +01:00
parent 1fd45fb95f
commit 1fcc263046
2 changed files with 315 additions and 64 deletions

View File

@ -34,6 +34,14 @@ if (! check_acl($config['id_user'], 0, 'PM') && ! is_user_admin($config['id_user
return;
}
config_update_value('demo_data_load_progress', 0);
html_print_input_hidden('demo_items_count', 0);
$submit_value = (string) get_parameter('update_button', '');
$demo_items_count = db_get_value('count(*)', 'tdemo_data');
$demo_agents_count = db_get_value('count(*)', 'tdemo_data', 'table_name', 'tagente');
// Basic/Advanced mode.
$mode = (string) get_parameter('mode', 'basic');
@ -85,7 +93,7 @@ ui_print_standard_header(
);
$table_aux = new stdClass();
$table_aux->id = 'table-password-policy';
$table_aux->id = 'table-demo';
$table_aux->class = 'filter-table-adv';
$table_aux->width = '100%';
$table_aux->data = [];
@ -124,20 +132,34 @@ $table_aux->data['row1'][] = html_print_label_input_block(
'agents_num',
$config['gis_default_icon'],
'',
'',
'',
'30',
30,
true,
false,
true,
'w80px'
).'&nbsp&nbsp<span class="italic_a">'.__('(%d demo agents currently in the system)').'</span>',
).'&nbsp&nbsp<span id="agent-count-span" class="italic_a">'.__('(%d demo agents currently in the system)', $demo_agents_count).'</span>',
],
true
)
);
$table_aux->data['row2'][] = progress_bar(
0,
100,
20,
'',
0,
false,
((int) 0 !== -1) ? false : '#f3b200',
[
'class' => 'progress_bar',
'id' => 'progress_bar',
]
).html_print_input_hidden('js_timer_'.$operation['id'], 0, true);
if ($mode === 'advanced') {
$table_aux->data['row2'][] = html_print_label_input_block(
$table_aux->data['row3'][] = html_print_label_input_block(
__('Generate historical data for all agents (15 days by default)'),
html_print_checkbox_switch(
'enable_pass_policy_admin',
@ -147,7 +169,7 @@ if ($mode === 'advanced') {
)
);
$table_aux->data['row3'][] = html_print_label_input_block(
$table_aux->data['row4'][] = html_print_label_input_block(
__('Create services, visual console, dashboard, reports, clusters and network maps'),
html_print_checkbox_switch(
'enable_pass_policy_admin',
@ -157,7 +179,7 @@ if ($mode === 'advanced') {
)
);
$table_aux->data['row4'][] = html_print_label_input_block(
$table_aux->data['row5'][] = html_print_label_input_block(
__('Generate custom/combined graphs'),
html_print_checkbox_switch(
'enable_pass_policy_admin',
@ -167,7 +189,7 @@ if ($mode === 'advanced') {
)
);
$table_aux->data['row5'][] = html_print_label_input_block(
$table_aux->data['row6'][] = html_print_label_input_block(
__('Generate netflow demo data'),
html_print_checkbox_switch(
'enable_pass_policy_admin',
@ -177,7 +199,7 @@ if ($mode === 'advanced') {
)
);
$table_aux->data['row6'][] = html_print_label_input_block(
$table_aux->data['row7'][] = html_print_label_input_block(
__('Generate logs for each agent'),
html_print_checkbox_switch(
'enable_pass_policy_admin',
@ -187,7 +209,7 @@ if ($mode === 'advanced') {
)
);
$table_aux->data['row7'][] = html_print_label_input_block(
$table_aux->data['row8'][] = html_print_label_input_block(
__('Generate inventory data for each agent'),
html_print_checkbox_switch(
'enable_pass_policy_admin',
@ -197,7 +219,7 @@ if ($mode === 'advanced') {
)
);
$table_aux->data['row8'][] = html_print_label_input_block(
$table_aux->data['row9'][] = html_print_label_input_block(
__('Generate SNMP traps for each agent'),
html_print_checkbox_switch(
'enable_pass_policy_admin',
@ -207,7 +229,7 @@ if ($mode === 'advanced') {
)
);
$table_aux->data['row9'][] = html_print_label_input_block(
$table_aux->data['row10'][] = html_print_label_input_block(
__('Days of historical data to insert in the agent data'),
html_print_input_text(
'days_hist_data',
@ -250,18 +272,21 @@ $actionButtons[] = html_print_submit_button(
__('Create demo data'),
'update_button',
false,
[ 'icon' => 'update' ],
[
'icon' => 'update',
'fixed_id' => 'btn-create-demo-data',
],
true
);
$actionButtons[] = html_print_button(
$actionButtons[] = html_print_submit_button(
__('Delete demo data'),
'delete_session_users',
'update_button',
false,
'',
[
'icon' => 'delete',
'mode' => 'secondary',
'icon' => 'delete',
'mode' => 'secondary',
'fixed_id' => 'btn-delete-demo-data',
],
true
);
@ -271,29 +296,161 @@ html_print_action_buttons(
);
echo '</form>';
?>
<script type="text/javascript">
$('#form_setup').on('submit', function(e) {
e.preventDefault();
console.log("SBM");
var params = {};
params["action"] = "create_demo_data";
params["page"] = "include/ajax/demo_data.ajax";
params["agents_num"] = $('#agents_num').val();
$('#btn-delete-demo-data').hide();
$(document).ready (function () {
var demo_items_count = <?php echo $demo_items_count; ?>;
var demo_agents_count = <?php echo $demo_agents_count; ?>;
var agent_count_span_str = '<?php echo __('demo agents currently in the system'); ?>';
var agents_str = '<?php echo __('agents'); ?>';
var delete_demo_data_str = '<?php echo __('Delete demo data'); ?>';
if (demo_agents_count > 0) {
$('#span-btn-delete-demo-data').text(delete_demo_data_str+' ('+demo_agents_count+')');
$('#btn-delete-demo-data').show();
}
$("#table-demo-row2").hide();
var submit_value = '<?php echo $submit_value; ?>';
if (submit_value == 'Create&#x20;demo&#x20;data') {
$("#table-demo-row2").show();
init_progress_bar('create');
var params = {};
params["action"] = "create_demo_data";
params["page"] = "include/ajax/demo_data.ajax";
params["agents_num"] = $('#agents_num').val();
jQuery.ajax({
data: params,
type: "POST",
url: "ajax.php",
dataType: 'json',
success: function(data) {
if (data.agents_count > 0) {
$('#span-btn-delete-demo-data').text(delete_demo_data_str+' ('+data.agents_count+')');
$('#agent-count-span').text('('+(data.agents_count ?? 0)+' '+agent_count_span_str+')');
$('#btn-delete-demo-data').show();
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log("ERROR");
}
});
}
if (submit_value == 'Delete&#x20;demo&#x20;data') {
$("#table-demo-row2").show();
init_progress_bar('cleanup');
var params = {};
params["action"] = "cleanup_demo_data";
params["page"] = "include/ajax/demo_data.ajax";
jQuery.ajax({
data: params,
type: "POST",
url: "ajax.php",
success: function(data) {
$('#span-btn-delete-demo-data').text(delete_demo_data_str);
$('#agent-count-span').text('('+(data.agents_count ?? 0)+' '+agent_count_span_str+')');
$('#btn-delete-demo-data').hide();
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log("ERROR");
}
});
}
});
function demo_load_progress(id_queue, operation) {
if (id_queue == null)
return;
var src_code = $('#' + id_queue).attr("src");
/* Check stop begin */
var progress_src = null;
var elements_src = src_code.split("&");
$.each(elements_src, function (key, value) {
/* Get progress of element */
if (value.indexOf("progress=") != -1) {
var tokens_src = value.split("=");
progress_src = tokens_src[1];
}
});
/* STOP timer condition (progress >= 100) */
if (progress_src >= 100) {
clearInterval($("#hidden-js_timer_" + id_queue).val());
return;
}
var params = {};
params["action"] = "get_progress_bar";
params["operation"] = operation;
if (operation == 'cleanup') {
var demo_items_count = '<?php echo $demo_items_count; ?>';
params["demo_items_to_cleanup"] = demo_items_count;
}
params["page"] = "include/ajax/demo_data.ajax";
params["id_queue"] = id_queue;
// Browse!
jQuery.ajax({
data: params,
type: "POST",
url: "ajax.php",
success: function(data) {
console.log("SUCCESS", data);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log("ERROR");
progress_tag_pos = src_code.indexOf("progress=");
rest_pos = src_code.indexOf("&", progress_tag_pos);
pre_src = src_code.substr(0,progress_tag_pos);
post_src = src_code.substr(rest_pos);
/* Create new src code for progress bar */
new_src_code = pre_src + "progress=" + data + post_src;
if (data != '')
$('#' + id_queue).attr("src", new_src_code);
}
});
});
}
function init_progress_bar(operation) {
/* Get progress bar */
var elements = $(".progress_bar");
$.each(elements, function (key, progress_bar) {
var elements_bar = $(progress_bar).attr("src").split("&");
var current_progress = null;
$.each(elements_bar, function (key, value) {
/* Get progress */
if (value.indexOf("progress=") != -1) {
var tokens = value.split("=");
current_progress = tokens[1];
}
});
/* Get Queue id */
var id_bar = $(progress_bar).attr("id");
clearInterval($("#hidden-js_timer_" + id_bar).val());
/* Only autorefresh incomplete bars */
if (current_progress < 100) {
/* 1 seconds between ajax request */
var id_interval = setInterval("demo_load_progress('"+ id_bar +"','"+operation+"')", (1 * 1000));
/* This will keep timer info */
$("#hidden-js_timer_" + id_bar).val(id_interval);
}
});
}
</script>

View File

@ -20,17 +20,23 @@
* ============================================================================
*/
global $config;
// Login check.
check_login();
include $config['homedir'].'/include/functions_inventory.php';
$action = (string) get_parameter('action', null);
if ($action === 'create_demo_data') {
config_update_value('demo_data_load_progress', 0);
$agents_num = (int) get_parameter('agents_num', 0);
hd("qqqq ".$agents_num, true);
if ($agents_num > 0) {
// Read all ini files.
$parsed_ini[] = parse_ini_file($config['homedir'].'/extras/demodata/agents/linux.ini', true, INI_SCANNER_TYPED);
hd($parsed_ini, true);
$ag_groups_num = ($agents_num / 10);
// Create new group per group of ten agents that are to be created.
@ -88,6 +94,7 @@ hd($parsed_ini, true);
$agent_last_id = (int) explode(' ', $agent_alias)[1];
}
// Traverse ini files and create items.
foreach ($parsed_ini as $ini_so_data) {
$agent_data = $ini_so_data['agent_data'];
$modules_data = $ini_so_data['modules'];
@ -109,6 +116,21 @@ hd($parsed_ini, true);
if ($id_os === false) {
continue;
}
if ($id_os > 0) {
// Register created OS in tdemo_data.
$values = [
'item_id' => $id_os,
'table_name' => 'tconfig_os',
];
$result = (bool) db_process_sql_insert('tdemo_data', $values);
if ($result === false) {
// Rollback OS creation if could not be registered in tdemo_data.
db_process_sql_delete('tconfig_os', ['id_os' => $id_os]);
continue;
}
}
}
// Define agents to be created per group of 10.
@ -122,7 +144,7 @@ hd($parsed_ini, true);
$agents_to_create = ($ag_groups_num * $agents_per_os[$os_name]);
for ($i=0; $i < $agents_to_create; $i++) {
for ($i = 0; $i < $agents_to_create; $i++) {
$next_ip_address = calculateNextHostAddress($address_network);
$os_version = current($os_versions);
@ -163,6 +185,15 @@ hd($parsed_ini, true);
$agents_created_count++;
$percentage_inc = ((($agents_created_count * 100) / $agents_to_create) / count($parsed_ini));
$current_progress_val = db_get_value_filter('value', 'tconfig', ['token' => 'demo_data_load_progress']);
if ($current_progress_val === false) {
$current_progress_val = 0;
}
config_update_value('demo_data_load_progress', ($current_progress_val + $percentage_inc));
if (($agents_created_count % 10) === 0) {
$group_idx++;
}
@ -249,6 +280,7 @@ hd($parsed_ini, true);
$current_date_time = $date_time->format('Y-m-d H:i:s');
while (1) {
// Insert in tmodule_inventory.
$modules_array = [];
foreach ($inventory as $key => $value) {
$modules_array[$key] = ($value[$module_access_idx] ?? null);
@ -266,7 +298,6 @@ hd($parsed_ini, true);
'id_os' => 1,
];
// STEP 1: tmodule_inventory.
$created_inventory_mod_id = inventory_create_inventory_module($values);
if ($created_inventory_mod_id > 0) {
@ -286,18 +317,7 @@ hd($parsed_ini, true);
$module_access_idx++;
// STEP 2: tagent_module_inventory.
$values = [
'id_agente' => $created_agent_id,
'id_module_inventory' => $created_inventory_mod_id,
'interval' => 300,
'utimestamp' => time(),
'timestamp' => $current_date_time,
];
// STEP 3: Create inventory values (tagente_datos_inventory).
// Insert in tagent_module_inventory and tagente_datos_inventory.
$field_idx = 1;
$values_array = explode(';', $modules_array['values']);
@ -309,9 +329,6 @@ hd($parsed_ini, true);
ARRAY_FILTER_USE_KEY
);
hd("INV VALUES", true);
hd($selected_inventory_values, true);
$data_lines = [];
while (1) {
$line_values = array_column($selected_inventory_values, $field_idx);
@ -326,19 +343,40 @@ hd($parsed_ini, true);
$data_str = implode('\n', $data_lines);
hd("DATA STR",true);
hd($data_str, true);
$values = [
'id_agente' => $created_agent_id,
'id_module_inventory' => $created_inventory_mod_id,
'interval' => 300,
'utimestamp' => time(),
'timestamp' => $current_date_time,
'data' => $data_str,
];
$created_module_inventory_id = db_process_sql_insert('tagent_module_inventory', $values);
if ($created_module_inventory_id > 0) {
// Register created demo item in tdemo_data.
$values = [
'item_id' => $created_module_inventory_id,
'table_name' => 'tagent_module_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('tagent_module_inventory', ['id_agent_module_inventory' => $created_module_inventory_id]);
continue;
}
}
$inventory_data_values = [
'data' => $data_str,
'utimestamp' => time(),
'timestamp' => $current_date_time,
'id_agent_module_inventory' => $created_module_inventory_id,
'data' => $data_str,
'utimestamp' => time(),
'timestamp' => $current_date_time,
];
$created_inventory_data = db_process_sql_insert('tagente_datos_inventory', $inventory_data_values);
hd("CID",true);
hd($inventory_data_values, true);
hd($created_inventory_data, true);
if ($created_inventory_data > 0) {
// Register created inventory data element in tdemo_data.
@ -363,9 +401,65 @@ hd($parsed_ini, true);
}
}
$demo_agents_count = db_get_value('count(*)', 'tdemo_data', 'table_name', 'tagente');
echo json_encode(['agents_count' => $demo_agents_count]);
return;
}
if ($action === 'cleanup_demo_data') {
config_update_value('demo_data_load_progress', 0);
$demo_items = db_get_all_rows_in_table('tdemo_data');
foreach ($demo_items as $item) {
$table_id_field_dict = [
'tconfig_os' => 'id_os',
'tagente' => 'id_agente',
'tgrupo' => 'id_grupo',
'tagente_modulo' => 'id_agente_modulo',
'tmodule_inventory' => 'id_module_inventory',
'tagent_module_inventory' => 'id_agent_module_inventory',
'tagente_datos_inventory' => 'id_agent_module_inventory',
];
$table_id_field = $table_id_field_dict[$item['table_name']];
$result = db_process_sql_delete(
$item['table_name'],
[$table_id_field => $item['item_id']]
);
if ($result !== false) {
db_process_sql_delete(
'tdemo_data',
['item_id' => $item['item_id']]
);
}
}
echo 1;
}
if ($action === 'get_progress_bar') {
$operation = (string) get_parameter('operation');
if ($operation === 'create') {
$current_progress_val = db_get_value_filter('value', 'tconfig', ['token' => 'demo_data_load_progress']);
if ($current_progress_val === false) {
$current_progress_val = 0;
}
} else if ($operation === 'cleanup') {
$demo_items_to_cleanup = (int) get_parameter('demo_items_to_cleanup');
$count_current_demo_items = db_get_value('count(*)', 'tdemo_data');
$current_progress_val = ((($demo_items_to_cleanup - $count_current_demo_items) * 100) / $demo_items_to_cleanup);
}
echo $current_progress_val;
return;
}
function calculateNextHostAddress($ip) {
list($network, $subnet) = explode('/', $ip);
@ -374,13 +468,13 @@ function calculateNextHostAddress($ip) {
$octets = explode('.', $network);
// Convert the last octet to an integer.
$lastOctet = (int)$octets[3];
$lastOctet = (int) $octets[3];
// Increment the last octet, and wrap around if it exceeds 255.
$lastOctet = ($lastOctet + 1) % 256;
$lastOctet = (($lastOctet + 1) % 256);
// Assemble the next host address.
$nextHost = implode('.', array($octets[0], $octets[1], $octets[2], $lastOctet));
$nextHost = implode('.', [$octets[0], $octets[1], $octets[2], $lastOctet]);
return $nextHost . '/' . $subnet;
}
return $nextHost.'/'.$subnet;
}