Refectoring of the main classes
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 15 Dec 2015 21:51:02 +0000 (22:51 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 15 Dec 2015 22:43:02 +0000 (22:43 +0000)
This is supposed to remove some too complicated and
redundant code.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/collecty/plugins/base.py
src/collecty/plugins/conntrack.py
src/collecty/plugins/cpufreq.py
src/collecty/plugins/sensors.py

index 547d027..5b13044 100644 (file)
@@ -303,6 +303,9 @@ class Object(object):
        def __repr__(self):
                return "<%s>" % self.__class__.__name__
 
+       def __lt__(self, other):
+               return self.id < other.id
+
        @property
        def collecty(self):
                return self.plugin.collecty
@@ -537,7 +540,8 @@ class GraphTemplate(object):
                self.object_id = object_id
 
                # Get the main object
-               self.object = self.get_object(self.object_id)
+               self.objects = self.get_objects(self.object_id)
+               self.objects.sort()
 
        def __repr__(self):
                return "<%s>" % self.__class__.__name__
@@ -550,6 +554,14 @@ class GraphTemplate(object):
        def log(self):
                return self.plugin.log
 
+       @property
+       def object(self):
+               """
+                       Shortcut to the main object
+               """
+               if len(self.objects) == 1:
+                       return self.objects[0]
+
        def _make_command_line(self, interval, format=DEFAULT_IMAGE_FORMAT,
                        width=None, height=None, with_title=True, thumbnail=False):
                args = [e for e in GRAPH_DEFAULT_ARGUMENTS]
@@ -595,6 +607,18 @@ class GraphTemplate(object):
 
                return args
 
+       def _add_defs(self):
+               use_prefix = len(self.objects) >= 2
+
+               args = []
+               for object in self.objects:
+                       if use_prefix:
+                               args += object.make_rrd_defs(object.id)
+                       else:
+                               args += object.make_rrd_defs()
+
+               return args
+
        def _add_vdefs(self, args):
                ret = []
 
@@ -616,35 +640,33 @@ class GraphTemplate(object):
 
                return ret
 
-       def get_object(self, *args, **kwargs):
-               return self.plugin.get_object(*args, **kwargs)
+       def get_objects(self, *args, **kwargs):
+               object = self.plugin.get_object(*args, **kwargs)
 
-       def get_object_table(self):
-               return {
-                       "file" : self.object,
-               }
+               if object:
+                       return [object,]
 
-       @property
-       def object_table(self):
-               if not hasattr(self, "_object_table"):
-                       self._object_table = self.get_object_table()
-
-               return self._object_table
+               return []
 
        def generate_graph(self, interval=None, **kwargs):
+               assert self.objects, "Cannot render graph without any objects"
+
                # Make sure that all collected data is in the database
                # to get a recent graph image
-               if self.object:
-                       self.object.commit()
+               for object in self.objects:
+                       object.commit()
 
                args = self._make_command_line(interval, **kwargs)
 
                self.log.info(_("Generating graph %s") % self)
 
-               if self.object:
-                       args += self.object.make_rrd_defs()
+               rrd_graph = self.rrd_graph
+
+               # Add DEFs for all objects
+               if not any((e.startswith("DEF:") for e in rrd_graph)):
+                       args += self._add_defs()
 
-               args += self.rrd_graph
+               args += rrd_graph
                args = self._add_vdefs(args)
 
                # Convert arguments to string
index 1e9bb79..22fcd60 100644 (file)
@@ -136,8 +136,10 @@ class ConntrackLayer3ProtocolsGraphTemplate(base.GraphTemplate):
                "ipv4"  : "#cccc33",
        }
 
-       def get_object(self, *args):
-               return self.plugin.get_object("layer3-protocols")
+       def get_objects(self, *args):
+               return [
+                       self.plugin.get_object("layer3-protocols"),
+               ]
 
        @property
        def protocols(self):
@@ -248,8 +250,10 @@ class ConntrackLayer4ProtocolsGraphTemplate(ConntrackLayer3ProtocolsGraphTemplat
                "dccp"    : 7,
        }
 
-       def get_object(self, *args):
-               return self.plugin.get_object("layer4-protocols")
+       def get_objects(self, *args):
+               return [
+                       self.plugin.get_object("layer4-protocols"),
+               ]
 
        @property
        def graph_title(self):
index 4bce860..8bbd1fb 100644 (file)
@@ -31,6 +31,9 @@ class GraphTemplateCPUFreq(base.GraphTemplate):
 
        lower_limit = 0
 
+       def get_objects(self, *args, **kwargs):
+               return list(self.plugin.objects)
+
        @property
        def graph_title(self):
                _ = self.locale.translate
@@ -41,45 +44,23 @@ class GraphTemplateCPUFreq(base.GraphTemplate):
                _ = self.locale.translate
                return "%s [%s]" % (_("Frequency"), _("Hz"))
 
-       def get_object_table(self):
-               objects_table = {}
-
-               for processor in self.plugin.objects:
-                       objects_table[processor.id] = processor
-
-               return objects_table
-
-       core_colours = {
-               "cpu0" : "#ff000066",
-               "cpu1" : "#00ff0066",
-               "cpu2" : "#0000ff66",
-               "cpu3" : "#ffff0066",
-       }
+       processor_colours = [
+               "#ff000066",
+               "#00ff0066",
+               "#0000ff66",
+               "#ffff0066",
+       ]
 
        @property
        def rrd_graph(self):
                rrd_graph = []
 
-               for core, processor in sorted(self.object_table.items()):
-                       i = {
-                               "core"   : core,
-                               "colour" : self.core_colours.get(core, "#000000"),
-                               "name"   : processor.name,
-                       }
-
-                       core_graph = [
-                               "DEF:current_%(core)s=%%(%(core)s)s:current:AVERAGE",
-                               "DEF:minimum_%(core)s=%%(%(core)s)s:minimum:AVERAGE",
-                               "DEF:maximum_%(core)s=%%(%(core)s)s:maximum:AVERAGE",
-
-                               "VDEF:avg_%(core)s=current_%(core)s,AVERAGE",
-
-                               "LINE2:current_%(core)s%(colour)s:%(name)-10s",
-                               "GPRINT:avg_%(core)s:%%6.2lf %%sHz\l",
+               for processor, colour in zip(self.objects, self.processor_colours):
+                       rrd_graph += processor.make_rrd_defs(processor.id) + [
+                               "LINE2:%s%s:%-10s" % (processor.id, colour, processor.name),
+                               "GPRINT:%s:%%6.2lf %%sHz" % processor.id,
                        ]
 
-                       rrd_graph += [line % i for line in core_graph]
-
                return rrd_graph
 
        rrd_graph_args = [
index efdbe9b..1fd4b19 100644 (file)
@@ -36,44 +36,39 @@ class GraphTemplateSensorsTemperature(base.GraphTemplate):
                _ = self.locale.translate
 
                return [
-                       "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,-",
+                       # 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_high=value,high,GT,value,UNKN,IF",
-                       "CDEF:value_normal=value,high,GT,UNKN,value,IF",
+                       "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_line=critical,MINIMUM",
-                       "VDEF:low_line=low,MAXIMUM",
+                       "VDEF:critical_c_line=critical_c,MINIMUM",
+                       "VDEF:low_c_line=low_c,MAXIMUM",
 
                        # Draw the temperature value
-                       "LINE3:value_high#ff0000",
-                       "LINE2:value_normal#00ff00:%-15s" % _("Temperature"),
+                       "LINE3:value_c_high#ff0000",
+                       "LINE2:value_c_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"),
+                       "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_line#000000:%-15s" % _("Critical"),
-                       "GPRINT:critical_line:%%6.2lf °C\\r",
-                       "HRULE:low_line#0000ff:%-15s" % _("Low"),
-                       "GPRINT:low_line:%%6.2lf °C\\r",
+                       "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
@@ -90,95 +85,81 @@ class GraphTemplateSensorsTemperature(base.GraphTemplate):
 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-*")
 
-       def get_object_table(self):
-               objects_table = {}
-
-               counter = 0
-               for object in self.get_temperature_sensors():
-                       objects_table["core%s" % counter] = object
-                       counter += 1
+       def get_objects(self, *args, **kwargs):
+               sensors = self.get_temperature_sensors()
 
-               return objects_table
+               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",
+                       "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"),
+                       "LINE3:all_value_c_high#FF0000",
+                       "LINE3: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",
+                               "LINE2:%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