mirror of
https://github.com/munin-monitoring/contrib.git
synced 2025-07-21 18:41:03 +00:00
Improved docker_ performance when getting CPU/Memory stats
Added parallel processing using python's muliprocessing module to fork a new process each for each docker container when gathering stats. This vastly improves performance since using a for loop the script blocks until the stats are returned.
This commit is contained in:
parent
5392e84b8a
commit
fcd2af7b07
1 changed files with 22 additions and 5 deletions
|
@ -50,6 +50,7 @@ https://docs.docker.com/compose/reference/envvars/):
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import docker
|
import docker
|
||||||
|
from multiprocessing import Process, Queue
|
||||||
|
|
||||||
|
|
||||||
def print_containers_status(client):
|
def print_containers_status(client):
|
||||||
|
@ -84,9 +85,26 @@ def print_containers_status(client):
|
||||||
print('dead.value', dead)
|
print('dead.value', dead)
|
||||||
|
|
||||||
|
|
||||||
def print_containers_cpu(client):
|
def get_container_stats(container, q):
|
||||||
|
q.put(container.stats(stream=False))
|
||||||
|
|
||||||
|
|
||||||
|
def parallel_container_stats(client):
|
||||||
|
proc_list = []
|
||||||
|
stats = {}
|
||||||
for container in client.containers.list():
|
for container in client.containers.list():
|
||||||
stats = container.stats(stream=False)
|
q = Queue()
|
||||||
|
p = Process(target=get_container_stats, args=(container, q))
|
||||||
|
proc_list.append({'proc': p, 'queue': q, 'container': container})
|
||||||
|
p.start()
|
||||||
|
for proc in proc_list:
|
||||||
|
proc['proc'].join()
|
||||||
|
stats[proc['container']] = proc['queue'].get()
|
||||||
|
return stats.items()
|
||||||
|
|
||||||
|
|
||||||
|
def print_containers_cpu(client):
|
||||||
|
for container, stats in parallel_container_stats(client):
|
||||||
cpu_count = len(stats["cpu_stats"]["cpu_usage"]["percpu_usage"])
|
cpu_count = len(stats["cpu_stats"]["cpu_usage"]["percpu_usage"])
|
||||||
cpu_percent = 0.0
|
cpu_percent = 0.0
|
||||||
cpu_delta = float(stats["cpu_stats"]["cpu_usage"]["total_usage"]) \
|
cpu_delta = float(stats["cpu_stats"]["cpu_usage"]["total_usage"]) \
|
||||||
|
@ -99,9 +117,8 @@ def print_containers_cpu(client):
|
||||||
|
|
||||||
|
|
||||||
def print_containers_memory(client):
|
def print_containers_memory(client):
|
||||||
for container in client.containers.list():
|
for container, stats in parallel_container_stats(client):
|
||||||
stats = container.stats(stream=False)['memory_stats']
|
print(container.name + '.value', stats['memory_stats']['stats']['total_rss'])
|
||||||
print(container.name + '.value', stats['stats']['total_rss'])
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue