dockerd.chart.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # -*- coding: utf-8 -*-
  2. # Description: docker netdata python.d module
  3. # Author: Kévin Darcel (@tuxity)
  4. try:
  5. import docker
  6. HAS_DOCKER = True
  7. except ImportError:
  8. HAS_DOCKER = False
  9. from distutils.version import StrictVersion
  10. from bases.FrameworkServices.SimpleService import SimpleService
  11. # charts order (can be overridden if you want less charts, or different order)
  12. ORDER = [
  13. 'running_containers',
  14. 'healthy_containers',
  15. 'unhealthy_containers'
  16. ]
  17. CHARTS = {
  18. 'running_containers': {
  19. 'options': [None, 'Number of running containers', 'containers', 'running containers',
  20. 'docker.running_containers', 'line'],
  21. 'lines': [
  22. ['running_containers', 'running']
  23. ]
  24. },
  25. 'healthy_containers': {
  26. 'options': [None, 'Number of healthy containers', 'containers', 'healthy containers',
  27. 'docker.healthy_containers', 'line'],
  28. 'lines': [
  29. ['healthy_containers', 'healthy']
  30. ]
  31. },
  32. 'unhealthy_containers': {
  33. 'options': [None, 'Number of unhealthy containers', 'containers', 'unhealthy containers',
  34. 'docker.unhealthy_containers', 'line'],
  35. 'lines': [
  36. ['unhealthy_containers', 'unhealthy']
  37. ]
  38. }
  39. }
  40. MIN_REQUIRED_VERSION = '3.2.0'
  41. class Service(SimpleService):
  42. def __init__(self, configuration=None, name=None):
  43. SimpleService.__init__(self, configuration=configuration, name=name)
  44. self.order = ORDER
  45. self.definitions = CHARTS
  46. self.client = None
  47. def check(self):
  48. if not HAS_DOCKER:
  49. self.error("'docker' package is needed to use dockerd module")
  50. return False
  51. if StrictVersion(docker.__version__) < StrictVersion(MIN_REQUIRED_VERSION):
  52. self.error("installed 'docker' package version {0}, minimum required version {1}, please upgrade".format(
  53. docker.__version__,
  54. MIN_REQUIRED_VERSION,
  55. ))
  56. return False
  57. self.client = docker.DockerClient(base_url=self.configuration.get('url', 'unix://var/run/docker.sock'))
  58. try:
  59. self.client.ping()
  60. except docker.errors.APIError as error:
  61. self.error(error)
  62. return False
  63. return True
  64. def get_data(self):
  65. data = dict()
  66. data['running_containers'] = len(self.client.containers.list(sparse=True))
  67. data['healthy_containers'] = len(self.client.containers.list(filters={'health': 'healthy'}, sparse=True))
  68. data['unhealthy_containers'] = len(self.client.containers.list(filters={'health': 'unhealthy'}, sparse=True))
  69. return data or None