From 55aee67df3aabf01adca549e3f6d66cd82b7632e Mon Sep 17 00:00:00 2001 From: Enrique Martin Date: Sun, 3 Dec 2023 23:23:57 +0100 Subject: [PATCH] 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);