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
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__
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]
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 = []
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
_ = 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
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