1
|
|
|
# |
2
|
|
|
# Glances - An eye on your system |
3
|
|
|
# |
4
|
|
|
# SPDX-FileCopyrightText: 2025 Nicolas Hennion <[email protected]> |
5
|
|
|
# |
6
|
|
|
# SPDX-License-Identifier: LGPL-3.0-only |
7
|
|
|
# |
8
|
|
|
|
9
|
|
|
from glances import __version__ as glances_version |
10
|
|
|
from glances.globals import weak_lru_cache |
11
|
|
|
from glances.main import GlancesMain |
12
|
|
|
from glances.stats import GlancesStats |
13
|
|
|
|
14
|
|
|
plugin_dependencies_tree = { |
15
|
|
|
'processlist': ['processcount'], |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
class GlancesAPI: |
20
|
|
|
ttl = 2.0 # Default cache TTL in seconds |
21
|
|
|
|
22
|
|
|
def __init__(self, config=None, args=None, args_begin_at=1): |
23
|
|
|
self.__version__ = glances_version.split('.')[0] # Get the major version |
24
|
|
|
|
25
|
|
|
core = GlancesMain(args_begin_at) |
26
|
|
|
self.args = args if args is not None else core.get_args() |
27
|
|
|
self.config = config if config is not None else core.get_config() |
28
|
|
|
self._stats = GlancesStats(config=self.config, args=self.args) |
29
|
|
|
|
30
|
|
|
# Set the cache TTL for the API |
31
|
|
|
self.ttl = self.args.time if self.args.time is not None else self.ttl |
32
|
|
|
|
33
|
|
|
# Init the stats of all plugins in order to ensure that rate are computed |
34
|
|
|
self._stats.update() |
35
|
|
|
|
36
|
|
|
@weak_lru_cache(maxsize=1, ttl=ttl) |
37
|
|
|
def __getattr__(self, item): |
38
|
|
|
"""Fallback to the stats object for any missing attributes.""" |
39
|
|
|
if item in self._stats.getPluginsList(): |
40
|
|
|
if item in plugin_dependencies_tree: |
41
|
|
|
# Ensure dependencies are updated before accessing the plugin |
42
|
|
|
for dependency in plugin_dependencies_tree[item]: |
43
|
|
|
self._stats.get_plugin(dependency).update() |
44
|
|
|
# Update the plugin stats |
45
|
|
|
self._stats.get_plugin(item).update() |
46
|
|
|
return self._stats.get_plugin(item) |
47
|
|
|
raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{item}'") |
48
|
|
|
|
49
|
|
|
def plugins(self): |
50
|
|
|
"""Return the list of available plugins.""" |
51
|
|
|
return self._stats.getPluginsList() |
52
|
|
|
|