Skip to content

Commit

Permalink
changed functions for templates to match new layout
Browse files Browse the repository at this point in the history
  • Loading branch information
Griesbacher committed Jan 29, 2016
1 parent 5ee3cb2 commit f523601
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 89 deletions.
2 changes: 1 addition & 1 deletion histou/grafana/dashboard.php
Expand Up @@ -133,7 +133,7 @@ public function addAnnotation($name, $hostname, $servicename, $iconColor, $lineC
"iconSize" => $iconSize,
"lineColor" => $lineColor,
"name" => $name,
"query" => "SELECT * FROM \"$hostname&$servicename&messages\" WHERE \"type\" = '$name' AND \$timeFilter",
"query" => "SELECT * FROM messages WHERE type = '$name' AND host = '$hostname' AND service = '$servicename' AND \$timeFilter",
"showLine" => true,
"tagsColumn" => "author",
"textColumn" => "value",
Expand Down
121 changes: 115 additions & 6 deletions histou/grafana/graphpanel.php
Expand Up @@ -56,6 +56,7 @@ public function __construct($title, $legendShow = SHOW_LEGEND, $id = -1)
$this->data['linewidth'] = 2;
$this->data['targets'] = array();
$this->data['seriesOverrides'] = array();
$this->data['datasource'] = "-- Mixed --";
}

/**
Expand All @@ -75,7 +76,7 @@ public function setTooltip(array $tooltip)
@param array $tags tags for the query.
@return null.
**/
public function addTargetSimple($target, $alias = "", $factor = 1, array $tags = array())
/*public function addTargetSimple($target, $alias = "", $factor = 1, array $tags = array())
{
if ($factor == 1) {
$influxdbQuery = sprintf('select mean(value) from "%s" where AND $timeFilter group by time($interval)', $target);
Expand Down Expand Up @@ -104,7 +105,7 @@ public function addTargetSimple($target, $alias = "", $factor = 1, array $tags =
"datasource" => INFLUX_DB
)
);
}
}*/

/**
Changes the color of a line.
Expand Down Expand Up @@ -235,7 +236,7 @@ private function convertUnit($unit)
@param string $color hexcolor.
@return null.
**/
private function addThreshold($host, $service, $command, $perfLabel, $type, $color, $alias)
/*private function addThreshold($host, $service, $command, $perfLabel, $type, $color, $alias)
{
foreach (array('normal', 'min', 'max') as $tag) {
$target = \histou\helper\str::influxdbTablename($host, $service, $command, $perfLabel, $type);
Expand All @@ -260,7 +261,7 @@ private function addThreshold($host, $service, $command, $perfLabel, $type, $col
);
$this->addAliasColor($localAlias, $color);
}
}
}*/

/**
Adds yellow warning lines
Expand Down Expand Up @@ -307,7 +308,7 @@ public function setLinewidth($width)
@param string $color Color of the dots
@return null.
**/
public function addDowntime($host, $service, $command, $perfLabel, $color = '#EEE')
/*public function addDowntime($host, $service, $command, $perfLabel, $color = '#EEE')
{
$target = sprintf(
'%s%s%s%s%s%s%s%svalue',
Expand Down Expand Up @@ -338,7 +339,7 @@ public function addDowntime($host, $service, $command, $perfLabel, $color = '#EE
)
);
$this->addAliasColor($alias, $color);
}
}*/

/**
Fills the area below a line.
Expand Down Expand Up @@ -386,4 +387,112 @@ public function setYAxis($alias, $number = 1)
)
);
}

public function createTarget($host, $service, $command, $performanceLabel, array $filterTags = array())
{
return array(
'measurement' => 'metrics',
'alias' => '$col',
'select' => array(),
'tags' => $this->createFilterTags($host, $service, $command, $performanceLabel, $filterTags),
'dsType' => 'influxdb',
'resultFormat' => 'time_series',
'datasource' => INFLUX_DB
);
}

/**
Creates filter tags array based on host, service...
**/
public function createFilterTags($host, $service, $command, $performanceLabel, array $filterTags = array())
{
$tags = array();
$filter = array('host' => $host, 'service' => $service, 'command' => $command, 'performanceLabel' => $performanceLabel);
$filter = array_merge($filter, $filterTags);
$i = 0;
foreach ($filter as $key => $value) {
if ($i == 0) {
array_push($tags, array('key'=> $key, 'operator' => '=', 'value' => $value ));
} else {
array_push($tags, array('condition' => 'AND', 'key'=> $key, 'operator' => '=', 'value' => $value ));
}
$i++;
}
return $tags;
}

/**
This creates a target with an value.
**/
public function genTargetSimple($host, $service, $command, $performanceLabel, $alias = '')
{
if ($alias == '') {
$alias = $performanceLabel;
}
$target = $this->createTarget($host, $service, $command, $performanceLabel);
$target = $this->addXToTarget($target, array('value'), $alias, '#085DFF');
return $target;
}

public function addWarnToTarget($target, $alias = '')
{
return $this->addXToTarget($target, array('warn', 'warn-min', 'warn-max'), $alias, '#FFFC15');
}

public function addCritToTarget($target, $alias = '')
{
return $this->addXToTarget($target, array('crit', 'crit-min', 'crit-max'), $alias, '#FF3727');
}

private function addXToTarget($target, $types, $alias, $color, $keepAlias = true)
{
foreach ($types as $type) {
if ($keepAlias) {
$alias = $alias.'-'.$type;
}
array_push($target['select'], $this->createSelect($type, $alias));
$this->addAliasColor($alias, $color);
}
return $target;
}

public function createSelect($name, $alias)
{
return array(
array('type' => 'field', 'params' => array($name)),
array('type' => 'mean', 'params' => array()),
array('type' => 'alias', 'params' => array($alias))
);
}
/**
This creates a target for an downtime.
**/
public function genDowntimeTarget($host, $service, $command, $performanceLabel, $alias = '')
{
if ($alias == '') {
$alias = 'downtime';
}
$target = $this->createTarget($host, $service, $command, $performanceLabel, array('downtime' => "true"));
$target = $this->addXToTarget($target, array('value'), $alias, '#EEE', false);
array_push(
$this->data['seriesOverrides'],
array(
'lines' => true,
'alias' => $alias,
'linewidth' => 3,
'legend' => false,
'fill' => 3,
)
);
return $target;
}


/**
Adds the target to the dashboard.
**/
public function addTarget($target)
{
array_push($this->data['targets'], $target);
}
}
124 changes: 42 additions & 82 deletions templates/default/default.php
Expand Up @@ -18,94 +18,54 @@

$genTemplate = function ($perfData) {
/*$perfData:
Array
(
[host] => debian
[service] => http
[perfLabel] => Array
(
[size] => Array
(
[value] => Array
(
[unit] => B
)
Array
(
[host] => debian
[service] => hostcheck
[perfLabel] => Array
(
[pl] => Array
(
[crit] => 100
[fill] => none
[max] =>
[min] => 0
[type] => normal
[unit] => %
[value] => 0
[warn] => 80
)
[min] => Array
(
[unit] => B
)
[rta] => Array
(
[crit] => 5000
[fill] => none
[max] =>
[min] => 0
[type] => normal
[unit] => ms
[value] => 0.045
[warn] => 3000
)
)
)
[time] => Array
(
[value] => Array
(
[unit] => s
)
[min] => Array
(
[unit] => s
)
)
)
[command] => http
)
[command] => command
)
*/

$perfKeys = array_keys($perfData['perfLabel']);
$dashboard = new \histou\grafana\Dashboard($perfData['host'].'-'.$perfData['service']);
for ($i = 0; $i < sizeof($perfData['perfLabel']); $i++) {
$row = new \histou\grafana\Row($perfData['service'].' '.$perfData['command']);
$panel = new \histou\grafana\GraphPanel(
$perfData['host'].' '.$perfData['service']
.' '.$perfData['command'].' '.$perfKeys[$i]
);
//add value graph
$target = sprintf(
'%s%s%s%s%s%s%s%s%s',
$perfData['host'],
INFLUX_FIELDSEPERATOR,
$perfData['service'],
INFLUX_FIELDSEPERATOR,
$perfData['command'],
INFLUX_FIELDSEPERATOR,
$perfKeys[$i],
INFLUX_FIELDSEPERATOR,
"value"
);
$alias = $perfData['host']." ".$perfData['service']." ".$perfKeys[$i]." value";
$panel->addAliasColor($alias, '#085DFF');
$panel->addTargetSimple($target, $alias);
$panel->fillBelowLine($alias, 2);
//Add Label
if (isset($perfData['perfLabel'][$perfKeys[$i]]['value']['unit'])) {
$panel->setLeftUnit($perfData['perfLabel'][$perfKeys[$i]]['value']['unit']);
}
//Add Warning and Critical
$panel->addWarning(
$perfData['host'],
$perfData['service'],
$perfData['command'],
$perfKeys[$i]
);
$panel->addCritical(
$perfData['host'],
$perfData['service'],
$perfData['command'],
$perfKeys[$i]
);
$panel->addDowntime(
$perfData['host'],
$perfData['service'],
$perfData['command'],
$perfKeys[$i]
);
foreach($perfData['perfLabel'] as $key => $values){
$row = new \histou\grafana\Row($perfData['host'].' '.$perfData['service'].' '.$perfData['command']);
$panel = new \histou\grafana\GraphPanel($perfData['host'].' '.$perfData['service'].' '.$perfData['command'].' '.$key);
$target = $panel->genTargetSimple($perfData['host'], $perfData['service'], $perfData['command'], $key);
$target = $panel->addWarnToTarget($target, $key);
$target = $panel->addCritToTarget($target, $key);
$panel->addTarget($target);

$downtime = $panel->genDowntimeTarget($perfData['host'], $perfData['service'], $perfData['command'], $key);
$panel->addTarget($downtime);

$row->addPanel($panel);
$dashboard->addRow($row);
}
Expand Down

0 comments on commit f523601

Please sign in to comment.