Skip to content

Commit

Permalink
Merge pull request #182 from jimbobmcgee/feature-optsForViews
Browse files Browse the repository at this point in the history
Allow extra rrdtool options in configured preset views
  • Loading branch information
lingej authored Mar 21, 2021
2 parents ce0eda9 + 0bbf49d commit 1af9f65
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 20 deletions.
9 changes: 8 additions & 1 deletion sample-config/pnp/config.php.in
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,14 @@ $conf['use_calendar'] = 1;
#
# Define default views with title and start timerange in seconds
#
# remarks: required escape on " with backslash
# supported keys:
# 'title' => The name of the view, e.g. "4 Hours"
# 'start' => The number of seconds before the current time from whence the graph x-axis should start
# 'extra_args' => additional arguments passed to rrdtool for this view's graph
#
# remarks:
# required escape on " with backslash
# extra_args are appended to args/opts defined elsewhere (e.g. in templates), but no effort is made to de-duplicate
#
#$views[] = array('title' => 'One Hour', 'start' => (60*60) );
$views[] = array('title' => '4 Hours', 'start' => (60*60*4) );
Expand Down
36 changes: 31 additions & 5 deletions share/pnp/application/models/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ public function getTimeRange($start=FALSE ,$end=FALSE ,$view="") {
$timerange['f_start'] = date($this->config->conf['date_fmt'],$start);
$timerange['end'] = $end;
$timerange['f_end'] = date($this->config->conf['date_fmt'],$end);
$timerange['cmd'] = " --start $start --end $end ";
$timerange['cmd'] = $this->buildViewCmd(FALSE, $start, $end);
$timerange['type'] = "start-end";
$this->TIMERANGE = $timerange;
return;
Expand Down Expand Up @@ -733,19 +733,46 @@ public function getTimeRange($start=FALSE ,$end=FALSE ,$view="") {
$timerange['f_start'] = date($this->config->conf['date_fmt'],$start);
$timerange['end'] = $end;
$timerange['f_end'] = date($this->config->conf['date_fmt'],$end);
$timerange['cmd'] = " --start $start --end $end ";
$timerange['cmd'] = $this->buildViewCmd($view, $start, $end);
$timerange['type'] = "views";
for ($i = 0; $i < count($this->config->views); $i++) {
$timerange[$i]['title'] = $this->config->views[$i]['title'];
$timerange[$i]['start'] = $end - $this->config->views[$i]['start'];
$timerange[$i]['f_start'] = date($this->config->conf['date_fmt'],$end - $this->config->views[$i]['start']);
$timerange[$i]['end'] = $end;
$timerange[$i]['f_end'] = date($this->config->conf['date_fmt'],$end);
$timerange[$i]['cmd'] = " --start " . ($end - $this->config->views[$i]['start']) . " --end $end" ;
$timerange[$i]['cmd'] = $this->buildViewCmd($i, ($end - $this->config->views[$i]['start']), $end);
$timerange[$i]['type'] = "views";
}
$this->TIMERANGE = $timerange;
}

public function buildViewCmd($view=FALSE, $start=FALSE, $end=FALSE) {
// abstract creation of RRDtool options string, so we can add more features to a view;
// implemented in this file by replacing cmd builds containing '--start'
if ($view !== FALSE && !array_key_exists($view, $this->config->views)) {
$view = 1;
}
$view = $this->config->views[$view];

$cmd = '';
if ($start !== FALSE) {
$cmd .= " --start $start";
} elseif (array_key_exists('start', $view)) {
$cmd .= (" --start " . $view['start']);
}
if ($end !== FALSE) {
$cmd .= " --end $end";
} elseif (array_key_exists('end', $view)) {
$cmd .= (" --end " . $view['end']);
}
if (array_key_exists('extra_args', $view)) {
$cmd .= (" " . $view['extra_args']);
}

return "$cmd ";
}

public function buildBasketStruct($basket,$view = NULL){
if(is_array($basket) && (!empty($basket))){
if($view == ""){
Expand Down Expand Up @@ -997,8 +1024,7 @@ public function getPageDetails($page){
public function buildXport($host,$service){
// FIXME add max rows to config
$this->XPORT = " -m 2000";
$this->XPORT .= " --start=".$this->TIMERANGE['start'];
$this->XPORT .= " --end=".$this->TIMERANGE['end'];
$this->XPORT .= $this->buildViewCmd(FALSE, $this->TIMERANGE['start'], $this->TIMERANGE['end']);
$this->readXML($host,$service);
$count = 0;
$RRAs = array('MIN','MAX','AVERAGE');
Expand Down
34 changes: 21 additions & 13 deletions share/pnp/application/views/graph_content.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,27 @@
$gid = htmlentities("?host=".urlencode($gid["host"])."&srv=".urlencode($gid["srv"]));

echo "<div start=".$value['TIMERANGE']['start']." end=".$value['TIMERANGE']['end']." style=\"width:".$value['GRAPH_WIDTH']."px; height:".$value['GRAPH_HEIGHT']."px; position:absolute; top:33px\" class=\"graph\" id=\"".$gid."\" ></div>";

$path = pnp::addToUri( array(
'host' => $value['MACRO']['HOSTNAME'],
'srv' => $value['MACRO']['SERVICEDESC'],
'view' => $value['VIEW'],
'source' => $value['SOURCE'],
'start' => $value['TIMERANGE']['start'],
'end' => $value['TIMERANGE']['end']
), FALSE
);
echo "<img class=\"graph\" src=\"".url::base(TRUE)."image" . $path . "\"></a>\n";
echo "</div>\n";
echo "</div><p>\n";

// build the URI which renders the dynamic graph image
$path = array('host' => $value['MACRO']['HOSTNAME'],
'srv' => $value['MACRO']['SERVICEDESC'],
'source' => $value['SOURCE']);

// only include `view` in the querystring if we are in a preset view; likewise, only
// include timerange start/end if we are not in a preset view; this will help later
// to differentiate between preset and custom timeranges, for display purposes
if ($value['TIMERANGE']['type']=='views') {
$path['view'] = $value['VIEW'];
} else {
$path['start'] = $value['TIMERANGE']['start'];
$path['end'] = $value['TIMERANGE']['end'];
}

$path = pnp::addToUri($path, FALSE);

echo "<img class=\"graph\" src=\"".url::base(TRUE)."image" . $path . "\"></a>\n";
echo "</div>\n";
echo "</div><p>\n";
}
echo "</div>\n";
?>
Expand Down
2 changes: 1 addition & 1 deletion share/pnp/application/views/timerange_box.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
echo "<div class=\"p4 ui-widget-content ui-corner-bottom\">\n";
$start = $this->session->get('start','');
$end = $this->session->get('end','');
$path = pnp::addToUri(array('start' => $start,'end' => $end));
$path = pnp::addToUri(array('view' => '', 'start' => $start,'end' => $end));
if($start && $end){
echo "<a class=\"multi0\" href=\"".$path."\">".Kohana::lang('common.timerange-selector-link')."</a><br>\n";
}
Expand Down

0 comments on commit 1af9f65

Please sign in to comment.