Refactor servers list (static and dynamic)

This commit is contained in:
nicolargo 2024-11-02 08:59:45 +01:00
parent d538b2dd13
commit 3352f095b6
9 changed files with 43 additions and 74 deletions

View File

@ -546,7 +546,7 @@ disable=False
# List is limited to 256 servers max (1 to 256)
server_1_name=localhost
server_1_alias=Local WebUI
server_1_port=61208
server_1_port=61266
server_1_protocol=rest
#server_1_name=localhost
#server_1_alias=My local PC

View File

@ -2,36 +2,3 @@ You are in the main Glances source folder. This page is **ONLY** for developers.
If you are looking for the user manual, please follow this link:
https://glances.readthedocs.io/en/stable/
===
__init__.py Global module init
__main__.py Entry point for Glances module
config.py Manage the configuration file
globals.py Share variables upon modules
main.py Main script to rule them up...
client.py Glances client
server.py Glances server
webserver.py Glances web server (Based on FastAPI)
autodiscover.py Glances autodiscover module (via zeroconf)
standalone.py Glances standalone (curses interface)
password.py Manage password for Glances client/server
stats.py The stats manager
timer.py The timer class
actions.py Manage trigger actions (via mustache)
snmp.py Glances SNMP client (via pysnmp)
...
plugins
=> Glances plugins
...
outputs
=> Glances UI
glances_curses.py The curses interface
glances_restful-api.py The HTTP/API & Web based interface
...
exports
=> Glances exports
...
amps
=> Glances Application Monitoring Processes (AMP)
...

View File

@ -1,7 +1,7 @@
#
# This file is part of Glances.
#
# SPDX-FileCopyrightText: 2022 Nicolas Hennion <nicolas@nicolargo.com>
# SPDX-FileCopyrightText: 2024 Nicolas Hennion <nicolas@nicolargo.com>
#
# SPDX-License-Identifier: LGPL-3.0-only
#
@ -14,13 +14,12 @@ import webbrowser
from defusedxml import xmlrpc
from glances import __apiversion__
from glances.autodiscover import GlancesAutoDiscoverServer
from glances.client import GlancesClient, GlancesClientTransport
from glances.globals import json_loads
from glances.logger import LOG_FILENAME, logger
from glances.outputs.glances_curses_browser import GlancesCursesBrowser
from glances.password_list import GlancesPasswordList as GlancesPassword
from glances.static_list import GlancesStaticServer
from glances.servers_list import GlancesServersList
try:
import requests
@ -42,26 +41,18 @@ class GlancesClientBrowser:
# Store the arg/config
self.args = args
self.config = config
self.static_server = None
self.password = None
# Load the configuration file
self.password = None
self.load()
# Start the autodiscover mode (Zeroconf listener)
if not self.args.disable_autodiscover:
self.autodiscover_server = GlancesAutoDiscoverServer()
else:
self.autodiscover_server = None
# Init the server list
self.servers_list = GlancesServersList(config=config, args=args)
# Init screen
self.screen = GlancesCursesBrowser(args=self.args)
def load(self):
"""Load server and password list from the configuration file."""
# Init the static server list (if defined)
self.static_server = GlancesStaticServer(config=self.config)
# Init the password list (if defined)
self.password = GlancesPassword(config=self.config)
@ -70,14 +61,7 @@ class GlancesClientBrowser:
Merge of static + autodiscover servers list.
"""
ret = []
if self.args.browser:
ret = self.static_server.get_servers_list()
if self.autodiscover_server is not None:
ret = self.static_server.get_servers_list() + self.autodiscover_server.get_servers_list()
return ret
return self.servers_list.get_servers_list()
def __get_uri(self, server):
"""Return the URI for the given server dict."""
@ -112,7 +96,7 @@ class GlancesClientBrowser:
return server
# Get the stats
for column in self.static_server.get_columns():
for column in self.servers_list.get_columns():
server_key = self.__get_key(column)
try:
# Value
@ -155,7 +139,7 @@ class GlancesClientBrowser:
else:
server['status'] = 'ONLINE'
for column in self.static_server.get_columns():
for column in self.servers_list.get_columns():
server_key = self.__get_key(column)
try:
r = requests.get(f'{uri}/{column['plugin']}/{column['field']}', timeout=3)
@ -295,13 +279,7 @@ class GlancesClientBrowser:
def set_in_selected(self, key, value):
"""Set the (key, value) for the selected server in the list."""
# Static list then dynamic one
if self.screen.active_server >= len(self.static_server.get_servers_list()):
self.autodiscover_server.set_server(
self.screen.active_server - len(self.static_server.get_servers_list()), key, value
)
else:
self.static_server.set_server(self.screen.active_server, key, value)
self.servers_list.set_in_selected(self.screen.active_server, key, value)
def end(self):
"""End of the client browser session."""

View File

@ -811,11 +811,11 @@ Examples of use:
def is_client(self):
"""Return True if Glances is running in client mode."""
return (self.args.client or self.args.browser) and not self.args.server
return (self.args.client or self.args.browser) and not self.args.server and not self.args.webserver
def is_client_browser(self):
"""Return True if Glances is running in client browser mode."""
return self.args.browser and not self.args.server
return self.args.browser and not self.args.server and not self.args.webserver
def is_server(self):
"""Return True if Glances is running in server mode."""

View File

@ -227,15 +227,27 @@ class GlancesRestfulApi:
for path, endpoint in route_mapping.items():
router.add_api_route(path, endpoint)
# WEB UI
# Browser WEBUI
if self.args.browser:
# Template for the root browser.html file
router.add_api_route('/browser', self._browser, response_class=HTMLResponse)
# Statics files
self._app.mount(self.url_prefix + '/static', StaticFiles(directory=self.STATIC_PATH), name="static")
logger.debug(f"The Browser WebUI is enable and got statics files in {self.STATIC_PATH}")
bindmsg = f'Glances Browser Web User Interface started on {self.bind_url}browser'
logger.info(bindmsg)
print(bindmsg)
# WEBUI
if not self.args.disable_webui:
# Template for the root index.html file
router.add_api_route('/', self._index, response_class=HTMLResponse)
# Statics files
self._app.mount(self.url_prefix + '/static', StaticFiles(directory=self.STATIC_PATH), name="static")
logger.info(f"The WebUI is enable and got statics files in {self.STATIC_PATH}")
logger.debug(f"The WebUI is enable and got statics files in {self.STATIC_PATH}")
bindmsg = f'Glances Web User Interface started on {self.bind_url}'
logger.info(bindmsg)
@ -303,6 +315,16 @@ class GlancesRestfulApi:
# Display
return self._templates.TemplateResponse("index.html", {"request": request, "refresh_time": refresh_time})
def _browser(self, request: Request):
"""Return main browser.html (/browser) file.
Note: This function is only called the first time the page is loaded.
"""
refresh_time = request.query_params.get('refresh', default=max(1, int(self.args.time)))
# Display
return self._templates.TemplateResponse("browser.html", {"request": request, "refresh_time": refresh_time})
def _api_status(self):
"""Glances API RESTful implementation.

View File

@ -12,10 +12,13 @@ module.exports = (_, env) => {
return {
mode: isProd ? 'production' : 'development',
entry: "./js/app.js",
entry: {
glances: "./js/app.js",
browser: "./js/browser.js"
},
output: {
path: path.join(__dirname, "public"),
filename: "glances.js",
filename: "[name].js",
publicPath: '/',
clean: true
},

View File

@ -16,8 +16,8 @@ from base64 import b64decode
from defusedxml import xmlrpc
from glances import __version__
from glances.autodiscover import GlancesAutoDiscoverClient
from glances.logger import logger
from glances.servers_list_dynamic import GlancesAutoDiscoverClient
from glances.stats_server import GlancesStatsServer
from glances.timer import Timer

View File

@ -46,7 +46,6 @@ class GlancesStaticServer:
new_server[s] = config.get_value(self._section, f'{postfix}{s}')
if new_server['name'] is None:
logger.error(f'Name not define for {postfix}, skip it.')
continue
# Type in order to support both RPC and REST servers (see #1121)