pandorafms/pandora_console/include/ajax/demo_data.ajax.php

481 lines
18 KiB
PHP
Raw Normal View History

2023-10-26 19:05:07 +02:00
<?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.
* ============================================================================
*/
2023-11-10 11:24:34 +01:00
global $config;
// Login check.
check_login();
2023-10-26 19:05:07 +02:00
include $config['homedir'].'/include/functions_inventory.php';
$action = (string) get_parameter('action', null);
if ($action === 'create_demo_data') {
2023-11-10 11:24:34 +01:00
config_update_value('demo_data_load_progress', 0);
2023-10-26 19:05:07 +02:00
$agents_num = (int) get_parameter('agents_num', 0);
2023-11-10 11:24:34 +01:00
2023-10-26 19:05:07 +02:00
if ($agents_num > 0) {
// Read all ini files.
$parsed_ini[] = parse_ini_file($config['homedir'].'/extras/demodata/agents/linux.ini', true, INI_SCANNER_TYPED);
2023-11-10 11:24:34 +01:00
2023-10-26 19:05:07 +02:00
$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];
}
2023-11-10 11:24:34 +01:00
// Traverse ini files and create items.
2023-10-26 19:05:07 +02:00
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;
}
2023-11-10 11:24:34 +01:00
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;
}
}
2023-10-26 19:05:07 +02:00
}
// 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]);
2023-11-10 11:24:34 +01:00
for ($i = 0; $i < $agents_to_create; $i++) {
2023-10-26 19:05:07 +02:00
$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++;
2023-11-10 11:24:34 +01:00
$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));
2023-10-26 19:05:07 +02:00
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) {
2023-11-10 11:24:34 +01:00
// Insert in tmodule_inventory.
2023-10-26 19:05:07 +02:00
$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,
];
$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++;
2023-11-10 11:24:34 +01:00
// Insert in tagent_module_inventory and tagente_datos_inventory.
2023-10-26 19:05:07 +02:00
$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
);
$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);
2023-11-10 11:24:34 +01:00
$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;
}
}
2023-10-26 19:05:07 +02:00
$inventory_data_values = [
2023-11-10 11:24:34 +01:00
'id_agent_module_inventory' => $created_module_inventory_id,
'data' => $data_str,
'utimestamp' => time(),
'timestamp' => $current_date_time,
2023-10-26 19:05:07 +02:00
];
$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;
}
}
};
}
}
}
2023-11-10 11:24:34 +01:00
$demo_agents_count = db_get_value('count(*)', 'tdemo_data', 'table_name', 'tagente');
echo json_encode(['agents_count' => $demo_agents_count]);
2023-10-26 19:05:07 +02:00
return;
}
2023-11-10 11:24:34 +01:00
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;
}
2023-10-26 19:05:07 +02:00
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.
2023-11-10 11:24:34 +01:00
$lastOctet = (int) $octets[3];
2023-10-26 19:05:07 +02:00
// Increment the last octet, and wrap around if it exceeds 255.
2023-11-10 11:24:34 +01:00
$lastOctet = (($lastOctet + 1) % 256);
2023-10-26 19:05:07 +02:00
// Assemble the next host address.
2023-11-10 11:24:34 +01:00
$nextHost = implode('.', [$octets[0], $octets[1], $octets[2], $lastOctet]);
2023-10-26 19:05:07 +02:00
2023-11-10 11:24:34 +01:00
return $nextHost.'/'.$subnet;
}