Refactoring: Changed all check parameters starting with an 'o' to the new rulespec...
[check_mk.git] / checks / tinkerforge
blob5a7c808b7b23e51bfe16db16297ca385bc416d06
1 #!/usr/bin/python
2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
9 # | |
10 # | Copyright Mathias Kettner 2016 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.
27 # <<<tinkerforge:sep(44)>>>
28 # temperature,6QHSgJ.a.tiq,2181
29 # humidity,6QHSgJ.c.ugg,250
30 # ambient,6JLy11.c.uKA,124
32 # based on customers investigation
33 tinkerforge_humidity_default_levels = (35, 40, 50, 55)
36 def parse_tinkerforge(info):
37 # biggest trouble here is generating sensible item names as tho ones
38 # provided to us are simply random-generated
40 def gen_pos(parent, pos):
41 if parent == "0":
42 res = ""
43 else:
44 res = "%s%s" % (gen_pos(*master_index[parent]), pos)
45 return res
47 # first, go through all readings and group them by brick(let) type.
48 # On this opportunity, also create an index of master bricks which we need
49 # to query the stack topology
50 master_index = {}
51 temp = {}
52 for line in info:
53 brick_type, path = line[:2]
54 try:
55 brick_type, subtype = brick_type.split(".")
56 except ValueError:
57 subtype = None
58 parent, pos, uid = path.split(".")
60 if brick_type == "master":
61 master_index[uid] = (parent, pos)
63 values = line[2:]
64 temp.setdefault(brick_type, []).append((parent, pos, subtype, values))
66 # now go through all the bricks again and sort them within each brick_type-group by their
67 # position in the topology. items higher up in the topology come first, and among
68 # "siblings" they are sorted by the port on this host.
69 res = {}
70 for brick_type, bricks in temp.iteritems():
71 counter = 1
72 for brick in sorted(
73 bricks, key=lambda b: gen_pos(b[0], b[1]).rjust(len(master_index) + 1, ' ')):
74 name = str(counter)
75 if brick[2]:
76 name = "%s %s" % (brick[2], counter)
77 res.setdefault(brick_type, {})[name] = brick[3]
78 counter += 1
80 return res
83 def inventory_tinkerforge(brick_type, parsed):
84 for path in parsed.get(brick_type, {}).keys():
85 if brick_type == "humidity":
86 yield path, "tinkerforge_humidity_default_levels"
87 elif brick_type == "ambient":
88 yield path, None
89 else:
90 yield path, {}
93 def check_tinkerforge_master(item, params, parsed):
94 if 'master' in parsed and item in parsed['master']:
95 try:
96 voltage, current, chip_temp = parsed['master'][item]
97 yield 0, "%.1f mV" % float(voltage)
98 yield 0, "%.1f mA" % float(current)
99 yield check_temperature(float(chip_temp) / 10.0, params, "tinkerforge_%s" % item)
100 except:
101 yield 2, parsed['master'][item][0], []
104 def check_tinkerforge_temperature(item, params, parsed):
105 if 'temperature' in parsed and item in parsed['temperature']:
106 reading = float(parsed['temperature'][item][0]) / 100.0
107 return check_temperature(reading, params, "tinkerforge_%s" % item)
110 def check_tinkerforge_ambient(item, params, parsed):
111 if 'ambient' in parsed and item in parsed['ambient']:
112 reading = float(parsed['ambient'][item][0]) / 100.0
113 if not params:
114 params = None
115 return check_levels(reading, 'brightness', params, unit="lx")
118 def check_tinkerforge_humidity(item, params, parsed):
119 if 'humidity' in parsed and item in parsed['humidity']:
120 return check_humidity(float(parsed['humidity'][item][0]) / 10.0, params)
123 def check_tinkerforge_motion(item, params, parsed):
124 def test_in_period(time, periods):
125 time_mins = time[0] * 60 + time[1]
126 for per in periods:
127 per_mins_low = per[0][0] * 60 + per[0][1]
128 per_mins_high = per[1][0] * 60 + per[1][1]
129 if time_mins >= per_mins_low and time_mins < per_mins_high:
130 return True
131 return False
133 weekdays = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
134 if 'motion' in parsed and item in parsed['motion']:
135 today = time.localtime()
136 if 'time_periods' in params:
137 periods = params['time_periods'][weekdays[today.tm_wday]]
138 else:
139 periods = [((0, 0), (24, 0))]
140 reading = int(parsed['motion'][item][0])
141 if reading == 1:
142 status = 1 if test_in_period((today.tm_hour, today.tm_min), periods) else 0
143 return status, "Motion detected", [('motion', reading)]
144 return 0, "No motion detected", [('motion', reading)]
147 check_info['tinkerforge'] = {
148 'inventory_function': lambda parsed: inventory_tinkerforge('master', parsed),
149 'check_function': check_tinkerforge_master,
150 'parse_function': parse_tinkerforge,
151 'service_description': "Master %s"
154 check_info['tinkerforge.temperature'] = {
155 'inventory_function': lambda parsed: inventory_tinkerforge('temperature', parsed),
156 'check_function': check_tinkerforge_temperature,
157 'has_perfdata': True,
158 'service_description': "Temperature %s",
159 'group': 'temperature',
160 'includes': ['temperature.include']
163 check_info['tinkerforge.ambient'] = {
164 'inventory_function': lambda parsed: inventory_tinkerforge('ambient', parsed),
165 'check_function': check_tinkerforge_ambient,
166 'has_perfdata': True,
167 'group': 'brightness',
168 'service_description': "Ambient Light %s"
171 check_info['tinkerforge.humidity'] = {
172 'inventory_function': lambda parsed: inventory_tinkerforge('humidity', parsed),
173 'check_function': check_tinkerforge_humidity,
174 'has_perfdata': True,
175 'group': 'humidity',
176 'service_description': "Humidity %s",
177 'includes': ["humidity.include"],
180 check_info['tinkerforge.motion'] = {
181 'inventory_function': lambda parsed: inventory_tinkerforge('motion', parsed),
182 'check_function': check_tinkerforge_motion,
183 'has_perfdata': True,
184 'group': 'motion',
185 'service_description': "Motion Detector %s",