From 1fd45fb95fac7c963b725e2bd2fc05e5b31a009a Mon Sep 17 00:00:00 2001 From: "alejandro.campos@artica.es" Date: Thu, 26 Oct 2023 19:05:07 +0200 Subject: [PATCH 01/42] implement demo data feature --- pandora_console/extras/mr/67.sql | 10 + pandora_console/godmode/menu.php | 5 + pandora_console/godmode/setup/demo.php | 299 ++++++++++++++ .../include/ajax/demo_data.ajax.php | 386 ++++++++++++++++++ .../include/class/WelcomeWindow.class.php | 9 + pandora_console/include/functions_agents.php | 14 +- pandora_console/pandoradb.sql | 13 +- 7 files changed, 734 insertions(+), 2 deletions(-) create mode 100644 pandora_console/extras/mr/67.sql create mode 100644 pandora_console/godmode/setup/demo.php create mode 100644 pandora_console/include/ajax/demo_data.ajax.php diff --git a/pandora_console/extras/mr/67.sql b/pandora_console/extras/mr/67.sql new file mode 100644 index 0000000000..0ae98fd735 --- /dev/null +++ b/pandora_console/extras/mr/67.sql @@ -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; diff --git a/pandora_console/godmode/menu.php b/pandora_console/godmode/menu.php index 94509ab820..059d19bae9 100644 --- a/pandora_console/godmode/menu.php +++ b/pandora_console/godmode/menu.php @@ -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; + } } diff --git a/pandora_console/godmode/setup/demo.php b/pandora_console/godmode/setup/demo.php new file mode 100644 index 0000000000..9af7e2721e --- /dev/null +++ b/pandora_console/godmode/setup/demo.php @@ -0,0 +1,299 @@ + false, + 'text' => ''.html_print_image( + 'images/setup.png', + true, + [ + 'title' => __('General'), + 'class' => 'invert_filter', + ] + ).'', +]; + +$buttons['advanced'] = [ + 'active' => false, + 'text' => ''.html_print_image( + 'images/key.png', + true, + [ + 'title' => __('Advanced'), + 'class' => 'invert_filter', + ] + ).'', +]; + +// 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' + ).'  '.__('(%d demo agents currently in the system)').'', + ], + 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' + ) + ); + ?> + + '; +echo '
'; +echo ''.__('Configure demo data').''; +html_print_input_hidden('update_config', 1); +html_print_table($table_aux); +echo '
'; + +$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 ''; + +?> + + \ No newline at end of file diff --git a/pandora_console/include/ajax/demo_data.ajax.php b/pandora_console/include/ajax/demo_data.ajax.php new file mode 100644 index 0000000000..1016903e57 --- /dev/null +++ b/pandora_console/include/ajax/demo_data.ajax.php @@ -0,0 +1,386 @@ + 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; +} \ No newline at end of file diff --git a/pandora_console/include/class/WelcomeWindow.class.php b/pandora_console/include/class/WelcomeWindow.class.php index 5e66268403..cd057414d0 100644 --- a/pandora_console/include/class/WelcomeWindow.class.php +++ b/pandora_console/include/class/WelcomeWindow.class.php @@ -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(""); } 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 = ''; } + // Task to do actions. + function loadDemoDataPage() { + window.location = ''; + } + function openCreateModulesDialog() { $('#dialog_goliat').dialog({ title: '', diff --git a/pandora_console/include/functions_agents.php b/pandora_console/include/functions_agents.php index 720b0e6e55..04174569a1 100644 --- a/pandora_console/include/functions_agents.php +++ b/pandora_console/include/functions_agents.php @@ -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; diff --git a/pandora_console/pandoradb.sql b/pandora_console/pandoradb.sql index 141ab4344d..86533ed028 100644 --- a/pandora_console/pandoradb.sql +++ b/pandora_console/pandoradb.sql @@ -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; \ No newline at end of file +) 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; + From 1fcc263046aa69d254b81f3707c91d95f866734c Mon Sep 17 00:00:00 2001 From: "alejandro.campos@artica.es" Date: Fri, 10 Nov 2023 11:24:34 +0100 Subject: [PATCH 02/42] implemented demo data load --- pandora_console/godmode/setup/demo.php | 221 +++++++++++++++--- .../include/ajax/demo_data.ajax.php | 158 ++++++++++--- 2 files changed, 315 insertions(+), 64 deletions(-) diff --git a/pandora_console/godmode/setup/demo.php b/pandora_console/godmode/setup/demo.php index 9af7e2721e..d0d65c748f 100644 --- a/pandora_console/godmode/setup/demo.php +++ b/pandora_console/godmode/setup/demo.php @@ -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' - ).'  '.__('(%d demo agents currently in the system)').'', + ).'  '.__('(%d demo agents currently in the system)', $demo_agents_count).'', ], 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 ''; - ?> \ No newline at end of file diff --git a/pandora_console/include/ajax/demo_data.ajax.php b/pandora_console/include/ajax/demo_data.ajax.php index 1016903e57..c2d163054a 100644 --- a/pandora_console/include/ajax/demo_data.ajax.php +++ b/pandora_console/include/ajax/demo_data.ajax.php @@ -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; -} \ No newline at end of file + return $nextHost.'/'.$subnet; +} From d6bffbade18f638b124fa62cf115b467f430425b Mon Sep 17 00:00:00 2001 From: "alejandro.campos@artica.es" Date: Mon, 20 Nov 2023 16:52:49 +0100 Subject: [PATCH 03/42] implement demo data load --- pandora_console/godmode/menu.php | 2 +- pandora_console/godmode/setup/demo.php | 21 +- .../include/ajax/demo_data.ajax.php | 1471 +++++++++++++---- .../include/class/WelcomeWindow.class.php | 2 +- pandora_console/include/db/mysql.php | 10 +- pandora_console/include/db/oracle.php | 4 +- pandora_console/include/db/postgresql.php | 4 +- pandora_console/include/functions_db.php | 8 +- 8 files changed, 1185 insertions(+), 337 deletions(-) diff --git a/pandora_console/godmode/menu.php b/pandora_console/godmode/menu.php index 059d19bae9..0e41b1925b 100644 --- a/pandora_console/godmode/menu.php +++ b/pandora_console/godmode/menu.php @@ -507,7 +507,7 @@ if ($access_console_node === true) { enterprise_hook('skins_submenu'); - $sub['godmode/setup/demo']['text'] = __('Demo'); + $sub['godmode/setup/demo']['text'] = __('Demo data'); $sub['godmode/setup/demo']['id'] = 'demo'; $menu_godmode['gsetup']['sub'] = $sub; diff --git a/pandora_console/godmode/setup/demo.php b/pandora_console/godmode/setup/demo.php index d0d65c748f..9edaedd957 100644 --- a/pandora_console/godmode/setup/demo.php +++ b/pandora_console/godmode/setup/demo.php @@ -38,6 +38,7 @@ config_update_value('demo_data_load_progress', 0); html_print_input_hidden('demo_items_count', 0); +$agents_num = (int) get_parameter('agents_num', 30); $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'); @@ -130,7 +131,7 @@ $table_aux->data['row1'][] = html_print_label_input_block( 'content' => html_print_select( $arraySelectIcon, 'agents_num', - $config['gis_default_icon'], + $agents_num, '', '30', 30, @@ -291,10 +292,14 @@ $actionButtons[] = html_print_submit_button( true ); +echo ''; + echo ''; ?> @@ -308,9 +313,11 @@ echo ''; var agents_str = ''; var delete_demo_data_str = ''; - + + $('#btn-set').show(); if (demo_agents_count > 0) { $('#span-btn-delete-demo-data').text(delete_demo_data_str+' ('+demo_agents_count+')'); + $('#btn-create-demo-data').hide(); $('#btn-delete-demo-data').show(); } @@ -326,7 +333,7 @@ echo ''; var params = {}; params["action"] = "create_demo_data"; params["page"] = "include/ajax/demo_data.ajax"; - params["agents_num"] = $('#agents_num').val(); + params["agents_num"] = ; jQuery.ajax({ data: params, @@ -335,13 +342,16 @@ echo ''; dataType: 'json', success: function(data) { if (data.agents_count > 0) { + $('#btn-create-demo-data').hide(); $('#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"); + error: function(jqXHR, textStatus, errorThrown) { + // Handle error + console.log('Error: ' + textStatus + ' - ' + errorThrown); + console.log(jqXHR.responseText); } }); } @@ -362,6 +372,7 @@ echo ''; $('#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(); + $('#btn-create-demo-data').show(); }, error: function(XMLHttpRequest, textStatus, errorThrown) { console.log("ERROR"); diff --git a/pandora_console/include/ajax/demo_data.ajax.php b/pandora_console/include/ajax/demo_data.ajax.php index c2d163054a..66be29f692 100644 --- a/pandora_console/include/ajax/demo_data.ajax.php +++ b/pandora_console/include/ajax/demo_data.ajax.php @@ -1,6 +1,6 @@ 0) { - // Read all ini files. - $parsed_ini[] = parse_ini_file($config['homedir'].'/extras/demodata/agents/linux.ini', true, INI_SCANNER_TYPED); + // Read agent ini files. + $directories = [ + 'agents', + 'graphs', + 'network_maps', + 'services', + 'reports', + ]; - $ag_groups_num = ($agents_num / 10); + $demodata_directory = $config['homedir'].'/extras/demodata/'; - // 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' - ); + foreach ($directories as $directory) { + $directory_path = $demodata_directory.$directory; + if (is_dir($directory_path)) { + $files = scandir($directory_path); + $files = array_diff($files, ['.', '..']); + } - if ($group_name === false) { - $group_last_id = 0; - } else { - $group_last_id = (int) explode(' ', $group_name)[2]; - } + foreach ($files as $file) { + $parsed_ini[$directory][] = parse_ini_file($directory_path.'/'.$file, true, INI_SCANNER_TYPED); + } + } - if (!($group_last_id > -1)) { - echo json_encode(['msg' => 'Demo group ID not valid']); - return; - } + $total_agents_to_create = (int) get_parameter('agents_num', 0); - $demo_group_name = 'Demo group '.($i + $group_last_id + 1); + if ($total_agents_to_create > 0) { + $agents_to_create = 0; + $agents_created_count = []; - $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; + // First loop over agents to get the total agents to be created and init agent created count for each agent. + foreach ($parsed_ini['agents'] as $ini_agent_data) { + if (isset($ini_agent_data['agent_data']['agents_number']) === true + && $ini_agent_data['agent_data']['agents_number'] > 0 + ) { + $agents_to_create += (int) $ini_agent_data['agent_data']['agents_number']; + $agents_created_count[$ini_agent_data['agent_data']['agent_name']] = 0; } } - $agents_created_count = 0; - $group_idx = 0; + $agent_created_total = 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 ($total_agents_to_create > 0 && $agents_to_create > 0) { + while ($agent_created_total < $total_agents_to_create) { + // Traverse agent ini files and create agents. + foreach ($parsed_ini['agents'] as $ini_agent_data) { + // Read blocks: agent data, modules, inventory, inventory values. + $agent_data = $ini_agent_data['agent_data']; - if ($agent_alias === false) { - $agent_last_id = 0; - } else { - $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']; - $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; - } - - 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]); + if (isset($agent_data['agents_number']) === true + && !((int) $agent_data['agents_number'] > 0) + ) { + // No agents are specified to be created for this agent. continue; } - } - } - // Define agents to be created per group of 10. - $agents_per_os = [ - 'Linux' => 5, - 'Windows' => 2, - 'MAC OS' => 1, - 'BSD' => 1, - 'Cisco' => 1, - ]; + $iter_agents_to_create = $agent_data['agents_number']; - $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++; - - $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++; - } - - // 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) { + if (($agent_created_total + $iter_agents_to_create) >= $total_agents_to_create) { + // Total agents limit specified by user has been reached. break; + } else { + // Calculate max number of agents that can be created in this iteration until max number specified by user is reached. + $max_agents_to_limit = ($total_agents_to_create - ($agent_created_total + $iter_agents_to_create)); } - $id_tipo = db_get_value_filter('id_tipo', 'ttipo_modulo', ['nombre' => $modules_array['type']]); + $modules_data = $ini_agent_data['modules']; + $inventory = $ini_agent_data['inventory']; + $inventory_values = $ini_agent_data['inventory_values']; + $traps = $ini_agent_data['traps']; - $values = [ - 'unit' => $modules_array['unit'], - 'descripcion' => $modules_array['description'], - 'id_tipo_modulo' => $id_tipo, - 'id_module_group' => ($modules_array['group'] ?? 0), - ]; + $address_network = $agent_data['address_network']; - $created_mod_id = modules_create_agent_module( - $created_agent_id, - io_safe_input($modules_array['name']), - $values - ); + if (isset($agent_data['mac']) === true && is_string($agent_data['mac']) === true) { + $mac = $agent_data['mac']; + if ($agent_data['mac'] === '__randomMAC__') { + $mac = generateRandomMacAddress(); + } + } - if ($created_mod_id > 0) { - // Register created demo item in tdemo_data. - $values = [ - 'item_id' => $created_mod_id, - 'table_name' => 'tagente_modulo', - ]; + if (isset($address_network) === false || is_string($address_network) === false) { + // Agent address is not specified or not valid. + continue; + } - $result = (bool) db_process_sql_insert('tdemo_data', $values); + $os_versions = $agent_data['os_versions']; + $os_name = $agent_data['os_name']; + $group_name = $agent_data['group']; - 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]); + // 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) { + // Could not create OS. Skip agent creation. continue; } - // Insert module data. - $parsed = explode(';', $modules_array['values']); + 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 ((string) $parsed[0] === 'RANDOM') { - $data = rand($parsed[1], $parsed[2]); - } else if ((string) $parsed[0] === 'PROC') { - $probability = (int) $parsed[1]; + 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; + } + } + } - $data = 0; + $group_id = get_group_or_create_demo_group($group_name); - if ($probability > 0) { - $randomNumber = rand(1, 100); + // 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++) { + $next_ip_address = calculateNextHostAddress($address_network); - if ($randomNumber <= $probability) { - $data = 1; + $os_version = current($os_versions); + next($os_versions); + + if (current($os_versions) === false) { + reset($os_versions); + } + + $created_agent_id = agents_create_agent( + $agent_data['agent_alias'].'-'.($agents_created_count[$agent_data['agent_name']] + 1), + $group_id, + // Default interval. + 300, + $next_ip_address, + false, + true, + $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; + } + } else { + // Could not create agent. Skip. + continue; + } + + $agents_created_count[$agent_data['agent_name']]++; + + // Calculate progress. + $percentage_inc = (100 / ($agents_to_create * count($parsed_ini))); + $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)); + + // 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); + } + + $module_access_idx++; + + $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']]); + + if (isset($modules_array['type']) === false + || is_string($modules_array['type']) === false + ) { + // Module type not defined. + continue; + } + + $module_description = ''; + + if (isset($modules_array['description']) === true && is_string($modules_array['description']) === true) { + $module_description = str_replace('_mac_', $mac, $modules_array['description']); + } + + $values = [ + 'unit' => $modules_array['unit'], + 'descripcion' => $module_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']); + + $date_time = new DateTime(); + $current_date_time = $date_time->format('Y-m-d H:i:s'); + $back_periods = 10; + $period_mins = 5; + + $utimestamp = time(); + + // Generate back_periods amounts of tagente_datos rows each period_mins minutes back in time. + for ($p = 0; $p < $back_periods; $p++) { + if ((string) $parsed[0] === 'RANDOM') { + $data = rand($parsed[1], $parsed[2]); + } else if ((string) $parsed[0] === 'PROC') { + $probability = (int) $parsed[1]; + + $data = 1; + + if ($probability > 0) { + $randomNumber = rand(1, 100); + + if ($randomNumber <= $probability) { + // Set to 0 with a certain probability. + $data = 0; + } + } + } + + $agent_data_values = [ + 'id_agente_modulo' => $created_mod_id, + 'datos' => $data, + 'utimestamp' => $utimestamp, + ]; + + $created_data_id = 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]); + continue; + } + } + + $utimestamp -= 300; + } + } + }; + + // 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) { + // Insert in tmodule_inventory. + $modules_array = []; + foreach ($inventory as $key => $value) { + $modules_array[$key] = ($value[$module_access_idx] ?? null); + } + + $module_access_idx++; + + $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' => $id_os, + ]; + + $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; + } + } + + // Insert in tagent_module_inventory and 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 + ); + + $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); + + $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 = [ + '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); + + 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; } } } - $agent_data_values = [ - 'id_agente_modulo' => $created_mod_id, - 'datos' => $data, - 'utimestamp' => time(), - ]; + // Create traps. + $date_time = new DateTime(); + $current_date_time = $date_time->format('Y-m-d H:i:s'); + $back_periods = 10; + $period_mins = 5; + $utimestamp = time(); - db_process_sql_insert('tagente_datos', $agent_data_values); - } + for ($p = 0; $p < $back_periods; $p++) { + $trap_access_idx = 1; - $module_access_idx++; - }; + while (1) { + $traps_array = []; + foreach ($traps as $key => $value) { + $traps_array[$key] = ($value[$trap_access_idx] ?? null); + } - // Create inventory modules. - $module_access_idx = 1; - $date_time = new DateTime(); - $current_date_time = $date_time->format('Y-m-d H:i:s'); + $trap_access_idx++; + $test_empty_array = array_filter($traps_array); - while (1) { - // Insert in tmodule_inventory. - $modules_array = []; - foreach ($inventory as $key => $value) { - $modules_array[$key] = ($value[$module_access_idx] ?? null); - } + if (empty($test_empty_array) === true) { + break; + } - $test_empty_array = array_filter($modules_array); + $create_trap = false; + if (isset($traps_array['chance_percent']) === false) { + // Chance percent must be specified. + continue; + } else { + $trap_creation_prob = (int) $traps_array['chance_percent']; - if (empty($test_empty_array) === true) { - break; - } + if ($trap_creation_prob > 0) { + $randomNumber = rand(1, 100); + if ($randomNumber <= $trap_creation_prob) { + $create_trap = true; + } + } + } - $values = [ - 'name' => $modules_array['name'], - 'data_format' => $modules_array['format'], - 'id_os' => 1, - ]; + if ($create_trap === false) { + continue; + } - $created_inventory_mod_id = inventory_create_inventory_module($values); + if (isset($traps_array['value']) === false || is_string($traps_array['value']) === false) { + // Trap value must be specified. + continue; + } else { + $parsed = explode(';', $traps_array['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); + $data = ''; + if ((string) $parsed[0] === 'RANDOM') { + $data = rand($parsed[1], $parsed[2]); + } + } - 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; + $values = [ + 'oid' => $traps_array['oid'], + 'value' => $data, + 'type' => $traps_array['snmp_type'], + 'timestamp' => $current_date_time, + 'utimestamp' => $utimestamp, + ]; + + $created_trap_id = db_process_sql_insert('ttrap', $values); + + if ($created_trap_id > 0) { + // Register created demo item in tdemo_data. + $values = [ + 'item_id' => $created_trap_id, + 'table_name' => 'ttrap', + ]; + $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('ttrap', ['id_module_inventory' => $created_trap_id]); + continue; + } + } + } + + $date_time->sub(new DateInterval("PT{$period_mins}M")); + $current_date_time = $date_time->format('Y-m-d H:i:s'); + $utimestamp -= 300; } } + } - $module_access_idx++; + $agent_created_total = array_sum($agents_created_count); - // Insert in tagent_module_inventory and 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 - ); - - $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); - - $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 = [ - '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); - - 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; - } - } - }; + if ($agent_created_total === 0) { + // Stop traversing agent files if no agent could be created after first round. + break; + } } } } + // Create network maps. + foreach ($parsed_ini['network_maps'] as $ini_nm_data) { + $map_data = $ini_nm_data['map_data']; + $map_items = $ini_nm_data['map_items']; + + $nm_name = $map_data['name']; + $nm_group = $map_data['group']; + $nm_description = $map_data['description']; + $nm_node_radius = $map_data['node_radius']; + + $nm_id_group = get_group_or_create_demo_group($nm_group); + + if ($nm_id_group === false) { + // Network map could not be created. Skip network map creation. + continue; + } + + $values = []; + $new_map_filter = []; + $new_map_filter['dont_show_subgroups'] = 0; + $new_map_filter['node_radius'] = $nm_node_radius; + $new_map_filter['x_offs'] = 0; + $new_map_filter['y_offs'] = 0; + $new_map_filter['z_dash'] = '0.5'; + $new_map_filter['node_sep'] = '0.25'; + $new_map_filter['rank_sep'] = '0.25'; + $new_map_filter['mindist'] = 1; + $new_map_filter['kval'] = '0.3'; + $values['filter'] = json_encode($new_map_filter); + $values['description'] = $nm_description; + $values['id_group'] = $nm_id_group; + $values['name'] = $nm_name; + + $id_map = db_process_sql_insert('tmap', $values); + + if ($id_map > 0) { + // Register created map in tdemo_data. + $values = [ + 'item_id' => $id_map, + 'table_name' => 'tmap', + ]; + $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('tmap', ['id' => $id_map]); + continue; + } + } else { + // Network map group could not be created. Skip creation of map. + continue; + } + + if (count($map_items) > 0) { + $item_access_idx = 1; + + while (1) { + $items_array = []; + foreach ($map_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; + } + + $item_values = []; + + $item_values['id_map'] = $id_map; + + if (isset($items_array['agent_name']) === true || is_string($items_array['agent_name']) === false) { + $matched_agents = agents_get_agents(['nombre' => $items_array['agent_name']], ['id_agente', 'id_os', 'alias']); + + $matched_agent = $matched_agents[0]['id_agente']; + $alias = $matched_agents[0]['alias']; + if (isset($matched_agent) === true && $matched_agent > 0) { + $item_values['source_data'] = $matched_agent; + if (isset($matched_agents[0]['id_agente']) === true && $matched_agents[0]['id_os']) { + $agent_os_id = $matched_agents[0]['id_os']; + $icon_name = db_get_value('icon_name', 'tconfig_os', 'id_os', $agent_os_id); + } + } else { + // Skip report item creation if agent does not exist. + continue; + } + } else { + continue; + } + + $style_values = [ + 'shape' => 'circle', + 'image' => 'images/networkmap/'.$icon_name, + 'width' => null, + 'height' => null, + 'label' => $alias, + ]; + + $item_values['style'] = json_encode($style_values); + $item_values['x'] = (isset($items_array['x']) === true) ? $items_array['x'] : '0'; + $item_values['y'] = (isset($items_array['y']) === true) ? $items_array['y'] : '0'; + + $created_nm_item_id = db_process_sql_insert('titem', $item_values); + + if ($created_nm_item_id > 0) { + // Register created demo item in tdemo_data. + $values = [ + 'item_id' => $created_nm_item_id, + 'table_name' => 'titem', + ]; + $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('titem', ['id' => $created_nm_item_id]); + continue; + } + } + } + } + + // Calculate progress. + $percentage_inc = (100 / count($parsed_ini)); + $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)); + } + + // Create graphs. + foreach ($parsed_ini['graphs'] as $ini_graph_data) { + // Constant graph types. + $graph_types = [ + 'line' => 2, + 'area' => 0, + 's_line' => 3, + 's_area' => 1, + 'h_bars' => 6, + 'v_bars' => 7, + 'gauge' => 5, + 'pie' => 8, + ]; + + $graph_data = $ini_graph_data['graph_data']; + $graph_items = $ini_graph_data['graph_items']; + + $graph_name = $graph_data['name']; + $graph_group = $graph_data['group']; + $graph_description = $graph_data['description']; + $graph_type = (isset($graph_types[$graph_data['type']]) === true) ? $graph_types[$graph_data['type']] : 0; + $graph_periodicity = $graph_data['periodicity']; + + $graph_id_group = get_group_or_create_demo_group($graph_group); + + if ($graph_id_group === false) { + // Group could not be created. Skip graph creation. + continue; + } + + $values = []; + $values['description'] = $graph_description; + $values['id_group'] = $graph_id_group; + $values['name'] = $graph_name; + $values['period'] = $graph_periodicity; + $values['stacked'] = $graph_type; + + $id_graph = db_process_sql_insert('tgraph', $values); + + if ($id_graph > 0) { + // Register created graph in tdemo_data. + $values = [ + 'item_id' => $id_graph, + 'table_name' => 'tgraph', + ]; + $result = (bool) db_process_sql_insert('tdemo_data', $values); + + if ($result === false) { + // Rollback graph creation if could not be registered in tdemo_data. + db_process_sql_delete('tgraph', ['id_graph' => $id_graph]); + continue; + } + } else { + // Graph could not be created. Skip creation of graph. + continue; + } + + if (count($graph_items) > 0) { + $item_access_idx = 1; + + while (1) { + $items_array = []; + foreach ($graph_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; + } + + $item_values = []; + + if (isset($items_array['agent_name']) === false || is_string($items_array['agent_name']) === false) { + // Agent must be defined. Skip graph item creation. + continue; + } + + if (isset($items_array['module']) === false || is_string($items_array['module']) === false) { + // Module must be defined. Skip graph item creation. + continue; + } + + $matched_agents = agents_get_agents(['nombre' => $items_array['agent_name']], ['id_agente']); + $agent_id = $matched_agents[0]['id_agente']; + + $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; + } + + $item_values = [ + 'id_graph' => $id_graph, + 'id_agent_module' => $module_id, + ]; + + $created_graph_item_id = db_process_sql_insert('tgraph_source', $item_values); + + if ($created_graph_item_id > 0) { + // Register created demo item in tdemo_data. + $values = [ + 'item_id' => $created_graph_item_id, + 'table_name' => 'tgraph_source', + ]; + $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('tgraph_source', ['id_gs' => $created_graph_item_id]); + continue; + } + } + } + } + + // Calculate progress. + $percentage_inc = (100 / count($parsed_ini)); + $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)); + } + + // Create reports. + foreach ($parsed_ini['reports'] as $ini_report_data) { + $report_data = $ini_report_data['report_data']; + $report_items = $ini_report_data['report_items']; + + $group_id = get_group_or_create_demo_group($report_data['group']); + + if ($group_id === false) { + // Could not create group. Skip report creation. + continue; + } + + $report_values = []; + $report_values['id_group'] = $group_id; + $report_values['name'] = $report_data['name']; + $report_values['description'] = $report_data['description']; + + $created_report_id = db_process_sql_insert('treport', $report_values); + + if ($created_report_id > 0) { + // Register created graph in tdemo_data. + $values = [ + 'item_id' => $created_report_id, + 'table_name' => 'treport', + ]; + $result = (bool) db_process_sql_insert('tdemo_data', $values); + + if ($result === false) { + // Rollback report creation if could not be registered in tdemo_data. + db_process_sql_delete('treport', ['id_report' => $created_report_id]); + continue; + } + } else { + // Report could not be created. Skip creation of map. + continue; + } + + if (count($report_items) > 0) { + $item_access_idx = 1; + + while (1) { + $items_array = []; + foreach ($report_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; + } + + $item_values = []; + + $item_values['id_report'] = $created_report_id; + + if (isset($items_array['agent_name']) === true) { + if (is_string($items_array['module']) === false) { + continue; + } + + $matched_agents = agents_get_agents(['nombre' => $items_array['agent_name']], ['id_agente']); + + $matched_agent = $matched_agents[0]['id_agente']; + if (isset($matched_agent) === true && $matched_agent > 0) { + $item_values['id_agent'] = $matched_agent; + } else { + // Skip report item creation if agent does not exist. + continue; + } + } + + if (isset($items_array['module']) === true) { + if (is_string($items_array['module']) === false) { + // Module wrong data type read. Skip. + continue; + } + + if ($item_values['id_agent'] > 0) { + $module_id = db_get_value_sql('SELECT id_agente_modulo FROM tagente_modulo WHERE nombre = "'.io_safe_input($items_array['module']).'" AND id_agente = '.$item_values['id_agent']); + + if ($module_id > 0) { + $item_values['id_agent_module'] = $module_id; + } else { + // Skip report item creation if agent module does not exist. + continue; + } + } else { + continue; + } + } + + 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) { + $item_values['id_gs'] = $id_custom_graph; + } else { + // Skip report item creation if specified custom graph does not exist. + continue; + } + } + + $created_report_item_id = db_process_sql_insert('treport_content', $item_values); + + if ($created_report_item_id > 0) { + // Register created demo item in tdemo_data. + $values = [ + 'item_id' => $created_report_item_id, + 'table_name' => 'treport_content', + ]; + $result = (bool) db_process_sql_insert('tdemo_data', $values); + + if ($result === false) { + // Rollback report item if could not be registered in tdemo_data. + db_process_sql_delete('treport_content', ['id_rc' => $created_report_item_id]); + continue; + } + } + } + } + + // Calculate progress. + $percentage_inc = (100 / count($parsed_ini)); + $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)); + } + + // Create services. + foreach ($parsed_ini['services'] as $ini_service_data) { + $service_data = $ini_service_data['service_data']; + $service_items = $ini_service_data['service_items']; + + // Check for mandatory fields. + if (isset($service_data['name']) === false + || isset($service_data['group']) === false + ) { + continue; + } + + $id_group = get_group_or_create_demo_group($service_data['group']); + + if ($id_group === false) { + // Group could not be created. Skip graph creation. + continue; + } + + $service_values = []; + + $service_values['name'] = $service_data['name']; + $service_values['description'] = $service_data['description']; + $service_values['id_group'] = $id_group; + $service_values['critical'] = $service_data['critical']; + $service_values['warning'] = $service_data['warning']; + $service_values['auto_calculate'] = (isset($service_data['mode']) === true && (string) $service_data['mode'] === 'smart') ? 1 : 0; + + $created_service_id = db_process_sql_insert('tservice', $service_values); + + if ($created_service_id > 0) { + // Register created service in tdemo_data. + $values = [ + 'item_id' => $created_service_id, + 'table_name' => 'tservice', + ]; + $result = (bool) db_process_sql_insert('tdemo_data', $values); + + if ($result === false) { + // Rollback service creation if could not be registered in tdemo_data. + db_process_sql_delete('tservice', ['id' => $created_service_id]); + continue; + } + } else { + // Service could not be created. Skip creation of map. + continue; + } + + if (count($service_items) > 0) { + $item_access_idx = 1; + while (1) { + $items_array = []; + foreach ($service_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; + } + + $element_values = []; + + $element_values = ['id_service' => $created_service_id]; + + $element_type = (string) $items_array['type']; + + if (in_array($element_type, ['agent', 'module', 'dynamic', 'service']) === false) { + // Skip element creation if type not valid. + continue; + } + + if (in_array($element_type, ['agent', 'module', 'dynamic']) === true) { + // Get agent ID and module ID. + $matched_agents = agents_get_agents(['nombre' => $items_array['agent_name']], ['id_agente']); + $matched_agent = $matched_agents[0]['id_agente']; + + if (isset($matched_agent) === true && $matched_agent > 0) { + $element_values['id_agent'] = $matched_agent; + } else { + // Skip element creation if agent does not exist. + continue; + } + } + + if (in_array($element_type, ['module', 'dynamic']) === true) { + if ($element_values['id_agent'] > 0) { + $module_id = db_get_value_sql('SELECT id_agente_modulo FROM tagente_modulo WHERE nombre = "'.io_safe_input($items_array['module']).'" AND id_agente = '.$element_values['id_agent']); + + if ($module_id > 0) { + $element_values['id_agente_modulo'] = $module_id; + } else { + // Skip element creation if agent module does not exist. + continue; + } + } else { + continue; + } + } + + if ($element_type === 'dynamic') { + if ($service_values['auto_calculate'] === 1) { + if (isset($items_array['match']) === false + || ($items_array['match'] !== 'agent' && $items_array['match'] !== 'module') + ) { + // If failed to provide match value, 'agent' is assigned by default. + $match_value = 'agent'; + } else { + $match_value = $items_array['match']; + } + + if (isset($items_array['group']) === true) { + $group_id_value = get_group_or_create_demo_group($items_array['group']); + + if ($group_id_value === false) { + $group_id_value = -1; + } + } else { + $group_id_value = -1; + } + + $element_values['id_agent'] = 0; + $element_values['id_agente_modulo'] = 0; + + $rules_arr = [ + 'dynamic_type' => $match_value, + 'group' => $group_id_value, + 'agent_name' => (isset($items_array['agent_name']) === true) ? $items_array['agent_name'] : '', + 'module_name' => (isset($items_array['module']) === true) ? $items_array['module'] : '', + 'regex_mode' => (isset($items_array['regex']) === true) ? $items_array['regex'] : false, + 'custom_fields' => [], + ]; + + $element_values['rules'] = base64_encode(json_encode($rules_arr)); + } + } + + if ($element_type === 'service') { + if (isset($items_array['service_name']) === true + && is_string($items_array['service_name']) === true + ) { + $services = services_get_services(['name' => $items_array['service_name']]); + + $service_id = $services[0]['id']; + + if ($service_id > 0) { + $element_values['id_service_child'] = $service_id; + } else { + // Skip element creation if specified service does not exist. + continue; + } + } else { + // Skip element creation if service name was not provided. + continue; + } + } + + $id = db_process_sql_insert('tservice_element', $element_values); + + if ($id > 0) { + // Register created demo item in tdemo_data. + $values = [ + 'item_id' => $id, + 'table_name' => 'tservice_element', + ]; + $result = (bool) db_process_sql_insert('tdemo_data', $values); + + if ($result === false) { + // Rollback service element if could not be registered in tdemo_data. + db_process_sql_delete('tservice_element', ['id' => $id]); + continue; + } + } + } + } + + // Calculate progress. + $percentage_inc = (100 / count($parsed_ini)); + $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'); + echo json_encode(['agents_count' => $demo_agents_count]); return; @@ -421,6 +1193,16 @@ if ($action === 'cleanup_demo_data') { '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', + 'treport_content' => 'id_rc', + 'tservice' => 'id', + 'tservice_element' => 'id', + 'ttrap' => 'id_trap', + 'tagente_datos' => 'id_agente_modulo', + 'titem' => 'id', + 'tgraph_source' => 'id_gs', ]; $table_id_field = $table_id_field_dict[$item['table_name']]; @@ -461,7 +1243,8 @@ if ($action === 'get_progress_bar') { return; } -function calculateNextHostAddress($ip) { +function calculateNextHostAddress($ip) +{ list($network, $subnet) = explode('/', $ip); // Convert the network address to an array of octets. @@ -478,3 +1261,53 @@ function calculateNextHostAddress($ip) { return $nextHost.'/'.$subnet; } + +function get_group_or_create_demo_group($name) +{ + if (is_string($name) === false) { + return false; + } + + $id_group = db_get_value('id_grupo', 'tgrupo', 'nombre', $name); + + if ($id_group > 0) { + return $id_group; + } else { + $id_group = groups_create_group($group, []); + + if ($id_group > 0) { + // Register created group in tdemo_data. + $values = [ + 'item_id' => $id_group, + 'table_name' => 'tgrupo', + ]; + $result = (bool) db_process_sql_insert('tdemo_data', $values); + + if ($result === false) { + // Rollback demo group creation if could not be registered in tdemo_data. + db_process_sql_delete('tgrupo', ['id_grupo' => $id_group]); + return false; + } + + return $id_group; + } else { + // Network map group could not be created. Skip creation of map. + return false; + } + } +} + +function generateRandomMacAddress() +{ + $macAddress = []; + + // Generate the remaining five octets. + for ($i = 0; $i < 6; $i++) { + $macAddress[] = str_pad(dechex(mt_rand(0, 255)), 2, '0', STR_PAD_LEFT); + } + + // Join the octets with colons to form the MAC address. + $randomMacAddress = implode(':', $macAddress); + + return $randomMacAddress; +} diff --git a/pandora_console/include/class/WelcomeWindow.class.php b/pandora_console/include/class/WelcomeWindow.class.php index cd057414d0..c0800e8b4c 100644 --- a/pandora_console/include/class/WelcomeWindow.class.php +++ b/pandora_console/include/class/WelcomeWindow.class.php @@ -1158,7 +1158,7 @@ class WelcomeWindow extends Wizard // Task to do actions. function loadDemoDataPage() { - window.location = ''; + window.location = ''; } function openCreateModulesDialog() { diff --git a/pandora_console/include/db/mysql.php b/pandora_console/include/db/mysql.php index e8e7a6b020..048ec8719f 100644 --- a/pandora_console/include/db/mysql.php +++ b/pandora_console/include/db/mysql.php @@ -655,7 +655,7 @@ function mysql_encapsule_fields_with_same_name_to_instructions($field) * * @return mixed Value of first column of the first row. False if there were no row. */ -function mysql_db_get_value_filter($field, $table, $filter, $where_join='AND', $search_history_db=false) +function mysql_db_get_value_filter($field, $table, $filter, $where_join='AND', $search_history_db=false, $cache=true) { if (!is_array($filter) || empty($filter)) { return false; @@ -672,7 +672,7 @@ function mysql_db_get_value_filter($field, $table, $filter, $where_join='AND', $ db_format_array_where_clause_sql($filter, $where_join) ); - $result = db_get_all_rows_sql($sql, $search_history_db); + $result = db_get_all_rows_sql($sql, $search_history_db, $cache); if ($result === false) { return false; @@ -1022,7 +1022,11 @@ function mysql_db_get_row_filter($table, $filter, $fields=false, $where_join='AN } $sql = sprintf('SELECT %s FROM %s %s', $fields, $table, $filter); - +if ($table==='tagente_modulo') { + hd("QUERYYYYYY", true); + hd($sql, true); +} + return db_get_row_sql($sql, $historydb, $cache); } diff --git a/pandora_console/include/db/oracle.php b/pandora_console/include/db/oracle.php index 3ec2b558a4..c712e6a008 100644 --- a/pandora_console/include/db/oracle.php +++ b/pandora_console/include/db/oracle.php @@ -574,7 +574,7 @@ function oracle_encapsule_fields_with_same_name_to_instructions($field) * * @return mixed Value of first column of the first row. False if there were no row. */ -function oracle_db_get_value_filter($field, $table, $filter, $where_join='AND', $search_history_db=false) +function oracle_db_get_value_filter($field, $table, $filter, $where_join='AND', $search_history_db=false, $cache=true) { if (! is_array($filter) || empty($filter)) { return false; @@ -590,7 +590,7 @@ function oracle_db_get_value_filter($field, $table, $filter, $where_join='AND', $table, db_format_array_where_clause_sql($filter, $where_join) ); - $result = db_get_all_rows_sql($sql, $search_history_db); + $result = db_get_all_rows_sql($sql, $search_history_db, $cache); if ($result === false) { return false; diff --git a/pandora_console/include/db/postgresql.php b/pandora_console/include/db/postgresql.php index a664bd45c7..8c1394a97d 100644 --- a/pandora_console/include/db/postgresql.php +++ b/pandora_console/include/db/postgresql.php @@ -474,7 +474,7 @@ function postgresql_encapsule_fields_with_same_name_to_instructions($field) * * @return mixed Value of first column of the first row. False if there were no row. */ -function postgresql_db_get_value_filter($field, $table, $filter, $where_join='AND', $search_history_db=false) +function postgresql_db_get_value_filter($field, $table, $filter, $where_join='AND', $search_history_db=false, $cache=true) { if (! is_array($filter) || empty($filter)) { return false; @@ -496,7 +496,7 @@ function postgresql_db_get_value_filter($field, $table, $filter, $where_join='AN db_format_array_where_clause_sql($filter, $where_join) ); - $result = db_get_all_rows_sql($sql, $search_history_db); + $result = db_get_all_rows_sql($sql, $search_history_db, $cache); $row = array_shift($result); $value = array_shift($row); diff --git a/pandora_console/include/functions_db.php b/pandora_console/include/functions_db.php index 637257e764..3872b405a0 100644 --- a/pandora_console/include/functions_db.php +++ b/pandora_console/include/functions_db.php @@ -380,21 +380,21 @@ function db_get_value( * * @return mixed Value of first column of the first row. False if there were no row. */ -function db_get_value_filter($field, $table, $filter, $where_join='AND', $search_history_db=false) +function db_get_value_filter($field, $table, $filter, $where_join='AND', $search_history_db=false, $cache=true) { global $config; switch ($config['dbtype']) { case 'mysql': - return mysql_db_get_value_filter($field, $table, $filter, $where_join, $search_history_db); + return mysql_db_get_value_filter($field, $table, $filter, $where_join, $search_history_db, $cache); break; case 'postgresql': - return postgresql_db_get_value_filter($field, $table, $filter, $where_join, $search_history_db); + return postgresql_db_get_value_filter($field, $table, $filter, $where_join, $search_history_db, $cache); break; case 'oracle': - return oracle_db_get_value_filter($field, $table, $filter, $where_join, $search_history_db); + return oracle_db_get_value_filter($field, $table, $filter, $where_join, $search_history_db, $cache); break; } From 31e4e872c14e7521580d4451fab1972c41e45cc3 Mon Sep 17 00:00:00 2001 From: "alejandro.campos@artica.es" Date: Tue, 21 Nov 2023 08:29:33 +0100 Subject: [PATCH 04/42] implement demo data load --- .../include/ajax/demo_data.ajax.php | 313 ++++++++++++++++++ pandora_console/include/db/mysql.php | 4 - 2 files changed, 313 insertions(+), 4 deletions(-) diff --git a/pandora_console/include/ajax/demo_data.ajax.php b/pandora_console/include/ajax/demo_data.ajax.php index 66be29f692..d5c8d5bd1e 100644 --- a/pandora_console/include/ajax/demo_data.ajax.php +++ b/pandora_console/include/ajax/demo_data.ajax.php @@ -27,6 +27,7 @@ check_login(); include $config['homedir'].'/include/functions_inventory.php'; include $config['homedir'].'/include/functions_custom_graphs.php'; +include $config['homedir'].'/include/functions_reports.php'; $action = (string) get_parameter('action', null); @@ -40,6 +41,7 @@ if ($action === 'create_demo_data') { 'network_maps', 'services', 'reports', + 'dashboards', ]; $demodata_directory = $config['homedir'].'/extras/demodata/'; @@ -782,6 +784,10 @@ if ($action === 'create_demo_data') { $matched_agents = agents_get_agents(['nombre' => $items_array['agent_name']], ['id_agente']); $agent_id = $matched_agents[0]['id_agente']; + if (!($agent_id > 0)) { + continue; + } + $module_row = modules_get_agentmodule_id(io_safe_input($items_array['module']), $agent_id); $module_id = $module_row['id_agente_modulo']; @@ -1038,6 +1044,11 @@ if ($action === 'create_demo_data') { break; } + if (isset($items_array['type']) === false) { + // Service element type must be specified. + continue; + } + $element_values = []; $element_values = ['id_service' => $created_service_id]; @@ -1171,6 +1182,303 @@ if ($action === 'create_demo_data') { config_update_value('demo_data_load_progress', ($current_progress_val + $percentage_inc)); } + // Create dashboards. + foreach ($parsed_ini['dashboards'] as $ini_data) { + $data = $ini_data['dashboard_data']; + $items = $ini_data['dashboard_items']; + + // Check for mandatory fields. + if (isset($data['name']) === false + || isset($data['group']) === false + ) { + // Name and group fields must be specified for dashbaord. + 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; + + $created_id = db_process_sql_insert('tdashboard', $insert_values); + + if ($created_id > 0) { + // Register created item in tdemo_data. + $values = [ + 'item_id' => $created_id, + 'table_name' => 'tdashboard', + ]; + $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('tdashboard', ['id' => $created_id]); + continue; + } + } else { + // Dashboard could not be created. Skip creation of item. + continue; + } + + if (count($items) > 0) { + $item_access_idx = 1; + $order = -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 || isset($items_array['title']) === false) { + // All dashboard widgets must have a type and a title. + continue; + } + + // Get ID of widget type. Skip if it does not exist. + $type_id = db_get_value('id', 'twidget', 'unique_name', $items_array['type']); + + if (!($type_id > 0)) { + continue; + } + + $title = io_safe_input($items_array['title']); + + if ($items_array['type'] === 'single_graph') { + if (isset($items_array['agent_name']) === false + || isset($items_array['module']) === false + ) { + // The above fields are required for this item. + continue; + } + + $matched_agents = agents_get_agents(['nombre' => $items_array['agent_name']], ['id_agente']); + $agent_id = $matched_agents[0]['id_agente']; + + if (!($agent_id > 0)) { + continue; + } + + $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 = []; + + $options_data = [ + 'title' => $title, + 'background' => '#ffffff', + 'agentId' => "$agent_id", + 'metaconsoleId' => 0, + 'moduleId' => "$module_id", + 'period' => (isset($items_array['interval']) === false) ? $items_array['interval'] : '86400', + 'showLegend' => 1, + 'projection_switch' => false, + 'period_projection' => '300', + ]; + + $order++; + } + + if ($items_array['type'] === 'custom_graph') { + if (isset($items_array['graph_name']) === false + || isset($items_array['graph_type']) === false + ) { + // The above fields are required for this item. + continue; + } + + // Try to get graph and skip if not exists. + $id_graph = db_get_value('id_graph', 'tgraph', 'name', $items_array['graph_name']); + + if (!($id_graph > 0)) { + continue; + } + + $graph_types = [ + 'line' => 2, + 'area' => 0, + 's_line' => 3, + 's_area' => 1, + 'h_bars' => 6, + 'v_bars' => 7, + 'gauge' => 5, + 'pie' => 8, + ]; + + if (isset($graph_types[$items_array['graph_type']]) === true) { + $graph_type_id = $items_array['graph_type']; + } else { + // Specified graph type is not a valid one. + continue; + } + + $element_values = []; + + $options_data = [ + 'title' => $title, + 'background' => '#ffffff', + 'id_graph' => $id_graph, + 'type' => $graph_type_id, + 'period' => (isset($items_array['interval']) === false) ? $items_array['interval'] : 86400, + 'showLegend' => 1, + ]; + + $order++; + } + + if ($items_array['type'] === 'reports') { + if (isset($items_array['report_name']) === false) { + // The above fields are required for this item. + continue; + } + + $id_report = reports_get_reports(['name' => $items_array['report_name']], ['id_report'])[0]['id_report']; + + if (!($id_report > 0)) { + continue; + } + + $element_values = []; + + $options_data = [ + 'title' => $title, + 'background' => '#ffffff', + 'reportId' => $id_report, + ]; + + $order++; + } + + if ($items_array['type'] === 'network_map') { + if (isset($items_array['map_name']) === false) { + // The above fields are required for this item. + continue; + } + + $id_map = db_get_value('id', 'tmap', 'name', $items_array['map_name']); + + if (!($id_map > 0)) { + continue; + } + + $element_values = []; + + $options_data = [ + 'title' => $title, + 'background' => '#ffffff', + 'networkmapId' => "$id_map", + 'xOffset' => '0', + 'yOffset' => '0', + 'zoomLevel' => 0.5, + ]; + + $order++; + } + + if ($items_array['type'] === 'service_map') { + if (isset($items_array['service_name']) === false) { + // The above fields are required for this item. + continue; + } + + $services = services_get_services(['name' => $items_array['service_name']]); + + $service_id = $services[0]['id']; + + if (!($service_id > 0)) { + continue; + } + + $element_values = []; + + $options_data = [ + 'title' => $title, + 'background' => '#ffffff', + 'serviceId' => "$service_id", + 'sunburst' => 0, + ]; + + $order++; + } + + $item_x = $items_array['x']; + $item_y = $items_array['y']; + $item_width = $items_array['width']; + $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", + ]; + + $element_values = [ + 'position' => json_encode($position_data), + 'options' => json_encode($options_data), + 'order' => $order, + 'id_dashboard' => $created_id, + 'id_widget' => $type_id, + 'prop_width' => 0.32, + 'prop_height' => 0.32, + ]; + + $id = db_process_sql_insert('twidget_dashboard', $element_values); + + if ($id > 0) { + // Register created demo item in tdemo_data. + $values = [ + 'item_id' => $id, + 'table_name' => 'twidget_dashboard', + ]; + $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('twidget_dashboard', ['id' => $id]); + continue; + } + } + } + } + + // Calculate progress. + $percentage_inc = (100 / count($parsed_ini)); + $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'); @@ -1203,6 +1511,8 @@ if ($action === 'cleanup_demo_data') { 'tagente_datos' => 'id_agente_modulo', 'titem' => 'id', 'tgraph_source' => 'id_gs', + 'twidget_dashboard' => 'id', + 'tdashboard' => 'id', ]; $table_id_field = $table_id_field_dict[$item['table_name']]; @@ -1243,6 +1553,7 @@ if ($action === 'get_progress_bar') { return; } + function calculateNextHostAddress($ip) { list($network, $subnet) = explode('/', $ip); @@ -1262,6 +1573,7 @@ function calculateNextHostAddress($ip) return $nextHost.'/'.$subnet; } + function get_group_or_create_demo_group($name) { if (is_string($name) === false) { @@ -1297,6 +1609,7 @@ function get_group_or_create_demo_group($name) } } + function generateRandomMacAddress() { $macAddress = []; diff --git a/pandora_console/include/db/mysql.php b/pandora_console/include/db/mysql.php index 048ec8719f..01fdf6512f 100644 --- a/pandora_console/include/db/mysql.php +++ b/pandora_console/include/db/mysql.php @@ -1022,10 +1022,6 @@ function mysql_db_get_row_filter($table, $filter, $fields=false, $where_join='AN } $sql = sprintf('SELECT %s FROM %s %s', $fields, $table, $filter); -if ($table==='tagente_modulo') { - hd("QUERYYYYYY", true); - hd($sql, true); -} return db_get_row_sql($sql, $historydb, $cache); } From 0868c0c995a27c01cf9a41279273169ea4d55409 Mon Sep 17 00:00:00 2001 From: "alejandro.campos@artica.es" Date: Tue, 21 Nov 2023 14:26:33 +0100 Subject: [PATCH 05/42] implement demo data load --- pandora_console/godmode/setup/demo.php | 92 +- .../include/ajax/demo_data.ajax.php | 2016 ++++++++++------- 2 files changed, 1224 insertions(+), 884 deletions(-) diff --git a/pandora_console/godmode/setup/demo.php b/pandora_console/godmode/setup/demo.php index 9edaedd957..daf400003d 100644 --- a/pandora_console/godmode/setup/demo.php +++ b/pandora_console/godmode/setup/demo.php @@ -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') { ) ); ?> - + '; var agent_count_span_str = ''; var agents_str = ''; var delete_demo_data_str = ''; - - + $('#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 ''; var submit_value = ''; + var mode = ""; + + if (mode == 'advanced' + && submit_value != 'Create demo data' + && submit_value != 'Delete demo data' + ) { + confirmDialog({ + title: "", + message: "", + hideCancelButton: true, + onAccept: function() { + $('#user_profile_form').submit(); + } + }); + } + if (submit_value == 'Create demo data') { $("#table-demo-row2").show(); @@ -334,6 +355,7 @@ echo ''; params["action"] = "create_demo_data"; params["page"] = "include/ajax/demo_data.ajax"; params["agents_num"] = ; + params["enabled_items"] = ; jQuery.ajax({ data: params, diff --git a/pandora_console/include/ajax/demo_data.ajax.php b/pandora_console/include/ajax/demo_data.ajax.php index d5c8d5bd1e..5353a49557 100644 --- a/pandora_console/include/ajax/demo_data.ajax.php +++ b/pandora_console/include/ajax/demo_data.ajax.php @@ -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,718 +566,236 @@ if ($action === 'create_demo_data') { } } - // Create network maps. - foreach ($parsed_ini['network_maps'] as $ini_nm_data) { - $map_data = $ini_nm_data['map_data']; - $map_items = $ini_nm_data['map_items']; + 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']; + $map_items = $ini_nm_data['map_items']; - $nm_name = $map_data['name']; - $nm_group = $map_data['group']; - $nm_description = $map_data['description']; - $nm_node_radius = $map_data['node_radius']; + $nm_name = $map_data['name']; + $nm_group = $map_data['group']; + $nm_description = $map_data['description']; + $nm_node_radius = $map_data['node_radius']; - $nm_id_group = get_group_or_create_demo_group($nm_group); + $nm_id_group = get_group_or_create_demo_group($nm_group); - if ($nm_id_group === false) { - // Network map could not be created. Skip network map creation. - continue; - } - - $values = []; - $new_map_filter = []; - $new_map_filter['dont_show_subgroups'] = 0; - $new_map_filter['node_radius'] = $nm_node_radius; - $new_map_filter['x_offs'] = 0; - $new_map_filter['y_offs'] = 0; - $new_map_filter['z_dash'] = '0.5'; - $new_map_filter['node_sep'] = '0.25'; - $new_map_filter['rank_sep'] = '0.25'; - $new_map_filter['mindist'] = 1; - $new_map_filter['kval'] = '0.3'; - $values['filter'] = json_encode($new_map_filter); - $values['description'] = $nm_description; - $values['id_group'] = $nm_id_group; - $values['name'] = $nm_name; - - $id_map = db_process_sql_insert('tmap', $values); - - if ($id_map > 0) { - // Register created map in tdemo_data. - $values = [ - 'item_id' => $id_map, - 'table_name' => 'tmap', - ]; - $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('tmap', ['id' => $id_map]); + if ($nm_id_group === false) { + // Network map could not be created. Skip network map creation. continue; } - } else { - // Network map group could not be created. Skip creation of map. - continue; - } - if (count($map_items) > 0) { - $item_access_idx = 1; + $values = []; + $new_map_filter = []; + $new_map_filter['dont_show_subgroups'] = 0; + $new_map_filter['node_radius'] = $nm_node_radius; + $new_map_filter['x_offs'] = 0; + $new_map_filter['y_offs'] = 0; + $new_map_filter['z_dash'] = '0.5'; + $new_map_filter['node_sep'] = '0.25'; + $new_map_filter['rank_sep'] = '0.25'; + $new_map_filter['mindist'] = 1; + $new_map_filter['kval'] = '0.3'; + $values['filter'] = json_encode($new_map_filter); + $values['description'] = $nm_description; + $values['id_group'] = $nm_id_group; + $values['name'] = $nm_name; - while (1) { - $items_array = []; - foreach ($map_items as $key => $value) { - $items_array[$key] = ($value[$item_access_idx] ?? null); - } + $id_map = db_process_sql_insert('tmap', $values); - $item_access_idx++; - - $test_empty_array = array_filter($items_array); - - if (empty($test_empty_array) === true) { - break; - } - - $item_values = []; - - $item_values['id_map'] = $id_map; - - if (isset($items_array['agent_name']) === true || is_string($items_array['agent_name']) === false) { - $matched_agents = agents_get_agents(['nombre' => $items_array['agent_name']], ['id_agente', 'id_os', 'alias']); - - $matched_agent = $matched_agents[0]['id_agente']; - $alias = $matched_agents[0]['alias']; - if (isset($matched_agent) === true && $matched_agent > 0) { - $item_values['source_data'] = $matched_agent; - if (isset($matched_agents[0]['id_agente']) === true && $matched_agents[0]['id_os']) { - $agent_os_id = $matched_agents[0]['id_os']; - $icon_name = db_get_value('icon_name', 'tconfig_os', 'id_os', $agent_os_id); - } - } else { - // Skip report item creation if agent does not exist. - continue; - } - } else { - continue; - } - - $style_values = [ - 'shape' => 'circle', - 'image' => 'images/networkmap/'.$icon_name, - 'width' => null, - 'height' => null, - 'label' => $alias, + if ($id_map > 0) { + // Register created map in tdemo_data. + $values = [ + 'item_id' => $id_map, + 'table_name' => 'tmap', ]; + $result = (bool) db_process_sql_insert('tdemo_data', $values); - $item_values['style'] = json_encode($style_values); - $item_values['x'] = (isset($items_array['x']) === true) ? $items_array['x'] : '0'; - $item_values['y'] = (isset($items_array['y']) === true) ? $items_array['y'] : '0'; - - $created_nm_item_id = db_process_sql_insert('titem', $item_values); - - if ($created_nm_item_id > 0) { - // Register created demo item in tdemo_data. - $values = [ - 'item_id' => $created_nm_item_id, - 'table_name' => 'titem', - ]; - $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('titem', ['id' => $created_nm_item_id]); - continue; - } + if ($result === false) { + // Rollback demo item creation if could not be registered in tdemo_data. + db_process_sql_delete('tmap', ['id' => $id_map]); + continue; } - } - } - - // Calculate progress. - $percentage_inc = (100 / count($parsed_ini)); - $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)); - } - - // Create graphs. - foreach ($parsed_ini['graphs'] as $ini_graph_data) { - // Constant graph types. - $graph_types = [ - 'line' => 2, - 'area' => 0, - 's_line' => 3, - 's_area' => 1, - 'h_bars' => 6, - 'v_bars' => 7, - 'gauge' => 5, - 'pie' => 8, - ]; - - $graph_data = $ini_graph_data['graph_data']; - $graph_items = $ini_graph_data['graph_items']; - - $graph_name = $graph_data['name']; - $graph_group = $graph_data['group']; - $graph_description = $graph_data['description']; - $graph_type = (isset($graph_types[$graph_data['type']]) === true) ? $graph_types[$graph_data['type']] : 0; - $graph_periodicity = $graph_data['periodicity']; - - $graph_id_group = get_group_or_create_demo_group($graph_group); - - if ($graph_id_group === false) { - // Group could not be created. Skip graph creation. - continue; - } - - $values = []; - $values['description'] = $graph_description; - $values['id_group'] = $graph_id_group; - $values['name'] = $graph_name; - $values['period'] = $graph_periodicity; - $values['stacked'] = $graph_type; - - $id_graph = db_process_sql_insert('tgraph', $values); - - if ($id_graph > 0) { - // Register created graph in tdemo_data. - $values = [ - 'item_id' => $id_graph, - 'table_name' => 'tgraph', - ]; - $result = (bool) db_process_sql_insert('tdemo_data', $values); - - if ($result === false) { - // Rollback graph creation if could not be registered in tdemo_data. - db_process_sql_delete('tgraph', ['id_graph' => $id_graph]); + } else { + // Network map group could not be created. Skip creation of map. continue; } - } else { - // Graph could not be created. Skip creation of graph. - continue; - } - if (count($graph_items) > 0) { - $item_access_idx = 1; + if (count($map_items) > 0) { + $item_access_idx = 1; - while (1) { - $items_array = []; - foreach ($graph_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; - } - - $item_values = []; - - if (isset($items_array['agent_name']) === false || is_string($items_array['agent_name']) === false) { - // Agent must be defined. Skip graph item creation. - continue; - } - - if (isset($items_array['module']) === false || is_string($items_array['module']) === false) { - // Module must be defined. Skip graph item creation. - continue; - } - - $matched_agents = agents_get_agents(['nombre' => $items_array['agent_name']], ['id_agente']); - $agent_id = $matched_agents[0]['id_agente']; - - if (!($agent_id > 0)) { - continue; - } - - $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; - } - - $item_values = [ - 'id_graph' => $id_graph, - 'id_agent_module' => $module_id, - ]; - - $created_graph_item_id = db_process_sql_insert('tgraph_source', $item_values); - - if ($created_graph_item_id > 0) { - // Register created demo item in tdemo_data. - $values = [ - 'item_id' => $created_graph_item_id, - 'table_name' => 'tgraph_source', - ]; - $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('tgraph_source', ['id_gs' => $created_graph_item_id]); - continue; - } - } - } - } - - // Calculate progress. - $percentage_inc = (100 / count($parsed_ini)); - $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)); - } - - // Create reports. - foreach ($parsed_ini['reports'] as $ini_report_data) { - $report_data = $ini_report_data['report_data']; - $report_items = $ini_report_data['report_items']; - - $group_id = get_group_or_create_demo_group($report_data['group']); - - if ($group_id === false) { - // Could not create group. Skip report creation. - continue; - } - - $report_values = []; - $report_values['id_group'] = $group_id; - $report_values['name'] = $report_data['name']; - $report_values['description'] = $report_data['description']; - - $created_report_id = db_process_sql_insert('treport', $report_values); - - if ($created_report_id > 0) { - // Register created graph in tdemo_data. - $values = [ - 'item_id' => $created_report_id, - 'table_name' => 'treport', - ]; - $result = (bool) db_process_sql_insert('tdemo_data', $values); - - if ($result === false) { - // Rollback report creation if could not be registered in tdemo_data. - db_process_sql_delete('treport', ['id_report' => $created_report_id]); - continue; - } - } else { - // Report could not be created. Skip creation of map. - continue; - } - - if (count($report_items) > 0) { - $item_access_idx = 1; - - while (1) { - $items_array = []; - foreach ($report_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; - } - - $item_values = []; - - $item_values['id_report'] = $created_report_id; - - if (isset($items_array['agent_name']) === true) { - if (is_string($items_array['module']) === false) { - continue; + while (1) { + $items_array = []; + foreach ($map_items as $key => $value) { + $items_array[$key] = ($value[$item_access_idx] ?? null); } - $matched_agents = agents_get_agents(['nombre' => $items_array['agent_name']], ['id_agente']); + $item_access_idx++; - $matched_agent = $matched_agents[0]['id_agente']; - if (isset($matched_agent) === true && $matched_agent > 0) { - $item_values['id_agent'] = $matched_agent; - } else { - // Skip report item creation if agent does not exist. - continue; - } - } + $test_empty_array = array_filter($items_array); - if (isset($items_array['module']) === true) { - if (is_string($items_array['module']) === false) { - // Module wrong data type read. Skip. - continue; + if (empty($test_empty_array) === true) { + break; } - if ($item_values['id_agent'] > 0) { - $module_id = db_get_value_sql('SELECT id_agente_modulo FROM tagente_modulo WHERE nombre = "'.io_safe_input($items_array['module']).'" AND id_agente = '.$item_values['id_agent']); + $item_values = []; - if ($module_id > 0) { - $item_values['id_agent_module'] = $module_id; - } else { - // Skip report item creation if agent module does not exist. - continue; - } - } else { - continue; - } - } + $item_values['id_map'] = $id_map; - 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 (isset($items_array['agent_name']) === true || is_string($items_array['agent_name']) === false) { + $matched_agents = agents_get_agents(['nombre' => $items_array['agent_name']], ['id_agente', 'id_os', 'alias']); - if ($id_custom_graph > 0) { - $item_values['id_gs'] = $id_custom_graph; - } else { - // Skip report item creation if specified custom graph does not exist. - continue; - } - } - - $created_report_item_id = db_process_sql_insert('treport_content', $item_values); - - if ($created_report_item_id > 0) { - // Register created demo item in tdemo_data. - $values = [ - 'item_id' => $created_report_item_id, - 'table_name' => 'treport_content', - ]; - $result = (bool) db_process_sql_insert('tdemo_data', $values); - - if ($result === false) { - // Rollback report item if could not be registered in tdemo_data. - db_process_sql_delete('treport_content', ['id_rc' => $created_report_item_id]); - continue; - } - } - } - } - - // Calculate progress. - $percentage_inc = (100 / count($parsed_ini)); - $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)); - } - - // Create services. - foreach ($parsed_ini['services'] as $ini_service_data) { - $service_data = $ini_service_data['service_data']; - $service_items = $ini_service_data['service_items']; - - // Check for mandatory fields. - if (isset($service_data['name']) === false - || isset($service_data['group']) === false - ) { - continue; - } - - $id_group = get_group_or_create_demo_group($service_data['group']); - - if ($id_group === false) { - // Group could not be created. Skip graph creation. - continue; - } - - $service_values = []; - - $service_values['name'] = $service_data['name']; - $service_values['description'] = $service_data['description']; - $service_values['id_group'] = $id_group; - $service_values['critical'] = $service_data['critical']; - $service_values['warning'] = $service_data['warning']; - $service_values['auto_calculate'] = (isset($service_data['mode']) === true && (string) $service_data['mode'] === 'smart') ? 1 : 0; - - $created_service_id = db_process_sql_insert('tservice', $service_values); - - if ($created_service_id > 0) { - // Register created service in tdemo_data. - $values = [ - 'item_id' => $created_service_id, - 'table_name' => 'tservice', - ]; - $result = (bool) db_process_sql_insert('tdemo_data', $values); - - if ($result === false) { - // Rollback service creation if could not be registered in tdemo_data. - db_process_sql_delete('tservice', ['id' => $created_service_id]); - continue; - } - } else { - // Service could not be created. Skip creation of map. - continue; - } - - if (count($service_items) > 0) { - $item_access_idx = 1; - while (1) { - $items_array = []; - foreach ($service_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) { - // Service element type must be specified. - continue; - } - - $element_values = []; - - $element_values = ['id_service' => $created_service_id]; - - $element_type = (string) $items_array['type']; - - if (in_array($element_type, ['agent', 'module', 'dynamic', 'service']) === false) { - // Skip element creation if type not valid. - continue; - } - - if (in_array($element_type, ['agent', 'module', 'dynamic']) === true) { - // Get agent ID and module ID. - $matched_agents = agents_get_agents(['nombre' => $items_array['agent_name']], ['id_agente']); - $matched_agent = $matched_agents[0]['id_agente']; - - if (isset($matched_agent) === true && $matched_agent > 0) { - $element_values['id_agent'] = $matched_agent; - } else { - // Skip element creation if agent does not exist. - continue; - } - } - - if (in_array($element_type, ['module', 'dynamic']) === true) { - if ($element_values['id_agent'] > 0) { - $module_id = db_get_value_sql('SELECT id_agente_modulo FROM tagente_modulo WHERE nombre = "'.io_safe_input($items_array['module']).'" AND id_agente = '.$element_values['id_agent']); - - if ($module_id > 0) { - $element_values['id_agente_modulo'] = $module_id; - } else { - // Skip element creation if agent module does not exist. - continue; - } - } else { - continue; - } - } - - if ($element_type === 'dynamic') { - if ($service_values['auto_calculate'] === 1) { - if (isset($items_array['match']) === false - || ($items_array['match'] !== 'agent' && $items_array['match'] !== 'module') - ) { - // If failed to provide match value, 'agent' is assigned by default. - $match_value = 'agent'; - } else { - $match_value = $items_array['match']; - } - - if (isset($items_array['group']) === true) { - $group_id_value = get_group_or_create_demo_group($items_array['group']); - - if ($group_id_value === false) { - $group_id_value = -1; + $matched_agent = $matched_agents[0]['id_agente']; + $alias = $matched_agents[0]['alias']; + if (isset($matched_agent) === true && $matched_agent > 0) { + $item_values['source_data'] = $matched_agent; + if (isset($matched_agents[0]['id_agente']) === true && $matched_agents[0]['id_os']) { + $agent_os_id = $matched_agents[0]['id_os']; + $icon_name = db_get_value('icon_name', 'tconfig_os', 'id_os', $agent_os_id); } } else { - $group_id_value = -1; - } - - $element_values['id_agent'] = 0; - $element_values['id_agente_modulo'] = 0; - - $rules_arr = [ - 'dynamic_type' => $match_value, - 'group' => $group_id_value, - 'agent_name' => (isset($items_array['agent_name']) === true) ? $items_array['agent_name'] : '', - 'module_name' => (isset($items_array['module']) === true) ? $items_array['module'] : '', - 'regex_mode' => (isset($items_array['regex']) === true) ? $items_array['regex'] : false, - 'custom_fields' => [], - ]; - - $element_values['rules'] = base64_encode(json_encode($rules_arr)); - } - } - - if ($element_type === 'service') { - if (isset($items_array['service_name']) === true - && is_string($items_array['service_name']) === true - ) { - $services = services_get_services(['name' => $items_array['service_name']]); - - $service_id = $services[0]['id']; - - if ($service_id > 0) { - $element_values['id_service_child'] = $service_id; - } else { - // Skip element creation if specified service does not exist. + // Skip report item creation if agent does not exist. continue; } } else { - // Skip element creation if service name was not provided. continue; } - } - $id = db_process_sql_insert('tservice_element', $element_values); - - if ($id > 0) { - // Register created demo item in tdemo_data. - $values = [ - 'item_id' => $id, - 'table_name' => 'tservice_element', + $style_values = [ + 'shape' => 'circle', + 'image' => 'images/networkmap/'.$icon_name, + 'width' => null, + 'height' => null, + 'label' => $alias, ]; - $result = (bool) db_process_sql_insert('tdemo_data', $values); - if ($result === false) { - // Rollback service element if could not be registered in tdemo_data. - db_process_sql_delete('tservice_element', ['id' => $id]); - continue; + $item_values['style'] = json_encode($style_values); + $item_values['x'] = (isset($items_array['x']) === true) ? $items_array['x'] : '0'; + $item_values['y'] = (isset($items_array['y']) === true) ? $items_array['y'] : '0'; + + $created_nm_item_id = db_process_sql_insert('titem', $item_values); + + if ($created_nm_item_id > 0) { + // Register created demo item in tdemo_data. + $values = [ + 'item_id' => $created_nm_item_id, + 'table_name' => 'titem', + ]; + $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('titem', ['id' => $created_nm_item_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)); } - - // Calculate progress. - $percentage_inc = (100 / count($parsed_ini)); - $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)); } - // Create dashboards. - foreach ($parsed_ini['dashboards'] as $ini_data) { - $data = $ini_data['dashboard_data']; - $items = $ini_data['dashboard_items']; - - // Check for mandatory fields. - if (isset($data['name']) === false - || isset($data['group']) === false - ) { - // Name and group fields must be specified for dashbaord. - 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; - - $created_id = db_process_sql_insert('tdashboard', $insert_values); - - if ($created_id > 0) { - // Register created item in tdemo_data. - $values = [ - 'item_id' => $created_id, - 'table_name' => 'tdashboard', + if ((bool) $enabled_items['enable_cg'] === true) { + // Create graphs. + foreach ($parsed_ini['graphs'] as $ini_graph_data) { + // Constant graph types. + $graph_types = [ + 'line' => 2, + 'area' => 0, + 's_line' => 3, + 's_area' => 1, + 'h_bars' => 6, + 'v_bars' => 7, + 'gauge' => 5, + 'pie' => 8, ]; - $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('tdashboard', ['id' => $created_id]); + $graph_data = $ini_graph_data['graph_data']; + $graph_items = $ini_graph_data['graph_items']; + + $graph_name = $graph_data['name']; + $graph_group = $graph_data['group']; + $graph_description = $graph_data['description']; + $graph_type = (isset($graph_types[$graph_data['type']]) === true) ? $graph_types[$graph_data['type']] : 0; + $graph_periodicity = $graph_data['periodicity']; + + $graph_id_group = get_group_or_create_demo_group($graph_group); + + if ($graph_id_group === false) { + // Group could not be created. Skip graph creation. continue; } - } else { - // Dashboard could not be created. Skip creation of item. - continue; - } - if (count($items) > 0) { - $item_access_idx = 1; - $order = -1; - while (1) { - $items_array = []; - foreach ($items as $key => $value) { - $items_array[$key] = ($value[$item_access_idx] ?? null); - } + $values = []; + $values['description'] = $graph_description; + $values['id_group'] = $graph_id_group; + $values['name'] = $graph_name; + $values['period'] = $graph_periodicity; + $values['stacked'] = $graph_type; - $item_access_idx++; + $id_graph = db_process_sql_insert('tgraph', $values); - $test_empty_array = array_filter($items_array); + if ($id_graph > 0) { + // Register created graph in tdemo_data. + $values = [ + 'item_id' => $id_graph, + 'table_name' => 'tgraph', + ]; + $result = (bool) db_process_sql_insert('tdemo_data', $values); - if (empty($test_empty_array) === true) { - break; - } - - if (isset($items_array['type']) === false || isset($items_array['title']) === false) { - // All dashboard widgets must have a type and a title. + if ($result === false) { + // Rollback graph creation if could not be registered in tdemo_data. + db_process_sql_delete('tgraph', ['id_graph' => $id_graph]); continue; } + } else { + // Graph could not be created. Skip creation of graph. + continue; + } - // Get ID of widget type. Skip if it does not exist. - $type_id = db_get_value('id', 'twidget', 'unique_name', $items_array['type']); + if (count($graph_items) > 0) { + $item_access_idx = 1; - if (!($type_id > 0)) { - continue; - } + while (1) { + $items_array = []; + foreach ($graph_items as $key => $value) { + $items_array[$key] = ($value[$item_access_idx] ?? null); + } - $title = io_safe_input($items_array['title']); + $item_access_idx++; - if ($items_array['type'] === 'single_graph') { - if (isset($items_array['agent_name']) === false - || isset($items_array['module']) === false - ) { - // The above fields are required for this item. + $test_empty_array = array_filter($items_array); + + if (empty($test_empty_array) === true) { + break; + } + + $item_values = []; + + if (isset($items_array['agent_name']) === false || is_string($items_array['agent_name']) === false) { + // Agent must be defined. Skip graph item creation. continue; - } + } + + if (isset($items_array['module']) === false || is_string($items_array['module']) === false) { + // Module must be defined. Skip graph item creation. + continue; + } $matched_agents = agents_get_agents(['nombre' => $items_array['agent_name']], ['id_agente']); $agent_id = $matched_agents[0]['id_agente']; @@ -1281,204 +812,989 @@ if ($action === 'create_demo_data') { continue; } - $element_values = []; - - $options_data = [ - 'title' => $title, - 'background' => '#ffffff', - 'agentId' => "$agent_id", - 'metaconsoleId' => 0, - 'moduleId' => "$module_id", - 'period' => (isset($items_array['interval']) === false) ? $items_array['interval'] : '86400', - 'showLegend' => 1, - 'projection_switch' => false, - 'period_projection' => '300', + $item_values = [ + 'id_graph' => $id_graph, + 'id_agent_module' => $module_id, ]; - $order++; - } + $created_graph_item_id = db_process_sql_insert('tgraph_source', $item_values); - if ($items_array['type'] === 'custom_graph') { - if (isset($items_array['graph_name']) === false - || isset($items_array['graph_type']) === false - ) { - // The above fields are required for this item. - continue; - } + if ($created_graph_item_id > 0) { + // Register created demo item in tdemo_data. + $values = [ + 'item_id' => $created_graph_item_id, + 'table_name' => 'tgraph_source', + ]; + $result = (bool) db_process_sql_insert('tdemo_data', $values); - // Try to get graph and skip if not exists. - $id_graph = db_get_value('id_graph', 'tgraph', 'name', $items_array['graph_name']); - - if (!($id_graph > 0)) { - continue; - } - - $graph_types = [ - 'line' => 2, - 'area' => 0, - 's_line' => 3, - 's_area' => 1, - 'h_bars' => 6, - 'v_bars' => 7, - 'gauge' => 5, - 'pie' => 8, - ]; - - if (isset($graph_types[$items_array['graph_type']]) === true) { - $graph_type_id = $items_array['graph_type']; - } else { - // Specified graph type is not a valid one. - continue; - } - - $element_values = []; - - $options_data = [ - 'title' => $title, - 'background' => '#ffffff', - 'id_graph' => $id_graph, - 'type' => $graph_type_id, - 'period' => (isset($items_array['interval']) === false) ? $items_array['interval'] : 86400, - 'showLegend' => 1, - ]; - - $order++; - } - - if ($items_array['type'] === 'reports') { - if (isset($items_array['report_name']) === false) { - // The above fields are required for this item. - continue; - } - - $id_report = reports_get_reports(['name' => $items_array['report_name']], ['id_report'])[0]['id_report']; - - if (!($id_report > 0)) { - continue; - } - - $element_values = []; - - $options_data = [ - 'title' => $title, - 'background' => '#ffffff', - 'reportId' => $id_report, - ]; - - $order++; - } - - if ($items_array['type'] === 'network_map') { - if (isset($items_array['map_name']) === false) { - // The above fields are required for this item. - continue; - } - - $id_map = db_get_value('id', 'tmap', 'name', $items_array['map_name']); - - if (!($id_map > 0)) { - continue; - } - - $element_values = []; - - $options_data = [ - 'title' => $title, - 'background' => '#ffffff', - 'networkmapId' => "$id_map", - 'xOffset' => '0', - 'yOffset' => '0', - 'zoomLevel' => 0.5, - ]; - - $order++; - } - - if ($items_array['type'] === 'service_map') { - if (isset($items_array['service_name']) === false) { - // The above fields are required for this item. - continue; - } - - $services = services_get_services(['name' => $items_array['service_name']]); - - $service_id = $services[0]['id']; - - if (!($service_id > 0)) { - continue; - } - - $element_values = []; - - $options_data = [ - 'title' => $title, - 'background' => '#ffffff', - 'serviceId' => "$service_id", - 'sunburst' => 0, - ]; - - $order++; - } - - $item_x = $items_array['x']; - $item_y = $items_array['y']; - $item_width = $items_array['width']; - $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", - ]; - - $element_values = [ - 'position' => json_encode($position_data), - 'options' => json_encode($options_data), - 'order' => $order, - 'id_dashboard' => $created_id, - 'id_widget' => $type_id, - 'prop_width' => 0.32, - 'prop_height' => 0.32, - ]; - - $id = db_process_sql_insert('twidget_dashboard', $element_values); - - if ($id > 0) { - // Register created demo item in tdemo_data. - $values = [ - 'item_id' => $id, - 'table_name' => 'twidget_dashboard', - ]; - $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('twidget_dashboard', ['id' => $id]); - continue; + if ($result === false) { + // Rollback demo item if could not be registered in tdemo_data. + db_process_sql_delete('tgraph_source', ['id_gs' => $created_graph_item_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)); } - - // Calculate progress. - $percentage_inc = (100 / count($parsed_ini)); - $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)); } + if ((bool) $enabled_items['enable_rep'] === true) { + // Create reports. + foreach ($parsed_ini['reports'] as $ini_report_data) { + $report_data = $ini_report_data['report_data']; + $report_items = $ini_report_data['report_items']; + + $group_id = get_group_or_create_demo_group($report_data['group']); + + if ($group_id === false) { + // Could not create group. Skip report creation. + continue; + } + + $report_values = []; + $report_values['id_group'] = $group_id; + $report_values['name'] = $report_data['name']; + $report_values['description'] = $report_data['description']; + + $created_report_id = db_process_sql_insert('treport', $report_values); + + if ($created_report_id > 0) { + // Register created graph in tdemo_data. + $values = [ + 'item_id' => $created_report_id, + 'table_name' => 'treport', + ]; + $result = (bool) db_process_sql_insert('tdemo_data', $values); + + if ($result === false) { + // Rollback report creation if could not be registered in tdemo_data. + db_process_sql_delete('treport', ['id_report' => $created_report_id]); + continue; + } + } else { + // Report could not be created. Skip creation of map. + continue; + } + + if (count($report_items) > 0) { + $item_access_idx = 1; + + while (1) { + $items_array = []; + foreach ($report_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; + } + + $item_values = []; + + $item_values['id_report'] = $created_report_id; + + if (isset($items_array['agent_name']) === true) { + if (is_string($items_array['module']) === false) { + continue; + } + + $matched_agents = agents_get_agents(['nombre' => $items_array['agent_name']], ['id_agente']); + + $matched_agent = $matched_agents[0]['id_agente']; + if (isset($matched_agent) === true && $matched_agent > 0) { + $item_values['id_agent'] = $matched_agent; + } else { + // Skip report item creation if agent does not exist. + continue; + } + } + + if (isset($items_array['module']) === true) { + if (is_string($items_array['module']) === false) { + // Module wrong data type read. Skip. + continue; + } + + if ($item_values['id_agent'] > 0) { + $module_id = db_get_value_sql('SELECT id_agente_modulo FROM tagente_modulo WHERE nombre = "'.io_safe_input($items_array['module']).'" AND id_agente = '.$item_values['id_agent']); + + if ($module_id > 0) { + $item_values['id_agent_module'] = $module_id; + } else { + // Skip report item creation if agent module does not exist. + continue; + } + } else { + continue; + } + } + + 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) { + $item_values['id_gs'] = $id_custom_graph; + } else { + // Skip report item creation if specified custom graph does not exist. + continue; + } + } + + $created_report_item_id = db_process_sql_insert('treport_content', $item_values); + + if ($created_report_item_id > 0) { + // Register created demo item in tdemo_data. + $values = [ + 'item_id' => $created_report_item_id, + 'table_name' => 'treport_content', + ]; + $result = (bool) db_process_sql_insert('tdemo_data', $values); + + if ($result === false) { + // Rollback report item if could not be registered in tdemo_data. + db_process_sql_delete('treport_content', ['id_rc' => $created_report_item_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)); + } + } + + if ((bool) $enabled_items['enable_services'] === true) { + // Create services. + foreach ($parsed_ini['services'] as $ini_service_data) { + $service_data = $ini_service_data['service_data']; + $service_items = $ini_service_data['service_items']; + + // Check for mandatory fields. + if (isset($service_data['name']) === false + || isset($service_data['group']) === false + ) { + continue; + } + + $id_group = get_group_or_create_demo_group($service_data['group']); + + if ($id_group === false) { + // Group could not be created. Skip graph creation. + continue; + } + + $service_values = []; + + $service_values['name'] = $service_data['name']; + $service_values['description'] = $service_data['description']; + $service_values['id_group'] = $id_group; + $service_values['critical'] = $service_data['critical']; + $service_values['warning'] = $service_data['warning']; + $service_values['auto_calculate'] = (isset($service_data['mode']) === true && (string) $service_data['mode'] === 'smart') ? 1 : 0; + + $created_service_id = db_process_sql_insert('tservice', $service_values); + + if ($created_service_id > 0) { + // Register created service in tdemo_data. + $values = [ + 'item_id' => $created_service_id, + 'table_name' => 'tservice', + ]; + $result = (bool) db_process_sql_insert('tdemo_data', $values); + + if ($result === false) { + // Rollback service creation if could not be registered in tdemo_data. + db_process_sql_delete('tservice', ['id' => $created_service_id]); + continue; + } + } else { + // Service could not be created. Skip creation of map. + continue; + } + + if (count($service_items) > 0) { + $item_access_idx = 1; + while (1) { + $items_array = []; + foreach ($service_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) { + // Service element type must be specified. + continue; + } + + $element_values = []; + + $element_values = ['id_service' => $created_service_id]; + + $element_type = (string) $items_array['type']; + + if (in_array($element_type, ['agent', 'module', 'dynamic', 'service']) === false) { + // Skip element creation if type not valid. + continue; + } + + if (in_array($element_type, ['agent', 'module', 'dynamic']) === true) { + // Get agent ID and module ID. + $matched_agents = agents_get_agents(['nombre' => $items_array['agent_name']], ['id_agente']); + $matched_agent = $matched_agents[0]['id_agente']; + + if (isset($matched_agent) === true && $matched_agent > 0) { + $element_values['id_agent'] = $matched_agent; + } else { + // Skip element creation if agent does not exist. + continue; + } + } + + if (in_array($element_type, ['module', 'dynamic']) === true) { + if ($element_values['id_agent'] > 0) { + $module_id = db_get_value_sql('SELECT id_agente_modulo FROM tagente_modulo WHERE nombre = "'.io_safe_input($items_array['module']).'" AND id_agente = '.$element_values['id_agent']); + + if ($module_id > 0) { + $element_values['id_agente_modulo'] = $module_id; + } else { + // Skip element creation if agent module does not exist. + continue; + } + } else { + continue; + } + } + + if ($element_type === 'dynamic') { + if ($service_values['auto_calculate'] === 1) { + if (isset($items_array['match']) === false + || ($items_array['match'] !== 'agent' && $items_array['match'] !== 'module') + ) { + // If failed to provide match value, 'agent' is assigned by default. + $match_value = 'agent'; + } else { + $match_value = $items_array['match']; + } + + if (isset($items_array['group']) === true) { + $group_id_value = get_group_or_create_demo_group($items_array['group']); + + if ($group_id_value === false) { + $group_id_value = -1; + } + } else { + $group_id_value = -1; + } + + $element_values['id_agent'] = 0; + $element_values['id_agente_modulo'] = 0; + + $rules_arr = [ + 'dynamic_type' => $match_value, + 'group' => $group_id_value, + 'agent_name' => (isset($items_array['agent_name']) === true) ? $items_array['agent_name'] : '', + 'module_name' => (isset($items_array['module']) === true) ? $items_array['module'] : '', + 'regex_mode' => (isset($items_array['regex']) === true) ? $items_array['regex'] : false, + 'custom_fields' => [], + ]; + + $element_values['rules'] = base64_encode(json_encode($rules_arr)); + } + } + + if ($element_type === 'service') { + if (isset($items_array['service_name']) === true + && is_string($items_array['service_name']) === true + ) { + $services = services_get_services(['name' => $items_array['service_name']]); + + $service_id = $services[0]['id']; + + if ($service_id > 0) { + $element_values['id_service_child'] = $service_id; + } else { + // Skip element creation if specified service does not exist. + continue; + } + } else { + // Skip element creation if service name was not provided. + continue; + } + } + + $id = db_process_sql_insert('tservice_element', $element_values); + + if ($id > 0) { + // Register created demo item in tdemo_data. + $values = [ + 'item_id' => $id, + 'table_name' => 'tservice_element', + ]; + $result = (bool) db_process_sql_insert('tdemo_data', $values); + + if ($result === false) { + // Rollback service element if could not be registered in tdemo_data. + db_process_sql_delete('tservice_element', ['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)); + } + } + + if ((bool) $enabled_items['enable_dashboards'] === true) { + // Create dashboards. + foreach ($parsed_ini['dashboards'] as $ini_data) { + $data = $ini_data['dashboard_data']; + $items = $ini_data['dashboard_items']; + + // Check for mandatory fields. + if (isset($data['name']) === false + || isset($data['group']) === false + ) { + // Name and group fields must be specified for dashbaord. + 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; + + $created_id = db_process_sql_insert('tdashboard', $insert_values); + + if ($created_id > 0) { + // Register created item in tdemo_data. + $values = [ + 'item_id' => $created_id, + 'table_name' => 'tdashboard', + ]; + $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('tdashboard', ['id' => $created_id]); + continue; + } + } else { + // Dashboard could not be created. Skip creation of item. + continue; + } + + if (count($items) > 0) { + $item_access_idx = 1; + $order = -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 || isset($items_array['title']) === false) { + // All dashboard widgets must have a type and a title. + continue; + } + + // Get ID of widget type. Skip if it does not exist. + $type_id = db_get_value('id', 'twidget', 'unique_name', $items_array['type']); + + if (!($type_id > 0)) { + continue; + } + + $title = io_safe_input($items_array['title']); + + if ($items_array['type'] === 'single_graph') { + if (isset($items_array['agent_name']) === false + || isset($items_array['module']) === false + ) { + // The above fields are required for this item. + continue; + } + + $matched_agents = agents_get_agents(['nombre' => $items_array['agent_name']], ['id_agente']); + $agent_id = $matched_agents[0]['id_agente']; + + if (!($agent_id > 0)) { + continue; + } + + $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 = []; + + $options_data = [ + 'title' => $title, + 'background' => '#ffffff', + 'agentId' => "$agent_id", + 'metaconsoleId' => 0, + 'moduleId' => "$module_id", + 'period' => (isset($items_array['interval']) === false) ? $items_array['interval'] : '86400', + 'showLegend' => 1, + 'projection_switch' => false, + 'period_projection' => '300', + ]; + + $order++; + } + + if ($items_array['type'] === 'custom_graph') { + if (isset($items_array['graph_name']) === false + || isset($items_array['graph_type']) === false + ) { + // The above fields are required for this item. + continue; + } + + // Try to get graph and skip if not exists. + $id_graph = db_get_value('id_graph', 'tgraph', 'name', $items_array['graph_name']); + + if (!($id_graph > 0)) { + continue; + } + + $graph_types = [ + 'line' => 2, + 'area' => 0, + 's_line' => 3, + 's_area' => 1, + 'h_bars' => 6, + 'v_bars' => 7, + 'gauge' => 5, + 'pie' => 8, + ]; + + if (isset($graph_types[$items_array['graph_type']]) === true) { + $graph_type_id = $items_array['graph_type']; + } else { + // Specified graph type is not a valid one. + continue; + } + + $element_values = []; + + $options_data = [ + 'title' => $title, + 'background' => '#ffffff', + 'id_graph' => $id_graph, + 'type' => $graph_type_id, + 'period' => (isset($items_array['interval']) === false) ? $items_array['interval'] : 86400, + 'showLegend' => 1, + ]; + + $order++; + } + + if ($items_array['type'] === 'reports') { + if (isset($items_array['report_name']) === false) { + // The above fields are required for this item. + continue; + } + + $id_report = reports_get_reports(['name' => $items_array['report_name']], ['id_report'])[0]['id_report']; + + if (!($id_report > 0)) { + continue; + } + + $element_values = []; + + $options_data = [ + 'title' => $title, + 'background' => '#ffffff', + 'reportId' => $id_report, + ]; + + $order++; + } + + if ($items_array['type'] === 'network_map') { + if (isset($items_array['map_name']) === false) { + // The above fields are required for this item. + continue; + } + + $id_map = db_get_value('id', 'tmap', 'name', $items_array['map_name']); + + if (!($id_map > 0)) { + continue; + } + + $element_values = []; + + $options_data = [ + 'title' => $title, + 'background' => '#ffffff', + 'networkmapId' => "$id_map", + 'xOffset' => '0', + 'yOffset' => '0', + 'zoomLevel' => 0.5, + ]; + + $order++; + } + + if ($items_array['type'] === 'service_map') { + if (isset($items_array['service_name']) === false) { + // The above fields are required for this item. + continue; + } + + $services = services_get_services(['name' => $items_array['service_name']]); + + $service_id = $services[0]['id']; + + if (!($service_id > 0)) { + continue; + } + + $element_values = []; + + $options_data = [ + 'title' => $title, + 'background' => '#ffffff', + 'serviceId' => "$service_id", + 'sunburst' => 0, + ]; + + $order++; + } + + $item_x = $items_array['x']; + $item_y = $items_array['y']; + $item_width = $items_array['width']; + $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", + ]; + + $element_values = [ + 'position' => json_encode($position_data), + 'options' => json_encode($options_data), + 'order' => $order, + 'id_dashboard' => $created_id, + 'id_widget' => $type_id, + 'prop_width' => 0.32, + 'prop_height' => 0.32, + ]; + + $id = db_process_sql_insert('twidget_dashboard', $element_values); + + if ($id > 0) { + // Register created demo item in tdemo_data. + $values = [ + 'item_id' => $id, + 'table_name' => 'twidget_dashboard', + ]; + $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('twidget_dashboard', ['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)); + } + } + + 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']]; From c7770ec4f1e05794d0fb426fc36e9a139cc22f3d Mon Sep 17 00:00:00 2001 From: "alejandro.campos@artica.es" Date: Thu, 23 Nov 2023 17:59:22 +0100 Subject: [PATCH 06/42] implement demo data load --- pandora_console/godmode/setup/demo.php | 794 ++++--- .../include/ajax/demo_data.ajax.php | 2083 +++++++++++------ pandora_console/include/constants.php | 9 + 3 files changed, 1821 insertions(+), 1065 deletions(-) diff --git a/pandora_console/godmode/setup/demo.php b/pandora_console/godmode/setup/demo.php index daf400003d..b1caf6f9cc 100644 --- a/pandora_console/godmode/setup/demo.php +++ b/pandora_console/godmode/setup/demo.php @@ -34,320 +34,496 @@ if (users_is_admin() === false) { return; } -config_update_value('demo_data_load_progress', 0); - html_print_input_hidden('demo_items_count', 0); +$display_loading = (bool) get_parameter('display_loading', 0); + $agents_num = (int) get_parameter('agents_num', 30); -$submit_value = (string) get_parameter('update_button', ''); +$create_data = (bool) get_parameter('create_data', false); +$delete_data = (bool) get_parameter('delete_data', false); $def_value = 0; -if ($submit_value === '') { +if ($create_data === false) { $def_value = 1; } +$adv_options_is_enabled = get_parameter('toggle_adv_opts', 0); +$days_hist_data = (int) get_parameter('days_hist_data', 15); +$interval = get_parameter('interval', 0); + +// Map directory and demo item ID. +$dir_item_id_map = [ + DEMO_CUSTOM_GRAPH => 'graphs', + DEMO_NETWORK_MAP => 'network_maps', + DEMO_SERVICE => 'services', + DEMO_REPORT => 'reports', + DEMO_DASHBOARD => 'dashboards', + DEMO_VISUAL_CONSOLE => 'visual_consoles', +]; + $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), + 'graphs' => (int) get_parameter('enable_cg', $def_value), + 'network_maps' => (int) get_parameter('enable_nm', $def_value), + 'services' => (int) get_parameter('enable_services', $def_value), + 'reports' => (int) get_parameter('enable_rep', $def_value), + 'dashboards' => (int) get_parameter('enable_dashboards', $def_value), + 'visual_consoles' => (int) get_parameter('enable_vc', $def_value), + 'enable_history' => (int) get_parameter('enable_history', 0), ]; -$demo_items_count = db_get_value('count(*)', 'tdemo_data'); -$demo_agents_count = db_get_value('count(*)', 'tdemo_data', 'table_name', 'tagente'); +$demo_items_count = (int) db_get_value('count(*)', 'tdemo_data'); -// Basic/Advanced mode. -$mode = (string) get_parameter('mode', 'basic'); - -$buttons = []; - -// Draws header. -$buttons['basic'] = [ - 'active' => false, - 'text' => ''.html_print_image( - 'images/setup.png', - true, - [ - 'title' => __('General'), - 'class' => 'invert_filter', - ] - ).'', -]; - -$buttons['advanced'] = [ - 'active' => false, - 'text' => ''.html_print_image( - 'images/key.png', - true, - [ - 'title' => __('Advanced'), - 'class' => 'invert_filter', - ] - ).'', -]; - -// Header. -ui_print_standard_header( - __('Demo data'), - 'images/custom_field.png', +$current_progress_val = db_get_value_filter( + 'value', + 'tconfig', + ['token' => 'demo_data_load_progress'], + 'AND', false, - '', - true, - $buttons, - [ - [ - 'link' => '', - 'label' => __('Setup'), - ], - [ - 'link' => '', - 'label' => __('Demo data'), - ], - ] + false ); -$table_aux = new stdClass(); -$table_aux->id = 'table-demo'; -$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%'; +$current_progress_val_delete = db_get_value_filter( + 'value', + 'tconfig', + ['token' => 'demo_data_delete_progress'], + 'AND', + false, + false +); -if ($mode === 'advanced') { - $arraySelectIcon = [ - 10 => '10', - 30 => '30', - 50 => '50', - 500 => '500', - 1000 => '1000', - 2000 => '2000', - ]; -} else { - $arraySelectIcon = [ - 10 => '10', - 30 => '30', - 50 => '50', - ]; -} +$running_create = ($current_progress_val > 0 && $current_progress_val < 100); +$running_delete = ($current_progress_val_delete > 0 && $current_progress_val_delete < 100); -$agent_num = (int) get_parameter('agents_num'); +// Real time loading. +if ($display_loading === true || $running_create === true || $running_delete) { + $table_load = new stdClass(); + $table_load->id = 'table-demo'; + $table_load->class = 'filter-table-adv'; + $table_load->width = '100%'; + $table_load->data = []; + $table_load->size = []; + $table_load->size[0] = '50%'; + $table_load->size[1] = '50%'; -$otherData = []; -$table_aux->data['row1'][] = html_print_label_input_block( - __('Agents'), + $table_load->data['row0'][] = 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 ($create_data === true || $running_create === true) { + // Map demo item ID to display name in page. + $items_ids_text_map = [ + DEMO_AGENT => 'agents', + DEMO_SERVICE => 'services', + DEMO_NETWORK_MAP => 'network maps', + DEMO_CUSTOM_GRAPH => 'custom graphs', + DEMO_REPORT => 'custom reports', + DEMO_VISUAL_CONSOLE => 'visual consoles', + DEMO_DASHBOARD => 'dashboards', + ]; + + if ($adv_options_is_enabled === true) { + $enabled_keys = array_keys(array_filter($enabled_items)); + + $items_ids_text_map = array_filter( + $items_ids_text_map, + function ($k) use ($dir_item_id_map, $enabled_keys) { + return in_array($dir_item_id_map[$k], $enabled_keys); + }, + ARRAY_FILTER_USE_KEY + ); + + array_merge([DEMO_AGENT => 'agents'], $items_ids_text_map); + } + + $list_mkup = '
    '; + foreach ($items_ids_text_map as $item_id => $item_text) { + $list_mkup .= '
  • '; + $list_mkup .= '
    '.html_print_image( + 'images/icono-unknown.png', + true, + [ + 'title' => __('View'), + 'class' => 'icon w100p h100p', + ] + ).'
    '; + $list_mkup .= 'Create demo '.$item_text.''; + $list_mkup .= '
      '; + $list_mkup .= '
    '; + $list_mkup .= '
  • '; + } + + $list_mkup .= '
'; + } + + echo '
'; + echo '
'; + echo ''.__('Progress').''; + html_print_table($table_load); + echo $list_mkup; + + echo '
'; + + if ($create_data === true || $running_create === true) { + $btn_span = __('View summary'); + $icon = 'next'; + } else { + $btn_span = __('Back'); + $icon = 'back'; + } + + + $action_btns = html_print_action_buttons( + html_print_submit_button( + $btn_span, + 'redirect_button', + false, + [ + 'icon' => $icon, + ], + true + ), + [], + true + ); + + // Only rendered when data creation has been completed. html_print_div( [ - 'class' => '', - 'content' => html_print_select( - $arraySelectIcon, - 'agents_num', - $agents_num, + 'id' => 'action-btns-loading-done', + 'class' => 'invisible', + 'content' => $action_btns, + ] + ); + + echo '
'; +} else { + // Configuration. + if ($demo_items_count === 0) { + $table_aux = new stdClass(); + $table_aux->id = 'table-demo'; + $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%'; + + $agent_sel_values = [ + 10 => '10', + 30 => '30', + 50 => '50', + 500 => '500', + 1000 => '1000', + 2000 => '2000', + ]; + + $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( + $agent_sel_values, + 'agents_num', + $agents_num, + '', + '30', + 30, + true, + false, + true, + 'w80px' + ), + ], + true + ) + ); + + $table_aux->data['row2'][] = html_print_label_input_block( + __('Advanced options'), + html_print_checkbox_switch( + 'toggle_adv_opts', + 1, + false, + true + ) + ); + + $table_adv = new stdClass(); + $table_adv->id = 'table-adv'; + $table_adv->class = 'filter-table-adv'; + $table_adv->width = '100%'; + $table_adv->data = []; + $table_adv->size = []; + $table_adv->size[0] = '50%'; + $table_adv->size[1] = '50%'; + + $table_adv->data['row0'][] = html_print_label_input_block( + __('Generate historical data for all agents (15 days by default)'), + html_print_checkbox_switch( + 'enable_history', + 1, + false, + true + ) + ); + + $table_adv->data['row1'][] = html_print_label_input_block( + __('Days of historical data to insert in the agent data'), + html_print_input_text( + 'days_hist_data', + $days_hist_data, '', - '30', - 30, + 10, + 20, true, false, - true, + false, + '', 'w80px' - ).'  '.__('(%d demo agents currently in the system)', $demo_agents_count).'', - ], - 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); + $table_adv->data['row2'][] = html_print_label_input_block( + __('Create custom graphs'), + html_print_checkbox_switch( + 'enable_cg', + 1, + $enabled_items['graphs'], + true + ) + ); -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_historical', - 1, - true, - true - ) - ); - - $table_aux->data['row4'][] = html_print_label_input_block( - __('Create custom graphs'), - html_print_checkbox_switch( - 'enable_cg', - 1, - $enabled_items['enable_cg'], - true - ) - ); - - $table_aux->data['row5'][] = html_print_label_input_block( - __('Create network maps'), - html_print_checkbox_switch( - 'enable_nm', - 1, - $enabled_items['enable_nm'], - true - ) - ); - - $table_aux->data['row6'][] = html_print_label_input_block( - __('Create services'), - html_print_checkbox_switch( - 'enable_services', - 1, - $enabled_items['enable_services'], - true - ) - ); - - $table_aux->data['row7'][] = html_print_label_input_block( - __('Create reports'), - html_print_checkbox_switch( - 'enable_rep', - 1, - $enabled_items['enable_rep'], - true - ) - ); - - $table_aux->data['row8'][] = html_print_label_input_block( - __('Create dashboards'), - html_print_checkbox_switch( - 'enable_dashboards', - 1, - $enabled_items['enable_dashboards'], - true - ) - ); - - - $table_aux->data['row9'][] = html_print_label_input_block( - __('Create visual consoles'), - html_print_checkbox_switch( - 'enable_vc', - 1, - $enabled_items['enable_vc'], - true - ) - ); - - $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', - 15, + $interval_select = html_print_extended_select_for_time( + 'interval', + $interval, '', + '', + '0', 10, - 20, true, false, - false, - '', - 'w80px' - ) - ); - ?> + true, + 'w20p' + ); - data['row3'][] = html_print_label_input_block( + __('Interval'), + $interval_select + ); + + $table_adv->data['row4'][] = html_print_label_input_block( + __('Create network maps'), + html_print_checkbox_switch( + 'enable_nm', + 1, + $enabled_items['network_maps'], + true + ) + ); + + $table_adv->data['row5'][] = html_print_label_input_block( + __('Create services'), + html_print_checkbox_switch( + 'enable_services', + 1, + $enabled_items['services'], + true + ) + ); + + $table_adv->data['row6'][] = html_print_label_input_block( + __('Create reports'), + html_print_checkbox_switch( + 'enable_rep', + 1, + $enabled_items['reports'], + true + ) + ); + + $table_adv->data['row7'][] = html_print_label_input_block( + __('Create dashboards'), + html_print_checkbox_switch( + 'enable_dashboards', + 1, + $enabled_items['dashboards'], + true + ) + ); + + + $table_adv->data['row8'][] = html_print_label_input_block( + __('Create visual consoles'), + html_print_checkbox_switch( + 'enable_vc', + 1, + $enabled_items['visual_consoles'], + true + ) + ); + + echo '
'; + echo '
'; + echo ''.__('Configure demo data').''; + html_print_input_hidden('create_data', 1); + html_print_input_hidden('display_loading', 1); + html_print_table($table_aux); + html_print_div( + [ + 'class' => 'invisible', + 'content' => html_print_table($table_adv), + ], + true + ); + echo '
'; + + $actionButtons = []; + + $actionButtons[] = html_print_submit_button( + __('Create demo data'), + 'create_button', + false, + [ + 'icon' => 'update', + 'fixed_id' => 'btn-create-demo-data', + ], + true + ); + + // echo ''; + + echo '
'; + } else { + // Summary data. + $demo_agents_count = (int) db_get_value('count(*)', 'tdemo_data', 'table_name', 'tagente'); + $demo_services_count = (int) db_get_value('count(*)', 'tdemo_data', 'table_name', 'tservice'); + $demo_nm_count = (int) db_get_value('count(*)', 'tdemo_data', 'table_name', 'tmap'); + $demo_cg_count = (int) db_get_value('count(*)', 'tdemo_data', 'table_name', 'tgraph'); + $demo_rep_count = (int) db_get_value('count(*)', 'tdemo_data', 'table_name', 'treport'); + $demo_vc_count = (int) db_get_value('count(*)', 'tdemo_data', 'table_name', 'tlayout'); + $demo_dashboards_count = (int) db_get_value('count(*)', 'tdemo_data', 'table_name', 'tdashboard'); + + $table_summary = new stdClass(); + $table_summary->id = 'table-summary'; + $table_summary->class = 'filter-table-adv'; + $table_summary->width = '100%'; + $table_summary->data = []; + $table_summary->size = []; + $table_summary->size[0] = '50%'; + $table_summary->size[1] = '50%'; + + $table_summary->data['row0'][0] = __('Agents'); + $table_summary->data['row0'][1] = ($demo_agents_count > 0) ? $demo_agents_count : '-'; + $table_summary->data['row1'][0] = __('Services'); + $table_summary->data['row1'][1] = ($demo_services_count > 0) ? $demo_services_count : '-'; + $table_summary->data['row2'][0] = __('Network maps'); + $table_summary->data['row2'][1] = ($demo_nm_count > 0) ? $demo_nm_count : '-'; + $table_summary->data['row3'][0] = __('Custom graphs'); + $table_summary->data['row3'][1] = ($demo_cg_count > 0) ? $demo_cg_count : '-'; + $table_summary->data['row4'][0] = __('Custom reports'); + $table_summary->data['row4'][1] = ($demo_rep_count > 0) ? $demo_rep_count : '-'; + $table_summary->data['row5'][0] = __('Visual consoles'); + $table_summary->data['row5'][1] = ($demo_vc_count > 0) ? $demo_vc_count : '-'; + $table_summary->data['row6'][0] = __('Dashboards'); + $table_summary->data['row6'][1] = ($demo_dashboards_count > 0) ? $demo_dashboards_count : '-'; + + echo '
'; + echo '
'; + echo ''.__('Active demo data summary').''; + html_print_table($table_summary); + echo '
'; + + html_print_input_hidden('delete_data', 1); + html_print_input_hidden('display_loading', 1); + + html_print_action_buttons( + html_print_submit_button( + __('Delete all demo data'), + 'delete_button', + false, + [ + 'icon' => 'delete', + 'mode' => 'secondary', + 'fixed_id' => 'btn-delete-demo-data', + ], + true + ) + ); + echo '
'; + } } -echo '
'; -echo '
'; -echo ''.__('Configure demo data').''; -html_print_input_hidden('update_config', 1); -html_print_table($table_aux); -echo '
'; - -$actionButtons = []; - -$actionButtons[] = html_print_submit_button( - __('Create demo data'), - 'update_button', - false, - [ - 'icon' => 'update', - 'fixed_id' => 'btn-create-demo-data', - ], - true -); - -$actionButtons[] = html_print_submit_button( - __('Delete demo data'), - 'update_button', - false, - [ - 'icon' => 'delete', - 'mode' => 'secondary', - 'fixed_id' => 'btn-delete-demo-data', - ], - true -); - -echo ''; - -echo '
'; ?> \ No newline at end of file diff --git a/pandora_console/include/ajax/demo_data.ajax.php b/pandora_console/include/ajax/demo_data.ajax.php index 5353a49557..d355abe41a 100644 --- a/pandora_console/include/ajax/demo_data.ajax.php +++ b/pandora_console/include/ajax/demo_data.ajax.php @@ -38,10 +38,16 @@ require $config['homedir'].'/include/functions_inventory.php'; require $config['homedir'].'/include/functions_custom_graphs.php'; require $config['homedir'].'/include/functions_reports.php'; +// Ensure script keeps running even if client-side of the AJAX request disconnected. +ignore_user_abort(true); + +set_time_limit(500); + $action = (string) get_parameter('action', null); if ($action === 'create_demo_data') { config_update_value('demo_data_load_progress', 0); + config_update_value('demo_data_load_status', '{}'); // Read agent ini files. $directories = [ @@ -56,22 +62,36 @@ if ($action === 'create_demo_data') { $demodata_directory = $config['homedir'].'/extras/demodata/'; - foreach ($directories as $directory) { + $adv_options_is_enabled = (bool) get_parameter('adv_options_is_enabled', false); + if ($adv_options_is_enabled === true) { + $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); + + $enabled_directories = array_intersect($directories, array_keys(array_filter($enabled_items))); + $enabled_directories[] = 'agents'; + } else { + $enabled_directories = $directories; + } + + foreach ($enabled_directories as $directory) { $directory_path = $demodata_directory.$directory; if (is_dir($directory_path)) { $files = scandir($directory_path); $files = array_diff($files, ['.', '..']); } + sort($files, (SORT_NATURAL | SORT_FLAG_CASE)); + foreach ($files as $file) { - $parsed_ini[$directory][] = parse_ini_file($directory_path.'/'.$file, true, INI_SCANNER_TYPED); + $parsed_ini[$directory][] = array_merge(['filename' => $file], parse_ini_file($directory_path.'/'.$file, true, INI_SCANNER_TYPED)); } } $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); + + $total_items_count = count($parsed_ini); if ($total_agents_to_create > 0) { $agents_to_create = 0; @@ -90,10 +110,20 @@ if ($action === 'create_demo_data') { $agent_created_total = 0; if ($total_agents_to_create > 0 && $agents_to_create > 0) { - while ($agent_created_total < $total_agents_to_create) { + while ($agent_created_total < ($total_agents_to_create - 1)) { + if (count($parsed_ini['agents']) === 0) { + register_error(DEMO_AGENT, __('No configuration files found or failed to parse files')); + break; + } + + ///$iter_agents_created = 0; + + // Get first server: general value for all created modules. . + $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) { - // Read blocks: agent data, modules, inventory, inventory values. + $filename = $ini_agent_data['filename']; $agent_data = $ini_agent_data['agent_data']; if (isset($agent_data['agents_number']) === true @@ -103,14 +133,28 @@ if ($action === 'create_demo_data') { continue; } + if (isset($agent_data['agent_name']) === false + || is_string($agent_data['agent_name']) === false + || isset($agent_data['agent_alias']) === false + || is_string($agent_data['agent_alias']) === false + ) { + register_error( + DEMO_AGENT, + __('Error in %s: name and/or alias is not specified or does not have a valid format. Skipping agent creation', $filename), + true + ); + continue; + } + $iter_agents_to_create = $agent_data['agents_number']; - if (($agent_created_total + $iter_agents_to_create) >= $total_agents_to_create) { + if (($agent_created_total + $iter_agents_to_create/* + $iter_agents_created*/) >= $total_agents_to_create) { // Total agents limit specified by user has been reached. break; } else { + ///hd($total_agents_to_create." - (".$agent_created_total." + ".$iter_agents_to_create." + ".$iter_agents_created.")", true); // Calculate max number of agents that can be created in this iteration until max number specified by user is reached. - $max_agents_to_limit = ($total_agents_to_create - ($agent_created_total + $iter_agents_to_create)); + $max_agents_to_limit = ($total_agents_to_create - ($agent_created_total + $iter_agents_to_create/* + $iter_agents_created*/)); } $modules_data = $ini_agent_data['modules']; @@ -129,6 +173,11 @@ if ($action === 'create_demo_data') { if (isset($address_network) === false || is_string($address_network) === false) { // Agent address is not specified or not valid. + register_error( + DEMO_AGENT, + __('Error in %s: address is not specified or does not have a valid format. Skipping agent creation', $filename), + true + ); continue; } @@ -142,10 +191,15 @@ if ($action === 'create_demo_data') { if ($id_os === false) { // Create OS if does not exist. $values = ['name' => $os_name]; - $id_os = (bool) db_process_sql_insert('tconfig_os', $values); + $id_os = db_process_sql_insert('tconfig_os', $values); if ($id_os === false) { // Could not create OS. Skip agent creation. + register_error( + DEMO_AGENT, + __('Error in %s: failed to create the specified operating system. Skipping agent creation', $filename), + true + ); continue; } @@ -166,6 +220,9 @@ if ($action === 'create_demo_data') { } $group_id = get_group_or_create_demo_group($group_name); + $agent_interval = ($adv_options_is_enabled === true) ? $interval : 300; + + $iter_agents_created = 0; // 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++) { @@ -178,13 +235,18 @@ if ($action === 'create_demo_data') { reset($os_versions); } + $values = [ + 'server_name' => $server_name + ]; + + $create_alias = $agent_data['agent_alias'].'-'.($agents_created_count[$agent_data['agent_name']] + 1); $created_agent_id = agents_create_agent( - $agent_data['agent_alias'].'-'.($agents_created_count[$agent_data['agent_name']] + 1), + $create_alias, $group_id, // Default interval. - 300, + $agent_interval, $next_ip_address, - false, + $values, true, $id_os, $os_version @@ -201,31 +263,27 @@ if ($action === 'create_demo_data') { if ($result === false) { // Rollback agent creation if could not be registered in tdemo_data. db_process_sql_delete('tagente', ['id_agente' => $created_agent_id]); + + register_error( + DEMO_AGENT, + __('Uncaught error (source %s): could not create agent %s', $filename, $create_alias), + true + ); + continue; } } else { // Could not create agent. Skip. + register_error( + DEMO_AGENT, + __('Uncaught error (source %s): could not create agent %s', $filename, $create_alias), + true + ); continue; } $agents_created_count[$agent_data['agent_name']]++; - - // Calculate progress. - $percentage_inc = (100 / ($agents_to_create * $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)); + $iter_agents_created++; // Create agent modules. $module_access_idx = 1; @@ -244,12 +302,37 @@ if ($action === 'create_demo_data') { break; } - $id_tipo = db_get_value_filter('id_tipo', 'ttipo_modulo', ['nombre' => $modules_array['type']]); + if (isset($modules_array['name']) === false + || is_string($modules_array['name']) === false + ) { + register_error( + DEMO_AGENT, + __('Error in %s: all modules must have a name. Skipping creation of item with index %d', $filename, ($module_access_idx - 1)), + true + ); + continue; + } if (isset($modules_array['type']) === false || is_string($modules_array['type']) === false ) { // Module type not defined. + register_error( + DEMO_AGENT, + __('Error in %s: module type is not specified or does not have a valid format (%s). Skipping creation of item with index %d', $filename, ($module_access_idx - 1)), + true + ); + continue; + } + + $id_tipo = db_get_value_filter('id_tipo', 'ttipo_modulo', ['nombre' => $modules_array['type']]); + + if (!($id_tipo > 0)) { + register_error( + DEMO_AGENT, + __('Error in %s: the specified module type is not defined in the system (%s). Skipping creation of item with index %d', $filename, $modules_array['name'], ($module_access_idx - 1)), + true + ); continue; } @@ -264,6 +347,7 @@ if ($action === 'create_demo_data') { 'descripcion' => $module_description, 'id_tipo_modulo' => $id_tipo, 'id_module_group' => ($modules_array['group'] ?? 0), + 'id_modulo' => 1, ]; $created_mod_id = modules_create_agent_module( @@ -284,6 +368,13 @@ if ($action === 'create_demo_data') { 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]); + + register_error( + DEMO_AGENT, + __('Uncaught error (source %s): could not create module with index %d', $filename, ($module_access_idx - 1)), + true + ); + continue; } @@ -292,8 +383,11 @@ if ($action === 'create_demo_data') { $date_time = new DateTime(); $current_date_time = $date_time->format('Y-m-d H:i:s'); - $back_periods = 10; - $period_mins = 5; + $back_periods = 1; + + if ($adv_options_is_enabled === true && $history_is_enabled === true) { + $back_periods = round(($days_hist_data * SECONDS_1DAY) / $interval); + } $utimestamp = time(); @@ -336,14 +430,64 @@ if ($action === 'create_demo_data') { 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]); - continue; } } - $utimestamp -= 300; + if ($p === 0) { + $status_values = [ + 'datos' => $data, + 'estado' => 0, + 'known_status' => 0, + 'timestamp' => $current_date_time, + 'utimestamp' => $utimestamp, + 'last_status' => 0, + 'last_known_status' => 0, + ]; + + $status_id = db_get_value( + 'id_agente_estado', + 'tagente_estado', + 'id_agente_modulo', + $created_mod_id + ); + + $update_status_res = db_process_sql_update( + 'tagente_estado', + $status_values, + ['id_agente_estado' => $status_id] + ); + + if ($update_status_res !== false) { + // Register created demo item in tdemo_data. + $values = [ + 'item_id' => $status_id, + 'table_name' => 'tagente_estado', + ]; + + $result = 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_estado', ['id_agente_estado' => $status_id]); + } + } + } + + if ($adv_options_is_enabled === true && $history_is_enabled === true) { + $utimestamp -= $interval; + } } + } else { + // Could not create module. + register_error( + DEMO_AGENT, + __('Uncaught error (source %s): could not create module with index %d', $filename, ($module_access_idx - 1)), + true + ); + + continue; } - }; + } // Create inventory modules. $module_access_idx = 1; @@ -365,6 +509,29 @@ if ($action === 'create_demo_data') { break; } + if (isset($modules_array['name']) === false + || is_string($modules_array['name']) === false + ) { + register_error( + DEMO_AGENT, + __('Error in %s: all inventory modules must have a name. Skipping creation of item with index %d', $filename, ($module_access_idx - 1)), + true + ); + } + + if (isset($modules_array['format']) === false + || is_string($modules_array['format']) === false + || isset($modules_array['values']) === false + || is_string($modules_array['values']) === false + ) { + register_error( + DEMO_AGENT, + __('Error in %s: one or more required fields (format, values) were not found for inventory module %s. Skipping creation of item with index %d', $filename, $modules_array['name'], ($module_access_idx - 1)), + true + ); + continue; + } + $values = [ 'name' => $modules_array['name'], 'data_format' => $modules_array['format'], @@ -384,8 +551,23 @@ if ($action === 'create_demo_data') { 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]); + + register_error( + DEMO_AGENT, + __('Uncaught error (source %s): could not create inventory module with index %d', $filename, ($module_access_idx - 1)), + true + ); + continue; } + } else { + register_error( + DEMO_AGENT, + __('Uncaught error (source %s): could not create inventory module with index %d', $filename, ($module_access_idx - 1)), + true + ); + + continue; } // Insert in tagent_module_inventory and tagente_datos_inventory. @@ -472,8 +654,12 @@ if ($action === 'create_demo_data') { // Create traps. $date_time = new DateTime(); $current_date_time = $date_time->format('Y-m-d H:i:s'); - $back_periods = 10; - $period_mins = 5; + $back_periods = 1; + + if ($adv_options_is_enabled === true && $history_is_enabled === true) { + $back_periods = round(($days_hist_data * SECONDS_1DAY) / $interval); + } + $utimestamp = time(); for ($p = 0; $p < $back_periods; $p++) { @@ -492,18 +678,27 @@ if ($action === 'create_demo_data') { break; } - $create_trap = false; - if (isset($traps_array['chance_percent']) === false) { - // Chance percent must be specified. + if (isset($traps_array['oid']) === false || is_string($traps_array['oid']) === false + || isset($traps_array['value']) === false || is_string($traps_array['value']) === false + || isset($traps_array['snmp_type']) === false || is_string($traps_array['snmp_type']) === false + || isset($traps_array['chance_percent']) === false || is_string($traps_array['chance_percent']) === false + ) { + register_error( + DEMO_AGENT, + __('Error in %s: all traps must have the following required fields: oid, value, snmp_type, chance_percent. Skipping creation of item with index %d', $filename, ($trap_access_idx - 1)), + true + ); continue; - } else { - $trap_creation_prob = (int) $traps_array['chance_percent']; + } - if ($trap_creation_prob > 0) { - $randomNumber = rand(1, 100); - if ($randomNumber <= $trap_creation_prob) { - $create_trap = true; - } + $create_trap = false; + + $trap_creation_prob = (int) $traps_array['chance_percent']; + + if ($trap_creation_prob > 0) { + $randomNumber = rand(1, 100); + if ($randomNumber <= $trap_creation_prob) { + $create_trap = true; } } @@ -511,16 +706,11 @@ if ($action === 'create_demo_data') { continue; } - if (isset($traps_array['value']) === false || is_string($traps_array['value']) === false) { - // Trap value must be specified. - continue; - } else { - $parsed = explode(';', $traps_array['values']); + $parsed = explode(';', $traps_array['values']); - $data = ''; - if ((string) $parsed[0] === 'RANDOM') { - $data = rand($parsed[1], $parsed[2]); - } + $data = ''; + if ((string) $parsed[0] === 'RANDOM') { + $data = rand($parsed[1], $parsed[2]); } $values = [ @@ -544,16 +734,33 @@ if ($action === 'create_demo_data') { if ($result === false) { // Rollback inventory module if could not be registered in tdemo_data. db_process_sql_delete('ttrap', ['id_module_inventory' => $created_trap_id]); + + register_error( + DEMO_AGENT, + __('Uncaught error (source %s): could not create trap with index %d', $filename, ($trap_access_idx - 1)), + true + ); + continue; } + } else { + register_error( + DEMO_AGENT, + __('Uncaught error (source %s): could not create trap with index %d', $filename, ($trap_access_idx - 1)), + true + ); } } - $date_time->sub(new DateInterval("PT{$period_mins}M")); - $current_date_time = $date_time->format('Y-m-d H:i:s'); - $utimestamp -= 300; + if ($adv_options_is_enabled === true && $history_is_enabled === true) { + $date_time->sub(new DateInterval("PT{$interval}S")); + $current_date_time = $date_time->format('Y-m-d H:i:s'); + $utimestamp -= $interval; + } } } + + update_progress($total_items_count, $total_agents_to_create, $iter_agents_created); } $agent_created_total = array_sum($agents_created_count); @@ -563,459 +770,40 @@ if ($action === 'create_demo_data') { break; } } + + update_item_checked(DEMO_AGENT); } } - 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']; - $map_items = $ini_nm_data['map_items']; - - $nm_name = $map_data['name']; - $nm_group = $map_data['group']; - $nm_description = $map_data['description']; - $nm_node_radius = $map_data['node_radius']; - - $nm_id_group = get_group_or_create_demo_group($nm_group); - - if ($nm_id_group === false) { - // Network map could not be created. Skip network map creation. - continue; - } - - $values = []; - $new_map_filter = []; - $new_map_filter['dont_show_subgroups'] = 0; - $new_map_filter['node_radius'] = $nm_node_radius; - $new_map_filter['x_offs'] = 0; - $new_map_filter['y_offs'] = 0; - $new_map_filter['z_dash'] = '0.5'; - $new_map_filter['node_sep'] = '0.25'; - $new_map_filter['rank_sep'] = '0.25'; - $new_map_filter['mindist'] = 1; - $new_map_filter['kval'] = '0.3'; - $values['filter'] = json_encode($new_map_filter); - $values['description'] = $nm_description; - $values['id_group'] = $nm_id_group; - $values['name'] = $nm_name; - - $id_map = db_process_sql_insert('tmap', $values); - - if ($id_map > 0) { - // Register created map in tdemo_data. - $values = [ - 'item_id' => $id_map, - 'table_name' => 'tmap', - ]; - $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('tmap', ['id' => $id_map]); - continue; - } - } else { - // Network map group could not be created. Skip creation of map. - continue; - } - - if (count($map_items) > 0) { - $item_access_idx = 1; - - while (1) { - $items_array = []; - foreach ($map_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; - } - - $item_values = []; - - $item_values['id_map'] = $id_map; - - if (isset($items_array['agent_name']) === true || is_string($items_array['agent_name']) === false) { - $matched_agents = agents_get_agents(['nombre' => $items_array['agent_name']], ['id_agente', 'id_os', 'alias']); - - $matched_agent = $matched_agents[0]['id_agente']; - $alias = $matched_agents[0]['alias']; - if (isset($matched_agent) === true && $matched_agent > 0) { - $item_values['source_data'] = $matched_agent; - if (isset($matched_agents[0]['id_agente']) === true && $matched_agents[0]['id_os']) { - $agent_os_id = $matched_agents[0]['id_os']; - $icon_name = db_get_value('icon_name', 'tconfig_os', 'id_os', $agent_os_id); - } - } else { - // Skip report item creation if agent does not exist. - continue; - } - } else { - continue; - } - - $style_values = [ - 'shape' => 'circle', - 'image' => 'images/networkmap/'.$icon_name, - 'width' => null, - 'height' => null, - 'label' => $alias, - ]; - - $item_values['style'] = json_encode($style_values); - $item_values['x'] = (isset($items_array['x']) === true) ? $items_array['x'] : '0'; - $item_values['y'] = (isset($items_array['y']) === true) ? $items_array['y'] : '0'; - - $created_nm_item_id = db_process_sql_insert('titem', $item_values); - - if ($created_nm_item_id > 0) { - // Register created demo item in tdemo_data. - $values = [ - 'item_id' => $created_nm_item_id, - 'table_name' => 'titem', - ]; - $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('titem', ['id' => $created_nm_item_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)); - } - } - - if ((bool) $enabled_items['enable_cg'] === true) { - // Create graphs. - foreach ($parsed_ini['graphs'] as $ini_graph_data) { - // Constant graph types. - $graph_types = [ - 'line' => 2, - 'area' => 0, - 's_line' => 3, - 's_area' => 1, - 'h_bars' => 6, - 'v_bars' => 7, - 'gauge' => 5, - 'pie' => 8, - ]; - - $graph_data = $ini_graph_data['graph_data']; - $graph_items = $ini_graph_data['graph_items']; - - $graph_name = $graph_data['name']; - $graph_group = $graph_data['group']; - $graph_description = $graph_data['description']; - $graph_type = (isset($graph_types[$graph_data['type']]) === true) ? $graph_types[$graph_data['type']] : 0; - $graph_periodicity = $graph_data['periodicity']; - - $graph_id_group = get_group_or_create_demo_group($graph_group); - - if ($graph_id_group === false) { - // Group could not be created. Skip graph creation. - continue; - } - - $values = []; - $values['description'] = $graph_description; - $values['id_group'] = $graph_id_group; - $values['name'] = $graph_name; - $values['period'] = $graph_periodicity; - $values['stacked'] = $graph_type; - - $id_graph = db_process_sql_insert('tgraph', $values); - - if ($id_graph > 0) { - // Register created graph in tdemo_data. - $values = [ - 'item_id' => $id_graph, - 'table_name' => 'tgraph', - ]; - $result = (bool) db_process_sql_insert('tdemo_data', $values); - - if ($result === false) { - // Rollback graph creation if could not be registered in tdemo_data. - db_process_sql_delete('tgraph', ['id_graph' => $id_graph]); - continue; - } - } else { - // Graph could not be created. Skip creation of graph. - continue; - } - - if (count($graph_items) > 0) { - $item_access_idx = 1; - - while (1) { - $items_array = []; - foreach ($graph_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; - } - - $item_values = []; - - if (isset($items_array['agent_name']) === false || is_string($items_array['agent_name']) === false) { - // Agent must be defined. Skip graph item creation. - continue; - } - - if (isset($items_array['module']) === false || is_string($items_array['module']) === false) { - // Module must be defined. Skip graph item creation. - continue; - } - - $matched_agents = agents_get_agents(['nombre' => $items_array['agent_name']], ['id_agente']); - $agent_id = $matched_agents[0]['id_agente']; - - if (!($agent_id > 0)) { - continue; - } - - $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; - } - - $item_values = [ - 'id_graph' => $id_graph, - 'id_agent_module' => $module_id, - ]; - - $created_graph_item_id = db_process_sql_insert('tgraph_source', $item_values); - - if ($created_graph_item_id > 0) { - // Register created demo item in tdemo_data. - $values = [ - 'item_id' => $created_graph_item_id, - 'table_name' => 'tgraph_source', - ]; - $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('tgraph_source', ['id_gs' => $created_graph_item_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)); - } - } - - if ((bool) $enabled_items['enable_rep'] === true) { - // Create reports. - foreach ($parsed_ini['reports'] as $ini_report_data) { - $report_data = $ini_report_data['report_data']; - $report_items = $ini_report_data['report_items']; - - $group_id = get_group_or_create_demo_group($report_data['group']); - - if ($group_id === false) { - // Could not create group. Skip report creation. - continue; - } - - $report_values = []; - $report_values['id_group'] = $group_id; - $report_values['name'] = $report_data['name']; - $report_values['description'] = $report_data['description']; - - $created_report_id = db_process_sql_insert('treport', $report_values); - - if ($created_report_id > 0) { - // Register created graph in tdemo_data. - $values = [ - 'item_id' => $created_report_id, - 'table_name' => 'treport', - ]; - $result = (bool) db_process_sql_insert('tdemo_data', $values); - - if ($result === false) { - // Rollback report creation if could not be registered in tdemo_data. - db_process_sql_delete('treport', ['id_report' => $created_report_id]); - continue; - } - } else { - // Report could not be created. Skip creation of map. - continue; - } - - if (count($report_items) > 0) { - $item_access_idx = 1; - - while (1) { - $items_array = []; - foreach ($report_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; - } - - $item_values = []; - - $item_values['id_report'] = $created_report_id; - - if (isset($items_array['agent_name']) === true) { - if (is_string($items_array['module']) === false) { - continue; - } - - $matched_agents = agents_get_agents(['nombre' => $items_array['agent_name']], ['id_agente']); - - $matched_agent = $matched_agents[0]['id_agente']; - if (isset($matched_agent) === true && $matched_agent > 0) { - $item_values['id_agent'] = $matched_agent; - } else { - // Skip report item creation if agent does not exist. - continue; - } - } - - if (isset($items_array['module']) === true) { - if (is_string($items_array['module']) === false) { - // Module wrong data type read. Skip. - continue; - } - - if ($item_values['id_agent'] > 0) { - $module_id = db_get_value_sql('SELECT id_agente_modulo FROM tagente_modulo WHERE nombre = "'.io_safe_input($items_array['module']).'" AND id_agente = '.$item_values['id_agent']); - - if ($module_id > 0) { - $item_values['id_agent_module'] = $module_id; - } else { - // Skip report item creation if agent module does not exist. - continue; - } - } else { - continue; - } - } - - 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) { - $item_values['id_gs'] = $id_custom_graph; - } else { - // Skip report item creation if specified custom graph does not exist. - continue; - } - } - - $created_report_item_id = db_process_sql_insert('treport_content', $item_values); - - if ($created_report_item_id > 0) { - // Register created demo item in tdemo_data. - $values = [ - 'item_id' => $created_report_item_id, - 'table_name' => 'treport_content', - ]; - $result = (bool) db_process_sql_insert('tdemo_data', $values); - - if ($result === false) { - // Rollback report item if could not be registered in tdemo_data. - db_process_sql_delete('treport_content', ['id_rc' => $created_report_item_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)); - } - } - - if ((bool) $enabled_items['enable_services'] === true) { + $services_count = count($parsed_ini['services']); + if ($services_count > 0) { // Create services. foreach ($parsed_ini['services'] as $ini_service_data) { + $filename = $ini_service_data['filename']; $service_data = $ini_service_data['service_data']; $service_items = $ini_service_data['service_items']; // Check for mandatory fields. if (isset($service_data['name']) === false + || is_string($service_data['name']) === false || isset($service_data['group']) === false + || is_string($service_data['group']) === false ) { + register_error( + DEMO_SERVICE, + __('Error in %s: name and/or group is not specified or does not have a valid format. Skipping service creation', $filename) + ); continue; } $id_group = get_group_or_create_demo_group($service_data['group']); if ($id_group === false) { - // Group could not be created. Skip graph creation. + // Group could not be created. Skip service creation. + register_error( + DEMO_SERVICE, + __('Error in %s: the specified group does not exist in the system and could not be created. Skipping service creation', $filename) + ); continue; } @@ -1041,10 +829,21 @@ if ($action === 'create_demo_data') { if ($result === false) { // Rollback service creation if could not be registered in tdemo_data. db_process_sql_delete('tservice', ['id' => $created_service_id]); + + register_error( + DEMO_SERVICE, + __('Uncaught error (source %s): could not create service %s', $filename, $service_values['name']) + ); + continue; } } else { // Service could not be created. Skip creation of map. + register_error( + DEMO_SERVICE, + __('Uncaught error (source %s): could not create service %s', $filename, $service_values['name']) + ); + continue; } @@ -1066,6 +865,10 @@ if ($action === 'create_demo_data') { if (isset($items_array['type']) === false) { // Service element type must be specified. + register_error( + DEMO_SERVICE, + __('Error in %s: all service items must have the following required fields: type. Skipping creation of item with index %d', $filename, ($item_access_idx - 1)) + ); continue; } @@ -1077,6 +880,10 @@ if ($action === 'create_demo_data') { if (in_array($element_type, ['agent', 'module', 'dynamic', 'service']) === false) { // Skip element creation if type not valid. + register_error( + DEMO_SERVICE, + __('Error in %s: the specified type of service item is not valid. All service items must have one of the following types: agent, module, dynamic, service. Skipping creation of item with index %d', $filename, ($item_access_idx - 1)) + ); continue; } @@ -1089,6 +896,10 @@ if ($action === 'create_demo_data') { $element_values['id_agent'] = $matched_agent; } else { // Skip element creation if agent does not exist. + register_error( + DEMO_SERVICE, + __('Error in %s: the specified agent does not exist in the system: %s. Skipping creation of item with index %d', $filename, $items_array['agent_name'], ($item_access_idx - 1)) + ); continue; } } @@ -1101,6 +912,10 @@ if ($action === 'create_demo_data') { $element_values['id_agente_modulo'] = $module_id; } else { // Skip element creation if agent module does not exist. + register_error( + DEMO_SERVICE, + __('Error in %s: the specified agent module does not exist in the system: %s. Skipping creation of item with index %d', $filename, $items_array['module'], ($item_access_idx - 1)) + ); continue; } } else { @@ -1157,6 +972,10 @@ if ($action === 'create_demo_data') { $element_values['id_service_child'] = $service_id; } else { // Skip element creation if specified service does not exist. + register_error( + DEMO_SERVICE, + __('Error in %s: the specified service does not exist in the system: %s. Skipping creation of item with index %d', $filename, $items_array['service_name'], ($item_access_idx - 1)) + ); continue; } } else { @@ -1178,83 +997,118 @@ if ($action === 'create_demo_data') { if ($result === false) { // Rollback service element if could not be registered in tdemo_data. db_process_sql_delete('tservice_element', ['id' => $id]); + + register_error( + DEMO_SERVICE, + __('Uncaught error (source %s): could not create service item with index %d', $filename, ($item_access_idx - 1)) + ); + continue; } + } else { + register_error( + DEMO_SERVICE, + __('Uncaught error (source %s): could not create service item with index %d', $filename, ($item_access_idx - 1)) + ); } } } - - // 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)); } + + update_progress($total_items_count, $services_count); + update_item_checked(DEMO_SERVICE); + } else { + register_error(DEMO_SERVICE, __('No configuration files found or failed to parse files')); } - if ((bool) $enabled_items['enable_dashboards'] === true) { - // Create dashboards. - foreach ($parsed_ini['dashboards'] as $ini_data) { - $data = $ini_data['dashboard_data']; - $items = $ini_data['dashboard_items']; + $nm_count = count($parsed_ini['network_maps']); + if ($nm_count > 0) { + // Create network maps. + foreach ($parsed_ini['network_maps'] as $ini_nm_data) { + $filename = $ini_nm_data['filename']; + $map_data = $ini_nm_data['map_data']; + $map_items = $ini_nm_data['map_items']; - // Check for mandatory fields. - if (isset($data['name']) === false - || isset($data['group']) === false + if (isset($map_data['name']) === false + || is_string($map_data['name']) === false + || isset($map_data['group']) === false + || is_string($map_data['group']) === false ) { - // Name and group fields must be specified for dashbaord. + register_error( + DEMO_NETWORK_MAP, + __('Error in %s: name and/or group is not specified or does not have a valid format. Skipping network map creation', $filename) + ); continue; } - $id_group = get_group_or_create_demo_group($data['group']); + $nm_name = $map_data['name']; + $nm_group = $map_data['group']; + $nm_description = (isset($map_data['description']) === true) ? $map_data['description'] : ''; + $nm_node_radius = (isset($map_data['node_radius']) === true) ? $map_data['node_radius'] : '40'; - if ($id_group === false) { - // Group could not be created. Skip dashboard creation. + $nm_id_group = get_group_or_create_demo_group($nm_group); + + if ($nm_id_group === false) { + // Group could not be created. Skip network map creation. + register_error( + DEMO_NETWORK_MAP, + __('Error in %s: the specified group does not exist in the system and could not be created. Skipping service creation', $filename) + ); continue; } - $insert_values = []; + $values = []; + $new_map_filter = []; + $new_map_filter['dont_show_subgroups'] = 0; + $new_map_filter['node_radius'] = $nm_node_radius; + $new_map_filter['x_offs'] = 0; + $new_map_filter['y_offs'] = 0; + $new_map_filter['z_dash'] = '0.5'; + $new_map_filter['node_sep'] = '0.25'; + $new_map_filter['rank_sep'] = '0.25'; + $new_map_filter['mindist'] = 1; + $new_map_filter['kval'] = '0.3'; + $values['filter'] = json_encode($new_map_filter); + $values['description'] = $nm_description; + $values['id_group'] = $nm_id_group; + $values['name'] = $nm_name; - $insert_values['name'] = io_safe_input($data['name']); - $insert_values['id_group'] = $id_group; + $id_map = db_process_sql_insert('tmap', $values); - $created_id = db_process_sql_insert('tdashboard', $insert_values); - - if ($created_id > 0) { - // Register created item in tdemo_data. + if ($id_map > 0) { + // Register created map in tdemo_data. $values = [ - 'item_id' => $created_id, - 'table_name' => 'tdashboard', + 'item_id' => $id_map, + 'table_name' => 'tmap', ]; $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('tdashboard', ['id' => $created_id]); + db_process_sql_delete('tmap', ['id' => $id_map]); + + register_error( + DEMO_NETWORK_MAP, + __('Uncaught error (source %s): could not create network map %s', $filename, $nm_name) + ); + continue; } } else { - // Dashboard could not be created. Skip creation of item. + // Network map group could not be created. Skip creation of map. + register_error( + DEMO_NETWORK_MAP, + __('Uncaught error (source %s): could not create network map %s', $filename, $nm_name) + ); continue; } - if (count($items) > 0) { + if (count($map_items) > 0) { $item_access_idx = 1; - $order = -1; + while (1) { $items_array = []; - foreach ($items as $key => $value) { + foreach ($map_items as $key => $value) { $items_array[$key] = ($value[$item_access_idx] ?? null); } @@ -1266,245 +1120,461 @@ if ($action === 'create_demo_data') { break; } - if (isset($items_array['type']) === false || isset($items_array['title']) === false) { - // All dashboard widgets must have a type and a title. - continue; - } + $item_values = []; - // Get ID of widget type. Skip if it does not exist. - $type_id = db_get_value('id', 'twidget', 'unique_name', $items_array['type']); + $item_values['id_map'] = $id_map; - if (!($type_id > 0)) { - continue; - } + if (isset($items_array['agent_name']) === true || is_string($items_array['agent_name']) === false) { + $matched_agents = agents_get_agents(['nombre' => $items_array['agent_name']], ['id_agente', 'id_os', 'alias']); - $title = io_safe_input($items_array['title']); - - if ($items_array['type'] === 'single_graph') { - if (isset($items_array['agent_name']) === false - || isset($items_array['module']) === false - ) { - // The above fields are required for this item. - continue; - } - - $matched_agents = agents_get_agents(['nombre' => $items_array['agent_name']], ['id_agente']); - $agent_id = $matched_agents[0]['id_agente']; - - if (!($agent_id > 0)) { - continue; - } - - $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 = []; - - $options_data = [ - 'title' => $title, - 'background' => '#ffffff', - 'agentId' => "$agent_id", - 'metaconsoleId' => 0, - 'moduleId' => "$module_id", - 'period' => (isset($items_array['interval']) === false) ? $items_array['interval'] : '86400', - 'showLegend' => 1, - 'projection_switch' => false, - 'period_projection' => '300', - ]; - - $order++; - } - - if ($items_array['type'] === 'custom_graph') { - if (isset($items_array['graph_name']) === false - || isset($items_array['graph_type']) === false - ) { - // The above fields are required for this item. - continue; - } - - // Try to get graph and skip if not exists. - $id_graph = db_get_value('id_graph', 'tgraph', 'name', $items_array['graph_name']); - - if (!($id_graph > 0)) { - continue; - } - - $graph_types = [ - 'line' => 2, - 'area' => 0, - 's_line' => 3, - 's_area' => 1, - 'h_bars' => 6, - 'v_bars' => 7, - 'gauge' => 5, - 'pie' => 8, - ]; - - if (isset($graph_types[$items_array['graph_type']]) === true) { - $graph_type_id = $items_array['graph_type']; + $matched_agent = $matched_agents[0]['id_agente']; + $alias = $matched_agents[0]['alias']; + if (isset($matched_agent) === true && $matched_agent > 0) { + $item_values['source_data'] = $matched_agent; + if (isset($matched_agents[0]['id_agente']) === true && $matched_agents[0]['id_os']) { + $agent_os_id = $matched_agents[0]['id_os']; + $icon_name = db_get_value('icon_name', 'tconfig_os', 'id_os', $agent_os_id); + } } else { - // Specified graph type is not a valid one. + // Skip report item creation if agent does not exist. + + register_error( + DEMO_NETWORK_MAP, + __('Error in %s: the specified agent does not exist in the system: %s. Skipping creation of item with index %d', $filename, $items_array['agent_name'], ($item_access_idx - 1)) + ); + continue; } + } else { + register_error( + DEMO_NETWORK_MAP, + __('Error in %s: all network map items must have the following required fields: agent_name. Skipping creation of item with index %d', $filename, ($item_access_idx - 1)) + ); - $element_values = []; - - $options_data = [ - 'title' => $title, - 'background' => '#ffffff', - 'id_graph' => $id_graph, - 'type' => $graph_type_id, - 'period' => (isset($items_array['interval']) === false) ? $items_array['interval'] : 86400, - 'showLegend' => 1, - ]; - - $order++; + continue; } - if ($items_array['type'] === 'reports') { - if (isset($items_array['report_name']) === false) { - // The above fields are required for this item. - continue; - } - - $id_report = reports_get_reports(['name' => $items_array['report_name']], ['id_report'])[0]['id_report']; - - if (!($id_report > 0)) { - continue; - } - - $element_values = []; - - $options_data = [ - 'title' => $title, - 'background' => '#ffffff', - 'reportId' => $id_report, - ]; - - $order++; - } - - if ($items_array['type'] === 'network_map') { - if (isset($items_array['map_name']) === false) { - // The above fields are required for this item. - continue; - } - - $id_map = db_get_value('id', 'tmap', 'name', $items_array['map_name']); - - if (!($id_map > 0)) { - continue; - } - - $element_values = []; - - $options_data = [ - 'title' => $title, - 'background' => '#ffffff', - 'networkmapId' => "$id_map", - 'xOffset' => '0', - 'yOffset' => '0', - 'zoomLevel' => 0.5, - ]; - - $order++; - } - - if ($items_array['type'] === 'service_map') { - if (isset($items_array['service_name']) === false) { - // The above fields are required for this item. - continue; - } - - $services = services_get_services(['name' => $items_array['service_name']]); - - $service_id = $services[0]['id']; - - if (!($service_id > 0)) { - continue; - } - - $element_values = []; - - $options_data = [ - 'title' => $title, - 'background' => '#ffffff', - 'serviceId' => "$service_id", - 'sunburst' => 0, - ]; - - $order++; - } - - $item_x = $items_array['x']; - $item_y = $items_array['y']; - $item_width = $items_array['width']; - $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", + $style_values = [ + 'shape' => 'circle', + 'image' => 'images/networkmap/'.$icon_name, + 'width' => null, + 'height' => null, + 'label' => $alias, ]; - $element_values = [ - 'position' => json_encode($position_data), - 'options' => json_encode($options_data), - 'order' => $order, - 'id_dashboard' => $created_id, - 'id_widget' => $type_id, - 'prop_width' => 0.32, - 'prop_height' => 0.32, - ]; + $item_values['style'] = json_encode($style_values); + $item_values['x'] = (isset($items_array['x']) === true) ? $items_array['x'] : '0'; + $item_values['y'] = (isset($items_array['y']) === true) ? $items_array['y'] : '0'; - $id = db_process_sql_insert('twidget_dashboard', $element_values); + $created_nm_item_id = db_process_sql_insert('titem', $item_values); - if ($id > 0) { + if ($created_nm_item_id > 0) { // Register created demo item in tdemo_data. $values = [ - 'item_id' => $id, - 'table_name' => 'twidget_dashboard', + 'item_id' => $created_nm_item_id, + 'table_name' => 'titem', ]; $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('twidget_dashboard', ['id' => $id]); + db_process_sql_delete('titem', ['id' => $created_nm_item_id]); + + register_error( + DEMO_NETWORK_MAP, + __('Uncaught error (source %s): could not create network map item with index %d', $filename, ($item_access_idx - 1)) + ); + continue; } + } else { + register_error( + DEMO_NETWORK_MAP, + __('Uncaught error (source %s): could not create network map item with index %d', $filename, ($item_access_idx - 1)) + ); } } } - - // 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)); } + + update_progress($total_items_count, $nm_count); + update_item_checked(DEMO_NETWORK_MAP); + } else { + register_error(DEMO_NETWORK_MAP, __('No configuration files found or failed to parse files')); } - if ((bool) $enabled_items['enable_vc'] === true) { + $cg_count = count($parsed_ini['graphs']); + if ($cg_count > 0) { + // Create graphs. + foreach ($parsed_ini['graphs'] as $ini_graph_data) { + // Constant graph types. + $graph_types = [ + 'line' => 2, + 'area' => 0, + 's_line' => 3, + 's_area' => 1, + 'h_bars' => 6, + 'v_bars' => 7, + 'gauge' => 5, + 'pie' => 8, + ]; + + $filename = $ini_graph_data['filename']; + $graph_data = $ini_graph_data['graph_data']; + $graph_items = $ini_graph_data['graph_items']; + + $graph_name = $graph_data['name']; + $graph_group = $graph_data['group']; + $graph_description = $graph_data['description']; + $graph_type = (isset($graph_types[$graph_data['type']]) === true) ? $graph_types[$graph_data['type']] : 0; + $graph_periodicity = $graph_data['periodicity']; + + $graph_id_group = get_group_or_create_demo_group($graph_group); + + if ($graph_id_group === false) { + // Group could not be created. Skip graph creation. + register_error( + DEMO_CUSTOM_GRAPH, + __('Error in %s: the specified group does not exist in the system and could not be created. Skipping service creation', $filename) + ); + continue; + } + + $values = []; + $values['description'] = $graph_description; + $values['id_group'] = $graph_id_group; + $values['name'] = $graph_name; + $values['period'] = $graph_periodicity; + $values['stacked'] = $graph_type; + + $id_graph = db_process_sql_insert('tgraph', $values); + + if ($id_graph > 0) { + // Register created graph in tdemo_data. + $values = [ + 'item_id' => $id_graph, + 'table_name' => 'tgraph', + ]; + $result = (bool) db_process_sql_insert('tdemo_data', $values); + + if ($result === false) { + // Rollback graph creation if could not be registered in tdemo_data. + db_process_sql_delete('tgraph', ['id_graph' => $id_graph]); + + register_error( + DEMO_CUSTOM_GRAPH, + __('Uncaught error (source %s): could not create custom graph %s', $filename, $graph_name) + ); + + continue; + } + } else { + // Graph could not be created. Skip creation of graph. + register_error( + DEMO_CUSTOM_GRAPH, + __('Uncaught error (source %s): could not create custom graph %s', $filename, $graph_name) + ); + continue; + } + + if (count($graph_items) > 0) { + $item_access_idx = 1; + + while (1) { + $items_array = []; + foreach ($graph_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; + } + + $item_values = []; + + if (isset($items_array['agent_name']) === false + || is_string($items_array['agent_name']) === false + || isset($items_array['module']) === false + || is_string($items_array['module']) === false + ) { + // Agent and module must be defined. Skip graph item creation. + register_error( + DEMO_CUSTOM_GRAPH, + __('Error in %s: one or more required fields (agent_name, module) were not found for custom graph item with index %d. Skipping creation of item with index %d', $filename, $item_access_idx, ($item_access_idx - 1)) + ); + continue; + } + + + $matched_agents = agents_get_agents(['nombre' => $items_array['agent_name']], ['id_agente']); + $agent_id = $matched_agents[0]['id_agente']; + + if (!($agent_id > 0)) { + register_error( + DEMO_CUSTOM_GRAPH, + __('Error in %s: the specified agent does not exist in the system: %s. Skipping creation of item with index %d', $filename, $items_array['agent_name'], ($item_access_idx - 1)) + ); + continue; + } + + $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)) { + register_error( + DEMO_CUSTOM_GRAPH, + __('Error in %s: the specified agent module does not exist in the system: %s. Skipping creation of item with index %d', $filename, $items_array['module'], ($item_access_idx - 1)) + ); + continue; + } + + $item_values = [ + 'id_graph' => $id_graph, + 'id_agent_module' => $module_id, + ]; + + $created_graph_item_id = db_process_sql_insert('tgraph_source', $item_values); + + if ($created_graph_item_id > 0) { + // Register created demo item in tdemo_data. + $values = [ + 'item_id' => $created_graph_item_id, + 'table_name' => 'tgraph_source', + ]; + $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('tgraph_source', ['id_gs' => $created_graph_item_id]); + + register_error( + DEMO_CUSTOM_GRAPH, + __('Uncaught error (source %s): could not create custom graph item with index %d', $filename, ($item_access_idx - 1)) + ); + + continue; + } + } else { + register_error( + DEMO_CUSTOM_GRAPH, + __('Uncaught error (source %s): could not create custom graph item with index %d', $filename, ($item_access_idx - 1)) + ); + } + } + } + } + + update_progress($total_items_count, $cg_count); + update_item_checked(DEMO_CUSTOM_GRAPH); + } else { + register_error(DEMO_CUSTOM_GRAPH, __('No configuration files found or failed to parse files')); + } + + $rep_count = count($parsed_ini['reports']); + if ($rep_count > 0) { + // Create reports. + foreach ($parsed_ini['reports'] as $ini_report_data) { + $filename = $ini_report_data['filename']; + $report_data = $ini_report_data['report_data']; + $report_items = $ini_report_data['report_items']; + + // Check for mandatory fields. + if (isset($report_data['name']) === false + || is_string($report_data['name']) === false + || isset($report_data['group']) === false + || is_string($report_data['group']) === false + ) { + register_error( + DEMO_REPORT, + __('Error in %s: name and/or group is not specified or does not have a valid format. Skipping custom report creation', $filename) + ); + } + + $group_id = get_group_or_create_demo_group($report_data['group']); + + if ($group_id === false) { + // Could not create group. Skip report creation. + register_error( + DEMO_REPORT, + __('Error in %s: the specified group does not exist in the system and could not be created. Skipping service creation', $filename) + ); + continue; + } + + $report_values = []; + $report_values['id_group'] = $group_id; + $report_values['name'] = $report_data['name']; + $report_values['description'] = $report_data['description']; + + $created_report_id = db_process_sql_insert('treport', $report_values); + + if ($created_report_id > 0) { + // Register created graph in tdemo_data. + $values = [ + 'item_id' => $created_report_id, + 'table_name' => 'treport', + ]; + $result = (bool) db_process_sql_insert('tdemo_data', $values); + + if ($result === false) { + // Rollback report creation if could not be registered in tdemo_data. + db_process_sql_delete('treport', ['id_report' => $created_report_id]); + + register_error( + DEMO_REPORT, + __('Uncaught error (source %s): could not create custom report %s', $filename, $report_data['name']) + ); + + continue; + } + } else { + // Report could not be created. Skip creation of map. + register_error( + DEMO_REPORT, + __('Uncaught error (source %s): could not create custom report %s', $filename, $report_data['name']) + ); + continue; + } + + if (count($report_items) > 0) { + $item_access_idx = 1; + + while (1) { + $items_array = []; + foreach ($report_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 + || is_string($items_array['type']) === false) { + // All report items must have a type. + register_error( + DEMO_REPORT, + __('Error in %s: all custom report items must have the following required fields: type. Skipping creation of item with index %d', $filename, ($item_access_idx - 1)) + ); + continue; + } + + $item_values = []; + + $item_values['id_report'] = $created_report_id; + + if (isset($items_array['agent_name']) === true) { + if (isset($items_array['module']) === false + || is_string($items_array['module']) === false + ) { + continue; + } + + $matched_agents = agents_get_agents(['nombre' => $items_array['agent_name']], ['id_agente']); + + $matched_agent = $matched_agents[0]['id_agente']; + if (isset($matched_agent) === true && $matched_agent > 0) { + $item_values['id_agent'] = $matched_agent; + } else { + // Skip report item creation if agent does not exist. + register_error( + DEMO_REPORT, + __('Error in %s: the specified agent does not exist in the system: %s. Skipping creation of item with index %d', $filename, $items_array['agent_name'], ($item_access_idx - 1)) + ); + continue; + } + } + + if (isset($items_array['module']) === true) { + if (is_string($items_array['module']) === false) { + // Module wrong data type read. Skip. + continue; + } + + if ($item_values['id_agent'] > 0) { + $module_id = db_get_value_sql('SELECT id_agente_modulo FROM tagente_modulo WHERE nombre = "'.io_safe_input($items_array['module']).'" AND id_agente = '.$item_values['id_agent']); + + if ($module_id > 0) { + $item_values['id_agent_module'] = $module_id; + } else { + // Skip report item creation if agent module does not exist. + register_error( + DEMO_REPORT, + __('Error in %s: the specified agent module does not exist in the system: %s. Skipping creation of item with index %d', $filename, $items_array['module'], ($item_access_idx - 1)) + ); + continue; + } + } else { + continue; + } + } + + 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) { + $item_values['id_gs'] = $id_custom_graph; + } else { + // Skip report item creation if specified custom graph does not exist. + register_error( + DEMO_REPORT, + __('Error in %s: the specified custom graph does not exist in the system: %s. Skipping creation of item with index %d', $filename, $items_array['graph_name'], ($item_access_idx - 1)) + ); + continue; + } + } + + $created_report_item_id = db_process_sql_insert('treport_content', $item_values); + + if ($created_report_item_id > 0) { + // Register created demo item in tdemo_data. + $values = [ + 'item_id' => $created_report_item_id, + 'table_name' => 'treport_content', + ]; + $result = (bool) db_process_sql_insert('tdemo_data', $values); + + if ($result === false) { + // Rollback report item if could not be registered in tdemo_data. + db_process_sql_delete('treport_content', ['id_rc' => $created_report_item_id]); + + register_error( + DEMO_REPORT, + __('Uncaught error (source %s): could not create custom report item with index %d', $filename, ($item_access_idx - 1)) + ); + + continue; + } + } else { + register_error( + DEMO_REPORT, + __('Uncaught error (source %s): could not create custom report item with index %d', $filename, ($item_access_idx - 1)) + ); + } + } + } + } + + update_progress($total_items_count, $rep_count); + update_item_checked(DEMO_REPORT); + } else { + register_error(DEMO_REPORT, __('No configuration files found or failed to parse files')); + } + + $vc_count = count($parsed_ini['visual_consoles']); + if ($vc_count > 0) { // Create visual consoles. foreach ($parsed_ini['visual_consoles'] as $ini_data) { + $filename = $ini_data['filename']; $data = $ini_data['visual_console_data']; $items = $ini_data['visual_console_items']; @@ -1513,6 +1583,10 @@ if ($action === 'create_demo_data') { || isset($data['group']) === false ) { // Name and group fields must be specified for vc. + register_error( + DEMO_VISUAL_CONSOLE, + __('Error in %s: name and/or group is not specified or does not have a valid format. Skipping visual console creation', $filename) + ); continue; } @@ -1520,6 +1594,10 @@ if ($action === 'create_demo_data') { if ($id_group === false) { // Group could not be created. Skip dashboard creation. + register_error( + DEMO_VISUAL_CONSOLE, + __('Error in %s: the specified group does not exist in the system and could not be created. Skipping visual console creation', $filename) + ); continue; } @@ -1545,10 +1623,20 @@ if ($action === 'create_demo_data') { if ($result === false) { // Rollback demo item creation if could not be registered in tdemo_data. db_process_sql_delete('tlayout', ['id' => $created_id]); + + register_error( + DEMO_VISUAL_CONSOLE, + __('Uncaught error (source %s): could not create visual console %s', $filename, $insert_values['name']) + ); + continue; } } else { - // Dashboard could not be created. Skip creation of item. + // VC could not be created. Skip creation of item. + register_error( + DEMO_VISUAL_CONSOLE, + __('Uncaught error (source %s): could not create visual console %s', $filename, $insert_values['name']) + ); continue; } @@ -1570,6 +1658,10 @@ if ($action === 'create_demo_data') { if (isset($items_array['type']) === false) { // All visual console items must have a type. + register_error( + DEMO_VISUAL_CONSOLE, + __('Error in %s: all visual console items must have the following required fields: type. Skipping creation of item with index %d', $filename, ($item_access_idx - 1)) + ); continue; } @@ -1585,6 +1677,10 @@ if ($action === 'create_demo_data') { // Get ID of item type. Skip if it does not exist. if (isset($types[$items_array['type']]) === false) { + register_error( + DEMO_VISUAL_CONSOLE, + __('Error in %s: the specified type is not a valid one. It must be one of the following values: static_image, module_graph, custom_graph, value, label, icon. Skipping creation of item with index %d', $filename, ($item_access_idx - 1)) + ); continue; } @@ -1598,6 +1694,10 @@ if ($action === 'create_demo_data') { || 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 static image item type. Skipping creation of item with index %d', $filename, ($item_access_idx - 1)) + ); continue; } @@ -1693,6 +1793,10 @@ if ($action === 'create_demo_data') { || 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 icon item type. Skipping creation of item with index %d', $filename, ($item_access_idx - 1)) + ); continue; } @@ -1735,7 +1839,7 @@ if ($action === 'create_demo_data') { } if (isset($items_array['label']) === true) { - $element_values['label'] = io_safe_input($items_array['label']); + $element_values['label'] = io_safe_input($items_array['label']); } if (isset($items_array['label_position']) === true) { @@ -1771,29 +1875,307 @@ if ($action === 'create_demo_data') { if ($result === false) { // Rollback demo item if could not be registered in tdemo_data. db_process_sql_delete('tlayout_data', ['id' => $id]); + + register_error( + DEMO_VISUAL_CONSOLE, + __('Uncaught error (source %s): could not create visual console item with index %d', $filename, ($item_access_idx - 1)) + ); + + continue; + } + } else { + register_error( + DEMO_VISUAL_CONSOLE, + __('Uncaught error (source %s): could not create visual console item with index %d', $filename, ($item_access_idx - 1)) + ); + } + } + } + } + + update_progress($total_items_count, $vc_count); + update_item_checked(DEMO_VISUAL_CONSOLE); + } else { + register_error(DEMO_VISUAL_CONSOLE, __('No configuration files found or failed to parse files')); + } + + $dashboards_count = count($parsed_ini['dashboards']); + if ($dashboards_count > 0) { + // Create dashboards. + foreach ($parsed_ini['dashboards'] as $ini_data) { + $data = $ini_data['dashboard_data']; + $items = $ini_data['dashboard_items']; + + // Check for mandatory fields. + if (isset($data['name']) === false + || isset($data['group']) === false + ) { + // Name and group fields must be specified for dashbaord. + 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; + + $created_id = db_process_sql_insert('tdashboard', $insert_values); + + if ($created_id > 0) { + // Register created item in tdemo_data. + $values = [ + 'item_id' => $created_id, + 'table_name' => 'tdashboard', + ]; + $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('tdashboard', ['id' => $created_id]); + continue; + } + } else { + // Dashboard could not be created. Skip creation of item. + continue; + } + + if (count($items) > 0) { + $item_access_idx = 1; + $order = -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 || isset($items_array['title']) === false) { + // All dashboard widgets must have a type and a title. + continue; + } + + // Get ID of widget type. Skip if it does not exist. + $type_id = db_get_value('id', 'twidget', 'unique_name', $items_array['type']); + + if (!($type_id > 0)) { + continue; + } + + $title = io_safe_input($items_array['title']); + $element_values = []; + + if ($items_array['type'] === 'single_graph') { + if (isset($items_array['agent_name']) === false + || isset($items_array['module']) === false + ) { + // The above fields are required for this item. + continue; + } + + $matched_agents = agents_get_agents(['nombre' => $items_array['agent_name']], ['id_agente']); + $agent_id = $matched_agents[0]['id_agente']; + + if (!($agent_id > 0)) { + continue; + } + + $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; + } + + $options_data = [ + 'title' => $title, + 'background' => '#ffffff', + 'agentId' => "$agent_id", + 'metaconsoleId' => 0, + 'moduleId' => "$module_id", + 'period' => (isset($items_array['interval']) === false) ? $items_array['interval'] : '86400', + 'showLegend' => 1, + 'projection_switch' => false, + 'period_projection' => '300', + ]; + + $order++; + } + + if ($items_array['type'] === 'custom_graph') { + if (isset($items_array['graph_name']) === false + || isset($items_array['graph_type']) === false + ) { + // The above fields are required for this item. + continue; + } + + // Try to get graph and skip if not exists. + $id_graph = db_get_value('id_graph', 'tgraph', 'name', $items_array['graph_name']); + + if (!($id_graph > 0)) { + continue; + } + + $graph_types = [ + 'line' => 2, + 'area' => 0, + 's_line' => 3, + 's_area' => 1, + 'h_bars' => 6, + 'v_bars' => 7, + 'gauge' => 5, + 'pie' => 8, + ]; + + if (isset($graph_types[$items_array['graph_type']]) === true) { + $graph_type_id = $items_array['graph_type']; + } else { + // Specified graph type is not a valid one. + continue; + } + + $options_data = [ + 'title' => $title, + 'background' => '#ffffff', + 'id_graph' => $id_graph, + 'type' => $graph_type_id, + 'period' => (isset($items_array['interval']) === false) ? $items_array['interval'] : 86400, + 'showLegend' => 1, + ]; + + $order++; + } + + if ($items_array['type'] === 'reports') { + if (isset($items_array['report_name']) === false) { + // The above fields are required for this item. + continue; + } + + $id_report = reports_get_reports(['name' => $items_array['report_name']], ['id_report'])[0]['id_report']; + + if (!($id_report > 0)) { + continue; + } + + $options_data = [ + 'title' => $title, + 'background' => '#ffffff', + 'reportId' => $id_report, + ]; + + $order++; + } + + if ($items_array['type'] === 'network_map') { + if (isset($items_array['map_name']) === false) { + // The above fields are required for this item. + continue; + } + + $id_map = db_get_value('id', 'tmap', 'name', $items_array['map_name']); + + if (!($id_map > 0)) { + continue; + } + + $options_data = [ + 'title' => $title, + 'background' => '#ffffff', + 'networkmapId' => "$id_map", + 'xOffset' => '0', + 'yOffset' => '0', + 'zoomLevel' => 0.5, + ]; + + $order++; + } + + if ($items_array['type'] === 'service_map') { + if (isset($items_array['service_name']) === false) { + // The above fields are required for this item. + continue; + } + + $services = services_get_services(['name' => $items_array['service_name']]); + + $service_id = $services[0]['id']; + + if (!($service_id > 0)) { + continue; + } + + $options_data = [ + 'title' => $title, + 'background' => '#ffffff', + 'serviceId' => "$service_id", + 'sunburst' => 0, + ]; + + $order++; + } + + $item_x = $items_array['x']; + $item_y = $items_array['y']; + $item_width = $items_array['width']; + $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", + ]; + + $element_values = [ + 'position' => json_encode($position_data), + 'options' => json_encode($options_data), + 'order' => $order, + 'id_dashboard' => $created_id, + 'id_widget' => $type_id, + 'prop_width' => 0.32, + 'prop_height' => 0.32, + ]; + + $id = db_process_sql_insert('twidget_dashboard', $element_values); + + if ($id > 0) { + // Register created demo item in tdemo_data. + $values = [ + 'item_id' => $id, + 'table_name' => 'twidget_dashboard', + ]; + $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('twidget_dashboard', ['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)); } + + update_progress($total_items_count, $dashboards_count); + update_item_checked(DEMO_DASHBOARD); + } else { + register_error(DEMO_DASHBOARD, __('No configuration files found or failed to parse files')); } $demo_agents_count = db_get_value('count(*)', 'tdemo_data', 'table_name', 'tagente'); @@ -1831,24 +2213,30 @@ if ($action === 'cleanup_demo_data') { 'tdashboard' => 'id', 'tlayout' => 'id', 'tlayout_data' => 'id', + 'tagente_estado' => 'id_agente_estado', ]; $table_id_field = $table_id_field_dict[$item['table_name']]; - $result = db_process_sql_delete( + $result1 = db_process_sql_delete( $item['table_name'], [$table_id_field => $item['item_id']] ); - if ($result !== false) { - db_process_sql_delete( + if ($result1 !== false) { + $result2 = db_process_sql_delete( 'tdemo_data', ['item_id' => $item['item_id']] ); } } - echo 1; + if ($result1 !== false && $result1 !== false) { + echo 1; + return; + } + + echo 0; } if ($action === 'get_progress_bar') { @@ -1856,22 +2244,47 @@ if ($action === 'get_progress_bar') { if ($operation === 'create') { $current_progress_val = db_get_value_filter('value', 'tconfig', ['token' => 'demo_data_load_progress']); + $demo_data_load_status = db_get_value_filter('value', 'tconfig', ['token' => 'demo_data_load_status']); if ($current_progress_val === false) { $current_progress_val = 0; } + + // Clean up checked items status and errors associated to them every time check items are obtained. + /*$demo_data_load_status_decoded = json_decode(io_safe_output($demo_data_load_status), true); + foreach ($demo_data_load_status_decoded['checked_items'] as $item_id) { + $demo_data_load_status_decoded['errors'][$item_id] = []; + } + + $demo_data_load_status_decoded['checked_items'] = []; + + config_update_value('demo_data_load_status', json_encode($demo_data_load_status_decoded));*/ + + $ret = [ + 'current_progress_val' => $current_progress_val, + 'demo_data_load_status' => json_decode(io_safe_output($demo_data_load_status), true), + ]; } 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); + config_update_value('demo_data_delete_progress', $current_progress_val); + $ret = ['current_progress_val' => $current_progress_val]; } - echo $current_progress_val; + echo json_encode($ret); return; } +/** + * AUXILIARY FUNCTION: Calculate and return next host address within subnet given a CIDR-formatted address. + * + * @param string $ip CIDR IP address. + * + * @return string Next host address. + */ function calculateNextHostAddress($ip) { list($network, $subnet) = explode('/', $ip); @@ -1892,6 +2305,14 @@ function calculateNextHostAddress($ip) } +/** + * AUXILIARY FUNCTION: Try to return the group ID of a group given its name or create + * and return its ID if it does not exist. + * + * @param string $name Group name. + * + * @return mixed group ID or false if group not found and could not be created. + */ function get_group_or_create_demo_group($name) { if (is_string($name) === false) { @@ -1928,6 +2349,11 @@ function get_group_or_create_demo_group($name) } +/** + * AUXILIARY FUNCTION: Generate and return a randomly generated MAC address. + * + * @return string Random MAC address string. + */ function generateRandomMacAddress() { $macAddress = []; @@ -1942,3 +2368,118 @@ function generateRandomMacAddress() return $randomMacAddress; } + + +function update_progress($total_items_count, $total_type_items_count, $created_num=1) { + // Calculate progress. + $percentage_inc = (($created_num * 100) / ($total_type_items_count * $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; + } + + $new_val = ($current_progress_val + $percentage_inc); + + if ((int) round($new_val) === 100) { + $new_val = 100; + } + + config_update_value('demo_data_load_progress', $new_val); +} + + +function update_item_checked($item_id) { + $current_load_status_data = db_get_value_filter( + 'value', + 'tconfig', + ['token' => 'demo_data_load_status'], + 'AND', + false, + false + ); + + if ($current_load_status_data === false || $current_load_status_data === '{}') { + $current_load_data_arr = []; + } else { + $current_load_data_arr = json_decode(io_safe_output($current_load_status_data), true); + } + + $current_load_data_arr['checked_items'][] = $item_id; + + config_update_value('demo_data_load_status', json_encode($current_load_data_arr)); +} + + +function register_error( + $item_id, + $error_msg, + $search_for_repeated_msgs=false +) { + $current_load_status_data = db_get_value_filter( + 'value', + 'tconfig', + ['token' => 'demo_data_load_status'], + 'AND', + false, + false + ); + + if ($current_load_status_data === false || $current_load_status_data === '{}') { + $current_load_data_arr = []; + } else { + $current_load_data_arr = json_decode(io_safe_output($current_load_status_data), true); + } + + if ($search_for_repeated_msgs === true && isset($current_load_data_arr['errors'][$item_id]) === true) { + $matching_string = null; + $msg_key = array_search($error_msg, $current_load_data_arr['errors'][$item_id]); + + if ($msg_key === false) { + foreach ($current_load_data_arr['errors'][$item_id] as $key => $string) { + // Use regular expression to check if the part after "(...) " matches the error string. + if (preg_match('/\s*\((.*?)\)\s*(.*)/', $string, $matches)) { + $rest_of_string = $matches[2]; + + if ($rest_of_string === $error_msg) { + $matching_string = $string; + $msg_key = $key; + break; + } + } + } + + if ($matching_string === null) { + // String not found. + $current_load_data_arr['errors'][$item_id][] = $error_msg; + } else { + // Count parentheses string was found, then replace number. + $new_error_msg = preg_replace_callback( + '/\((\d+)\)/', + function ($matches) { + $currentNumber = $matches[1]; + $newNumber = ($currentNumber + 1); + return "($newNumber)"; + }, + $matching_string + ); + $current_load_data_arr['errors'][$item_id][$msg_key] = $new_error_msg; + } + } else { + // String without count found. + $new_error_msg = '(2) '.$error_msg; + $current_load_data_arr['errors'][$item_id][$msg_key] = $new_error_msg; + } + } else { + $current_load_data_arr['errors'][$item_id][] = $error_msg; + } + + config_update_value('demo_data_load_status', json_encode($current_load_data_arr)); +} diff --git a/pandora_console/include/constants.php b/pandora_console/include/constants.php index 2dba4790e3..e6919f21f0 100644 --- a/pandora_console/include/constants.php +++ b/pandora_console/include/constants.php @@ -893,3 +893,12 @@ define('HOME_SCREEN_DASHBOARD', 'dashboard'); // Alert correlation. define('EVENT_ALERTS', 1); define('LOG_ALERTS', 2); + +// Demo items IDs. +define('DEMO_AGENT', 1); +define('DEMO_NETWORK_MAP', 2); +define('DEMO_CUSTOM_GRAPH', 3); +define('DEMO_REPORT', 4); +define('DEMO_SERVICE', 5); +define('DEMO_DASHBOARD', 6); +define('DEMO_VISUAL_CONSOLE', 7); From cdc77b5f7047fe75aaab92b853e7e7f72b35e1b9 Mon Sep 17 00:00:00 2001 From: Enrique Martin Date: Fri, 24 Nov 2023 10:24:46 +0100 Subject: [PATCH 07/42] Added demo agents plugin --- .../util/plugin/pandora_demo_agents.pl | 666 ++++++++++++++++++ 1 file changed, 666 insertions(+) create mode 100644 pandora_server/util/plugin/pandora_demo_agents.pl diff --git a/pandora_server/util/plugin/pandora_demo_agents.pl b/pandora_server/util/plugin/pandora_demo_agents.pl new file mode 100644 index 0000000000..8469a84a93 --- /dev/null +++ b/pandora_server/util/plugin/pandora_demo_agents.pl @@ -0,0 +1,666 @@ +#!/usr/bin/perl +################################################################################ +# Author: Enrique Martin Garcia +# Copyright: 2023, PandoraFMS +# Maintainer: Operations department +# Version: 1.0 +################################################################################ + +use strict; +use warnings; + +use lib '/usr/lib/perl5'; + +use POSIX qw(strftime); +use Scalar::Util qw(looks_like_number); +use Digest::MD5 qw(md5_hex); +use PandoraFMS::PluginTools qw(empty trim print_agent); + +## +# GLOBALS +################## + +my $timestamp = strftime '%Y-%m-%d %H:%M:%S', localtime; +my ($sec,$min,$hour,$mday,$mon,$year)=localtime(time); +$year += 1900; +$mon += 1; + +my @sorted_ini; +my @agents_indexes; +my @block_agents_created_count; +my $current_ini = 0; + +my $result = 1; + +my $errors = ''; + +## +# FUNCTIONS +################## + +sub help() { + print STDERR "$0 [agents_seconds_interval] [traps_ip] [traps_community] [tentacle_ip] [tentacle_port] [tentacle_extra_opts]\n"; +} + +sub result($) { + my ($res) = @_; + + if($errors ne '') { + print STDERR $errors; + } + + print $res . "\n"; +} + +sub add_error($) { + my ($err) = @_; + + $errors .= '[ERROR] ' . $err . "\n"; +} + +sub error($) { + my ($msg) = @_; + + print STDERR '[ERROR] ' . $msg . "\n"; + result(0); + exit; +} + +sub alphanumerically { + my ($num_a, $num_b) = map { /(\d+)[^\d]*$/ ? $1 : 0 } ($a, $b); + $num_a <=> $num_b; +} + +sub parse_ini_file { + my ($file_path) = @_; + + open my $fh, '<', $file_path or return undef; + + my %ini_data; + my %ini_indexes; + my $current_section = ''; + + while (my $line = <$fh>) { + chomp $line; + + # Skip comments and empty lines + next if $line =~ /^\s*#/ || $line =~ /^\s*$/; + + # Match section headers + if ($line =~ /^\s*\[(\w+)\]\s*$/) { + $current_section = $1; + next; + } + + my $key; + my $index; + my $value; + + # Match key-value pairs + # key=value + if ($line =~ /^\s*(\w+)\s*=\s*[\"\']?\s*(.+?)\s*[\"\']?\s*$/) { + $key = $1; + $value = $2; + + # Store in the hash + $ini_data{$current_section}{$key} = $value; + + # Match key-array values pairs + # key[]=value + } elsif ($line =~ /^\s*(\w+)\[\]\s*=\s*[\"\']?\s*(.+?)\s*[\"\']?\s*$/) { + $key = $1; + $value = $2; + + # Create HASH if not defined or key is not HASH + if (!defined($ini_data{$current_section}{$key}) || ref($ini_data{$current_section}{$key}) ne "HASH") { + $ini_data{$current_section}{$key} = {}; + $ini_indexes{$current_section}{$key} = 0; + } + + # Get dynamic index + $index = $ini_indexes{$current_section}{$key}; + + # Store in the hash + $ini_data{$current_section}{$key}{$index} = $value; + + # Set new dynamic index + while(defined($ini_data{$current_section}{$key}{$ini_indexes{$current_section}{$key}})) { + $ini_indexes{$current_section}{$key}++; + } + + # Match indexed key-array values pairs + # key[index]=value + } elsif ($line =~ /^\s*(\w+)\[([\w\d]+)\]\s*=\s*[\"\']?\s*(.+?)\s*[\"\']?\s*$/) { + $key = $1; + $index = $2; + $value = $3; + + # Create HASH if not defined or key is not HASH + if (!defined($ini_data{$current_section}{$key}) || ref($ini_data{$current_section}{$key}) ne "HASH") { + $ini_data{$current_section}{$key} = {}; + $ini_indexes{$current_section}{$key} = 0; + } + + # Store in the hash + $ini_data{$current_section}{$key}{$index} = $value; + + # Set new dynamic index + if(looks_like_number($index) && $index == $ini_indexes{$current_section}{$key}) { + $ini_indexes{$current_section}{$key}++; + } + + # Not a valid line, INI bad format + } else { + return undef; + } + } + + close $fh; + + # Verify ini content + if(!defined($ini_data{'agent_data'})) { + return undef; + } + + if(!defined($ini_data{'agent_data'}{'agent_name'})) { + return undef; + } + + # Initialize agent_data keys + if(!defined($ini_data{'agent_data'}{'agents_number'}) || !looks_like_number($ini_data{'agent_data'}{'agents_number'})) { + $ini_data{'agent_data'}{'agents_number'} = 1; + } + + if(!defined($ini_data{'agent_data'}{'agent_alias'})) { + $ini_data{'agent_data'}{'agent_alias'} = $ini_data{'agent_data'}{'agent_name'}; + } + + if(!defined($ini_data{'agent_data'}{'group'})) { + $ini_data{'agent_data'}{'group'} = ''; + } + + # Initialize modules keys + if(!defined($ini_data{'modules'})) { + $ini_data{'modules'} = {}; + } + + if(!defined($ini_data{'modules'}{'name'})) { + $ini_data{'modules'}{'name'} = {}; + } + + # Initialize inventory keys + if(!defined($ini_data{'inventory'})) { + $ini_data{'inventory'} = {}; + } + + if(!defined($ini_data{'inventory'}{'name'})) { + $ini_data{'inventory'}{'name'} = {}; + } + + if(!defined($ini_data{'inventory_values'})) { + $ini_data{'inventory_values'} = {}; + } + + # Initialize traps keys + if(!defined($ini_data{'traps'})) { + $ini_data{'traps'} = {}; + } + + if(!defined($ini_data{'traps'}{'oid'})) { + $ini_data{'traps'}{'oid'} = {}; + } + + return %ini_data; +} + +sub get_bool($) { + my ($false_chance) = @_; + + $false_chance = 0 if $false_chance < 0; + $false_chance = 100 if $false_chance > 100; + + return int(rand(100)) + 1 <= $false_chance ? 0 : 1; +} + +sub get_value($) { + my ($value_rule) = @_; + + my $value = 1; + + my @rule_parts = split(/;/, $value_rule); + + if($rule_parts[0] eq 'RANDOM') { + my $min = 0; + if(defined($rule_parts[1])) { + $min = $rule_parts[1]; + } + my $max = 100; + if(defined($rule_parts[2])) { + $max = $rule_parts[2]; + } + + $value = $min + rand($max - $min); + + } elsif($rule_parts[0] eq 'PROC') { + my $error_percent = 0; + if(defined($rule_parts[1])) { + $error_percent = $rule_parts[1]; + } + + $value = get_bool($error_percent); + } + + return int($value); +} + +sub daily_match($$$) { + my ($conf, $m_hour, $m_min) = @_; + my $match = 0; + + if($hour == $m_hour && $min - ($conf->{'agents_interval'} / 60) < $m_min) { + $match = 1; + } + + return $match; +} + +sub ip_to_long { + my $ip = shift; + my @ip_parts = split(/\./, $ip); + return ($ip_parts[0] << 24) + ($ip_parts[1] << 16) + ($ip_parts[2] << 8) + $ip_parts[3]; +} + +sub long_to_ip { + my $long = shift; + return join('.', ($long >> 24) & 255, ($long >> 16) & 255, ($long >> 8) & 255, $long & 255); +} + +sub get_broadcast_ip_long { + my ($base_ip, $subnet_mask) = @_; + my $ip_long = ip_to_long($base_ip); + my $broadcast_ip_long = ($ip_long | (2**(32 - $subnet_mask) - 1)); + return $broadcast_ip_long; +} + +sub get_network_ip_long { + my ($base_ip, $subnet_mask) = @_; + return ip_to_long($base_ip); +} + +sub next_ip($$) { + my ($network , $counter) = @_; + + # Get base IP and subnet mask + my ($base_ip, $subnet_mask) = split('/', $network); + + # If subnet mask is 32 there is only 1 IP + if($subnet_mask == 32){ + return $base_ip; + } + + # Get broadcast and network IPs long + my $broadcast_ip = get_broadcast_ip_long($base_ip, $subnet_mask); + my $network_ip = get_network_ip_long($base_ip, $subnet_mask); + + # Get next IP + my $next_ip = $network_ip + $counter; + + # Keep rotating until next IP is below broadcast + while ($next_ip >= $broadcast_ip) { + $counter = $next_ip - $broadcast_ip + 1; + $next_ip = $network_ip + $counter; + } + + return long_to_ip($next_ip); +} + +sub transfer_xml($$$) { + my ($conf, $xml, $name) = @_; + my $file_name; + my $short_filename; + my $file_path; + + if ($xml =~ /\n/ || ! -f $xml) { + # Not a file, it's content. + if (! (empty ($name))) { + $file_name = $name . "." . sprintf("%d",time()) . ".data"; + } + else { + # Inherit file name + ($file_name) = $xml =~ /\s+agent_name='(.*?)'\s+.*$/m; + if (empty($file_name)){ + ($file_name) = $xml =~ /\s+agent_name="(.*?)"\s+.*$/m; + } + if (empty($file_name)){ + $file_name = trim(`hostname`); + } + + # Tentacle server does not allow files with symbols in theirs name. + $file_name =~ s/[^a-zA-Z0-9_-]//g; + $short_filename = $file_name; + $file_name .= "." . sprintf("%d",time()) . ".data"; + } + + if (empty($file_name)) { + return (0, "Failed to generate file name"); + } + + $conf->{temp} = $conf->{tmp} if (empty($conf->{temp}) && defined($conf->{tmp})); + $conf->{temp} = $conf->{temporal} if (empty($conf->{temp}) && defined($conf->{temporal})); + $conf->{temp} = $conf->{__system}->{tmp} if (empty($conf->{temp}) && defined($conf->{__system})) && (ref($conf->{__system}) eq "HASH"); + $conf->{temp} = '/tmp' if empty($conf->{temp}); + + $file_path = $conf->{temp} . "/" . $file_name; + + #Creating XML file in temp directory + + while ( -e $file_path ) { + sleep (1); + $file_name = $short_filename . "." . sprintf("%d",time()) . ".data"; + $file_path = $conf->{temp} . "/" . $file_name; + } + + my $r = open (my $FD, ">>", $file_path); + + if (empty($r)) { + return (0, "Cannot write XML [" . $file_path . "]"); + } + + my $bin_opts = ':raw:encoding(UTF8)'; + + if ($^O eq "Windows") { + $bin_opts .= ':crlf'; + } + + binmode($FD, $bin_opts); + + print $FD $xml; + + close ($FD); + + } else { + $file_path = $xml; + } + + # Reassign default values if not present + $conf->{tentacle_port} = "41121" if empty($conf->{tentacle_port}); + $conf->{tentacle_opts} = "" if empty($conf->{tentacle_opts}); + + #Send using tentacle + my $msg = `tentacle_client -v -a $conf->{tentacle_ip} -p $conf->{tentacle_port} $conf->{tentacle_opts} "$file_path" 2>&1`; + my $r = $?; + + unlink ($file_path); + + if ($r != 0) { + return (0, trim($msg)); + } + + return (1, ""); +} + +sub send_snmp_trap($$) { + my ($conf, $trap) = @_; + + # Check trap chance + if (get_bool($trap->{'chance_percent'}) == 1) { + my $value = get_value($trap->{'value'}); + my $msg = `snmptrap -v 1 -c $conf->{'traps_community'} $conf->{'traps_ip'} $trap->{'oid'} $trap->{'address'} $trap->{'snmp_type'} 1 0 $trap->{'oid'} s "$value" 2>&1`; + my $r = $?; + + if ($r != 0) { + return (0, trim($msg)); + } + } + + return (1, ""); +} + +sub generate_agent($) { + my ($cfg) = @_; + + # Set current ini + if($block_agents_created_count[$current_ini] >= $sorted_ini[$current_ini]->{'agent_data'}->{'agents_number'}) { + $block_agents_created_count[$current_ini] = 0; + $current_ini++; + } + + if($current_ini >= @sorted_ini) { + $current_ini = 0; + } + + # Get agent info + my $agent; + $agent->{'agent_name'} = $sorted_ini[$current_ini]->{'agent_data'}->{'agent_name'}.'-'.$agents_indexes[$current_ini]; + $agent->{'agent_alias'} = $sorted_ini[$current_ini]->{'agent_data'}->{'agent_alias'}.'-'.$agents_indexes[$current_ini]; + $agent->{'group'} = $sorted_ini[$current_ini]->{'agent_data'}->{'group'}; + $agent->{'interval'} = $cfg->{'agents_interval'}; + $agent->{'timestamp'} = $timestamp; + + # Get modules info + my @modules; + foreach my $k (sort keys %{$sorted_ini[$current_ini]->{'modules'}->{'name'}}) { + # Set default type if not defined + if(!defined($sorted_ini[$current_ini]->{'modules'}->{'type'}->{$k})) { + $sorted_ini[$current_ini]->{'modules'}->{'type'}->{$k} = 'generic_data_string'; + } + + # Set default value if not defined + if(!defined($sorted_ini[$current_ini]->{'modules'}->{'values'}->{$k})) { + $sorted_ini[$current_ini]->{'modules'}->{'values'}->{$k} = 'RANDOM;0;100'; + } + + push (@modules, { + 'name' => $sorted_ini[$current_ini]->{'modules'}->{'name'}->{$k}, + 'type' => $sorted_ini[$current_ini]->{'modules'}->{'type'}->{$k}, + 'value' => get_value($sorted_ini[$current_ini]->{'modules'}->{'values'}->{$k}) + }); + } + + # Create XML + my $xml = print_agent({},$agent,\@modules); + + # Append inventory data to XML (only once a day at 00:00) + if (!empty($sorted_ini[$current_ini]->{'inventory'}->{'name'}) && daily_match($cfg, 0, 0)) { + + # Remove agent_data closing tag + $xml =~ s/<\/agent_data>//i; + + $xml .= "\n"; + + # Add inventory for each module + foreach my $k (sort keys %{$sorted_ini[$current_ini]->{'inventory'}->{'name'}}) { + # Only if values are defined + if(defined($sorted_ini[$current_ini]->{'inventory'}->{'values'}->{$k})) { + # Get inventory module name + my $inventory_name = $sorted_ini[$current_ini]->{'inventory'}->{'name'}->{$k}; + + $xml .= "\t\n"; + $xml .= "\t\t\n"; + + # Get inventory values keys + my @values_keys = split(/;/, $sorted_ini[$current_ini]->{'inventory'}->{'values'}->{$k}); + + my %inventory_values; + + # Parse each inventory values key for indexes + foreach my $key (@values_keys) { + # Get indexes from each key + foreach my $i (sort keys %{$sorted_ini[$current_ini]->{'inventory_values'}->{$key}}) { + $inventory_values{$i} = (); + } + } + + # Parse each inventory values key for values + foreach my $key (@values_keys) { + # Get values from each key + foreach my $v (sort keys %inventory_values) { + if (defined($sorted_ini[$current_ini]->{'inventory_values'}->{$key}->{$v})) { + push(@{$inventory_values{$v}}, $sorted_ini[$current_ini]->{'inventory_values'}->{$key}->{$v}); + } else { + push(@{$inventory_values{$v}}, ''); + } + } + } + + $xml .= "\t\t\n"; + + # Get each value string + foreach my $r (sort keys %inventory_values) { + my $inv_value = join(';', @{$inventory_values{$r}}); + $xml .= "\t\t\t\n"; + } + + $xml .= "\t\t\n"; + $xml .= "\t\n\n"; + } + } + + $xml .= "\n"; + + # Close agent_data tag again + $xml .= "\n"; + } + + # Get file name MD5 + my $file_md5 = md5_hex($agent->{'agent_name'}); + + # Send XML + my ($transfer_res, $transfer_err) = transfer_xml($cfg, $xml, $file_md5); + if ($transfer_res == 0) { + add_error("Failed to transfer XML for agent: " . $agent->{'agent_name'} . "\n\t" . $transfer_err); + $result = 0; + } + + # Get traps source address + my $traps_addr = '127.0.0.1'; + if(defined($sorted_ini[$current_ini]->{'agent_data'}->{'address_network'})) { + $traps_addr = next_ip($sorted_ini[$current_ini]->{'agent_data'}->{'address_network'}, $agents_indexes[$current_ini]); + } + + # Generate SNMP traps + foreach my $k (sort keys %{$sorted_ini[$current_ini]->{'traps'}->{'oid'}}) { + # Set default type if not defined + if(!defined($sorted_ini[$current_ini]->{'traps'}->{'snmp_type'}->{$k})) { + $sorted_ini[$current_ini]->{'traps'}->{'snmp_type'}->{$k} = '6'; + } + + # Set default value if not defined + if(!defined($sorted_ini[$current_ini]->{'traps'}->{'value'}->{$k})) { + $sorted_ini[$current_ini]->{'traps'}->{'value'}->{$k} = 'RANDOM;0;100'; + } + + # Set default chance if not defined + if(!defined($sorted_ini[$current_ini]->{'traps'}->{'chance_percent'}->{$k})) { + $sorted_ini[$current_ini]->{'traps'}->{'chance_percent'}->{$k} = '5'; + } + + my $trap; + $trap->{'oid'} = $sorted_ini[$current_ini]->{'traps'}->{'oid'}->{$k}; + $trap->{'snmp_type'} = $sorted_ini[$current_ini]->{'traps'}->{'snmp_type'}->{$k}; + $trap->{'value'} = $sorted_ini[$current_ini]->{'traps'}->{'value'}->{$k}; + $trap->{'chance_percent'} = $sorted_ini[$current_ini]->{'traps'}->{'chance_percent'}->{$k}; + $trap->{'address'} = $traps_addr; + + my ($trap_res, $trap_err) = send_snmp_trap($cfg, $trap); + if ($trap_res == 0) { + add_error("Failed to send SNMP trap for agent: " . $agent->{'agent_name'} . "\n\t" . $trap_err); + $result = 0; + } + + undef($trap); + } + + # Increase agents indexes + $agents_indexes[$current_ini]++; +} + +## +# ARGUMENTS +################## + +my $agents_files_path = $ARGV[0]; +my $total_agents = $ARGV[1]; +my $agents_interval = (defined($ARGV[2]) ? $ARGV[2] : 300); + +my $traps_ip = (defined($ARGV[3]) ? $ARGV[3] : '127.0.0.1'); +my $traps_community = (defined($ARGV[4]) ? $ARGV[4] : 'public'); + +my $tentacle_ip = (defined($ARGV[5]) ? $ARGV[5] : '127.0.0.1'); +my $tentacle_port = (defined($ARGV[6]) ? $ARGV[6] : 41121); + +my $tentacle_opts = join(' ', @ARGV[7..$#ARGV]); + +# Verify parameters + +if(!defined($agents_files_path)) { + help(); + error("Agents definition folder must be defined"); +} + +if(!defined($total_agents) || !looks_like_number($total_agents) || $total_agents <= 0) { + help(); + error("Total number of agents must be defined and a number greater than 0"); +} + +if(!-d $agents_files_path || !-r $agents_files_path) { + error("Can't access agents definition folder: " . $agents_files_path); +} + +if(!looks_like_number($agents_interval) || $agents_interval <= 0) { + $agents_interval = 300; +} + +if(!looks_like_number($tentacle_port) || $tentacle_port <= 0) { + $tentacle_port = 41121; +} + +## +# MAIN +################## + +# Set config +my $config; +$config->{'tentacle_ip'} = $tentacle_ip; +$config->{'tentacle_port'} = $tentacle_port; +$config->{'tentacle_opts'} = $tentacle_opts; +$config->{'agents_interval'} = $agents_interval; +$config->{'traps_ip'} = $traps_ip; +$config->{'traps_community'} = $traps_community; + +# Open the directory +opendir(my $dh, $agents_files_path) or error("Could not open directory '$agents_files_path': $!"); + +# Read the directory and sort the filenames numerically +my @files = sort alphanumerically map { "$agents_files_path/$_" } grep { -f "$agents_files_path/$_" } readdir($dh); + +# Close the directory +closedir($dh); + +# Parse each ini file and sort them +foreach my $file (@files) { + if (-f $file && -r $file) { + my %ini_data = parse_ini_file($file); + if (defined(\%ini_data)) { + push(@sorted_ini, \%ini_data); + push(@agents_indexes, 1); + push(@block_agents_created_count, 0); + } + } +} + +# Error if not agents definitions loaded +if (empty(@sorted_ini)) { + error("Unable to load agents definitions from folder '$agents_files_path'"); +} + +# Generate all requested agents +my $generated_agents = 0; +while ($generated_agents < $total_agents) { + generate_agent($config); + $block_agents_created_count[$current_ini]++; + $generated_agents++; +} + +# Print result +result($result); \ No newline at end of file From 26e4b46b1fa0e3661e3a0337e7e643ed9662a8c7 Mon Sep 17 00:00:00 2001 From: Enrique Martin Date: Fri, 24 Nov 2023 12:26:59 +0100 Subject: [PATCH 08/42] Removed agent_alias from agent_data --- pandora_server/util/plugin/pandora_demo_agents.pl | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pandora_server/util/plugin/pandora_demo_agents.pl b/pandora_server/util/plugin/pandora_demo_agents.pl index 8469a84a93..d58472aa66 100644 --- a/pandora_server/util/plugin/pandora_demo_agents.pl +++ b/pandora_server/util/plugin/pandora_demo_agents.pl @@ -171,10 +171,6 @@ sub parse_ini_file { $ini_data{'agent_data'}{'agents_number'} = 1; } - if(!defined($ini_data{'agent_data'}{'agent_alias'})) { - $ini_data{'agent_data'}{'agent_alias'} = $ini_data{'agent_data'}{'agent_name'}; - } - if(!defined($ini_data{'agent_data'}{'group'})) { $ini_data{'agent_data'}{'group'} = ''; } @@ -432,7 +428,7 @@ sub generate_agent($) { # Get agent info my $agent; $agent->{'agent_name'} = $sorted_ini[$current_ini]->{'agent_data'}->{'agent_name'}.'-'.$agents_indexes[$current_ini]; - $agent->{'agent_alias'} = $sorted_ini[$current_ini]->{'agent_data'}->{'agent_alias'}.'-'.$agents_indexes[$current_ini]; + $agent->{'agent_alias'} = $sorted_ini[$current_ini]->{'agent_data'}->{'agent_name'}.'-'.$agents_indexes[$current_ini]; $agent->{'group'} = $sorted_ini[$current_ini]->{'agent_data'}->{'group'}; $agent->{'interval'} = $cfg->{'agents_interval'}; $agent->{'timestamp'} = $timestamp; From a1916dc4b948cdbebf2509a53ab52fce8ee700a4 Mon Sep 17 00:00:00 2001 From: "alejandro.campos@artica.es" Date: Fri, 24 Nov 2023 12:56:21 +0100 Subject: [PATCH 09/42] implement demo data load --- pandora_console/godmode/menu.php | 8 +- pandora_console/godmode/setup/demo.php | 147 ++++++++++-------- pandora_console/godmode/setup/setup.php | 15 ++ pandora_console/images/demo_data.png | Bin 0 -> 655 bytes .../include/ajax/demo_data.ajax.php | 29 ++-- pandora_console/include/functions_agents.php | 4 +- 6 files changed, 118 insertions(+), 85 deletions(-) create mode 100644 pandora_console/images/demo_data.png diff --git a/pandora_console/godmode/menu.php b/pandora_console/godmode/menu.php index 0e41b1925b..e779ad11eb 100644 --- a/pandora_console/godmode/menu.php +++ b/pandora_console/godmode/menu.php @@ -496,6 +496,9 @@ if ($access_console_node === true) { $sub2['godmode/setup/setup§ion=welcome_tips']['text'] = __('Welcome Tips'); $sub2['godmode/setup/setup§ion=welcome_tips']['refr'] = 0; + $sub2['godmode/setup/setup§ion=demo_data']['text'] = __('Demo data'); + $sub2['godmode/setup/setup§ion=demo_data']['refr'] = 0; + if ((bool) $config['activate_gis'] === true) { $sub2['godmode/setup/setup§ion=gis']['text'] = __('Map conections GIS'); } @@ -506,12 +509,7 @@ if ($access_console_node === true) { enterprise_hook('skins_submenu'); - - $sub['godmode/setup/demo']['text'] = __('Demo data'); - $sub['godmode/setup/demo']['id'] = 'demo'; - $menu_godmode['gsetup']['sub'] = $sub; - } } diff --git a/pandora_console/godmode/setup/demo.php b/pandora_console/godmode/setup/demo.php index b1caf6f9cc..800701a2b9 100644 --- a/pandora_console/godmode/setup/demo.php +++ b/pandora_console/godmode/setup/demo.php @@ -131,9 +131,8 @@ if ($display_loading === true || $running_create === true || $running_delete) { DEMO_DASHBOARD => 'dashboards', ]; - if ($adv_options_is_enabled === true) { + if ((bool) $adv_options_is_enabled === true) { $enabled_keys = array_keys(array_filter($enabled_items)); - $items_ids_text_map = array_filter( $items_ids_text_map, function ($k) use ($dir_item_id_map, $enabled_keys) { @@ -141,10 +140,13 @@ if ($display_loading === true || $running_create === true || $running_delete) { }, ARRAY_FILTER_USE_KEY ); - - array_merge([DEMO_AGENT => 'agents'], $items_ids_text_map); } + if (enterprise_installed() === false) { + unset($items_ids_text_map[DEMO_SERVICE]); + } + + $items_ids_text_map = ([DEMO_AGENT => 'agents'] + $items_ids_text_map); $list_mkup = '
    '; foreach ($items_ids_text_map as $item_id => $item_text) { $list_mkup .= '
  • '; @@ -240,7 +242,7 @@ if ($display_loading === true || $running_create === true || $running_delete) { 'agents_num', $agents_num, '', - '30', + '', 30, true, false, @@ -271,8 +273,26 @@ if ($display_loading === true || $running_create === true || $running_delete) { $table_adv->size[0] = '50%'; $table_adv->size[1] = '50%'; + $interval_select = html_print_extended_select_for_time( + 'interval', + $interval, + '', + '', + '0', + 10, + true, + false, + true, + 'w20p' + ); + $table_adv->data['row0'][] = html_print_label_input_block( - __('Generate historical data for all agents (15 days by default)'), + __('Agents interval'), + $interval_select + ); + + $table_adv->data['row1'][] = html_print_label_input_block( + __('Generate historical data for all agents'), html_print_checkbox_switch( 'enable_history', 1, @@ -281,7 +301,7 @@ if ($display_loading === true || $running_create === true || $running_delete) { ) ); - $table_adv->data['row1'][] = html_print_label_input_block( + $table_adv->data['row2'][] = html_print_label_input_block( __('Days of historical data to insert in the agent data'), html_print_input_text( 'days_hist_data', @@ -297,33 +317,17 @@ if ($display_loading === true || $running_create === true || $running_delete) { ) ); - $table_adv->data['row2'][] = html_print_label_input_block( - __('Create custom graphs'), - html_print_checkbox_switch( - 'enable_cg', - 1, - $enabled_items['graphs'], - true - ) - ); - - $interval_select = html_print_extended_select_for_time( - 'interval', - $interval, - '', - '', - '0', - 10, - true, - false, - true, - 'w20p' - ); - - $table_adv->data['row3'][] = html_print_label_input_block( - __('Interval'), - $interval_select - ); + if (enterprise_installed() === true) { + $table_adv->data['row3'][] = html_print_label_input_block( + __('Create services'), + html_print_checkbox_switch( + 'enable_services', + 1, + $enabled_items['services'], + true + ) + ); + } $table_adv->data['row4'][] = html_print_label_input_block( __('Create network maps'), @@ -336,11 +340,11 @@ if ($display_loading === true || $running_create === true || $running_delete) { ); $table_adv->data['row5'][] = html_print_label_input_block( - __('Create services'), + __('Create custom graphs'), html_print_checkbox_switch( - 'enable_services', + 'enable_cg', 1, - $enabled_items['services'], + $enabled_items['graphs'], true ) ); @@ -356,17 +360,6 @@ if ($display_loading === true || $running_create === true || $running_delete) { ); $table_adv->data['row7'][] = html_print_label_input_block( - __('Create dashboards'), - html_print_checkbox_switch( - 'enable_dashboards', - 1, - $enabled_items['dashboards'], - true - ) - ); - - - $table_adv->data['row8'][] = html_print_label_input_block( __('Create visual consoles'), html_print_checkbox_switch( 'enable_vc', @@ -376,6 +369,16 @@ if ($display_loading === true || $running_create === true || $running_delete) { ) ); + $table_adv->data['row8'][] = html_print_label_input_block( + __('Create dashboards'), + html_print_checkbox_switch( + 'enable_dashboards', + 1, + $enabled_items['dashboards'], + true + ) + ); + echo '
    '; echo '
    '; echo ''.__('Configure demo data').''; @@ -432,20 +435,32 @@ if ($display_loading === true || $running_create === true || $running_delete) { $table_summary->size[0] = '50%'; $table_summary->size[1] = '50%'; - $table_summary->data['row0'][0] = __('Agents'); - $table_summary->data['row0'][1] = ($demo_agents_count > 0) ? $demo_agents_count : '-'; - $table_summary->data['row1'][0] = __('Services'); - $table_summary->data['row1'][1] = ($demo_services_count > 0) ? $demo_services_count : '-'; - $table_summary->data['row2'][0] = __('Network maps'); - $table_summary->data['row2'][1] = ($demo_nm_count > 0) ? $demo_nm_count : '-'; - $table_summary->data['row3'][0] = __('Custom graphs'); - $table_summary->data['row3'][1] = ($demo_cg_count > 0) ? $demo_cg_count : '-'; - $table_summary->data['row4'][0] = __('Custom reports'); - $table_summary->data['row4'][1] = ($demo_rep_count > 0) ? $demo_rep_count : '-'; - $table_summary->data['row5'][0] = __('Visual consoles'); - $table_summary->data['row5'][1] = ($demo_vc_count > 0) ? $demo_vc_count : '-'; - $table_summary->data['row6'][0] = __('Dashboards'); - $table_summary->data['row6'][1] = ($demo_dashboards_count > 0) ? $demo_dashboards_count : '-'; + $i = 0; + $table_summary->data[$i][0] = __('Agents'); + $table_summary->data[$i][1] = ($demo_agents_count > 0) ? $demo_agents_count : '-'; + $i++; + + if (enterprise_installed() === true) { + $table_summary->data[$i][0] = __('Services'); + $table_summary->data[$i][1] = ($demo_services_count > 0) ? $demo_services_count : '-'; + $i++; + } + + $i++; + $table_summary->data[$i][0] = __('Network maps'); + $table_summary->data[$i][1] = ($demo_nm_count > 0) ? $demo_nm_count : '-'; + $i++; + $table_summary->data[$i][0] = __('Custom graphs'); + $table_summary->data[$i][1] = ($demo_cg_count > 0) ? $demo_cg_count : '-'; + $i++; + $table_summary->data[$i][0] = __('Custom reports'); + $table_summary->data[$i][1] = ($demo_rep_count > 0) ? $demo_rep_count : '-'; + $i++; + $table_summary->data[$i][0] = __('Visual consoles'); + $table_summary->data[$i][1] = ($demo_vc_count > 0) ? $demo_vc_count : '-'; + $i++; + $table_summary->data[$i][0] = __('Dashboards'); + $table_summary->data[$i][1] = ($demo_dashboards_count > 0) ? $demo_dashboards_count : '-'; echo ''; echo '
    '; @@ -504,16 +519,16 @@ if ($display_loading === true || $running_create === true || $running_delete) { } }); - $('#table-adv-row1').hide(); + $('#table-adv-row2').hide(); if ($('#checkbox-enable_history').is(':checked') === true) { - $('#table-adv-row1').show(); + $('#table-adv-row2').show(); } $('#checkbox-enable_history').change(function() { if ($(this).is(':checked') === true) { - $('#table-adv-row1').show(); + $('#table-adv-row2').show(); } else { - $('#table-adv-row1').hide(); + $('#table-adv-row2').hide(); } }); diff --git a/pandora_console/godmode/setup/setup.php b/pandora_console/godmode/setup/setup.php index 0fd6bebff8..b8f41b790f 100644 --- a/pandora_console/godmode/setup/setup.php +++ b/pandora_console/godmode/setup/setup.php @@ -243,6 +243,11 @@ $buttons['welcome_tips'] = [ 'text' => ''.html_print_image('images/inventory.png', true, ['title' => __('Welcome tips'), 'class' => 'invert_filter']).'', ]; +$buttons['demo_data'] = [ + 'active' => false, + 'text' => ''.html_print_image('images/demo_data.png', true, ['title' => __('Demo data'), 'class' => 'invert_filter']).'', +]; + if ($config['activate_gis']) { $buttons['gis'] = [ 'active' => false, @@ -351,6 +356,12 @@ switch ($section) { $help_header = ''; break; + case 'demo_data': + $buttons['demo_data']['active'] = true; + $subpage = __('Demo data'); + $help_header = ''; + break; + case 'enterprise': $buttons['enterprise']['active'] = true; $subpage = __('Enterprise'); @@ -466,6 +477,10 @@ switch ($section) { include_once $config['homedir'].'/godmode/setup/welcome_tips.php'; break; + case 'demo_data': + include_once $config['homedir'].'/godmode/setup/demo.php'; + break; + default: enterprise_hook('setup_enterprise_select_tab', [$section]); break; diff --git a/pandora_console/images/demo_data.png b/pandora_console/images/demo_data.png new file mode 100644 index 0000000000000000000000000000000000000000..edc30ceaf68b42f70d5e8ae4f3043bd9c7137950 GIT binary patch literal 655 zcmeAS@N?(olHy`uVBq!ia0vp^q9Dw{#=yXMPhcvL&A^iG==e(WdjqS5++X<#}Es_xgplx z%#H%*o^BIZBv!_?O^`d7BZ_0v^xlr%2~Hl9-Tp9!HA%1>lvn6rJm{u4HI!v`Opqs+ zr?5-Tnk$#??fH{-bLX3Thv(=$dG@!~xVXA=>E)MEK65Reu)Qqc(_I?l^lqY^RjboM zHSc8?o1Len@yt5AZ0#F=&ShRa4vVH(@I3UI@@`jKkjC$@#}lSLsbQXZIx(cs%+rzo z^rmg?&fA~PcymCbaUs7@_wmQkeUI-{Prf0xW`Yb4Te^Uo(RYW!_~}1u>h@lLJ$Lc? z_il?fE>ibSvFKZ2H2dtlMZHQ_rr0qYeNiB-p2D!w*XRqw*PStX?0)OT0}m->{MM+r z#2j}wLXy?+g3zrO6Z-CnZ18>+%VUU%!3i7R+;@VUl};e3D{s!;F%|!qYd7+8qDbbo;Tx%T-&1 zI*+8S4g3AbX_}-=UHu#7lh $server_name + 'server_name' => $server_name, + 'id_os' => $id_os, + 'os_version' => $os_version, ]; $create_alias = $agent_data['agent_alias'].'-'.($agents_created_count[$agent_data['agent_name']] + 1); @@ -247,9 +256,7 @@ if ($action === 'create_demo_data') { $agent_interval, $next_ip_address, $values, - true, - $id_os, - $os_version + true ); if ($created_agent_id > 0) { @@ -775,7 +782,7 @@ if ($action === 'create_demo_data') { } } - $services_count = count($parsed_ini['services']); + $services_count = count($parsed_ini['services'] ?? []); if ($services_count > 0) { // Create services. foreach ($parsed_ini['services'] as $ini_service_data) { @@ -1021,7 +1028,7 @@ if ($action === 'create_demo_data') { register_error(DEMO_SERVICE, __('No configuration files found or failed to parse files')); } - $nm_count = count($parsed_ini['network_maps']); + $nm_count = count($parsed_ini['network_maps'] ?? []); if ($nm_count > 0) { // Create network maps. foreach ($parsed_ini['network_maps'] as $ini_nm_data) { @@ -1203,7 +1210,7 @@ if ($action === 'create_demo_data') { register_error(DEMO_NETWORK_MAP, __('No configuration files found or failed to parse files')); } - $cg_count = count($parsed_ini['graphs']); + $cg_count = count($parsed_ini['graphs'] ?? []); if ($cg_count > 0) { // Create graphs. foreach ($parsed_ini['graphs'] as $ini_graph_data) { @@ -1375,7 +1382,7 @@ if ($action === 'create_demo_data') { register_error(DEMO_CUSTOM_GRAPH, __('No configuration files found or failed to parse files')); } - $rep_count = count($parsed_ini['reports']); + $rep_count = count($parsed_ini['reports'] ?? []); if ($rep_count > 0) { // Create reports. foreach ($parsed_ini['reports'] as $ini_report_data) { @@ -1570,7 +1577,7 @@ if ($action === 'create_demo_data') { register_error(DEMO_REPORT, __('No configuration files found or failed to parse files')); } - $vc_count = count($parsed_ini['visual_consoles']); + $vc_count = count($parsed_ini['visual_consoles'] ?? []); if ($vc_count > 0) { // Create visual consoles. foreach ($parsed_ini['visual_consoles'] as $ini_data) { @@ -1899,7 +1906,7 @@ if ($action === 'create_demo_data') { register_error(DEMO_VISUAL_CONSOLE, __('No configuration files found or failed to parse files')); } - $dashboards_count = count($parsed_ini['dashboards']); + $dashboards_count = count($parsed_ini['dashboards'] ?? []); if ($dashboards_count > 0) { // Create dashboards. foreach ($parsed_ini['dashboards'] as $ini_data) { diff --git a/pandora_console/include/functions_agents.php b/pandora_console/include/functions_agents.php index 04174569a1..a7115496ed 100644 --- a/pandora_console/include/functions_agents.php +++ b/pandora_console/include/functions_agents.php @@ -218,9 +218,7 @@ function agents_create_agent( $interval, $ip_address, $values=false, - $alias_as_name=false, - $os=false, - $os_version=false + $alias_as_name=false ) { global $config; From 8849e8040641d6f742fff2edf00a382153e44dc9 Mon Sep 17 00:00:00 2001 From: Enrique Martin Date: Tue, 28 Nov 2023 12:59:11 +0100 Subject: [PATCH 10/42] Added demodata PRD files --- .../extras/demodata/agents/0-demo-agent.prd | 26 ++ .../extras/demodata/agents/1-linux.prd | 145 +++++++++ .../extras/demodata/agents/2-windows.prd | 114 +++++++ .../extras/demodata/agents/3-macosx.prd | 70 +++++ .../extras/demodata/agents/4-freebsd.prd | 69 +++++ .../extras/demodata/agents/5-cisco.prd | 293 ++++++++++++++++++ .../demodata/dashboards/1-dashboard.prd | 43 +++ .../extras/demodata/graphs/1-cpu-graph.prd | 21 ++ .../extras/demodata/graphs/2-disks-graph.prd | 21 ++ .../demodata/graphs/3-network-usage-graph.prd | 27 ++ .../demodata/network_maps/1-networkmap.prd | 97 ++++++ .../extras/demodata/reports/1-sla-report.prd | 47 +++ .../demodata/reports/2-graphs-report.prd | 50 +++ .../demodata/services/1-mysql-service.prd | 26 ++ .../demodata/services/2-http-service.prd | 26 ++ .../demodata/services/3-parent-service.prd | 16 + .../services/4-sunburst-linux-service.prd | 17 + .../services/5-sunburst-windows-service.prd | 17 + .../services/6-sunburst-macos-service.prd | 17 + .../services/7-sunburst-freebsd-service.prd | 17 + .../services/8-sunburst-cisco-service.prd | 17 + .../services/9-sunburst-parent-service.prd | 26 ++ .../1-linux-visual-console.prd | 150 +++++++++ 23 files changed, 1352 insertions(+) create mode 100644 pandora_console/extras/demodata/agents/0-demo-agent.prd create mode 100644 pandora_console/extras/demodata/agents/1-linux.prd create mode 100644 pandora_console/extras/demodata/agents/2-windows.prd create mode 100644 pandora_console/extras/demodata/agents/3-macosx.prd create mode 100644 pandora_console/extras/demodata/agents/4-freebsd.prd create mode 100644 pandora_console/extras/demodata/agents/5-cisco.prd create mode 100644 pandora_console/extras/demodata/dashboards/1-dashboard.prd create mode 100644 pandora_console/extras/demodata/graphs/1-cpu-graph.prd create mode 100644 pandora_console/extras/demodata/graphs/2-disks-graph.prd create mode 100644 pandora_console/extras/demodata/graphs/3-network-usage-graph.prd create mode 100644 pandora_console/extras/demodata/network_maps/1-networkmap.prd create mode 100644 pandora_console/extras/demodata/reports/1-sla-report.prd create mode 100644 pandora_console/extras/demodata/reports/2-graphs-report.prd create mode 100644 pandora_console/extras/demodata/services/1-mysql-service.prd create mode 100644 pandora_console/extras/demodata/services/2-http-service.prd create mode 100644 pandora_console/extras/demodata/services/3-parent-service.prd create mode 100644 pandora_console/extras/demodata/services/4-sunburst-linux-service.prd create mode 100644 pandora_console/extras/demodata/services/5-sunburst-windows-service.prd create mode 100644 pandora_console/extras/demodata/services/6-sunburst-macos-service.prd create mode 100644 pandora_console/extras/demodata/services/7-sunburst-freebsd-service.prd create mode 100644 pandora_console/extras/demodata/services/8-sunburst-cisco-service.prd create mode 100644 pandora_console/extras/demodata/services/9-sunburst-parent-service.prd create mode 100644 pandora_console/extras/demodata/visual_consoles/1-linux-visual-console.prd diff --git a/pandora_console/extras/demodata/agents/0-demo-agent.prd b/pandora_console/extras/demodata/agents/0-demo-agent.prd new file mode 100644 index 0000000000..af1fa72a66 --- /dev/null +++ b/pandora_console/extras/demodata/agents/0-demo-agent.prd @@ -0,0 +1,26 @@ +[agent_data] + +agents_number="1" + +agent_name="demo-global-agent" +agent_alias="demo-global-agent" + +group="Demo servers" + +address_network="192.168.0.0/24" + +os_name="Linux" + +os_versions[]="Rocky Linux 9.2 (Blue Onyx)" +os_versions[]="CentOS Linux 7 (Core)" +os_versions[]="CentOS Linux 6 (Core)" +os_versions[]="RHEL 8.2" +os_versions[]="RHEL 8.1" +os_versions[]="Alma Linux 9.1" + +[modules] + +name[1]="Host Alive" +type[1]="generic_proc" +values[1]="PROC;5" +group[1]="Network" \ No newline at end of file diff --git a/pandora_console/extras/demodata/agents/1-linux.prd b/pandora_console/extras/demodata/agents/1-linux.prd new file mode 100644 index 0000000000..5cd29b117e --- /dev/null +++ b/pandora_console/extras/demodata/agents/1-linux.prd @@ -0,0 +1,145 @@ +[agent_data] + +agents_number="4" + +agent_name="linux" +agent_alias="linux" + +group="Demo servers" + +address_network="192.168.1.0/24" + +os_name="Linux" + +os_versions[]="Rocky Linux 9.2 (Blue Onyx)" +os_versions[]="CentOS Linux 7 (Core)" +os_versions[]="CentOS Linux 6 (Core)" +os_versions[]="RHEL 8.2" +os_versions[]="RHEL 8.1" +os_versions[]="Alma Linux 9.1" + +[modules] + +name[1]="Network Usage" +type[1]="generic_data_inc" +description[1]="Network usage in the system" +values[1]="RANDOM;0;10000" +unit[1]="bytes/sec" +group[1]="Network" + +name[2]="Network latency" +type[2]="generic_data" +description[2]="Network latency to router" +values[2]="RANDOM;0;20" +unit[2]="msec" +group[2]="Network" + +name[3]="CPU Load" +type[3]="generic_data" +description[3]="CPU Usage" +values[3]="RANDOM;20;90" +unit[3]="%" +group[3]="System" + +name[4]="Disk Usage" +type[4]="generic_data" +description[4]="% used space" +values[4]="RANDOM;50;60" +unit[4]="%" +group[4]="System" + +name[5]="Memory Usage" +type[5]="generic_data" +description[5]="% Used memory" +values[5]="RANDOM;0;20" +unit[5]="%" +group[5]="System" + +name[6]="Antivirus Updated" +type[6]="generic_proc" +description[6]="Detect if A/V is enabled and updated" +values[6]="PROC;5" +group[6]="Security" + +name[7]="Insecure passwords status" +type[7]="generic_proc" +description[7]="No insecure passwords found" +values[7]="PROC;0" +group[7]="Security" + +name[8]="Files check status" +type[8]="generic_proc" +description[8]="No changed files found" +values[8]="PROC;0" +group[8]="Security" + +name[9]="Authorized ports status" +type[9]="generic_proc" +description[9]="No unauthorized ports found" +values[9]="PROC;0" +group[9]="Security" + +name[10]="Service httpd" +type[10]="generic_proc" +values[10]="PROC;5" +group[10]="Applications" + +name[11]="Service mysqld" +type[11]="generic_proc" +values[11]="PROC;5" +group[11]="Applications" + +name[12]="Service sshd" +type[12]="generic_proc" +values[12]="PROC;5" +group[12]="Applications" + +[inventory] + +name[1]="Software" +format[1]="Name;Version;Description" +values[1]="name;version;description" + +name[2]="Users" +format[2]="Username" +values[2]="username" + +name[3]="File System" +format[3]="Device;Free size;Total size;Mount point" +values[3]="device;free_size;total_size;mount_point" + +[inventory_values] + +name[1]="aajohan-comfortaa-fonts" +name[2]="aardvark-dns" +name[3]="abattis-cantarell-fonts" +name[4]="abrt" + +version[1]="3.101" +version[2]="1.0.3" +version[3]="0.301" +version[4]="2.14.6" + +description[1]="Modern style true type font" +description[2]="Authoritative DNS server for A/AAAA container records" +description[3]="Humanist sans serif font" +description[4]="Automatic bug detection and reporting tool" + +username[1]="root" +username[2]="pandora" + +device[1]="/dev/sda1" +device[2]="/dev/sda2" +device[3]="/dev/sdb1" + +free_size[1]="96G" +free_size[2]="136G" +free_size[3]="97M" + +total_size[1]="180G" +total_size[2]="1.6T" +total_size[3]="819M" + +mount_point[1]="/home" +mount_point[2]="/" +mount_point[3]="/tmp" \ No newline at end of file diff --git a/pandora_console/extras/demodata/agents/2-windows.prd b/pandora_console/extras/demodata/agents/2-windows.prd new file mode 100644 index 0000000000..1d1d6c782c --- /dev/null +++ b/pandora_console/extras/demodata/agents/2-windows.prd @@ -0,0 +1,114 @@ +[agent_data] + +agents_number="2" + +agent_name="windows" +agent_alias="windows" + +group="Demo servers" + +address_network="192.168.2.0/24" + +os_name="Windows" + +os_versions[]="Microsoft Windows 11 Pro" +os_versions[]="Microsoft Windows 11" +os_versions[]="Home Microsoft Windows 10 Pro" +os_versions[]="Microsoft Windows 10 Home" +os_versions[]="Microsoft Windows 2018" + +[modules] + +name[1]="Network Usage" +type[1]="generic_data_inc" +description[1]="Network usage in the system" +values[1]="RANDOM;0;10000" +unit[1]="bytes/sec" +group[1]="Network" + +name[2]="Network latency" +type[2]="generic_data" +description[2]="Network latency to router" +values[2]="RANDOM;0;20" +unit[2]="msec" +group[2]="Network" + +name[3]="CPU Load" +type[3]="generic_data" +description[3]="CPU Usage" +values[3]="RANDOM;20;90" +unit[3]="%" +group[3]="System" + +name[4]="Disk Usage" +type[4]="generic_data" +description[4]="% used space" +values[4]="RANDOM;50;60" +unit[4]="%" +group[4]="System" + +name[5]="Memory Usage" +type[5]="generic_data" +description[5]="% Used memory" +values[5]="RANDOM;0;20" +unit[5]="%" +group[5]="System" + +name[6]="Service WMI" +type[6]="generic_proc" +values[6]="PROC;5" +group[6]="Applications" + +[inventory] + +name[1]="Software" +format[1]="Name;Version" +values[1]="name;version" + +name[2]="Users" +format[2]="Domain;User" +values[2]="domain;user" + +name[3]="File System" +format[3]="Device;Total size;Free size;Mount point" +values[3]="device;total_size;free_size;mount_point" + +name[4]="Windows Serial Number" +format[4]="Serial number" +values[4]="serial_number" + +[inventory_values] + +name[1]="Microsoft Update Health Tools" +name[2]="Java(TM) SE Development Kit 17.0.6 (64-bit)" +name[3]="DisplayLink Graphics" +name[4]="Microsoft Visual C++ 2019 X64 Additional Runtime - 14.28.29325" + +version[1]="5.71.0.0" +version[2]="17.0.6.0" +version[3]="10.2.7042.0" +version[4]="14.28.29325" + +domain[1]="DEMO" +domain[2]="DEMO" + +user[1]="Administrator" +user[2]="pandora" + +device[1]="C:" +device[2]="D:" +device[3]="T:" + +total_size[1]="1.6T" +total_size[2]="180G" +total_size[3]="819M" + +free_size[1]="136G" +free_size[2]="96G" +free_size[3]="97M" + +mount_point[1]="OS" +mount_point[2]="Data" +mount_point[3]="Temp" + +serial_number[1]="Demo-56 4d 7d 43 24 e4 c6 ac-67 57 52 60 e9 10 26 78" \ No newline at end of file diff --git a/pandora_console/extras/demodata/agents/3-macosx.prd b/pandora_console/extras/demodata/agents/3-macosx.prd new file mode 100644 index 0000000000..ba8d75de87 --- /dev/null +++ b/pandora_console/extras/demodata/agents/3-macosx.prd @@ -0,0 +1,70 @@ +[agent_data] + +agents_number="1" + +agent_name="macos" +agent_alias="macos" + +group="Demo servers" + +address_network="192.168.3.0/24" + +os_name="MacOS" + +os_versions[]="12.1" +os_versions[]="13.1" +os_versions[]="11.2" +os_versions[]="11.3" +os_versions[]="10.3" + +[modules] + +name[1]="Network Usage" +type[1]="generic_data_inc" +description[1]="Network usage in the system" +values[1]="RANDOM;0;10000" +unit[1]="bytes/sec" +group[1]="Network" + +name[2]="Network latency" +type[2]="generic_data" +description[2]="Network latency to router" +values[2]="RANDOM;0;20" +unit[2]="msec" +group[2]="Network" + +name[3]="CPU Load" +type[3]="generic_data" +description[3]="CPU Usage" +values[3]="RANDOM;20;90" +unit[3]="%" +group[3]="System" + +name[4]="Disk Usage" +type[4]="generic_data" +description[4]="% used space" +values[4]="RANDOM;50;60" +unit[4]="%" +group[4]="System" + +name[5]="Memory Usage" +type[5]="generic_data" +description[5]="% Used memory" +values[5]="RANDOM;0;20" +unit[5]="%" +group[5]="System" + +name[6]="Service httpd" +type[6]="generic_proc" +values[6]="PROC;5" +group[6]="Applications" + +name[7]="Service mysqld" +type[7]="generic_proc" +values[7]="PROC;5" +group[7]="Applications" + +name[8]="Service sshd" +type[8]="generic_proc" +values[8]="PROC;5" +group[8]="Applications" \ No newline at end of file diff --git a/pandora_console/extras/demodata/agents/4-freebsd.prd b/pandora_console/extras/demodata/agents/4-freebsd.prd new file mode 100644 index 0000000000..956a501660 --- /dev/null +++ b/pandora_console/extras/demodata/agents/4-freebsd.prd @@ -0,0 +1,69 @@ +[agent_data] + +agents_number="1" + +agent_name="freebsd" +agent_alias="freebsd" + +group="Demo servers" + +address_network="192.168.4.0/24" + +os_name="BSD" + +os_versions[]="14.0" +os_versions[]="13.1" +os_versions[]="13.2" +os_versions[]="13.3" + +[modules] + +name[1]="Network Usage" +type[1]="generic_data_inc" +description[1]="Network usage in the system" +values[1]="RANDOM;0;10000" +unit[1]="bytes/sec" +group[1]="Network" + +name[2]="Network latency" +type[2]="generic_data" +description[2]="Network latency to router" +values[2]="RANDOM;0;20" +unit[2]="msec" +group[2]="Network" + +name[3]="CPU Load" +type[3]="generic_data" +description[3]="CPU Usage" +values[3]="RANDOM;20;90" +unit[3]="%" +group[3]="System" + +name[4]="Disk Usage" +type[4]="generic_data" +description[4]="% used space" +values[4]="RANDOM;50;60" +unit[4]="%" +group[4]="System" + +name[5]="Memory Usage" +type[5]="generic_data" +description[5]="% Used memory" +values[5]="RANDOM;0;20" +unit[5]="%" +group[5]="System" + +name[6]="Service httpd" +type[6]="generic_proc" +values[6]="PROC;5" +group[6]="Applications" + +name[7]="Service mysqld" +type[7]="generic_proc" +values[7]="PROC;5" +group[7]="Applications" + +name[8]="Service sshd" +type[8]="generic_proc" +values[8]="PROC;5" +group[8]="Applications" \ No newline at end of file diff --git a/pandora_console/extras/demodata/agents/5-cisco.prd b/pandora_console/extras/demodata/agents/5-cisco.prd new file mode 100644 index 0000000000..58febab7fe --- /dev/null +++ b/pandora_console/extras/demodata/agents/5-cisco.prd @@ -0,0 +1,293 @@ +[agent_data] + +agents_number="1" + +agent_name="cisco" +agent_alias="cisco" + +group="Demo network" + +address_network="192.168.5.0/24" +mac="__randomMAC__" + +os_name="Cisco" + +os_versions[]="C9200L" +os_versions[]="C9300LM" +os_versions[]="C9600" + +[modules] + +name[1]="CPU Usage (5 min avg)" +type[1]="generic_data" +description[1]="CPU Usage" +values[1]="RANDOM;20;90" +unit[1]="%" +group[1]="General" + +name[2]="Big Buffer Misses" +type[2]="generic_data" +description[2]="Buffer misses" +values[2]="RANDOM;20;90" +unit[2]="%" +group[2]="General" + +name[3]="Temperature" +type[3]="generic_data" +description[3]="System temp" +values[3]="RANDOM;20;30" +unit[3]="ºC" +group[3]="General" + +name[4]="Host alive" +type[4]="generic_proc" +values[4]="PROC;5" +group[4]="Networking" + +name[5]="Gi0/0_ifOperStatus" +type[5]="generic_proc" +description[5]="(MAC: _mac_ - Gi0/0_ifOperStatus)" +values[5]="PROC;5" +group[5]="Networking" + +name[6]="Gi0/0_ifInOctets" +type[6]="generic_data_inc" +description[6]="(MAC: _mac_ - Gi0/0_ifInOctets)" +values[6]="RANDOM;0;1000" +unit[6]="bytes/s" +group[6]="Networking" + +name[7]="Gi0/0_ifOutOctets" +type[7]="generic_data_inc" +description[7]="(MAC: _mac_ - Gi0/0_ifOutOctets)" +values[7]="RANDOM;0;1000" +unit[7]="bytes/s" +group[7]="Networking" + +name[8]="Gi0/1_ifOperStatus" +type[8]="generic_proc" +description[8]="(MAC: _mac_ - Gi0/1_ifOperStatus)" +values[8]="PROC;5" +group[8]="Networking" + +name[9]="Gi0/1_ifInOctets" +type[9]="generic_data_inc" +description[9]="(MAC: _mac_ - Gi0/1_ifInOctets)" +values[9]="RANDOM;0;1000" +unit[9]="bytes/s" +group[9]="Networking" + +name[10]="Gi0/1_ifOutOctets" +type[10]="generic_data_inc" +description[10]="(MAC: _mac_ - Gi0/1_ifOutOctets)" +values[10]="RANDOM;0;1000" +unit[10]="bytes/s" +group[10]="Networking" + +name[11]="Gi0/2_ifOperStatus" +type[11]="generic_proc" +description[11]="(MAC: _mac_ - Gi0/2_ifOperStatus)" +values[11]="PROC;5" +group[11]="Networking" + +name[12]="Gi0/2_ifInOctets" +type[12]="generic_data_inc" +description[12]="(MAC: _mac_ - Gi0/2_ifInOctets)" +values[12]="RANDOM;0;1000" +unit[12]="bytes/s" +group[12]="Networking" + +name[13]="Gi0/2_ifOutOctets" +type[13]="generic_data_inc" +description[13]="(MAC: _mac_ - Gi0/2_ifOutOctets)" +values[13]="RANDOM;0;1000" +unit[13]="bytes/s" +group[13]="Networking" + +name[14]="Gi0/3_ifOperStatus" +type[14]="generic_proc" +description[14]="(MAC: _mac_ - Gi0/3_ifOperStatus)" +values[14]="PROC;5" +group[14]="Networking" + +name[15]="Gi0/3_ifInOctets" +type[15]="generic_data_inc" +description[15]="(MAC: _mac_ - Gi0/3_ifInOctets)" +values[15]="RANDOM;0;1000" +unit[15]="bytes/s" +group[15]="Networking" + +name[16]="Gi0/3_ifOutOctets" +type[16]="generic_data_inc" +description[16]="(MAC: _mac_ - Gi0/3_ifOutOctets)" +values[16]="RANDOM;0;1000" +unit[16]="bytes/s" +group[16]="Networking" + +name[17]="Gi0/4_ifOperStatus" +type[17]="generic_proc" +description[17]="(MAC: _mac_ - Gi0/4_ifOperStatus)" +values[17]="PROC;5" +group[17]="Networking" + +name[18]="Gi0/4_ifInOctets" +type[18]="generic_data_inc" +description[18]="(MAC: _mac_ - Gi0/4_ifInOctets)" +values[18]="RANDOM;0;1000" +unit[18]="bytes/s" +group[18]="Networking" + +name[19]="Gi0/4_ifOutOctets" +type[19]="generic_data_inc" +description[19]="(MAC: _mac_ - Gi0/4_ifOutOctets)" +values[19]="RANDOM;0;1000" +unit[19]="bytes/s" +group[19]="Networking" + +name[20]="Gi0/5_ifOperStatus" +type[20]="generic_proc" +description[20]="(MAC: _mac_ - Gi0/5_ifOperStatus)" +values[20]="PROC;5" +group[20]="Networking" + +name[21]="Gi0/5_ifInOctets" +type[21]="generic_data_inc" +description[21]="(MAC: _mac_ - Gi0/5_ifInOctets)" +values[21]="RANDOM;0;1000" +unit[21]="bytes/s" +group[21]="Networking" + +name[22]="Gi0/5_ifOutOctets" +type[22]="generic_data_inc" +description[22]="(MAC: _mac_ - Gi0/5_ifOutOctets)" +values[22]="RANDOM;0;1000" +unit[22]="bytes/s" +group[22]="Networking" + +name[23]="Gi0/6_ifOperStatus" +type[23]="generic_proc" +description[23]="(MAC: _mac_ - Gi0/6_ifOperStatus)" +values[23]="PROC;5" +group[23]="Networking" + +name[24]="Gi0/6_ifInOctets" +type[24]="generic_data_inc" +description[24]="(MAC: _mac_ - Gi0/6_ifInOctets)" +values[24]="RANDOM;0;1000" +unit[24]="bytes/s" +group[24]="Networking" + +name[25]="Gi0/6_ifOutOctets" +type[25]="generic_data_inc" +description[25]="(MAC: _mac_ - Gi0/6_ifOutOctets)" +values[25]="RANDOM;0;1000" +unit[25]="bytes/s" +group[25]="Networking" + +name[26]="Gi0/7_ifOperStatus" +type[26]="generic_proc" +description[26]="(MAC: _mac_ - Gi0/7_ifOperStatus)" +values[26]="PROC;5" +group[26]="Networking" + +name[27]="Gi0/7_ifInOctets" +type[27]="generic_data_inc" +description[27]="(MAC: _mac_ - Gi0/7_ifInOctets)" +values[27]="RANDOM;0;1000" +unit[27]="bytes/s" +group[27]="Networking" + +name[28]="Gi0/7_ifOutOctets" +type[28]="generic_data_inc" +description[28]="(MAC: _mac_ - Gi0/7_ifOutOctets)" +values[28]="RANDOM;0;1000" +unit[28]="bytes/s" +group[28]="Networking" + +name[29]="Gi0/8_ifOperStatus" +type[29]="generic_proc" +description[29]="(MAC: _mac_ - Gi0/8_ifOperStatus)" +values[29]="PROC;5" +group[29]="Networking" + +name[30]="Gi0/8_ifInOctets" +type[30]="generic_data_inc" +description[30]="(MAC: _mac_ - Gi0/8_ifInOctets)" +values[30]="RANDOM;0;1000" +unit[30]="bytes/s" +group[30]="Networking" + +name[31]="Gi0/8_ifOutOctets" +type[31]="generic_data_inc" +description[31]="(MAC: _mac_ - Gi0/8_ifOutOctets)" +values[31]="RANDOM;0;1000" +unit[31]="bytes/s" +group[31]="Networking" + +name[32]="Gi0/9_ifOperStatus" +type[32]="generic_proc" +description[32]="(MAC: _mac_ - Gi0/9_ifOperStatus)" +values[32]="PROC;5" +group[32]="Networking" + +name[33]="Gi0/9_ifInOctets" +type[33]="generic_data_inc" +description[33]="(MAC: _mac_ - Gi0/9_ifInOctets)" +values[33]="RANDOM;0;1000" +unit[33]="bytes/s" +group[33]="Networking" + +name[34]="Gi0/9_ifOutOctets" +type[34]="generic_data_inc" +description[34]="(MAC: _mac_ - Gi0/9_ifOutOctets)" +values[34]="RANDOM;0;1000" +unit[34]="bytes/s" +group[34]="Networking" + +name[35]="ipInReceives" +type[35]="generic_data" +description[35]="The total number of input datagrams received from interfaces" +values[35]="RANDOM;0;1000" +unit[35]="datagrams" +group[35]="Networking" + +name[36]="ipOutRequests" +type[36]="generic_data" +description[36]="The total number of output datagrams requested from interfaces" +values[36]="RANDOM;0;1000" +unit[36]="datagrams" +group[36]="Networking" + +name[37]="Latency" +type[37]="generic_data" +description[37]="Network latency" +values[37]="RANDOM;0;100" +unit[37]="msec" +group[37]="Networking" + +[traps] + +oid[1]=".1.3.6.1.4.1.9.9.41.1.2.3.1.2.0" +value[1]="RANDOM;0;100" +snmp_type[1]="6" +chance_percent[1]="5" + +oid[2]=".1.3.6.1.4.1.9.9.41.1.2.3.1.3.0" +value[2]="RANDOM;0;100" +snmp_type[2]="6" +chance_percent[2]="5" + +oid[3]=".1.3.6.1.4.1.9.9.41.1.2.3.1.4.0" +value[3]="RANDOM;0;100" +snmp_type[3]="6" +chance_percent[3]="5" + +oid[4]=".1.3.6.1.4.1.9.9.41.1.2.3.1.5.0" +value[4]="RANDOM;0;100" +snmp_type[4]="6" +chance_percent[4]="5" + +oid[5]=".1.3.6.1.4.1.9.9.41.1.2.3.1.6.0" +value[5]="RANDOM;0;100" +snmp_type[5]="6" +chance_percent[5]="5" \ No newline at end of file diff --git a/pandora_console/extras/demodata/dashboards/1-dashboard.prd b/pandora_console/extras/demodata/dashboards/1-dashboard.prd new file mode 100644 index 0000000000..8cfc89bc01 --- /dev/null +++ b/pandora_console/extras/demodata/dashboards/1-dashboard.prd @@ -0,0 +1,43 @@ +[dashboard_data] + +name="Demo dashboard" +group="Demo dashboard" + +[dashboard_items] + +title[1]="Agents sunburst" +type[1]="service_map" +service_name[1]="Sunburst service" +x[1]="0" +y[1]="0" +width[1]="6" +height[1]="10" +show_sunburst[1]=true + +title[2]="Linux CPU Load" +type[2]="custom_graph" +graph_name[2]="Linux CPU Load" +x[2]="6" +y[2]="0" +width[2]="6" +height[2]="5" +graph_type[2]="s_area" +interval[2]="86400" + +title[3]="Linux Disk Usage" +type[3]="custom_graph" +graph_name[3]="Linux Disk Usage" +x[3]="6" +y[3]="5" +width[3]="6" +height[3]="5" +graph_type[3]="line" +interval[3]="86400" + +title[4]="Network map" +type[4]="network_map" +map_name[4]="Demo network map" +x[4]="0" +y[4]="10" +width[4]="12" +height[4]="10" \ No newline at end of file diff --git a/pandora_console/extras/demodata/graphs/1-cpu-graph.prd b/pandora_console/extras/demodata/graphs/1-cpu-graph.prd new file mode 100644 index 0000000000..61b7ab456c --- /dev/null +++ b/pandora_console/extras/demodata/graphs/1-cpu-graph.prd @@ -0,0 +1,21 @@ +[graph_data] + +name="Linux CPU Load" +group="Demo servers" +description="This is a demo custom graph for linux agents CPU Load" +type="s_area" +periodicity="86400" + +[graph_items] + +agent_name[1]="linux-1" +module[1]="CPU Load" + +agent_name[2]="linux-2" +module[2]="CPU Load" + +agent_name[3]="linux-3" +module[3]="CPU Load" + +agent_name[4]="linux-4" +module[4]="CPU Load" \ No newline at end of file diff --git a/pandora_console/extras/demodata/graphs/2-disks-graph.prd b/pandora_console/extras/demodata/graphs/2-disks-graph.prd new file mode 100644 index 0000000000..cf3032c5f0 --- /dev/null +++ b/pandora_console/extras/demodata/graphs/2-disks-graph.prd @@ -0,0 +1,21 @@ +[graph_data] + +name="Linux Disk Usage" +group="Demo servers" +description="This is a demo custom graph for linux agents Disk Usage" +type="line" +periodicity="86400" + +[graph_items] + +agent_name[1]="linux-1" +module[1]="Disk Usage" + +agent_name[2]="linux-2" +module[2]="Disk Usage" + +agent_name[3]="linux-3" +module[3]="Disk Usage" + +agent_name[4]="linux-4" +module[4]="Disk Usage" \ No newline at end of file diff --git a/pandora_console/extras/demodata/graphs/3-network-usage-graph.prd b/pandora_console/extras/demodata/graphs/3-network-usage-graph.prd new file mode 100644 index 0000000000..3108b4cb7f --- /dev/null +++ b/pandora_console/extras/demodata/graphs/3-network-usage-graph.prd @@ -0,0 +1,27 @@ +[graph_data] + +name="Network Usage" +group="Demo servers" +description="This is a demo custom graph for linux and windows agents Network Usage" +type="s_area" +periodicity="86400" + +[graph_items] + +agent_name[1]="linux-1" +module[1]="Network Usage" + +agent_name[2]="linux-2" +module[2]="Network Usage" + +agent_name[3]="linux-3" +module[3]="Network Usage" + +agent_name[4]="linux-4" +module[4]="Network Usage" + +agent_name[5]="windows-1" +module[5]="Network Usage" + +agent_name[6]="windows-2" +module[6]="Network Usage" \ No newline at end of file diff --git a/pandora_console/extras/demodata/network_maps/1-networkmap.prd b/pandora_console/extras/demodata/network_maps/1-networkmap.prd new file mode 100644 index 0000000000..f780eb0977 --- /dev/null +++ b/pandora_console/extras/demodata/network_maps/1-networkmap.prd @@ -0,0 +1,97 @@ +[map_data] + +name="Demo network map" +group="Demo network" +description="This is a demo network map" +node_radius="40" + +[map_items] + +agent_name[1]="cisco-1" +x[1]="854" +y[1]="221" + +agent_name[2]="freebsd-1" +x[2]="1184" +y[2]="-25" +parent[2]="1" + +agent_name[3]="macos-1" +x[3]="1268" +y[3]="235" +parent[3]="1" + +agent_name[4]="windows-1" +x[4]="528" +y[4]="-37" +parent[4]="1" + +agent_name[5]="windows-2" +x[5]="514" +y[5]="389" +parent[5]="1" + +agent_name[6]="linux-1" +x[6]="342" +y[6]="-273" +parent[6]="4" + +agent_name[7]="linux-2" +x[7]="240" +y[7]="-7" +parent[7]="4" + +agent_name[8]="linux-3" +x[8]="214" +y[8]="275" +parent[8]="5" + +agent_name[9]="linux-4" +x[9]="226" +y[9]="595" +parent[9]="5" + +agent_name[10]="cisco-2" +x[10]="1036" +y[10]="711" +parent[10]="1" + +agent_name[11]="freebsd-2" +x[11]="892" +y[11]="-47" +parent[11]="1" + +agent_name[12]="macos-2" +x[12]="1150" +y[12]="409" +parent[12]="1" + +agent_name[13]="windows-3" +x[13]="686" +y[13]="875" +parent[13]="10" + +agent_name[14]="windows-4" +x[14]="1452" +y[14]="847" +parent[14]="10" + +agent_name[15]="linux-5" +x[15]="314" +y[15]="845" +parent[15]="13" + +agent_name[16]="linux-6" +x[16]="474" +y[16]="1043" +parent[16]="13" + +agent_name[17]="linux-7" +x[17]="1812" +y[17]="1011" +parent[17]="14" + +agent_name[18]="linux-8" +x[18]="1828" +y[18]="723" +parent[18]="14" \ No newline at end of file diff --git a/pandora_console/extras/demodata/reports/1-sla-report.prd b/pandora_console/extras/demodata/reports/1-sla-report.prd new file mode 100644 index 0000000000..e3d130b4a0 --- /dev/null +++ b/pandora_console/extras/demodata/reports/1-sla-report.prd @@ -0,0 +1,47 @@ +[report_data] + +name="SLA report" +group="Demo servers" +description="This is a demo report for SLA" + +[report_items] + +name[1]="linux-1 httpd" +type[1]="sla" +agent_name[1]="linux-1" +module[1]="Service httpd" + +name[2]="linux-2 httpd" +type[2]="sla" +agent_name[2]="linux-2" +module[2]="Service httpd" + +name[3]="linux-3 httpd" +type[3]="sla" +agent_name[3]="linux-3" +module[3]="Service httpd" + +name[4]="linux-4 httpd" +type[4]="sla" +agent_name[4]="linux-4" +module[4]="Service httpd" + +name[5]="linux-1 mysqld" +type[5]="sla" +agent_name[5]="linux-1" +module[5]="Service mysqld" + +name[6]="linux-2 mysqld" +type[6]="sla" +agent_name[6]="linux-2" +module[6]="Service mysqld" + +name[7]="linux-3 mysqld" +type[7]="sla" +agent_name[7]="linux-3" +module[7]="Service mysqld" + +name[8]="linux-4 mysqld" +type[8]="sla" +agent_name[8]="linux-4" +module[8]="Service mysqld" \ No newline at end of file diff --git a/pandora_console/extras/demodata/reports/2-graphs-report.prd b/pandora_console/extras/demodata/reports/2-graphs-report.prd new file mode 100644 index 0000000000..ecf3fe0502 --- /dev/null +++ b/pandora_console/extras/demodata/reports/2-graphs-report.prd @@ -0,0 +1,50 @@ +[report_data] + +name="CPU Load report" +group="Demo network" +description="This is a demo report for CPU Load" + +[report_items] + +type[1]="custom_graph" +graph_name[1]="Linux CPU Load" + +name[2]="linux-1 CPU Load graph" +type[2]="simple_graph" +agent_name[2]="linux-1" +module[2]="CPU Load" + +name[3]="linux-1 CPU Load data" +type[3]="historical" +agent_name[3]="linux-1" +module[3]="CPU Load" + +name[4]="linux-2 CPU Load graph" +type[4]="simple_graph" +agent_name[4]="linux-2" +module[4]="CPU Load" + +name[5]="linux-2 CPU Load data" +type[5]="historical" +agent_name[5]="linux-2" +module[5]="CPU Load" + +name[6]="linux-3 CPU Load graph" +type[6]="simple_graph" +agent_name[6]="linux-3" +module[6]="CPU Load" + +name[7]="linux-3 CPU Load data" +type[7]="historical" +agent_name[7]="linux-3" +module[7]="CPU Load" + +name[8]="linux-4 CPU Load graph" +type[8]="simple_graph" +agent_name[8]="linux-4" +module[8]="CPU Load" + +name[9]="linux-4 CPU Load data" +type[9]="historical" +agent_name[9]="linux-4" +module[9]="CPU Load" \ No newline at end of file diff --git a/pandora_console/extras/demodata/services/1-mysql-service.prd b/pandora_console/extras/demodata/services/1-mysql-service.prd new file mode 100644 index 0000000000..23e366e44a --- /dev/null +++ b/pandora_console/extras/demodata/services/1-mysql-service.prd @@ -0,0 +1,26 @@ +[service_data] + +name="MySQL service" +group="Demo services" +description="This is a demo service for MySQL" +mode="smart" +critical="50" +warning="30" + +[service_items] + +type[1]="module" +agent_name[1]="linux-1" +module[1]="Service mysqld" + +type[2]="module" +agent_name[2]="linux-2" +module[2]="Service mysqld" + +type[3]="module" +agent_name[3]="linux-3" +module[3]="Service mysqld" + +type[4]="module" +agent_name[4]="linux-4" +module[4]="Service mysqld" \ No newline at end of file diff --git a/pandora_console/extras/demodata/services/2-http-service.prd b/pandora_console/extras/demodata/services/2-http-service.prd new file mode 100644 index 0000000000..2257797134 --- /dev/null +++ b/pandora_console/extras/demodata/services/2-http-service.prd @@ -0,0 +1,26 @@ +[service_data] + +name="HTTP service" +group="Demo services" +description="This is a demo service for HTTP" +mode="smart" +critical="50" +warning="30" + +[service_items] + +type[1]="module" +agent_name[1]="linux-1" +module[1]="Service httpd" + +type[2]="module" +agent_name[2]="linux-2" +module[2]="Service httpd" + +type[3]="module" +agent_name[3]="linux-3" +module[3]="Service httpd" + +type[4]="module" +agent_name[4]="linux-4" +module[4]="Service httpd" \ No newline at end of file diff --git a/pandora_console/extras/demodata/services/3-parent-service.prd b/pandora_console/extras/demodata/services/3-parent-service.prd new file mode 100644 index 0000000000..a31f669735 --- /dev/null +++ b/pandora_console/extras/demodata/services/3-parent-service.prd @@ -0,0 +1,16 @@ +[service_data] + +name="Web cluster service" +group="Demo services" +description="This is a demo service for a clustered web" +mode="smart" +critical="50" +warning="50" + +[service_items] + +type[1]="service" +service_name[1]="MySQL service" + +type[2]="service" +service_name[2]="HTTP service" \ No newline at end of file diff --git a/pandora_console/extras/demodata/services/4-sunburst-linux-service.prd b/pandora_console/extras/demodata/services/4-sunburst-linux-service.prd new file mode 100644 index 0000000000..b85adcef56 --- /dev/null +++ b/pandora_console/extras/demodata/services/4-sunburst-linux-service.prd @@ -0,0 +1,17 @@ +[service_data] + +name="Linux service" +group="Demo services" +description="This is a demo sunburst service for linux agents" +mode="smart" +critical="50" +warning="30" +show_sunburst=true + +[service_items] + +type[1]="dynamic" +match[1]="agent" +group[1]="Demo servers" +regex[1]=true +agent_name[1]="linux" \ No newline at end of file diff --git a/pandora_console/extras/demodata/services/5-sunburst-windows-service.prd b/pandora_console/extras/demodata/services/5-sunburst-windows-service.prd new file mode 100644 index 0000000000..cada189ddf --- /dev/null +++ b/pandora_console/extras/demodata/services/5-sunburst-windows-service.prd @@ -0,0 +1,17 @@ +[service_data] + +name="Windows service" +group="Demo services" +description="This is a demo sunburst service for windows agents" +mode="smart" +critical="50" +warning="30" +show_sunburst=true + +[service_items] + +type[1]="dynamic" +match[1]="agent" +group[1]="Demo servers" +regex[1]=true +agent_name[1]="windows" \ No newline at end of file diff --git a/pandora_console/extras/demodata/services/6-sunburst-macos-service.prd b/pandora_console/extras/demodata/services/6-sunburst-macos-service.prd new file mode 100644 index 0000000000..7344761920 --- /dev/null +++ b/pandora_console/extras/demodata/services/6-sunburst-macos-service.prd @@ -0,0 +1,17 @@ +[service_data] + +name="MacOS service" +group="Demo services" +description="This is a demo sunburst service for macos agents" +mode="smart" +critical="50" +warning="30" +show_sunburst=true + +[service_items] + +type[1]="dynamic" +match[1]="agent" +group[1]="Demo servers" +regex[1]=true +agent_name[1]="macos" \ No newline at end of file diff --git a/pandora_console/extras/demodata/services/7-sunburst-freebsd-service.prd b/pandora_console/extras/demodata/services/7-sunburst-freebsd-service.prd new file mode 100644 index 0000000000..c97828123b --- /dev/null +++ b/pandora_console/extras/demodata/services/7-sunburst-freebsd-service.prd @@ -0,0 +1,17 @@ +[service_data] + +name="FreeBSD service" +group="Demo services" +description="This is a demo sunburst service for freebsd agents" +mode="smart" +critical="50" +warning="30" +show_sunburst=true + +[service_items] + +type[1]="dynamic" +match[1]="agent" +group[1]="Demo servers" +regex[1]=true +agent_name[1]="freebsd" \ No newline at end of file diff --git a/pandora_console/extras/demodata/services/8-sunburst-cisco-service.prd b/pandora_console/extras/demodata/services/8-sunburst-cisco-service.prd new file mode 100644 index 0000000000..079c5231c3 --- /dev/null +++ b/pandora_console/extras/demodata/services/8-sunburst-cisco-service.prd @@ -0,0 +1,17 @@ +[service_data] + +name="Cisco service" +group="Demo services" +description="This is a demo sunburst service for cisco agents" +mode="smart" +critical="50" +warning="30" +show_sunburst=true + +[service_items] + +type[1]="dynamic" +match[1]="agent" +group[1]="Demo servers" +regex[1]=true +agent_name[1]="cisco" \ No newline at end of file diff --git a/pandora_console/extras/demodata/services/9-sunburst-parent-service.prd b/pandora_console/extras/demodata/services/9-sunburst-parent-service.prd new file mode 100644 index 0000000000..2e13575e63 --- /dev/null +++ b/pandora_console/extras/demodata/services/9-sunburst-parent-service.prd @@ -0,0 +1,26 @@ +[service_data] + +name="Sunburst service" +group="Demo services" +description="This is a demo sunburst service for all agents" +mode="smart" +critical="50" +warning="30" +show_sunburst=true + +[service_items] + +type[1]="service" +service_name[1]="Linux service" + +type[2]="service" +service_name[2]="Windows service" + +type[3]="service" +service_name[3]="MacOS service" + +type[4]="service" +service_name[4]="FreeBSD service" + +type[5]="service" +service_name[5]="Cisco service" \ No newline at end of file diff --git a/pandora_console/extras/demodata/visual_consoles/1-linux-visual-console.prd b/pandora_console/extras/demodata/visual_consoles/1-linux-visual-console.prd new file mode 100644 index 0000000000..741cabb050 --- /dev/null +++ b/pandora_console/extras/demodata/visual_consoles/1-linux-visual-console.prd @@ -0,0 +1,150 @@ +[visual_console_data] + +name="Linux visual console" +group="Demo servers" +background="blackabstract.jpg" +background_color="#ffffff" +width="1285" +height="768" + +[visual_console_items] + +type[1]="label" +label[1]="

    Security

    " +label_position[1]="down" +x[1]="160" +y[1]="25" +width[1]="160" +height[1]="55" + +type[2]="label" +label[2]="

    Performance

    " +label_position[2]="down" +x[2]="840" +y[2]="25" +width[2]="160" +height[2]="55" + +type[3]="label" +label[3]="

    Services

    " +label_position[3]="down" +x[3]="170" +y[3]="400" +width[3]="160" +height[3]="55" + +type[4]="static_image" +image[4]="status" +agent_name[4]="linux-1" +module[4]="Antivirus Updated" +label[4]="

    _module_

    " +label_position[4]="right" +x[4]="80" +y[4]="105" +width[4]="50" +height[4]="50" + +type[5]="static_image" +image[5]="status" +agent_name[5]="linux-1" +module[5]="Insecure passwords status" +label[5]="

    _module_

    " +label_position[5]="right" +x[5]="80" +y[5]="165" +width[5]="50" +height[5]="50" + +type[6]="static_image" +image[6]="status" +agent_name[6]="linux-1" +module[6]="Files check status" +label[6]="

    _module_

    " +label_position[6]="right" +x[6]="80" +y[6]="225" +width[6]="50" +height[6]="50" + +type[7]="static_image" +image[7]="status" +agent_name[7]="linux-1" +module[7]="Authorized ports status" +label[7]="

    _module_

    " +label_position[7]="right" +x[7]="80" +y[7]="285" +width[7]="50" +height[7]="50" + +type[8]="static_image" +image[8]="status" +agent_name[8]="linux-1" +module[8]="Service httpd" +label[8]="

    _module_

    " +label_position[8]="right" +x[8]="80" +y[8]="490" +width[8]="50" +height[8]="50" + +type[9]="static_image" +image[9]="status" +agent_name[9]="linux-1" +module[9]="Service mysqld" +label[9]="

    _module_

    " +label_position[9]="right" +x[9]="80" +y[9]="550" +width[9]="50" +height[9]="50" + +type[10]="static_image" +image[10]="status" +agent_name[10]="linux-1" +module[10]="Service sshd" +label[10]="

    _module_

    " +label_position[10]="right" +x[10]="80" +y[10]="610" +width[10]="50" +height[10]="50" + +type[11]="module_graph" +image[11]="black" +agent_name[11]="linux-1" +module[11]="CPU Load" +interval[11]="86N00" +graph_type[11]="area" +label[11]="" +label_position[11]="down" +x[11]="625" +y[11]="100" +width[11]="600" +height[11]="180" + +type[12]="module_graph" +image[12]="black" +agent_name[12]="linux-1" +module[12]="Memory Usage" +interval[12]="86N00" +graph_type[12]="area" +label[12]="" +label_position[12]="down" +x[12]="625" +y[12]="300" +width[12]="600" +height[12]="180" + +type[13]="module_graph" +image[13]="black" +agent_name[13]="linux-1" +module[13]="Disk Usage" +interval[13]="86N00" +graph_type[13]="area" +label[13]="" +label_position[13]="down" +x[13]="625" +y[13]="500" +width[13]="600" +height[13]="180" \ No newline at end of file From 4cf2b3427ec6d6beb6f0f05445854925f454f330 Mon Sep 17 00:00:00 2001 From: Enrique Martin Date: Tue, 28 Nov 2023 16:00:01 +0100 Subject: [PATCH 11/42] Added peridicity to reports prd format --- .../extras/demodata/reports/1-sla-report.prd | 10 +++++++++- .../extras/demodata/reports/2-graphs-report.prd | 11 ++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/pandora_console/extras/demodata/reports/1-sla-report.prd b/pandora_console/extras/demodata/reports/1-sla-report.prd index e3d130b4a0..f40c9b896f 100644 --- a/pandora_console/extras/demodata/reports/1-sla-report.prd +++ b/pandora_console/extras/demodata/reports/1-sla-report.prd @@ -10,38 +10,46 @@ name[1]="linux-1 httpd" type[1]="sla" agent_name[1]="linux-1" module[1]="Service httpd" +periodicity[1]="86400" name[2]="linux-2 httpd" type[2]="sla" agent_name[2]="linux-2" module[2]="Service httpd" +periodicity[2]="86400" name[3]="linux-3 httpd" type[3]="sla" agent_name[3]="linux-3" module[3]="Service httpd" +periodicity[3]="86400" name[4]="linux-4 httpd" type[4]="sla" agent_name[4]="linux-4" module[4]="Service httpd" +periodicity[4]="86400" name[5]="linux-1 mysqld" type[5]="sla" agent_name[5]="linux-1" module[5]="Service mysqld" +periodicity[5]="86400" name[6]="linux-2 mysqld" type[6]="sla" agent_name[6]="linux-2" module[6]="Service mysqld" +periodicity[6]="86400" name[7]="linux-3 mysqld" type[7]="sla" agent_name[7]="linux-3" module[7]="Service mysqld" +periodicity[7]="86400" name[8]="linux-4 mysqld" type[8]="sla" agent_name[8]="linux-4" -module[8]="Service mysqld" \ No newline at end of file +module[8]="Service mysqld" +periodicity[8]="86400" \ No newline at end of file diff --git a/pandora_console/extras/demodata/reports/2-graphs-report.prd b/pandora_console/extras/demodata/reports/2-graphs-report.prd index ecf3fe0502..3e8e273e37 100644 --- a/pandora_console/extras/demodata/reports/2-graphs-report.prd +++ b/pandora_console/extras/demodata/reports/2-graphs-report.prd @@ -8,43 +8,52 @@ description="This is a demo report for CPU Load" type[1]="custom_graph" graph_name[1]="Linux CPU Load" +periodicity[1]="86400" name[2]="linux-1 CPU Load graph" type[2]="simple_graph" agent_name[2]="linux-1" module[2]="CPU Load" +periodicity[2]="86400" name[3]="linux-1 CPU Load data" type[3]="historical" agent_name[3]="linux-1" module[3]="CPU Load" +periodicity[3]="86400" name[4]="linux-2 CPU Load graph" type[4]="simple_graph" agent_name[4]="linux-2" module[4]="CPU Load" +periodicity[4]="86400" name[5]="linux-2 CPU Load data" type[5]="historical" agent_name[5]="linux-2" module[5]="CPU Load" +periodicity[5]="86400" name[6]="linux-3 CPU Load graph" type[6]="simple_graph" agent_name[6]="linux-3" module[6]="CPU Load" +periodicity[6]="86400" name[7]="linux-3 CPU Load data" type[7]="historical" agent_name[7]="linux-3" module[7]="CPU Load" +periodicity[7]="86400" name[8]="linux-4 CPU Load graph" type[8]="simple_graph" agent_name[8]="linux-4" module[8]="CPU Load" +periodicity[8]="86400" name[9]="linux-4 CPU Load data" type[9]="historical" agent_name[9]="linux-4" -module[9]="CPU Load" \ No newline at end of file +module[9]="CPU Load" +periodicity[9]="86400" \ No newline at end of file From e56b0ee37e0dd399395fb32aae1b809c17e03cc0 Mon Sep 17 00:00:00 2001 From: Enrique Martin Date: Tue, 28 Nov 2023 16:46:42 +0100 Subject: [PATCH 12/42] Added all widgets by defult --- pandora_console/extras/mr/67.sql | 368 +++++++++++++++++++++++++++++++ 1 file changed, 368 insertions(+) diff --git a/pandora_console/extras/mr/67.sql b/pandora_console/extras/mr/67.sql index 0ae98fd735..3e48bf9e8b 100644 --- a/pandora_console/extras/mr/67.sql +++ b/pandora_console/extras/mr/67.sql @@ -7,4 +7,372 @@ CREATE TABLE IF NOT EXISTS `tdemo_data` ( PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=UTF8MB4; +SET @class_name = 'AgentHive'; +SET @unique_name = 'AgentHive'; +SET @description = 'Agents hive'; +SET @page = 'AgentHive.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'AvgSumMaxMinModule'; +SET @unique_name = 'AvgSumMaxMinModule'; +SET @description = 'Avg|Sum|Max|Min Module Data'; +SET @page = 'AvgSumMaxMinModule.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'BasicChart'; +SET @unique_name = 'BasicChart'; +SET @description = 'Basic chart'; +SET @page = 'BasicChart.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'BlockHistogram'; +SET @unique_name = 'BlockHistogram'; +SET @description = 'Block histogram'; +SET @page = 'BlockHistogram.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'ColorModuleTabs'; +SET @unique_name = 'ColorModuleTabs'; +SET @description = 'Color tabs modules'; +SET @page = 'ColorModuleTabs.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'DataMatrix'; +SET @unique_name = 'DataMatrix'; +SET @description = 'Data Matrix'; +SET @page = 'DataMatrix.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'EventCardboard'; +SET @unique_name = 'EventCardboard'; +SET @description = 'Event cardboard'; +SET @page = 'EventCardboard.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'GroupedMeterGraphs'; +SET @unique_name = 'GroupedMeterGraphs'; +SET @description = 'Grouped meter graphs'; +SET @page = 'GroupedMeterGraphs.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'ModulesByStatus'; +SET @unique_name = 'ModulesByStatus'; +SET @description = 'Modules by status'; +SET @page = 'ModulesByStatus.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'AgentModuleWidget'; +SET @unique_name = 'agent_module'; +SET @description = 'Agent/Module View'; +SET @page = 'agent_module.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'AlertsFiredWidget'; +SET @unique_name = 'alerts_fired'; +SET @description = 'Triggered alerts report'; +SET @page = 'alerts_fired.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'ClockWidget'; +SET @unique_name = 'clock'; +SET @description = 'Clock'; +SET @page = 'clock.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'CustomGraphWidget'; +SET @unique_name = 'custom_graph'; +SET @description = 'Defined custom graph'; +SET @page = 'custom_graph.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'EventsListWidget'; +SET @unique_name = 'events_list'; +SET @description = 'List of latest events'; +SET @page = 'events_list.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'WelcomeWidget'; +SET @unique_name = 'example'; +SET @description = 'Welcome message to Pandora FMS'; +SET @page = 'example.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'GraphModuleHistogramWidget'; +SET @unique_name = 'graph_module_histogram'; +SET @description = 'Module histogram'; +SET @page = 'graph_module_histogram.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'GroupsStatusWidget'; +SET @unique_name = 'groups_status'; +SET @description = 'General group status'; +SET @page = 'groups_status.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'GroupsStatusMapWidget'; +SET @unique_name = 'groups_status_map'; +SET @description = 'Group status map'; +SET @page = 'groups_status_map.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'HeatmapWidget'; +SET @unique_name = 'heatmap'; +SET @description = 'Heatmap'; +SET @page = 'heatmap.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'InventoryWidget'; +SET @unique_name = 'inventory'; +SET @description = 'Inventory'; +SET @page = 'inventory.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'MapsMadeByUser'; +SET @unique_name = 'maps_made_by_user'; +SET @description = 'Visual Console'; +SET @page = 'maps_made_by_user.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'MapsStatusWidget'; +SET @unique_name = 'maps_status'; +SET @description = 'General visual maps report'; +SET @page = 'maps_status.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'ModuleIconWidget'; +SET @unique_name = 'module_icon'; +SET @description = 'Icon and module value'; +SET @page = 'module_icon.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'ModuleStatusWidget'; +SET @unique_name = 'module_status'; +SET @description = 'Module status'; +SET @page = 'module_status.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'ModuleTableValueWidget'; +SET @unique_name = 'module_table_value'; +SET @description = 'Module in a table'; +SET @page = 'module_table_value.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'ModuleValueWidget'; +SET @unique_name = 'module_value'; +SET @description = 'Module value'; +SET @page = 'module_value.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'MonitorHealthWidget'; +SET @unique_name = 'monitor_health'; +SET @description = 'Global health info'; +SET @page = 'monitor_health.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'Netflow'; +SET @unique_name = 'netflow'; +SET @description = 'Netflow'; +SET @page = 'netflow.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'NetworkMapWidget'; +SET @unique_name = 'network_map'; +SET @description = 'Network map'; +SET @page = 'network_map.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'OsQuickReportWidget'; +SET @unique_name = 'os_quick_report'; +SET @description = 'OS quick report'; +SET @page = 'os_quick_report.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'PostWidget'; +SET @unique_name = 'post'; +SET @description = 'Panel with a message'; +SET @page = 'post.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'ReportsWidget'; +SET @unique_name = 'reports'; +SET @description = 'Custom report'; +SET @page = 'reports.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'ServiceMapWidget'; +SET @unique_name = 'service_map'; +SET @description = 'Service map'; +SET @page = 'service_map.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'ServiceViewWidget'; +SET @unique_name = 'service_view'; +SET @description = 'Services view'; +SET @page = 'service_view.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'SingleGraphWidget'; +SET @unique_name = 'single_graph'; +SET @description = 'Agent module graph'; +SET @page = 'single_graph.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'SLAPercentWidget'; +SET @unique_name = 'sla_percent'; +SET @description = 'SLA percentage'; +SET @page = 'sla_percent.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'SystemGroupStatusWidget'; +SET @unique_name = 'system_group_status'; +SET @description = 'Groups status'; +SET @page = 'system_group_status.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'TacticalWidget'; +SET @unique_name = 'tactical'; +SET @description = 'Tactical view'; +SET @page = 'tactical.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'TopNWidget'; +SET @unique_name = 'top_n'; +SET @description = 'Top N of agent modules'; +SET @page = 'top_n.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'TopNEventByGroupWidget'; +SET @unique_name = 'top_n_events_by_group'; +SET @description = 'Top N events by agent'; +SET @page = 'top_n_events_by_group.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'TopNEventByModuleWidget'; +SET @unique_name = 'top_n_events_by_module'; +SET @description = 'Top N events by module'; +SET @page = 'top_n_events_by_module.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'TreeViewWidget'; +SET @unique_name = 'tree_view'; +SET @description = 'Tree view'; +SET @page = 'tree_view.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'UrlWidget'; +SET @unique_name = 'url'; +SET @description = 'URL content'; +SET @page = 'url.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'WuxWidget'; +SET @unique_name = 'wux_transaction'; +SET @description = 'Agent WUX transaction'; +SET @page = 'wux_transaction.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'WuxStatsWidget'; +SET @unique_name = 'wux_transaction_stats'; +SET @description = 'WUX transaction stats'; +SET @page = 'wux_transaction_stats.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + +SET @class_name = 'SecurityHardening'; +SET @unique_name = 'security_hardening'; +SET @description = 'Security Hardening'; +SET @page = 'security_hardening.php'; +SET @widget_id = NULL; +SELECT @widget_id := `id` FROM `twidget` WHERE `unique_name` = @unique_name; +INSERT IGNORE INTO `twidget` (`id`,`class_name`,`unique_name`,`description`,`options`,`page`) VALUES (@widget_id,@class_name,@unique_name,@description,'',@page); + COMMIT; From 7240bbf93446737cb4359868193d23625df05ccf Mon Sep 17 00:00:00 2001 From: "alejandro.campos@artica.es" Date: Wed, 29 Nov 2023 09:00:45 +0100 Subject: [PATCH 13/42] implement demo data load --- pandora_console/godmode/setup/demo.php | 217 ++++++- .../include/ajax/demo_data.ajax.php | 550 ++++++++++++++++-- pandora_console/include/constants.php | 1 + pandora_console/include/functions_agents.php | 11 +- pandora_console/include/functions_users.php | 2 +- .../include/lib/Dashboard/Manager.php | 2 +- .../include/lib/Dashboard/Widget.php | 4 +- 7 files changed, 728 insertions(+), 59 deletions(-) diff --git a/pandora_console/godmode/setup/demo.php b/pandora_console/godmode/setup/demo.php index 800701a2b9..df3af0bc02 100644 --- a/pandora_console/godmode/setup/demo.php +++ b/pandora_console/godmode/setup/demo.php @@ -49,7 +49,8 @@ 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', 0); +$interval = get_parameter('interval', 300); +$service_agent_id = (int) get_parameter('service_agent_id', 0); // Map directory and demo item ID. $dir_item_id_map = [ @@ -71,6 +72,15 @@ $enabled_items = [ 'enable_history' => (int) get_parameter('enable_history', 0), ]; +$generate_hist = (int) get_parameter('enable_history', $def_value); + +$plugin_agent = (int) get_parameter('plugin_agent', 0); +$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'); +$tentacle_port = get_parameter('tentacle_port', '41121'); +$tentacle_extra_options = get_parameter('tentacle_extra_options', ''); + $demo_items_count = (int) db_get_value('count(*)', 'tdemo_data'); $current_progress_val = db_get_value_filter( @@ -146,7 +156,9 @@ if ($display_loading === true || $running_create === true || $running_delete) { unset($items_ids_text_map[DEMO_SERVICE]); } + $items_ids_text_map[DEMO_PLUGIN] = 'plugin'; $items_ids_text_map = ([DEMO_AGENT => 'agents'] + $items_ids_text_map); + $list_mkup = '
      '; foreach ($items_ids_text_map as $item_id => $item_text) { $list_mkup .= '
    • '; @@ -233,7 +245,7 @@ if ($display_loading === true || $running_create === true || $running_delete) { $otherData = []; $table_aux->data['row1'][] = html_print_label_input_block( - __('Agents'), + __('Agents').ui_print_help_tip(__('You may need to increase the value of the plugin_timeout parameter in your server configuration to get all your agents data updated'), true), html_print_div( [ 'class' => '', @@ -296,7 +308,7 @@ if ($display_loading === true || $running_create === true || $running_delete) { html_print_checkbox_switch( 'enable_history', 1, - false, + (bool) $generate_hist, true ) ); @@ -327,9 +339,27 @@ if ($display_loading === true || $running_create === true || $running_delete) { true ) ); + + $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'] + ); } - $table_adv->data['row4'][] = html_print_label_input_block( + $table_adv->data['row5'][] = html_print_label_input_block( __('Create network maps'), html_print_checkbox_switch( 'enable_nm', @@ -339,7 +369,7 @@ if ($display_loading === true || $running_create === true || $running_delete) { ) ); - $table_adv->data['row5'][] = html_print_label_input_block( + $table_adv->data['row6'][] = html_print_label_input_block( __('Create custom graphs'), html_print_checkbox_switch( 'enable_cg', @@ -349,7 +379,7 @@ if ($display_loading === true || $running_create === true || $running_delete) { ) ); - $table_adv->data['row6'][] = html_print_label_input_block( + $table_adv->data['row7'][] = html_print_label_input_block( __('Create reports'), html_print_checkbox_switch( 'enable_rep', @@ -359,7 +389,7 @@ if ($display_loading === true || $running_create === true || $running_delete) { ) ); - $table_adv->data['row7'][] = html_print_label_input_block( + $table_adv->data['row8'][] = html_print_label_input_block( __('Create visual consoles'), html_print_checkbox_switch( 'enable_vc', @@ -369,6 +399,123 @@ if ($display_loading === true || $running_create === true || $running_delete) { ) ); + $table_adv->data['row9'][] = html_print_label_input_block( + __('Create dashboards'), + html_print_checkbox_switch( + 'enable_dashboards', + 1, + $enabled_items['dashboards'], + true + ) + ); + + $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'] + ); + + $table_adv->data['row11'][] = html_print_label_input_block( + __('Traps target IP').ui_print_help_tip(__('All demo traps are generated using version 1'), true), + html_print_input_text( + 'traps_target_ip', + $traps_target_ip, + '', + 50, + 255, + true, + false, + false, + '', + 'w300px' + ) + ); + + $table_adv->data['row12'][] = html_print_label_input_block( + __('Traps community'), + html_print_input_text( + 'traps_community', + $traps_community, + '', + 50, + 255, + true, + false, + false, + '', + 'w300px' + ) + ); + + $table_adv->data['row13'][] = html_print_label_input_block( + __('Tentacle target IP'), + html_print_input_text( + 'tentacle_target_ip', + $tentacle_target_ip, + '', + 50, + 255, + true, + false, + false, + '', + 'w300px' + ) + ); + + $table_adv->data['row14'][] = html_print_label_input_block( + __('Tentacle port'), + html_print_input_text( + 'tentacle_port', + $tentacle_port, + '', + 50, + 255, + true, + false, + false, + '', + 'w300px' + ) + ); + + $table_adv->data['row15'][] = html_print_label_input_block( + __('Tentacle extra options'), + html_print_input_text( + 'tentacle_extra_options', + $tentacle_extra_options, + '', + 50, + 255, + true, + false, + false, + '', + 'w300px' + ) + ); + + /* $table_adv->data['row8'][] = html_print_label_input_block( + __('Create dashboards'), + html_print_checkbox_switch( + 'enable_dashboards', + 1, + $enabled_items['dashboards'], + true + ) + ); $table_adv->data['row8'][] = html_print_label_input_block( __('Create dashboards'), html_print_checkbox_switch( @@ -378,6 +525,33 @@ if ($display_loading === true || $running_create === true || $running_delete) { true ) ); + $table_adv->data['row8'][] = html_print_label_input_block( + __('Create dashboards'), + html_print_checkbox_switch( + 'enable_dashboards', + 1, + $enabled_items['dashboards'], + true + ) + ); + $table_adv->data['row8'][] = html_print_label_input_block( + __('Create dashboards'), + html_print_checkbox_switch( + 'enable_dashboards', + 1, + $enabled_items['dashboards'], + true + ) + ); + $table_adv->data['row8'][] = html_print_label_input_block( + __('Create dashboards'), + html_print_checkbox_switch( + 'enable_dashboards', + 1, + $enabled_items['dashboards'], + true + ) + );*/ echo ''; echo '
      '; @@ -492,6 +666,10 @@ if ($display_loading === true || $running_create === true || $running_delete) { ?> \ No newline at end of file diff --git a/pandora_console/include/ajax/demo_data.ajax.php b/pandora_console/include/ajax/demo_data.ajax.php index 0eab652fe2..6b5f96c325 100644 --- a/pandora_console/include/ajax/demo_data.ajax.php +++ b/pandora_console/include/ajax/demo_data.ajax.php @@ -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']]; From 93e79b51ebaf99c70d898d26d05602f3bad0fd0a Mon Sep 17 00:00:00 2001 From: Enrique Martin Date: Wed, 29 Nov 2023 17:30:57 +0100 Subject: [PATCH 19/42] Changed traps chance --- pandora_console/extras/demodata/agents/5-cisco.prd | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandora_console/extras/demodata/agents/5-cisco.prd b/pandora_console/extras/demodata/agents/5-cisco.prd index 58febab7fe..9acab7907b 100644 --- a/pandora_console/extras/demodata/agents/5-cisco.prd +++ b/pandora_console/extras/demodata/agents/5-cisco.prd @@ -270,24 +270,24 @@ group[37]="Networking" oid[1]=".1.3.6.1.4.1.9.9.41.1.2.3.1.2.0" value[1]="RANDOM;0;100" snmp_type[1]="6" -chance_percent[1]="5" +chance_percent[1]="30" oid[2]=".1.3.6.1.4.1.9.9.41.1.2.3.1.3.0" value[2]="RANDOM;0;100" snmp_type[2]="6" -chance_percent[2]="5" +chance_percent[2]="30" oid[3]=".1.3.6.1.4.1.9.9.41.1.2.3.1.4.0" value[3]="RANDOM;0;100" snmp_type[3]="6" -chance_percent[3]="5" +chance_percent[3]="30" oid[4]=".1.3.6.1.4.1.9.9.41.1.2.3.1.5.0" value[4]="RANDOM;0;100" snmp_type[4]="6" -chance_percent[4]="5" +chance_percent[4]="30" oid[5]=".1.3.6.1.4.1.9.9.41.1.2.3.1.6.0" value[5]="RANDOM;0;100" snmp_type[5]="6" -chance_percent[5]="5" \ No newline at end of file +chance_percent[5]="30" \ No newline at end of file From 50ab2841e708eca66edb9ec7e176a73acf77447b Mon Sep 17 00:00:00 2001 From: Enrique Martin Date: Wed, 29 Nov 2023 18:02:33 +0100 Subject: [PATCH 20/42] Removed some elements from network map PRD --- .../demodata/network_maps/1-networkmap.prd | 47 +------------------ 1 file changed, 1 insertion(+), 46 deletions(-) diff --git a/pandora_console/extras/demodata/network_maps/1-networkmap.prd b/pandora_console/extras/demodata/network_maps/1-networkmap.prd index f780eb0977..1bfe9bb207 100644 --- a/pandora_console/extras/demodata/network_maps/1-networkmap.prd +++ b/pandora_console/extras/demodata/network_maps/1-networkmap.prd @@ -49,49 +49,4 @@ parent[8]="5" agent_name[9]="linux-4" x[9]="226" y[9]="595" -parent[9]="5" - -agent_name[10]="cisco-2" -x[10]="1036" -y[10]="711" -parent[10]="1" - -agent_name[11]="freebsd-2" -x[11]="892" -y[11]="-47" -parent[11]="1" - -agent_name[12]="macos-2" -x[12]="1150" -y[12]="409" -parent[12]="1" - -agent_name[13]="windows-3" -x[13]="686" -y[13]="875" -parent[13]="10" - -agent_name[14]="windows-4" -x[14]="1452" -y[14]="847" -parent[14]="10" - -agent_name[15]="linux-5" -x[15]="314" -y[15]="845" -parent[15]="13" - -agent_name[16]="linux-6" -x[16]="474" -y[16]="1043" -parent[16]="13" - -agent_name[17]="linux-7" -x[17]="1812" -y[17]="1011" -parent[17]="14" - -agent_name[18]="linux-8" -x[18]="1828" -y[18]="723" -parent[18]="14" \ No newline at end of file +parent[9]="5" \ No newline at end of file From ca17eef68a62c63f8f984494e7dbc88aca151aa6 Mon Sep 17 00:00:00 2001 From: "alejandro.campos@artica.es" Date: Wed, 29 Nov 2023 18:09:32 +0100 Subject: [PATCH 21/42] implement load demo data --- .../include/ajax/demo_data.ajax.php | 102 +++++++++++++----- .../include/class/WelcomeWindow.class.php | 2 +- 2 files changed, 76 insertions(+), 28 deletions(-) diff --git a/pandora_console/include/ajax/demo_data.ajax.php b/pandora_console/include/ajax/demo_data.ajax.php index 6b5f96c325..bbc8258f34 100644 --- a/pandora_console/include/ajax/demo_data.ajax.php +++ b/pandora_console/include/ajax/demo_data.ajax.php @@ -84,8 +84,10 @@ if ($action === 'create_demo_data') { } else { $enabled_directories = $directories; + // Set default values when advanced mode is disabled. $service_agent_name = 'demo-global-agent-1'; $plugin_agent_name = 'demo-global-agent-1'; + $days_hist_data = 15; } if (enterprise_installed() === false) { @@ -411,7 +413,9 @@ if ($action === 'create_demo_data') { $current_date_time = $date_time->format('Y-m-d H:i:s'); $back_periods = 1; - if ($adv_options_is_enabled === true && $history_is_enabled === true) { + if ($adv_options_is_enabled === false + || ($adv_options_is_enabled === true && $history_is_enabled === true) + ) { $back_periods = round(($days_hist_data * SECONDS_1DAY) / $interval); } @@ -491,7 +495,9 @@ if ($action === 'create_demo_data') { } } - if ($adv_options_is_enabled === true && $history_is_enabled === true) { + if ($adv_options_is_enabled === false + || ($adv_options_is_enabled === true && $history_is_enabled === true) + ) { $utimestamp -= $interval; } } @@ -655,7 +661,9 @@ if ($action === 'create_demo_data') { $current_date_time = $date_time->format('Y-m-d H:i:s'); $back_periods = 1; - if ($adv_options_is_enabled === true && $history_is_enabled === true) { + if ($adv_options_is_enabled === false + || ($adv_options_is_enabled === true && $history_is_enabled === true) + ) { $back_periods = round(($days_hist_data * SECONDS_1DAY) / $interval); } @@ -754,7 +762,9 @@ if ($action === 'create_demo_data') { } } - if ($adv_options_is_enabled === true && $history_is_enabled === true) { + if ($adv_options_is_enabled === false + || ($adv_options_is_enabled === true && $history_is_enabled === true) + ) { $date_time->sub(new DateInterval("PT{$interval}S")); $current_date_time = $date_time->format('Y-m-d H:i:s'); $utimestamp -= $interval; @@ -1714,7 +1724,7 @@ if ($action === 'create_demo_data') { $item_values = []; $item_values['id_report'] = $created_report_id; - $item_values['name'] = $items_array['name']; + $item_values['name'] = io_safe_input($items_array['name']); $item_values['type'] = $items_array['type']; if (isset($items_array['agent_name']) === true) { @@ -1814,6 +1824,43 @@ if ($action === 'create_demo_data') { continue; } + + if ($items_array['type'] === 'SLA') { + $sla_values = [ + 'id_report_content' => $created_report_item_id, + 'id_agent_module' => $item_values['id_agent_module'], + ]; + + $created_report_content_sla_id = db_process_sql_insert('treport_content_sla_combined', $sla_values); + + if ($created_report_content_sla_id > 0) { + // Register created demo item in tdemo_data. + $values = [ + 'item_id' => $created_report_content_sla_id, + 'table_name' => 'treport_content_sla_combined', + ]; + $result = (bool) db_process_sql_insert('tdemo_data', $values); + + if ($result === false) { + // Rollback report item if could not be registered in tdemo_data. + db_process_sql_delete('treport_content_sla_combined', ['id' => $created_report_content_sla_id]); + + register_error( + DEMO_REPORT, + __('Uncaught error (source %s): could not create custom report item with index %d', $filename, ($item_access_idx - 1)) + ); + + continue; + } + } else { + register_error( + DEMO_REPORT, + __('Uncaught error (source %s): could not create custom report item with index %d', $filename, ($item_access_idx - 1)) + ); + + continue; + } + } } else { register_error( DEMO_REPORT, @@ -2667,28 +2714,29 @@ if ($action === 'cleanup_demo_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', - 'tgraph' => 'id_graph', - 'tmap' => 'id', - 'treport' => 'id_report', - 'treport_content' => 'id_rc', - 'tservice' => 'id', - 'tservice_element' => 'id', - 'ttrap' => 'id_trap', - 'titem' => 'id', - 'tgraph_source' => 'id_gs', - 'twidget_dashboard' => 'id', - 'tdashboard' => 'id', - 'tlayout' => 'id', - 'tlayout_data' => 'id', - 'tagente_estado' => 'id_agente_estado', - 'trel_item' => 'id', - 'tplugin' => 'id', + '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', + 'tgraph' => 'id_graph', + 'tmap' => 'id', + 'treport' => 'id_report', + 'treport_content' => 'id_rc', + 'treport_content_sla_combined' => 'id', + 'tservice' => 'id', + 'tservice_element' => 'id', + 'ttrap' => 'id_trap', + 'titem' => 'id', + 'tgraph_source' => 'id_gs', + 'twidget_dashboard' => 'id', + 'tdashboard' => 'id', + 'tlayout' => 'id', + 'tlayout_data' => 'id', + 'tagente_estado' => 'id_agente_estado', + 'trel_item' => 'id', + 'tplugin' => 'id', ]; $table_id_field = $table_id_field_dict[$item['table_name']]; diff --git a/pandora_console/include/class/WelcomeWindow.class.php b/pandora_console/include/class/WelcomeWindow.class.php index c0800e8b4c..b51e753048 100644 --- a/pandora_console/include/class/WelcomeWindow.class.php +++ b/pandora_console/include/class/WelcomeWindow.class.php @@ -1158,7 +1158,7 @@ class WelcomeWindow extends Wizard // Task to do actions. function loadDemoDataPage() { - window.location = ''; + window.location = ''; } function openCreateModulesDialog() { From dc2ac2a1b355a01480f96fa933864449ab0c2d65 Mon Sep 17 00:00:00 2001 From: "alejandro.campos@artica.es" Date: Wed, 29 Nov 2023 18:13:56 +0100 Subject: [PATCH 22/42] implement load demo data --- pandora_console/include/ajax/demo_data.ajax.php | 1 + 1 file changed, 1 insertion(+) diff --git a/pandora_console/include/ajax/demo_data.ajax.php b/pandora_console/include/ajax/demo_data.ajax.php index bbc8258f34..41e40e4604 100644 --- a/pandora_console/include/ajax/demo_data.ajax.php +++ b/pandora_console/include/ajax/demo_data.ajax.php @@ -87,6 +87,7 @@ if ($action === 'create_demo_data') { // Set default values when advanced mode is disabled. $service_agent_name = 'demo-global-agent-1'; $plugin_agent_name = 'demo-global-agent-1'; + $interval = 300; $days_hist_data = 15; } From 76327c6d737be80d2c7411b66d61ca2e5fc7cf9a Mon Sep 17 00:00:00 2001 From: Enrique Martin Date: Thu, 30 Nov 2023 08:54:21 +0100 Subject: [PATCH 23/42] Fixed demo graphs generation --- .../visual_consoles/1-linux-visual-console.prd | 6 +++--- .../include/ajax/demo_data.ajax.php | 18 ++++++++---------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/pandora_console/extras/demodata/visual_consoles/1-linux-visual-console.prd b/pandora_console/extras/demodata/visual_consoles/1-linux-visual-console.prd index e56a93d350..299d2296ed 100644 --- a/pandora_console/extras/demodata/visual_consoles/1-linux-visual-console.prd +++ b/pandora_console/extras/demodata/visual_consoles/1-linux-visual-console.prd @@ -114,7 +114,7 @@ type[11]="module_graph" image[11]="black" agent_name[11]="linux-1" module[11]="CPU Load" -interval[11]="86N00" +interval[11]="86400" graph_type[11]="area" label[11]="" label_position[11]="down" @@ -127,7 +127,7 @@ type[12]="module_graph" image[12]="black" agent_name[12]="linux-1" module[12]="Memory Usage" -interval[12]="86N00" +interval[12]="86400" graph_type[12]="area" label[12]="" label_position[12]="down" @@ -140,7 +140,7 @@ type[13]="module_graph" image[13]="black" agent_name[13]="linux-1" module[13]="Disk Usage" -interval[13]="86N00" +interval[13]="86400" graph_type[13]="area" label[13]="" label_position[13]="down" diff --git a/pandora_console/include/ajax/demo_data.ajax.php b/pandora_console/include/ajax/demo_data.ajax.php index 41e40e4604..2cbac20e99 100644 --- a/pandora_console/include/ajax/demo_data.ajax.php +++ b/pandora_console/include/ajax/demo_data.ajax.php @@ -2113,19 +2113,13 @@ if ($action === 'create_demo_data') { if (isset($items_array['graph_type']) === true) { $element_values['type_graph'] = $items_array['graph_type']; } + + if (isset($items_array['image']) === true) { + $element_values['image'] = $items_array['image']; + } } 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 @@ -2142,6 +2136,10 @@ if ($action === 'create_demo_data') { if (isset($items_array['interval']) === true) { $element_values['period'] = $items_array['interval']; } + + if (isset($items_array['image']) === true) { + $element_values['image'] = $items_array['image']; + } } if ($items_array['type'] === 'icon') { From 3807baa8ef46ac4ea4a8a146e3a0419360ffd87f Mon Sep 17 00:00:00 2001 From: Enrique Martin Date: Thu, 30 Nov 2023 17:41:01 +0100 Subject: [PATCH 24/42] Added GIS random data to agents --- .../extras/demodata/agents/0-demo-agent.prd | 4 + .../extras/demodata/agents/1-linux.prd | 4 + .../extras/demodata/agents/2-windows.prd | 4 + .../extras/demodata/agents/3-macosx.prd | 4 + .../extras/demodata/agents/4-freebsd.prd | 4 + .../extras/demodata/agents/5-cisco.prd | 4 + .../demodata/network_maps/1-networkmap.prd | 92 ++++++++++++++++++- .../include/ajax/demo_data.ajax.php | 59 ++++++++++++ 8 files changed, 174 insertions(+), 1 deletion(-) diff --git a/pandora_console/extras/demodata/agents/0-demo-agent.prd b/pandora_console/extras/demodata/agents/0-demo-agent.prd index af1fa72a66..1a45fb939c 100644 --- a/pandora_console/extras/demodata/agents/0-demo-agent.prd +++ b/pandora_console/extras/demodata/agents/0-demo-agent.prd @@ -18,6 +18,10 @@ os_versions[]="RHEL 8.2" os_versions[]="RHEL 8.1" os_versions[]="Alma Linux 9.1" +latitude="RANDOM;37;42" +longitude="RANDOM;-8;-1" +altitude="0" + [modules] name[1]="Host Alive" diff --git a/pandora_console/extras/demodata/agents/1-linux.prd b/pandora_console/extras/demodata/agents/1-linux.prd index 5cd29b117e..35d76f3f79 100644 --- a/pandora_console/extras/demodata/agents/1-linux.prd +++ b/pandora_console/extras/demodata/agents/1-linux.prd @@ -18,6 +18,10 @@ os_versions[]="RHEL 8.2" os_versions[]="RHEL 8.1" os_versions[]="Alma Linux 9.1" +latitude="RANDOM;37;42" +longitude="RANDOM;-8;-1" +altitude="0" + [modules] name[1]="Network Usage" diff --git a/pandora_console/extras/demodata/agents/2-windows.prd b/pandora_console/extras/demodata/agents/2-windows.prd index 1d1d6c782c..921abeb7dc 100644 --- a/pandora_console/extras/demodata/agents/2-windows.prd +++ b/pandora_console/extras/demodata/agents/2-windows.prd @@ -17,6 +17,10 @@ os_versions[]="Home Microsoft Windows 10 Pro" os_versions[]="Microsoft Windows 10 Home" os_versions[]="Microsoft Windows 2018" +latitude="RANDOM;37;42" +longitude="RANDOM;-8;-1" +altitude="0" + [modules] name[1]="Network Usage" diff --git a/pandora_console/extras/demodata/agents/3-macosx.prd b/pandora_console/extras/demodata/agents/3-macosx.prd index ba8d75de87..0bbf20edbb 100644 --- a/pandora_console/extras/demodata/agents/3-macosx.prd +++ b/pandora_console/extras/demodata/agents/3-macosx.prd @@ -17,6 +17,10 @@ os_versions[]="11.2" os_versions[]="11.3" os_versions[]="10.3" +latitude="RANDOM;37;42" +longitude="RANDOM;-8;-1" +altitude="0" + [modules] name[1]="Network Usage" diff --git a/pandora_console/extras/demodata/agents/4-freebsd.prd b/pandora_console/extras/demodata/agents/4-freebsd.prd index 956a501660..c3d9d3eba2 100644 --- a/pandora_console/extras/demodata/agents/4-freebsd.prd +++ b/pandora_console/extras/demodata/agents/4-freebsd.prd @@ -16,6 +16,10 @@ os_versions[]="13.1" os_versions[]="13.2" os_versions[]="13.3" +latitude="RANDOM;37;42" +longitude="RANDOM;-8;-1" +altitude="0" + [modules] name[1]="Network Usage" diff --git a/pandora_console/extras/demodata/agents/5-cisco.prd b/pandora_console/extras/demodata/agents/5-cisco.prd index 9acab7907b..1c8e3ff0a1 100644 --- a/pandora_console/extras/demodata/agents/5-cisco.prd +++ b/pandora_console/extras/demodata/agents/5-cisco.prd @@ -16,6 +16,10 @@ os_versions[]="C9200L" os_versions[]="C9300LM" os_versions[]="C9600" +latitude="RANDOM;37;42" +longitude="RANDOM;-8;-1" +altitude="0" + [modules] name[1]="CPU Usage (5 min avg)" diff --git a/pandora_console/extras/demodata/network_maps/1-networkmap.prd b/pandora_console/extras/demodata/network_maps/1-networkmap.prd index 1bfe9bb207..7ef0db5db8 100644 --- a/pandora_console/extras/demodata/network_maps/1-networkmap.prd +++ b/pandora_console/extras/demodata/network_maps/1-networkmap.prd @@ -49,4 +49,94 @@ parent[8]="5" agent_name[9]="linux-4" x[9]="226" y[9]="595" -parent[9]="5" \ No newline at end of file +parent[9]="5" + +agent_name[10]="cisco-2" +x[10]="1036" +y[10]="711" +parent[10]="1" + +agent_name[11]="freebsd-2" +x[11]="892" +y[11]="-47" +parent[11]="1" + +agent_name[12]="macos-2" +x[12]="1150" +y[12]="409" +parent[12]="1" + +agent_name[13]="windows-3" +x[13]="686" +y[13]="875" +parent[13]="10" + +agent_name[14]="windows-4" +x[14]="1452" +y[14]="847" +parent[14]="10" + +agent_name[15]="linux-5" +x[15]="314" +y[15]="845" +parent[15]="13" + +agent_name[16]="linux-6" +x[16]="474" +y[16]="1043" +parent[16]="13" + +agent_name[17]="linux-7" +x[17]="1812" +y[17]="1011" +parent[17]="14" + +agent_name[18]="linux-8" +x[18]="1828" +y[18]="723" +parent[18]="14" + +agent_name[19]="cisco-3" +x[19]="1824" +y[19]="285" +parent[19]="3" + +agent_name[20]="freebsd-3" +x[20]="-82" +y[20]="583" +parent[20]="9" + +agent_name[21]="macos-3" +x[21]="-140" +y[21]="299" +parent[21]="20" + +agent_name[22]="windows-5" +x[22]="-390" +y[22]="385" +parent[22]="20" + +agent_name[23]="windows-6" +x[23]="-470" +y[23]="671" +parent[23]="20" + +agent_name[24]="linux-9" +x[24]="-296" +y[24]="899" +parent[24]="20" + +agent_name[25]="linux-10" +x[25]="-25" +y[25]="961" +parent[25]="20" + +agent_name[26]="linux-11" +x[26]="1683" +y[26]="13" +parent[26]="19" + +agent_name[27]="linux-12" +x[27]="2135" +y[27]="51" +parent[27]="19" \ No newline at end of file diff --git a/pandora_console/include/ajax/demo_data.ajax.php b/pandora_console/include/ajax/demo_data.ajax.php index 2cbac20e99..77b1fedef1 100644 --- a/pandora_console/include/ajax/demo_data.ajax.php +++ b/pandora_console/include/ajax/demo_data.ajax.php @@ -264,6 +264,37 @@ if ($action === 'create_demo_data') { reset($os_versions); } + $latitude = 0; + $longitude = 0; + $altitude = 0; + + if (isset($agent_data['latitude']) === true) { + $gis_parsed = explode(';', $agent_data['latitude']); + if ((string) $gis_parsed[0] === 'RANDOM') { + $latitude = rand($gis_parsed[1], $gis_parsed[2]); + } else { + $latitude = $agent_data['latitude']; + } + } + + if (isset($agent_data['longitude']) === true) { + $gis_parsed = explode(';', $agent_data['longitude']); + if ((string) $gis_parsed[0] === 'RANDOM') { + $longitude = rand($gis_parsed[1], $gis_parsed[2]); + } else { + $longitude = $agent_data['longitude']; + } + } + + if (isset($agent_data['altitude']) === true) { + $gis_parsed = explode(';', $agent_data['altitude']); + if ((string) $gis_parsed[0] === 'RANDOM') { + $altitude = rand($gis_parsed[1], $gis_parsed[2]); + } else { + $altitude = $agent_data['altitude']; + } + } + $values = [ 'server_name' => $server_name, 'id_os' => $id_os, @@ -310,6 +341,34 @@ if ($action === 'create_demo_data') { continue; } + // Register GIS data + $values = [ + 'tagente_id_agente' => $created_agent_id, + 'current_longitude' => $longitude, + 'current_latitude' => $latitude, + 'current_altitude' => $altitude, + 'stored_longitude' => $longitude, + 'stored_latitude' => $latitude, + 'stored_altitude' => $altitude, + 'number_of_packages' => 1, + 'manual_placement' => 1, + ]; + $result = db_process_sql_insert('tgis_data_status', $values); + + if ($result !== false) { + $values = [ + 'item_id' => $created_agent_id, + 'table_name' => 'tgis_data_status', + ]; + $result = (bool) db_process_sql_insert('tdemo_data', $values); + + if ($result === false) { + // Rollback GIS data creation if could not be registered in tdemo_data. + db_process_sql_delete('tgis_data_status', ['tagente_id_agente' => $created_agent_id]); + } + } + + $agents_created_count[$agent_data['agent_alias']]++; $iter_agents_created++; From d6e107ea95303eff08173d25a0b1321b7c8965ff Mon Sep 17 00:00:00 2001 From: "alejandro.campos@artica.es" Date: Fri, 1 Dec 2023 09:48:45 +0100 Subject: [PATCH 25/42] performance improvement --- .../include/ajax/demo_data.ajax.php | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/pandora_console/include/ajax/demo_data.ajax.php b/pandora_console/include/ajax/demo_data.ajax.php index 41e40e4604..f8582a0075 100644 --- a/pandora_console/include/ajax/demo_data.ajax.php +++ b/pandora_console/include/ajax/demo_data.ajax.php @@ -114,7 +114,6 @@ if ($action === 'create_demo_data') { } $total_agents_to_create = (int) get_parameter('agents_num', 0); - $total_items_count = count($parsed_ini); if ($total_agents_to_create > 0) { @@ -134,6 +133,7 @@ if ($action === 'create_demo_data') { } $agent_created_total = 0; + $agent_data_values_buffer = []; if ($total_agents_to_create > 0 && $agents_to_create > 0) { while ($agent_created_total < ($total_agents_to_create - 1)) { @@ -311,7 +311,7 @@ if ($action === 'create_demo_data') { } $agents_created_count[$agent_data['agent_alias']]++; - + $iter_agents_created++; // Create agent modules. @@ -439,6 +439,8 @@ if ($action === 'create_demo_data') { if ($randomNumber <= $probability) { // Set to 0 with a certain probability. $data = 0; + + // Set to critical status if 0. $new_status = 1; } } @@ -450,13 +452,15 @@ if ($action === 'create_demo_data') { 'utimestamp' => $utimestamp, ]; - $created_data_res = db_process_sql_insert('tagente_datos', $agent_data_values); - - if ($created_data_res === false) { - continue; - } - if ($p === 0) { + // Insert current module data right away so module status is initialized with such value. + $created_data_res = db_process_sql_insert('tagente_datos', $agent_data_values); + + if ($created_data_res === false) { + continue; + } + + // Proceed to update module status. $status_values = [ 'datos' => $data, 'estado' => $new_status, @@ -494,6 +498,9 @@ if ($action === 'create_demo_data') { db_process_sql_delete('tagente_estado', ['id_agente_estado' => $status_id]); } } + } else { + // Buffer history data for later bulk insertion (performance reasons). + $agent_data_values_buffer[] = $agent_data_values; } if ($adv_options_is_enabled === false @@ -784,6 +791,17 @@ if ($action === 'create_demo_data') { } } + $agent_data_values_buffer_chunks = array_chunk($agent_data_values_buffer, 1000); + + foreach ($agent_data_values_buffer_chunks as $chunk) { + // Bulk inserts. + mysql_db_process_sql_insert_multiple( + 'tagente_datos', + $chunk, + false + ); + } + update_item_checked(DEMO_AGENT); } } From 14fc8ef2b1958f10f9f13bbacd46c683d7f873fa Mon Sep 17 00:00:00 2001 From: Enrique Martin Date: Sun, 3 Dec 2023 21:30:17 +0100 Subject: [PATCH 26/42] Added new items to visual consoles and fixed unknown modules on creation --- .../include/ajax/demo_data.ajax.php | 270 ++++++++++++++++-- 1 file changed, 250 insertions(+), 20 deletions(-) diff --git a/pandora_console/include/ajax/demo_data.ajax.php b/pandora_console/include/ajax/demo_data.ajax.php index 5a50eb4979..8a1c5b740a 100644 --- a/pandora_console/include/ajax/demo_data.ajax.php +++ b/pandora_console/include/ajax/demo_data.ajax.php @@ -295,10 +295,15 @@ if ($action === 'create_demo_data') { } } + $date_time = new DateTime(); + $current_date_time = $date_time->format('Y-m-d H:i:s'); + $values = [ - 'server_name' => $server_name, - 'id_os' => $id_os, - 'os_version' => $os_version, + 'server_name' => $server_name, + 'id_os' => $id_os, + 'os_version' => $os_version, + 'ultimo_contacto' => $current_date_time, + 'ultimo_contacto_remoto' => $current_date_time, ]; $create_alias = $agent_data['agent_alias'].'-'.($agents_created_count[$agent_data['agent_alias']] + 1); @@ -528,6 +533,7 @@ if ($action === 'create_demo_data') { 'utimestamp' => $utimestamp, 'last_status' => 0, 'last_known_status' => 0, + 'current_interval' => $agent_interval, ]; $status_id = db_get_value( @@ -2052,14 +2058,28 @@ if ($action === 'create_demo_data') { // Map used types. $types = [ - 'static_image' => 0, - 'module_graph' => 1, - 'custom_graph' => 1, - 'value' => 2, - 'label' => 4, - 'icon' => 5, + 'static_image' => 0, + 'module_graph' => 1, + 'custom_graph' => 1, + 'value' => 2, + 'percentile' => 3, + 'label' => 4, + 'icon' => 5, + 'bubble' => 9, + 'event_history' => 14, + 'circular_progress_bar' => 15, + 'circular_progress_bar_int' => 16, + 'color_cloud' => 20, + 'odometer' => 22, + 'basic_chart' => 23, ]; + $value_process_types = [ + 'max' => 6, + 'min' => 7, + 'avg' => 8, + ] + // Get ID of item type. Skip if it does not exist. if (isset($types[$items_array['type']]) === false) { register_error( @@ -2072,6 +2092,16 @@ if ($action === 'create_demo_data') { $element_values = []; $element_values['type'] = $types[$items_array['type']]; + if ($items_array['type'] == 'value') { + if (isset($items_array['process']) === true && isset($value_process_types[$items_array['process']])) { + $element_values['type'] = $value_process_types[$items_array['process']] + + if (isset($items_array['interval']) === true) { + $element_values['period'] = $items_array['interval']; + } + } + } + $element_values['id_layout'] = $created_id; if ($items_array['type'] === 'static_image') { @@ -2136,17 +2166,6 @@ 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']], @@ -2219,6 +2238,166 @@ if ($action === 'create_demo_data') { } } + if ($items_array['type'] === 'basic_chart') { + if (isset($items_array['agent_name']) === true) { + $matched_agents = agents_get_agents( + ['nombre' => $items_array['agent_name']], + ['id_agente'], + 'AR', + [ + 'field' => 'nombre', + 'order' => 'ASC', + ], + false, + 0, + false, + false, + false + ); + $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 ($items_array['type'] === 'event_history') { + if (isset($items_array['agent_name']) === true) { + $matched_agents = agents_get_agents( + ['nombre' => $items_array['agent_name']], + ['id_agente'], + 'AR', + [ + 'field' => 'nombre', + 'order' => 'ASC', + ], + false, + 0, + false, + false, + false + ); + $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 ($items_array['type'] === 'odometer') { + if (isset($items_array['agent_name']) === true) { + $matched_agents = agents_get_agents( + ['nombre' => $items_array['agent_name']], + ['id_agente'], + 'AR', + [ + 'field' => 'nombre', + 'order' => 'ASC', + ], + false, + 0, + false, + false, + false + ); + $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 ($items_array['type'] === 'color_cloud') { + if (isset($items_array['agent_name']) === true) { + $matched_agents = agents_get_agents( + ['nombre' => $items_array['agent_name']], + ['id_agente'], + 'AR', + [ + 'field' => 'nombre', + 'order' => 'ASC', + ], + false, + 0, + false, + false, + false + ); + $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 ($items_array['type'] === 'icon') { if (isset($items_array['image']) === false || is_string($items_array['image']) === false @@ -2306,6 +2485,57 @@ if ($action === 'create_demo_data') { $element_values['height'] = $items_array['height']; } + // Check here percentile items as height is used for max value + if ($items_array['type'] === 'percentile' || $items_array['type'] === 'bubble' || $items_array['type'] === 'circular_progress_bar' || $items_array['type'] === 'circular_progress_bar_int') { + if (isset($items_array['agent_name']) === true) { + $matched_agents = agents_get_agents( + ['nombre' => $items_array['agent_name']], + ['id_agente'], + 'AR', + [ + 'field' => 'nombre', + 'order' => 'ASC', + ], + false, + 0, + false, + false, + false + ); + $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; + } + } + + $element_values['border_width'] = 0; + if (isset($items_array['min']) === true) { + $element_values['border_width'] = $items_array['min']; + } + + $element_values['height'] = 100; + if (isset($items_array['max']) === true) { + $element_values['height'] = $items_array['max']; + } + + $element_values['image'] = 'percent'; + } + $id = db_process_sql_insert('tlayout_data', $element_values); if ($id > 0) { From b14c77862e6ad5f283fac1f81c5a8f903ff96b8d Mon Sep 17 00:00:00 2001 From: Enrique Martin Date: Sun, 3 Dec 2023 21:43:00 +0100 Subject: [PATCH 27/42] Added new items to visual consoles --- pandora_console/include/ajax/demo_data.ajax.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pandora_console/include/ajax/demo_data.ajax.php b/pandora_console/include/ajax/demo_data.ajax.php index 8a1c5b740a..af6a2af628 100644 --- a/pandora_console/include/ajax/demo_data.ajax.php +++ b/pandora_console/include/ajax/demo_data.ajax.php @@ -2066,6 +2066,7 @@ if ($action === 'create_demo_data') { 'label' => 4, 'icon' => 5, 'bubble' => 9, + 'box' => 12, 'event_history' => 14, 'circular_progress_bar' => 15, 'circular_progress_bar_int' => 16, @@ -2398,6 +2399,16 @@ if ($action === 'create_demo_data') { } } + if ($items_array['type'] === 'box') { + if (isset($items_array['border_color']) === true) { + $element_values['border_color'] = $items_array['border_color']; + } + + if (isset($items_array['fill_color']) === true) { + $element_values['fill_color'] = $items_array['fill_color']; + } + } + if ($items_array['type'] === 'icon') { if (isset($items_array['image']) === false || is_string($items_array['image']) === false @@ -2485,6 +2496,8 @@ if ($action === 'create_demo_data') { $element_values['height'] = $items_array['height']; } + $element_values['show_on_top'] = (isset($items_array['show_on_top']) === true && $items_array['show_on_top'] === true) ? 1 : 0; + // Check here percentile items as height is used for max value if ($items_array['type'] === 'percentile' || $items_array['type'] === 'bubble' || $items_array['type'] === 'circular_progress_bar' || $items_array['type'] === 'circular_progress_bar_int') { if (isset($items_array['agent_name']) === true) { From 55aee67df3aabf01adca549e3f6d66cd82b7632e Mon Sep 17 00:00:00 2001 From: Enrique Martin Date: Sun, 3 Dec 2023 23:23:57 +0100 Subject: [PATCH 28/42] Added GIS maps creation --- .../extras/demodata/gis_maps/1-gismap.prd | 16 ++ pandora_console/godmode/setup/demo.php | 36 +++- .../include/ajax/demo_data.ajax.php | 176 +++++++++++++++++- pandora_console/include/constants.php | 13 +- 4 files changed, 223 insertions(+), 18 deletions(-) create mode 100644 pandora_console/extras/demodata/gis_maps/1-gismap.prd diff --git a/pandora_console/extras/demodata/gis_maps/1-gismap.prd b/pandora_console/extras/demodata/gis_maps/1-gismap.prd new file mode 100644 index 0000000000..84016c5279 --- /dev/null +++ b/pandora_console/extras/demodata/gis_maps/1-gismap.prd @@ -0,0 +1,16 @@ +[gis_data] + +name="Demo GIS map" +group="Demo servers" +zoom_level="6" +initial_latitude="40" +initial_longitude="-3" +initial_altitude="0" +default_latitude="40" +default_longitude="-3" +default_altitude="0" + +[gis_layers] + +name[1]="Demo servers" +group[1]="Demo servers" \ No newline at end of file diff --git a/pandora_console/godmode/setup/demo.php b/pandora_console/godmode/setup/demo.php index 3707dbe8ca..3cd5f6274f 100644 --- a/pandora_console/godmode/setup/demo.php +++ b/pandora_console/godmode/setup/demo.php @@ -56,6 +56,7 @@ $service_agent_name = get_parameter('service_agent_name', 'demo-global-agent-1') $dir_item_id_map = [ DEMO_CUSTOM_GRAPH => 'graphs', DEMO_NETWORK_MAP => 'network_maps', + DEMO_GIS_MAP => 'gis_maps', DEMO_SERVICE => 'services', DEMO_REPORT => 'reports', DEMO_DASHBOARD => 'dashboards', @@ -65,6 +66,7 @@ $dir_item_id_map = [ $enabled_items = [ 'graphs' => (int) get_parameter('enable_cg', $def_value), 'network_maps' => (int) get_parameter('enable_nm', $def_value), + 'gis_maps' => (int) get_parameter('enable_gis', $def_value), 'services' => (int) get_parameter('enable_services', $def_value), 'reports' => (int) get_parameter('enable_rep', $def_value), 'dashboards' => (int) get_parameter('enable_dashboards', $def_value), @@ -135,6 +137,7 @@ if ($display_loading === true || $running_create === true || $running_delete) { DEMO_AGENT => 'agents', DEMO_SERVICE => 'services', DEMO_NETWORK_MAP => 'network maps', + DEMO_GIS_MAP => 'GIS maps', DEMO_CUSTOM_GRAPH => 'custom graphs', DEMO_REPORT => 'custom reports', DEMO_VISUAL_CONSOLE => 'visual consoles', @@ -233,7 +236,6 @@ if ($display_loading === true || $running_create === true || $running_delete) { $table_aux->size[1] = '50%'; $agent_sel_values = [ - 10 => '10', 30 => '30', 50 => '50', 500 => '500', @@ -370,6 +372,16 @@ if ($display_loading === true || $running_create === true || $running_delete) { ); $table_adv->data['row6'][] = html_print_label_input_block( + __('Create GIS maps'), + html_print_checkbox_switch( + 'enable_gis', + 1, + $enabled_items['gis_maps'], + true + ) + ); + + $table_adv->data['row7'][] = html_print_label_input_block( __('Create custom graphs'), html_print_checkbox_switch( 'enable_cg', @@ -379,7 +391,7 @@ if ($display_loading === true || $running_create === true || $running_delete) { ) ); - $table_adv->data['row7'][] = html_print_label_input_block( + $table_adv->data['row8'][] = html_print_label_input_block( __('Create reports'), html_print_checkbox_switch( 'enable_rep', @@ -389,7 +401,7 @@ if ($display_loading === true || $running_create === true || $running_delete) { ) ); - $table_adv->data['row8'][] = html_print_label_input_block( + $table_adv->data['row9'][] = html_print_label_input_block( __('Create visual consoles'), html_print_checkbox_switch( 'enable_vc', @@ -399,7 +411,7 @@ if ($display_loading === true || $running_create === true || $running_delete) { ) ); - $table_adv->data['row9'][] = html_print_label_input_block( + $table_adv->data['row10'][] = html_print_label_input_block( __('Create dashboards'), html_print_checkbox_switch( 'enable_dashboards', @@ -409,7 +421,7 @@ if ($display_loading === true || $running_create === true || $running_delete) { ) ); - $table_adv->data['row10'][] = html_print_label_input_block( + $table_adv->data['row11'][] = html_print_label_input_block( __('Demo data plugin agent'), html_print_input_text( 'plugin_agent', @@ -425,7 +437,7 @@ if ($display_loading === true || $running_create === true || $running_delete) { ) ); - $table_adv->data['row11'][] = html_print_label_input_block( + $table_adv->data['row12'][] = html_print_label_input_block( __('Traps target IP').ui_print_help_tip(__('All demo traps are generated using version 1'), true), html_print_input_text( 'traps_target_ip', @@ -441,7 +453,7 @@ if ($display_loading === true || $running_create === true || $running_delete) { ) ); - $table_adv->data['row12'][] = html_print_label_input_block( + $table_adv->data['row13'][] = html_print_label_input_block( __('Traps community'), html_print_input_text( 'traps_community', @@ -457,7 +469,7 @@ if ($display_loading === true || $running_create === true || $running_delete) { ) ); - $table_adv->data['row13'][] = html_print_label_input_block( + $table_adv->data['row14'][] = html_print_label_input_block( __('Tentacle target IP'), html_print_input_text( 'tentacle_target_ip', @@ -473,7 +485,7 @@ if ($display_loading === true || $running_create === true || $running_delete) { ) ); - $table_adv->data['row14'][] = html_print_label_input_block( + $table_adv->data['row15'][] = html_print_label_input_block( __('Tentacle port'), html_print_input_text( 'tentacle_port', @@ -489,7 +501,7 @@ if ($display_loading === true || $running_create === true || $running_delete) { ) ); - $table_adv->data['row15'][] = html_print_label_input_block( + $table_adv->data['row16'][] = html_print_label_input_block( __('Tentacle extra options'), html_print_input_text( 'tentacle_extra_options', @@ -593,6 +605,7 @@ if ($display_loading === true || $running_create === true || $running_delete) { $demo_agents_count = (int) db_get_value('count(*)', 'tdemo_data', 'table_name', 'tagente'); $demo_services_count = (int) db_get_value('count(*)', 'tdemo_data', 'table_name', 'tservice'); $demo_nm_count = (int) db_get_value('count(*)', 'tdemo_data', 'table_name', 'tmap'); + $demo_gis_count = (int) db_get_value('count(*)', 'tdemo_data', 'table_name', 'tgis_map'); $demo_cg_count = (int) db_get_value('count(*)', 'tdemo_data', 'table_name', 'tgraph'); $demo_rep_count = (int) db_get_value('count(*)', 'tdemo_data', 'table_name', 'treport'); $demo_vc_count = (int) db_get_value('count(*)', 'tdemo_data', 'table_name', 'tlayout'); @@ -622,6 +635,9 @@ if ($display_loading === true || $running_create === true || $running_delete) { $table_summary->data[$i][0] = __('Network maps'); $table_summary->data[$i][1] = ($demo_nm_count > 0) ? $demo_nm_count : '-'; $i++; + $table_summary->data[$i][0] = __('GIS maps'); + $table_summary->data[$i][1] = ($demo_gis_count > 0) ? $demo_gis_count : '-'; + $i++; $table_summary->data[$i][0] = __('Custom graphs'); $table_summary->data[$i][1] = ($demo_cg_count > 0) ? $demo_cg_count : '-'; $i++; diff --git a/pandora_console/include/ajax/demo_data.ajax.php b/pandora_console/include/ajax/demo_data.ajax.php index af6a2af628..50cc0ad6a5 100644 --- a/pandora_console/include/ajax/demo_data.ajax.php +++ b/pandora_console/include/ajax/demo_data.ajax.php @@ -58,6 +58,7 @@ if ($action === 'create_demo_data') { 'reports', 'dashboards', 'visual_consoles', + 'gis_maps', ]; $demodata_directory = $config['homedir'].'/extras/demodata/'; @@ -1524,6 +1525,174 @@ if ($action === 'create_demo_data') { register_error(DEMO_NETWORK_MAP, __('No configuration files found or failed to parse files')); } + $gis_count = count($parsed_ini['gis_maps'] ?? []); + if ($gis_count > 0) { + // Enable GIS features + $token = 'activate_gis'; + $activate_gis = db_get_value_filter('value', 'tconfig', ['token' => $token]); + if ($activate_gis === false) { + config_create_value($token, 1); + } else { + config_update_value($token, 1); + } + + // Create GIS maps. + foreach ($parsed_ini['gis_maps'] as $ini_gis_data) { + $filename = $ini_gis_data['filename']; + $gis_data = $ini_gis_data['gis_data']; + $gis_layers = $ini_gis_data['gis_layers']; + + if (isset($gis_data['name']) === false + || is_string($gis_data['name']) === false + || isset($gis_data['group']) === false + || is_string($gis_data['group']) === false + ) { + register_error( + DEMO_GIS_MAP, + __('Error in %s: name and/or group is not specified or does not have a valid format. Skipping GIS map creation', $filename) + ); + continue; + } + + $gis_name = $gis_data['name']; + $gis_group = $gis_data['group']; + $gis_zoom_level = (isset($gis_data['zoom_level']) === true) ? $gis_data['zoom_level'] : '6'; + $gis_initial_latitude = (isset($gis_data['initial_latitude']) === true) ? $gis_data['initial_latitude'] : '0'; + $gis_initial_longitude = (isset($gis_data['initial_longitude']) === true) ? $gis_data['initial_longitude'] : '0'; + $gis_initial_altitude = (isset($gis_data['initial_altitude']) === true) ? $gis_data['initial_altitude'] : '0'; + $gis_default_latitude = (isset($gis_data['default_latitude']) === true) ? $gis_data['default_latitude'] : '0'; + $gis_default_longitude = (isset($gis_data['default_longitude']) === true) ? $gis_data['default_longitude'] : '0'; + $gis_default_altitude = (isset($gis_data['default_altitude']) === true) ? $gis_data['default_altitude'] : '0'; + + $gis_id_group = get_group_or_create_demo_group($gis_group); + + if ($gis_id_group === false) { + // Group could not be created. Skip GIS map creation. + register_error( + DEMO_GIS_MAP, + __('Error in %s: the specified group does not exist in the system and could not be created. Skipping GIS map creation', $filename) + ); + continue; + } + + $values = []; + $values['map_name'] = io_safe_input($gis_name); + $values['group_id'] = $gis_id_group; + $values['zoom_level'] = $gis_zoom_level; + $values['initial_latitude'] = $gis_initial_latitude; + $values['initial_longitude'] = $gis_initial_longitude; + $values['initial_altitude'] = $gis_initial_altitude; + $values['default_latitude'] = $gis_default_latitude; + $values['default_longitude'] = $gis_default_longitude; + $values['default_altitude'] = $gis_default_altitude; + + $id_map = db_process_sql_insert('tgis_map', $values); + + if ($id_map > 0) { + // Register created map in tdemo_data. + $values = [ + 'item_id' => $id_map, + 'table_name' => 'tgis_map', + ]; + $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('tgis_map', ['id_tgis_map' => $id_map]); + + register_error( + DEMO_GIS_MAP, + __('Uncaught error (source %s): could not create GIS map %s', $filename, $gis_name) + ); + + continue; + } + } else { + // Network map group could not be created. Skip creation of map. + register_error( + DEMO_GIS_MAP, + __('Uncaught error (source %s): could not create GIS map %s', $filename, $gis_name) + ); + continue; + } + + $values = []; + $values['tgis_map_id_tgis_map'] = $id_map; + $values['tgis_map_con_id_tmap_con'] = 1; + + db_process_sql_insert('tgis_map_has_tgis_map_con', $values); + + if (count($gis_layers) > 0) { + $item_access_idx = 1; + + while (1) { + $items_array = []; + foreach ($gis_layers 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; + } + + $item_values = []; + + $layer_order = $item_access_idx - 2; + + $item_values['tgis_map_id_tgis_map'] = $id_map; + $item_values['layer_stack_order'] = $layer_order; + $item_values['tgrupo_id_grupo'] = -1; + $item_values['view_layer'] = 1; + $item_values['layer_name'] = io_safe_input((isset($items_array['name']) === true) ? $items_array['name'] : 'layer-'-$layer_order); + + if (isset($items_array['group']) === true) { + $layer_id_group = get_group_or_create_demo_group($items_array['group']); + if ($layer_id_group !== false) { + $item_values['tgrupo_id_grupo'] = $layer_id_group; + } + } + + $created_gis_layer_id = db_process_sql_insert('tgis_map_layer', $item_values); + + if ($created_gis_layer_id > 0) { + // Register created demo item in tdemo_data. + $values = [ + 'item_id' => $created_gis_layer_id, + 'table_name' => 'tgis_map_layer', + ]; + $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('tgis_map_layer', ['id_tmap_layer' => $created_gis_layer_id]); + + register_error( + DEMO_GIS_MAP, + __('Uncaught error (source %s): could not create GIS map item with index %d', $filename, ($item_access_idx - 1)) + ); + + continue; + } + } else { + register_error( + DEMO_GIS_MAP, + __('Uncaught error (source %s): could not create GIS map item with index %d', $filename, ($item_access_idx - 1)) + ); + } + } + } + } + + update_progress($total_items_count, $gis_count, $gis_count); + update_item_checked(DEMO_GIS_MAP); + } else { + register_error(DEMO_GIS_MAP, __('No configuration files found or failed to parse files')); + } + $cg_count = count($parsed_ini['graphs'] ?? []); if ($cg_count > 0) { // Create graphs. @@ -2079,7 +2248,7 @@ if ($action === 'create_demo_data') { 'max' => 6, 'min' => 7, 'avg' => 8, - ] + ]; // Get ID of item type. Skip if it does not exist. if (isset($types[$items_array['type']]) === false) { @@ -2095,7 +2264,7 @@ if ($action === 'create_demo_data') { $element_values['type'] = $types[$items_array['type']]; if ($items_array['type'] == 'value') { if (isset($items_array['process']) === true && isset($value_process_types[$items_array['process']])) { - $element_values['type'] = $value_process_types[$items_array['process']] + $element_values['type'] = $value_process_types[$items_array['process']]; if (isset($items_array['interval']) === true) { $element_values['period'] = $items_array['interval']; @@ -3056,6 +3225,9 @@ if ($action === 'cleanup_demo_data') { 'tagente_estado' => 'id_agente_estado', 'trel_item' => 'id', 'tplugin' => 'id', + 'tgis_data_status' => 'tagente_id_agente', + 'tgis_map' => 'id_tgis_map', + 'tgis_map_layer' => 'id_tmap_layer', ]; $table_id_field = $table_id_field_dict[$item['table_name']]; diff --git a/pandora_console/include/constants.php b/pandora_console/include/constants.php index ff39542202..4a5063ccbf 100644 --- a/pandora_console/include/constants.php +++ b/pandora_console/include/constants.php @@ -897,9 +897,10 @@ define('LOG_ALERTS', 2); // Demo items IDs. define('DEMO_AGENT', 1); define('DEMO_NETWORK_MAP', 2); -define('DEMO_CUSTOM_GRAPH', 3); -define('DEMO_REPORT', 4); -define('DEMO_SERVICE', 5); -define('DEMO_DASHBOARD', 6); -define('DEMO_VISUAL_CONSOLE', 7); -define('DEMO_PLUGIN', 8); +define('DEMO_GIS_MAP', 3); +define('DEMO_CUSTOM_GRAPH', 4); +define('DEMO_REPORT', 5); +define('DEMO_SERVICE', 6); +define('DEMO_DASHBOARD', 7); +define('DEMO_VISUAL_CONSOLE', 8); +define('DEMO_PLUGIN', 9); From 7892eb5c0c03cc45e6952570e0d95d49a1aeee52 Mon Sep 17 00:00:00 2001 From: Enrique Martin Date: Sun, 3 Dec 2023 23:52:55 +0100 Subject: [PATCH 29/42] Added log modules --- .../extras/demodata/agents/1-linux.prd | 64 ++++++++++++++++++- .../util/plugin/pandora_demo_agents.pl | 39 +++++++++++ 2 files changed, 102 insertions(+), 1 deletion(-) diff --git a/pandora_console/extras/demodata/agents/1-linux.prd b/pandora_console/extras/demodata/agents/1-linux.prd index 35d76f3f79..aaec5cd6ad 100644 --- a/pandora_console/extras/demodata/agents/1-linux.prd +++ b/pandora_console/extras/demodata/agents/1-linux.prd @@ -146,4 +146,66 @@ total_size[3]="819M" mount_point[1]="/home" mount_point[2]="/" -mount_point[3]="/tmp" \ No newline at end of file +mount_point[3]="/tmp" + +[log_modules] + +source[1]="httpd" +data[1]='127.0.0.1 - "GET /index.html HTTP/1.1" 200 3456 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36"' + +source[2]="httpd" +data[2]='192.168.1.1 - "POST /submit.php HTTP/1.1" 302 0 "https://example.com/form" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36"' + +source[3]="httpd" +data[3]='10.0.0.2 - "GET /images/logo.png HTTP/1.1" 304 0 "-" "Mozilla/5.0 (iPhone; CPU iPhone OS 14_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.1 Mobile/15E148 Safari/604.1"' + +source[4]="httpd" +data[4]='172.16.0.1 - "GET /page/about-us HTTP/1.1" 404 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36"' + +source[5]="httpd" +data[5]='192.168.0.10 - "GET /css/style.css HTTP/1.1" 200 768 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36"' + +source[6]="httpd" +data[6]='203.0.113.45 - "GET /blog/post-123 HTTP/1.1" 200 5432 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36"' + +source[7]="httpd" +data[7]='54.78.90.12 - "GET /category/api-reference HTTP/1.1" 200 6543 "-" "Mozilla/5.0 (Linux; Android 10; SM-G960U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.181 Mobile Safari/537.36"' + +source[8]="httpd" +data[8]='88.77.66.55 - "GET /contact-us HTTP/1.1" 301 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36"' + +source[9]="httpd" +data[9]='198.51.100.23 - "GET /images/photo.jpg HTTP/1.1" 200 4567 "https://example.com/gallery" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36"' + +source[10]="httpd" +data[10]='128.0.0.1 - "GET /downloads/file.zip HTTP/1.1" 200 12345 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36"' + +source[11]="mysqld" +data[11]="[Warning] Aborted connection 123 to db: 'your_database' user: 'your_user' host: '192.168.1.100' (Got an error reading communication packets)" + +source[12]="mysqld" +data[12]="[ERROR] Table 'your_database.your_table' doesn't exist" + +source[13]="mysqld" +data[13]="[Note] Starting crash recovery..." + +source[14]="mysqld" +data[14]="[Warning] Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT. Statement is unsafe because it uses a LIMIT clause. Use a different log bin event type." + +source[15]="mysqld" +data[15]="[ERROR] Out of memory; check if mysqld or some other process uses all available memory; if not, you may have to use 'ulimit' to allow mysqld to use more memory or you can add more swap space" + +source[16]="mysqld" +data[16]="[Note] Aborted connection 456 to db: 'another_database' user: 'another_user' host: 'localhost' (Got timeout reading communication packets)" + +source[17]="mysqld" +data[17]="[ERROR] Can't create a new thread (errno 11); if you are not out of available memory, you can consult the manual for a possible OS-dependent bug" + +source[18]="mysqld" +data[18]="[Warning] 'user' entry 'your_user@localhost' ignored in --skip-name-resolve mode." + +source[19]="mysqld" +data[19]="[Note] InnoDB: Starting rollback of uncommitted transactions" + +source[20]="mysqld" +data[20]="[ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed." \ No newline at end of file diff --git a/pandora_server/util/plugin/pandora_demo_agents.pl b/pandora_server/util/plugin/pandora_demo_agents.pl index d58472aa66..88ec91bf2c 100644 --- a/pandora_server/util/plugin/pandora_demo_agents.pl +++ b/pandora_server/util/plugin/pandora_demo_agents.pl @@ -197,6 +197,19 @@ sub parse_ini_file { $ini_data{'inventory_values'} = {}; } + # Initialize log modules keys + if(!defined($ini_data{'log_modules'})) { + $ini_data{'log_modules'} = {}; + } + + if(!defined($ini_data{'log_modules'}{'source'})) { + $ini_data{'log_modules'}{'source'} = {}; + } + + if(!defined($ini_data{'log_modules'}{'data'})) { + $ini_data{'log_modules'}{'data'} = {}; + } + # Initialize traps keys if(!defined($ini_data{'traps'})) { $ini_data{'traps'} = {}; @@ -518,6 +531,32 @@ sub generate_agent($) { $xml .= "\n"; } + # Append log module data to XML (only once a day at 00:00) + if (!empty($sorted_ini[$current_ini]->{'log_modules'}->{'source'}) && !empty($sorted_ini[$current_ini]->{'log_modules'}->{'data'})) { + + # Remove agent_data closing tag + $xml =~ s/<\/agent_data>//i; + + # Add log modules for each source + foreach my $log_source (sort keys %{$sorted_ini[$current_ini]->{'log_modules'}->{'source'}}) { + # Only if data is defined + if(defined($sorted_ini[$current_ini]->{'log_modules'}->{'data'}->{$log_source})) { + # Add log module 50% of times + if(get_bool(50)) { + my $log_data = $sorted_ini[$current_ini]->{'log_modules'}->{'data'}->{$log_source}; + + $xml .= "\n"; + $xml .= "\t\n"; + $xml .= "\t\n"; + $xml .= "\n"; + } + } + } + + # Close agent_data tag again + $xml .= "\n"; + } + # Get file name MD5 my $file_md5 = md5_hex($agent->{'agent_name'}); From 988841084c0a1a0d5eae40dd15956ba066a125f3 Mon Sep 17 00:00:00 2001 From: Enrique Martin Date: Mon, 4 Dec 2023 00:00:41 +0100 Subject: [PATCH 30/42] Fixed log modules generation --- pandora_server/util/plugin/pandora_demo_agents.pl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pandora_server/util/plugin/pandora_demo_agents.pl b/pandora_server/util/plugin/pandora_demo_agents.pl index 88ec91bf2c..a746d6969f 100644 --- a/pandora_server/util/plugin/pandora_demo_agents.pl +++ b/pandora_server/util/plugin/pandora_demo_agents.pl @@ -538,12 +538,13 @@ sub generate_agent($) { $xml =~ s/<\/agent_data>//i; # Add log modules for each source - foreach my $log_source (sort keys %{$sorted_ini[$current_ini]->{'log_modules'}->{'source'}}) { + foreach my $log_key (sort keys %{$sorted_ini[$current_ini]->{'log_modules'}->{'source'}}) { # Only if data is defined - if(defined($sorted_ini[$current_ini]->{'log_modules'}->{'data'}->{$log_source})) { + if(defined($sorted_ini[$current_ini]->{'log_modules'}->{'data'}->{$log_key})) { # Add log module 50% of times if(get_bool(50)) { - my $log_data = $sorted_ini[$current_ini]->{'log_modules'}->{'data'}->{$log_source}; + my $log_source = $sorted_ini[$current_ini]->{'log_modules'}->{'source'}->{$log_key}; + my $log_data = $sorted_ini[$current_ini]->{'log_modules'}->{'data'}->{$log_key}; $xml .= "\n"; $xml .= "\t\n"; From 75ddb3b934d4c656344895b5d03324ce371a776d Mon Sep 17 00:00:00 2001 From: "alejandro.campos@artica.es" Date: Mon, 4 Dec 2023 11:37:12 +0100 Subject: [PATCH 31/42] minor changes in demo data functionality --- pandora_console/godmode/setup/demo.php | 47 ----------- .../include/ajax/demo_data.ajax.php | 83 ++++++++++++------- .../include/class/WelcomeWindow.class.php | 63 ++++++++++++-- 3 files changed, 109 insertions(+), 84 deletions(-) diff --git a/pandora_console/godmode/setup/demo.php b/pandora_console/godmode/setup/demo.php index 3707dbe8ca..90f7281db7 100644 --- a/pandora_console/godmode/setup/demo.php +++ b/pandora_console/godmode/setup/demo.php @@ -233,7 +233,6 @@ if ($display_loading === true || $running_create === true || $running_delete) { $table_aux->size[1] = '50%'; $agent_sel_values = [ - 10 => '10', 30 => '30', 50 => '50', 500 => '500', @@ -505,52 +504,6 @@ if ($display_loading === true || $running_create === true || $running_delete) { ) ); - /* $table_adv->data['row8'][] = html_print_label_input_block( - __('Create dashboards'), - html_print_checkbox_switch( - 'enable_dashboards', - 1, - $enabled_items['dashboards'], - true - ) - ); - $table_adv->data['row8'][] = html_print_label_input_block( - __('Create dashboards'), - html_print_checkbox_switch( - 'enable_dashboards', - 1, - $enabled_items['dashboards'], - true - ) - ); - $table_adv->data['row8'][] = html_print_label_input_block( - __('Create dashboards'), - html_print_checkbox_switch( - 'enable_dashboards', - 1, - $enabled_items['dashboards'], - true - ) - ); - $table_adv->data['row8'][] = html_print_label_input_block( - __('Create dashboards'), - html_print_checkbox_switch( - 'enable_dashboards', - 1, - $enabled_items['dashboards'], - true - ) - ); - $table_adv->data['row8'][] = html_print_label_input_block( - __('Create dashboards'), - html_print_checkbox_switch( - 'enable_dashboards', - 1, - $enabled_items['dashboards'], - true - ) - );*/ - echo ''; echo '
      '; echo ''.__('Configure demo data').''; diff --git a/pandora_console/include/ajax/demo_data.ajax.php b/pandora_console/include/ajax/demo_data.ajax.php index 5a50eb4979..3054f72d35 100644 --- a/pandora_console/include/ajax/demo_data.ajax.php +++ b/pandora_console/include/ajax/demo_data.ajax.php @@ -134,6 +134,7 @@ if ($action === 'create_demo_data') { $agent_created_total = 0; $agent_data_values_buffer = []; + $agent_traps_values_buffer = []; if ($total_agents_to_create > 0 && $agents_to_create > 0) { while ($agent_created_total < ($total_agents_to_create - 1)) { @@ -798,35 +799,8 @@ if ($action === 'create_demo_data') { 'utimestamp' => $utimestamp, ]; - $created_trap_id = db_process_sql_insert('ttrap', $values); - - if ($created_trap_id > 0) { - // Register created demo item in tdemo_data. - $values = [ - 'item_id' => $created_trap_id, - 'table_name' => 'ttrap', - ]; - $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('ttrap', ['id_module_inventory' => $created_trap_id]); - - register_error( - DEMO_AGENT, - __('Uncaught error (source %s): could not create trap with index %d', $filename, ($trap_access_idx - 1)), - true - ); - - continue; - } - } else { - register_error( - DEMO_AGENT, - __('Uncaught error (source %s): could not create trap with index %d', $filename, ($trap_access_idx - 1)), - true - ); - } + // Buffer history traps for later bulk insertion (performance reasons). + $agent_traps_values_buffer[] = $values; } if ($adv_options_is_enabled === false @@ -850,10 +824,11 @@ if ($action === 'create_demo_data') { } } - $agent_data_values_buffer_chunks = array_chunk($agent_data_values_buffer, 1000); + $agent_data_values_buffer_chunks = array_chunk($agent_data_values_buffer, 100000); + $agent_traps_values_buffer_chunks = array_chunk($agent_traps_values_buffer, 100000); foreach ($agent_data_values_buffer_chunks as $chunk) { - // Bulk inserts. + // Bulk inserts (insert batches of up to 100,000 as a performance limit). mysql_db_process_sql_insert_multiple( 'tagente_datos', $chunk, @@ -861,6 +836,52 @@ if ($action === 'create_demo_data') { ); } + // Get last trap in database. + $id_trap_begin = db_get_value( + 'MAX(id_trap)', + 'ttrap', + 1, + 1, + false, + false + ); + + if ($id_trap_begin === false) { + $id_trap_begin = 0; + } + + foreach ($agent_traps_values_buffer_chunks as $chunk) { + // Bulk inserts (insert batches of up to 100,000 as a performance limit). + mysql_db_process_sql_insert_multiple( + 'ttrap', + $chunk, + false + ); + } + + // Get last trap in database after insertion. + $id_trap_end = db_get_value( + 'MAX(id_trap)', + 'ttrap', + 1, + 1, + false, + false + ); + + if ($id_trap_end === false) { + $id_trap_end = 0; + } + + for ($i = ($id_trap_begin + 1); $i <= $id_trap_end; $i++) { + // Register created demo item in tdemo_data. + $values = [ + 'item_id' => $i, + 'table_name' => 'ttrap', + ]; + db_process_sql_insert('tdemo_data', $values); + } + update_item_checked(DEMO_AGENT); } } diff --git a/pandora_console/include/class/WelcomeWindow.class.php b/pandora_console/include/class/WelcomeWindow.class.php index b51e753048..638ecf78b4 100644 --- a/pandora_console/include/class/WelcomeWindow.class.php +++ b/pandora_console/include/class/WelcomeWindow.class.php @@ -699,6 +699,45 @@ class WelcomeWindow extends Wizard echo html_print_submit_button(__('Create'), 'create_goliat', false, ['icon' => 'next', 'style' => 'margin-top:15px; float:right;']); ?> +