This file is indexed.

/usr/share/check_mk/checks/temperature.include is in check-mk-server 1.2.6p12-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2014             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# ails.  You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.


def fahrenheit_to_celsius(tempf):
    return (float(tempf) - 32) * (5.0 / 9.0)

def celsius_to_fahrenheit(tempc):
    return (float(tempc) * (9.0 / 5.0)) + 32

def from_celsius(tempc, unit):
    if unit == "f":
        return celsius_to_fahrenheit(tempc)
    elif unit == "k":
        return tempc + 273.15
    else:
        return tempc

def to_celsius(reading, unit):
    if type(reading) == tuple:
        return tuple([to_celsius(x, unit) for x in reading])
    elif unit == "f":
        return fahrenheit_to_celsius(reading)
    elif unit == "k":
        return reading - 273.15
    else:
        return reading

# Format number according to its datatype
def render_temp(n, output_unit):
    t = from_celsius(n, output_unit)
    if type(n) == int:
        return "%d" % t
    else:
        return "%.1f" % t

temp_unitsym = {
"c": "°C",
"f": "°F",
"k": "K",
}


# Checks Celsius temperature against crit/warn levels defined in params. temp must
# be int or float. Parameters:
# temp:              temperature reading of the device (per default interpreted as Celsius)
# params:            check parameters (pair or dict)
# dev_unit:          unit of the device reading if this is not Celsius ("f": Fahrenheit, "k": Kelvin)
# dev_levels:        warn/crit levels of the device itself, if any. In the same unit as temp (dev_unit)
# dev_level_lower:   lower warn/crit device levels
# dev_status:        temperature state (0, 1, 2) as the device reports it (if applies)
# Note: you must not specify dev_status and dev_levels at the same time!

def check_temperature(reading, params, dev_unit = "c",
                      dev_levels = None, dev_levels_lower = None,
                      dev_status = None, dev_status_name = None):


    def check_temp_levels(temp, warn, crit, warn_lower, crit_lower):
        if crit != None and temp >= crit:
            status = 2
        elif crit_lower != None and temp < crit_lower:
            status = 2
        elif warn != None and temp >= warn:
            status = 1
        elif warn_lower != None and temp < warn_lower:
            status = 1
        else:
            status = 0
        return status

    # min that deals correctly with None
    def minn(a,b):
        if a == None:
            return b
        elif b == None:
            return a
        else:
            return min(a,b)

    # Convert legacy tuple params into new dict
    if params == None or params == (None, None):
        params = {}
    elif type(params) == tuple:
        params = { "levels" : params }

    # Convert reading into Celsius
    input_unit = params.get("input_unit", dev_unit)
    output_unit = params.get("output_unit", "c")
    temp = to_celsius(reading, input_unit)

    # Prepare levels, dealing with user defined and device's own levels
    usr_levels = params.get("levels")
    usr_levels_lower = params.get("levels_lower")

    # Set all user levels to None. None means do not impose a level
    usr_warn       = None
    usr_crit       = None
    usr_warn_lower = None
    usr_crit_lower = None

    if usr_levels:
        usr_warn, usr_crit = usr_levels
    if usr_levels_lower:
        usr_warn_lower, usr_crit_lower = usr_levels_lower

    # Same for device levels
    dev_warn       = None
    dev_crit       = None
    dev_warn_lower = None
    dev_crit_lower = None

    if dev_levels:
        dev_warn, dev_crit = to_celsius(dev_levels, dev_unit)
    if dev_levels_lower:
        dev_warn_lower, dev_crit_lower = to_celsius(dev_levels_lower, dev_unit)

    # Decide which of user's and device's levels should be used according to the setting
    # "device_levels_handling". Result is four variables: {warn,crit}{,_lower}
    dlh = params.get("device_levels_handling", "usrdefault")

    # Ignore device's own levels
    if dlh == "usr":
        warn, crit, warn_lower, crit_lower = usr_warn, usr_crit, usr_warn_lower, usr_crit_lower
        dev_status = None

    # Only use device's levels, ignore yours
    elif dlh == "dev":
        warn, crit, warn_lower, crit_lower = dev_warn, dev_crit, dev_warn_lower, dev_crit_lower

    # The following four cases are all identical, if either *only* device levels or *only*
    # user levels exist (or no levels at all).

    # Use least critical of your and device's levels. If just one of both is defined,
    # take that. max deals correctly with None here. min does not work because None < int.
    # minn is a min that deals with None in the way we want here.
    elif dlh == "best":
        warn, crit = max(usr_warn, dev_warn), max(usr_crit, dev_crit)
        warn_lower, crit_lower = minn(usr_warn_lower, dev_warn_lower), minn(usr_crit_lower, dev_crit_lower)

    # Use most critical of your and device's levels
    elif dlh == "worst":
        warn, crit = minn(usr_warn, dev_warn), minn(usr_crit, dev_crit)
        warn_lower, crit_lower = max(usr_warn_lower, dev_warn_lower), max(usr_crit_lower, dev_crit_lower)

    # Use user's levels if present, otherwise the device's
    elif dlh == "usrdefault":
        if usr_levels:
            warn, crit = usr_levels
            dev_status = None
        else:
            warn, crit = dev_warn, dev_crit
        if usr_levels_lower:
            warn_lower, crit_lower = usr_levels_lower
        else:
            warn_lower, crit_lower = dev_warn_lower, dev_crit_lower

    # Use device's levels if present, otherwise yours
    elif dlh == "devdefault":
        if dev_levels or dev_status != None:
            warn, crit = dev_levels
        else:
            warn, crit = usr_warn, usr_crit
        if dev_levels_lower:
            warn_lower, crit_lower = dev_levels_lower
        else:
            warn_lower, crit_lower = usr_warn_lower, usr_crit_lower

    # Now finally compute status. Hooray!
    status = check_temp_levels(temp, warn, crit, warn_lower, crit_lower)
    if dev_status != None:
        if dlh == "best":
            status = minn(status, dev_status)
        else:
            status = max(status, dev_status)

    perfdata = [ ("temp", temp, warn, crit, warn_lower, crit_lower) ]

    # Render actual temperature, e.g. "17.8 °F"
    infotext = "%s %s" % (render_temp(temp, output_unit), temp_unitsym[output_unit])

    if dev_status != None and dev_status_name:
        infotext += ", %s" % dev_status_name

    # In case of a non-OK status output the information about the levels
    if status != 0:
        usr_levelstext       = ""
        usr_levelstext_lower = ""
        dev_levelstext       = ""
        dev_levelstext_lower = ""

        if usr_levels:
            usr_levelstext = " (warn/crit at %s/%s %s)" % (
                render_temp(usr_warn, output_unit),
                render_temp(usr_crit, output_unit),
                temp_unitsym[output_unit])

        if usr_levels_lower:
            usr_levelstext_lower = " (warn/crit below %s/%s %s)" % (
                render_temp(usr_warn_lower, output_unit),
                render_temp(usr_crit_lower, output_unit),
                temp_unitsym[output_unit])

        if dev_levels:
            dev_levelstext = " (device warn/crit at %s/%s %s)" % (
                render_temp(dev_warn, output_unit),
                render_temp(dev_crit, output_unit),
                temp_unitsym[output_unit])

        if dev_levels_lower:
            dev_levelstext_lower = " (device warn/crit below %s/%s %s)" % (
                render_temp(dev_warn_lower, output_unit),
                render_temp(dev_crit_lower, output_unit),
                temp_unitsym[output_unit])

        # Output only levels that are relevant when computing the state
        if dlh == "usr":
            infotext += usr_levelstext + usr_levelstext_lower

        elif dlh == "dev":
            infotext += dev_levelstext + dev_levelstext_lower

        elif dlh in ("best", "worst"):
            infotext += usr_levelstext + usr_levelstext_lower + dev_levelstext + dev_levelstext_lower

        elif dlh == "devdefault":
            infotext += dev_levelstext + dev_levelstext_lower
            if not dev_levels:
                infotext += usr_levelstext
            if not dev_levels_lower:
                infotext += usr_levelstext_lower

        elif dlh == "usrdefault":
            infotext += usr_levelstext + usr_levelstext_lower
            if not usr_levels:
                infotext += dev_levelstext
            if not usr_levels_lower:
                infotext += dev_levelstext_lower

    return status, infotext, perfdata


def check_temperature_list(sensorlist, params):

    if type(params) == tuple:
        params = { "levels" : params }
    elif params == None:
        params = {}

    output_unit = params.get("output_unit", "c")

    def worststate(a, b):
        if a != 3 and b != 3:
            return max(a,b)
        elif a != 2 and b != 2:
            return 3
        else:
            return 2

    if sensorlist == []:
        return

    sensor_count = len(sensorlist)
    tempsum = 0
    tempmax = sensorlist[0][1]
    tempmin = sensorlist[0][1]
    status = 0
    detailtext = ""
    for entry in sensorlist:

        if len(entry) == 2:
            sub_item, temp = entry
            kwargs = {}
        else:
            sub_item, temp, kwargs = entry
        if type(temp) not in [ float, int ]:
            temp = float(temp)

        tempsum += temp
        tempmax = max(tempmax, temp)
        tempmin = min(tempmin, temp)
        sub_status, sub_infotext, sub_perfdata = check_temperature(temp, params, **kwargs)
        status = worststate(status, sub_status)
        if status != 0:
            detailtext += (sub_item + ": " + sub_infotext + state_markers[sub_status] + ", ")
    if detailtext:
        detailtext = " " + detailtext[:-2] # Drop trailing ", ", add space to join with summary

    unitsym = temp_unitsym[output_unit]
    tempavg = tempsum / float(sensor_count)
    summarytext = "%d Sensors; Highest: %s %s, Average: %s %s, Lowest: %s %s" % ( sensor_count,
                                                        render_temp(tempmax, output_unit), unitsym,
                                                        render_temp(tempavg, output_unit), unitsym,
                                                        render_temp(tempmin, output_unit), unitsym )
    infotext = summarytext + detailtext
    perfdata = [ ("temp", tempmax) ]

    return status, infotext, perfdata