From e723b04a049b865293b24fa950dcda59f4552c3a Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Fri, 29 Sep 2017 17:36:46 +0200 Subject: [PATCH] Query only the templates designed for the monitored object's check command refs #55 --- application/controllers/ListController.php | 5 ++- application/views/scripts/list/hosts.phtml | 4 +- application/views/scripts/list/services.phtml | 4 +- library/Graphite/Graphing/Template.php | 31 +++++++++++++++ library/Graphite/Graphing/Templates.php | 3 +- library/Graphite/Web/Widget/Graphs.php | 38 +++++++++++++++---- library/Graphite/Web/Widget/Graphs/Host.php | 7 +++- .../Graphite/Web/Widget/Graphs/Service.php | 9 +++-- 8 files changed, 83 insertions(+), 18 deletions(-) diff --git a/application/controllers/ListController.php b/application/controllers/ListController.php index 13ceace..f881342 100644 --- a/application/controllers/ListController.php +++ b/application/controllers/ListController.php @@ -29,7 +29,7 @@ class ListController extends MonitoringAwareController ); $this->view->hosts = $hosts = $this->applyMonitoringRestriction( - $this->backend->select()->from('hoststatus', ['host_name', 'host_display_name']) + $this->backend->select()->from('hoststatus', ['host_name', 'host_display_name', 'host_check_command']) ); $this->filterQuery($hosts); @@ -54,7 +54,8 @@ class ListController extends MonitoringAwareController 'host_name', 'host_display_name', 'service_description', - 'service_display_name' + 'service_display_name', + 'service_check_command' ]) ); diff --git a/application/views/scripts/list/hosts.phtml b/application/views/scripts/list/hosts.phtml index c7ffd23..7809718 100644 --- a/application/views/scripts/list/hosts.phtml +++ b/application/views/scripts/list/hosts.phtml @@ -39,7 +39,9 @@ if ($filterEditor->getFilter()->isEmpty()) { ) . ''; } - echo (new Host($host->host_name))->setCompact()->handleRequest(); + echo (new Host($host->host_name, $host->host_check_command)) + ->setCompact() + ->handleRequest(); echo ''; } diff --git a/application/views/scripts/list/services.phtml b/application/views/scripts/list/services.phtml index ef8a395..0b42b12 100644 --- a/application/views/scripts/list/services.phtml +++ b/application/views/scripts/list/services.phtml @@ -49,7 +49,9 @@ if ($filterEditor->getFilter()->isEmpty()) { ) . ''; } - echo (new Service($service->host_name, $service->service_description))->setCompact()->handleRequest(); + echo (new Service($service->host_name, $service->service_description, $service->service_check_command)) + ->setCompact() + ->handleRequest(); } if (! $compact && $services->hasMore()) { diff --git a/library/Graphite/Graphing/Template.php b/library/Graphite/Graphing/Template.php index 41ae992..a86fd5e 100644 --- a/library/Graphite/Graphing/Template.php +++ b/library/Graphite/Graphing/Template.php @@ -24,6 +24,13 @@ class Template */ protected $urlParams = []; + /** + * The check command this template is designed for + * + * @var string + */ + protected $checkCommand; + /** * Constructor */ @@ -173,4 +180,28 @@ class Template return $this; } + + /** + * Get the check command this template is designed for + * + * @return string + */ + public function getCheckCommand() + { + return $this->checkCommand; + } + + /** + * Set the check command this template is designed for + * + * @param string $checkCommand + * + * @return $this + */ + public function setCheckCommand($checkCommand) + { + $this->checkCommand = $checkCommand; + + return $this; + } } diff --git a/library/Graphite/Graphing/Templates.php b/library/Graphite/Graphing/Templates.php index 379d689..ff80ea0 100644 --- a/library/Graphite/Graphing/Templates.php +++ b/library/Graphite/Graphing/Templates.php @@ -264,7 +264,8 @@ class Templates $templates[$templateName] = (new Template()) ->setCurves($curves) - ->setUrlParams($urlParams); + ->setUrlParams($urlParams) + ->setCheckCommand($checkCommand); } foreach ($templates as $templateName => $template) { diff --git a/library/Graphite/Web/Widget/Graphs.php b/library/Graphite/Web/Widget/Graphs.php index fe65078..f36689b 100644 --- a/library/Graphite/Web/Widget/Graphs.php +++ b/library/Graphite/Web/Widget/Graphs.php @@ -55,6 +55,13 @@ abstract class Graphs extends AbstractWidget */ protected $compact = false; + /** + * The check command of the monitored object we display graphs for + * + * @var string + */ + protected $checkCommand; + /** * Factory, based on the given object * @@ -67,14 +74,31 @@ abstract class Graphs extends AbstractWidget switch ($object->getType()) { case 'host': /** @var Host $object */ - return (new HostGraphs($object->getName())); + return new HostGraphs( + $object->getName(), + $object->host_check_command + ); case 'service': /** @var Service $object */ - return (new ServiceGraphs($object->getHost()->getName(), $object->getName())); + return new ServiceGraphs( + $object->getHost()->getName(), + $object->getName(), + $object->service_check_command + ); } } + /** + * Constructor + * + * @param string $checkCommand The check command of the monitored object we display graphs for + */ + public function __construct($checkCommand) + { + $this->checkCommand = $checkCommand; + } + /** * Process the given request using this widget * @@ -104,13 +128,11 @@ abstract class Graphs extends AbstractWidget $filter = $this->getMonitoredObjectFilter(); $imageBaseUrl = $this->getImageBaseUrl(); $templates = static::getAllTemplates()->getTemplates(); - $templateNames = array_keys($templates); - shuffle($templateNames); - - foreach ($templateNames as $templateName) { - if ($this->designedForMyMonitoredObjectType($templates[$templateName])) { - $charts = $templates[$templateName]->getCharts(static::getMetricsDataSource(), $filter); + foreach ($templates as $templateName => $template) { + if ($this->designedForMyMonitoredObjectType($template) + && $template->getCheckCommand() === $this->checkCommand) { + $charts = $template->getCharts(static::getMetricsDataSource(), $filter); if (! empty($charts)) { $result[] = '
'; diff --git a/library/Graphite/Web/Widget/Graphs/Host.php b/library/Graphite/Web/Widget/Graphs/Host.php index 4b3d9c4..ee9831c 100644 --- a/library/Graphite/Web/Widget/Graphs/Host.php +++ b/library/Graphite/Web/Widget/Graphs/Host.php @@ -18,10 +18,13 @@ class Host extends Graphs /** * Constructor * - * @param string $host The host to render the graphs of + * @param string $host The host to render the graphs of + * @param string $checkCommand The check command of the monitored object we display graphs for */ - public function __construct($host) + public function __construct($host, $checkCommand) { + parent::__construct($checkCommand); + $this->host = $host; } diff --git a/library/Graphite/Web/Widget/Graphs/Service.php b/library/Graphite/Web/Widget/Graphs/Service.php index 39ac18f..a74d1d6 100644 --- a/library/Graphite/Web/Widget/Graphs/Service.php +++ b/library/Graphite/Web/Widget/Graphs/Service.php @@ -25,11 +25,14 @@ class Service extends Graphs /** * Constructor * - * @param string $host The host to render the graphs of - * @param string $service The service to render the graphs of + * @param string $host The host to render the graphs of + * @param string $service The service to render the graphs of + * @param string $checkCommand The check command of the monitored object we display graphs for */ - public function __construct($host, $service) + public function __construct($host, $service, $checkCommand) { + parent::__construct($checkCommand); + $this->host = $host; $this->service = $service; }