function rrdtool_graph_merge_args_from_json($rrdtool_graph, $graph_config, $context, $size, $conf_rrds, $conf_graphreport_stats, $conf_graphreport_stat_items) { $title = sanitize($graph_config['title']); $rrdtool_graph['title'] = $title; // If vertical label is empty or non-existent set it to space otherwise // rrdtool will fail if (!isset($graph_config['vertical_label']) || $graph_config['vertical_label'] == "") { $rrdtool_graph['vertical-label'] = " "; } else { $rrdtool_graph['vertical-label'] = sanitize($graph_config['vertical_label']); } $rrdtool_graph['lower-limit'] = '0'; if (isset($graph_config['height_adjustment'])) { $rrdtool_graph['height'] += $size == 'medium' ? $graph_config['height_adjustment'] : 0; } else { $rrdtool_graph['height'] += $size == 'medium' ? 28 : 0; } // find longest label length, so we pad the others accordingly to get // consistent column alignment $max_label_length = 0; foreach ($graph_config['series'] as $series) { $max_label_length = max(strlen($series['label']), $max_label_length); } $seriesdef = ''; $cdef = ''; $graphdef = ''; $stack_counter = 0; $total_ids = array(); // Loop through all the graph series foreach ($graph_config['series'] as $series_id => $series) { // ignore series if context is not defined in json template if (isset($series['contexts']) and !in_array($context, $series['contexts'])) { continue; } $rrd_dir = $conf_rrds . "/" . $series['clustername'] . "/" . $series['hostname']; $metric = sanitize($series['metric']); $metric_file = $rrd_dir . "/" . $metric . ".rrd"; // Make sure metric file exists. Otherwise we'll get a broken graph if (is_file($metric_file)) { // Need this when defining graphs that may use same metric names $unique_id = "a" . $series_id; $total_ids[] = $unique_id; // use custom DS defined in json template if it's // defined (default = 'sum') $DS = isset($series['ds']) ? sanitize($series['ds']) : 'sum'; $seriesdef .= " DEF:'{$unique_id}'='{$metric_file}':'{$DS}':AVERAGE "; if (isset($graph_config['scale'])) { $cdef .= " CDEF:'s{$unique_id}'={$unique_id},{$graph_config['scale']},* "; } if (isset($graph_config['percent']) && $graph_config['percent'] == '1') { $cdef .= " CDEF:'p{$unique_id}'={$unique_id},total,/,100,* "; } // By default graph is a line graph $series_type = isset($series['type']) ? $series['type'] : "line"; list($graphdef, $stack_counter) = graphdef_add_series($graphdef, $series, $series_type, $graph_config['percent'], $graph_config['scale'], $stack_counter, $unique_id, $max_label_length, $conf_graphreport_stats, $conf_graphreport_stat_items); } // end of if (is_file($metric_file)) } $show_total = isset($graph_config['show_total']) && $graph_config['show_total'] == 1; if ($show_total || isset($graph_config['percent']) && $graph_config['percent'] == '1') { $cdef = add_total_to_cdef($cdef, $total_ids, $graph_config['scale']); if ($show_total) { $cdef .= " LINE1:'total'#000000:'Total' "; if ($conf_graphreport_stats) { $cdef .= legendEntry('total', $conf_graphreport_stat_items); } } } // If we end up with the empty series it means that no RRD files matched. // This can happen if we are trying to create a report and metrics for // this host were not collected. If that happens we should create an // empty graph if ($seriesdef == "") { $rrdtool_graph['series'] = 'HRULE:1#FFCC33:"No matching metrics detected or RRDs not readable"'; } else { $rrdtool_graph['series'] = $seriesdef . ' ' . $cdef . ' ' . $graphdef; } return $rrdtool_graph; }
function build_rrdtool_args_from_json(&$rrdtool_graph, $graph_config) { global $context, $hostname, $range, $rrd_dir, $size, $conf; if ($conf['strip_domainname']) { $hostname = strip_domainname($hostname); } $title = sanitize($graph_config['title']); $rrdtool_graph['title'] = $title; // If vertical label is empty or non-existent set it to space otherwise // rrdtool will fail if (!isset($graph_config['vertical_label']) || $graph_config['vertical_label'] == "") { $rrdtool_graph['vertical-label'] = " "; } else { $rrdtool_graph['vertical-label'] = sanitize($graph_config['vertical_label']); } $rrdtool_graph['lower-limit'] = '0'; if (isset($graph_config['height_adjustment'])) { $rrdtool_graph['height'] += $size == 'medium' ? $graph_config['height_adjustment'] : 0; } else { $rrdtool_graph['height'] += $size == 'medium' ? 28 : 0; } // find longest label length, so we pad the others accordingly to get // consistent column alignment $max_label_length = 0; foreach ($graph_config['series'] as $item) { $max_label_length = max(strlen($item['label']), $max_label_length); } $series = ''; $stack_counter = 0; // Available line types $line_widths = array("1", "2", "3"); // Loop through all the graph items foreach ($graph_config['series'] as $index => $item) { // ignore item if context is not defined in json template if (isset($item['contexts']) and in_array($context, $item['contexts']) == false) { continue; } $rrd_dir = $conf['rrds'] . "/" . $item['clustername'] . "/" . $item['hostname']; $metric = sanitize($item['metric']); $metric_file = $rrd_dir . "/" . $metric . ".rrd"; // Make sure metric file exists. Otherwise we'll get a broken graph if (is_file($metric_file)) { # Need this when defining graphs that may use same metric names $unique_id = "a" . $index; $label = str_pad(sanitize($item['label']), $max_label_length); // use custom DS defined in json template if it's // defined (default = 'sum') $DS = "sum"; if (isset($item['ds'])) { $DS = sanitize($item['ds']); } $series .= " DEF:'{$unique_id}'='{$metric_file}':'{$DS}':AVERAGE "; // By default graph is a line graph isset($item['type']) ? $item_type = $item['type'] : ($item_type = "line"); // TODO sanitize color switch ($item_type) { case "line": // Make sure it's a recognized line type isset($item['line_width']) && in_array($item['line_width'], $line_widths) ? $line_width = $item['line_width'] : ($line_width = "1"); $series .= "LINE" . $line_width . ":'{$unique_id}'#{$item['color']}:'{$label}' "; break; case "stack": // First element in a stack has to be AREA if ($stack_counter == 0) { $series .= "AREA"; $stack_counter++; } else { $series .= "STACK"; } $series .= ":'{$unique_id}'#{$item['color']}:'{$label}' "; break; } // end of switch ( $item_type ) if ($conf['graphreport_stats']) { $series .= legendEntry($unique_id, $conf['graphreport_stat_items']); } } // end of if ( is_file($metric_file) ) { } // end of foreach( $graph_config[ 'series' ] as $index => $item ) // If we end up with the empty series it means that no RRD files matched. // This can happen if we are trying to create a report and metrics for // this host were not collected. If that happens we should create an // empty graph if ($series == "") { $rrdtool_graph['series'] = 'HRULE:1#FFCC33:"No matching metrics detected"'; } else { $rrdtool_graph['series'] = $series; } return $rrdtool_graph; }
function build_rrdtool_args_from_json(&$rrdtool_graph, $graph_config) { global $context, $hostname, $range, $rrd_dir, $size, $conf; if ($conf['strip_domainname']) { $hostname = strip_domainname($hostname); } $title = sanitize($graph_config['title']); $rrdtool_graph['title'] = $title; // If vertical label is empty or non-existent set it to space otherwise // rrdtool will fail if (!isset($graph_config['vertical_label']) || $graph_config['vertical_label'] == "") { $rrdtool_graph['vertical-label'] = " "; } else { $rrdtool_graph['vertical-label'] = sanitize($graph_config['vertical_label']); } $rrdtool_graph['lower-limit'] = '0'; if (isset($graph_config['height_adjustment'])) { $rrdtool_graph['height'] += $size == 'medium' ? $graph_config['height_adjustment'] : 0; } else { $rrdtool_graph['height'] += $size == 'medium' ? 28 : 0; } // find longest label length, so we pad the others accordingly to get // consistent column alignment $max_label_length = 0; foreach ($graph_config['series'] as $item) { $max_label_length = max(strlen($item['label']), $max_label_length); } $series = ''; $cdef = ''; $graphdef = ''; $stack_counter = 0; // Available line types $line_widths = array("1", "2", "3"); $total_ids = array(); // Loop through all the graph items foreach ($graph_config['series'] as $index => $item) { // ignore item if context is not defined in json template if (isset($item['contexts']) and in_array($context, $item['contexts']) == false) { continue; } $rrd_dir = $conf['rrds'] . "/" . $item['clustername'] . "/" . $item['hostname']; $metric = sanitize($item['metric']); $metric_file = $rrd_dir . "/" . $metric . ".rrd"; // Make sure metric file exists. Otherwise we'll get a broken graph if (is_file($metric_file)) { // Need this when defining graphs that may use same metric names $unique_id = "a" . $index; $total_ids[] = $unique_id; # Pad the label with spaces $label = str_pad(sanitize($item['label']), $max_label_length); // use custom DS defined in json template if it's // defined (default = 'sum') $DS = "sum"; if (isset($item['ds'])) { $DS = sanitize($item['ds']); } $series .= " DEF:'{$unique_id}'='{$metric_file}':'{$DS}':AVERAGE "; if (isset($graph_config['scale'])) { $cdef .= " CDEF:'s{$unique_id}'={$unique_id},{$graph_config['scale']},* "; } if (isset($graph_config['percent']) && $graph_config['percent'] == '1') { $cdef .= " CDEF:'p{$unique_id}'={$unique_id},total,/,100,* "; } // By default graph is a line graph isset($item['type']) ? $item_type = $item['type'] : ($item_type = "line"); // TODO sanitize color switch ($item_type) { case "line": // Make sure it's a recognized line type isset($item['line_width']) && in_array($item['line_width'], $line_widths) ? $line_width = $item['line_width'] : ($line_width = "1"); $graphdef .= "LINE" . $line_width . ":'{$unique_id}'#{$item['color']}:'{$label}' "; break; case "stack": case "percent": // First element in a stack has to be AREA if ($stack_counter == 0) { $graphdef .= "AREA"; $stack_counter++; } else { $graphdef .= "STACK"; } if (isset($graph_config['percent']) && $graph_config['percent'] == '1') { $graphdef .= ":'p{$unique_id}'#{$item['color']}:'{$label}' "; } else { if (isset($graph_config['scale'])) { $graphdef .= ":'s{$unique_id}'#{$item['color']}:'{$label}' "; } else { $graphdef .= ":'{$unique_id}'#{$item['color']}:'{$label}' "; } } break; // Percentile lines // Percentile lines case "percentile": $percentile = isset($item['percentile']) ? floatval($item['percentile']) : 95; $graphdef .= "VDEF:t{$unique_id}={$unique_id},{$percentile},PERCENT "; isset($item['line_width']) && in_array($item['line_width'], $line_widths) ? $line_width = $item['line_width'] : ($line_width = "1"); $graphdef .= "LINE" . $line_width . ":'t{$unique_id}'#{$item['color']}:'{$label}':dashes "; break; case "area": $graphdef .= "AREA"; $graphdef .= ":'{$unique_id}'#{$item['color']}:'{$label}' "; break; } // end of switch ( $item_type ) if ($conf['graphreport_stats']) { if (isset($graph_config['percent']) && $graph_config['percent'] == '1') { $graphdef .= legendEntry('p' . $unique_id, $conf['graphreport_stat_items']); } else { if (isset($graph_config['scale'])) { $graphdef .= legendEntry('s' . $unique_id, $conf['graphreport_stat_items']); } else { $graphdef .= legendEntry($unique_id, $conf['graphreport_stat_items']); } } } } // end of if ( is_file($metric_file) ) { } // end of foreach( $graph_config[ 'series' ] as $index => $item ) // Percentage calculation for cdefs, if required //if (isset($graph_config['percent']) && $graph_config['percent'] == '1') { $total = " CDEF:'total'="; if (count($total_ids) == 0) { // Handle nothing gracefully, do nothing } else { if (count($total_ids) == 1) { // Concat just that id, leave it at that (100%) $total .= $total_ids[0]; if (isset($graph_config['scale'])) { $total .= ",{$graph_config['scale']},*"; } $cdef = $total . ' ' . $cdef; } else { $total .= $total_ids[0]; for ($i = 1; $i < count($total_ids); $i++) { $total .= ',' . $total_ids[$i] . ',ADDNAN'; } if (isset($graph_config['scale'])) { $total .= ",{$graph_config['scale']},*"; } // Prepend total calculation $cdef = $total . ', ' . $cdef; } } //} // if graph_config['percent'] if (isset($graph_config['show_total']) && $graph_config['show_total'] == 1) { $cdef .= " LINE1:'total'#000000:'Total' " . legendEntry('total', $conf['graphreport_stat_items']); } // If we end up with the empty series it means that no RRD files matched. // This can happen if we are trying to create a report and metrics for // this host were not collected. If that happens we should create an // empty graph if ($series == "") { $rrdtool_graph['series'] = 'HRULE:1#FFCC33:"No matching metrics detected"'; } else { $rrdtool_graph['series'] = $series . ' ' . $cdef . ' ' . $graphdef; } return $rrdtool_graph; }