dockerd.chart.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 bases.FrameworkServices.SimpleService import SimpleService
  10. # default module values (can be overridden per job in `config`)
  11. # update_every = 1
  12. priority = 60000
  13. retries = 60
  14. # charts order (can be overridden if you want less charts, or different order)
  15. ORDER = [
  16. 'running_containers',
  17. 'healthy_containers',
  18. 'unhealthy_containers'
  19. ]
  20. CHARTS = {
  21. 'running_containers': {
  22. 'options': [None, 'Number of running containers', 'running containers', 'running containers',
  23. 'docker.running_containers', 'line'],
  24. 'lines': [
  25. ['running_containers', 'running']
  26. ]
  27. },
  28. 'healthy_containers': {
  29. 'options': [None, 'Number of healthy containers', 'healthy containers', 'healthy containers',
  30. 'docker.healthy_containers', 'line'],
  31. 'lines': [
  32. ['healthy_containers', 'healthy']
  33. ]
  34. },
  35. 'unhealthy_containers': {
  36. 'options': [None, 'Number of unhealthy containers', 'unhealthy containers', 'unhealthy containers',
  37. 'docker.unhealthy_containers', 'line'],
  38. 'lines': [
  39. ['unhealthy_containers', 'unhealthy']
  40. ]
  41. }
  42. }
  43. class Service(SimpleService):
  44. def __init__(self, configuration=None, name=None):
  45. SimpleService.__init__(self, configuration=configuration, name=name)
  46. self.order = ORDER
  47. self.definitions = CHARTS
  48. def check(self):
  49. if not HAS_DOCKER:
  50. self.error('\'docker\' package is needed to use docker.chart.py')
  51. return False
  52. self.client = docker.DockerClient(base_url=self.configuration.get('url', 'unix://var/run/docker.sock'))
  53. try:
  54. self.client.ping()
  55. except docker.errors.APIError as error:
  56. self.error(error)
  57. return False
  58. return True
  59. def get_data(self):
  60. data = dict()
  61. data['running_containers'] = len(self.client.containers.list(sparse=True))
  62. data['healthy_containers'] = len(self.client.containers.list(filters={'health': 'healthy'}, sparse=True))
  63. data['unhealthy_containers'] = len(self.client.containers.list(filters={'health': 'unhealthy'}, sparse=True))
  64. return data or None