2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
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
):
44 res
= "%s%s" % (gen_pos(*master_index
[parent
]), pos
)
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
53 brick_type
, path
= line
[:2]
55 brick_type
, subtype
= brick_type
.split(".")
58 parent
, pos
, uid
= path
.split(".")
60 if brick_type
== "master":
61 master_index
[uid
] = (parent
, pos
)
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.
70 for brick_type
, bricks
in temp
.iteritems():
73 bricks
, key
=lambda b
: gen_pos(b
[0], b
[1]).rjust(len(master_index
) + 1, ' ')):
76 name
= "%s %s" % (brick
[2], counter
)
77 res
.setdefault(brick_type
, {})[name
] = brick
[3]
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":
93 def check_tinkerforge_master(item
, params
, parsed
):
94 if 'master' in parsed
and item
in parsed
['master']:
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
)
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
115 return check_levels(reading
, 'brightness', params
, unit
="lx", infoname
="Brightness")
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]
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
:
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
]]
139 periods
= [((0, 0), (24, 0))]
140 reading
= int(parsed
['motion'][item
][0])
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,
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,
185 'service_description': "Motion Detector %s",