Merge branch 'issue1365' into develop

This commit is contained in:
nicolargo 2023-06-04 10:44:56 +02:00
commit 7f08871ca4
6 changed files with 258 additions and 628 deletions

View File

@ -22,8 +22,13 @@ history_size=1200
[outputs] [outputs]
# Theme name for the Curses interface: black or white # Theme name for the Curses interface: black or white
curse_theme=black curse_theme=black
# Limit the number of processes to display in the WebUI # Limit the number of processes to display (for the WebUI)
max_processes_display=30 max_processes_display=30
# Set the URL prefix (for the WebUI and the API)
# Example: url_prefix=/glances/ => http://localhost/glances/
# The final / is mandatory
# Default is no prefix (/)
#url_prefix=/glances/
############################################################################## ##############################################################################
# plugins # plugins

View File

@ -24,6 +24,11 @@ history_size=1200
curse_theme=black curse_theme=black
# Limit the number of processes to display in the WebUI # Limit the number of processes to display in the WebUI
max_processes_display=30 max_processes_display=30
# Set the URL prefix (for the WebUI and the API)
# Example: url_prefix=/glances/ => http://localhost/glances/
# The final / is mandatory
# Default is no prefix (/)
#url_prefix=/glances/
############################################################################## ##############################################################################
# plugins # plugins

File diff suppressed because it is too large Load Diff

View File

@ -27,7 +27,7 @@ level margin: \\n[rst2man-indent\\n[rst2man-indent-level]]
.\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]]
.in \\n[rst2man-indent\\n[rst2man-indent-level]]u .in \\n[rst2man-indent\\n[rst2man-indent-level]]u
.. ..
.TH "GLANCES" "1" "May 16, 2023" "3.4.0" "Glances" .TH "GLANCES" "1" "Jun 04, 2023" "4.0.0_beta01" "Glances"
.SH NAME .SH NAME
glances \- An eye on your system glances \- An eye on your system
.SH SYNOPSIS .SH SYNOPSIS

View File

@ -16,6 +16,7 @@ from io import open
import webbrowser import webbrowser
import zlib import zlib
import socket import socket
from urllib.parse import urljoin
from glances.globals import b, json_dumps from glances.globals import b, json_dumps
from glances.timer import Timer from glances.timer import Timer
@ -85,8 +86,10 @@ class GlancesBottle(object):
# Load configuration file # Load configuration file
self.load_config(config) self.load_config(config)
# Set the bind URL # Set the bind URL (only used for log information purpose)
self.bind_url = 'http://{}:{}/'.format(self.args.bind_address, self.args.port) self.bind_url = urljoin('http://{}:{}/'.format(self.args.bind_address,
self.args.port),
self.url_prefix)
# Init Bottle # Init Bottle
self._app = Bottle() self._app = Bottle()
@ -107,10 +110,12 @@ class GlancesBottle(object):
def load_config(self, config): def load_config(self, config):
"""Load the outputs section of the configuration file.""" """Load the outputs section of the configuration file."""
# Limit the number of processes to display in the WebUI # Limit the number of processes to display in the WebUI
self.url_prefix = '/'
if config is not None and config.has_section('outputs'): if config is not None and config.has_section('outputs'):
logger.debug('Read number of processes to display in the WebUI')
n = config.get_value('outputs', 'max_processes_display', default=None) n = config.get_value('outputs', 'max_processes_display', default=None)
logger.debug('Number of processes to display in the WebUI: {}'.format(n)) logger.debug('Number of processes to display in the WebUI: {}'.format(n))
self.url_prefix = config.get_value('outputs', 'url_prefix', default='/')
logger.debug('URL prefix: {}'.format(self.url_prefix))
def __update__(self): def __update__(self):
# Never update more than 1 time per cached_time # Never update more than 1 time per cached_time
@ -162,7 +167,7 @@ class GlancesBottle(object):
self._app.route( self._app.route(
'/api/%s/<plugin>/<item>/<value:path>' % self.API_VERSION, method="GET", callback=self._api_value '/api/%s/<plugin>/<item>/<value:path>' % self.API_VERSION, method="GET", callback=self._api_value
) )
bindmsg = 'Glances RESTful API Server started on {}api/{}/'.format(self.bind_url, self.API_VERSION) bindmsg = 'Glances RESTful API Server started on {}api/{}'.format(self.bind_url, self.API_VERSION)
logger.info(bindmsg) logger.info(bindmsg)
# WEB UI # WEB UI
@ -193,13 +198,31 @@ class GlancesBottle(object):
# 2) Glances standalone mode is running on Windows OS # 2) Glances standalone mode is running on Windows OS
webbrowser.open(self.bind_url, new=2, autoraise=1) webbrowser.open(self.bind_url, new=2, autoraise=1)
try: # Run the Web application
self._app.run(host=self.args.bind_address, port=self.args.port, quiet=not self.args.debug) if self.url_prefix != '/':
except socket.error as e: # Create an outer Bottle class instance to manage url_prefix
logger.critical('Error: Can not ran Glances Web server ({})'.format(e)) self.main_app = Bottle()
self.main_app.mount(self.url_prefix, self._app)
try:
self.main_app.run(host=self.args.bind_address,
port=self.args.port,
quiet=not self.args.debug)
except socket.error as e:
logger.critical('Error: Can not ran Glances Web server ({})'.format(e))
else:
try:
self._app.run(host=self.args.bind_address,
port=self.args.port,
quiet=not self.args.debug)
except socket.error as e:
logger.critical('Error: Can not ran Glances Web server ({})'.format(e))
def end(self): def end(self):
"""End the bottle.""" """End the bottle."""
logger.info("Close the Web server")
self._app.close()
if self.url_prefix != '/':
self.main_app.close()
def _index(self, refresh_time=None): def _index(self, refresh_time=None):
"""Bottle callback for index.html (/) file.""" """Bottle callback for index.html (/) file."""

View File

@ -30,7 +30,25 @@ The Glances Restfull/API server could be ran using the following command line:
# glances -w --disable-webui # glances -w --disable-webui
Note: Change request URL api/3 by api/2 if you use Glances 2.x. API URL
-------
The default root API URL is ``http://localhost:61208/api/3``.
The bind addresse and port could be changed using the ``--bind`` and ``--port`` command line options.
It is also possible to define an URL prefix using the ``url_prefix`` option from the [outputs] section
of the Glances configuration file. The url_prefix should always end with a slash (``/``).
For example:
.. code-block:: ini
[outputs]
url_prefix = /glances/
will change the root API URL to ``http://localhost:61208/glances/api/3`` and the Web UI URL to
``http://localhost:61208/glances/``
""" """