Merge branch 'feature/merge-templates'

This commit is contained in:
Alexander A. Klimov 2018-01-12 12:30:29 +01:00
commit 90026c38cb
24 changed files with 72 additions and 362 deletions

View file

@ -86,7 +86,7 @@ class GraphController extends MonitoringAwareController
*/
protected function supplyImage($checkCommand)
{
$templates = $this->getAllTemplates()->getTemplates();
$templates = $this->getAllTemplates()->getTemplates($checkCommand);
if (! isset($templates[$this->graphParams['template']])) {
throw new HttpNotFoundException($this->translate('No such template'));
}

View file

@ -38,6 +38,14 @@ of a Icinga 2 [check-command](https://www.icinga.com/docs/icinga2/latest/doc/03-
To get multiple graphs for hosts and services with this check-command, multiple
templates can reference the same check-command.
If multiple check commands do effectively the same thing and yield the same
perfdata, all of them may be specified separated by comma. E.g.:
```ini
[ping-rta.graph]
check_command = "ping, ping4, ping6"
```
**metrics_filters**
Define what metric to use and how many curves to display in the resulting graph.

View file

@ -41,13 +41,6 @@ class Template
*/
protected $urlParams = [];
/**
* The check command this template is designed for
*
* @var string|null
*/
protected $checkCommand;
/**
* Constructor
*/
@ -210,30 +203,6 @@ class Template
return $this;
}
/**
* Get the check command this template is designed for
*
* @return string|null
*/
public function getCheckCommand()
{
return $this->checkCommand;
}
/**
* Set the check command this template is designed for
*
* @param string|null $checkCommand
*
* @return $this
*/
public function setCheckCommand($checkCommand)
{
$this->checkCommand = $checkCommand;
return $this;
}
/**
* Get {@link hostNameTemplate}
*

View file

@ -18,11 +18,18 @@ use SplFileInfo;
class Templates
{
/**
* All templates by their name
* All templates by their check command and name
*
* @var Template[][]
*/
protected $templates = [];
/**
* All default templates by their name
*
* @var Template[]
*/
protected $templates = [];
protected $defaultTemplates = [];
/**
* Constructor
@ -86,7 +93,9 @@ class Templates
}
foreach ($templates as $templateName => $template) {
$checkCommand = isset($template['graph']['check_command']) ? $template['graph']['check_command'] : null;
$checkCommands = isset($template['graph']['check_command'])
? array_unique(preg_split('/\s*,\s*/', $template['graph']['check_command'], -1, PREG_SPLIT_NO_EMPTY))
: [];
unset($template['graph']['check_command']);
if (isset($template['graph'])) {
@ -230,15 +239,30 @@ class Templates
$templates[$templateName] = empty($curves) ? null : (new Template())
->setCurves($curves)
->setUrlParams($urlParams)
->setCheckCommand($checkCommand);
->setUrlParams($urlParams);
}
foreach ($templates as $templateName => $template) {
if ($template === null) {
unset($this->templates[$templateName]);
if (empty($checkCommands)) {
unset($this->defaultTemplates[$templateName]);
} else {
foreach ($checkCommands as $checkCommand) {
unset($this->templates[$checkCommand][$templateName]);
if (empty($this->templates[$checkCommand])) {
unset($this->templates[$checkCommand]);
}
}
}
} else {
$this->templates[$templateName] = $template;
if (empty($checkCommands)) {
$this->defaultTemplates[$templateName] = $template;
} else {
foreach ($checkCommands as $checkCommand) {
$this->templates[$checkCommand][$templateName] = $template;
}
}
}
}
@ -246,12 +270,14 @@ class Templates
}
/**
* Get all loaded templates by their name
* Get all loaded templates for the given check command by their name, fall back to the default one(s)
*
* @return Template[]
* @param string $checkCommand
*
* @return Template[]
*/
public function getTemplates()
public function getTemplates($checkCommand)
{
return $this->templates;
return isset($this->templates[$checkCommand]) ? $this->templates[$checkCommand] : $this->defaultTemplates;
}
}

View file

@ -198,30 +198,16 @@ abstract class Graphs extends AbstractWidget
$result = []; // kind of string builder
$filter = $this->getMonitoredObjectFilter();
$imageBaseUrl = $this->preloadDummy ? $this->getDummyImageBaseUrl() : $this->getImageBaseUrl();
$templates = static::getAllTemplates()->getTemplates();
$checkCommand = $this->obscuredCheckCommand === null ? $this->checkCommand : $this->obscuredCheckCommand;
$limit = $this->maxVisibleGraphs;
$classes = $this->classes;
$classes[] = 'images';
$div = '<div class="' . implode(' ', $classes) . '">';
$concreteTemplates = [];
$defaultTemplates = [];
foreach ($templates as $templateName => $template) {
if ($this->designedForMyMonitoredObjectType($template)) {
$templateCheckCommand = $template->getCheckCommand();
if ($templateCheckCommand === $checkCommand) {
$concreteTemplates[$templateName] = $template;
} elseif ($templateCheckCommand === null) {
$defaultTemplates[$templateName] = $template;
}
}
}
$renderedGraphs = 0;
foreach ((empty($concreteTemplates) ? $defaultTemplates : $concreteTemplates) as $templateName => $template) {
foreach (static::getAllTemplates()->getTemplates(
$this->obscuredCheckCommand === null ? $this->checkCommand : $this->obscuredCheckCommand
) as $templateName => $template) {
$charts = $template->getCharts(static::getMetricsDataSource(), $filter, $this->checkCommand);
if (! empty($charts)) {
foreach ($charts as $chart) {
@ -315,15 +301,6 @@ abstract class Graphs extends AbstractWidget
];
}
/**
* Return whether the given template is designed for the type of the monitored object we display graphs for
*
* @param Template $template
*
* @return bool
*/
abstract protected function designedForMyMonitoredObjectType(Template $template);
/**
* Return a identifier specifying the monitored object we display graphs for
*

View file

@ -45,17 +45,6 @@ class Host extends Graphs
return $url->setParam('host.name', $this->host);
}
protected function designedForMyMonitoredObjectType(Template $template)
{
foreach ($template->getCurves() as $curve) {
if (in_array('host_name_template', $curve[0]->getMacros())) {
return true;
}
}
return false;
}
protected function getMonitoredObjectIdentifier()
{
return $this->host;

View file

@ -54,17 +54,6 @@ class Service extends Graphs
return $url->setParam('host.name', $this->host)->setParam('service.name', $this->service);
}
protected function designedForMyMonitoredObjectType(Template $template)
{
foreach ($template->getCurves() as $curve) {
if (in_array('service_name_template', $curve[0]->getMacros())) {
return true;
}
}
return false;
}
protected function getMonitoredObjectIdentifier()
{
return $this->host . ':' . $this->service;

View file

@ -1,14 +0,0 @@
[dns-time.graph]
check_command = "dns"
[dns-time.metrics_filters]
value = "$service_name_template$.perfdata.time.value"
[dns-time.urlparams]
areaAlpha = "0.5"
areaMode = "all"
min = "0"
yUnitSystem = "none"
[dns-time.functions]
value = "alias(color(scale($metric$, 1000), '#1a7dd7'), 'Response time (ms)')"

View file

@ -1,5 +1,5 @@
[fping-rta.graph]
check_command = "fping"
check_command = "fping, fping4, fping6"
[fping-rta.metrics_filters]
rta.value = "$service_name_template$.perfdata.rta.value"
@ -15,7 +15,7 @@ rta.value = "alias(color(scale($metric$, 1000), '#1a7dd7'), 'Round trip time (ms
[fping-loss.graph]
check_command = "fping"
check_command = "fping, fping4, fping6"
[fping-loss.metrics_filters]
loss.value = "$service_name_template$.perfdata.loss.value"

View file

@ -1,30 +0,0 @@
[fping4-rta.graph]
check_command = "fping4"
[fping4-rta.metrics_filters]
rta.value = "$service_name_template$.perfdata.rta.value"
[fping4-rta.urlparams]
areaAlpha = "0.5"
areaMode = "all"
min = "0"
yUnitSystem = "none"
[fping4-rta.functions]
rta.value = "alias(color(scale($metric$, 1000), '#1a7dd7'), 'Round trip time (ms)')"
[fping4-loss.graph]
check_command = "fping4"
[fping4-loss.metrics_filters]
loss.value = "$service_name_template$.perfdata.loss.value"
[fping4-loss.urlparams]
areaAlpha = "0.5"
areaMode = "all"
min = "0"
yUnitSystem = "none"
[fping4-loss.functions]
loss.value = "alias(color($metric$, '#1a7dd7'), 'Packet loss (%)')"

View file

@ -1,30 +0,0 @@
[fping6-rta.graph]
check_command = "fping6"
[fping6-rta.metrics_filters]
rta.value = "$service_name_template$.perfdata.rta.value"
[fping6-rta.urlparams]
areaAlpha = "0.5"
areaMode = "all"
min = "0"
yUnitSystem = "none"
[fping6-rta.functions]
rta.value = "alias(color(scale($metric$, 1000), '#1a7dd7'), 'Round trip time (ms)')"
[fping6-loss.graph]
check_command = "fping6"
[fping6-loss.metrics_filters]
loss.value = "$service_name_template$.perfdata.loss.value"
[fping6-loss.urlparams]
areaAlpha = "0.5"
areaMode = "all"
min = "0"
yUnitSystem = "none"
[fping6-loss.functions]
loss.value = "alias(color($metric$, '#1a7dd7'), 'Packet loss (%)')"

View file

@ -1,14 +0,0 @@
[ftp-time.graph]
check_command = "ftp"
[ftp-time.metrics_filters]
value = "$service_name_template$.perfdata.time.value"
[ftp-time.urlparams]
areaAlpha = "0.5"
areaMode = "all"
min = "0"
yUnitSystem = "none"
[ftp-time.functions]
value = "alias(color(scale($metric$, 1000), '#1a7dd7'), 'Response time (ms)')"

View file

@ -1,30 +0,0 @@
[http-time.graph]
check_command = "http"
[http-time.metrics_filters]
value = "$service_name_template$.perfdata.time.value"
[http-time.urlparams]
areaAlpha = "0.5"
areaMode = "all"
min = "0"
yUnitSystem = "none"
[http-time.functions]
value = "alias(color(scale($metric$, 1000), '#1a7dd7'), 'Response time (ms)')"
[http-size.graph]
check_command = "http"
[http-size.metrics_filters]
value = "$service_name_template$.perfdata.size.value"
[http-size.urlparams]
areaAlpha = "0.5"
areaMode = "all"
min = "0"
yUnitSystem = "binary"
[http-size.functions]
value = "alias(color($metric$, '#1a7dd7'), 'Response size (bytes)')"

View file

@ -1,14 +0,0 @@
[imap-time.graph]
check_command = "imap"
[imap-time.metrics_filters]
value = "$service_name_template$.perfdata.time.value"
[imap-time.urlparams]
areaAlpha = "0.5"
areaMode = "all"
min = "0"
yUnitSystem = "none"
[imap-time.functions]
value = "alias(color(scale($metric$, 1000), '#1a7dd7'), 'Response time (ms)')"

View file

@ -1,14 +0,0 @@
[ldap-time.graph]
check_command = "ldap"
[ldap-time.metrics_filters]
value = "$service_name_template$.perfdata.time.value"
[ldap-time.urlparams]
areaAlpha = "0.5"
areaMode = "all"
min = "0"
yUnitSystem = "none"
[ldap-time.functions]
value = "alias(color(scale($metric$, 1000), '#1a7dd7'), 'Response time (ms)')"

View file

@ -1,5 +1,5 @@
[ping-rta.graph]
check_command = "ping"
check_command = "ping, ping4, ping6"
[ping-rta.metrics_filters]
rta.value = "$service_name_template$.perfdata.rta.value"
@ -15,7 +15,7 @@ rta.value = "alias(color(scale($metric$, 1000), '#1a7dd7'), 'Round trip time (ms
[ping-pl.graph]
check_command = "ping"
check_command = "ping, ping4, ping6"
[ping-pl.metrics_filters]
pl.value = "$service_name_template$.perfdata.pl.value"

View file

@ -1,30 +0,0 @@
[ping4-rta.graph]
check_command = "ping4"
[ping4-rta.metrics_filters]
rta.value = "$service_name_template$.perfdata.rta.value"
[ping4-rta.urlparams]
areaAlpha = "0.5"
areaMode = "all"
min = "0"
yUnitSystem = "none"
[ping4-rta.functions]
rta.value = "alias(color(scale($metric$, 1000), '#1a7dd7'), 'Round trip time (ms)')"
[ping4-pl.graph]
check_command = "ping4"
[ping4-pl.metrics_filters]
pl.value = "$service_name_template$.perfdata.pl.value"
[ping4-pl.urlparams]
areaAlpha = "0.5"
areaMode = "all"
min = "0"
yUnitSystem = "none"
[ping4-pl.functions]
pl.value = "alias(color($metric$, '#1a7dd7'), 'Packet loss (%)')"

View file

@ -1,30 +0,0 @@
[ping6-rta.graph]
check_command = "ping6"
[ping6-rta.metrics_filters]
rta.value = "$service_name_template$.perfdata.rta.value"
[ping6-rta.urlparams]
areaAlpha = "0.5"
areaMode = "all"
min = "0"
yUnitSystem = "none"
[ping6-rta.functions]
rta.value = "alias(color(scale($metric$, 1000), '#1a7dd7'), 'Round trip time (ms)')"
[ping6-pl.graph]
check_command = "ping6"
[ping6-pl.metrics_filters]
pl.value = "$service_name_template$.perfdata.pl.value"
[ping6-pl.urlparams]
areaAlpha = "0.5"
areaMode = "all"
min = "0"
yUnitSystem = "none"
[ping6-pl.functions]
pl.value = "alias(color($metric$, '#1a7dd7'), 'Packet loss (%)')"

View file

@ -0,0 +1,14 @@
[response-size.graph]
check_command = "http"
[response-size.metrics_filters]
value = "$service_name_template$.perfdata.size.value"
[response-size.urlparams]
areaAlpha = "0.5"
areaMode = "all"
min = "0"
yUnitSystem = "binary"
[response-size.functions]
value = "alias(color($metric$, '#1a7dd7'), 'Response size (bytes)')"

View file

@ -1,14 +1,14 @@
[dig-time.graph]
check_command = "dig"
[response-time.graph]
check_command = "dig, dns, ftp, http, imap, ldap, smtp, ssh, tcp, udp"
[dig-time.metrics_filters]
[response-time.metrics_filters]
value = "$service_name_template$.perfdata.time.value"
[dig-time.urlparams]
[response-time.urlparams]
areaAlpha = "0.5"
areaMode = "all"
min = "0"
yUnitSystem = "none"
[dig-time.functions]
[response-time.functions]
value = "alias(color(scale($metric$, 1000), '#1a7dd7'), 'Response time (ms)')"

View file

@ -1,14 +0,0 @@
[smtp-time.graph]
check_command = "smtp"
[smtp-time.metrics_filters]
value = "$service_name_template$.perfdata.time.value"
[smtp-time.urlparams]
areaAlpha = "0.5"
areaMode = "all"
min = "0"
yUnitSystem = "none"
[smtp-time.functions]
value = "alias(color(scale($metric$, 1000), '#1a7dd7'), 'Response time (ms)')"

View file

@ -1,14 +0,0 @@
[ssh.graph]
check_command = "ssh"
[ssh.metrics_filters]
value = "$service_name_template$.perfdata.time.value"
[ssh.urlparams]
areaAlpha = "0.5"
areaMode = "all"
min = "0"
yUnitSystem = "none"
[ssh.functions]
value = "alias(color(scale($metric$, 1000), '#1a7dd7'), 'Response time (ms)')"

View file

@ -1,14 +0,0 @@
[tcp-time.graph]
check_command = "tcp"
[tcp-time.metrics_filters]
value = "$service_name_template$.perfdata.time.value"
[tcp-time.urlparams]
areaAlpha = "0.5"
areaMode = "all"
min = "0"
yUnitSystem = "none"
[tcp-time.functions]
value = "alias(color(scale($metric$, 1000), '#1a7dd7'), 'Response time (ms)')"

View file

@ -1,14 +0,0 @@
[udp-time.graph]
check_command = "udp"
[udp-time.metrics_filters]
value = "$service_name_template$.perfdata.time.value"
[udp-time.urlparams]
areaAlpha = "0.5"
areaMode = "all"
min = "0"
yUnitSystem = "none"
[udp-time.functions]
value = "alias(color(scale($metric$, 1000), '#1a7dd7'), 'Response time (ms)')"