From 1c6a9e1412bb010a23c07380df29342853fe1449 Mon Sep 17 00:00:00 2001 From: nicolargo Date: Mon, 3 Nov 2025 14:44:11 +0100 Subject: [PATCH] Restful API issue after a while (stats are no more updated) #3333 --- .gitignore | 1 + glances/outputs/glances_restful_api.py | 9 ---- glances/stats.py | 2 +- .../issues/issue3322-homepage/README.txt | 52 +++++++++++++++++++ .../issues/issue3322-homepage/run-homepage.sh | 7 +++ .../issues/issue3333-homeassistant/README.txt | 18 +++++++ .../run-homeassistant.sh | 9 ++++ tests/test_perf.py | 20 ++++++- 8 files changed, 106 insertions(+), 12 deletions(-) create mode 100644 tests-data/issues/issue3322-homepage/README.txt create mode 100644 tests-data/issues/issue3322-homepage/run-homepage.sh create mode 100644 tests-data/issues/issue3333-homeassistant/README.txt create mode 100644 tests-data/issues/issue3333-homeassistant/run-homeassistant.sh diff --git a/.gitignore b/.gitignore index e48fd58c..69815871 100644 --- a/.gitignore +++ b/.gitignore @@ -69,3 +69,4 @@ uv.lock # Test .coverage +tests-data/issues/*/config/ diff --git a/glances/outputs/glances_restful_api.py b/glances/outputs/glances_restful_api.py index 314a804e..2a8c8192 100644 --- a/glances/outputs/glances_restful_api.py +++ b/glances/outputs/glances_restful_api.py @@ -11,7 +11,6 @@ import os import socket import sys -import tempfile import webbrowser from typing import Annotated, Any, Union from urllib.parse import urljoin @@ -47,7 +46,6 @@ try: except ImportError: logger.critical('Uvicorn import error. Glances cannot start in web server mode.') sys.exit(2) -import builtins import contextlib import threading import time @@ -469,13 +467,6 @@ class GlancesRestfulApi: HTTP/400 if plugin is not found HTTP/404 if others error """ - if self.args.debug: - fname = os.path.join(tempfile.gettempdir(), 'glances-debug.json') - try: - with builtins.open(fname) as f: - return f.read() - except OSError: - logger.debug(f"Debug file ({fname}) not found") # Update the stat self.__update_stats() diff --git a/glances/stats.py b/glances/stats.py index c9d70cd9..4878c3b3 100644 --- a/glances/stats.py +++ b/glances/stats.py @@ -268,7 +268,7 @@ please rename it to "{plugin_path.capitalize()}Plugin"' # It's a weak cache to avoid updating the same plugin too often # Note: the function always return None - @weak_lru_cache(maxsize=128, ttl=1) + @weak_lru_cache(ttl=1) def update_plugin(self, p): """Update stats, history and views for the given plugin name p""" self._plugins[p].update() diff --git a/tests-data/issues/issue3322-homepage/README.txt b/tests-data/issues/issue3322-homepage/README.txt new file mode 100644 index 00000000..c8519a35 --- /dev/null +++ b/tests-data/issues/issue3322-homepage/README.txt @@ -0,0 +1,52 @@ +Pre-requisites: +- Docker needs to be installed on your system +- https://gethomepage.dev/installation/docker/ + +Start Docker: + + cd ./tests-data/issues/issue3322-homepage/ + sh ./run-homepage.sh + +Access to the interface: + + firefox http://localhost:3000/ + +Edit the ./config/widgets.yaml file and add (replace 192.168.1.26 by your local IP @): + +- glances: + url: http://192.168.1.26:61208 + # username: user # optional if auth enabled in Glances + # password: pass # optional if auth enabled in Glances + version: 4 # required only if running glances v4 or higher, defaults to 3 + cpu: true # optional, enabled by default, disable by setting to false + mem: true # optional, enabled by default, disable by setting to false + cputemp: true # disabled by default + uptime: true # disabled by default + disk: / # disabled by default, use mount point of disk(s) in glances. Can also be a list (see below) + diskUnits: bytes # optional, bytes (default) or bbytes. Only applies to disk + expanded: true # show the expanded view + label: MyMachine # optional + +And the ./config/services.yaml (replace 192.168.1.26 by your local IP @): + +- Glances: + - CPU Usage: + widget: + type: glances + url: http://192.168.1.26:61208 + version: 4 # required only if running glances v4 or higher, defaults to 3 + metric: cpu + + - MEM Usage: + widget: + type: glances + url: http://192.168.1.26:61208 + version: 4 # required only if running glances v4 or higher, defaults to 3 + metric: memory + + - Network Usage: + widget: + type: glances + url: http://192.168.1.26:61208 + version: 4 # required only if running glances v4 or higher, defaults to 3 + metric: network:wlp0s20f3 diff --git a/tests-data/issues/issue3322-homepage/run-homepage.sh b/tests-data/issues/issue3322-homepage/run-homepage.sh new file mode 100644 index 00000000..26b18ef1 --- /dev/null +++ b/tests-data/issues/issue3322-homepage/run-homepage.sh @@ -0,0 +1,7 @@ +docker run --rm \ + --name homepage \ + -p 3000:3000 \ + -e HOMEPAGE_ALLOWED_HOSTS=localhost:3000,0.0.0.0:3000 \ + -v ./config:/app/config \ + -v /var/run/docker.sock:/var/run/docker.sock \ + ghcr.io/gethomepage/homepage:latest diff --git a/tests-data/issues/issue3333-homeassistant/README.txt b/tests-data/issues/issue3333-homeassistant/README.txt new file mode 100644 index 00000000..536e521e --- /dev/null +++ b/tests-data/issues/issue3333-homeassistant/README.txt @@ -0,0 +1,18 @@ +Pre-requisites: +- Docker needs to be installed on your system +- https://www.home-assistant.io/installation/linux/#install-home-assistant-container + +Start Docker: + + cd ./tests-data/issues/issue3333-homeassistant/ + sh ./run-homeassistant.sh + +Access to the interface: + + firefox http://localhost:8123/ + +And install the Glances plugin. + + Parameters / Services / + Add / Glances + +Stats will be refreshed every 5 minutes. diff --git a/tests-data/issues/issue3333-homeassistant/run-homeassistant.sh b/tests-data/issues/issue3333-homeassistant/run-homeassistant.sh new file mode 100644 index 00000000..8874c411 --- /dev/null +++ b/tests-data/issues/issue3333-homeassistant/run-homeassistant.sh @@ -0,0 +1,9 @@ +docker run -d \ + --name homeassistant \ + --privileged \ + --restart=unless-stopped \ + -e TZ=Europe/Paris \ + -v ./config:/config \ + -v /run/dbus:/run/dbus:ro \ + --network=host \ + ghcr.io/home-assistant/home-assistant:stable diff --git a/tests/test_perf.py b/tests/test_perf.py index eb88914a..4358c551 100755 --- a/tests/test_perf.py +++ b/tests/test_perf.py @@ -16,9 +16,25 @@ def test_perf_update(glances_stats): """ Test Glances perf. """ - perf_timer = Timer(6) + test_duration = 12 # seconds + perf_timer = Timer(test_duration) counter = 0 + from_cache = 0 + from_update = 0 + previous_interrupts_gauge = None while not perf_timer.finished(): glances_stats.update() + # interrupts_gauge should always increase + interrupts_gauge = glances_stats.get_plugin('cpu').get_raw().get('interrupts_gauge') + if interrupts_gauge is not None: + if interrupts_gauge == previous_interrupts_gauge: + from_cache += 1 + else: + from_update += 1 + previous_interrupts_gauge = interrupts_gauge counter += 1 - assert counter > 6 + print(f"{counter} iterations. From cache: {from_cache} | From update: {from_update}") + assert counter > test_duration + assert from_update < from_cache + assert from_cache >= test_duration * 10 + assert from_update >= (test_duration / 2) - 1