Skip to content

Commit

Permalink
add exceptions for containers in restarting state
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Wellauer committed Dec 22, 2020
1 parent 0ac627b commit 40c2743
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions docker_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

__author__ = 'El Acheche Anis'
__license__ = 'GPLv3'
__version__ = '2018-06-04'
__version__ = '2020-12-22'


def get_ct_stats(container):
Expand All @@ -36,23 +36,41 @@ def get_ct_stats(container):

def get_mem_pct(stats):
'''Get a container memory usage in %'''
usage = stats['memory_stats']['usage']
limit = stats['memory_stats']['limit']
return round(usage * 100 / limit, 2)
try:
usage = stats['memory_stats']['usage']
except KeyError:
usage = 0
try:
limit = stats['memory_stats']['limit']
except KeyError:
limit = 0
try:
return round(usage * 100 / limit, 2)
except ZeroDivisionError:
return 0


def get_cpu_pct(stats):
'''Get a container cpu usage in %'''
cpu_delta = stats['cpu_stats']['cpu_usage']['total_usage'] - \
stats['precpu_stats']['cpu_usage']['total_usage']
system_delta = stats['cpu_stats']['system_cpu_usage'] - \
stats['precpu_stats']['system_cpu_usage']
try:
cpu_delta = stats['cpu_stats']['cpu_usage']['total_usage'] - \
stats['precpu_stats']['cpu_usage']['total_usage']
except KeyError:
cpu_delta = 0
try:
system_delta = stats['cpu_stats']['system_cpu_usage'] - \
stats['precpu_stats']['system_cpu_usage']
except KeyError:
system_delta = 0
try:
online_cpus = stats['cpu_stats']['online_cpus']
except KeyError:
online_cpus = len([item
for item in stats['cpu_stats']['percpu_usage']
if item > 0])
try:
online_cpus = len([item
for item in stats['cpu_stats']['percpu_usage']
if item > 0])
except KeyError:
online_cpus = 0
if cpu_delta > 0 and system_delta > 0:
return (cpu_delta / system_delta) * online_cpus * 100
return 0.0
Expand All @@ -68,7 +86,6 @@ def get_net_io(stats):
net_out = stats['networks']['eth0']['tx_bytes']
except KeyError:
net_out = 0

return net_in, net_out


Expand All @@ -79,10 +96,14 @@ def get_disk_io(stats):
disk_in = disk[0]['value']
except IndexError:
disk_in = 0
except TypeError:
disk_in = 0
try:
disk_out = disk[1]['value']
except IndexError:
disk_out = 0
except TypeError:
disk_out = 0
return disk_in, disk_out


Expand Down

0 comments on commit 40c2743

Please sign in to comment.