def build_graph_network_init(start, end, probes, summary, zip_file=False): """Builds the graph for the probes, or a summary graph.""" # Graph is cachable ? cachable = False intervals = {} for scale in scales: intervals[scales[scale][0]['interval']] = { 'resolution': scales[scale][0]['resolution'], 'name': scale } scale = None if end - start in intervals: scale = intervals[end - start]['name'] if end >= int(time.time()) - scales[scale][0]['resolution']: cachable = True # Retrieve probes (and multi-probes) if not isinstance(probes, list): probes = [probes] if len(probes) == 0: probes = list(probes_set_network) if not isinstance(probes, list): probes = [probes] probes = filter(lambda p: p in probes_set_network, probes) probes_in = set() probes_out = set() for probe in probes: for probe in find_probe_uid('network', probe): if get_rrd_filename(probe, "network_in") and get_rrd_filename(probe, "network_out"): probes_in.add(probe) probes_out.add(probe) probes_in = list(probes_in) probes_out = list(probes_out) # Single probe and no summary if len(probes) == 1 and not summary and scale: png_file = get_png_filename(probes[0], "network_in", scale) # All probes summary elif len(probes) == len(probes_set_network) and summary: png_file = cfg.CONF.png_dir + '/' + scale + '/summary-network.png' # Other combinaison else: png_file = NamedTemporaryFile(prefix="kwapi", suffix=".png").name if zip_file: #Force temporary name png_file = NamedTemporaryFile(prefix="kwapi", suffix=".png").name # Get the file from cache if cachable and os.path.exists(png_file) and os.path.getmtime(png_file) > \ time.time() - scales[scale][0]['resolution']: return png_file else: return build_graph_network(start, end, probes, probes_in, probes_out, summary, cachable, png_file, scale)
def build_graph_network(start, end, probes, probes_in, probes_out, summary, cachable, png_file, scale): # Build required (PNG file not found or outdated) scale_label = '' if scale: scale_label = ' (' + scales[scale][0]['label'] + ')' if summary: # Specific arguments for summary graph args = [png_file, '--title', 'Summary' + scale_label, '--width', '694', '--height', '261', ] else: # Specific arguments for probe graph args = [png_file, '--title', str(probes[0]) + scale_label, '--width', '497', '--height', '187', #'--upper-limit', str(cfg.CONF.max_metrics), ] # Common arguments args += ['--start', str(start), '--end', str(end), '--full-size-mode', '--imgformat', 'PNG', '--alt-y-grid', '--vertical-label', 'bits/s', #'--lower-limit', '0', #'--rigid', '--base', '1000', ] if end - start <= 300: args += ['--x-grid', 'SECOND:30:MINUTE:1:MINUTE:1:0:%H:%M'] cdef_metric_in = 'CDEF:metric_in=' cdef_metric_with_unknown_in = 'CDEF:metric_with_unknown_in=' cdef_metric_out = 'CDEF:metric_out=' cdef_metric_with_unknown_out = 'CDEF:metric_with_unknown_out=' graph_lines_in = [] graph_lines_out = [] stack_in = False stack_out = False probe_list = list(probes_in) # Generate colors color_seq_green = color_generator(len(probes_in)+1) for probe in sorted(probes_in, reverse=True): probe_colors[probe] = color_seq_green.next() #IN for probe in sorted(probe_list, reverse=True): probe_uuid = uuid.uuid5(uuid.NAMESPACE_DNS, str(probe)) rrd_file_in = get_rrd_filename(probe, "network_in") # Data source args.append('DEF:metric_with_unknown_%s_in=%s:o:AVERAGE' % (probe_uuid, rrd_file_in)) args.append('CDEF:metric_with_unknown_%s_in_scale=metric_with_' 'unknown_%s_in,8,*,' % (probe_uuid,probe_uuid)) # Data source without unknown values args.append('CDEF:metric_%s_in=metric_with_unknown_%s_in,UN,0,' 'metric_with_unknown_%s_in,8,*,IF,' % (probe_uuid, probe_uuid, probe_uuid)) # Prepare CDEF expression of total metric in cdef_metric_in += 'metric_%s_in,8,*,' % probe_uuid cdef_metric_with_unknown_in += 'metric_with_unknown_%s_in,8,*,' \ % probe_uuid # Draw the area for the probe in color = probe_colors.get(str(probe), '#336600') if not stack_in: args.append('AREA:metric_with_unknown_%s_in_scale%s::' % (probe_uuid, color + 'AA')) stack_in = True else: graph_lines_in.append('STACK:metric_with_unknown_%s_in_scale%s::' % (probe_uuid, color + 'AA')) args += graph_lines_in # Generate colors color_seq_blue = color_generator(len(probes_out)+1, 190) for probe in sorted(probes_out, reverse=True): probe_colors[probe] = color_seq_blue.next() #OUT probe_list = list(probes_out) for probe in sorted(probe_list, reverse=True): probe_uuid = uuid.uuid5(uuid.NAMESPACE_DNS, str(probe)) rrd_file_out = get_rrd_filename(probe, "network_out") if not rrd_file_out: break # Data source args.append('DEF:metric_with_unknown_%s_out=%s:o:AVERAGE' % (probe_uuid, rrd_file_out)) args.append('CDEF:metric_with_unknown_%s_out_neg=metric_with_' 'unknown_%s_out,-8,*,' % (probe_uuid,probe_uuid)) # Data source without unknown values args.append('CDEF:metric_%s_out=metric_with_unknown_%s_out,UN,0,' 'metric_with_unknown_%s_out,8,*,IF,' % (probe_uuid, probe_uuid, probe_uuid)) # Prepare CDEF expression of total metric out cdef_metric_out += 'metric_%s_out,8,*,' % probe_uuid cdef_metric_with_unknown_out += 'metric_with_unknown_%s_out,8,*,' \ % probe_uuid #Draw the probe out color = probe_colors.get(str(probe),'#0033CC') if not stack_out: args.append('AREA:metric_with_unknown_%s_out_neg%s::' % (probe_uuid, color + 'AA')) stack_out = True else: graph_lines_out.append('STACK:metric_with_unknown_%s_out_neg%s::' % (probe_uuid, color + 'AA')) args += graph_lines_out if len(probe_list) >= 2: # Prepare CDEF expression by adding the required number of '+' cdef_metric_in += '+,' * int(len(probes_in)-2) + '+' cdef_metric_with_unknown_in += '+,' * int(len(probes_in)-2) + '+' cdef_metric_out += '+,' * int(len(probes_out)-2) + '+' cdef_metric_with_unknown_out += '+,' * int(len(probes_out)-2) + '+' args.append('HRULE:0#000000') args.append(cdef_metric_in) args.append(cdef_metric_out) args.append(cdef_metric_with_unknown_in) args.append(cdef_metric_with_unknown_out) # IN # Min metric args.append('VDEF:metricmin_in=metric_with_unknown_in,MINIMUM') # Max metric args.append('VDEF:metricmax_in=metric_with_unknown_in,MAXIMUM') # Partial average that will be displayed (ignoring unknown values) args.append('VDEF:metricavg_with_unknown_in=metric_with_unknown_in,AVERAGE') # Real average args.append('VDEF:metricavg_in=metric_in,AVERAGE') # OUT # Min metric args.append('VDEF:metricmin_out=metric_with_unknown_out,MINIMUM') # Max metric args.append('VDEF:metricmax_out=metric_with_unknown_out,MAXIMUM') # Partial average that will be displayed (ignoring unknown values) args.append('VDEF:metricavg_with_unknown_out=metric_with_unknown_out,AVERAGE') # Real average args.append('VDEF:metricavg_out=metric_out,AVERAGE') # Legend args.append('GPRINT:metricavg_with_unknown_in:AvgIN\: %3.1lf%sb/s') args.append('GPRINT:metricmin_in:MinIN\: %3.1lf%sb/s') args.append('GPRINT:metricmax_in:MaxIN\: %3.1lf%sb/s') args.append('GPRINT:metric_with_unknown_in:LAST:LastIN\: %3.1lf%sb/s\j') args.append('GPRINT:metricavg_with_unknown_out:AvgOUT\: %3.1lf%sb/s') args.append('GPRINT:metricmin_out:MinOUT\: %3.1lf%sb/s') args.append('GPRINT:metricmax_out:MaxOUT\: %3.1lf%sb/s') args.append('GPRINT:metric_with_unknown_out:LAST:LastOUT\: %3.1lf%sb/s\j') args.append('TEXTALIGN:center') if len(probes_in) == 0 or len(probes_out) == 0: return None try: rrdtool.graph(args) except Exception as e: LOG.error("start %s, end %s, probes %s, probes_in %s, probes_out %s, summary %s, cachable %s, png_file %s, scale %s" \ % (start, end, probes, probes_in, probes_out, summary, cachable, png_file, scale)) LOG.error("%s", e) return png_file
def build_graph_energy(start, end, probes, probes_name, summary, cachable, png_file, scale): # Build required (PNG file not found or outdated) scale_label = '' if scale: scale_label = ' (' + scales[scale][0]['label'] + ')' if summary: # Specific arguments for summary graph args = [png_file, '--title', 'Summary' + scale_label, '--width', '694', '--height', '261', ] else: # Specific arguments for probe graph args = [png_file, '--title', str(",".join(probes_name)) + scale_label, '--width', '497', '--height', '187', '--upper-limit', str(cfg.CONF.max_metrics), ] # Common arguments args += ['--start', str(start), '--end', str(end), '--full-size-mode', '--imgformat', 'PNG', '--alt-y-grid', '--vertical-label', 'Watts', '--lower-limit', '0', '--rigid', ] if end - start <= 300: args += ['--x-grid', 'SECOND:30:MINUTE:1:MINUTE:1:0:%H:%M'] cdef_watt = 'CDEF:watt=' cdef_watt_with_unknown = 'CDEF:watt_with_unknown=' graph_lines = [] stack = False probe_list = sorted(probes, reverse=True) # Generate colors color_seq_green = color_generator(len(probe_list)+1) for probe in probe_list: probe_colors[probe] = color_seq_green.next() for probe in probe_list: probe_uuid = uuid.uuid5(uuid.NAMESPACE_DNS, str(probe)) rrd_file = get_rrd_filename(probe, "power") # Data source args.append('DEF:watt_with_unknown_%s=%s:w:AVERAGE' % (probe_uuid, rrd_file)) # Data source without unknown values args.append('CDEF:watt_%s=watt_with_unknown_%s,UN,0,watt_with_' 'unknown_%s,IF' % (probe_uuid, probe_uuid, probe_uuid)) # Prepare CDEF expression of total watt consumption cdef_watt += 'watt_%s,' % probe_uuid cdef_watt_with_unknown += 'watt_with_unknown_%s,' % probe_uuid # Draw the area for the probe color = probe_colors[probe] args.append('AREA:watt_with_unknown_%s%s::STACK' % (probe_uuid, color + 'AA')) if not stack: graph_lines.append('LINE:watt_with_unknown_%s%s::' % (probe_uuid, color)) stack = True else: graph_lines.append('LINE:watt_with_unknown_%s%s::STACK' % (probe_uuid, color)) if len(probe_list) >= 2: # Prepare CDEF expression by adding the required number of '+' cdef_watt += '+,' * int(len(probe_list)-2) + '+' cdef_watt_with_unknown += '+,' * int(len(probe_list)-2) + '+' args += graph_lines args.append(cdef_watt) args.append(cdef_watt_with_unknown) # Min watt args.append('VDEF:wattmin=watt_with_unknown,MINIMUM') # Max watt args.append('VDEF:wattmax=watt_with_unknown,MAXIMUM') # Partial average that will be displayed (ignoring unknown values) args.append('VDEF:wattavg_with_unknown=watt_with_unknown,AVERAGE') # Real average (to compute kWh) args.append('VDEF:wattavg=watt,AVERAGE') # Compute kWh for the probe # RPN expressions must contain DEF or CDEF variables, so we pop a # CDEF value args.append('CDEF:kwh=watt,POP,wattavg,1000.0,/,%s,3600.0,/,*' % str(end - start)) # Compute cost args.append('CDEF:cost=watt,POP,kwh,%f,*' % cfg.CONF.kwh_price) # Legend args.append('GPRINT:wattavg_with_unknown:Avg\: %3.1lf W') args.append('GPRINT:wattmin:Min\: %3.1lf W') args.append('GPRINT:wattmax:Max\: %3.1lf W') args.append('GPRINT:watt_with_unknown:LAST:Last\: %3.1lf W\j') args.append('TEXTALIGN:center') args.append('GPRINT:kwh:LAST:Total\: %lf kWh') args.append('GPRINT:cost:LAST:Cost\: %lf ' + cfg.CONF.currency) try: rrdtool.graph(args) except Exception as e: LOG.error("start %s, end %s, probes %s, probes_name %s, summary %s, cachable %s, png_file %s, scale %s" \ % (start, end, probes, probes_name, summary, cachable, png_file, scale)) LOG.error("%s", e) return png_file