diff --git a/application/controllers/GraphController.php b/application/controllers/GraphController.php index 5dc16e9..27b9c4a 100644 --- a/application/controllers/GraphController.php +++ b/application/controllers/GraphController.php @@ -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')); } diff --git a/doc/04-Templates.md b/doc/04-Templates.md index 2284182..4fb16b8 100644 --- a/doc/04-Templates.md +++ b/doc/04-Templates.md @@ -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. diff --git a/library/Graphite/Graphing/Template.php b/library/Graphite/Graphing/Template.php index e8f11fe..e0b59b5 100644 --- a/library/Graphite/Graphing/Template.php +++ b/library/Graphite/Graphing/Template.php @@ -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} * diff --git a/library/Graphite/Graphing/Templates.php b/library/Graphite/Graphing/Templates.php index f31762c..39eb199 100644 --- a/library/Graphite/Graphing/Templates.php +++ b/library/Graphite/Graphing/Templates.php @@ -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; } } diff --git a/library/Graphite/Web/Widget/Graphs.php b/library/Graphite/Web/Widget/Graphs.php index 3553e84..2d2d07b 100644 --- a/library/Graphite/Web/Widget/Graphs.php +++ b/library/Graphite/Web/Widget/Graphs.php @@ -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 = '
'; - $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 * diff --git a/library/Graphite/Web/Widget/Graphs/Host.php b/library/Graphite/Web/Widget/Graphs/Host.php index 1b67ca9..33763a2 100644 --- a/library/Graphite/Web/Widget/Graphs/Host.php +++ b/library/Graphite/Web/Widget/Graphs/Host.php @@ -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; diff --git a/library/Graphite/Web/Widget/Graphs/Service.php b/library/Graphite/Web/Widget/Graphs/Service.php index f102ebf..2f01ffb 100644 --- a/library/Graphite/Web/Widget/Graphs/Service.php +++ b/library/Graphite/Web/Widget/Graphs/Service.php @@ -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; diff --git a/templates/dns.ini b/templates/dns.ini deleted file mode 100644 index e46cc87..0000000 --- a/templates/dns.ini +++ /dev/null @@ -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)')" \ No newline at end of file diff --git a/templates/fping.ini b/templates/fping.ini index eff820a..f78876a 100644 --- a/templates/fping.ini +++ b/templates/fping.ini @@ -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" diff --git a/templates/fping4.ini b/templates/fping4.ini deleted file mode 100644 index da78491..0000000 --- a/templates/fping4.ini +++ /dev/null @@ -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 (%)')" diff --git a/templates/fping6.ini b/templates/fping6.ini deleted file mode 100644 index 251af3b..0000000 --- a/templates/fping6.ini +++ /dev/null @@ -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 (%)')" diff --git a/templates/ftp.ini b/templates/ftp.ini deleted file mode 100644 index 623b0ad..0000000 --- a/templates/ftp.ini +++ /dev/null @@ -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)')" diff --git a/templates/http.ini b/templates/http.ini deleted file mode 100644 index be23f7e..0000000 --- a/templates/http.ini +++ /dev/null @@ -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)')" diff --git a/templates/imap.ini b/templates/imap.ini deleted file mode 100644 index 1672cda..0000000 --- a/templates/imap.ini +++ /dev/null @@ -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)')" diff --git a/templates/ldap.ini b/templates/ldap.ini deleted file mode 100644 index fa76f2c..0000000 --- a/templates/ldap.ini +++ /dev/null @@ -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)')" diff --git a/templates/ping.ini b/templates/ping.ini index 8468b10..3891297 100644 --- a/templates/ping.ini +++ b/templates/ping.ini @@ -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" diff --git a/templates/ping4.ini b/templates/ping4.ini deleted file mode 100644 index 45a2a8e..0000000 --- a/templates/ping4.ini +++ /dev/null @@ -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 (%)')" diff --git a/templates/ping6.ini b/templates/ping6.ini deleted file mode 100644 index 00862a7..0000000 --- a/templates/ping6.ini +++ /dev/null @@ -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 (%)')" diff --git a/templates/response-size.ini b/templates/response-size.ini new file mode 100644 index 0000000..4991f4e --- /dev/null +++ b/templates/response-size.ini @@ -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)')" diff --git a/templates/dig.ini b/templates/response-time.ini similarity index 53% rename from templates/dig.ini rename to templates/response-time.ini index d1dbc31..e278290 100644 --- a/templates/dig.ini +++ b/templates/response-time.ini @@ -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)')" diff --git a/templates/smtp.ini b/templates/smtp.ini deleted file mode 100644 index b3e3e9f..0000000 --- a/templates/smtp.ini +++ /dev/null @@ -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)')" diff --git a/templates/ssh.ini b/templates/ssh.ini deleted file mode 100644 index 33ab339..0000000 --- a/templates/ssh.ini +++ /dev/null @@ -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)')" diff --git a/templates/tcp.ini b/templates/tcp.ini deleted file mode 100644 index 25f11b7..0000000 --- a/templates/tcp.ini +++ /dev/null @@ -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)')" diff --git a/templates/udp.ini b/templates/udp.ini deleted file mode 100644 index 8d742c6..0000000 --- a/templates/udp.ini +++ /dev/null @@ -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)')"