Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / docker / docker_ @ a3ae1af1

Historique | Voir | Annoter | Télécharger (19,2 ko)

1 937cb1d0 Doctor
#!/usr/bin/env python3
2
"""
3
=head1 NAME
4
5
docker_ - Docker wildcard-plugin to monitor a L<Docker|https://www.docker.com> host.
6
7 1fa5558a Olivier Mehani
This wildcard plugin provides series C<containers>, C<images>, C<status>,
8
C<volumes>, C<cpu>, C<memory> and C<network> as separate graphs. It also
9
supports a C<multi> suffix that provides all of those as a multigraph.
10 937cb1d0 Doctor
11
=head1 INSTALLATION
12
13
- Copy this plugin in your munin plugins directory
14
- Install Python3 "docker" package
15
16
=over 2
17
18 1fa5558a Olivier Mehani
If you want all the graphs as a multigraph, create a single multi symlink.
19
20
    ln -s /usr/share/munin/plugins/docker_ /etc/munin/plugins/docker_multi
21
22
Or choose a subset of those you want.
23
24 937cb1d0 Doctor
    ln -s /usr/share/munin/plugins/docker_ /etc/munin/plugins/docker_containers
25
    ln -s /usr/share/munin/plugins/docker_ /etc/munin/plugins/docker_cpu
26
    ln -s /usr/share/munin/plugins/docker_ /etc/munin/plugins/docker_images
27
    ln -s /usr/share/munin/plugins/docker_ /etc/munin/plugins/docker_memory
28 13d5b234 Olivier Mehani
    ln -s /usr/share/munin/plugins/docker_ /etc/munin/plugins/docker_network
29 937cb1d0 Doctor
    ln -s /usr/share/munin/plugins/docker_ /etc/munin/plugins/docker_status
30
    ln -s /usr/share/munin/plugins/docker_ /etc/munin/plugins/docker_volumes
31
32
=back
33
34
After the installation you need to restart your munin-node:
35
36
=over 2
37
38
    systemctl restart munin-node
39
40
=back
41
42
=head1 CONFIGURATION
43
44
This plugin need to run as root, you need to create a file named docker placed in the
45
directory /etc/munin/plugin-conf.d/ with the following config (you can also use
46
Docker environment variables here as described in
47
https://docs.docker.com/compose/reference/envvars/):
48
49 9e2b9182 Rowan Wookey
You can use the EXCLUDE_CONTAINER_NAME environment variable to specify a regular expression
50
which if matched will exclude the matching containers from the memory and cpu graphs.
51
52
For example
53
54 c6f88968 Lars Kruse
 env.EXCLUDE_CONTAINER_NAME runner
55 9e2b9182 Rowan Wookey
56
Would exclude all containers with the word "runner" in the name.
57
58
59 937cb1d0 Doctor
=over 2
60
61
    [docker_*]
62 7ac85085 Lars Kruse
    group docker
63
    env.DOCKER_HOST unix://run/docker.sock
64 9e2b9182 Rowan Wookey
    env.EXCLUDE_CONTAINER_NAME regexp
65 937cb1d0 Doctor
66
=back
67 13d5b234 Olivier Mehani
68 7ac85085 Lars Kruse
You may need to pick a different group depending on the name schema of your
69
distribution.  Or maybe use "user root", if nothing else works.
70
71 13d5b234 Olivier Mehani
=head1 AUTHORS
72
73
This section has been reverse-engineered from git logs
74
75 e794a441 Olivier Mehani
Codimp <contact@lithio.fr>: original rewrite
76 c6f88968 Lars Kruse
77 e794a441 Olivier Mehani
Rowan Wookey <admin@rwky.net>: performance improvement
78 c6f88968 Lars Kruse
79 e794a441 Olivier Mehani
Olivier Mehani <shtrom@ssji.net>: Network support, ClientWrapper, general cleanup, multigraph
80 13d5b234 Olivier Mehani
81
=head1 MAGIC MARKERS
82
83 c6f88968 Lars Kruse
 #%# family=auto
84 1fa5558a Olivier Mehani
 #%# capabilities=autoconf suggest multigraph
85 13d5b234 Olivier Mehani
86 c6f88968 Lars Kruse
=cut
87 937cb1d0 Doctor
"""
88
89
import os
90
import sys
91 9e2b9182 Rowan Wookey
import re
92 254eb56d Olivier Mehani
try:
93
    from functools import cached_property
94
except ImportError:
95
    # If cached_property is not available,
96
    # just use the property decorator, without caching
97
    # This is for backward compatibility with Python<3.8
98
    cached_property = property
99 fcd2af7b Rowan Wookey
from multiprocessing import Process, Queue
100 937cb1d0 Doctor
101
102 13d5b234 Olivier Mehani
def sorted_by_creation_date(func):
103
    def sorted_func(*args, **kwargs):
104
        return sorted(
105
            func(*args, **kwargs),
106
            key=(
107
                lambda x: x.attrs['CreatedAt']
108
                if 'CreatedAt' in x.attrs
109
                else x.attrs['Created']
110
            )
111
        )
112
    return sorted_func
113
114
115 be4ad90d Lars Kruse
def clean_fieldname(text):
116
    if text == "root":
117
        # "root" is a magic (forbidden) word
118
        return "_root"
119
    else:
120
        return re.sub(r"(^[^A-Za-z_]|[^A-Za-z0-9_])", "_", text)
121
122
123 13d5b234 Olivier Mehani
class ClientWrapper:
124
    """
125
    A small wrapper for the docker client, to centralise some parsing logic,
126
    and support caching.
127
128
    In addition, when the exclude_re parameter is not None,
129
    any container which name is matched by the RE will not be excluded from reports.
130
    """
131
    client = None
132
    exclude = None
133
134
    def __init__(self, client, exclude_re=None):
135
        self.client = client
136
        if exclude_re:
137
            self.exclude = re.compile(exclude_re)
138
139 d3a7b794 Olivier Mehani
    @property
140
    def api(self):
141
        return self.client.api
142
143 13d5b234 Olivier Mehani
    @cached_property
144
    @sorted_by_creation_date
145
    def all_containers(self):
146
        return [c for c in self.client.containers.list(all=True)
147
                if not self.exclude
148
                or not self.exclude.search(c.name)]
149
150
    @cached_property
151
    @sorted_by_creation_date
152
    def intermediate_images(self):
153
        return list(
154
            set(self.all_images)
155
            .difference(
156
                set(self.images)
157
                .difference(
158
                    set(self.dangling_images)
159
                )
160
            )
161
        )
162
163
    @cached_property
164
    @sorted_by_creation_date
165
    def all_images(self):
166
        return self.client.images.list(all=True)
167
168
    @cached_property
169
    @sorted_by_creation_date
170
    def images(self):
171
        images = self.client.images.list()
172
        return list(
173
            set(images)
174
            .difference(
175
                set(self.dangling_images))
176
        )
177
178
    @cached_property
179
    @sorted_by_creation_date
180
    def dangling_images(self):
181
        return self.client.images.list(filters={'dangling': True})
182
183
    @cached_property
184
    @sorted_by_creation_date
185
    def volumes(self):
186
        return self.client.volumes.list()
187
188
189 b09fa4a7 Olivier Mehani
def container_summary(container, *args):
190 13d5b234 Olivier Mehani
    summary = container.name
191 b09fa4a7 Olivier Mehani
    attributes = container_attributes(container, *args)
192 13d5b234 Olivier Mehani
    if attributes:
193
        summary += f' ({attributes})'
194
    return summary
195
196
197 b09fa4a7 Olivier Mehani
def container_attributes(container, *args):
198 13d5b234 Olivier Mehani
    attributes = container.image.tags
199
    attributes.append(container.attrs['Created'])
200 b09fa4a7 Olivier Mehani
    return ', '.join(attributes + list(args))
201 13d5b234 Olivier Mehani
202
203 937cb1d0 Doctor
def print_containers_status(client):
204 13d5b234 Olivier Mehani
    running = []
205 24825188 Olivier Mehani
    unhealthy = []
206 13d5b234 Olivier Mehani
    paused = []
207
    created = []
208
    restarting = []
209
    removing = []
210
    exited = []
211
    dead = []
212
    for container in client.all_containers:
213 937cb1d0 Doctor
        if container.status == 'running':
214 24825188 Olivier Mehani
            state = client.api.inspect_container(container.name)['State']
215
            if state.get('Health', {}).get('Status') == 'unhealthy':
216
                unhealthy.append(container)
217
            else:
218
                running.append(container)
219 937cb1d0 Doctor
        elif container.status == 'paused':
220 13d5b234 Olivier Mehani
            paused.append(container)
221 937cb1d0 Doctor
        elif container.status == 'created':
222 13d5b234 Olivier Mehani
            created.append(container)
223 937cb1d0 Doctor
        elif container.status == 'restarting':
224 13d5b234 Olivier Mehani
            restarting.append(container)
225 937cb1d0 Doctor
        elif container.status == 'removing':
226 13d5b234 Olivier Mehani
            removing.append(container)
227 937cb1d0 Doctor
        elif container.status == 'exited':
228 13d5b234 Olivier Mehani
            exited.append(container)
229 937cb1d0 Doctor
        elif container.status == 'dead':
230 13d5b234 Olivier Mehani
            dead.append(container)
231
    print('running.value', len(running))
232
    print('running.extinfo', ', '.join(container_summary(c) for c in running))
233 24825188 Olivier Mehani
    print('unhealthy.value', len(unhealthy))
234
    print('unhealthy.extinfo', ', '.join(container_summary(c) for c in unhealthy))
235 13d5b234 Olivier Mehani
    print('paused.value', len(paused))
236
    print('paused.extinfo', ', '.join(container_summary(c) for c in paused))
237
    print('created.value', len(created))
238
    print('created.extinfo', ', '.join(container_summary(c) for c in created))
239
    print('restarting.value', len(restarting))
240
    print('restarting.extinfo', ', '.join(container_summary(c) for c in restarting))
241
    print('removing.value', len(removing))
242
    print('removing.extinfo', ', '.join(container_summary(c) for c in removing))
243
    print('exited.value', len(exited))
244
    print('exited.extinfo', ', '.join(container_summary(c) for c in exited))
245
    print('dead.value', len(dead))
246
    print('dead.extinfo', ', '.join(container_summary(c) for c in dead))
247
248
249
def image_summary(image):
250
    attributes = image.tags
251
    attributes.append(image.attrs['Created'])
252
    attributes.append(f"{round(image.attrs['Size']/1024**2, 2)} MiB")
253
    return f"{image.short_id} ({', '.join(attributes)})"
254
255
256
def print_images_count(client):
257
    images = client.images
258
    intermediate = client.intermediate_images
259
    dangling = client.dangling_images
260
261
    print('intermediate_quantity.value', len(intermediate))
262
    print('intermediate_quantity.extinfo', ', '.join(image_summary(i) for i in intermediate))
263
    print('images_quantity.value', len(images))
264
    print('images_quantity.extinfo', ', '.join(image_summary(i) for i in images))
265
    print('dangling_quantity.value', len(dangling))
266
    print('dangling_quantity.extinfo', ', '.join(image_summary(i) for i in dangling))
267 937cb1d0 Doctor
268
269 fcd2af7b Rowan Wookey
def get_container_stats(container, q):
270
    q.put(container.stats(stream=False))
271
272
273
def parallel_container_stats(client):
274
    proc_list = []
275
    stats = {}
276 a3ae1af1 Lars Kruse
    for container in client.all_containers:
277 fcd2af7b Rowan Wookey
        q = Queue()
278
        p = Process(target=get_container_stats, args=(container, q))
279
        proc_list.append({'proc': p, 'queue': q, 'container': container})
280
        p.start()
281
    for proc in proc_list:
282
        proc['proc'].join()
283
        stats[proc['container']] = proc['queue'].get()
284
    return stats.items()
285
286
287
def print_containers_cpu(client):
288
    for container, stats in parallel_container_stats(client):
289 937cb1d0 Doctor
        cpu_percent = 0.0
290 13d5b234 Olivier Mehani
        cpu_delta = (float(stats["cpu_stats"]["cpu_usage"]["total_usage"])
291
                     - float(stats["precpu_stats"]["cpu_usage"]["total_usage"]))
292
        system_delta = (float(stats["cpu_stats"]["system_cpu_usage"])
293
                        - float(stats["precpu_stats"]["system_cpu_usage"]))
294 937cb1d0 Doctor
        if system_delta > 0.0:
295 109144de Olivier Mehani
            cpu_percent = cpu_delta / system_delta * 100.0 * os.cpu_count()
296 51af0be0 Daniel Neugebauer
        clean_container_name = clean_fieldname(container.name)
297
        print(clean_container_name + '.value', cpu_percent)
298
        print(clean_container_name + '.extinfo', container_attributes(container))
299 937cb1d0 Doctor
300
301
def print_containers_memory(client):
302 fcd2af7b Rowan Wookey
    for container, stats in parallel_container_stats(client):
303 3a20ae41 Olivier Mehani
        if 'total_rss' in stats['memory_stats']['stats']:  # cgroupv1 only?
304
            memory_usage = stats['memory_stats']['stats']['total_rss']
305
            extinfo = 'Resident Set Size'
306
        else:
307
            memory_usage = stats['memory_stats']['usage']
308
            extinfo = 'Total memory usage'
309 51af0be0 Daniel Neugebauer
        clean_container_name = clean_fieldname(container.name)
310
        print(clean_container_name + '.value', memory_usage)
311
        print(clean_container_name + '.extinfo', container_attributes(container, extinfo))
312 13d5b234 Olivier Mehani
313
314
def print_containers_network(client):
315
    for container, stats in parallel_container_stats(client):
316
        tx_bytes = 0
317
        rx_bytes = 0
318 64d5d8af Oliver
        if "networks" in stats:
319
            for data in stats['networks'].values():
320
                tx_bytes += data['tx_bytes']
321
                rx_bytes += data['rx_bytes']
322 51af0be0 Daniel Neugebauer
        clean_container_name = clean_fieldname(container.name)
323
        print(clean_container_name + '_up.value', tx_bytes)
324
        print(clean_container_name + '_down.value', rx_bytes)
325
        print(clean_container_name + '_up.extinfo', container_attributes(container))
326 13d5b234 Olivier Mehani
327
328
def volume_summary(volume):
329
    summary = f"{volume.short_id}"
330
    if volume.attrs['Labels']:
331 b1e3e601 Olivier Mehani
        summary += f" ({', '.join(volume.attrs['Labels'])})"
332 13d5b234 Olivier Mehani
    return summary
333 937cb1d0 Doctor
334
335 1fa5558a Olivier Mehani
def status(client, mode):
336
    if mode == "config":
337
        print("graph_title Docker status")
338
        print("graph_vlabel containers")
339
        print("graph_category virtualization")
340
        print("graph_total All containers")
341
        print("running.label RUNNING")
342
        print("running.draw AREASTACK")
343
        print("running.info Running containers can be manipulated with "
344
              "`docker container [attach|kill|logs|pause|restart|stop] <NAME>` or "
345
              "commands run in them with `docker container exec "
346
              "[--detach|--interactive,--privileged,--tty] <NAME> <COMMAND>`"
347
              )
348
        print("unhealthy.label UNHEALTHY")
349
        print("unhealthy.draw AREASTACK")
350
        print("unhealthy.warning 1")
351
        print("unhealthy.info Unhealthy containers can be restarted with "
352
              "`docker container restart <NAME>`")
353
        print("paused.label PAUSED")
354
        print("paused.draw AREASTACK")
355
        print("paused.info Paused containers can be resumed with "
356
              "`docker container unpause <NAME>`")
357
        print("created.label CREATED")
358
        print("created.draw AREASTACK")
359
        print("created.info New containers can be created with "
360
              "`docker container create --name <NAME> <IMAGE_ID >` or "
361
              "`docker container run --name <NAME> <IMAGE_ID> <COMMAND>`")
362
        print("restarting.label RESTARTING")
363
        print("restarting.draw AREASTACK")
364
        print("restarting.info Containers can be restarted with "
365
              "`docker container restart <NAME>`")
366
        print("removing.label REMOVING")
367
        print("removing.draw AREASTACK")
368
        print("removing.info Containers can be removed with "
369
              "`docker container rm <NAME>`")
370
        print("exited.label EXITED")
371
        print("exited.draw AREASTACK")
372
        print("exited.info Exited containers can be started with "
373
              "`docker container start [--attach] <NAME>`")
374
        print("dead.label DEAD")
375
        print("dead.draw AREASTACK")
376
        print("dead.warning 1")
377
        print("dead.info Dead containers can be started with "
378
              "`docker container start <NAME>`")
379
    else:
380
        print_containers_status(client)
381
382
383
def containers(client, mode):
384
    if mode == "config":
385
        print("graph_title Docker containers")
386
        print("graph_vlabel containers")
387
        print("graph_category virtualization")
388
        print("containers_quantity.label Containers")
389
    else:
390
        print('containers_quantity.value', len(client.containers))
391
392
393
def images(client, mode):
394
    if mode == "config":
395
        print("graph_title Docker images")
396
        print("graph_vlabel images")
397
        print("graph_category virtualization")
398
        print("graph_total All images")
399
        print("intermediate_quantity.label Intermediate images")
400
        print("intermediate_quantity.draw AREASTACK")
401
        print("intermediate_quantity.info All unused images can be deleted with "
402
              "`docker image prune --all`")
403
        print("images_quantity.label Images")
404
        print("images_quantity.draw AREASTACK")
405
        print("images_quantity.info Images can be used in containers with "
406
              "`docker container create --name <NAME> <IMAGE_ID >` or "
407
              "`docker container run --name <NAME> <IMAGE_ID> <COMMAND>`")
408
        print("dangling_quantity.label Dangling images")
409
        print("dangling_quantity.draw AREASTACK")
410
        print("dangling_quantity.info Dangling images can be deleted with "
411
              "`docker image prune`"
412
              "or tagged with `docker image tag <IMAGE_ID> <NAME>`")
413
        print("dangling_quantity.warning 10")
414
    else:
415
        print_images_count(client)
416
417
418
def volumes(client, mode):
419
    if mode == "config":
420
        print("graph_title Docker volumes")
421
        print("graph_vlabel volumes")
422
        print("graph_category virtualization")
423
        print("volumes_quantity.label Volumes")
424
        print("volumes_quantity.draw AREASTACK")
425
        print("volumes_quantity.info Unused volumes can be deleted with "
426
              "`docker volume prune`")
427
    else:
428
        print('volumes_quantity.value', len(client.volumes))
429
        print('volumes_quantity.extinfo', ', '.join(volume_summary(v) for v in client.volumes))
430
431
432
def cpu(client, mode):
433
    if mode == "config":
434
        graphlimit = str(os.cpu_count() * 100)
435
        print("graph_title Docker containers CPU usage")
436
        print("graph_args --base 1000 -r --lower-limit 0 --upper-limit " + graphlimit)
437
        print("graph_scale no")
438
        print("graph_period second")
439
        print("graph_vlabel CPU usage (%)")
440
        print("graph_category virtualization")
441
        print("graph_info This graph shows docker container CPU usage.")
442
        print("graph_total Total CPU usage")
443
        for container in client.all_containers:
444 be4ad90d Lars Kruse
            fieldname = clean_fieldname(container.name)
445
            print("{}.label {}".format(fieldname, container.name))
446
            print("{}.draw AREASTACK".format(fieldname))
447
            print("{}.info {}".format(fieldname, container_attributes(container)))
448 1fa5558a Olivier Mehani
    else:
449
        print_containers_cpu(client)
450
451
452
def network(client, mode):
453 98aeca5f Lars Kruse
    if mode == "config":
454
        print("graph_title Docker containers network usage")
455
        print("graph_args --base 1024 -l 0")
456
        print("graph_vlabel bits in (-) / out (+) per ${graph_period}")
457
        print("graph_category virtualization")
458
        print("graph_info This graph shows docker container network usage.")
459
        print("graph_total Total network usage")
460
        for container in client.all_containers:
461
            fieldname = clean_fieldname(container.name)
462
            print("{}_down.label {}_received".format(fieldname, container.name))
463
            print("{}_down.type DERIVE".format(fieldname))
464
            print("{}_down.min 0".format(fieldname))
465
            print("{}_down.graph no".format(fieldname))
466
            print("{}_down.cdef {}_down,8,*".format(fieldname, fieldname))
467
            print("{}_up.label {}".format(fieldname, container.name))
468
            print("{}_up.draw LINESTACK1".format(fieldname))
469
            print("{}_up.type DERIVE".format(fieldname))
470
            print("{}_up.min 0".format(fieldname))
471
            print("{}_up.negative {}_down".format(fieldname, fieldname))
472
            print("{}_up.cdef {}_up,8,*".format(fieldname, fieldname))
473
            print("{}_up.info {}".format(fieldname, container_attributes(container)))
474
    else:
475
        print_containers_network(client)
476 1fa5558a Olivier Mehani
477
478
def memory(client, mode):
479
    if mode == "config":
480
        print("graph_title Docker containers memory usage")
481
        print("graph_args --base 1024 -l 0")
482
        print("graph_vlabel Bytes")
483
        print("graph_category virtualization")
484
        print("graph_info This graph shows docker container memory usage.")
485
        print("graph_total Total memory usage")
486
        for container in client.all_containers:
487 be4ad90d Lars Kruse
            fieldname = clean_fieldname(container.name)
488
            print("{}.label {}".format(fieldname, container.name))
489
            print("{}.draw AREASTACK".format(fieldname))
490
            print("{}.info {}".format(fieldname, container_attributes(container)))
491 1fa5558a Olivier Mehani
    else:
492
        print_containers_memory(client)
493
494
495 937cb1d0 Doctor
def main():
496 1fa5558a Olivier Mehani
    series = [
497 807f15b9 Ricardo Sanz
        'containers',
498 1fa5558a Olivier Mehani
        'cpu',
499
        'images',
500
        'memory',
501
        'network',
502
        'status',
503
        'volumes',
504
    ]
505
506 937cb1d0 Doctor
    try:
507
        mode = sys.argv[1]
508
    except IndexError:
509
        mode = ""
510
    wildcard = sys.argv[0].split("docker_")[1].split("_")[0]
511
512 13d5b234 Olivier Mehani
    try:
513
        import docker
514
        client = docker.from_env()
515
        if mode == "autoconf":
516
            client.ping()
517
            print('yes')
518
            sys.exit(0)
519
    except Exception as e:
520
        print(f'no ({e})')
521
        if mode == "autoconf":
522
            sys.exit(0)
523
        sys.exit(1)
524
525 937cb1d0 Doctor
    if mode == "suggest":
526 1fa5558a Olivier Mehani
        # The multigraph covers all other graphs,
527
        # so we only need to suggest one
528
        print("multi")
529 13d5b234 Olivier Mehani
        sys.exit(0)
530 937cb1d0 Doctor
531 13d5b234 Olivier Mehani
    client = ClientWrapper(client,
532
                           exclude_re=os.getenv('EXCLUDE_CONTAINER_NAME'))
533 937cb1d0 Doctor
534 1fa5558a Olivier Mehani
    if wildcard in series:
535
        # dereference the function name by looking in the globals()
536
        # this assumes that the function name matches the series name exactly
537
        # if this were to change, a different approach would be needed,
538
        # most likely using a Dict of series name string to callable
539
        globals()[wildcard](client, mode)
540
    elif wildcard == 'multi':
541
        for s in series:
542
            print(f'multigraph docker_{s}')
543
            # ditto
544
            globals()[s](client, mode)
545
    else:
546
        print(f'unknown series ({wildcard})', file=sys.stderr)
547
        sys.exit(1)
548 937cb1d0 Doctor
549
550
if __name__ == '__main__':
551
    main()