timdaman / check_docker

Nagios plugin to check docker containers
GNU General Public License v3.0
152 stars 60 forks source link

KeyError when checking cpu or memory #51

Closed MostFrabjous closed 5 years ago

MostFrabjous commented 5 years ago

I just tried check_docker the first time. If I use the checks for cpu or mem, I get a KeyError.

Error Logs

--cpu

Traceback (most recent call last):
  File "check_docker.py", line 929, in main
    [x.result() for x in futures.as_completed(threads)]
  File "check_docker.py", line 929, in <listcomp>
    [x.result() for x in futures.as_completed(threads)]
  File "/usr/lib64/python3.4/concurrent/futures/_base.py", line 395, in result
    return self.__get_result()
  File "/usr/lib64/python3.4/concurrent/futures/_base.py", line 354, in __get_result
    raise self._exception
  File "/usr/lib64/python3.4/concurrent/futures/thread.py", line 54, in run
    result = self.fn(*self.args, **self.kwargs)
  File "check_docker.py", line 423, in wrapper
    func(container, *args, **kwargs)
  File "check_docker.py", line 672, in check_cpu
    usage = calculate_cpu_capacity_precentage(info=info, stats=stats)
  File "check_docker.py", line 636, in calculate_cpu_capacity_precentage
    num_cpus = len(stats['cpu_stats']['cpu_usage']['percpu_usage'])
KeyError: 'percpu_usage'

--mem

Traceback (most recent call last):
  File "check_docker.py", line 929, in main
    [x.result() for x in futures.as_completed(threads)]
  File "check_docker.py", line 929, in <listcomp>
    [x.result() for x in futures.as_completed(threads)]
  File "/usr/lib64/python3.4/concurrent/futures/_base.py", line 395, in result
    return self.__get_result()
  File "/usr/lib64/python3.4/concurrent/futures/_base.py", line 354, in __get_result
    raise self._exception
  File "/usr/lib64/python3.4/concurrent/futures/thread.py", line 54, in run
    result = self.fn(*self.args, **self.kwargs)
  File "check_docker.py", line 423, in wrapper
    func(container, *args, **kwargs)
  File "check_docker.py", line 513, in check_memory
    adjusted_usage = inspection['memory_stats']['usage'] - inspection['memory_stats']['stats']['total_cache']
KeyError: 'usage'
MostFrabjous commented 5 years ago

This happens only if the container ist not started. In my case the state was Restarting

timdaman commented 5 years ago

Interesting. I just took a peek and certainly I see why. Surprised no-one reported this before because it is a pretty obvious bug. I think I have a fix for it in this branch. Tell me if it works for you. https://github.com/timdaman/check_docker/tree/better_status

timdaman commented 5 years ago

I have merged a fix for this issue in 2.0.8