Refactoring: Moved check parameters from unsorted.py to dedicated modules (CMK-1393)
[check_mk.git] / checks / mrpe
blobaa5343aaca6d42c01126e2c5063877d2e215c2c9
1 #!/usr/bin/python
2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
9 # | |
10 # | Copyright Mathias Kettner 2014 mk@mathias-kettner.de |
11 # +------------------------------------------------------------------+
13 # This file is part of Check_MK.
14 # The official homepage is at http://mathias-kettner.de/check_mk.
16 # check_mk is free software; you can redistribute it and/or modify it
17 # under the terms of the GNU General Public License as published by
18 # the Free Software Foundation in version 2. check_mk is distributed
19 # in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
20 # out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
21 # PARTICULAR PURPOSE. See the GNU General Public License for more de-
22 # tails. You should have received a copy of the GNU General Public
23 # License along with GNU Make; see the file COPYING. If not, write
24 # to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25 # Boston, MA 02110-1301 USA.
28 def inventory_mrpe(info):
29 import urllib
30 items = []
31 for line in info:
32 # New Linux agent sends (check_name) in first column. Stay
33 # compatible with MRPE versions not providing this info
34 if line[0].startswith("("):
35 item = line[1]
36 else:
37 item = line[0]
38 item = urllib.unquote(item)
39 items.append((item, None))
40 return items
43 def mrpe_parse_perfdata(perfinfo):
44 varname, valuetxt = perfinfo.split("=", 1)
45 values = valuetxt.split(";")
46 return tuple([varname] + values)
49 def check_mrpe(item, params, info):
50 import urllib
51 # This check is cluster-aware. An item might be found
52 # more than once. In that case we use the best of the
53 # multiple statuses.
55 best_state = None
57 for line in info:
58 if line[0].startswith("("):
59 check_name = line[0][1:-1]
60 line = line[1:]
61 else:
62 check_name = None
63 if urllib.unquote(line[0]) == item:
64 try:
65 state = int(line[1])
66 except:
67 state = None
69 # convert to original format by joining and replacing \1 back with \n
70 rest = " ".join(line[2:]).replace("\1", "\n")
71 # split into lines
72 lines = rest.split('\n')
73 # First line: OUTPUT|PERFDATA
74 parts = lines[0].split("|", 1)
75 output = [parts[0].strip()]
76 if state is None or state not in [0, 1, 2, 3]:
77 output[0] = "Invalid plugin status %s. Output is: %s" % (state, output[0])
78 state = 3
79 if len(parts) > 1:
80 perfdata = parts[1].strip().split()
81 else:
82 perfdata = []
84 # Further lines
85 now_comes_perfdata = False
86 for l in lines[1:]:
87 if now_comes_perfdata:
88 perfdata += l.split()
89 else:
90 parts = l.split("|", 1)
91 output.append(parts[0].strip())
92 if len(parts) > 1:
93 perfdata += parts[1].strip().split()
94 now_comes_perfdata = True
97 if best_state in [ None, 2 ] \
98 or (state < best_state and state != 2):
99 perf_parsed = []
100 for perfvalue in perfdata:
101 try:
102 perf_parsed.append(mrpe_parse_perfdata(perfvalue))
103 except:
104 pass
106 # name of check command needed for PNP to choose the correct template
107 if check_name:
108 perf_parsed.append(check_name)
109 best_result = state, "\n".join(output), perf_parsed
110 best_state = state
112 if best_state is None:
113 return (3, "Check output not found in output of MRPE")
114 return best_result
117 check_info["mrpe"] = {
118 'check_function': check_mrpe,
119 'inventory_function': inventory_mrpe,
120 'service_description': '%s',
121 'has_perfdata': True,