Projet

Général

Profil

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

root / plugins / docker / docker_ @ d3a7b794

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