Issues (50)

glances/plugins/containers/__init__.py (1 issue)

1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of Glances.
4
#
5
# SPDX-FileCopyrightText: 2022 Nicolas Hennion <[email protected]>
6
#
7
# SPDX-License-Identifier: LGPL-3.0-only
8
#
9
10
"""Containers plugin."""
11
12
import os
13
from copy import deepcopy
14
15
from glances.logger import logger
16
from glances.plugins.containers.engines.docker import DockerContainersExtension, import_docker_error_tag
17
from glances.plugins.containers.engines.podman import PodmanContainersExtension, import_podman_error_tag
18
from glances.plugins.plugin.model import GlancesPluginModel
19
from glances.processes import glances_processes
20
from glances.processes import sort_stats as sort_stats_processes
21
22
# Fields description
23
# description: human readable description
24
# short_name: shortname to use un UI
25
# unit: unit type
26
# rate: is it a rate ? If yes, // by time_since_update when displayed,
27
# min_symbol: Auto unit should be used if value > than 1 'X' (K, M, G)...
28
fields_description = {
29
    'name': {
30
        'description': 'Container name',
31
    },
32
    'id': {
33
        'description': 'Container ID',
34
    },
35
    'image': {
36
        'description': 'Container image',
37
    },
38
    'status': {
39
        'description': 'Container status',
40
    },
41
    'created': {
42
        'description': 'Container creation date',
43
    },
44
    'command': {
45
        'description': 'Container command',
46
    },
47
    'cpu_percent': {
48
        'description': 'Container CPU consumption',
49
        'unit': 'percent',
50
    },
51
    'memory_usage': {
52
        'description': 'Container memory usage',
53
        'unit': 'byte',
54
    },
55
    'io_rx': {
56
        'description': 'Container IO bytes read rate',
57
        'unit': 'bytepersecond',
58
    },
59
    'io_wx': {
60
        'description': 'Container IO bytes write rate',
61
        'unit': 'bytepersecond',
62
    },
63
    'network_rx': {
64
        'description': 'Container network RX bitrate',
65
        'unit': 'bitpersecond',
66
    },
67
    'network_tx': {
68
        'description': 'Container network TX bitrate',
69
        'unit': 'bitpersecond',
70
    },
71
    'uptime': {
72
        'description': 'Container uptime',
73
    },
74
    'engine': {
75
        'description': 'Container engine (Docker and Podman are currently supported)',
76
    },
77
    'pod_name': {
78
        'description': 'Pod name (only with Podman)',
79
    },
80
    'pod_id': {
81
        'description': 'Pod ID (only with Podman)',
82
    },
83
}
84
85
# Define the items history list (list of items to add to history)
86
# TODO: For the moment limited to the CPU. Had to change the graph exports
87
#       method to display one graph per container.
88
# items_history_list = [{'name': 'cpu_percent',
89
#                        'description': 'Container CPU consumption in %',
90
#                        'y_unit': '%'},
91
#                       {'name': 'memory_usage',
92
#                        'description': 'Container memory usage in bytes',
93
#                        'y_unit': 'B'},
94
#                       {'name': 'network_rx',
95
#                        'description': 'Container network RX bitrate in bits per second',
96
#                        'y_unit': 'bps'},
97
#                       {'name': 'network_tx',
98
#                        'description': 'Container network TX bitrate in bits per second',
99
#                        'y_unit': 'bps'},
100
#                       {'name': 'io_r',
101
#                        'description': 'Container IO bytes read per second',
102
#                        'y_unit': 'Bps'},
103
#                       {'name': 'io_w',
104
#                        'description': 'Container IO bytes write per second',
105
#                        'y_unit': 'Bps'}]
106
items_history_list = [{'name': 'cpu_percent', 'description': 'Container CPU consumption in %', 'y_unit': '%'}]
107
108
# List of key to remove before export
109
export_exclude_list = ['cpu', 'io', 'memory', 'network']
110
111
# Sort dictionary for human
112
sort_for_human = {
113
    'io_counters': 'disk IO',
114
    'cpu_percent': 'CPU consumption',
115
    'memory_usage': 'memory consumption',
116
    'cpu_times': 'uptime',
117
    'name': 'container name',
118
    None: 'None',
119
}
120
121
122
class PluginModel(GlancesPluginModel):
123
    """Glances Docker plugin.
124
125
    stats is a dict: {'version': {...}, 'containers': [{}, {}]}
126
    """
127
128
    def __init__(self, args=None, config=None):
129
        """Init the plugin."""
130
        super(PluginModel, self).__init__(
131
            args=args, config=config, items_history_list=items_history_list, fields_description=fields_description
132
        )
133
134
        # The plugin can be disabled using: args.disable_docker
135
        self.args = args
136
137
        # Default config keys
138
        self.config = config
139
140
        # We want to display the stat in the curse interface
141
        self.display_curse = True
142
143
        # Init the Docker API
144
        self.docker_extension = DockerContainersExtension() if not import_docker_error_tag else None
145
146
        # Init the Podman API
147
        if import_podman_error_tag:
148
            self.podman_extension = None
149
        else:
150
            self.podman_extension = PodmanContainersExtension(podman_sock=self._podman_sock())
151
152
        # Sort key
153
        self.sort_key = None
154
155
        # Force a first update because we need two update to have the first stat
156
        self.update()
157
        self.refresh_timer.set(0)
158
159
    def _podman_sock(self):
160
        """Return the podman sock.
161
        Could be desfined in the [docker] section thanks to the podman_sock option.
162
        Default value: unix:///run/user/1000/podman/podman.sock
163
        """
164
        conf_podman_sock = self.get_conf_value('podman_sock')
165
        if len(conf_podman_sock) == 0:
166
            return "unix:///run/user/1000/podman/podman.sock"
167
        else:
168
            return conf_podman_sock[0]
169
170
    def exit(self):
171
        """Overwrite the exit method to close threads."""
172
        if self.docker_extension:
173
            self.docker_extension.stop()
174
        if self.podman_extension:
175
            self.podman_extension.stop()
176
        # Call the father class
177
        super(PluginModel, self).exit()
178
179
    def get_key(self):
180
        """Return the key of the list."""
181
        return 'name'
182
183
    def get_export(self):
184
        """Overwrite the default export method.
185
186
        - Only exports containers
187
        - The key is the first container name
188
        """
189
        try:
190
            ret = deepcopy(self.stats)
191
        except KeyError as e:
192
            logger.debug("docker plugin - Docker export error {}".format(e))
193
            ret = []
194
195
        # Remove fields uses to compute rate
196
        for container in ret:
197
            for i in export_exclude_list:
198
                container.pop(i)
199
200
        return ret
201
202
    def _all_tag(self):
203
        """Return the all tag of the Glances/Docker configuration file.
204
205
        # By default, Glances only display running containers
206
        # Set the following key to True to display all containers
207
        all=True
208
        """
209
        all_tag = self.get_conf_value('all')
210
        if len(all_tag) == 0:
211
            return False
212
        else:
213
            return all_tag[0].lower() == 'true'
214
215
    @GlancesPluginModel._check_decorator
216
    @GlancesPluginModel._log_result_decorator
217
    def update(self):
218
        """Update Docker and podman stats using the input method."""
219
        # Connection should be ok
220
        if self.docker_extension is None and self.podman_extension is None:
221
            return self.get_init_value()
222
223
        if self.input_method == 'local':
224
            # Update stats
225
            stats_docker = self.update_docker() if self.docker_extension else {}
226
            stats_podman = self.update_podman() if self.podman_extension else {}
227
            stats = stats_docker.get('containers', []) + stats_podman.get('containers', [])
228
        elif self.input_method == 'snmp':
229
            # Update stats using SNMP
230
            # Not available
231
            pass
232
233
        # Sort and update the stats
234
        # @TODO: Have a look because sort did not work for the moment (need memory stats ?)
235
        self.sort_key, self.stats = sort_docker_stats(stats)
0 ignored issues
show
The variable stats does not seem to be defined for all execution paths.
Loading history...
236
237
        return self.stats
238
239
    def update_docker(self):
240
        """Update Docker stats using the input method."""
241
        version, containers = self.docker_extension.update(all_tag=self._all_tag())
242
        for container in containers:
243
            container["engine"] = 'docker'
244
        return {"version": version, "containers": containers}
245
246
    def update_podman(self):
247
        """Update Podman stats."""
248
        version, containers = self.podman_extension.update(all_tag=self._all_tag())
249
        for container in containers:
250
            container["engine"] = 'podman'
251
        return {"version": version, "containers": containers}
252
253
    def get_user_ticks(self):
254
        """Return the user ticks by reading the environment variable."""
255
        return os.sysconf(os.sysconf_names['SC_CLK_TCK'])
256
257
    def memory_usage_no_cache(self, mem):
258
        """Return the 'real' memory usage by removing inactive_file to usage"""
259
        # Ref: https://github.com/docker/docker-py/issues/3210
260
        return mem['usage'] - (mem['inactive_file'] if 'inactive_file' in mem else 0)
261
262
    def update_views(self):
263
        """Update stats views."""
264
        # Call the father's method
265
        super(PluginModel, self).update_views()
266
267
        if not self.stats:
268
            return False
269
270
        # Add specifics information
271
        # Alert
272
        for i in self.stats:
273
            # Init the views for the current container (key = container name)
274
            self.views[i[self.get_key()]] = {'cpu': {}, 'mem': {}}
275
            # CPU alert
276
            if 'cpu' in i and 'total' in i['cpu']:
277
                # Looking for specific CPU container threshold in the conf file
278
                alert = self.get_alert(i['cpu']['total'], header=i['name'] + '_cpu', action_key=i['name'])
279
                if alert == 'DEFAULT':
280
                    # Not found ? Get back to default CPU threshold value
281
                    alert = self.get_alert(i['cpu']['total'], header='cpu')
282
                self.views[i[self.get_key()]]['cpu']['decoration'] = alert
283
            # MEM alert
284
            if 'memory' in i and 'usage' in i['memory']:
285
                # Looking for specific MEM container threshold in the conf file
286
                alert = self.get_alert(
287
                    self.memory_usage_no_cache(i['memory']),
288
                    maximum=i['memory']['limit'],
289
                    header=i['name'] + '_mem',
290
                    action_key=i['name'],
291
                )
292
                if alert == 'DEFAULT':
293
                    # Not found ? Get back to default MEM threshold value
294
                    alert = self.get_alert(
295
                        self.memory_usage_no_cache(i['memory']), maximum=i['memory']['limit'], header='mem'
296
                    )
297
                self.views[i[self.get_key()]]['mem']['decoration'] = alert
298
299
        # Display Engine and Pod name ?
300
        show_pod_name = False
301
        if any(ct.get("pod_name") for ct in self.stats):
302
            show_pod_name = True
303
        self.views['show_pod_name'] = show_pod_name
304
        show_engine_name = False
305
        if len(set(ct["engine"] for ct in self.stats)) > 1:
306
            show_engine_name = True
307
        self.views['show_engine_name'] = show_engine_name
308
309
        return True
310
311
    def msg_curse(self, args=None, max_width=None):
312
        """Return the dict to display in the curse interface."""
313
        # Init the return message
314
        ret = []
315
316
        # Only process if stats exist (and non null) and display plugin enable...
317
        if not self.stats or len(self.stats) == 0 or self.is_disabled():
318
            return ret
319
320
        # Build the string message
321
        # Title
322
        msg = '{}'.format('CONTAINERS')
323
        ret.append(self.curse_add_line(msg, "TITLE"))
324
        msg = ' {}'.format(len(self.stats))
325
        ret.append(self.curse_add_line(msg))
326
        msg = ' sorted by {}'.format(sort_for_human[self.sort_key])
327
        ret.append(self.curse_add_line(msg))
328
        ret.append(self.curse_new_line())
329
        # Header
330
        ret.append(self.curse_new_line())
331
        # Get the maximum containers name
332
        # Max size is configurable. See feature request #1723.
333
        name_max_width = min(
334
            self.config.get_int_value('containers', 'max_name_size', default=20) if self.config is not None else 20,
335
            len(max(self.stats, key=lambda x: len(x['name']))['name']),
336
        )
337
338
        if self.views['show_engine_name']:
339
            msg = ' {:{width}}'.format('Engine', width=6)
340
            ret.append(self.curse_add_line(msg))
341
        if self.views['show_pod_name']:
342
            msg = ' {:{width}}'.format('Pod', width=12)
343
            ret.append(self.curse_add_line(msg))
344
        msg = ' {:{width}}'.format('Name', width=name_max_width)
345
        ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'name' else 'DEFAULT'))
346
        msg = '{:>10}'.format('Status')
347
        ret.append(self.curse_add_line(msg))
348
        msg = '{:>10}'.format('Uptime')
349
        ret.append(self.curse_add_line(msg))
350
        msg = '{:>6}'.format('CPU%')
351
        ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'cpu_percent' else 'DEFAULT'))
352
        msg = '{:>7}'.format('MEM')
353
        ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'memory_usage' else 'DEFAULT'))
354
        msg = '/{:<7}'.format('MAX')
355
        ret.append(self.curse_add_line(msg))
356
        msg = '{:>7}'.format('IOR/s')
357
        ret.append(self.curse_add_line(msg))
358
        msg = ' {:<7}'.format('IOW/s')
359
        ret.append(self.curse_add_line(msg))
360
        msg = '{:>7}'.format('Rx/s')
361
        ret.append(self.curse_add_line(msg))
362
        msg = ' {:<7}'.format('Tx/s')
363
        ret.append(self.curse_add_line(msg))
364
        msg = ' {:8}'.format('Command')
365
        ret.append(self.curse_add_line(msg))
366
367
        # Data
368
        for container in self.stats:
369
            ret.append(self.curse_new_line())
370
            if self.views['show_engine_name']:
371
                ret.append(self.curse_add_line(' {:{width}}'.format(container["engine"], width=6)))
372
            if self.views['show_pod_name']:
373
                ret.append(self.curse_add_line(' {:{width}}'.format(container.get("pod_id", "-"), width=12)))
374
            # Name
375
            ret.append(self.curse_add_line(self._msg_name(container=container, max_width=name_max_width)))
376
            # Status
377
            status = self.container_alert(container['status'])
378
            msg = '{:>10}'.format(container['status'][0:10])
379
            ret.append(self.curse_add_line(msg, status))
380
            # Uptime
381
            if container['uptime']:
382
                msg = '{:>10}'.format(container['uptime'])
383
            else:
384
                msg = '{:>10}'.format('_')
385
            ret.append(self.curse_add_line(msg))
386
            # CPU
387
            try:
388
                msg = '{:>6.1f}'.format(container['cpu']['total'])
389
            except (KeyError, TypeError):
390
                msg = '{:>6}'.format('_')
391
            ret.append(self.curse_add_line(msg, self.get_views(item=container['name'], key='cpu', option='decoration')))
392
            # MEM
393
            try:
394
                msg = '{:>7}'.format(self.auto_unit(self.memory_usage_no_cache(container['memory'])))
395
            except KeyError:
396
                msg = '{:>7}'.format('_')
397
            ret.append(self.curse_add_line(msg, self.get_views(item=container['name'], key='mem', option='decoration')))
398
            try:
399
                msg = '/{:<7}'.format(self.auto_unit(container['memory']['limit']))
400
            except (KeyError, TypeError):
401
                msg = '/{:<7}'.format('_')
402
            ret.append(self.curse_add_line(msg))
403
            # IO R/W
404
            unit = 'B'
405
            try:
406
                value = self.auto_unit(int(container['io_rx'])) + unit
407
                msg = '{:>7}'.format(value)
408
            except (KeyError, TypeError):
409
                msg = '{:>7}'.format('_')
410
            ret.append(self.curse_add_line(msg))
411
            try:
412
                value = self.auto_unit(int(container['io_wx'])) + unit
413
                msg = ' {:<7}'.format(value)
414
            except (KeyError, TypeError):
415
                msg = ' {:<7}'.format('_')
416
            ret.append(self.curse_add_line(msg))
417
            # NET RX/TX
418
            if args.byte:
419
                # Bytes per second (for dummy)
420
                to_bit = 1
421
                unit = ''
422
            else:
423
                # Bits per second (for real network administrator | Default)
424
                to_bit = 8
425
                unit = 'b'
426
            try:
427
                value = self.auto_unit(int(container['network_rx'] * to_bit)) + unit
428
                msg = '{:>7}'.format(value)
429
            except (KeyError, TypeError):
430
                msg = '{:>7}'.format('_')
431
            ret.append(self.curse_add_line(msg))
432
            try:
433
                value = self.auto_unit(int(container['network_tx'] * to_bit)) + unit
434
                msg = ' {:<7}'.format(value)
435
            except (KeyError, TypeError):
436
                msg = ' {:<7}'.format('_')
437
            ret.append(self.curse_add_line(msg))
438
            # Command
439
            if container['command'] is not None:
440
                msg = ' {}'.format(container['command'])
441
            else:
442
                msg = ' {}'.format('_')
443
            ret.append(self.curse_add_line(msg, splittable=True))
444
445
        return ret
446
447
    def _msg_name(self, container, max_width):
448
        """Build the container name."""
449
        name = container['name'][:max_width]
450
        return ' {:{width}}'.format(name, width=max_width)
451
452
    def container_alert(self, status):
453
        """Analyse the container status."""
454
        if status == 'running':
455
            return 'OK'
456
        elif status == 'exited':
457
            return 'WARNING'
458
        elif status == 'dead':
459
            return 'CRITICAL'
460
        else:
461
            return 'CAREFUL'
462
463
464
def sort_docker_stats(stats):
465
    # Sort Docker stats using the same function than processes
466
    sort_by = glances_processes.sort_key
467
    sort_by_secondary = 'memory_usage'
468
    if sort_by == 'memory_percent':
469
        sort_by = 'memory_usage'
470
        sort_by_secondary = 'cpu_percent'
471
    elif sort_by in ['username', 'io_counters', 'cpu_times']:
472
        sort_by = 'cpu_percent'
473
474
    # Sort docker stats
475
    sort_stats_processes(
476
        stats,
477
        sorted_by=sort_by,
478
        sorted_by_secondary=sort_by_secondary,
479
        # Reverse for all but name
480
        reverse=glances_processes.sort_key != 'name',
481
    )
482
483
    # Return the main sort key and the sorted stats
484
    return sort_by, stats
485