diff --git a/pandora_console/extras/mr/62.sql b/pandora_console/extras/mr/62.sql new file mode 100644 index 0000000000..14baa793af --- /dev/null +++ b/pandora_console/extras/mr/62.sql @@ -0,0 +1,17 @@ +START TRANSACTION; + +CREATE TABLE IF NOT EXISTS `tconsole` ( + `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, + `id_console` BIGINT NOT NULL DEFAULT 0, + `description` TEXT, + `version` TINYTEXT, + `last_execution` INT UNSIGNED NOT NULL DEFAULT 0, + `console_type` TINYINT NOT NULL DEFAULT 0, + `timezone` TINYTEXT, + `public_url` TEXT, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=UTF8MB4; + +ALTER TABLE `tuser_task_scheduled` ADD COLUMN `id_console` BIGINT NOT NULL DEFAULT 0; + +COMMIT; diff --git a/pandora_console/godmode/consoles/consoles.php b/pandora_console/godmode/consoles/consoles.php new file mode 100644 index 0000000000..02bbe981aa --- /dev/null +++ b/pandora_console/godmode/consoles/consoles.php @@ -0,0 +1,69 @@ +<?php +/** + * Consoles manager. + * + * @category Tools + * @package Pandora FMS + * @subpackage Enterprise + * @version 1.0.0 + * @license See below + * + * ______ ___ _______ _______ ________ + * | __ \.-----.--.--.--| |.-----.----.-----. | ___| | | __| + * | __/| _ | | _ || _ | _| _ | | ___| |__ | + * |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______| + * + * ============================================================================ + * Copyright (c) 2005-2021 Artica Soluciones Tecnologicas + * Please see http://pandorafms.org for full contribution list + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation for version 2. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * ============================================================================ + */ + +use PandoraFMS\Console; +use PandoraFMS\View; + +// Begin. +global $config; +check_login(); + +if (check_acl($config['id_user'], 0, 'PM') === false + && is_user_admin($config['id_user']) === false +) { + db_pandora_audit( + AUDIT_LOG_ACL_VIOLATION, + 'Trying to access Consoles Management' + ); + include 'general/noaccess.php'; + exit; +} + +$ajax_url = ui_get_full_url('ajax.php'); + +$message = ''; +$error = false; + +// Check is any consoles are registered. +$results = db_get_all_rows_in_table('tconsole'); + +$message = ''; + +if ($results === false) { + $message = ui_print_info_message( + __('If you want to have your consoles registered, you must define them by editing config.php in each individual console and wait for cron to run in order to be registered.') + ); +} + +View::render( + 'consoles/list', + [ + 'ajax_url' => $ajax_url, + 'message' => $message, + ] +); diff --git a/pandora_console/godmode/menu.php b/pandora_console/godmode/menu.php index d98eb9d3c6..9a9d11c02e 100644 --- a/pandora_console/godmode/menu.php +++ b/pandora_console/godmode/menu.php @@ -305,6 +305,13 @@ if ($access_console_node === true) { $sub['godmode/servers/modificar_server']['id'] = 'Manage servers'; } + if ((bool) check_acl($config['id_user'], 0, 'PM') === true + || is_user_admin($config['id_user']) === true + ) { + $sub['godmode/consoles/consoles']['text'] = __('Manage consoles'); + $sub['godmode/consoles/consoles']['id'] = 'Manage consoles'; + } + // This subtabs are only for Pandora Admin. if ((bool) check_acl($config['id_user'], 0, 'PM') === true) { enterprise_hook('ha_cluster'); diff --git a/pandora_console/include/ajax/consoles.ajax.php b/pandora_console/include/ajax/consoles.ajax.php new file mode 100644 index 0000000000..bb20320c75 --- /dev/null +++ b/pandora_console/include/ajax/consoles.ajax.php @@ -0,0 +1,96 @@ +<?php +/** + * Ajax script for Consoles' List view. + * + * @category Consoles + * @package Community + * @subpackage Software agents repository + * @version 1.0.0 + * @license See below + * + * ______ ___ _______ _______ ________ + * | __ \.-----.--.--.--| |.-----.----.-----. | ___| | | __| + * | __/| _ | | _ || _ | _| _ | | ___| |__ | + * |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______| + * + * ========================================================== + * Copyright (c) 2005-2022 Artica Soluciones Tecnológicas S.L + * This code is NOT free software. This code is NOT licenced under GPL2 licence + * You cannot redistribute it without written permission of copyright holder. + * ============================================================================ + */ + +// Begin. +global $config; + +// Login check. +check_login(); + +require_once $config['homedir'].'/include/functions_ui.php'; + +use PandoraFMS\Console; + +if (check_acl($config['id_user'], 0, 'PM') === false + && is_user_admin($config['id_user']) === false +) { + db_pandora_audit( + AUDIT_LOG_ACL_VIOLATION, + 'Trying to access Consoles Management' + ); + include 'general/noaccess.php'; + exit; +} + +$get_all_datatables_formatted = (bool) get_parameter('get_all_datatables_formatted'); +$delete = (bool) get_parameter('delete'); + +if ($get_all_datatables_formatted === true) { + $results = db_get_all_rows_in_table('tconsole', 'id_console'); + + if ($results === false) { + $results = []; + } + + $count = count($results); + + if ($results) { + $data = array_reduce( + $results, + function ($carry, $item) { + $item['last_execution'] = ui_print_timestamp($item['last_execution'], true); + $item['console_type'] = ((int) $item['console_type'] === 1) ? __('Reporting').'  '.html_print_image('images/report_list.png', true) : __('Standard'); + // Transforms array of arrays $data into an array + // of objects, making a post-process of certain fields. + $tmp = (object) $item; + $carry[] = $tmp; + return $carry; + } + ); + } + + // Datatables format: RecordsTotal && recordsfiltered. + echo json_encode( + [ + 'data' => $data, + 'recordsTotal' => $count, + 'recordsFiltered' => $count, + ] + ); + + return; +} + +if ($delete === true) { + $id = get_parameter('id'); + + try { + $console = new Console($id); + $console->delete(); + $console->save(); + echo json_encode(['result' => __('Console successfully deleted')]); + } catch (Exception $e) { + echo json_encode(['result' => $e->getMessage()]); + } + + return; +} diff --git a/pandora_console/include/lib/Console.php b/pandora_console/include/lib/Console.php new file mode 100644 index 0000000000..e6e8d25d57 --- /dev/null +++ b/pandora_console/include/lib/Console.php @@ -0,0 +1,138 @@ +<?php +// phpcs:disable Squiz.NamingConventions.ValidFunctionName.ScopeNotCamelCaps +/** + * Console entity class. + * + * @category Class + * @package Pandora FMS + * @subpackage OpenSource + * @version 1.0.0 + * @license See below + * + * ______ ___ _______ _______ ________ + * | __ \.-----.--.--.--| |.-----.----.-----. | ___| | | __| + * | __/| _ | | _ || _ | _| _ | | ___| |__ | + * |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______| + * + * ============================================================================ + * Copyright (c) 2005-2021 Artica Soluciones Tecnologicas + * Please see http://pandorafms.org for full contribution list + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation for version 2. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * ============================================================================ + */ + +// Begin. +namespace PandoraFMS; + +/** + * PandoraFMS console entity. + */ +class Console extends Entity +{ + + + /** + * Builds a PandoraFMS\Console object from given id. + * + * @param integer $id Id console. + */ + public function __construct(?int $id=null) + { + $table = 'tconsole'; + $filter = ['id' => $id]; + + $this->existsInDB = false; + + if (is_numeric($id) === true + && $id > 0 + ) { + parent::__construct( + $table, + $filter, + null, + false + ); + $this->existsInDB = true; + } else { + // Create empty skel. + parent::__construct($table, null); + } + } + + + /** + * Saves current definition of a Console to database. + * + * @return mixed Affected rows of false in case of error. + * @throws \Exception On error. + */ + public function save() + { + if ($this->fields['id'] > 0) { + // Update. + $updates = $this->fields; + + $rs = \db_process_sql_update( + $this->table, + $updates, + ['id' => $this->fields['id']] + ); + + if ($rs === false) { + global $config; + throw new \Exception( + __METHOD__.' error: '.$config['dbconnection']->error + ); + } + } else { + // Creation. + $inserts = $this->fields; + + // Clean null fields. + foreach ($inserts as $k => $v) { + if ($v === null) { + unset($inserts[$k]); + } + } + + $rs = \db_process_sql_insert( + $this->table, + $inserts + ); + + if ($rs === false) { + global $config; + throw new \Exception( + __METHOD__.' error: '.$config['dbconnection']->error + ); + } + + $this->fields['id'] = $rs; + } + + return true; + } + + + /** + * Remove this Console. + * + * @return void + */ + public function delete() + { + if ($this->existsInDB === true) { + \db_process_delete_temp( + $this->table, + 'id', + $this->fields['id'] + ); + } + } +} diff --git a/pandora_console/pandoradb.sql b/pandora_console/pandoradb.sql index e43aaa7646..f9eda8cf97 100644 --- a/pandora_console/pandoradb.sql +++ b/pandora_console/pandoradb.sql @@ -3758,6 +3758,7 @@ CREATE TABLE IF NOT EXISTS `tuser_task_scheduled` ( `flag_delete` TINYINT UNSIGNED NOT NULL DEFAULT 0, `id_grupo` INT UNSIGNED NOT NULL DEFAULT 0, `enabled` TINYINT UNSIGNED NOT NULL DEFAULT 1, + `id_console` BIGINT NOT NULL DEFAULT 0, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=UTF8MB4; @@ -4177,3 +4178,18 @@ CREATE TABLE IF NOT EXISTS `tmonitor_filter` ( `ag_custom_fields` TEXT, PRIMARY KEY (`id_filter`) ) ENGINE=InnoDB DEFAULT CHARSET=UTF8MB4; + +-- --------------------------------------------------------------------- +-- Table `tconsole` +-- --------------------------------------------------------------------- +CREATE TABLE IF NOT EXISTS `tconsole` ( + `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, + `id_console` BIGINT NOT NULL DEFAULT 0, + `description` TEXT, + `version` TINYTEXT, + `last_execution` INT UNSIGNED NOT NULL DEFAULT 0, + `console_type` TINYINT NOT NULL DEFAULT 0, + `timezone` TINYTEXT, + `public_url` TEXT, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=UTF8MB4; diff --git a/pandora_console/views/consoles/list.php b/pandora_console/views/consoles/list.php new file mode 100644 index 0000000000..f7a4d6bcc1 --- /dev/null +++ b/pandora_console/views/consoles/list.php @@ -0,0 +1,219 @@ +<?php +/** + * Console: Consoles list page. + * + * @category View + * @package Pandora FMS + * @subpackage Alert + * @version 1.0.0 + * @license See below + * + * ______ ___ _______ _______ ________ + * | __ \.-----.--.--.--| |.-----.----.-----. | ___| | | __| + * | __/| _ | | _ || _ | _| _ | | ___| |__ | + * |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______| + * + * ============================================================================ + * Copyright (c) 2005-2021 Artica Soluciones Tecnologicas + * Please see http://pandorafms.org for full contribution list + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation for version 2. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * ============================================================================ + */ + +// Header. +\ui_print_page_header( + // Title. + __('%s registered consoles', $config['rb_product_name']), + // Icon. + '', + // Return. + false, + // Help. + '', + // Godmode. + true, + // Options. + '' +); + +if (empty($message) === false) { + echo $message; +} + +// Auxiliar to display deletion modal. +echo '<div id="delete_modal" class="invisible"></div>'; +echo '<div id="msg" class="invisible"></div>'; + + +// Consoles list. +try { + $columns = [ + 'id_console', + 'description', + 'version', + 'last_execution', + 'console_type', + 'timezone', + 'public_url', + 'options', + ]; + + $column_names = [ + __('Console ID'), + __('Description'), + __('Version'), + __('Last Execution'), + __('Console type'), + __('Timezone'), + __('Public URL'), + [ + 'text' => __('Options'), + 'class' => 'action_buttons', + ], + ]; + + + $tableId = 'consoles_list'; + // Load datatables user interface. + ui_print_datatable( + [ + 'id' => $tableId, + 'class' => 'info_table', + 'style' => 'width: 100%', + 'columns' => $columns, + 'column_names' => $column_names, + 'ajax_url' => 'include/ajax/consoles.ajax', + 'ajax_data' => ['get_all_datatables_formatted' => 1], + 'ajax_postprocess' => 'process_datatables_item(item)', + 'no_sortable_columns' => [-1], + 'order' => [ + 'field' => 'id', + 'direction' => 'asc', + ], + ] + ); +} catch (Exception $e) { + echo $e->getMessage(); +} + +?> +<script type="text/javascript"> + /** + * Process datatable item before draw it. + */ + function process_datatables_item(item) { + item.options = '<a href="javascript:" onclick="delete_key(\''; + item.options += item.id; + item.options += '\')" ><?php echo html_print_image('images/cross.png', true, ['title' => __('Delete'), 'class' => 'invert_filter']); ?></a>'; + } + + /** + * Delete selected key + */ + function delete_key(id) { + $('#delete_modal').empty(); + $('#delete_modal').html('<?php echo __('<span>Are you sure?</span><br><br><i>WARNING: you also need to delete config.php options in your console or delete the whole console.</i>'); ?>'); + $('#delete_modal').dialog({ + title: '<?php echo __('Delete'); ?>', + buttons: [ + { + class: 'ui-widget ui-state-default ui-corner-all ui-button-text-only sub upd submit-cancel', + text: '<?php echo __('Cancel'); ?>', + click: function(e) { + $(this).dialog('close'); + cleanupDOM(); + + } + }, + { + text: 'Delete', + class: 'ui-widget ui-state-default ui-corner-all ui-button-text-only sub ok submit-next', + click: function(e) { + $.ajax({ + method: 'post', + url: '<?php echo ui_get_full_url('ajax.php', false, false, false); ?>', + data: { + page: 'include/ajax/consoles.ajax', + delete: 1, + id + }, + datatype: "json", + success: function (data) { + console.log("dataaaa"); + console.log(data); + showMsg(data); + }, + error: function(e) { + showMsg(e); + } + }); + } + } + ] + }); + } + + /** + * Process ajax responses and shows a dialog with results. + */ + function showMsg(data) { + var title = "<?php echo __('Success'); ?>"; + var dt_satellite_agents = $("#<?php echo $tableId; ?>").DataTable(); + dt_<?php echo $tableId; ?>.draw(false); + + var text = ''; + var failed = 0; + try { + data = JSON.parse(data); + text = data['result']; + } catch (err) { + title = "<?php echo __('Failed'); ?>"; + text = err.message; + failed = 1; + } + if (!failed && data['error'] != undefined) { + title = "<?php echo __('Failed'); ?>"; + text = data['error']; + failed = 1; + } + if (data['report'] != undefined) { + data['report'].forEach(function (item){ + text += '<br>'+item; + }); + } + + $('#msg').empty(); + $('#msg').html(text); + $('#msg').dialog({ + width: 450, + position: { + my: 'center', + at: 'center', + of: window, + collision: 'fit' + }, + title: title, + buttons: [ + { + class: "ui-widget ui-state-default ui-corner-all ui-button-text-only sub ok submit-next", + text: 'OK', + click: function(e) { + if (!failed) { + $(".ui-dialog-content").dialog("close"); + $('.info').hide(); + } else { + $(this).dialog('close'); + } + } + } + ] + }); + } + +</script> \ No newline at end of file