[checkmk-commits] Check_MK Git: check_mk: temperature.include: Added check_temperature_list function

Konstantin Büttner kb at mathias-kettner.de
Tue Feb 24 16:23:37 CET 2015


Module: check_mk
Branch: master
Commit: f52296a2382cc4560f698c256ded89ec6b528d4f
URL:    http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=f52296a2382cc4560f698c256ded89ec6b528d4f

Author: Konstantin Büttner <kb at mathias-kettner.de>
Date:   Tue Feb 24 16:23:33 2015 +0100

temperature.include: Added check_temperature_list function

---

 checks/temperature.include |  163 ++++++++++++++++++++++++++++----------------
 1 file changed, 103 insertions(+), 60 deletions(-)

diff --git a/checks/temperature.include b/checks/temperature.include
index 5fc8166..68177c3 100644
--- a/checks/temperature.include
+++ b/checks/temperature.include
@@ -31,6 +31,38 @@ def fahrenheit_to_celsius(tempf):
 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:
+        tuple([to_celsius(x, unit) for x in reading])
+    elif unit == "f":
+        return fahrenheit_to_celsius(reading)
+    if 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:
@@ -46,31 +78,6 @@ def check_temperature(reading, params, dev_unit = "c",
                       dev_levels = None, dev_levels_lower = None,
                       dev_status = None, dev_status_name = None):
 
-    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:
-            tuple([to_celsius(x, unit) for x in reading])
-        elif unit == "f":
-            return fahrenheit_to_celsius(reading)
-        if unit == "k":
-            return reading - 273.15
-        else:
-            return reading
-
-    # Format number according to its datatype
-    def render_temp(n):
-        t = from_celsius(n, output_unit)
-        if type(n) == int:
-            return "%d" % t
-        else:
-            return "%.1f" % t
 
     def check_temp_levels(temp, warn, crit, warn_lower, crit_lower):
         if crit != None and temp >= crit:
@@ -94,12 +101,6 @@ def check_temperature(reading, params, dev_unit = "c",
         else:
             return min(a,b)
 
-    unitsym = {
-        "c": "°C",
-        "f": "°F",
-        "k": "K",
-    }
-
     # Convert legacy tuple params into new dict
     if params == None or params == (None, None):
         params = {}
@@ -137,22 +138,6 @@ def check_temperature(reading, params, dev_unit = "c",
     if dev_levels_lower:
         dev_warn_lower, dev_crit_lower = to_celsius(dev_levels_lower, dev_unit)
 
-#    if dev_status:
-#        warn, crit, warn_lower, crit_lower = usr_warn, usr_crit, usr_warn_lower, usr_crit_lower
-#        ustatus = check_temp_levels(temp, warn, crit, warn_lower, crit_lower)
-#        if dlh == "usr":
-#            status = ustatus
-#        if dlh == "dev" or dlh == "devdefault":
-#            warn = crit = warn_lower = crit_lower = None
-#            status = dev_status
-#        if dlh == "best":
-#            status = min(dev_status, ustatus)
-#        if dlh == "worst":
-#            status = max(dev_status, ustatus)
-
-#    if not dev_status:
-
-
     # 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")
@@ -215,7 +200,7 @@ def check_temperature(reading, params, dev_unit = "c",
     perfdata = [ ("temp", temp, warn, crit, warn_lower, crit_lower) ]
 
     # Render actual temperature, e.g. "17.8 °F"
-    infotext = "%s %s" % (render_temp(temp), unitsym[output_unit])
+    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
@@ -229,27 +214,27 @@ def check_temperature(reading, params, dev_unit = "c",
 
         if usr_levels:
             usr_levelstext = " (warn/crit at %s/%s %s)" % (
-                render_temp(usr_warn),
-                render_temp(usr_crit),
-                unitsym[output_unit])
+                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),
-                render_temp(usr_crit_lower),
-                unitsym[output_unit])
+                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),
-                render_temp(dev_crit),
-                unitsym[output_unit])
+                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),
-                render_temp(dev_crit_lower),
-                unitsym[output_unit])
+                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":
@@ -276,3 +261,61 @@ def check_temperature(reading, params, dev_unit = "c",
                 infotext += dev_levelstext_lower
 
     return status, infotext, perfdata
+
+
+def check_temperature_list(sensorlist, params):
+
+    if type(params) == tuple:
+        params = { "levels" : params }
+    elif type(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



More information about the checkmk-commits mailing list