mirror of
https://github.com/nicolargo/glances.git
synced 2024-10-26 20:26:58 +03:00
Merge pull request #2374 from nicolargo/issue1985
Podman support for glances
This commit is contained in:
commit
2a1b1db5f1
@ -391,7 +391,7 @@ port_default_gateway=True
|
||||
#web_4_url=https://blog.nicolargo.com/nonexist
|
||||
#web_4_description=Intranet
|
||||
|
||||
[docker]
|
||||
[containers]
|
||||
disable=False
|
||||
# Only show specific containers (comma separated list of container name or regular expression)
|
||||
# Comment this line to display all containers (default configuration)
|
||||
@ -417,6 +417,8 @@ max_name_size=20
|
||||
# By default, Glances only display running containers
|
||||
# Set the following key to True to display all containers
|
||||
all=False
|
||||
# Define Podman sock
|
||||
#podman_sock=unix:///run/user/1000/podman/podman.sock
|
||||
|
||||
[amps]
|
||||
# AMPs configuration are defined in the bottom of this file
|
||||
|
@ -20,6 +20,7 @@ import types
|
||||
import subprocess
|
||||
import os
|
||||
from datetime import datetime
|
||||
import re
|
||||
|
||||
from glances.logger import logger
|
||||
|
||||
@ -366,3 +367,39 @@ def urlopen_auth(url, username, password):
|
||||
headers={'Authorization': 'Basic ' + base64.b64encode(('%s:%s' % (username, password)).encode()).decode()},
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def string_value_to_float(s):
|
||||
"""Convert a string with a value and an unit to a float.
|
||||
Example:
|
||||
'12.5 MB' -> 12500000.0
|
||||
'32.5 GB' -> 32500000000.0
|
||||
Args:
|
||||
s (string): Input string with value and unit
|
||||
Output:
|
||||
float: The value in float
|
||||
"""
|
||||
convert_dict = {
|
||||
None: 1,
|
||||
'B': 1,
|
||||
'KB': 1000,
|
||||
'MB': 1000000,
|
||||
'GB': 1000000000,
|
||||
'TB': 1000000000000,
|
||||
'PB': 1000000000000000,
|
||||
}
|
||||
unpack_string = [
|
||||
i[0] if i[1] == '' else i[1].upper() for i in re.findall(r'([\d.]+)|([^\d.]+)', s.replace(' ', ''))
|
||||
]
|
||||
if len(unpack_string) == 2:
|
||||
value, unit = unpack_string
|
||||
elif len(unpack_string) == 1:
|
||||
value = unpack_string[0]
|
||||
unit = None
|
||||
else:
|
||||
return None
|
||||
try:
|
||||
value = float(unpack_string[0])
|
||||
except ValueError:
|
||||
return None
|
||||
return value * convert_dict[unit]
|
||||
|
@ -57,7 +57,7 @@ class _GlancesCurses(object):
|
||||
'c': {'sort_key': 'cpu_percent'},
|
||||
'C': {'switch': 'disable_cloud'},
|
||||
'd': {'switch': 'disable_diskio'},
|
||||
'D': {'switch': 'disable_docker'},
|
||||
'D': {'switch': 'disable_containers'},
|
||||
# 'e' > Enable/Disable process extended
|
||||
# 'E' > Erase the process filter
|
||||
# 'f' > Show/hide fs / folder stats
|
||||
@ -124,7 +124,7 @@ class _GlancesCurses(object):
|
||||
_left_sidebar_max_width = 34
|
||||
|
||||
# Define right sidebar
|
||||
_right_sidebar = ['docker', 'processcount', 'amps', 'processlist', 'alert']
|
||||
_right_sidebar = ['containers', 'processcount', 'amps', 'processlist', 'alert']
|
||||
|
||||
def __init__(self, config=None, args=None):
|
||||
# Init
|
||||
@ -617,7 +617,7 @@ class _GlancesCurses(object):
|
||||
max_processes_displayed = (
|
||||
self.term_window.getmaxyx()[0]
|
||||
- 11
|
||||
- (0 if 'docker' not in __stat_display else self.get_stats_display_height(__stat_display["docker"]))
|
||||
- (0 if 'containers' not in __stat_display else self.get_stats_display_height(__stat_display["containers"]))
|
||||
- (
|
||||
0
|
||||
if 'processcount' not in __stat_display
|
||||
|
0
glances/plugins/containers/__init__.py
Normal file
0
glances/plugins/containers/__init__.py
Normal file
340
glances/plugins/containers/glances_docker.py
Normal file
340
glances/plugins/containers/glances_docker.py
Normal file
@ -0,0 +1,340 @@
|
||||
"""Docker Extension unit for Glances' Containers plugin."""
|
||||
import time
|
||||
|
||||
from glances.compat import iterkeys, itervalues, nativestr, pretty_date
|
||||
from glances.logger import logger
|
||||
from glances.plugins.containers.stats_streamer import StatsStreamer
|
||||
|
||||
# Docker-py library (optional and Linux-only)
|
||||
# https://github.com/docker/docker-py
|
||||
try:
|
||||
import docker
|
||||
from dateutil import parser, tz
|
||||
except Exception as e:
|
||||
import_docker_error_tag = True
|
||||
# Display debug message if import KeyError
|
||||
logger.debug("Error loading Docker deps Lib. Docker plugin is disabled ({})".format(e))
|
||||
else:
|
||||
import_docker_error_tag = False
|
||||
|
||||
|
||||
class DockerStatsFetcher:
|
||||
MANDATORY_MEMORY_FIELDS = ["usage", 'limit']
|
||||
|
||||
def __init__(self, container):
|
||||
self._container = container
|
||||
|
||||
# Previous computes stats are stored in the self._old_computed_stats variable
|
||||
# We store time data to enable IoR/s & IoW/s calculations to avoid complexity for consumers of the APIs exposed.
|
||||
self._old_computed_stats = {}
|
||||
|
||||
# Last time when output stats (results) were computed
|
||||
self._last_stats_computed_time = 0
|
||||
|
||||
# Threaded Streamer
|
||||
stats_iterable = container.stats(decode=True)
|
||||
self._streamer = StatsStreamer(stats_iterable, initial_stream_value={})
|
||||
|
||||
def _log_debug(self, msg, exception=None):
|
||||
logger.debug("containers (Docker) ID: {} - {} ({}) ".format(self._container.id, msg, exception))
|
||||
logger.debug(self._streamer.stats)
|
||||
|
||||
def stop(self):
|
||||
self._streamer.stop()
|
||||
|
||||
@property
|
||||
def activity_stats(self):
|
||||
"""Activity Stats
|
||||
|
||||
Each successive access of activity_stats will cause computation of activity_stats
|
||||
"""
|
||||
computed_activity_stats = self._compute_activity_stats()
|
||||
self._old_computed_stats = computed_activity_stats
|
||||
self._last_stats_computed_time = time.time()
|
||||
return computed_activity_stats
|
||||
|
||||
def _compute_activity_stats(self):
|
||||
with self._streamer.result_lock:
|
||||
io_stats = self._get_io_stats()
|
||||
cpu_stats = self._get_cpu_stats()
|
||||
memory_stats = self._get_memory_stats()
|
||||
network_stats = self._get_network_stats()
|
||||
|
||||
computed_stats = {
|
||||
"io": io_stats or {},
|
||||
"memory": memory_stats or {},
|
||||
"network": network_stats or {},
|
||||
"cpu": cpu_stats or {"total": 0.0},
|
||||
}
|
||||
return computed_stats
|
||||
|
||||
@property
|
||||
def time_since_update(self):
|
||||
# In case no update, default to 1
|
||||
return max(1, self._streamer.last_update_time - self._last_stats_computed_time)
|
||||
|
||||
def _get_cpu_stats(self):
|
||||
"""Return the container CPU usage.
|
||||
|
||||
Output: a dict {'total': 1.49}
|
||||
"""
|
||||
stats = {'total': 0.0}
|
||||
|
||||
try:
|
||||
cpu_stats = self._streamer.stats['cpu_stats']
|
||||
precpu_stats = self._streamer.stats['precpu_stats']
|
||||
cpu = {'system': cpu_stats['system_cpu_usage'], 'total': cpu_stats['cpu_usage']['total_usage']}
|
||||
precpu = {'system': precpu_stats['system_cpu_usage'], 'total': precpu_stats['cpu_usage']['total_usage']}
|
||||
|
||||
# Issue #1857
|
||||
# If either precpu_stats.online_cpus or cpu_stats.online_cpus is nil
|
||||
# then for compatibility with older daemons the length of
|
||||
# the corresponding cpu_usage.percpu_usage array should be used.
|
||||
cpu['nb_core'] = cpu_stats.get('online_cpus') or len(cpu_stats['cpu_usage']['percpu_usage'] or [])
|
||||
except KeyError as e:
|
||||
self._log_debug("Can't grab CPU stats", e)
|
||||
return None
|
||||
|
||||
try:
|
||||
cpu_delta = cpu['total'] - precpu['total']
|
||||
system_cpu_delta = cpu['system'] - precpu['system']
|
||||
# CPU usage % = (cpu_delta / system_cpu_delta) * number_cpus * 100.0
|
||||
stats['total'] = (cpu_delta / system_cpu_delta) * cpu['nb_core'] * 100.0
|
||||
except TypeError as e:
|
||||
self._log_debug("Can't compute CPU usage", e)
|
||||
return None
|
||||
|
||||
# Return the stats
|
||||
return stats
|
||||
|
||||
def _get_memory_stats(self):
|
||||
"""Return the container MEMORY.
|
||||
|
||||
Output: a dict {'rss': 1015808, 'cache': 356352, 'usage': ..., 'max_usage': ...}
|
||||
"""
|
||||
memory_stats = self._streamer.stats.get('memory_stats')
|
||||
|
||||
# Checks for memory_stats & mandatory fields
|
||||
if not memory_stats or any(field not in memory_stats for field in self.MANDATORY_MEMORY_FIELDS):
|
||||
self._log_debug("Missing MEM usage fields")
|
||||
return None
|
||||
|
||||
stats = {field: memory_stats[field] for field in self.MANDATORY_MEMORY_FIELDS}
|
||||
try:
|
||||
# Issue #1857 - Some stats are not always available in ['memory_stats']['stats']
|
||||
detailed_stats = memory_stats['stats']
|
||||
stats['rss'] = detailed_stats.get('rss') or detailed_stats.get('total_rss')
|
||||
stats['max_usage'] = detailed_stats.get('max_usage')
|
||||
stats['cache'] = detailed_stats.get('cache')
|
||||
except (KeyError, TypeError) as e:
|
||||
self._log_debug("Can't grab MEM usage", e) # stats do not have MEM information
|
||||
return None
|
||||
|
||||
# Return the stats
|
||||
return stats
|
||||
|
||||
def _get_network_stats(self):
|
||||
"""Return the container network usage using the Docker API (v1.0 or higher).
|
||||
|
||||
Output: a dict {'time_since_update': 3000, 'rx': 10, 'tx': 65}.
|
||||
with:
|
||||
time_since_update: number of seconds elapsed between the latest grab
|
||||
rx: Number of bytes received
|
||||
tx: Number of bytes transmitted
|
||||
"""
|
||||
eth0_stats = self._streamer.stats.get('networks', {}).get('eth0')
|
||||
|
||||
# Checks for net_stats & mandatory fields
|
||||
if not eth0_stats or any(field not in eth0_stats for field in ['rx_bytes', 'tx_bytes']):
|
||||
self._log_debug("Missing Network usage fields")
|
||||
return None
|
||||
|
||||
# Read the rx/tx stats (in bytes)
|
||||
stats = {'cumulative_rx': eth0_stats["rx_bytes"], 'cumulative_tx': eth0_stats["tx_bytes"]}
|
||||
|
||||
# Using previous stats to calculate rates
|
||||
old_network_stats = self._old_computed_stats.get("network")
|
||||
if old_network_stats:
|
||||
stats['time_since_update'] = round(self.time_since_update)
|
||||
stats['rx'] = stats['cumulative_rx'] - old_network_stats["cumulative_rx"]
|
||||
stats['tx'] = stats['cumulative_tx'] - old_network_stats['cumulative_tx']
|
||||
|
||||
# Return the stats
|
||||
return stats
|
||||
|
||||
def _get_io_stats(self):
|
||||
"""Return the container IO usage using the Docker API (v1.0 or higher).
|
||||
|
||||
Output: a dict {'time_since_update': 3000, 'ior': 10, 'iow': 65}.
|
||||
with:
|
||||
time_since_update: number of seconds elapsed between the latest grab
|
||||
ior: Number of bytes read
|
||||
iow: Number of bytes written
|
||||
"""
|
||||
io_service_bytes_recursive = self._streamer.stats.get('blkio_stats', {}).get('io_service_bytes_recursive')
|
||||
|
||||
# Checks for net_stats
|
||||
if not io_service_bytes_recursive:
|
||||
self._log_debug("Missing blockIO usage fields")
|
||||
return None
|
||||
|
||||
# Read the ior/iow stats (in bytes)
|
||||
try:
|
||||
# Read IOR and IOW value in the structure list of dict
|
||||
cumulative_ior = [i for i in io_service_bytes_recursive if i['op'].lower() == 'read'][0]['value']
|
||||
cumulative_iow = [i for i in io_service_bytes_recursive if i['op'].lower() == 'write'][0]['value']
|
||||
except (TypeError, IndexError, KeyError, AttributeError) as e:
|
||||
self._log_debug("Can't grab blockIO usage", e) # stats do not have io information
|
||||
return None
|
||||
|
||||
stats = {'cumulative_ior': cumulative_ior, 'cumulative_iow': cumulative_iow}
|
||||
|
||||
# Using previous stats to calculate difference
|
||||
old_io_stats = self._old_computed_stats.get("io")
|
||||
if old_io_stats:
|
||||
stats['time_since_update'] = round(self.time_since_update)
|
||||
stats['ior'] = stats['cumulative_ior'] - old_io_stats["cumulative_ior"]
|
||||
stats['iow'] = stats['cumulative_iow'] - old_io_stats["cumulative_iow"]
|
||||
|
||||
# Return the stats
|
||||
return stats
|
||||
|
||||
|
||||
class DockerContainersExtension:
|
||||
"""Glances' Containers Plugin's Docker Extension unit"""
|
||||
|
||||
CONTAINER_ACTIVE_STATUS = ['running', 'paused']
|
||||
|
||||
def __init__(self):
|
||||
if import_docker_error_tag:
|
||||
raise Exception("Missing libs required to run Docker Extension (Containers) ")
|
||||
|
||||
self.client = None
|
||||
self.ext_name = "containers (Docker)"
|
||||
self.stats_fetchers = {}
|
||||
self.connect()
|
||||
|
||||
def connect(self):
|
||||
"""Connect to the Docker server."""
|
||||
# Init the Docker API Client
|
||||
try:
|
||||
# Do not use the timeout option (see issue #1878)
|
||||
self.client = docker.from_env()
|
||||
except Exception as e:
|
||||
logger.error("{} plugin - Can't connect to Docker ({})".format(self.ext_name, e))
|
||||
self.client = None
|
||||
|
||||
def stop(self):
|
||||
# Stop all streaming threads
|
||||
for t in itervalues(self.stats_fetchers):
|
||||
t.stop()
|
||||
|
||||
def update(self, all_tag):
|
||||
"""Update Docker stats using the input method."""
|
||||
# Docker version
|
||||
# Example: {
|
||||
# "KernelVersion": "3.16.4-tinycore64",
|
||||
# "Arch": "amd64",
|
||||
# "ApiVersion": "1.15",
|
||||
# "Version": "1.3.0",
|
||||
# "GitCommit": "c78088f",
|
||||
# "Os": "linux",
|
||||
# "GoVersion": "go1.3.3"
|
||||
# }
|
||||
try:
|
||||
version_stats = self.client.version()
|
||||
except Exception as e:
|
||||
# Correct issue#649
|
||||
logger.error("{} plugin - Can't get Docker version ({})".format(self.ext_name, e))
|
||||
return {}, []
|
||||
|
||||
# Update current containers list
|
||||
try:
|
||||
# Issue #1152: Docker module doesn't export details about stopped containers
|
||||
# The Containers/all key of the configuration file should be set to True
|
||||
containers = self.client.containers.list(all=all_tag)
|
||||
except Exception as e:
|
||||
logger.error("{} plugin - Can't get containers list ({})".format(self.ext_name, e))
|
||||
return version_stats, []
|
||||
|
||||
# Start new thread for new container
|
||||
for container in containers:
|
||||
if container.id not in self.stats_fetchers:
|
||||
# StatsFetcher did not exist in the internal dict
|
||||
# Create it, add it to the internal dict
|
||||
logger.debug("{} plugin - Create thread for container {}".format(self.ext_name, container.id[:12]))
|
||||
self.stats_fetchers[container.id] = DockerStatsFetcher(container)
|
||||
|
||||
# Stop threads for non-existing containers
|
||||
absent_containers = set(iterkeys(self.stats_fetchers)) - set(c.id for c in containers)
|
||||
for container_id in absent_containers:
|
||||
# Stop the StatsFetcher
|
||||
logger.debug("{} plugin - Stop thread for old container {}".format(self.ext_name, container_id[:12]))
|
||||
self.stats_fetchers[container_id].stop()
|
||||
# Delete the StatsFetcher from the dict
|
||||
del self.stats_fetchers[container_id]
|
||||
|
||||
# Get stats for all containers
|
||||
container_stats = [self.generate_stats(container) for container in containers]
|
||||
return version_stats, container_stats
|
||||
|
||||
@property
|
||||
def key(self):
|
||||
"""Return the key of the list."""
|
||||
return 'name'
|
||||
|
||||
def generate_stats(self, container):
|
||||
# Init the stats for the current container
|
||||
stats = {
|
||||
'key': self.key,
|
||||
# Export name
|
||||
'name': nativestr(container.name),
|
||||
# Container Id
|
||||
'Id': container.id,
|
||||
# Container Image
|
||||
'Image': str(container.image.tags),
|
||||
# Container Status (from attrs)
|
||||
'Status': container.attrs['State']['Status'],
|
||||
'Created': container.attrs['Created'],
|
||||
'Command': [],
|
||||
}
|
||||
|
||||
if container.attrs['Config'].get('Entrypoint', None):
|
||||
stats['Command'].extend(container.attrs['Config'].get('Entrypoint', []))
|
||||
if container.attrs['Config'].get('Cmd', None):
|
||||
stats['Command'].extend(container.attrs['Config'].get('Cmd', []))
|
||||
if not stats['Command']:
|
||||
stats['Command'] = None
|
||||
|
||||
if stats['Status'] in self.CONTAINER_ACTIVE_STATUS:
|
||||
stats['StartedAt'] = container.attrs['State']['StartedAt']
|
||||
stats_fetcher = self.stats_fetchers[container.id]
|
||||
activity_stats = stats_fetcher.activity_stats
|
||||
stats.update(activity_stats)
|
||||
|
||||
# Additional fields
|
||||
stats['cpu_percent'] = stats["cpu"]['total']
|
||||
stats['memory_usage'] = stats["memory"].get('usage')
|
||||
if stats['memory'].get('cache') is not None:
|
||||
stats['memory_usage'] -= stats['memory']['cache']
|
||||
stats['io_r'] = stats['io'].get('ior')
|
||||
stats['io_w'] = stats['io'].get('iow')
|
||||
stats['network_rx'] = stats['network'].get('rx')
|
||||
stats['network_tx'] = stats['network'].get('tx')
|
||||
stats['Uptime'] = pretty_date(
|
||||
parser.parse(stats['StartedAt']).astimezone(tz.tzlocal()).replace(tzinfo=None)
|
||||
)
|
||||
else:
|
||||
stats['io'] = {}
|
||||
stats['cpu'] = {}
|
||||
stats['memory'] = {}
|
||||
stats['network'] = {}
|
||||
stats['io_r'] = None
|
||||
stats['io_w'] = None
|
||||
stats['cpu_percent'] = None
|
||||
stats['memory_percent'] = None
|
||||
stats['network_rx'] = None
|
||||
stats['network_tx'] = None
|
||||
stats['Uptime'] = None
|
||||
|
||||
return stats
|
345
glances/plugins/containers/glances_podman.py
Normal file
345
glances/plugins/containers/glances_podman.py
Normal file
@ -0,0 +1,345 @@
|
||||
"""Podman Extension unit for Glances' Containers plugin."""
|
||||
import time
|
||||
from datetime import datetime
|
||||
|
||||
from glances.compat import iterkeys, itervalues, nativestr, pretty_date, string_value_to_float
|
||||
from glances.logger import logger
|
||||
from glances.plugins.containers.stats_streamer import StatsStreamer
|
||||
|
||||
# Podman library (optional and Linux-only)
|
||||
# https://pypi.org/project/podman/
|
||||
try:
|
||||
from podman import PodmanClient
|
||||
except Exception as e:
|
||||
import_podman_error_tag = True
|
||||
# Display debug message if import KeyError
|
||||
logger.debug("Error loading Podman deps Lib. Podman feature in the Containers plugin is disabled ({})".format(e))
|
||||
else:
|
||||
import_podman_error_tag = False
|
||||
|
||||
|
||||
class PodmanContainerStatsFetcher:
|
||||
MANDATORY_FIELDS = ["CPU", "MemUsage", "MemLimit", "NetInput", "NetOutput", "BlockInput", "BlockOutput"]
|
||||
|
||||
def __init__(self, container):
|
||||
self._container = container
|
||||
|
||||
# Threaded Streamer
|
||||
stats_iterable = container.stats(decode=True)
|
||||
self._streamer = StatsStreamer(stats_iterable, initial_stream_value={})
|
||||
|
||||
def _log_debug(self, msg, exception=None):
|
||||
logger.debug("containers (Podman) ID: {} - {} ({})".format(self._container.id, msg, exception))
|
||||
logger.debug(self._streamer.stats)
|
||||
|
||||
def stop(self):
|
||||
self._streamer.stop()
|
||||
|
||||
@property
|
||||
def stats(self):
|
||||
stats = self._streamer.stats
|
||||
if stats["Error"]:
|
||||
self._log_debug("Stats fetching failed", stats["Error"])
|
||||
|
||||
return stats["Stats"][0]
|
||||
|
||||
@property
|
||||
def activity_stats(self):
|
||||
result_stats = {"cpu": {}, "memory": {}, "io": {}, "network": {}}
|
||||
api_stats = self.stats
|
||||
|
||||
if any(field not in api_stats for field in self.MANDATORY_FIELDS):
|
||||
self._log_debug("Missing mandatory fields")
|
||||
return result_stats
|
||||
|
||||
try:
|
||||
cpu_usage = float(api_stats.get("CPU", 0))
|
||||
|
||||
mem_usage = float(api_stats["MemUsage"])
|
||||
mem_limit = float(api_stats["MemLimit"])
|
||||
|
||||
rx = float(api_stats["NetInput"])
|
||||
tx = float(api_stats["NetOutput"])
|
||||
|
||||
ior = float(api_stats["BlockInput"])
|
||||
iow = float(api_stats["BlockOutput"])
|
||||
|
||||
# Hardcode `time_since_update` to 1 as podman already sends the calculated rate
|
||||
result_stats = {
|
||||
"cpu": {"total": cpu_usage},
|
||||
"memory": {"usage": mem_usage, "limit": mem_limit},
|
||||
"io": {"ior": ior, "iow": iow, "time_since_update": 1},
|
||||
"network": {"rx": rx, "tx": tx, "time_since_update": 1},
|
||||
}
|
||||
except ValueError as e:
|
||||
self._log_debug("Non float stats values found", e)
|
||||
|
||||
return result_stats
|
||||
|
||||
|
||||
class PodmanPodStatsFetcher:
|
||||
def __init__(self, pod_manager):
|
||||
self._pod_manager = pod_manager
|
||||
|
||||
# Threaded Streamer
|
||||
# Temporary patch to get podman extension working
|
||||
stats_iterable = (pod_manager.stats(decode=True) for _ in iter(int, 1))
|
||||
self._streamer = StatsStreamer(stats_iterable, initial_stream_value={})
|
||||
|
||||
def _log_debug(self, msg, exception=None):
|
||||
logger.debug("containers (Podman): Pod Manager - {} ({})".format(msg, exception))
|
||||
logger.debug(self._streamer.stats)
|
||||
|
||||
def stop(self):
|
||||
self._streamer.stop()
|
||||
|
||||
@property
|
||||
def activity_stats(self):
|
||||
result_stats = {}
|
||||
container_stats = self._streamer.stats
|
||||
for stat in container_stats:
|
||||
io_stats = self._get_io_stats(stat)
|
||||
cpu_stats = self._get_cpu_stats(stat)
|
||||
memory_stats = self._get_memory_stats(stat)
|
||||
network_stats = self._get_network_stats(stat)
|
||||
|
||||
computed_stats = {
|
||||
"name": stat["Name"],
|
||||
"cid": stat["CID"],
|
||||
"pod_id": stat["Pod"],
|
||||
"io": io_stats or {},
|
||||
"memory": memory_stats or {},
|
||||
"network": network_stats or {},
|
||||
"cpu": cpu_stats or {"total": 0.0},
|
||||
}
|
||||
result_stats[stat["CID"]] = computed_stats
|
||||
|
||||
return result_stats
|
||||
|
||||
def _get_cpu_stats(self, stats):
|
||||
"""Return the container CPU usage.
|
||||
|
||||
Output: a dict {'total': 1.49}
|
||||
"""
|
||||
if "CPU" not in stats:
|
||||
self._log_debug("Missing CPU usage fields")
|
||||
return None
|
||||
|
||||
cpu_usage = string_value_to_float(stats["CPU"].rstrip("%"))
|
||||
return {"total": cpu_usage}
|
||||
|
||||
def _get_memory_stats(self, stats):
|
||||
"""Return the container MEMORY.
|
||||
|
||||
Output: a dict {'rss': 1015808, 'cache': 356352, 'usage': ..., 'max_usage': ...}
|
||||
"""
|
||||
if "MemUsage" not in stats or "/" not in stats["MemUsage"]:
|
||||
self._log_debug("Missing MEM usage fields")
|
||||
return None
|
||||
|
||||
memory_usage_str = stats["MemUsage"]
|
||||
usage_str, limit_str = memory_usage_str.split("/")
|
||||
|
||||
try:
|
||||
usage = string_value_to_float(usage_str)
|
||||
limit = string_value_to_float(limit_str)
|
||||
except ValueError as e:
|
||||
self._log_debug("Compute MEM usage failed", e)
|
||||
return None
|
||||
|
||||
return {"usage": usage, "limit": limit}
|
||||
|
||||
def _get_network_stats(self, stats):
|
||||
"""Return the container network usage using the Docker API (v1.0 or higher).
|
||||
|
||||
Output: a dict {'time_since_update': 3000, 'rx': 10, 'tx': 65}.
|
||||
with:
|
||||
time_since_update: number of seconds elapsed between the latest grab
|
||||
rx: Number of bytes received
|
||||
tx: Number of bytes transmitted
|
||||
"""
|
||||
if "NetIO" not in stats or "/" not in stats["NetIO"]:
|
||||
self._log_debug("Compute MEM usage failed")
|
||||
return None
|
||||
|
||||
net_io_str = stats["NetIO"]
|
||||
rx_str, tx_str = net_io_str.split("/")
|
||||
|
||||
try:
|
||||
rx = string_value_to_float(rx_str)
|
||||
tx = string_value_to_float(tx_str)
|
||||
except ValueError as e:
|
||||
self._log_debug("Compute MEM usage failed", e)
|
||||
return None
|
||||
|
||||
# Hardcode `time_since_update` to 1 as podman docs don't specify the rate calculated procedure
|
||||
return {"rx": rx, "tx": tx, "time_since_update": 1}
|
||||
|
||||
def _get_io_stats(self, stats):
|
||||
"""Return the container IO usage using the Docker API (v1.0 or higher).
|
||||
|
||||
Output: a dict {'time_since_update': 3000, 'ior': 10, 'iow': 65}.
|
||||
with:
|
||||
time_since_update: number of seconds elapsed between the latest grab
|
||||
ior: Number of bytes read
|
||||
iow: Number of bytes written
|
||||
"""
|
||||
if "BlockIO" not in stats or "/" not in stats["BlockIO"]:
|
||||
self._log_debug("Missing BlockIO usage fields")
|
||||
return None
|
||||
|
||||
block_io_str = stats["BlockIO"]
|
||||
ior_str, iow_str = block_io_str.split("/")
|
||||
|
||||
try:
|
||||
ior = string_value_to_float(ior_str)
|
||||
iow = string_value_to_float(iow_str)
|
||||
except ValueError as e:
|
||||
self._log_debug("Compute BlockIO usage failed", e)
|
||||
return None
|
||||
|
||||
# Hardcode `time_since_update` to 1 as podman docs don't specify the rate calculated procedure
|
||||
return {"ior": ior, "iow": iow, "time_since_update": 1}
|
||||
|
||||
|
||||
class PodmanContainersExtension:
|
||||
"""Glances' Containers Plugin's Docker Extension unit"""
|
||||
|
||||
CONTAINER_ACTIVE_STATUS = ['running', 'paused']
|
||||
|
||||
def __init__(self, podman_sock):
|
||||
if import_podman_error_tag:
|
||||
raise Exception("Missing libs required to run Podman Extension (Containers)")
|
||||
|
||||
self.ext_name = "containers (Podman)"
|
||||
|
||||
self.client = None
|
||||
self.podman_sock = podman_sock
|
||||
self.pods_stats_fetcher = None
|
||||
self.container_stats_fetchers = {}
|
||||
|
||||
# Cache version details as the version call is costly (in terms of time)
|
||||
self._version = {}
|
||||
self._last_version_update = 0
|
||||
|
||||
self.connect()
|
||||
|
||||
def connect(self):
|
||||
"""Connect to Podman."""
|
||||
try:
|
||||
self.client = PodmanClient(base_url=self.podman_sock)
|
||||
except Exception as e:
|
||||
logger.error("{} plugin - Can't connect to Podman ({})".format(self.ext_name, e))
|
||||
|
||||
def update_version(self):
|
||||
try:
|
||||
self._version = self.client.version()
|
||||
self._last_version_update = time.time()
|
||||
except Exception as e:
|
||||
logger.error("{} plugin - Can't get Podman version ({})".format(self.ext_name, e))
|
||||
|
||||
def stop(self):
|
||||
# Stop all streaming threads
|
||||
for t in itervalues(self.container_stats_fetchers):
|
||||
t.stop()
|
||||
|
||||
if self.pods_stats_fetcher:
|
||||
self.pods_stats_fetcher.stop()
|
||||
|
||||
def update(self, all_tag):
|
||||
"""Update Podman stats using the input method."""
|
||||
|
||||
curr_time = time.time()
|
||||
if curr_time - self._last_version_update > 300: # 300 seconds
|
||||
self.update_version()
|
||||
|
||||
# Update current containers list
|
||||
try:
|
||||
# Issue #1152: Podman module doesn't export details about stopped containers
|
||||
# The Containers/all key of the configuration file should be set to True
|
||||
containers = self.client.containers.list(all=all_tag)
|
||||
if not self.pods_stats_fetcher:
|
||||
self.pods_stats_fetcher = PodmanPodStatsFetcher(self.client.pods)
|
||||
except Exception as e:
|
||||
logger.error("{} plugin - Can't get containers list ({})".format(self.ext_name, e))
|
||||
return self._version, []
|
||||
|
||||
# Start new thread for new container
|
||||
for container in containers:
|
||||
if container.id not in self.container_stats_fetchers:
|
||||
# StatsFetcher did not exist in the internal dict
|
||||
# Create it, add it to the internal dict
|
||||
logger.debug("{} plugin - Create thread for container {}".format(self.ext_name, container.id[:12]))
|
||||
self.container_stats_fetchers[container.id] = PodmanContainerStatsFetcher(container)
|
||||
|
||||
# Stop threads for non-existing containers
|
||||
absent_containers = set(iterkeys(self.container_stats_fetchers)) - set(c.id for c in containers)
|
||||
for container_id in absent_containers:
|
||||
# Stop the StatsFetcher
|
||||
logger.debug("{} plugin - Stop thread for old container {}".format(self.ext_name, container_id[:12]))
|
||||
self.container_stats_fetchers[container_id].stop()
|
||||
# Delete the StatsFetcher from the dict
|
||||
del self.container_stats_fetchers[container_id]
|
||||
|
||||
# Get stats for all containers
|
||||
container_stats = [self.generate_stats(container) for container in containers]
|
||||
|
||||
pod_stats = self.pods_stats_fetcher.activity_stats
|
||||
for stats in container_stats:
|
||||
if stats["Id"][:12] in pod_stats:
|
||||
stats["pod_name"] = pod_stats[stats["Id"][:12]]["name"]
|
||||
stats["pod_id"] = pod_stats[stats["Id"][:12]]["pod_id"]
|
||||
|
||||
return self._version, container_stats
|
||||
|
||||
@property
|
||||
def key(self):
|
||||
"""Return the key of the list."""
|
||||
return 'name'
|
||||
|
||||
def generate_stats(self, container):
|
||||
# Init the stats for the current container
|
||||
stats = {
|
||||
'key': self.key,
|
||||
# Export name
|
||||
'name': nativestr(container.name),
|
||||
# Container Id
|
||||
'Id': container.id,
|
||||
# Container Image
|
||||
'Image': str(container.image.tags),
|
||||
# Container Status (from attrs)
|
||||
'Status': container.attrs['State'],
|
||||
'Created': container.attrs['Created'],
|
||||
'Command': container.attrs.get('Command') or [],
|
||||
}
|
||||
|
||||
if stats['Status'] in self.CONTAINER_ACTIVE_STATUS:
|
||||
stats['StartedAt'] = datetime.fromtimestamp(container.attrs['StartedAt'])
|
||||
stats_fetcher = self.container_stats_fetchers[container.id]
|
||||
activity_stats = stats_fetcher.activity_stats
|
||||
stats.update(activity_stats)
|
||||
|
||||
# Additional fields
|
||||
stats['cpu_percent'] = stats["cpu"]['total']
|
||||
stats['memory_usage'] = stats["memory"].get('usage')
|
||||
if stats['memory'].get('cache') is not None:
|
||||
stats['memory_usage'] -= stats['memory']['cache']
|
||||
stats['io_r'] = stats['io'].get('ior')
|
||||
stats['io_w'] = stats['io'].get('iow')
|
||||
stats['network_rx'] = stats['network'].get('rx')
|
||||
stats['network_tx'] = stats['network'].get('tx')
|
||||
stats['Uptime'] = pretty_date(stats['StartedAt'])
|
||||
else:
|
||||
stats['io'] = {}
|
||||
stats['cpu'] = {}
|
||||
stats['memory'] = {}
|
||||
stats['network'] = {}
|
||||
stats['io_r'] = None
|
||||
stats['io_w'] = None
|
||||
stats['cpu_percent'] = None
|
||||
stats['memory_percent'] = None
|
||||
stats['network_rx'] = None
|
||||
stats['network_tx'] = None
|
||||
stats['Uptime'] = None
|
||||
|
||||
return stats
|
76
glances/plugins/containers/stats_streamer.py
Normal file
76
glances/plugins/containers/stats_streamer.py
Normal file
@ -0,0 +1,76 @@
|
||||
import threading
|
||||
import time
|
||||
|
||||
from glances.logger import logger
|
||||
|
||||
|
||||
class StatsStreamer:
|
||||
"""
|
||||
Utility class to stream an iterable using a background / daemon Thread
|
||||
|
||||
Use `StatsStreamer.stats` to access the latest streamed results
|
||||
"""
|
||||
|
||||
def __init__(self, iterable, initial_stream_value=None):
|
||||
"""
|
||||
iterable: an Iterable instance that needs to be streamed
|
||||
"""
|
||||
self._iterable = iterable
|
||||
# Iterable results are stored here
|
||||
self._raw_result = initial_stream_value
|
||||
# Use a Thread to stream iterable (daemon=True to automatically kill thread when main process dies)
|
||||
self._thread = threading.Thread(target=self._stream_results, daemon=True)
|
||||
# Event needed to stop the thread manually
|
||||
self._stopper = threading.Event()
|
||||
# Lock to avoid the daemon thread updating stats when main thread reads the stats
|
||||
self.result_lock = threading.Lock()
|
||||
# Last result streamed time (initial val 0)
|
||||
self._last_update_time = 0
|
||||
|
||||
self._thread.start()
|
||||
|
||||
def stop(self):
|
||||
"""Stop the thread."""
|
||||
self._stopper.set()
|
||||
|
||||
def stopped(self):
|
||||
"""Return True is the thread is stopped."""
|
||||
return self._stopper.is_set()
|
||||
|
||||
def _stream_results(self):
|
||||
"""Grab the stats.
|
||||
|
||||
Infinite loop, should be stopped by calling the stop() method
|
||||
"""
|
||||
try:
|
||||
for res in self._iterable:
|
||||
self._pre_update_hook()
|
||||
self._raw_result = res
|
||||
self._post_update_hook()
|
||||
|
||||
time.sleep(0.1)
|
||||
if self.stopped():
|
||||
break
|
||||
|
||||
except Exception as e:
|
||||
logger.debug("docker plugin - Exception thrown during run ({})".format(e))
|
||||
self.stop()
|
||||
|
||||
def _pre_update_hook(self):
|
||||
"""Hook that runs before worker thread updates the raw_stats"""
|
||||
self.result_lock.acquire()
|
||||
|
||||
def _post_update_hook(self):
|
||||
"""Hook that runs after worker thread updates the raw_stats"""
|
||||
self._last_update_time = time.time()
|
||||
self.result_lock.release()
|
||||
|
||||
@property
|
||||
def stats(self):
|
||||
"""Raw Stats getter."""
|
||||
return self._raw_result
|
||||
|
||||
@property
|
||||
def last_update_time(self):
|
||||
"""Raw Stats getter."""
|
||||
return self._last_update_time
|
428
glances/plugins/glances_containers.py
Normal file
428
glances/plugins/glances_containers.py
Normal file
@ -0,0 +1,428 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# This file is part of Glances.
|
||||
#
|
||||
# SPDX-FileCopyrightText: 2022 Nicolas Hennion <nicolas@nicolargo.com>
|
||||
#
|
||||
# SPDX-License-Identifier: LGPL-3.0-only
|
||||
#
|
||||
|
||||
"""Containers plugin."""
|
||||
|
||||
import os
|
||||
from copy import deepcopy
|
||||
|
||||
from glances.logger import logger
|
||||
from glances.plugins.containers.glances_docker import DockerContainersExtension, import_docker_error_tag
|
||||
from glances.plugins.containers.glances_podman import PodmanContainersExtension, import_podman_error_tag
|
||||
from glances.plugins.glances_plugin import GlancesPlugin
|
||||
from glances.processes import glances_processes
|
||||
from glances.processes import sort_stats as sort_stats_processes
|
||||
|
||||
# Define the items history list (list of items to add to history)
|
||||
# TODO: For the moment limited to the CPU. Had to change the graph exports
|
||||
# method to display one graph per container.
|
||||
# items_history_list = [{'name': 'cpu_percent',
|
||||
# 'description': 'Container CPU consumption in %',
|
||||
# 'y_unit': '%'},
|
||||
# {'name': 'memory_usage',
|
||||
# 'description': 'Container memory usage in bytes',
|
||||
# 'y_unit': 'B'},
|
||||
# {'name': 'network_rx',
|
||||
# 'description': 'Container network RX bitrate in bits per second',
|
||||
# 'y_unit': 'bps'},
|
||||
# {'name': 'network_tx',
|
||||
# 'description': 'Container network TX bitrate in bits per second',
|
||||
# 'y_unit': 'bps'},
|
||||
# {'name': 'io_r',
|
||||
# 'description': 'Container IO bytes read per second',
|
||||
# 'y_unit': 'Bps'},
|
||||
# {'name': 'io_w',
|
||||
# 'description': 'Container IO bytes write per second',
|
||||
# 'y_unit': 'Bps'}]
|
||||
items_history_list = [{'name': 'cpu_percent', 'description': 'Container CPU consumption in %', 'y_unit': '%'}]
|
||||
|
||||
# List of key to remove before export
|
||||
export_exclude_list = ['cpu', 'io', 'memory', 'network']
|
||||
|
||||
# Sort dictionary for human
|
||||
sort_for_human = {
|
||||
'io_counters': 'disk IO',
|
||||
'cpu_percent': 'CPU consumption',
|
||||
'memory_usage': 'memory consumption',
|
||||
'cpu_times': 'uptime',
|
||||
'name': 'container name',
|
||||
None: 'None',
|
||||
}
|
||||
|
||||
|
||||
class Plugin(GlancesPlugin):
|
||||
"""Glances Docker plugin.
|
||||
|
||||
stats is a dict: {'version': {...}, 'containers': [{}, {}]}
|
||||
"""
|
||||
|
||||
def __init__(self, args=None, config=None):
|
||||
"""Init the plugin."""
|
||||
super(Plugin, self).__init__(args=args, config=config, items_history_list=items_history_list)
|
||||
|
||||
# The plugin can be disabled using: args.disable_docker
|
||||
self.args = args
|
||||
|
||||
# Default config keys
|
||||
self.config = config
|
||||
|
||||
# We want to display the stat in the curse interface
|
||||
self.display_curse = True
|
||||
|
||||
# Init the Docker API
|
||||
self.docker_extension = DockerContainersExtension() if not import_docker_error_tag else None
|
||||
|
||||
# Init the Podman API
|
||||
if import_podman_error_tag:
|
||||
self.podman_client = None
|
||||
else:
|
||||
self.podman_client = PodmanContainersExtension(podman_sock=self._podman_sock())
|
||||
|
||||
# Sort key
|
||||
self.sort_key = None
|
||||
|
||||
# Force a first update because we need two update to have the first stat
|
||||
self.update()
|
||||
self.refresh_timer.set(0)
|
||||
|
||||
def _podman_sock(self):
|
||||
"""Return the podman sock.
|
||||
Could be desfined in the [docker] section thanks to the podman_sock option.
|
||||
Default value: unix:///run/user/1000/podman/podman.sock
|
||||
"""
|
||||
conf_podman_sock = self.get_conf_value('podman_sock')
|
||||
if len(conf_podman_sock) == 0:
|
||||
return "unix:///run/user/1000/podman/podman.sock"
|
||||
else:
|
||||
return conf_podman_sock[0]
|
||||
|
||||
def exit(self):
|
||||
"""Overwrite the exit method to close threads."""
|
||||
if self.docker_extension:
|
||||
self.docker_extension.stop()
|
||||
# Call the father class
|
||||
super(Plugin, self).exit()
|
||||
|
||||
def get_key(self):
|
||||
"""Return the key of the list."""
|
||||
return 'name'
|
||||
|
||||
def get_export(self):
|
||||
"""Overwrite the default export method.
|
||||
|
||||
- Only exports containers
|
||||
- The key is the first container name
|
||||
"""
|
||||
try:
|
||||
ret = deepcopy(self.stats['containers'])
|
||||
except KeyError as e:
|
||||
logger.debug("docker plugin - Docker export error {}".format(e))
|
||||
ret = []
|
||||
|
||||
# Remove fields uses to compute rate
|
||||
for container in ret:
|
||||
for i in export_exclude_list:
|
||||
container.pop(i)
|
||||
|
||||
return ret
|
||||
|
||||
def _all_tag(self):
|
||||
"""Return the all tag of the Glances/Docker configuration file.
|
||||
|
||||
# By default, Glances only display running containers
|
||||
# Set the following key to True to display all containers
|
||||
all=True
|
||||
"""
|
||||
all_tag = self.get_conf_value('all')
|
||||
if len(all_tag) == 0:
|
||||
return False
|
||||
else:
|
||||
return all_tag[0].lower() == 'true'
|
||||
|
||||
@GlancesPlugin._check_decorator
|
||||
@GlancesPlugin._log_result_decorator
|
||||
def update(self):
|
||||
"""Update Docker and podman stats using the input method."""
|
||||
# Connection should be ok
|
||||
if self.docker_extension is None and self.podman_client is None:
|
||||
return self.get_init_value()
|
||||
|
||||
if self.input_method == 'local':
|
||||
# Update stats
|
||||
stats_docker = self.update_docker() if self.docker_extension else {}
|
||||
stats_podman = self.update_podman() if self.podman_client else {}
|
||||
stats = {
|
||||
'version': stats_docker.get('version', {}),
|
||||
'version_podman': stats_podman.get('version', {}),
|
||||
'containers': stats_docker.get('containers', []) + stats_podman.get('containers', []),
|
||||
}
|
||||
elif self.input_method == 'snmp':
|
||||
# Update stats using SNMP
|
||||
# Not available
|
||||
pass
|
||||
|
||||
# Sort and update the stats
|
||||
# @TODO: Have a look because sort did not work for the moment (need memory stats ?)
|
||||
self.sort_key, self.stats = sort_docker_stats(stats)
|
||||
|
||||
return self.stats
|
||||
|
||||
def update_docker(self):
|
||||
"""Update Docker stats using the input method."""
|
||||
version, containers = self.docker_extension.update(all_tag=self._all_tag())
|
||||
for container in containers:
|
||||
container["engine"] = 'docker'
|
||||
return {"version": version, "containers": containers}
|
||||
|
||||
def update_podman(self):
|
||||
"""Update Podman stats."""
|
||||
version, containers = self.podman_client.update(all_tag=self._all_tag())
|
||||
for container in containers:
|
||||
container["engine"] = 'podman'
|
||||
return {"version": version, "containers": containers}
|
||||
|
||||
def get_user_ticks(self):
|
||||
"""Return the user ticks by reading the environment variable."""
|
||||
return os.sysconf(os.sysconf_names['SC_CLK_TCK'])
|
||||
|
||||
def get_stats_action(self):
|
||||
"""Return stats for the action.
|
||||
|
||||
Docker will return self.stats['containers']
|
||||
"""
|
||||
return self.stats['containers']
|
||||
|
||||
def update_views(self):
|
||||
"""Update stats views."""
|
||||
# Call the father's method
|
||||
super(Plugin, self).update_views()
|
||||
|
||||
if 'containers' not in self.stats:
|
||||
return False
|
||||
|
||||
# Add specifics information
|
||||
# Alert
|
||||
for i in self.stats['containers']:
|
||||
# Init the views for the current container (key = container name)
|
||||
self.views[i[self.get_key()]] = {'cpu': {}, 'mem': {}}
|
||||
# CPU alert
|
||||
if 'cpu' in i and 'total' in i['cpu']:
|
||||
# Looking for specific CPU container threshold in the conf file
|
||||
alert = self.get_alert(i['cpu']['total'], header=i['name'] + '_cpu', action_key=i['name'])
|
||||
if alert == 'DEFAULT':
|
||||
# Not found ? Get back to default CPU threshold value
|
||||
alert = self.get_alert(i['cpu']['total'], header='cpu')
|
||||
self.views[i[self.get_key()]]['cpu']['decoration'] = alert
|
||||
# MEM alert
|
||||
if 'memory' in i and 'usage' in i['memory']:
|
||||
# Looking for specific MEM container threshold in the conf file
|
||||
alert = self.get_alert(
|
||||
i['memory']['usage'], maximum=i['memory']['limit'], header=i['name'] + '_mem', action_key=i['name']
|
||||
)
|
||||
if alert == 'DEFAULT':
|
||||
# Not found ? Get back to default MEM threshold value
|
||||
alert = self.get_alert(i['memory']['usage'], maximum=i['memory']['limit'], header='mem')
|
||||
self.views[i[self.get_key()]]['mem']['decoration'] = alert
|
||||
|
||||
return True
|
||||
|
||||
def msg_curse(self, args=None, max_width=None):
|
||||
"""Return the dict to display in the curse interface."""
|
||||
# Init the return message
|
||||
ret = []
|
||||
|
||||
# Only process if stats exist (and non null) and display plugin enable...
|
||||
if not self.stats or 'containers' not in self.stats or len(self.stats['containers']) == 0 or self.is_disabled():
|
||||
return ret
|
||||
|
||||
show_pod_name = False
|
||||
if any(ct.get("pod_name") for ct in self.stats["containers"]):
|
||||
show_pod_name = True
|
||||
|
||||
show_engine_name = False
|
||||
if len(set(ct["engine"] for ct in self.stats["containers"])) > 1:
|
||||
show_engine_name = True
|
||||
|
||||
# Build the string message
|
||||
# Title
|
||||
msg = '{}'.format('CONTAINERS')
|
||||
ret.append(self.curse_add_line(msg, "TITLE"))
|
||||
msg = ' {}'.format(len(self.stats['containers']))
|
||||
ret.append(self.curse_add_line(msg))
|
||||
msg = ' sorted by {}'.format(sort_for_human[self.sort_key])
|
||||
ret.append(self.curse_add_line(msg))
|
||||
# msg = ' (served by Docker {})'.format(self.stats['version']["Version"])
|
||||
# ret.append(self.curse_add_line(msg))
|
||||
ret.append(self.curse_new_line())
|
||||
# Header
|
||||
ret.append(self.curse_new_line())
|
||||
# Get the maximum containers name
|
||||
# Max size is configurable. See feature request #1723.
|
||||
name_max_width = min(
|
||||
self.config.get_int_value('containers', 'max_name_size', default=20) if self.config is not None else 20,
|
||||
len(max(self.stats['containers'], key=lambda x: len(x['name']))['name']),
|
||||
)
|
||||
|
||||
if show_engine_name:
|
||||
msg = ' {:{width}}'.format('Engine', width=6)
|
||||
ret.append(self.curse_add_line(msg))
|
||||
if show_pod_name:
|
||||
msg = ' {:{width}}'.format('Pod', width=12)
|
||||
ret.append(self.curse_add_line(msg))
|
||||
msg = ' {:{width}}'.format('Name', width=name_max_width)
|
||||
ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'name' else 'DEFAULT'))
|
||||
msg = '{:>10}'.format('Status')
|
||||
ret.append(self.curse_add_line(msg))
|
||||
msg = '{:>10}'.format('Uptime')
|
||||
ret.append(self.curse_add_line(msg))
|
||||
msg = '{:>6}'.format('CPU%')
|
||||
ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'cpu_percent' else 'DEFAULT'))
|
||||
msg = '{:>7}'.format('MEM')
|
||||
ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'memory_usage' else 'DEFAULT'))
|
||||
msg = '/{:<7}'.format('MAX')
|
||||
ret.append(self.curse_add_line(msg))
|
||||
msg = '{:>7}'.format('IOR/s')
|
||||
ret.append(self.curse_add_line(msg))
|
||||
msg = ' {:<7}'.format('IOW/s')
|
||||
ret.append(self.curse_add_line(msg))
|
||||
msg = '{:>7}'.format('Rx/s')
|
||||
ret.append(self.curse_add_line(msg))
|
||||
msg = ' {:<7}'.format('Tx/s')
|
||||
ret.append(self.curse_add_line(msg))
|
||||
msg = ' {:8}'.format('Command')
|
||||
ret.append(self.curse_add_line(msg))
|
||||
|
||||
# Data
|
||||
for container in self.stats['containers']:
|
||||
ret.append(self.curse_new_line())
|
||||
if show_engine_name:
|
||||
ret.append(self.curse_add_line(' {:{width}}'.format(container["engine"], width=6)))
|
||||
if show_pod_name:
|
||||
ret.append(self.curse_add_line(' {:{width}}'.format(container.get("pod_id", " - "), width=12)))
|
||||
# Name
|
||||
ret.append(self.curse_add_line(self._msg_name(container=container, max_width=name_max_width)))
|
||||
# Status
|
||||
status = self.container_alert(container['Status'])
|
||||
msg = '{:>10}'.format(container['Status'][0:10])
|
||||
ret.append(self.curse_add_line(msg, status))
|
||||
# Uptime
|
||||
if container['Uptime']:
|
||||
msg = '{:>10}'.format(container['Uptime'])
|
||||
else:
|
||||
msg = '{:>10}'.format('_')
|
||||
ret.append(self.curse_add_line(msg, status))
|
||||
# CPU
|
||||
try:
|
||||
msg = '{:>6.1f}'.format(container['cpu']['total'])
|
||||
except KeyError:
|
||||
msg = '{:>6}'.format('_')
|
||||
ret.append(self.curse_add_line(msg, self.get_views(item=container['name'], key='cpu', option='decoration')))
|
||||
# MEM
|
||||
try:
|
||||
msg = '{:>7}'.format(self.auto_unit(container['memory']['usage']))
|
||||
except KeyError:
|
||||
msg = '{:>7}'.format('_')
|
||||
ret.append(self.curse_add_line(msg, self.get_views(item=container['name'], key='mem', option='decoration')))
|
||||
try:
|
||||
msg = '/{:<7}'.format(self.auto_unit(container['memory']['limit']))
|
||||
except KeyError:
|
||||
msg = '/{:<7}'.format('_')
|
||||
ret.append(self.curse_add_line(msg))
|
||||
# IO R/W
|
||||
unit = 'B'
|
||||
try:
|
||||
value = self.auto_unit(int(container['io']['ior'] // container['io']['time_since_update'])) + unit
|
||||
msg = '{:>7}'.format(value)
|
||||
except KeyError:
|
||||
msg = '{:>7}'.format('_')
|
||||
ret.append(self.curse_add_line(msg))
|
||||
try:
|
||||
value = self.auto_unit(int(container['io']['iow'] // container['io']['time_since_update'])) + unit
|
||||
msg = ' {:<7}'.format(value)
|
||||
except KeyError:
|
||||
msg = ' {:<7}'.format('_')
|
||||
ret.append(self.curse_add_line(msg))
|
||||
# NET RX/TX
|
||||
if args.byte:
|
||||
# Bytes per second (for dummy)
|
||||
to_bit = 1
|
||||
unit = ''
|
||||
else:
|
||||
# Bits per second (for real network administrator | Default)
|
||||
to_bit = 8
|
||||
unit = 'b'
|
||||
try:
|
||||
value = (
|
||||
self.auto_unit(
|
||||
int(container['network']['rx'] // container['network']['time_since_update'] * to_bit)
|
||||
)
|
||||
+ unit
|
||||
)
|
||||
msg = '{:>7}'.format(value)
|
||||
except KeyError:
|
||||
msg = '{:>7}'.format('_')
|
||||
ret.append(self.curse_add_line(msg))
|
||||
try:
|
||||
value = (
|
||||
self.auto_unit(
|
||||
int(container['network']['tx'] // container['network']['time_since_update'] * to_bit)
|
||||
)
|
||||
+ unit
|
||||
)
|
||||
msg = ' {:<7}'.format(value)
|
||||
except KeyError:
|
||||
msg = ' {:<7}'.format('_')
|
||||
ret.append(self.curse_add_line(msg))
|
||||
# Command
|
||||
if container['Command'] is not None:
|
||||
msg = ' {}'.format(' '.join(container['Command']))
|
||||
else:
|
||||
msg = ' {}'.format('_')
|
||||
ret.append(self.curse_add_line(msg, splittable=True))
|
||||
|
||||
return ret
|
||||
|
||||
def _msg_name(self, container, max_width):
|
||||
"""Build the container name."""
|
||||
name = container['name'][:max_width]
|
||||
return ' {:{width}}'.format(name, width=max_width)
|
||||
|
||||
def container_alert(self, status):
|
||||
"""Analyse the container status."""
|
||||
if status == 'running':
|
||||
return 'OK'
|
||||
elif status == 'exited':
|
||||
return 'WARNING'
|
||||
elif status == 'dead':
|
||||
return 'CRITICAL'
|
||||
else:
|
||||
return 'CAREFUL'
|
||||
|
||||
|
||||
def sort_docker_stats(stats):
|
||||
# Sort Docker stats using the same function than processes
|
||||
sort_by = glances_processes.sort_key
|
||||
sort_by_secondary = 'memory_usage'
|
||||
if sort_by == 'memory_percent':
|
||||
sort_by = 'memory_usage'
|
||||
sort_by_secondary = 'cpu_percent'
|
||||
elif sort_by in ['username', 'io_counters', 'cpu_times']:
|
||||
sort_by = 'cpu_percent'
|
||||
|
||||
# Sort docker stats
|
||||
sort_stats_processes(
|
||||
stats['containers'],
|
||||
sorted_by=sort_by,
|
||||
sorted_by_secondary=sort_by_secondary,
|
||||
# Reverse for all but name
|
||||
reverse=glances_processes.sort_key != 'name',
|
||||
)
|
||||
|
||||
# Return the main sort key and the sorted stats
|
||||
return sort_by, stats
|
@ -1,771 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# This file is part of Glances.
|
||||
#
|
||||
# SPDX-FileCopyrightText: 2022 Nicolas Hennion <nicolas@nicolargo.com>
|
||||
#
|
||||
# SPDX-License-Identifier: LGPL-3.0-only
|
||||
#
|
||||
|
||||
"""Docker plugin."""
|
||||
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
from copy import deepcopy
|
||||
|
||||
import requests.exceptions
|
||||
|
||||
from glances.compat import iterkeys, itervalues, nativestr, pretty_date
|
||||
from glances.logger import logger
|
||||
from glances.plugins.glances_plugin import GlancesPlugin
|
||||
from glances.processes import sort_stats as sort_stats_processes, glances_processes
|
||||
from glances.timer import getTimeSinceLastUpdate
|
||||
|
||||
# Docker-py library (optional and Linux-only)
|
||||
# https://github.com/docker/docker-py
|
||||
try:
|
||||
import docker
|
||||
from dateutil import parser, tz
|
||||
except Exception as e:
|
||||
import_error_tag = True
|
||||
# Display debug message if import KeyError
|
||||
logger.warning("Error loading Docker deps Lib. Docker plugin is disabled ({})".format(e))
|
||||
else:
|
||||
import_error_tag = False
|
||||
|
||||
# Define the items history list (list of items to add to history)
|
||||
# TODO: For the moment limited to the CPU. Had to change the graph exports
|
||||
# method to display one graph per container.
|
||||
# items_history_list = [{'name': 'cpu_percent',
|
||||
# 'description': 'Container CPU consumption in %',
|
||||
# 'y_unit': '%'},
|
||||
# {'name': 'memory_usage',
|
||||
# 'description': 'Container memory usage in bytes',
|
||||
# 'y_unit': 'B'},
|
||||
# {'name': 'network_rx',
|
||||
# 'description': 'Container network RX bitrate in bits per second',
|
||||
# 'y_unit': 'bps'},
|
||||
# {'name': 'network_tx',
|
||||
# 'description': 'Container network TX bitrate in bits per second',
|
||||
# 'y_unit': 'bps'},
|
||||
# {'name': 'io_r',
|
||||
# 'description': 'Container IO bytes read per second',
|
||||
# 'y_unit': 'Bps'},
|
||||
# {'name': 'io_w',
|
||||
# 'description': 'Container IO bytes write per second',
|
||||
# 'y_unit': 'Bps'}]
|
||||
items_history_list = [{'name': 'cpu_percent', 'description': 'Container CPU consumption in %', 'y_unit': '%'}]
|
||||
|
||||
# List of key to remove before export
|
||||
export_exclude_list = ['cpu', 'io', 'memory', 'network']
|
||||
|
||||
# Sort dictionary for human
|
||||
sort_for_human = {
|
||||
'io_counters': 'disk IO',
|
||||
'cpu_percent': 'CPU consumption',
|
||||
'memory_usage': 'memory consumption',
|
||||
'cpu_times': 'uptime',
|
||||
'name': 'container name',
|
||||
None: 'None',
|
||||
}
|
||||
|
||||
|
||||
class Plugin(GlancesPlugin):
|
||||
"""Glances Docker plugin.
|
||||
|
||||
stats is a dict: {'version': {...}, 'containers': [{}, {}]}
|
||||
"""
|
||||
|
||||
def __init__(self, args=None, config=None):
|
||||
"""Init the plugin."""
|
||||
super(Plugin, self).__init__(args=args, config=config, items_history_list=items_history_list)
|
||||
|
||||
# The plugin can be disabled using: args.disable_docker
|
||||
self.args = args
|
||||
|
||||
# Default config keys
|
||||
self.config = config
|
||||
|
||||
# We want to display the stat in the curse interface
|
||||
self.display_curse = True
|
||||
|
||||
# Init the Docker API
|
||||
self.docker_client = self.connect()
|
||||
|
||||
# Dict of thread (to grab stats asynchronously, one thread is created per container)
|
||||
# key: Container Id
|
||||
# value: instance of ThreadDockerGrabber
|
||||
self.thread_list = {}
|
||||
|
||||
# Dict of Network stats (Storing previous network stats to compute Rx/s and Tx/s)
|
||||
# key: Container Id
|
||||
# value: network stats dict
|
||||
self.network_old = {}
|
||||
|
||||
# Dict of Disk IO stats (Storing previous disk_io stats to compute Rx/s and Tx/s)
|
||||
# key: Container Id
|
||||
# value: network stats dict
|
||||
self.io_old = {}
|
||||
|
||||
# Sort key
|
||||
self.sort_key = None
|
||||
|
||||
# Force a first update because we need two update to have the first stat
|
||||
self.update()
|
||||
self.refresh_timer.set(0)
|
||||
|
||||
def exit(self):
|
||||
"""Overwrite the exit method to close threads."""
|
||||
for t in itervalues(self.thread_list):
|
||||
t.stop()
|
||||
# Call the father class
|
||||
super(Plugin, self).exit()
|
||||
|
||||
def get_key(self):
|
||||
"""Return the key of the list."""
|
||||
return 'name'
|
||||
|
||||
def get_export(self):
|
||||
"""Overwrite the default export method.
|
||||
|
||||
- Only exports containers
|
||||
- The key is the first container name
|
||||
"""
|
||||
try:
|
||||
ret = deepcopy(self.stats['containers'])
|
||||
except KeyError as e:
|
||||
logger.debug("docker plugin - Docker export error {}".format(e))
|
||||
ret = []
|
||||
|
||||
# Remove fields uses to compute rate
|
||||
for container in ret:
|
||||
for i in export_exclude_list:
|
||||
container.pop(i)
|
||||
|
||||
return ret
|
||||
|
||||
def connect(self):
|
||||
"""Connect to the Docker server."""
|
||||
try:
|
||||
# If the following line replace the next one, the issue #1878
|
||||
# is reproduced (Docker containers information missing with Docker 20.10.x)
|
||||
# So, for the moment disable the timeout option
|
||||
ret = docker.from_env()
|
||||
except Exception as e:
|
||||
logger.error("docker plugin - Can not connect to Docker ({})".format(e))
|
||||
ret = None
|
||||
|
||||
return ret
|
||||
|
||||
def _all_tag(self):
|
||||
"""Return the all tag of the Glances/Docker configuration file.
|
||||
|
||||
# By default, Glances only display running containers
|
||||
# Set the following key to True to display all containers
|
||||
all=True
|
||||
"""
|
||||
all_tag = self.get_conf_value('all')
|
||||
if len(all_tag) == 0:
|
||||
return False
|
||||
else:
|
||||
return all_tag[0].lower() == 'true'
|
||||
|
||||
@GlancesPlugin._check_decorator
|
||||
@GlancesPlugin._log_result_decorator
|
||||
def update(self):
|
||||
"""Update Docker stats using the input method."""
|
||||
# Init new stats
|
||||
stats = self.get_init_value()
|
||||
|
||||
# The Docker-py lib is mandatory and connection should be ok
|
||||
if import_error_tag or self.docker_client is None:
|
||||
return self.stats
|
||||
|
||||
if self.input_method == 'local':
|
||||
# Update stats
|
||||
|
||||
# Docker version
|
||||
# Example: {
|
||||
# "KernelVersion": "3.16.4-tinycore64",
|
||||
# "Arch": "amd64",
|
||||
# "ApiVersion": "1.15",
|
||||
# "Version": "1.3.0",
|
||||
# "GitCommit": "c78088f",
|
||||
# "Os": "linux",
|
||||
# "GoVersion": "go1.3.3"
|
||||
# }
|
||||
try:
|
||||
stats['version'] = self.docker_client.version()
|
||||
except Exception as e:
|
||||
# Correct issue#649
|
||||
logger.error("{} plugin - Cannot get Docker version ({})".format(self.plugin_name, e))
|
||||
# We may have lost connection remove version info
|
||||
if 'version' in self.stats:
|
||||
del self.stats['version']
|
||||
self.stats['containers'] = []
|
||||
return self.stats
|
||||
|
||||
# Update current containers list
|
||||
try:
|
||||
# Issue #1152: Docker module doesn't export details about stopped containers
|
||||
# The Docker/all key of the configuration file should be set to True
|
||||
containers = self.docker_client.containers.list(all=self._all_tag()) or []
|
||||
except Exception as e:
|
||||
logger.error("{} plugin - Cannot get containers list ({})".format(self.plugin_name, e))
|
||||
# We may have lost connection empty the containers list.
|
||||
self.stats['containers'] = []
|
||||
return self.stats
|
||||
|
||||
# Start new thread for new container
|
||||
for container in containers:
|
||||
if container.id not in self.thread_list:
|
||||
# Thread did not exist in the internal dict
|
||||
# Create it, add it to the internal dict and start it
|
||||
logger.debug(
|
||||
"{} plugin - Create thread for container {}".format(self.plugin_name, container.id[:12])
|
||||
)
|
||||
t = ThreadDockerGrabber(container)
|
||||
self.thread_list[container.id] = t
|
||||
t.start()
|
||||
|
||||
# Stop threads for non-existing containers
|
||||
absent_containers = set(iterkeys(self.thread_list)) - set([c.id for c in containers])
|
||||
for container_id in absent_containers:
|
||||
# Stop the thread
|
||||
logger.debug("{} plugin - Stop thread for old container {}".format(self.plugin_name, container_id[:12]))
|
||||
self.thread_list[container_id].stop()
|
||||
# Delete the item from the dict
|
||||
del self.thread_list[container_id]
|
||||
|
||||
# Get stats for all containers
|
||||
stats['containers'] = []
|
||||
for container in containers:
|
||||
# Shall we display the stats ?
|
||||
if not self.is_display(nativestr(container.name)):
|
||||
continue
|
||||
|
||||
# Init the stats for the current container
|
||||
container_stats = {}
|
||||
# The key is the container name and not the Id
|
||||
container_stats['key'] = self.get_key()
|
||||
# Export name
|
||||
container_stats['name'] = nativestr(container.name)
|
||||
# Container Id
|
||||
container_stats['Id'] = container.id
|
||||
# Container Image
|
||||
try:
|
||||
# See issue 2233
|
||||
container_stats['Image'] = container.image.tags
|
||||
except requests.exceptions.HTTPError:
|
||||
container_stats['Image'] = '-'
|
||||
|
||||
# Global stats (from attrs)
|
||||
# Container Status
|
||||
container_stats['Status'] = container.attrs['State']['Status']
|
||||
# Container Command (see #1912)
|
||||
container_stats['Command'] = []
|
||||
if container.attrs['Config'].get('Entrypoint', None):
|
||||
container_stats['Command'].extend(container.attrs['Config'].get('Entrypoint', []))
|
||||
if container.attrs['Config'].get('Cmd', None):
|
||||
container_stats['Command'].extend(container.attrs['Config'].get('Cmd', []))
|
||||
if not container_stats['Command']:
|
||||
container_stats['Command'] = None
|
||||
# Standards stats
|
||||
# See https://docs.docker.com/engine/api/v1.41/#operation/ContainerStats
|
||||
# Be aware that the API can change... (example see issue #1857)
|
||||
if container_stats['Status'] in ('running', 'paused'):
|
||||
# CPU
|
||||
container_stats['cpu'] = self.get_docker_cpu(container.id, self.thread_list[container.id].stats)
|
||||
container_stats['cpu_percent'] = container_stats['cpu'].get('total', None)
|
||||
# MEM
|
||||
container_stats['memory'] = self.get_docker_memory(
|
||||
container.id, self.thread_list[container.id].stats
|
||||
)
|
||||
container_stats['memory_usage'] = container_stats['memory'].get('usage', None)
|
||||
if container_stats['memory'].get('cache', None) is not None:
|
||||
container_stats['memory_usage'] -= container_stats['memory']['cache']
|
||||
# IO
|
||||
container_stats['io'] = self.get_docker_io(container.id, self.thread_list[container.id].stats)
|
||||
container_stats['io_r'] = container_stats['io'].get('ior', None)
|
||||
container_stats['io_w'] = container_stats['io'].get('iow', None)
|
||||
# NET
|
||||
container_stats['network'] = self.get_docker_network(
|
||||
container.id, self.thread_list[container.id].stats
|
||||
)
|
||||
container_stats['network_rx'] = container_stats['network'].get('rx', None)
|
||||
container_stats['network_tx'] = container_stats['network'].get('tx', None)
|
||||
# Uptime
|
||||
container_stats['Uptime'] = pretty_date(
|
||||
# parser.parse(container.attrs['State']['StartedAt']).replace(tzinfo=None)
|
||||
parser.parse(container.attrs['State']['StartedAt'])
|
||||
.astimezone(tz.tzlocal())
|
||||
.replace(tzinfo=None)
|
||||
)
|
||||
else:
|
||||
container_stats['cpu'] = {}
|
||||
container_stats['cpu_percent'] = None
|
||||
container_stats['memory'] = {}
|
||||
container_stats['memory_percent'] = None
|
||||
container_stats['io'] = {}
|
||||
container_stats['io_r'] = None
|
||||
container_stats['io_w'] = None
|
||||
container_stats['network'] = {}
|
||||
container_stats['network_rx'] = None
|
||||
container_stats['network_tx'] = None
|
||||
container_stats['Uptime'] = None
|
||||
# Add current container stats to the stats list
|
||||
stats['containers'].append(container_stats)
|
||||
|
||||
elif self.input_method == 'snmp':
|
||||
# Update stats using SNMP
|
||||
# Not available
|
||||
pass
|
||||
|
||||
# Sort and update the stats
|
||||
self.sort_key, self.stats = sort_docker_stats(stats)
|
||||
|
||||
return self.stats
|
||||
|
||||
def get_docker_cpu(self, container_id, all_stats):
|
||||
"""Return the container CPU usage.
|
||||
|
||||
Input: id is the full container id
|
||||
all_stats is the output of the stats method of the Docker API
|
||||
Output: a dict {'total': 1.49}
|
||||
"""
|
||||
cpu_stats = {'total': 0.0}
|
||||
|
||||
try:
|
||||
cpu = {
|
||||
'system': all_stats['cpu_stats']['system_cpu_usage'],
|
||||
'total': all_stats['cpu_stats']['cpu_usage']['total_usage'],
|
||||
}
|
||||
precpu = {
|
||||
'system': all_stats['precpu_stats']['system_cpu_usage'],
|
||||
'total': all_stats['precpu_stats']['cpu_usage']['total_usage'],
|
||||
}
|
||||
# Issue #1857
|
||||
# If either precpu_stats.online_cpus or cpu_stats.online_cpus is nil
|
||||
# then for compatibility with older daemons the length of
|
||||
# the corresponding cpu_usage.percpu_usage array should be used.
|
||||
cpu['nb_core'] = all_stats['cpu_stats'].get('online_cpus', None)
|
||||
if cpu['nb_core'] is None:
|
||||
cpu['nb_core'] = len(all_stats['cpu_stats']['cpu_usage']['percpu_usage'] or [])
|
||||
except KeyError as e:
|
||||
logger.debug("docker plugin - Cannot grab CPU usage for container {} ({})".format(container_id, e))
|
||||
logger.debug(all_stats)
|
||||
else:
|
||||
try:
|
||||
cpu_delta = cpu['total'] - precpu['total']
|
||||
system_cpu_delta = cpu['system'] - precpu['system']
|
||||
# CPU usage % = (cpu_delta / system_cpu_delta) * number_cpus * 100.0
|
||||
cpu_stats['total'] = (cpu_delta / system_cpu_delta) * cpu['nb_core'] * 100.0
|
||||
except TypeError as e:
|
||||
logger.debug("docker plugin - Cannot compute CPU usage for container {} ({})".format(container_id, e))
|
||||
logger.debug(all_stats)
|
||||
|
||||
# Return the stats
|
||||
return cpu_stats
|
||||
|
||||
def get_docker_memory(self, container_id, all_stats):
|
||||
"""Return the container MEMORY.
|
||||
|
||||
Input: id is the full container id
|
||||
all_stats is the output of the stats method of the Docker API
|
||||
Output: a dict {'rss': 1015808, 'cache': 356352, 'usage': ..., 'max_usage': ...}
|
||||
"""
|
||||
memory_stats = {}
|
||||
# Read the stats
|
||||
try:
|
||||
# Mandatory fields
|
||||
memory_stats['usage'] = all_stats['memory_stats']['usage']
|
||||
memory_stats['limit'] = all_stats['memory_stats']['limit']
|
||||
# Issue #1857
|
||||
# Some stats are not always available in ['memory_stats']['stats']
|
||||
if 'rss' in all_stats['memory_stats']['stats']:
|
||||
memory_stats['rss'] = all_stats['memory_stats']['stats']['rss']
|
||||
elif 'total_rss' in all_stats['memory_stats']['stats']:
|
||||
memory_stats['rss'] = all_stats['memory_stats']['stats']['total_rss']
|
||||
else:
|
||||
memory_stats['rss'] = None
|
||||
memory_stats['cache'] = all_stats['memory_stats']['stats'].get('cache', None)
|
||||
memory_stats['max_usage'] = all_stats['memory_stats'].get('max_usage', None)
|
||||
except (KeyError, TypeError) as e:
|
||||
# all_stats do not have MEM information
|
||||
logger.debug("docker plugin - Cannot grab MEM usage for container {} ({})".format(container_id, e))
|
||||
logger.debug(all_stats)
|
||||
# Return the stats
|
||||
return memory_stats
|
||||
|
||||
def get_docker_network(self, container_id, all_stats):
|
||||
"""Return the container network usage using the Docker API (v1.0 or higher).
|
||||
|
||||
Input: id is the full container id
|
||||
Output: a dict {'time_since_update': 3000, 'rx': 10, 'tx': 65}.
|
||||
with:
|
||||
time_since_update: number of seconds elapsed between the latest grab
|
||||
rx: Number of bytes received
|
||||
tx: Number of bytes transmitted
|
||||
"""
|
||||
# Init the returned dict
|
||||
network_new = {}
|
||||
|
||||
# Read the rx/tx stats (in bytes)
|
||||
try:
|
||||
net_stats = all_stats["networks"]
|
||||
except KeyError as e:
|
||||
# all_stats do not have NETWORK information
|
||||
logger.debug("docker plugin - Cannot grab NET usage for container {} ({})".format(container_id, e))
|
||||
logger.debug(all_stats)
|
||||
# No fallback available...
|
||||
return network_new
|
||||
|
||||
# Previous network interface stats are stored in the self.network_old variable
|
||||
# By storing time data we enable Rx/s and Tx/s calculations in the XML/RPC API, which would otherwise
|
||||
# be overly difficult work for users of the API
|
||||
try:
|
||||
network_new['cumulative_rx'] = net_stats["eth0"]["rx_bytes"]
|
||||
network_new['cumulative_tx'] = net_stats["eth0"]["tx_bytes"]
|
||||
except KeyError as e:
|
||||
# all_stats do not have INTERFACE information
|
||||
logger.debug(
|
||||
"docker plugin - Cannot grab network interface usage for container {} ({})".format(container_id, e)
|
||||
)
|
||||
logger.debug(all_stats)
|
||||
else:
|
||||
network_new['time_since_update'] = getTimeSinceLastUpdate('docker_net_{}'.format(container_id))
|
||||
if container_id in self.network_old:
|
||||
network_new['rx'] = network_new['cumulative_rx'] - self.network_old[container_id]['cumulative_rx']
|
||||
network_new['tx'] = network_new['cumulative_tx'] - self.network_old[container_id]['cumulative_tx']
|
||||
|
||||
# Save stats to compute next bitrate
|
||||
self.network_old[container_id] = network_new
|
||||
|
||||
# Return the stats
|
||||
return network_new
|
||||
|
||||
def get_docker_io(self, container_id, all_stats):
|
||||
"""Return the container IO usage using the Docker API (v1.0 or higher).
|
||||
|
||||
Input: id is the full container id
|
||||
Output: a dict {'time_since_update': 3000, 'ior': 10, 'iow': 65}.
|
||||
with:
|
||||
time_since_update: number of seconds elapsed between the latest grab
|
||||
ior: Number of bytes read
|
||||
iow: Number of bytes written
|
||||
"""
|
||||
# Init the returned dict
|
||||
io_new = {}
|
||||
|
||||
# Read the ior/iow stats (in bytes)
|
||||
try:
|
||||
io_stats = all_stats["blkio_stats"]
|
||||
except KeyError as e:
|
||||
# all_stats do not have io information
|
||||
logger.debug("docker plugin - Cannot grab block IO usage for container {} ({})".format(container_id, e))
|
||||
logger.debug(all_stats)
|
||||
# No fallback available...
|
||||
return io_new
|
||||
|
||||
# Previous io interface stats are stored in the self.io_old variable
|
||||
# By storing time data we enable IoR/s and IoW/s calculations in the
|
||||
# XML/RPC API, which would otherwise be overly difficult work
|
||||
# for users of the API
|
||||
try:
|
||||
io_service_bytes_recursive = io_stats['io_service_bytes_recursive']
|
||||
|
||||
# Read IOR and IOW value in the structure list of dict
|
||||
io_new['cumulative_ior'] = [i for i in io_service_bytes_recursive if i['op'].lower() == 'read'][0]['value']
|
||||
io_new['cumulative_iow'] = [i for i in io_service_bytes_recursive if i['op'].lower() == 'write'][0]['value']
|
||||
except (TypeError, IndexError, KeyError, AttributeError) as e:
|
||||
# all_stats do not have io information
|
||||
logger.debug("docker plugin - Cannot grab block IO usage for container {} ({})".format(container_id, e))
|
||||
else:
|
||||
io_new['time_since_update'] = getTimeSinceLastUpdate('docker_io_{}'.format(container_id))
|
||||
if container_id in self.io_old:
|
||||
io_new['ior'] = io_new['cumulative_ior'] - self.io_old[container_id]['cumulative_ior']
|
||||
io_new['iow'] = io_new['cumulative_iow'] - self.io_old[container_id]['cumulative_iow']
|
||||
|
||||
# Save stats to compute next bitrate
|
||||
self.io_old[container_id] = io_new
|
||||
|
||||
# Return the stats
|
||||
return io_new
|
||||
|
||||
def get_user_ticks(self):
|
||||
"""Return the user ticks by reading the environment variable."""
|
||||
return os.sysconf(os.sysconf_names['SC_CLK_TCK'])
|
||||
|
||||
def get_stats_action(self):
|
||||
"""Return stats for the action.
|
||||
|
||||
Docker will return self.stats['containers']
|
||||
"""
|
||||
return self.stats['containers']
|
||||
|
||||
def update_views(self):
|
||||
"""Update stats views."""
|
||||
# Call the father's method
|
||||
super(Plugin, self).update_views()
|
||||
|
||||
if 'containers' not in self.stats:
|
||||
return False
|
||||
|
||||
# Add specifics information
|
||||
# Alert
|
||||
for i in self.stats['containers']:
|
||||
# Init the views for the current container (key = container name)
|
||||
self.views[i[self.get_key()]] = {'cpu': {}, 'mem': {}}
|
||||
# CPU alert
|
||||
if 'cpu' in i and 'total' in i['cpu']:
|
||||
# Looking for specific CPU container threshold in the conf file
|
||||
alert = self.get_alert(i['cpu']['total'], header=i['name'] + '_cpu', action_key=i['name'])
|
||||
if alert == 'DEFAULT':
|
||||
# Not found ? Get back to default CPU threshold value
|
||||
alert = self.get_alert(i['cpu']['total'], header='cpu')
|
||||
self.views[i[self.get_key()]]['cpu']['decoration'] = alert
|
||||
# MEM alert
|
||||
if 'memory' in i and 'usage' in i['memory']:
|
||||
# Looking for specific MEM container threshold in the conf file
|
||||
alert = self.get_alert(
|
||||
i['memory']['usage'], maximum=i['memory']['limit'], header=i['name'] + '_mem', action_key=i['name']
|
||||
)
|
||||
if alert == 'DEFAULT':
|
||||
# Not found ? Get back to default MEM threshold value
|
||||
alert = self.get_alert(i['memory']['usage'], maximum=i['memory']['limit'], header='mem')
|
||||
self.views[i[self.get_key()]]['mem']['decoration'] = alert
|
||||
|
||||
return True
|
||||
|
||||
def msg_curse(self, args=None, max_width=None):
|
||||
"""Return the dict to display in the curse interface."""
|
||||
# Init the return message
|
||||
ret = []
|
||||
|
||||
# Only process if stats exist (and non null) and display plugin enable...
|
||||
if not self.stats or 'containers' not in self.stats or len(self.stats['containers']) == 0 or self.is_disabled():
|
||||
return ret
|
||||
|
||||
# Build the string message
|
||||
# Title
|
||||
msg = '{}'.format('CONTAINERS')
|
||||
ret.append(self.curse_add_line(msg, "TITLE"))
|
||||
msg = ' {}'.format(len(self.stats['containers']))
|
||||
ret.append(self.curse_add_line(msg))
|
||||
msg = ' sorted by {}'.format(sort_for_human[self.sort_key])
|
||||
ret.append(self.curse_add_line(msg))
|
||||
# msg = ' (served by Docker {})'.format(self.stats['version']["Version"])
|
||||
# ret.append(self.curse_add_line(msg))
|
||||
ret.append(self.curse_new_line())
|
||||
# Header
|
||||
ret.append(self.curse_new_line())
|
||||
# Get the maximum containers name
|
||||
# Max size is configurable. See feature request #1723.
|
||||
name_max_width = min(
|
||||
self.config.get_int_value('docker', 'max_name_size', default=20) if self.config is not None else 20,
|
||||
len(max(self.stats['containers'], key=lambda x: len(x['name']))['name']),
|
||||
)
|
||||
msg = ' {:{width}}'.format('Name', width=name_max_width)
|
||||
ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'name' else 'DEFAULT'))
|
||||
msg = '{:>10}'.format('Status')
|
||||
ret.append(self.curse_add_line(msg))
|
||||
msg = '{:>10}'.format('Uptime')
|
||||
ret.append(self.curse_add_line(msg))
|
||||
msg = '{:>6}'.format('CPU%')
|
||||
ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'cpu_percent' else 'DEFAULT'))
|
||||
msg = '{:>7}'.format('MEM')
|
||||
ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'memory_usage' else 'DEFAULT'))
|
||||
msg = '/{:<7}'.format('MAX')
|
||||
ret.append(self.curse_add_line(msg))
|
||||
msg = '{:>7}'.format('IOR/s')
|
||||
ret.append(self.curse_add_line(msg))
|
||||
msg = ' {:<7}'.format('IOW/s')
|
||||
ret.append(self.curse_add_line(msg))
|
||||
msg = '{:>7}'.format('Rx/s')
|
||||
ret.append(self.curse_add_line(msg))
|
||||
msg = ' {:<7}'.format('Tx/s')
|
||||
ret.append(self.curse_add_line(msg))
|
||||
msg = ' {:8}'.format('Command')
|
||||
ret.append(self.curse_add_line(msg))
|
||||
# Data
|
||||
for container in self.stats['containers']:
|
||||
ret.append(self.curse_new_line())
|
||||
# Name
|
||||
ret.append(self.curse_add_line(self._msg_name(container=container, max_width=name_max_width)))
|
||||
# Status
|
||||
status = self.container_alert(container['Status'])
|
||||
msg = '{:>10}'.format(container['Status'][0:10])
|
||||
ret.append(self.curse_add_line(msg, status))
|
||||
# Uptime
|
||||
if container['Uptime']:
|
||||
msg = '{:>10}'.format(container['Uptime'])
|
||||
else:
|
||||
msg = '{:>10}'.format('_')
|
||||
ret.append(self.curse_add_line(msg, status))
|
||||
# CPU
|
||||
try:
|
||||
msg = '{:>6.1f}'.format(container['cpu']['total'])
|
||||
except KeyError:
|
||||
msg = '{:>6}'.format('_')
|
||||
ret.append(self.curse_add_line(msg, self.get_views(item=container['name'], key='cpu', option='decoration')))
|
||||
# MEM
|
||||
try:
|
||||
msg = '{:>7}'.format(self.auto_unit(container['memory']['usage']))
|
||||
except KeyError:
|
||||
msg = '{:>7}'.format('_')
|
||||
ret.append(self.curse_add_line(msg, self.get_views(item=container['name'], key='mem', option='decoration')))
|
||||
try:
|
||||
msg = '/{:<7}'.format(self.auto_unit(container['memory']['limit']))
|
||||
except KeyError:
|
||||
msg = '/{:<7}'.format('_')
|
||||
ret.append(self.curse_add_line(msg))
|
||||
# IO R/W
|
||||
unit = 'B'
|
||||
try:
|
||||
value = self.auto_unit(int(container['io']['ior'] // container['io']['time_since_update'])) + unit
|
||||
msg = '{:>7}'.format(value)
|
||||
except KeyError:
|
||||
msg = '{:>7}'.format('_')
|
||||
ret.append(self.curse_add_line(msg))
|
||||
try:
|
||||
value = self.auto_unit(int(container['io']['iow'] // container['io']['time_since_update'])) + unit
|
||||
msg = ' {:<7}'.format(value)
|
||||
except KeyError:
|
||||
msg = ' {:<7}'.format('_')
|
||||
ret.append(self.curse_add_line(msg))
|
||||
# NET RX/TX
|
||||
if args.byte:
|
||||
# Bytes per second (for dummy)
|
||||
to_bit = 1
|
||||
unit = ''
|
||||
else:
|
||||
# Bits per second (for real network administrator | Default)
|
||||
to_bit = 8
|
||||
unit = 'b'
|
||||
try:
|
||||
value = (
|
||||
self.auto_unit(
|
||||
int(container['network']['rx'] // container['network']['time_since_update'] * to_bit)
|
||||
)
|
||||
+ unit
|
||||
)
|
||||
msg = '{:>7}'.format(value)
|
||||
except KeyError:
|
||||
msg = '{:>7}'.format('_')
|
||||
ret.append(self.curse_add_line(msg))
|
||||
try:
|
||||
value = (
|
||||
self.auto_unit(
|
||||
int(container['network']['tx'] // container['network']['time_since_update'] * to_bit)
|
||||
)
|
||||
+ unit
|
||||
)
|
||||
msg = ' {:<7}'.format(value)
|
||||
except KeyError:
|
||||
msg = ' {:<7}'.format('_')
|
||||
ret.append(self.curse_add_line(msg))
|
||||
# Command
|
||||
if container['Command'] is not None:
|
||||
msg = ' {}'.format(' '.join(container['Command']))
|
||||
else:
|
||||
msg = ' {}'.format('_')
|
||||
ret.append(self.curse_add_line(msg, splittable=True))
|
||||
|
||||
return ret
|
||||
|
||||
def _msg_name(self, container, max_width):
|
||||
"""Build the container name."""
|
||||
name = container['name'][:max_width]
|
||||
return ' {:{width}}'.format(name, width=max_width)
|
||||
|
||||
def container_alert(self, status):
|
||||
"""Analyse the container status."""
|
||||
if status == 'running':
|
||||
return 'OK'
|
||||
elif status == 'exited':
|
||||
return 'WARNING'
|
||||
elif status == 'dead':
|
||||
return 'CRITICAL'
|
||||
else:
|
||||
return 'CAREFUL'
|
||||
|
||||
|
||||
class ThreadDockerGrabber(threading.Thread):
|
||||
"""
|
||||
Specific thread to grab docker stats.
|
||||
|
||||
stats is a dict
|
||||
"""
|
||||
|
||||
def __init__(self, container):
|
||||
"""Init the class.
|
||||
|
||||
container: instance of Docker-py Container
|
||||
"""
|
||||
super(ThreadDockerGrabber, self).__init__()
|
||||
# Event needed to stop properly the thread
|
||||
self._stopper = threading.Event()
|
||||
# The docker-py return stats as a stream
|
||||
self._container = container
|
||||
self._stats_stream = container.stats(stream=True, decode=True)
|
||||
# The class return the stats as a dict
|
||||
self._stats = {}
|
||||
logger.debug("docker plugin - Create thread for container {}".format(self._container.name))
|
||||
|
||||
def run(self):
|
||||
"""Grab the stats.
|
||||
|
||||
Infinite loop, should be stopped by calling the stop() method
|
||||
"""
|
||||
try:
|
||||
for i in self._stats_stream:
|
||||
self._stats = i
|
||||
time.sleep(0.1)
|
||||
if self.stopped():
|
||||
break
|
||||
except Exception as e:
|
||||
logger.debug("docker plugin - Exception thrown during run ({})".format(e))
|
||||
self.stop()
|
||||
|
||||
@property
|
||||
def stats(self):
|
||||
"""Stats getter."""
|
||||
return self._stats
|
||||
|
||||
@stats.setter
|
||||
def stats(self, value):
|
||||
"""Stats setter."""
|
||||
self._stats = value
|
||||
|
||||
def stop(self, timeout=None):
|
||||
"""Stop the thread."""
|
||||
logger.debug("docker plugin - Close thread for container {}".format(self._container.name))
|
||||
self._stopper.set()
|
||||
|
||||
def stopped(self):
|
||||
"""Return True is the thread is stopped."""
|
||||
return self._stopper.is_set()
|
||||
|
||||
|
||||
def sort_docker_stats(stats):
|
||||
# Sort Docker stats using the same function than processes
|
||||
sort_by = glances_processes.sort_key
|
||||
sort_by_secondary = 'memory_usage'
|
||||
if sort_by == 'memory_percent':
|
||||
sort_by = 'memory_usage'
|
||||
sort_by_secondary = 'cpu_percent'
|
||||
elif sort_by in ['username', 'io_counters', 'cpu_times']:
|
||||
sort_by = 'cpu_percent'
|
||||
|
||||
# Sort docker stats
|
||||
sort_stats_processes(
|
||||
stats['containers'],
|
||||
sorted_by=sort_by,
|
||||
sorted_by_secondary=sort_by_secondary,
|
||||
# Reverse for all but name
|
||||
reverse=glances_processes.sort_key != 'name',
|
||||
)
|
||||
|
||||
# Return the main sort key and the sorted stats
|
||||
return sort_by, stats
|
@ -18,6 +18,7 @@ netifaces
|
||||
py3nvml; python_version >= "3.5"
|
||||
paho-mqtt
|
||||
pika
|
||||
podman; python_version >= "3.6"
|
||||
potsdb
|
||||
prometheus_client
|
||||
pygal
|
||||
|
4
setup.py
4
setup.py
@ -65,7 +65,7 @@ def get_install_extras_require():
|
||||
'cloud': ['requests'],
|
||||
'docker': ['docker>=2.0.0', 'python-dateutil', 'six'],
|
||||
'export': ['bernhard', 'cassandra-driver', 'couchdb', 'elasticsearch',
|
||||
'graphitesender', 'influxdb>=1.0.0', 'kafka-python', 'pymongo',
|
||||
'graphitesender', 'influxdb>=1.0.0', 'kafka-python',
|
||||
'pika', 'paho-mqtt', 'potsdb', 'prometheus_client', 'pyzmq',
|
||||
'statsd'],
|
||||
'folders': ['scandir'], # python_version<"3.5"
|
||||
@ -81,7 +81,9 @@ def get_install_extras_require():
|
||||
}
|
||||
if PY3:
|
||||
extras_require['export'].append('influxdb-client')
|
||||
extras_require['export'].append('pymongo')
|
||||
extras_require['gpu'] = ['py3nvml']
|
||||
extras_require['podman'] = ['podman']
|
||||
if sys.platform.startswith('linux'):
|
||||
extras_require['sensors'] = ['batinfo']
|
||||
|
||||
|
16
unitest.py
16
unitest.py
@ -10,6 +10,10 @@
|
||||
|
||||
"""Glances unitary tests suite."""
|
||||
|
||||
#
|
||||
# ./venv/bin/python unitest.py
|
||||
#
|
||||
|
||||
import time
|
||||
import unittest
|
||||
|
||||
@ -25,7 +29,7 @@ from glances.thresholds import GlancesThresholdCritical
|
||||
from glances.thresholds import GlancesThresholds
|
||||
from glances.plugins.glances_plugin import GlancesPlugin
|
||||
from glances.programs import processes_to_programs
|
||||
from glances.compat import subsample, range
|
||||
from glances.compat import subsample, range, string_value_to_float
|
||||
from glances.secure import secure_popen
|
||||
from glances.compat import PY3
|
||||
|
||||
@ -284,6 +288,16 @@ class TestGlances(unittest.TestCase):
|
||||
# Check if number of processes in the list equal counter
|
||||
# self.assertEqual(total, len(stats_grab))
|
||||
|
||||
def test_018_string_value_to_float(self):
|
||||
"""Check string_value_to_float function"""
|
||||
print('INFO: [TEST_018] Check string_value_to_float function')
|
||||
self.assertEqual(string_value_to_float('32kB'), 32000.0)
|
||||
self.assertEqual(string_value_to_float('32 KB'), 32000.0)
|
||||
self.assertEqual(string_value_to_float('15.5MB'), 15500000.0)
|
||||
self.assertEqual(string_value_to_float('25.9'), 25.9)
|
||||
self.assertEqual(string_value_to_float('12'), 12)
|
||||
self.assertEqual(string_value_to_float('--'), None)
|
||||
|
||||
def test_094_thresholds(self):
|
||||
"""Test thresholds classes"""
|
||||
print('INFO: [TEST_094] Thresholds')
|
||||
|
Loading…
Reference in New Issue
Block a user