diff --git a/plugins/docker/docker_ b/plugins/docker/docker_ index ff4bd06c..53e4bbfb 100755 --- a/plugins/docker/docker_ +++ b/plugins/docker/docker_ @@ -4,8 +4,9 @@ docker_ - Docker wildcard-plugin to monitor a L host. -This wildcard plugin provides at the moment only the suffixes C, C, C, -C, C, C and C. +This wildcard plugin provides series C, C, C, +C, C, C and C as separate graphs. It also +supports a C suffix that provides all of those as a multigraph. =head1 INSTALLATION @@ -14,6 +15,12 @@ C, C, C and C. =over 2 +If you want all the graphs as a multigraph, create a single multi symlink. + + ln -s /usr/share/munin/plugins/docker_ /etc/munin/plugins/docker_multi + +Or choose a subset of those you want. + ln -s /usr/share/munin/plugins/docker_ /etc/munin/plugins/docker_containers ln -s /usr/share/munin/plugins/docker_ /etc/munin/plugins/docker_cpu ln -s /usr/share/munin/plugins/docker_ /etc/munin/plugins/docker_images @@ -66,12 +73,12 @@ This section has been reverse-engineered from git logs * Rowan Wookey : performance improvement -* Olivier Mehani : Network support, ClientWrapper, gerenal cleanup +* Olivier Mehani : Network support, ClientWrapper, general cleanup, multigraph =head1 MAGIC MARKERS #%# family=auto - #%# capabilities=autoconf suggest + #%# capabilities=autoconf suggest multigraph =cut """ @@ -315,159 +322,123 @@ def volume_summary(volume): return summary -def main(): - try: - mode = sys.argv[1] - except IndexError: - mode = "" - wildcard = sys.argv[0].split("docker_")[1].split("_")[0] +def status(client, mode): + if mode == "config": + print("graph_title Docker status") + print("graph_vlabel containers") + print("graph_category virtualization") + print("graph_total All containers") + print("running.label RUNNING") + print("running.draw AREASTACK") + print("running.info Running containers can be manipulated with " + "`docker container [attach|kill|logs|pause|restart|stop] ` or " + "commands run in them with `docker container exec " + "[--detach|--interactive,--privileged,--tty] `" + ) + print("unhealthy.label UNHEALTHY") + print("unhealthy.draw AREASTACK") + print("unhealthy.warning 1") + print("unhealthy.info Unhealthy containers can be restarted with " + "`docker container restart `") + print("paused.label PAUSED") + print("paused.draw AREASTACK") + print("paused.info Paused containers can be resumed with " + "`docker container unpause `") + print("created.label CREATED") + print("created.draw AREASTACK") + print("created.info New containers can be created with " + "`docker container create --name ` or " + "`docker container run --name `") + print("restarting.label RESTARTING") + print("restarting.draw AREASTACK") + print("restarting.info Containers can be restarted with " + "`docker container restart `") + print("removing.label REMOVING") + print("removing.draw AREASTACK") + print("removing.info Containers can be removed with " + "`docker container rm `") + print("exited.label EXITED") + print("exited.draw AREASTACK") + print("exited.info Exited containers can be started with " + "`docker container start [--attach] `") + print("dead.label DEAD") + print("dead.draw AREASTACK") + print("dead.warning 1") + print("dead.info Dead containers can be started with " + "`docker container start `") + else: + print_containers_status(client) - try: - import docker - client = docker.from_env() - if mode == "autoconf": - client.ping() - print('yes') - sys.exit(0) - except Exception as e: - print(f'no ({e})') - if mode == "autoconf": - sys.exit(0) - sys.exit(1) - if mode == "suggest": - print("cpu") - print("images") - print("memory") - print("network") - print("status") - print("volumes") - sys.exit(0) +def containers(client, mode): + if mode == "config": + print("graph_title Docker containers") + print("graph_vlabel containers") + print("graph_category virtualization") + print("containers_quantity.label Containers") + else: + print('containers_quantity.value', len(client.containers)) - client = ClientWrapper(client, - exclude_re=os.getenv('EXCLUDE_CONTAINER_NAME')) - if wildcard == "status": - if mode == "config": - print("graph_title Docker status") - print("graph_vlabel containers") - print("graph_category virtualization") - print("graph_total All containers") - print("running.label RUNNING") - print("running.draw AREASTACK") - print("running.info Running containers can be manipulated with " - "`docker container [attach|kill|logs|pause|restart|stop] ` or " - "commands run in them with `docker container exec " - "[--detach|--interactive,--privileged,--tty] `" - ) - print("unhealthy.label UNHEALTHY") - print("unhealthy.draw AREASTACK") - print("unhealthy.warning 1") - print("unhealthy.info Unhealthy containers can be restarted with " - "`docker container restart `") - print("paused.label PAUSED") - print("paused.draw AREASTACK") - print("paused.info Paused containers can be resumed with " - "`docker container unpause `") - print("created.label CREATED") - print("created.draw AREASTACK") - print("created.info New containers can be created with " - "`docker container create --name ` or " - "`docker container run --name `") - print("restarting.label RESTARTING") - print("restarting.draw AREASTACK") - print("restarting.info Containers can be restarted with " - "`docker container restart `") - print("removing.label REMOVING") - print("removing.draw AREASTACK") - print("removing.info Containers can be removed with " - "`docker container rm `") - print("exited.label EXITED") - print("exited.draw AREASTACK") - print("exited.info Exited containers can be started with " - "`docker container start [--attach] `") - print("dead.label DEAD") - print("dead.draw AREASTACK") - print("dead.warning 1") - print("dead.info Dead containers can be started with " - "`docker container start `") - else: - print_containers_status(client) - elif wildcard == "containers": - if mode == "config": - print("graph_title Docker containers") - print("graph_vlabel containers") - print("graph_category virtualization") - print("containers_quantity.label Containers") - else: - print('containers_quantity.value', len(client.containers)) - elif wildcard == "images": - if mode == "config": - print("graph_title Docker images") - print("graph_vlabel images") - print("graph_category virtualization") - print("graph_total All images") - print("intermediate_quantity.label Intermediate images") - print("intermediate_quantity.draw AREASTACK") - print("intermediate_quantity.info All unused images can be deleted with " - "`docker image prune --all`") - print("images_quantity.label Images") - print("images_quantity.draw AREASTACK") - print("images_quantity.info Images can be used in containers with " - "`docker container create --name ` or " - "`docker container run --name `") - print("dangling_quantity.label Dangling images") - print("dangling_quantity.draw AREASTACK") - print("dangling_quantity.info Dangling images can be deleted with " - "`docker image prune`" - "or tagged with `docker image tag `") - print("dangling_quantity.warning 10") - else: - print_images_count(client) - elif wildcard == "volumes": - if mode == "config": - print("graph_title Docker volumes") - print("graph_vlabel volumes") - print("graph_category virtualization") - print("volumes_quantity.label Volumes") - print("volumes_quantity.draw AREASTACK") - print("volumes_quantity.info Unused volumes can be deleted with " - "`docker volume prune`") - else: - print('volumes_quantity.value', len(client.volumes)) - print('volumes_quantity.extinfo', ', '.join(volume_summary(v) for v in client.volumes)) - elif wildcard == "cpu": - if mode == "config": - graphlimit = str(os.cpu_count() * 100) - print("graph_title Docker containers CPU usage") - print("graph_args --base 1000 -r --lower-limit 0 --upper-limit " + graphlimit) - print("graph_scale no") - print("graph_period second") - print("graph_vlabel CPU usage (%)") - print("graph_category virtualization") - print("graph_info This graph shows docker container CPU usage.") - print("graph_total Total CPU usage") - for container in client.all_containers: - print("{}.label {}".format(container.name, container.name)) - print("{}.draw AREASTACK".format(container.name)) - print("{}.info {}".format(container.name, container_attributes(container))) - else: - print_containers_cpu(client) - elif wildcard == "memory": - if mode == "config": - print("graph_title Docker containers memory usage") - print("graph_args --base 1024 -l 0") - print("graph_vlabel Bytes") - print("graph_category virtualization") - print("graph_info This graph shows docker container memory usage.") - print("graph_total Total memory usage") - for container in client.all_containers: - print("{}.label {}".format(container.name, container.name)) - print("{}.draw AREASTACK".format(container.name)) - print("{}.info {}".format(container.name, container_attributes(container))) - else: - print_containers_memory(client) - elif wildcard == "network": +def images(client, mode): + if mode == "config": + print("graph_title Docker images") + print("graph_vlabel images") + print("graph_category virtualization") + print("graph_total All images") + print("intermediate_quantity.label Intermediate images") + print("intermediate_quantity.draw AREASTACK") + print("intermediate_quantity.info All unused images can be deleted with " + "`docker image prune --all`") + print("images_quantity.label Images") + print("images_quantity.draw AREASTACK") + print("images_quantity.info Images can be used in containers with " + "`docker container create --name ` or " + "`docker container run --name `") + print("dangling_quantity.label Dangling images") + print("dangling_quantity.draw AREASTACK") + print("dangling_quantity.info Dangling images can be deleted with " + "`docker image prune`" + "or tagged with `docker image tag `") + print("dangling_quantity.warning 10") + else: + print_images_count(client) + + +def volumes(client, mode): + if mode == "config": + print("graph_title Docker volumes") + print("graph_vlabel volumes") + print("graph_category virtualization") + print("volumes_quantity.label Volumes") + print("volumes_quantity.draw AREASTACK") + print("volumes_quantity.info Unused volumes can be deleted with " + "`docker volume prune`") + else: + print('volumes_quantity.value', len(client.volumes)) + print('volumes_quantity.extinfo', ', '.join(volume_summary(v) for v in client.volumes)) + + +def cpu(client, mode): + if mode == "config": + graphlimit = str(os.cpu_count() * 100) + print("graph_title Docker containers CPU usage") + print("graph_args --base 1000 -r --lower-limit 0 --upper-limit " + graphlimit) + print("graph_scale no") + print("graph_period second") + print("graph_vlabel CPU usage (%)") + print("graph_category virtualization") + print("graph_info This graph shows docker container CPU usage.") + print("graph_total Total CPU usage") + for container in client.all_containers: + print("{}.label {}".format(container.name, container.name)) + print("{}.draw AREASTACK".format(container.name)) + print("{}.info {}".format(container.name, container_attributes(container))) + else: + print_containers_cpu(client) + + +def network(client, mode): if mode == "config": print("graph_title Docker containers network usage") print("graph_args --base 1024 -l 0") @@ -492,5 +463,75 @@ def main(): print_containers_network(client) +def memory(client, mode): + if mode == "config": + print("graph_title Docker containers memory usage") + print("graph_args --base 1024 -l 0") + print("graph_vlabel Bytes") + print("graph_category virtualization") + print("graph_info This graph shows docker container memory usage.") + print("graph_total Total memory usage") + for container in client.all_containers: + print("{}.label {}".format(container.name, container.name)) + print("{}.draw AREASTACK".format(container.name)) + print("{}.info {}".format(container.name, container_attributes(container))) + else: + print_containers_memory(client) + + +def main(): + series = [ + 'cpu', + 'images', + 'memory', + 'network', + 'status', + 'volumes', + ] + + try: + mode = sys.argv[1] + except IndexError: + mode = "" + wildcard = sys.argv[0].split("docker_")[1].split("_")[0] + + try: + import docker + client = docker.from_env() + if mode == "autoconf": + client.ping() + print('yes') + sys.exit(0) + except Exception as e: + print(f'no ({e})') + if mode == "autoconf": + sys.exit(0) + sys.exit(1) + + if mode == "suggest": + # The multigraph covers all other graphs, + # so we only need to suggest one + print("multi") + sys.exit(0) + + client = ClientWrapper(client, + exclude_re=os.getenv('EXCLUDE_CONTAINER_NAME')) + + if wildcard in series: + # dereference the function name by looking in the globals() + # this assumes that the function name matches the series name exactly + # if this were to change, a different approach would be needed, + # most likely using a Dict of series name string to callable + globals()[wildcard](client, mode) + elif wildcard == 'multi': + for s in series: + print(f'multigraph docker_{s}') + # ditto + globals()[s](client, mode) + else: + print(f'unknown series ({wildcard})', file=sys.stderr) + sys.exit(1) + + if __name__ == '__main__': main()