added check_docker_networks.py
cfe7c838
Hari Sekhon
committed
1 changed file
check_docker_networks.py
/check_docker_networks.py+83
/check_docker_networks.py
Add comment 1 Plus  #!/usr/bin/env python
Add comment 2 Plus  # coding=utf-8
Add comment 3 Plus  # vim:ts=4:sts=4:sw=4:et
Add comment 4 Plus  #
Add comment 5 Plus  # Author: Hari Sekhon
Add comment 6 Plus  # Date: 2018-03-05 19:10:02 +0000 (Mon, 05 Mar 2018)
Add comment 7 Plus  #
Add comment 8 Plus  # https://github.com/harisekhon/nagios-plugins
Add comment 9 Plus  #
Add comment 10 Plus  # License: see accompanying Hari Sekhon LICENSE file
Add comment 11 Plus  #
Add comment 12 Plus  # If you're using my code you're welcome to connect with me on LinkedIn
Add comment 13 Plus  # and optionally send me feedback to help steer this or other code I publish
Add comment 14 Plus  #
Add comment 15 Plus  # https://www.linkedin.com/in/harisekhon
Add comment 16 Plus  #
Add comment 17 Plus  
Add comment 18 Plus  """
Add comment 19 Plus  
Add comment 20 Plus  Nagios Plugin to check the number of Docker networks via the Docker API
Add comment 21 Plus  
Add comment 22 Plus  Optional thresholds can be applied and perfdata is output for graphing the number of networks over time
Add comment 23 Plus  
Add comment 24 Plus  This is less efficient than the other adjacent docker plugins as it must retrieve a list
Add comment 25 Plus  which is an O(n) operation as there is no counter
Add comment 26 Plus  
Add comment 27 Plus  Supports TLS with similar options to official 'docker' command
Add comment 28 Plus  
Add comment 29 Plus  Tested on Docker 18.02
Add comment 30 Plus  
Add comment 31 Plus  """
Add comment 32 Plus  
Add comment 33 Plus  from __future__ import absolute_import
Add comment 34 Plus  from __future__ import division
Add comment 35 Plus  from __future__ import print_function
Add comment 36 Plus  from __future__ import unicode_literals
Add comment 37 Plus  
Add comment 38 Plus  import os
Add comment 39 Plus  import sys
Add comment 40 Plus  import traceback
Add comment 41 Plus  srcdir = os.path.abspath(os.path.dirname(__file__))
Add comment 42 Plus  libdir = os.path.join(srcdir, 'pylib')
Add comment 43 Plus  sys.path.append(libdir)
Add comment 44 Plus  try:
Add comment 45 Plus   # pylint: disable=wrong-import-position
Add comment 46 Plus   from harisekhon.utils import log
Add comment 47 Plus   from harisekhon import DockerNagiosPlugin
Add comment 48 Plus  except ImportError as _:
Add comment 49 Plus   print(traceback.format_exc(), end='')
Add comment 50 Plus   sys.exit(4)
Add comment 51 Plus  
Add comment 52 Plus  __author__ = 'Hari Sekhon'
Add comment 53 Plus  __version__ = '0.1'
Add comment 54 Plus  
Add comment 55 Plus  
Add comment 56 Plus  class CheckDockerNetworks(DockerNagiosPlugin):
Add comment 57 Plus  
Add comment 58 Plus   def __init__(self):
Add comment 59 Plus   # Python 2.x
Add comment 60 Plus   super(CheckDockerNetworks, self).__init__()
Add comment 61 Plus   # Python 3.x
Add comment 62 Plus   # super().__init__()
Add comment 63 Plus   self.msg = 'Docker msg not defined yet'
Add comment 64 Plus  
Add comment 65 Plus   def add_options(self):
Add comment 66 Plus   super(CheckDockerNetworks, self).add_options()
Add comment 67 Plus   self.add_thresholds()
Add comment 68 Plus  
Add comment 69 Plus   def process_options(self):
Add comment 70 Plus   super(CheckDockerNetworks, self).process_options()
Add comment 71 Plus   self.validate_thresholds(integer=True, positive=True, optional=True)
Add comment 72 Plus  
Add comment 73 Plus   def check(self, client):
Add comment 74 Plus   log.info('running Docker info')
Add comment 75 Plus   networks = len(client.networks.list())
Add comment 76 Plus   self.msg = 'Docker networks = {}'.format(networks)
Add comment 77 Plus   self.check_thresholds(networks)
Add comment 78 Plus   self.msg += ' | docker_networks={}{}'.format(networks, self.get_perf_thresholds())
Add comment 79 Plus  
Add comment 80 Plus  
Add comment 81 Plus  if __name__ == '__main__':
Add comment 82 Plus   CheckDockerNetworks().main()
Add comment 83 Plus