implement demo data feature

This commit is contained in:
alejandro.campos@artica.es 2023-10-26 19:05:07 +02:00
parent 1f8554e8d1
commit 1fd45fb95f
7 changed files with 734 additions and 2 deletions

View File

@ -0,0 +1,10 @@
START TRANSACTION;
CREATE TABLE IF NOT EXISTS `tdemo_data` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`item_id` INT UNSIGNED NULL DEFAULT NULL,
`table_name` VARCHAR(64) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=UTF8MB4;
COMMIT;

View File

@ -506,7 +506,12 @@ if ($access_console_node === true) {
enterprise_hook('skins_submenu');
$sub['godmode/setup/demo']['text'] = __('Demo');
$sub['godmode/setup/demo']['id'] = 'demo';
$menu_godmode['gsetup']['sub'] = $sub;
}
}

View File

@ -0,0 +1,299 @@
<?php
/**
* Enterprise Main Setup.
*
* @category Setup
* @package Pandora FMS
* @subpackage Enterprise
* @version 1.0.0
* @license See below
*
* ______ ___ _______ _______ ________
* | __ \.-----.--.--.--| |.-----.----.-----. | ___| | | __|
* | __/| _ | | _ || _ | _| _ | | ___| |__ |
* |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
*
* ============================================================================
* Copyright (c) 2007-2023 Pandora FMS, http://www.pandorafms.com
* This code is NOT free software. This code is NOT licenced under GPL2 licence
* You cannnot redistribute it without written permission of copyright holder.
* ============================================================================
*/
global $config;
global $table;
check_login();
if (! check_acl($config['id_user'], 0, 'PM') && ! is_user_admin($config['id_user'])) {
db_pandora_audit(
AUDIT_LOG_ACL_VIOLATION,
'Trying to access Visual Setup Management'
);
include 'general/noaccess.php';
return;
}
// Basic/Advanced mode.
$mode = (string) get_parameter('mode', 'basic');
$buttons = [];
// Draws header.
$buttons['basic'] = [
'active' => false,
'text' => '<a href="'.ui_get_full_url('index.php?sec=gsetup&sec2=godmode/setup/demo&amp;mode=basic').'">'.html_print_image(
'images/setup.png',
true,
[
'title' => __('General'),
'class' => 'invert_filter',
]
).'</a>',
];
$buttons['advanced'] = [
'active' => false,
'text' => '<a href="'.ui_get_full_url('index.php?sec=gsetup&sec2=godmode/setup/demo&amp;mode=advanced').'">'.html_print_image(
'images/key.png',
true,
[
'title' => __('Advanced'),
'class' => 'invert_filter',
]
).'</a>',
];
// Header.
ui_print_standard_header(
__('Demo data'),
'images/custom_field.png',
false,
'',
true,
$buttons,
[
[
'link' => '',
'label' => __('Setup'),
],
[
'link' => '',
'label' => __('Demo data'),
],
]
);
$table_aux = new stdClass();
$table_aux->id = 'table-password-policy';
$table_aux->class = 'filter-table-adv';
$table_aux->width = '100%';
$table_aux->data = [];
$table_aux->size = [];
$table_aux->size[0] = '50%';
$table_aux->size[1] = '50%';
if ($mode === 'advanced') {
$arraySelectIcon = [
10 => '10',
25 => '25',
30 => '30',
50 => '50',
500 => '500',
1000 => '1000',
2000 => '2000',
];
} else {
$arraySelectIcon = [
10 => '10',
30 => '30',
50 => '50',
];
}
$agent_num = (int) get_parameter('agents_num');
$otherData = [];
$table_aux->data['row1'][] = html_print_label_input_block(
__('Agents'),
html_print_div(
[
'class' => '',
'content' => html_print_select(
$arraySelectIcon,
'agents_num',
$config['gis_default_icon'],
'',
'',
'',
true,
false,
true,
'w80px'
).'&nbsp&nbsp<span class="italic_a">'.__('(%d demo agents currently in the system)').'</span>',
],
true
)
);
if ($mode === 'advanced') {
$table_aux->data['row2'][] = html_print_label_input_block(
__('Generate historical data for all agents (15 days by default)'),
html_print_checkbox_switch(
'enable_pass_policy_admin',
1,
$config['enable_pass_policy_admin'],
true
)
);
$table_aux->data['row3'][] = html_print_label_input_block(
__('Create services, visual console, dashboard, reports, clusters and network maps'),
html_print_checkbox_switch(
'enable_pass_policy_admin',
1,
$config['enable_pass_policy_admin'],
true
)
);
$table_aux->data['row4'][] = html_print_label_input_block(
__('Generate custom/combined graphs'),
html_print_checkbox_switch(
'enable_pass_policy_admin',
1,
$config['enable_pass_policy_admin'],
true
)
);
$table_aux->data['row5'][] = html_print_label_input_block(
__('Generate netflow demo data'),
html_print_checkbox_switch(
'enable_pass_policy_admin',
1,
$config['enable_pass_policy_admin'],
true
)
);
$table_aux->data['row6'][] = html_print_label_input_block(
__('Generate logs for each agent'),
html_print_checkbox_switch(
'enable_pass_policy_admin',
1,
$config['enable_pass_policy_admin'],
true
)
);
$table_aux->data['row7'][] = html_print_label_input_block(
__('Generate inventory data for each agent'),
html_print_checkbox_switch(
'enable_pass_policy_admin',
1,
$config['enable_pass_policy_admin'],
true
)
);
$table_aux->data['row8'][] = html_print_label_input_block(
__('Generate SNMP traps for each agent'),
html_print_checkbox_switch(
'enable_pass_policy_admin',
1,
$config['enable_pass_policy_admin'],
true
)
);
$table_aux->data['row9'][] = html_print_label_input_block(
__('Days of historical data to insert in the agent data'),
html_print_input_text(
'days_hist_data',
15,
'',
10,
20,
true,
false,
false,
'',
'w80px'
)
);
?>
<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
}
echo '<form class="max_floating_element_size" id="form_setup" method="post">';
echo '<fieldset>';
echo '<legend>'.__('Configure demo data').'</legend>';
html_print_input_hidden('update_config', 1);
html_print_table($table_aux);
echo '</fieldset>';
$actionButtons = [];
$actionButtons[] = html_print_submit_button(
__('Create demo data'),
'update_button',
false,
[ 'icon' => 'update' ],
true
);
$actionButtons[] = html_print_button(
__('Delete demo data'),
'delete_session_users',
false,
'',
[
'icon' => 'delete',
'mode' => 'secondary',
],
true
);
html_print_action_buttons(
implode('', $actionButtons)
);
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();
// 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");
}
});
});
</script>

View File

@ -0,0 +1,386 @@
<?php
/**
* Hook in Host&Devices for CSV import.
*
* @category Wizard
* @package Pandora FMS
* @subpackage Host&Devices - CSV Import Agents
* @version 1.0.0
* @license See below
*
* ______ ___ _______ _______ ________
* | __ \.-----.--.--.--| |.-----.----.-----. | ___| | | __|
* | __/| _ | | _ || _ | _| _ | | ___| |__ |
* |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
*
* ==========================================================
* Copyright (c) 2004-2023 Pandora FMS
* This code is NOT free software. This code is NOT licenced under GPL2 licence
* You cannnot redistribute it without written permission of copyright holder.
* ============================================================================
*/
include $config['homedir'].'/include/functions_inventory.php';
$action = (string) get_parameter('action', null);
if ($action === 'create_demo_data') {
$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.
for ($i=0; $i<$ag_groups_num; $i++) {
$group_name = db_get_value_sql(
'SELECT tg.nombre
FROM tdemo_data tdd INNER JOIN tgrupo tg ON tdd.item_id = tg.id_grupo
WHERE tdd.table_name = "tgrupo"
ORDER BY tdd.id DESC'
);
if ($group_name === false) {
$group_last_id = 0;
} else {
$group_last_id = (int) explode(' ', $group_name)[2];
}
if (!($group_last_id > -1)) {
echo json_encode(['msg' => 'Demo group ID not valid']);
return;
}
$demo_group_name = 'Demo group '.($i + $group_last_id + 1);
$created_group_id = groups_create_group($demo_group_name, []);
if ($created_group_id > 0) {
$demo_group_ids[] = $created_group_id;
// Register created demo item in tdemo_data.
$values = [
'item_id' => $created_group_id,
'table_name' => 'tgrupo',
];
$result = (bool) db_process_sql_insert('tdemo_data', $values);
if ($result === false) {
// Rollback group creation.
db_process_sql_delete('tgrupo', ['id_grupo' => $created_group_id]);
}
} else {
echo json_encode(['msg' => 'Could not create demo agent group']);
return;
}
}
$agents_created_count = 0;
$group_idx = 0;
$agent_alias = db_get_value_sql('SELECT ta.alias FROM tdemo_data tdd INNER JOIN tagente ta ON tdd.item_id = ta.id_agente WHERE tdd.table_name="tagente" ORDER BY tdd.id DESC');
if ($agent_alias === false) {
$agent_last_id = 0;
} else {
$agent_last_id = (int) explode(' ', $agent_alias)[1];
}
foreach ($parsed_ini as $ini_so_data) {
$agent_data = $ini_so_data['agent_data'];
$modules_data = $ini_so_data['modules'];
$inventory = $ini_so_data['inventory'];
$inventory_values = $ini_so_data['inventory_values'];
$address_network = $agent_data['address_network'];
$os_versions = $agent_data['os_versions'];
$os_name = $agent_data['os_name'];
// Get OS id given OS name.
$id_os = db_get_value_filter('id_os', 'tconfig_os', ['name' => $os_name]);
if ($id_os === false) {
// Create OS if does not exist.
$values = ['name' => $os_name];
$id_os = (bool) db_process_sql_insert('tconfig_os', $values);
if ($id_os === false) {
continue;
}
}
// Define agents to be created per group of 10.
$agents_per_os = [
'Linux' => 5,
'Windows' => 2,
'MAC OS' => 1,
'BSD' => 1,
'Cisco' => 1,
];
$agents_to_create = ($ag_groups_num * $agents_per_os[$os_name]);
for ($i=0; $i < $agents_to_create; $i++) {
$next_ip_address = calculateNextHostAddress($address_network);
$os_version = current($os_versions);
next($os_versions);
if (current($os_versions) === false) {
reset($os_versions);
}
$agent_last_id++;
$created_agent_id = agents_create_agent(
$agent_data['agent_alias'].' '.$agent_last_id,
$demo_group_ids[$group_idx],
// Default interval.
300,
$next_ip_address,
false,
false,
$id_os,
$os_version
);
if ($created_agent_id > 0) {
// Register created demo item in tdemo_data.
$values = [
'item_id' => $created_agent_id,
'table_name' => 'tagente',
];
$result = (bool) db_process_sql_insert('tdemo_data', $values);
if ($result === false) {
// Rollback agent creation if could not be registered in tdemo_data.
db_process_sql_delete('tagente', ['id_agente' => $created_agent_id]);
continue;
}
}
$agents_created_count++;
if (($agents_created_count % 10) === 0) {
$group_idx++;
}
// Create agent modules.
$module_access_idx = 1;
while (1) {
$modules_array = [];
foreach ($modules_data as $key => $value) {
$modules_array[$key] = ($value[$module_access_idx] ?? null);
}
$test_empty_array = array_filter($modules_array);
if (empty($test_empty_array) === true) {
break;
}
$id_tipo = db_get_value_filter('id_tipo', 'ttipo_modulo', ['nombre' => $modules_array['type']]);
$values = [
'unit' => $modules_array['unit'],
'descripcion' => $modules_array['description'],
'id_tipo_modulo' => $id_tipo,
'id_module_group' => ($modules_array['group'] ?? 0),
];
$created_mod_id = modules_create_agent_module(
$created_agent_id,
io_safe_input($modules_array['name']),
$values
);
if ($created_mod_id > 0) {
// Register created demo item in tdemo_data.
$values = [
'item_id' => $created_mod_id,
'table_name' => 'tagente_modulo',
];
$result = (bool) db_process_sql_insert('tdemo_data', $values);
if ($result === false) {
// Rollback agent module creation if could not be registered in tdemo_data.
db_process_sql_delete('tagente_modulo', ['id_agente_modulo' => $created_mod_id]);
continue;
}
// Insert module data.
$parsed = explode(';', $modules_array['values']);
if ((string) $parsed[0] === 'RANDOM') {
$data = rand($parsed[1], $parsed[2]);
} else if ((string) $parsed[0] === 'PROC') {
$probability = (int) $parsed[1];
$data = 0;
if ($probability > 0) {
$randomNumber = rand(1, 100);
if ($randomNumber <= $probability) {
$data = 1;
}
}
}
$agent_data_values = [
'id_agente_modulo' => $created_mod_id,
'datos' => $data,
'utimestamp' => time(),
];
db_process_sql_insert('tagente_datos', $agent_data_values);
}
$module_access_idx++;
};
// Create inventory modules.
$module_access_idx = 1;
$date_time = new DateTime();
$current_date_time = $date_time->format('Y-m-d H:i:s');
while (1) {
$modules_array = [];
foreach ($inventory as $key => $value) {
$modules_array[$key] = ($value[$module_access_idx] ?? null);
}
$test_empty_array = array_filter($modules_array);
if (empty($test_empty_array) === true) {
break;
}
$values = [
'name' => $modules_array['name'],
'data_format' => $modules_array['format'],
'id_os' => 1,
];
// STEP 1: tmodule_inventory.
$created_inventory_mod_id = inventory_create_inventory_module($values);
if ($created_inventory_mod_id > 0) {
// Register created demo item in tdemo_data.
$values = [
'item_id' => $created_inventory_mod_id,
'table_name' => 'tmodule_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('tmodule_inventory', ['id_module_inventory' => $created_inventory_mod_id]);
continue;
}
}
$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).
$field_idx = 1;
$values_array = explode(';', $modules_array['values']);
$selected_inventory_values = array_filter(
$inventory_values,
function ($key) use ($values_array) {
return in_array($key, $values_array);
},
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);
if (empty(array_filter($line_values)) === true) {
break;
}
$data_lines[] = implode(';', $line_values);
$field_idx++;
}
$data_str = implode('\n', $data_lines);
hd("DATA STR",true);
hd($data_str, true);
$inventory_data_values = [
'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.
$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;
}
}
};
}
}
}
return;
}
function calculateNextHostAddress($ip) {
list($network, $subnet) = explode('/', $ip);
// Convert the network address to an array of octets.
$octets = explode('.', $network);
// Convert the last octet to an integer.
$lastOctet = (int)$octets[3];
// Increment the last octet, and wrap around if it exceeds 255.
$lastOctet = ($lastOctet + 1) % 256;
// Assemble the next host address.
$nextHost = implode('.', array($octets[0], $octets[1], $octets[2], $lastOctet));
return $nextHost . '/' . $subnet;
}

View File

@ -581,6 +581,7 @@ class WelcomeWindow extends Wizard
],
];
$fields['load_demo_data'] = __('Load demo data');
$fields['wizard_agent'] = __('Agent installation wizard');
$fields['check_web'] = __('Create WEB monitoring');
$fields['check_connectivity'] = __('Create network monitoring');
@ -1112,6 +1113,9 @@ class WelcomeWindow extends Wizard
alert("<?php echo __('You must chose an option'); ?>");
} else {
switch($('#task_to_perform :selected').val()) {
case 'load_demo_data':
loadDemoDataPage();
break;
case 'wizard_agent':
deployAgent();
break;
@ -1152,6 +1156,11 @@ class WelcomeWindow extends Wizard
window.location = '<?php echo ui_get_full_url('index.php?sec=gagente&sec2=godmode/agentes/modificar_agente&show_deploy_agent=1'); ?>';
}
// Task to do actions.
function loadDemoDataPage() {
window.location = '<?php echo ui_get_full_url('index.php?sec=gagente&sec2=godmode/agentes/modificar_agente&show_deploy_agent=1'); ?>';
}
function openCreateModulesDialog() {
$('#dialog_goliat').dialog({
title: '<?php echo __('Create WEB monitoring'); ?>',

View File

@ -207,6 +207,8 @@ function agents_get_next_contact_time_left(int $id_agente)
* @param string $ip_address Agent IP.
* @param mixed $values Other tagente fields.
* @param boolean $alias_as_name True to not assign an alias as name.
* @param mixed $os OS ID.
* @param mixed $os_version OS version.
*
* @return integer New agent id if created. False if it could not be created.
*/
@ -216,7 +218,9 @@ function agents_create_agent(
$interval,
$ip_address,
$values=false,
$alias_as_name=false
$alias_as_name=false,
$os=false,
$os_version=false
) {
global $config;
@ -250,6 +254,14 @@ function agents_create_agent(
$values['direccion'] = $ip_address;
}
if (empty($os) === false) {
$values['id_os'] = $os;
}
if (empty($os_version) === false) {
$values['os_version'] = $os_version;
}
// Check if group has limit or overrides the agent limit.
if (group_allow_more_agents($id_group, true, 'create') === false) {
return false;

View File

@ -4516,4 +4516,15 @@ CREATE TABLE IF NOT EXISTS `tgraph_analytics_filter` (
`graph_modules` TEXT NULL,
`interval` INT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=UTF8MB4;
) ENGINE=InnoDB DEFAULT CHARSET=UTF8MB4;
-- ---------------------------------------------------------------------
-- Table `tdemo_data`
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `tdemo_data` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`item_id` INT UNSIGNED NULL DEFAULT NULL,
`table_name` VARCHAR(64) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=UTF8MB4;