-
Notifications
You must be signed in to change notification settings - Fork 506
/
adapter_check_mk.py
executable file
·127 lines (103 loc) · 3.99 KB
/
adapter_check_mk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python3
# vim:ts=4:sts=4:sw=4:et
#
# Author: Hari Sekhon
# Date: 2016-12-14 00:39:25 +0000 (Wed, 14 Dec 2016)
#
# https://github.com/HariSekhon/Nagios-Plugins
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn
# and optionally send me feedback to help steer this or other code I publish
#
# https://www.linkedin.com/in/HariSekhon
#
"""
Adapter program to convert any Nagios Plugin to Check MK local check format for immediate re-use of all existing
Nagios Plugins from the Advanced Nagios Plugins Collection or elsewhere.
Usage:
Put 'adapter_check_mk.py' at the front of any nagios plugin command line and it will call the plugin and
translate the output for you to Check MK format.
Alternatively you can feed it literal output from a nagios plugin combined with the --result <exitcode> switch.
Nagios Format:
message | perfdata
statuscode is the exit code
Check MK format (http://mathias-kettner.de/checkmk_localchecks.html):
statuscode name perfdata message
Since the format of Check MK local checks doesn't permit spaces in anything except the final message data, any spaces
in the check name or perfdata will be changed to underscores and multiple perfdata items will result in multiple lines
of output to separate out each perfdata item one per line
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
#from __future__ import unicode_literals
import os
import re
import sys
import traceback
srcdir = os.path.abspath(os.path.dirname(__file__))
libdir = os.path.join(srcdir, 'pylib')
sys.path.append(libdir)
try:
# pylint: disable=wrong-import-position
from harisekhon.utils import log, ERRORS, isFloat
from adapter_csv import AdapterCSV
except ImportError:
print(traceback.format_exc(), end='')
sys.exit(4)
__author__ = 'Hari Sekhon'
__version__ = '0.4'
class AdapterCheckMK(AdapterCSV):
def __init__(self):
# Python 2.x
super(AdapterCheckMK, self).__init__()
# Python 3.x
# super().__init__()
self.name = None
self.space_regex = re.compile(r'\s+')
def add_options(self):
self.add_opt('-n', '--name', metavar='<check_name>',
help='Name of the check (defaults to the basename of the plugin)')
super(AdapterCheckMK, self).add_options()
def process_options(self):
self.name = self.get_opt('name')
if not self.name:
for arg in self.args:
arg = os.path.basename(arg)
if arg and arg[0] != '-' and \
not self.is_interpreter(arg) and \
arg not in ERRORS and \
not isFloat(arg):
self.name = arg
break
if not self.name:
self.usage('--name not defined')
self.name = self.space_regex.sub('_', self.name)
log.info('name = %s', self.name)
# handles if you call plugin with explicit numbered interpreter eg. python2.7 etc...
@staticmethod
def is_interpreter(arg):
for prog in ('perl', 'python', 'ruby', 'groovy', 'jython'):
if arg[0:len(prog)] == prog:
return True
return False
def output(self):
perfdata = ""
for key, val in zip(self.headers[2:], self.perfdata):
perfdata += '{key}={val}|'.format(key=self.space_regex.sub('_', key),
val=val)
perfdata = perfdata.rstrip('|')
if not perfdata:
perfdata = '-'
output = '{status} {name} {perfdata} {message}'\
.format(status=ERRORS[self.status],
name=self.name,
perfdata=perfdata,
message=self.message)
print(output)
if __name__ == '__main__':
AdapterCheckMK().main()
# Must always exit zero for Geneos otherwise it won't take the output and will show as raw error
sys.exit(0)