9555-Server status

This commit is contained in:
Pablo Aragon 2023-04-12 14:48:19 +02:00
parent ba9cdf029d
commit 4deec005c1
6 changed files with 664 additions and 2 deletions

View File

@ -522,7 +522,7 @@ function servers_get_rate($avg_interval, $num_modules)
*
* @return mixed False in case the server doesn't exist or an array with info.
*/
function servers_get_info($id_server=-1)
function servers_get_info($id_server=-1, $sql_limit=-1)
{
global $config;
@ -538,6 +538,14 @@ function servers_get_info($id_server=-1)
SELECT *
FROM tserver '.$select_id.'
ORDER BY server_type';
if ($sql_limit !== -1) {
$sql = '
SELECT *
FROM tserver '.$select_id.'
ORDER BY server_type'.$sql_limit;
}
$result = db_get_all_rows_sql($sql);
$time = get_system_time();

View File

@ -571,7 +571,8 @@ table.event_details td.cell_event_name {
#list_Modules td:hover,
#list_agent_Modules td:hover,
#list_visualmaps td:hover,
#last_agent_events td:hover {
#last_agent_events td:hover,
#list_servers_status td:hover {
cursor: pointer;
}
@ -866,6 +867,31 @@ table.event_details td.cell_event_name {
#list_Modules .module_name {
font-size: 12px !important;
}
table#list_servers tr {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
align-items: center;
}
table#list_servers b.ui-table-cell-label {
display: none;
}
table#list_servers tr td.cell_0 {
width: 10%;
}
table#list_servers tr td.cell_1 {
width: 10%;
}
table#list_servers tr td.cell_2 {
width: 80%;
}
}
table#list_servers tr td.cell_0 {
border-top: 0px;
}
#list_Modules .cell_5 a {
@ -1648,3 +1674,61 @@ body.login-background
> span.ui-button-icon-space {
width: 5px;
}
/* ==== Spinner ==== */
.spinner {
width: 100px;
height: 100px;
border-radius: 100%;
background: linear-gradient(#82b92e, #c1ccdc);
animation: animate 1.2s linear infinite;
margin: auto;
margin-bottom: 40px;
}
.spinner span {
position: absolute;
width: 100%;
height: 100%;
border-radius: 100%;
background: linear-gradient(#82b92e, #c1ccdc);
}
.spinner span:nth-child(1) {
filter: blur(4px);
}
.spinner span:nth-child(2) {
filter: blur(8px);
}
.spinner span:nth-child(3) {
filter: blur(12px);
}
.spinner span:nth-child(4) {
filter: blur(16px);
}
.spinner:after {
content: "";
position: absolute;
top: 10px;
left: 10px;
right: 10px;
bottom: 10px;
/* background: transparent; */
background: #f1f1f1;
border: solid #fff 10px;
border-radius: 50%;
}
@keyframes animate {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
.status_small_squares,
.status_balls {
width: 12px;
height: 12px;
display: inline-block;
}

View File

@ -408,6 +408,13 @@ class User
$(".ui-overlay-a").addClass("login-background");
$(".ui-overlay-a").removeClass("ui-overlay-a");
$(".ui-page-theme-a").css({"background-color":"transparent !important"});
$("#text-login_btn").click(function (e) {
$("#user-container").hide();
$("#password-container").hide();
$("#text-login_btn").hide();
$("#spinner-login").show();
});
});
</script>
'
@ -448,6 +455,17 @@ class User
'required' => 'required',
];
$ui->formAddInputPassword($options);
$spinner = '
<div class="spinner invisible" id="spinner-login">
<span></span>
<span></span>
<span></span>
<span></span>
</div>
';
$ui->formAddHtml($spinner);
$options = [
'value' => __('Login'),
'icon' => 'arrow-r',

View File

@ -43,6 +43,7 @@ require_once 'operation/module_graph.php';
require_once 'operation/agent.php';
require_once 'operation/visualmaps.php';
require_once 'operation/visualmap.php';
require_once 'operation/server_status.php';
$enterpriseHook = enterprise_include('mobile/include/enterprise.class.php');
$enterpriseHook = enterprise_include('mobile/operation/home.php');
@ -177,6 +178,11 @@ switch ($action) {
$tactical->ajax($parameter2);
break;
case 'server_status':
$server_status = new ServerStatus();
$server_status->ajax($parameter2);
break;
default:
if (class_exists('Enterprise')) {
$enterprise->enterpriseAjax($parameter1, $parameter2);
@ -407,6 +413,11 @@ switch ($action) {
$vc = new Visualmap();
$vc->show();
break;
case 'server_status':
$server_status = new ServerStatus();
$server_status->show();
break;
}
break;
}

View File

@ -91,6 +91,13 @@ class Home
'icon' => 'ui-icon-modules ui-widget-icon-floatbeginning',
];
$items['server_status'] = [
'name' => __('Server status'),
'filename' => 'server_status.php',
'menu_item' => true,
'icon' => 'ui-icon- ui-widget-icon-floatbeginning',
];
// Not in home.
$items['agent'] = [
'name' => __('Agent'),

View File

@ -0,0 +1,534 @@
<?php
// phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
/**
* Modules list view for mobile
*
* @category Mobile
* @package Pandora FMS
* @subpackage Community
* @version 1.0.0
* @license See below
*
* ______ ___ _______ _______ ________
* | __ \.-----.--.--.--| |.-----.----.-----. | ___| | | __|
* | __/| _ | | _ || _ | _| _ | | ___| |__ |
* |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
*
* ============================================================================
* Copyright (c) 2005-2022 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.
class ServerStatus
{
private $correct_acl = false;
private $acl = 'AR';
private $default = true;
private $default_filters = [];
private $group = 0;
private $status = AGENT_MODULE_STATUS_NOT_NORMAL;
private $free_search = '';
private $name = '';
private $module_group = -1;
private $tag = '';
private $id_agent = 0;
private $all_servers = false;
private $list_status = null;
private $columns = null;
function __construct()
{
$system = System::getInstance();
if ($system->checkACL($this->acl)) {
$this->correct_acl = true;
} else {
$this->correct_acl = false;
}
if ($system->getConfig('metaconsole') === 0) {
$this->all_servers = true;
}
}
public function ajax($parameter2=false)
{
$system = System::getInstance();
if (!$this->correct_acl) {
return;
} else {
switch ($parameter2) {
case 'get_server_status':
$this->getFilters();
$page = $system->getRequest('page', 0);
$server_status = [];
$end = 1;
$listServers = $this->getListServers($page, true);
if (!empty($listServers['servers'])) {
$end = 0;
$servers = $listServers['servers'];
}
hd(json_encode(['end' => $end, 'servers' => $servers]), true);
echo json_encode(['end' => $end, 'servers' => $servers]);
break;
}
}
}
public function setFilters($filters)
{
if (isset($filters['id_agent'])) {
$this->id_agent = $filters['id_agent'];
}
if (isset($filters['all_servers'])) {
$this->all_servers = $filters['all_servers'];
}
if (isset($filters['status'])) {
$this->status = (int) $filters['status'];
}
if (isset($filters['name'])) {
$this->name = $filters['name'];
}
}
public function disabledColumns($columns=null)
{
if (!empty($columns)) {
foreach ($columns as $column) {
$this->columns[$column] = 0;
}
}
}
private function getFilters()
{
$system = System::getInstance();
$user = User::getInstance();
$this->default_filters['module_group'] = true;
$this->default_filters['group'] = true;
$this->default_filters['status'] = true;
$this->default_filters['free_search'] = true;
$this->default_filters['tag'] = true;
$this->free_search = $system->getRequest('free_search', '');
if ($this->free_search != '') {
$this->default = false;
$this->default_filters['free_search'] = false;
}
$this->status = $system->getRequest('status', __('Status'));
if (($this->status === __('Status')) || ((int) $this->status === AGENT_MODULE_STATUS_ALL)) {
$this->status = AGENT_MODULE_STATUS_ALL;
} else {
$this->default = false;
$this->default_filters['status'] = false;
}
$this->group = (int) $system->getRequest('group', __('Group'));
if (!$user->isInGroup($this->acl, $this->group)) {
$this->group = 0;
}
if (($this->group === __('Group')) || ($this->group == 0)) {
$this->group = 0;
} else {
$this->default = false;
$this->default_filters['group'] = false;
}
$this->module_group = (int) $system->getRequest('module_group', __('Module group'));
if (($this->module_group === __('Module group')) || ($this->module_group === -1)
|| ($this->module_group == 0)
) {
$this->module_group = -1;
} else {
$this->default = false;
$this->module_group = (int) $this->module_group;
$this->default_filters['module_group'] = false;
}
$this->tag = (int) $system->getRequest('tag', __('Tag'));
if (($this->tag === __('Tag')) || ($this->tag == 0)) {
$this->tag = 0;
} else {
$this->default = false;
$this->default_filters['tag'] = false;
}
}
public function show()
{
if (!$this->correct_acl) {
$this->show_fail_acl();
} else {
$this->getFilters();
$this->show_servers();
}
}
private function show_fail_acl()
{
$error['type'] = 'onStart';
$error['title_text'] = __('You don\'t have access to this page');
$error['content_text'] = System::getDefaultACLFailText();
if (class_exists('HomeEnterprise')) {
$home = new HomeEnterprise();
} else {
$home = new Home();
}
$home->show($error);
}
private function show_servers()
{
$ui = Ui::getInstance();
$ui->createPage();
$ui->createDefaultHeader(
__('Server status'),
$ui->createHeaderButton(
[
'icon' => 'ui-icon-back',
'pos' => 'left',
'text' => __('Back'),
'href' => 'index.php?page=home',
'class' => 'header-button-left',
]
)
);
$ui->showFooter(false);
$ui->beginContent();
$this->listServersHtml();
$ui->endContent();
$ui->showPage();
}
private function getListServers($page=0, $ajax=false)
{
global $config;
$system = System::getInstance();
$user = User::getInstance();
$total = 0;
$servers = [];
$servers_db = [];
if ($this->all_servers === true) {
$sql_limit = ' LIMIT '.(int) ($page * $system->getPageSize()).','.(int) $system->getPageSize();
}
if ($system->getConfig('metaconsole')) {
$servers_info = servers_get_info();
$total = (int) count(servers_get_info());
foreach ($servers_info as $server_value) {
$image_status = ui_print_status_image(STATUS_SERVER_OK, '', true);
if ($server_value['status'] == -1) {
$image_status = ui_print_status_image(
STATUS_SERVER_CRASH,
__('Server has crashed.'),
true
);
} else if ($server_value['status'] == 0) {
$image_status = ui_print_status_image(
STATUS_SERVER_DOWN,
__('Server is stopped.'),
true
);
}
// $row[__('Status')] = '<span class="data">'.$server_value['status'].'</span>';
$row[__('Status')] = '<span class="data">'.$image_status.'</span>';
$row[__('Image')] = '<span class="data">'.$server_value['img'].'</span>';
$row[__('Name')] = '<span class="data">'.$server_value['name'].'</span>';
$servers[$server_value['id_server'].'-'.$server_value['name']] = $row;
}
$meta_servers = metaconsole_get_servers();
if ($meta_servers === false) {
$meta_servers = [];
}
foreach ($meta_servers as $server) {
if (metaconsole_connect($server) != NOERR) {
continue;
}
$servers_info = servers_get_info();
$total += (int) count(servers_get_info());
foreach ($servers_info as $server_value) {
$image_status = ui_print_status_image(STATUS_SERVER_OK, '', true);
if ($server_value['status'] == -1) {
$image_status = ui_print_status_image(
STATUS_SERVER_CRASH,
__('Server has crashed.'),
true
);
} else if ($server_value['status'] == 0) {
$image_status = ui_print_status_image(
STATUS_SERVER_DOWN,
__('Server is stopped.'),
true
);
}
// $row[__('Status')] = '<span class="data">'.$server_value['status'].'</span>';
$row[__('Status')] = '<span class="data">'.$image_status.'</span>';
$row[__('Image')] = '<span class="data">'.$server_value['img'].'</span>';
$row[__('Name')] = '<span class="data">'.$server_value['name'].'</span>';
$servers[$server_value['id_server']] = $row;
}
metaconsole_restore_db();
}
} else {
$servers_info = servers_get_info(-1, $sql_limit);
$total = count(servers_get_info());
foreach ($servers_info as $server_value) {
echo '<script>console.log('.json_encode($server_value).');</script>';
$image_status = ui_print_status_image(STATUS_SERVER_OK, '', true);
if ($server_value['status'] == -1) {
$image_status = ui_print_status_image(
STATUS_SERVER_CRASH,
__('Server has crashed.'),
true
);
} else if ($server_value['status'] == 0) {
$image_status = ui_print_status_image(
STATUS_SERVER_DOWN,
__('Server is stopped.'),
true
);
}
// $row[__('Status')] = '<span class="data">'.$server_value['status'].'</span>';
$row[__('Status')] = '<span class="data">'.$image_status.'</span>';
$row[__('Image')] = '<span class="data">'.$server_value['img'].'</span>';
$row[__('Name')] = '<span class="data">'.$server_value['name'].'</span>';
$servers[$server_value['id_server']] = $row;
}
}
return [
'servers' => $servers,
'total' => $total,
];
}
public function listServersHtml($page=0, $return=false)
{
$system = System::getInstance();
$ui = Ui::getInstance();
$listServers = $this->getListServers($page);
if ($listServers['total'] == 0) {
$html = '<p class="empty_advice">'.__('No servers').'</p>';
if (!$return) {
$ui->contentAddHtml($html);
} else {
return $html;
}
} else {
if (!$return) {
$table = new Table();
$table->id = 'list_servers';
$table->importFromHash($listServers['servers']);
$ui->contentAddHtml($table->getHTML());
} else {
$table = new Table();
$table->id = 'list_servers_status';
$table->importFromHash($listServers['servers']);
$html = $table->getHTML();
return $html;
}
if ($this->all_servers === true) {
if ($system->getPageSize() < $listServers['total']) {
$ui->contentAddHtml(
'<div id="loading_rows">'.html_print_image('images/spinner.gif', true, false, false, false, false, true).' '.__('Loading...').'</div>'
);
$this->addJavascriptAddBottom();
}
}
}
$ui->contentAddLinkListener('list_servers');
}
private function addJavascriptAddBottom()
{
$ui = Ui::getInstance();
$ui->contentAddHtml(
"<script type=\"text/javascript\">
var load_more_rows = 1;
var page = 1;
function custom_scroll() {
if (load_more_rows) {
if ($(this).scrollTop() + $(this).height()
>= ($(document).height() - 100)) {
load_more_rows = 0;
postvars = {};
postvars[\"action\"] = \"ajax\";
postvars[\"parameter1\"] = \"server_status\";
postvars[\"parameter2\"] = \"get_server_status\";
postvars[\"page\"] = page;
page++;
$.post(\"index.php\",
postvars,
function (data) {
if (data.end) {
$(\"#loading_rows\").hide();
}
else {
$.each(data.server_status, function(key, server) {
console.log(1);
$(\"table#list_servers tbody\").append(\"<tr>\" +
\"<td class='cell_0'><b class='ui-table-cell-label'>".__('Status')."</b>\" + server['Status'] + \"</td>\" +
\"<td class='cell_1'><b class='ui-table-cell-label'>".__('Image')."</b>\" + server['Image'] + \"</td>\" +
\"<td class='cell_2'><b class='ui-table-cell-label'>".__('Name').'</b>" + server["Name"] + "</td>" +
"</tr>");
});
load_more_rows = 1;
refresh_link_listener_list_servers()
}
},
"json");
}
}
}
$(document).ready(function() {
$(window).bind("scroll", function () {
custom_scroll();
});
$(window).on("touchmove", function(event) {
custom_scroll();
});
});
</script>'
);
}
private function filterServersGetString()
{
if ($this->default) {
return __('(Default)');
} else {
$filters_to_serialize = [];
if (!$this->default_filters['group']) {
$filters_to_serialize[] = sprintf(
__('Group: %s'),
groups_get_name($this->group, true)
);
}
if (!$this->default_filters['module_group']) {
$module_group = db_get_value(
'name',
'tmodule_group',
'id_mg',
$this->module_group
);
$module_group = io_safe_output($module_group);
$filters_to_serialize[] = sprintf(
__('Module group: %s'),
$module_group
);
}
if (!$this->default_filters['status']) {
$filters_to_serialize[] = sprintf(
__('Status: %s'),
$this->list_status[$this->status]
);
}
if (!$this->default_filters['free_search']) {
$filters_to_serialize[] = sprintf(
__('Free Search: %s'),
$this->free_search
);
}
if (!$this->default_filters['tag']) {
$tag_name = tags_get_name($this->tag);
$filters_to_serialize[] = sprintf(
__('Tag: %s'),
$tag_name
);
}
$string = '('.implode(' - ', $filters_to_serialize).')';
return $string;
}
}
}