This file is indexed.

/usr/share/check_mk/checks/temperature.include is in check-mk-server 1.2.8p16-1ubuntu0.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#!/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-
# tails. 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, relative=False):
    if tempf is None:
        return None

    if relative:
        return float(tempf) * (5.0 / 9.0)
    else:
        return (float(tempf) - 32) * (5.0 / 9.0)

def celsius_to_fahrenheit(tempc, relative=False):
    if tempc is None:
        return None

    if relative:
        return float(tempc) * (9.0 / 5.0)
    else:
        return (float(tempc) * (9.0 / 5.0)) + 32

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

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

def convert_unit_relative(value, in_unit, out_unit):
    return from_celsius(to_celsius(value, in_unit, relative=True),
                        out_unit, relative=True)

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

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


def check_temperature_determine_levels(dlh, usr_warn, usr_crit, usr_warn_lower, usr_crit_lower,
                                       dev_warn, dev_crit, dev_warn_lower, dev_crit_lower,
                                       dev_unit):
    # min that deals correctly with None
    def minn(a, b):
        return min(a, b) or a or b

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

    # 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_warn is not None and usr_crit is not None:
            warn, crit = usr_warn, usr_crit
        else:
            warn, crit = dev_warn, dev_crit
        if usr_warn_lower is not None and usr_crit_lower is not None:
            warn_lower, crit_lower = usr_warn_lower, usr_crit_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_warn is not None and dev_crit is None:
            warn, crit = dev_warn, dev_crit
        else:
            warn, crit = usr_warn, usr_crit

        if dev_warn_lower is not None and dev_crit_lower is not None:
            warn_lower, crit_lower = dev_warn_lower, dev_crit_lower
        else:
            warn_lower, crit_lower = usr_warn_lower, usr_crit_lower

    return warn, crit, warn_lower, crit_lower


# determine temperature trends. This is a private function, not to be called by checks
def check_temperature_trend(temp, params, output_unit, crit, crit_lower, unique_name):
    def combiner(status, infotext):
        if "status" in dir(combiner):
            combiner.status = max(combiner.status, status)
        else:
            combiner.status = status

        if "infotext" in dir(combiner):
            combiner.infotext += ", " + infotext
        else:
            combiner.infotext = infotext

    try:
        trend_range_min = params["period"]
        this_time = time.time()

        # first compute current rate in C/s by computing delta since last check
        rate = get_rate("temp.%s.delta" % unique_name, this_time, temp, True)

        # average trend, initialize with zero, rate_avg is in C/s
        rate_avg = get_average("temp.%s.trend" % unique_name,
                                this_time, rate, trend_range_min, True)

        # rate_avg is growth in C/s, trend is in C per trend range minutes
        trend = float(rate_avg * trend_range_min * 60.0)
        sign = trend > 0 and "+" or ""
        combiner(0, "rate: %s%s/%g min" %\
                    (sign, render_temp(trend, output_unit, True), trend_range_min))

        if "trend_levels" in params:
            warn_upper_trend, crit_upper_trend = params["trend_levels"]
        else:
            warn_upper_trend = crit_upper_trend = None
        # it may be unclear to the user if he should specify temperature decrease as a negative
        # number or positive. This works either way. Having a positive lower bound makes no
        # sense anyway.
        if "trend_levels_lower" in params:
            warn_lower_trend, crit_lower_trend =\
                map(lambda x: abs(x) * -1, params["trend_levels_lower"])
        else:
            warn_lower_trend = crit_lower_trend = None

        if crit_upper_trend is not None and trend > crit_upper_trend:
            combiner(2, u"rising faster than %s/%g min(!!)" %\
                        (render_temp(crit_upper_trend, output_unit, True), trend_range_min))
        elif warn_upper_trend is not None and trend > warn_upper_trend:
            combiner(1, u"rising faster than %s/%g min(!)" %\
                        (render_temp(warn_upper_trend, output_unit, True), trend_range_min))
        elif crit_lower_trend is not None and trend < crit_lower_trend:
            combiner(2, u"falling faster than %s/%g min(!!)" %\
                        (render_temp(crit_lower_trend, output_unit, True), trend_range_min))
        elif warn_lower_trend is not None and trend < warn_lower_trend:
            combiner(1, u"falling faster than %s/%g min(!)" %\
                        (render_temp(warn_lower_trend, output_unit, True), trend_range_min))

        if "trend_timeleft" in params:
            # compute time until temperature limit is reached
            # The start value of minutes_left is negative. The pnp graph and the perfometer
            # will interpret this as inifinite -> not growing
            minutes_left = -1
            limit = trend > 0 and crit or crit_lower

            if limit:  # crit levels may not be set, especially lower level
                diff_to_limit = limit - temp
                if rate_avg != 0.0:
                    minutes_left = (diff_to_limit / rate_avg) / 60.0
                else:
                    minutes_left = float("inf")

                def format_minutes(minutes):
                    if minutes > 60:   # hours
                        hours = minutes / 60
                        minutes += - int(hours) * 60
                        return "%dh %02dm" % (hours, minutes)
                    else:
                        return "%d minutes" % minutes

                warn, crit = params["trend_timeleft"]
                if minutes_left <= crit:
                    combiner(2, "%s until temp limit reached(!!)" % format_minutes(minutes_left))
                elif minutes_left <= warn:
                    combiner(1, "%s until temp limit reached(!)" % format_minutes(minutes_left))
    except MKCounterWrapped:
        pass
    return combiner.status, combiner.infotext

# Checks Celsius temperature against crit/warn levels defined in params. temp must
# be int or float. Parameters:
# reading:           temperature reading of the device (per default interpreted as Celsius)
# params:            check parameters (pair or dict)
# unique_name:       unique name of this check, used for counters
# 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)
# dev_status_name:   the device name (will be added in the check output)
# Note: you must not specify dev_status and dev_levels at the same time!

def check_temperature(reading, params, unique_name, 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


    # 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, usr_crit             = usr_levels or (None, None)
    usr_warn_lower, usr_crit_lower = usr_levels_lower or (None, None)

    # Same for device levels
    dev_warn, dev_crit             = to_celsius(dev_levels or (None, None), dev_unit)
    dev_warn_lower, dev_crit_lower = to_celsius(dev_levels_lower or (None, None), 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")

    warn, crit, warn_lower, crit_lower =\
        check_temperature_determine_levels(dlh, usr_warn, usr_crit,
                                           usr_warn_lower, usr_crit_lower,
                                           dev_warn, dev_crit,
                                           dev_warn_lower, dev_crit_lower, dev_unit)

    if dlh == "usr" or (dlh == "userdefault" and usr_levels):
        # ignore device status if user-levels are used
        dev_status = None

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

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

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

    if dev_status is not None and dev_status != 0 and dev_status_name: # omit status in OK case
        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

    # all checks specify a unique_name but when multiple sensors are handled through
    #   check_temperature_list, trend is only calculated for the average and then the individual
    #   calls to check_temperate receive no unique_name
    # "trend_compute" in params tells us if there if there is configuration for trend computation
    #   when activating trend computation through the website, "period" is always set together with
    #   the trend_compute dictionary. But a check may want to specify default levels for trends
    #   without activating them. In this case they can leave period unset to deactivate the
    #   feature.
    if unique_name and\
            "trend_compute" in params\
            and "period" in params["trend_compute"]:
        trend_status, trend_infotext =\
            check_temperature_trend(temp, params["trend_compute"], output_unit,
                                    crit, crit_lower, unique_name)
        status = max(status, trend_status)
        if trend_infotext:
            infotext += ", " + trend_infotext

    return status, infotext, perfdata

# Wraps around check_temperature to check a list of sensors.
# sensorlist is a list of tuples:
# (subitem, temp, kwargs) or (subitem, temp)
# where subitem is a string (sensor-id)
# temp is a string, float or int temperature value
# and kwargs a dict of keyword arguments for check_temperature

def check_temperature_list(sensorlist, params, unique_name):

    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,
                                                                   None,
                                                                   **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) ]

    if "trend_compute" in params and\
            "period" in params["trend_compute"]:
        usr_warn, usr_crit             = params.get("levels") or (None, None)
        usr_warn_lower, usr_crit_lower = params.get("levels_lower") or (None, None)

        # no support for dev_unit or dev_levels in check_temperature_list so
        # this ignores the device level handling set in params
        warn, crit, warn_lower, crit_lower =\
            check_temperature_determine_levels("usr", usr_warn, usr_crit,
                                               usr_warn_lower, usr_crit_lower,
                                               None, None,
                                               None, None, "c")


        trend_status, trend_infotext =\
            check_temperature_trend(tempavg, params["trend_compute"], output_unit,
                                    crit, crit_lower, unique_name)
        status = max(status, trend_status)
        if trend_infotext:
            infotext += ", " + trend_infotext

    return status, infotext, perfdata