updated check_mk_wrapper.py
9b0d362d
Hari Sekhon
committed
1 changed file
check_mk_wrapper.py
/check_mk_wrapper.py-7+22
/check_mk_wrapper.py
Add comment 16
Add comment 17 """
Add comment 18
Add comment 19 Minus  Wrapper program to convert any Nagios Plugin to Check MK format for immediate re-use of all existing
Add comment 19 Plus  Wrapper program to convert any Nagios Plugin to Check MK local check format for immediate re-use of all existing
Add comment 20 Nagios Plugins from the Advanced Nagios Plugins Collection or elsewhere.
Add comment 21
Add comment 22 Usage:
Add comment 32
Add comment 33 statuscode is the exit code
Add comment 34
Add comment 35 Minus  Check MK format:
Add comment 35 Plus  Check MK format (http://mathias-kettner.de/checkmk_localchecks.html):
Add comment 36
Add comment 37 statuscode name perfdata message
Add comment 38
Add comment 39 Plus  Since the format of Check MK local checks doesn't permit spaces in anything except the final message data, any spaces
Add comment 40 Plus  in the check name or perfdata will be changed to underscores and multiple perfdata items will result in multiple lines
Add comment 41 Plus  of output to separate out each perfdata item one per line
Add comment 42 Plus  
Add comment 39 43 """
Add comment 40 44
Add comment 41 45 from __future__ import absolute_import
Add comment 44 48 #from __future__ import unicode_literals
Add comment 45 49
Add comment 46 50 import os
Add comment 51 Plus  import re
Add comment 47 52 import sys
Add comment 48 53 import traceback
Add comment 49 54 srcdir = os.path.abspath(os.path.dirname(__file__))
Add comment 51 56 sys.path.append(libdir)
Add comment 52 57 try:
Add comment 53 58 # pylint: disable=wrong-import-position
Add comment 54 Minus   from harisekhon.utils import ERRORS, isFloat
Add comment 59 Plus   from harisekhon.utils import log, ERRORS, isFloat
Add comment 55 60 from csv_wrapper import CSVWrapper
Add comment 56 61 except ImportError as _:
Add comment 57 62 print(traceback.format_exc(), end='')
Add comment 58 63 sys.exit(4)
Add comment 59 64
Add comment 60 65 __author__ = 'Hari Sekhon'
Add comment 61 Minus  __version__ = '0.1'
Add comment 66 Plus  __version__ = '0.2'
Add comment 62 67
Add comment 63 68
Add comment 64 69 class CheckMKWrapper(CSVWrapper):
Add comment 69 74 # Python 3.x
Add comment 70 75 # super().__init__()
Add comment 71 76 self.name = None
Add comment 77 Plus   self.space_regex = re.compile(r'\s+')
Add comment 72 78
Add comment 73 79 def add_options(self):
Add comment 74 80 self.add_opt('-n', '--name', metavar='<check_name>',
Add comment 88 94 break
Add comment 89 95 if not self.name:
Add comment 90 96 self.usage('--name not defined')
Add comment 97 Plus   self.name = self.space_regex.sub('_', self.name)
Add comment 98 Plus   log.info('name = %s', self.name)
Add comment 91 99
Add comment 92 100 # handles if you call plugin with explicit numbered interpreter eg. python2.7 etc...
Add comment 93 101 @staticmethod
Add comment 98 106 return False
Add comment 99 107
Add comment 100 108 def output(self):
Add comment 101 Minus   output = '"{status}" "{name}" "{perfdata}" "{message}"'\
Add comment 102 Minus   .format(status=self.status,
Add comment 109 Plus   perfdata = ""
Add comment 110 Plus   for key, val in zip(self.headers[2:], self.perfdata):
Add comment 111 Plus   perfdata += '{key}={val}|'.format(key=self.space_regex.sub('_', key),
Add comment 112 Plus   val=val)
Add comment 113 Plus   perfdata = perfdata.rstrip('|')
Add comment 114 Plus   if not perfdata:
Add comment 115 Plus   perfdata = '-'
Add comment 116 Plus   output = '{status} {name} {perfdata} {message}'\
Add comment 117 Plus   .format(status=ERRORS[self.status],
Add comment 103 118 name=self.name,
Add comment 104 Minus   perfdata=' '.join(self.perfdata),
Add comment 119 Plus   perfdata=perfdata,
Add comment 105 120 message=self.message)
Add comment 106 121 print(output)
Add comment 107 122