From f5236016112298bb5c9231573a043f3507b419b2 Mon Sep 17 00:00:00 2001 From: Griesbacher Date: Fri, 29 Jan 2016 11:23:52 +0100 Subject: [PATCH] changed functions for templates to match new layout --- histou/grafana/dashboard.php | 2 +- histou/grafana/graphpanel.php | 121 +++++++++++++++++++++++++++++++-- templates/default/default.php | 124 ++++++++++++---------------------- 3 files changed, 158 insertions(+), 89 deletions(-) diff --git a/histou/grafana/dashboard.php b/histou/grafana/dashboard.php index 0724616..d0412fa 100644 --- a/histou/grafana/dashboard.php +++ b/histou/grafana/dashboard.php @@ -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", diff --git a/histou/grafana/graphpanel.php b/histou/grafana/graphpanel.php index 2e113c7..9c76213 100644 --- a/histou/grafana/graphpanel.php +++ b/histou/grafana/graphpanel.php @@ -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 --"; } /** @@ -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); @@ -104,7 +105,7 @@ public function addTargetSimple($target, $alias = "", $factor = 1, array $tags = "datasource" => INFLUX_DB ) ); - } + }*/ /** Changes the color of a line. @@ -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); @@ -260,7 +261,7 @@ private function addThreshold($host, $service, $command, $perfLabel, $type, $col ); $this->addAliasColor($localAlias, $color); } - } + }*/ /** Adds yellow warning lines @@ -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', @@ -338,7 +339,7 @@ public function addDowntime($host, $service, $command, $perfLabel, $color = '#EE ) ); $this->addAliasColor($alias, $color); - } + }*/ /** Fills the area below a line. @@ -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); + } } diff --git a/templates/default/default.php b/templates/default/default.php index eb829cb..d6dc2de 100644 --- a/templates/default/default.php +++ b/templates/default/default.php @@ -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); }