implement demo data load

This commit is contained in:
alejandro.campos@artica.es 2023-11-21 14:26:33 +01:00
parent 31e4e872c1
commit 0868c0c995
2 changed files with 1224 additions and 884 deletions

View File

@ -25,10 +25,10 @@ global $table;
check_login();
if (! check_acl($config['id_user'], 0, 'PM') && ! is_user_admin($config['id_user'])) {
if (users_is_admin() === false) {
db_pandora_audit(
AUDIT_LOG_ACL_VIOLATION,
'Trying to access Visual Setup Management'
'Trying to access demo data manager'
);
include 'general/noaccess.php';
return;
@ -40,6 +40,21 @@ html_print_input_hidden('demo_items_count', 0);
$agents_num = (int) get_parameter('agents_num', 30);
$submit_value = (string) get_parameter('update_button', '');
$def_value = 0;
if ($submit_value === '') {
$def_value = 1;
}
$enabled_items = [
'enable_cg' => (int) get_parameter('enable_cg', $def_value),
'enable_nm' => (int) get_parameter('enable_nm', $def_value),
'enable_services' => (int) get_parameter('enable_services', $def_value),
'enable_rep' => (int) get_parameter('enable_rep', $def_value),
'enable_dashboards' => (int) get_parameter('enable_dashboards', $def_value),
'enable_vc' => (int) get_parameter('enable_vc', $def_value),
];
$demo_items_count = db_get_value('count(*)', 'tdemo_data');
$demo_agents_count = db_get_value('count(*)', 'tdemo_data', 'table_name', 'tagente');
@ -105,7 +120,6 @@ $table_aux->size[1] = '50%';
if ($mode === 'advanced') {
$arraySelectIcon = [
10 => '10',
25 => '25',
30 => '30',
50 => '50',
500 => '500',
@ -163,69 +177,70 @@ if ($mode === 'advanced') {
$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',
'enable_historical',
1,
$config['enable_pass_policy_admin'],
true,
true
)
);
$table_aux->data['row4'][] = html_print_label_input_block(
__('Create services, visual console, dashboard, reports, clusters and network maps'),
__('Create custom graphs'),
html_print_checkbox_switch(
'enable_pass_policy_admin',
'enable_cg',
1,
$config['enable_pass_policy_admin'],
$enabled_items['enable_cg'],
true
)
);
$table_aux->data['row5'][] = html_print_label_input_block(
__('Generate custom/combined graphs'),
__('Create network maps'),
html_print_checkbox_switch(
'enable_pass_policy_admin',
'enable_nm',
1,
$config['enable_pass_policy_admin'],
$enabled_items['enable_nm'],
true
)
);
$table_aux->data['row6'][] = html_print_label_input_block(
__('Generate netflow demo data'),
__('Create services'),
html_print_checkbox_switch(
'enable_pass_policy_admin',
'enable_services',
1,
$config['enable_pass_policy_admin'],
$enabled_items['enable_services'],
true
)
);
$table_aux->data['row7'][] = html_print_label_input_block(
__('Generate logs for each agent'),
__('Create reports'),
html_print_checkbox_switch(
'enable_pass_policy_admin',
'enable_rep',
1,
$config['enable_pass_policy_admin'],
$enabled_items['enable_rep'],
true
)
);
$table_aux->data['row8'][] = html_print_label_input_block(
__('Generate inventory data for each agent'),
__('Create dashboards'),
html_print_checkbox_switch(
'enable_pass_policy_admin',
'enable_dashboards',
1,
$config['enable_pass_policy_admin'],
$enabled_items['enable_dashboards'],
true
)
);
$table_aux->data['row9'][] = html_print_label_input_block(
__('Generate SNMP traps for each agent'),
__('Create visual consoles'),
html_print_checkbox_switch(
'enable_pass_policy_admin',
'enable_vc',
1,
$config['enable_pass_policy_admin'],
$enabled_items['enable_vc'],
true
)
);
@ -246,16 +261,7 @@ if ($mode === 'advanced') {
)
);
?>
<script type="text/javascript">
confirmDialog({
title: "<?php echo __('Warning'); ?>",
message: "<?php echo __('Advanced editor is intended for advanced users.'); ?>",
hideCancelButton: true,
onAccept: function() {
$('#user_profile_form').submit();
}
});
</script>
<?php
}
@ -313,7 +319,6 @@ echo '</form>';
var agents_str = '<?php echo __('agents'); ?>';
var delete_demo_data_str = '<?php echo __('Delete demo data'); ?>';
$('#btn-set').show();
if (demo_agents_count > 0) {
$('#span-btn-delete-demo-data').text(delete_demo_data_str+' ('+demo_agents_count+')');
@ -325,6 +330,22 @@ echo '</form>';
var submit_value = '<?php echo $submit_value; ?>';
var mode = "<?php echo $mode; ?>";
if (mode == 'advanced'
&& submit_value != 'Create&#x20;demo&#x20;data'
&& submit_value != 'Delete&#x20;demo&#x20;data'
) {
confirmDialog({
title: "<?php echo __('Warning'); ?>",
message: "<?php echo __('Advanced editor is intended for advanced users.'); ?>",
hideCancelButton: true,
onAccept: function() {
$('#user_profile_form').submit();
}
});
}
if (submit_value == 'Create&#x20;demo&#x20;data') {
$("#table-demo-row2").show();
@ -334,6 +355,7 @@ echo '</form>';
params["action"] = "create_demo_data";
params["page"] = "include/ajax/demo_data.ajax";
params["agents_num"] = <?php echo $agents_num; ?>;
params["enabled_items"] = <?php echo json_encode($enabled_items); ?>;
jQuery.ajax({
data: params,

View File

@ -25,9 +25,18 @@ global $config;
// Login check.
check_login();
include $config['homedir'].'/include/functions_inventory.php';
include $config['homedir'].'/include/functions_custom_graphs.php';
include $config['homedir'].'/include/functions_reports.php';
if (users_is_admin() === false) {
db_pandora_audit(
AUDIT_LOG_ACL_VIOLATION,
'Trying to access demo data manager'
);
include 'general/noaccess.php';
return;
}
require $config['homedir'].'/include/functions_inventory.php';
require $config['homedir'].'/include/functions_custom_graphs.php';
require $config['homedir'].'/include/functions_reports.php';
$action = (string) get_parameter('action', null);
@ -42,6 +51,7 @@ if ($action === 'create_demo_data') {
'services',
'reports',
'dashboards',
'visual_consoles',
];
$demodata_directory = $config['homedir'].'/extras/demodata/';
@ -59,6 +69,9 @@ if ($action === 'create_demo_data') {
}
$total_agents_to_create = (int) get_parameter('agents_num', 0);
$enabled_items = get_parameter('enabled_items');
$excluded_items = (int) array_count_values($enabled_items)[0];
$total_items_count = (count($parsed_ini) - $excluded_items);
if ($total_agents_to_create > 0) {
$agents_to_create = 0;
@ -198,7 +211,7 @@ if ($action === 'create_demo_data') {
$agents_created_count[$agent_data['agent_name']]++;
// Calculate progress.
$percentage_inc = (100 / ($agents_to_create * count($parsed_ini)));
$percentage_inc = (100 / ($agents_to_create * $total_items_count));
$current_progress_val = db_get_value_filter(
'value',
'tconfig',
@ -553,6 +566,7 @@ if ($action === 'create_demo_data') {
}
}
if ((bool) $enabled_items['enable_nm'] === true) {
// Create network maps.
foreach ($parsed_ini['network_maps'] as $ini_nm_data) {
$map_data = $ini_nm_data['map_data'];
@ -678,7 +692,7 @@ if ($action === 'create_demo_data') {
}
// Calculate progress.
$percentage_inc = (100 / count($parsed_ini));
$percentage_inc = (100 / $total_items_count);
$current_progress_val = db_get_value_filter(
'value',
'tconfig',
@ -694,7 +708,9 @@ if ($action === 'create_demo_data') {
config_update_value('demo_data_load_progress', ($current_progress_val + $percentage_inc));
}
}
if ((bool) $enabled_items['enable_cg'] === true) {
// Create graphs.
foreach ($parsed_ini['graphs'] as $ini_graph_data) {
// Constant graph types.
@ -821,7 +837,7 @@ if ($action === 'create_demo_data') {
}
// Calculate progress.
$percentage_inc = (100 / count($parsed_ini));
$percentage_inc = (100 / $total_items_count);
$current_progress_val = db_get_value_filter(
'value',
'tconfig',
@ -837,7 +853,9 @@ if ($action === 'create_demo_data') {
config_update_value('demo_data_load_progress', ($current_progress_val + $percentage_inc));
}
}
if ((bool) $enabled_items['enable_rep'] === true) {
// Create reports.
foreach ($parsed_ini['reports'] as $ini_report_data) {
$report_data = $ini_report_data['report_data'];
@ -963,7 +981,7 @@ if ($action === 'create_demo_data') {
}
// Calculate progress.
$percentage_inc = (100 / count($parsed_ini));
$percentage_inc = (100 / $total_items_count);
$current_progress_val = db_get_value_filter(
'value',
'tconfig',
@ -979,7 +997,9 @@ if ($action === 'create_demo_data') {
config_update_value('demo_data_load_progress', ($current_progress_val + $percentage_inc));
}
}
if ((bool) $enabled_items['enable_services'] === true) {
// Create services.
foreach ($parsed_ini['services'] as $ini_service_data) {
$service_data = $ini_service_data['service_data'];
@ -1165,7 +1185,7 @@ if ($action === 'create_demo_data') {
}
// Calculate progress.
$percentage_inc = (100 / count($parsed_ini));
$percentage_inc = (100 / $total_items_count);
$current_progress_val = db_get_value_filter(
'value',
'tconfig',
@ -1181,7 +1201,9 @@ if ($action === 'create_demo_data') {
config_update_value('demo_data_load_progress', ($current_progress_val + $percentage_inc));
}
}
if ((bool) $enabled_items['enable_dashboards'] === true) {
// Create dashboards.
foreach ($parsed_ini['dashboards'] as $ini_data) {
$data = $ini_data['dashboard_data'];
@ -1462,7 +1484,7 @@ if ($action === 'create_demo_data') {
}
// Calculate progress.
$percentage_inc = (100 / count($parsed_ini));
$percentage_inc = (100 / $total_items_count);
$current_progress_val = db_get_value_filter(
'value',
'tconfig',
@ -1478,7 +1500,301 @@ if ($action === 'create_demo_data') {
config_update_value('demo_data_load_progress', ($current_progress_val + $percentage_inc));
}
}
if ((bool) $enabled_items['enable_vc'] === true) {
// Create visual consoles.
foreach ($parsed_ini['visual_consoles'] as $ini_data) {
$data = $ini_data['visual_console_data'];
$items = $ini_data['visual_console_items'];
// Check for mandatory fields.
if (isset($data['name']) === false
|| isset($data['group']) === false
) {
// Name and group fields must be specified for vc.
continue;
}
$id_group = get_group_or_create_demo_group($data['group']);
if ($id_group === false) {
// Group could not be created. Skip dashboard creation.
continue;
}
$insert_values = [];
$insert_values['name'] = io_safe_input($data['name']);
$insert_values['id_group'] = $id_group;
$insert_values['background'] = (isset($data['background']) === true) ? $data['background'] : 'blackabstract.jpg';
$insert_values['background_color'] = (isset($data['background_color']) === true) ? $data['background_color'] : '#ffffff';
$insert_values['width'] = (isset($data['width']) === true) ? $data['width'] : 1024;
$insert_values['height'] = (isset($data['height']) === true) ? $data['height'] : 768;
$created_id = db_process_sql_insert('tlayout', $insert_values);
if ($created_id > 0) {
// Register created item in tdemo_data.
$values = [
'item_id' => $created_id,
'table_name' => 'tlayout',
];
$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('tlayout', ['id' => $created_id]);
continue;
}
} else {
// Dashboard could not be created. Skip creation of item.
continue;
}
if (count($items) > 0) {
$item_access_idx = 1;
while (1) {
$items_array = [];
foreach ($items as $key => $value) {
$items_array[$key] = ($value[$item_access_idx] ?? null);
}
$item_access_idx++;
$test_empty_array = array_filter($items_array);
if (empty($test_empty_array) === true) {
break;
}
if (isset($items_array['type']) === false) {
// All visual console items must have a type.
continue;
}
// Map used types.
$types = [
'static_image' => 0,
'module_graph' => 1,
'custom_graph' => 1,
'value' => 2,
'label' => 4,
'icon' => 5,
];
// Get ID of item type. Skip if it does not exist.
if (isset($types[$items_array['type']]) === false) {
continue;
}
$element_values = [];
$element_values['type'] = $types[$items_array['type']];
$element_values['id_layout'] = $created_id;
if ($items_array['type'] === 'static_image') {
if (isset($items_array['image']) === false
|| is_string($items_array['image']) === false
) {
// The above fields are required for this item.
continue;
}
$element_values['image'] = $items_array['image'];
if (isset($items_array['agent_name']) === true) {
$matched_agents = agents_get_agents(['nombre' => $items_array['agent_name']], ['id_agente']);
$agent_id = $matched_agents[0]['id_agente'];
if (!($agent_id > 0)) {
continue;
}
$element_values['id_agent'] = $agent_id;
if (isset($items_array['module']) === true) {
$module_row = modules_get_agentmodule_id(io_safe_input($items_array['module']), $agent_id);
$module_id = $module_row['id_agente_modulo'];
if (!($module_id > 0)) {
continue;
}
$element_values['id_agente_modulo'] = $module_id;
}
}
if (isset($items_array['visual_console']) === true) {
$id_vc = db_get_value('id', 'tlayout', 'name', $items_array['visual_console']);
if (!($id_vc > 0)) {
continue;
}
$element_values['id_layout_linked'] = $id_vc;
}
}
if ($items_array['type'] === 'module_graph') {
if (isset($items_array['agent_name']) === true) {
$matched_agents = agents_get_agents(['nombre' => $items_array['agent_name']], ['id_agente']);
$agent_id = $matched_agents[0]['id_agente'];
if (!($agent_id > 0)) {
continue;
}
$element_values['id_agent'] = $agent_id;
if (isset($items_array['module']) === true) {
$module_row = modules_get_agentmodule_id(io_safe_input($items_array['module']), $agent_id);
$module_id = $module_row['id_agente_modulo'];
if (!($module_id > 0)) {
continue;
}
$element_values['id_agente_modulo'] = $module_id;
}
}
if (isset($items_array['interval']) === true) {
$element_values['period'] = $items_array['interval'];
}
if (isset($items_array['graph_type']) === true) {
$element_values['type_graph'] = $items_array['graph_type'];
}
}
if ($items_array['type'] === 'custom_graph') {
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'];
if ($id_custom_graph > 0) {
$element_values['id_custom_graph'] = $id_custom_graph;
} else {
continue;
}
}
if (isset($items_array['interval']) === true) {
$element_values['period'] = $items_array['interval'];
}
}
if ($items_array['type'] === 'icon') {
if (isset($items_array['image']) === false
|| is_string($items_array['image']) === false
) {
// The above fields are required for this item.
continue;
}
$element_values['image'] = $items_array['image'];
if (isset($items_array['visual_console']) === true) {
$id_vc = db_get_value('id', 'tlayout', 'name', $items_array['visual_console']);
if (!($id_vc > 0)) {
continue;
}
$element_values['id_layout_linked'] = $id_vc;
}
}
if ($items_array['type'] === 'value') {
if (isset($items_array['agent_name']) === true) {
$matched_agents = agents_get_agents(['nombre' => $items_array['agent_name']], ['id_agente']);
$agent_id = $matched_agents[0]['id_agente'];
if (!($agent_id > 0)) {
continue;
}
$element_values['id_agent'] = $agent_id;
if (isset($items_array['module']) === true) {
$module_row = modules_get_agentmodule_id(io_safe_input($items_array['module']), $agent_id);
$module_id = $module_row['id_agente_modulo'];
if (!($module_id > 0)) {
continue;
}
$element_values['id_agente_modulo'] = $module_id;
}
}
}
if (isset($items_array['label']) === true) {
$element_values['label'] = io_safe_input($items_array['label']);
}
if (isset($items_array['label_position']) === true) {
$element_values['label_position'] = $items_array['label_position'];
}
if (isset($items_array['x']) === true) {
$element_values['pos_x'] = $items_array['x'];
}
if (isset($items_array['y']) === true) {
$element_values['pos_y'] = $items_array['y'];
}
if (isset($items_array['width']) === true) {
$element_values['width'] = $items_array['width'];
}
if (isset($items_array['height']) === true) {
$element_values['height'] = $items_array['height'];
}
$id = db_process_sql_insert('tlayout_data', $element_values);
if ($id > 0) {
// Register created demo item in tdemo_data.
$values = [
'item_id' => $id,
'table_name' => 'tlayout_data',
];
$result = (bool) db_process_sql_insert('tdemo_data', $values);
if ($result === false) {
// Rollback demo item if could not be registered in tdemo_data.
db_process_sql_delete('tlayout_data', ['id' => $id]);
continue;
}
}
}
}
// Calculate progress.
$percentage_inc = (100 / $total_items_count);
$current_progress_val = db_get_value_filter(
'value',
'tconfig',
['token' => 'demo_data_load_progress'],
'AND',
false,
false
);
if ($current_progress_val === false) {
$current_progress_val = 0;
}
config_update_value('demo_data_load_progress', ($current_progress_val + $percentage_inc));
}
}
$demo_agents_count = db_get_value('count(*)', 'tdemo_data', 'table_name', 'tagente');
@ -1513,6 +1829,8 @@ if ($action === 'cleanup_demo_data') {
'tgraph_source' => 'id_gs',
'twidget_dashboard' => 'id',
'tdashboard' => 'id',
'tlayout' => 'id',
'tlayout_data' => 'id',
];
$table_id_field = $table_id_field_dict[$item['table_name']];