]> git.ipfire.org Git - collecty.git/blobdiff - src/collecty/plugins/sensors.py
sensors: Make the lines less fat
[collecty.git] / src / collecty / plugins / sensors.py
index e4c247ea35fc5e006d18a2ec0d92e8357a47a0f3..8e890a90ebdd1af4351a88fd77e56386200cd39d 100644 (file)
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 # encoding: utf-8
 ###############################################################################
 #                                                                             #
@@ -24,172 +24,160 @@ from collecty import _collecty
 import os
 import re
 
-import base
+from . import base
 
 from ..i18n import _
 
 class GraphTemplateSensorsTemperature(base.GraphTemplate):
        name = "sensors-temperature"
 
-       rrd_graph = [
-               "DEF:value_kelvin=%(file)s:value:AVERAGE",
-               "DEF:critical_kelvin=%(file)s:critical:AVERAGE",
-               "DEF:high_kelvin=%(file)s:high:AVERAGE",
-               "DEF:low_kelvin=%(file)s:low:AVERAGE",
-
-               # Convert everything to celsius
-               "CDEF:value=value_kelvin,273.15,-",
-               "CDEF:critical=critical_kelvin,273.15,-",
-               "CDEF:high=high_kelvin,273.15,-",
-               "CDEF:low=low_kelvin,273.15,-",
-
-               # Change colour when the value gets above high
-               "CDEF:value_high=value,high,GT,value,UNKN,IF",
-               "CDEF:value_normal=value,high,GT,UNKN,value,IF",
-
-               "VDEF:value_cur=value,LAST",
-               "VDEF:value_avg=value,AVERAGE",
-               "VDEF:value_max=value,MAXIMUM",
-               "VDEF:value_min=value,MINIMUM",
-
-               # Get data points for the threshold lines
-               "VDEF:critical_line=critical,MINIMUM",
-               "VDEF:low_line=low,MAXIMUM",
-
-               # Draw the temperature value
-               "LINE3:value_high#ff0000",
-               "LINE2:value_normal#00ff00:%-15s" % _("Temperature"),
-
-               # Draw the legend
-               "GPRINT:value_cur:%%10.2lf °C\l",
-               "GPRINT:value_avg:  %-15s %%6.2lf °C\l" % _("Average"),
-               "GPRINT:value_max:  %-15s %%6.2lf °C\l" % _("Maximum"),
-               "GPRINT:value_min:  %-15s %%6.2lf °C\l" % _("Minimum"),
-
-               # Empty line
-               "COMMENT: \\n",
-
-               # Draw boundary lines
-               "COMMENT:%s\:" % _("Temperature Thresholds"),
-               "HRULE:critical_line#000000:%-15s" % _("Critical"),
-               "GPRINT:critical_line:%%6.2lf °C\\r",
-               "HRULE:low_line#0000ff:%-15s" % _("Low"),
-               "GPRINT:low_line:%%6.2lf °C\\r",
-       ]
+       @property
+       def rrd_graph(self):
+               _ = self.locale.translate
+
+               return [
+                       # Convert everything to Celsius
+                       "CDEF:value_c=value,273.15,-",
+                       "CDEF:critical_c=critical,273.15,-",
+                       "CDEF:high_c=high,273.15,-",
+                       "CDEF:low_c=low,273.15,-",
+
+                       # Change colour when the value gets above high
+                       "CDEF:value_c_high=value_c,high_c,GT,value_c,UNKN,IF",
+                       "CDEF:value_c_normal=value_c,high_c,GT,UNKN,value_c,IF",
+
+                       # Get data points for the threshold lines
+                       "VDEF:critical_c_line=critical_c,MINIMUM",
+                       "VDEF:low_c_line=low_c,MAXIMUM",
+
+                       # Draw the temperature value
+                       "LINE3:value_c_high#ff0000",
+                       "LINE2:value_c_normal#00ff00:%-15s" % _("Temperature"),
+
+                       # Draw the legend
+                       "GPRINT:value_c_cur:%10.2lf °C\l",
+                       "GPRINT:value_c_avg:  %-15s %%6.2lf °C\l" % _("Average"),
+                       "GPRINT:value_c_max:  %-15s %%6.2lf °C\l" % _("Maximum"),
+                       "GPRINT:value_c_min:  %-15s %%6.2lf °C\l" % _("Minimum"),
+
+                       # Empty line
+                       "COMMENT: \\n",
+
+                       # Draw boundary lines
+                       "COMMENT:%s\:" % _("Temperature Thresholds"),
+                       "HRULE:critical_c_line#000000:%-15s" % _("Critical"),
+                       "GPRINT:critical_c_line:%6.2lf °C\\r",
+                       "HRULE:low_c_line#0000ff:%-15s" % _("Low"),
+                       "GPRINT:low_c_line:%6.2lf °C\\r",
+               ]
 
        @property
        def graph_title(self):
+               _ = self.locale.translate
                return _("Temperature (%s)") % self.object.sensor.name
 
        @property
        def graph_vertical_label(self):
+               _ = self.locale.translate
                return _("° Celsius")
 
 
 class GraphTemplateSensorsProcessorTemperature(base.GraphTemplate):
        name = "processor-temperature"
 
-       core_colours = {
-               "core0" : "#ff000033",
-               "core1" : "#0000ff33",
-               "core2" : "#00ff0033",
-               "core3" : "#0000ff33",
-       }
+       core_colours = [
+               "#ff000033",
+               "#0000ff33",
+               "#00ff0033",
+               "#0000ff33",
+       ]
 
        def get_temperature_sensors(self):
-               return self.plugin.get_detected_sensor_objects("coretemp-*")
+               # Use the coretemp module if available
+               sensors = self.plugin.get_detected_sensor_objects("coretemp-*")
 
-       def get_object_table(self):
-               objects_table = {}
+               # Fall back to the ACPI sensor
+               if not sensors:
+                       sensors = self.plugin.get_detected_sensor_objects("acpitz-virtual-*")
 
-               counter = 0
-               for object in self.get_temperature_sensors():
-                       objects_table["core%s" % counter] = object
-                       counter += 1
+               return sensors
 
-               return objects_table
+       def get_objects(self, *args, **kwargs):
+               sensors = self.get_temperature_sensors()
+
+               return list(sensors)
 
        @property
        def rrd_graph(self):
+               _ = self.locale.translate
                rrd_graph = []
 
-               cores = sorted(self.object_table.keys())
-
-               for core in cores:
-                       i = {
-                               "core" : core,
-                       }
+               counter = 0
+               ids = []
 
-                       core_graph = [
-                               "DEF:value_kelvin_%(core)s=%%(%(core)s)s:value:AVERAGE",
-                               "DEF:critical_kelvin_%(core)s=%%(%(core)s)s:critical:AVERAGE",
-                               "DEF:high_kelvin_%(core)s=%%(%(core)s)s:high:AVERAGE",
+               for core in self.objects:
+                       id = "core%s" % counter
+                       counter += 1
+                       ids.append(id)
 
+                       rrd_graph += core.make_rrd_defs(id) + [
                                # Convert everything to celsius
-                               "CDEF:value_%(core)s=value_kelvin_%(core)s,273.15,-",
-                               "CDEF:critical_%(core)s=critical_kelvin_%(core)s,273.15,-",
-                               "CDEF:high_%(core)s=high_kelvin_%(core)s,273.15,-",
+                               "CDEF:%s_value_c=%s_value,273.15,-" % (id, id),
+                               "CDEF:%s_critical_c=%s_critical,273.15,-" % (id, id),
+                               "CDEF:%s_high_c=%s_high,273.15,-" % (id, id),
                        ]
 
-                       rrd_graph += [line % i for line in core_graph]
+               # Compute the temperature of the processor
+               # by taking the average of all cores
+               all_core_values = ("%s_value_c" % id for id in ids)
+               rrd_graph += [
+                       "CDEF:all_value_c=%s,%s,AVG" % (",".join(all_core_values), len(ids)),
+               ]
 
-               all_core_values = ("value_%s" % c for c in cores)
-               all_core_highs  = ("high_%s"  % c for c in cores)
+               # Get the high threshold of the first core
+               # (assuming that all cores have the same threshold)
+               for id in ids:
+                       rrd_graph.append("CDEF:all_high_c=%s_high_c" % id)
+                       break
 
                rrd_graph += [
-                       # Compute the temperature of the processor
-                       # by taking the average of all cores
-                       "CDEF:value=%s,%s,AVG" % (",".join(all_core_values), len(cores)),
-                       "CDEF:high=%s,MIN" % ",".join(all_core_highs),
-
                        # Change colour when the value gets above high
-                       "CDEF:value_high=value,high,GT,value,UNKN,IF",
-                       "CDEF:value_normal=value,high,GT,UNKN,value,IF",
-
-                       "VDEF:value_avg=value,AVERAGE",
-                       "VDEF:value_max=value,MAXIMUM",
-                       "VDEF:value_min=value,MINIMUM",
+                       "CDEF:all_value_c_high=all_value_c,all_high_c,GT,all_value_c,UNKN,IF",
+                       "CDEF:all_value_c_normal=all_value_c,all_high_c,GT,UNKN,all_value_c,IF",
 
-                       "LINE3:value_high#FF0000",
-                       "LINE3:value_normal#000000:%-15s\l" % _("Temperature"),
+                       "LINE2:all_value_c_high#FF0000",
+                       "LINE2:all_value_c_normal#000000:%-15s\l" % _("Temperature"),
 
-                       "GPRINT:value_avg:    %-15s %%6.2lf °C\l" % _("Average"),
-                       "GPRINT:value_max:    %-15s %%6.2lf °C\l" % _("Maximum"),
-                       "GPRINT:value_min:    %-15s %%6.2lf °C\l" % _("Minimum"),
+                       "GPRINT:all_value_c_avg:    %-15s %%6.2lf °C\l" % _("Average"),
+                       "GPRINT:all_value_c_max:    %-15s %%6.2lf °C\l" % _("Maximum"),
+                       "GPRINT:all_value_c_min:    %-15s %%6.2lf °C\l" % _("Minimum"),
                ]
 
-               for core in cores:
-                       object = self.object_table.get(core)
-
-                       i = {
-                               "colour" : self.core_colours.get(core, "#000000"),
-                               "core"   : core,
-                               "label"  : object.sensor.label,
-                       }
-
-                       core_graph = [
+               for id, core, colour in zip(ids, self.objects, self.core_colours):
+                       rrd_graph += [
                                # TODO these lines were supposed to be dashed, but that
                                # didn't really work here
-                               "LINE2:value_%(core)s%(colour)s:%(label)-10s",
+                               "LINE1:%s_value_c%s:%-10s" % (id, colour, core.sensor.label),
                        ]
 
-                       rrd_graph += [line % i for line in core_graph]
-
                # Draw the critical line
-               rrd_graph += [
-                       "VDEF:critical_line=critical_core0,MINIMUM",
-                       "HRULE:critical_line#000000:%-15s" % _("Critical"),
-                       "GPRINT:critical_line:%%6.2lf °C\\r",
-               ]
+               for id in ids:
+                       rrd_graph += [
+                               "HRULE:%s_critical_c_min#000000:%-15s" % (id, _("Critical")),
+                               "GPRINT:%s_critical_c_min:%%6.2lf °C\\r" % id,
+                       ]
+                       break
 
                return rrd_graph
 
        @property
        def graph_title(self):
+               _ = self.locale.translate
                return _("Processor")
 
        @property
        def graph_vertical_label(self):
+               _ = self.locale.translate
                return _("Temperature")
 
 
@@ -297,7 +285,7 @@ class SensorFanObject(SensorBaseObject):
                )
 
        @property
-       def mimimum(self):
+       def minimum(self):
                try:
                        return self.sensor.minimum
                except AttributeError: