From 45c29fb45348d362716ee8deca0207f6bd233907 Mon Sep 17 00:00:00 2001 From: PhiBo Date: Thu, 1 Jul 2021 14:33:15 +0200 Subject: [PATCH] Add new filters to apply dashboards --- application/forms/Graph/GraphForm.php | 33 ++++++++++++++++++++ application/views/scripts/graph/index.phtml | 4 +++ library/Grafana/ProvidedHook/Grapher.php | 34 ++++++++++++++++----- 3 files changed, 63 insertions(+), 8 deletions(-) diff --git a/application/forms/Graph/GraphForm.php b/application/forms/Graph/GraphForm.php index 2b8690d..e08964f 100644 --- a/application/forms/Graph/GraphForm.php +++ b/application/forms/Graph/GraphForm.php @@ -47,6 +47,31 @@ public function createElements(array $formData) ) ); + $this->addElement( + 'text', + 'serviceFilter', + array( + 'description' => $this->translate( + 'Use the dashboard if the filter matches the service name. (String or RegEx) ' + . '(Examples: "disk.*" or "disk /var/(log|lib).*")' + ), + 'label' => $this->translate('Service filter'), + 'required' => false + ) + ); + + $this->addElement( + 'text', + 'commandFilter', + array( + 'description' => $this->translate( + 'Use the dashboard if the filter matches the check_command. (String or RegEx)' + ), + 'label' => $this->translate('Command filter'), + 'required' => false + ) + ); + $this->addElement( 'text', 'dashboard', @@ -185,6 +210,8 @@ public function onSuccess() { $name = $this->getElement('name')->getValue(); $values = array( + 'serviceFilter' => $this->getElement('serviceFilter')->getValue(), + 'commandFilter' => $this->getElement('commandFilter')->getValue(), 'dashboard' => $this->getElement('dashboard')->getValue(), 'panelId' => $this->getElement('panelId')->getValue(), 'orgId' => $this->getElement('orgId')->getValue(), @@ -197,6 +224,12 @@ public function onSuccess() 'dashboarduid' => $this->getElement('dashboarduid')->getValue() ); + if (empty($values['serviceFilter'])) { + $values['serviceFilter'] = null; + } + if (empty($values['commandFilter'])) { + $values['commandFilter'] = null; + } if (empty($values['timerange'])) { $values['timerange'] = null; diff --git a/application/views/scripts/graph/index.phtml b/application/views/scripts/graph/index.phtml index 98024a0..9e67c6f 100644 --- a/application/views/scripts/graph/index.phtml +++ b/application/views/scripts/graph/index.phtml @@ -20,6 +20,8 @@ translate('Name') ?> + translate('Service filter') ?> + translate('Command filter') ?> translate('Dashboard') ?> translate('Dashboard UID') ?> translate('PanelID') ?> @@ -43,6 +45,8 @@ array('title' => sprintf($this->translate('Update Grafana graph %s'), $name)) ) ?> + escape($graph->serviceFilter) ?> + escape($graph->commandFilter) ?> escape($graph->dashboard) ?> escape($graph->dashboarduid) ?> escape($graph->panelId) ?> diff --git a/library/Grafana/ProvidedHook/Grapher.php b/library/Grafana/ProvidedHook/Grapher.php index 2fa799d..e7e7ed8 100644 --- a/library/Grafana/ProvidedHook/Grapher.php +++ b/library/Grafana/ProvidedHook/Grapher.php @@ -165,16 +165,34 @@ private function getGraphConf($serviceName, $serviceCommand = null) $this->graphConfig = Config::module('grafana', 'graphs'); - if ($this->graphConfig->hasSection(strtok($serviceName, - ' ')) && ($this->graphConfig->hasSection($serviceName) == false)) { - $serviceName = strtok($serviceName, ' '); + $selectedSectionName = null; + foreach ($this->graphConfig->keys() as $sectionName) { + $serviceFilter = $this->getGraphConfigOption($sectionName, 'serviceFilter'); + $commandFilter = $this->getGraphConfigOption($sectionName, 'commandFilter'); + if ($serviceFilter == null && $commandFilter == null) { + continue; + } + if (($serviceFilter == null || preg_match('/' . $serviceFilter . '/', $serviceName)) && + ($commandFilter == null || preg_match('/' . $commandFilter . '/', $serviceCommand))) { + $selectedSectionName = $sectionName; + break; + } } - if ($this->graphConfig->hasSection(strtok($serviceName, - ' ')) == false && ($this->graphConfig->hasSection($serviceName) == false)) { - $serviceName = $serviceCommand; - if ($this->graphConfig->hasSection($serviceCommand) == false && $this->defaultDashboard == 'none') { - return null; + + if ($selectedSectionName == null) { + if ($this->graphConfig->hasSection(strtok($serviceName, + ' ')) && ($this->graphConfig->hasSection($serviceName) == false)) { + $serviceName = strtok($serviceName, ' '); } + if ($this->graphConfig->hasSection(strtok($serviceName, + ' ')) == false && ($this->graphConfig->hasSection($serviceName) == false)) { + $serviceName = $serviceCommand; + if ($this->graphConfig->hasSection($serviceCommand) == false && $this->defaultDashboard == 'none') { + return null; + } + } + } else { + $serviceName = $selectedSectionName; } $this->dashboard = $this->getGraphConfigOption($serviceName, 'dashboard', $this->defaultDashboard);