Skip to content

Commit

Permalink
Add new filters to apply dashboards
Browse files Browse the repository at this point in the history
  • Loading branch information
phibos committed Aug 29, 2022
1 parent 7e27158 commit 45c29fb
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
33 changes: 33 additions & 0 deletions application/forms/Graph/GraphForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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(),
Expand All @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions application/views/scripts/graph/index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
<thead>
<tr>
<th><?= $this->translate('Name') ?></th>
<th><?= $this->translate('Service filter') ?></th>
<th><?= $this->translate('Command filter') ?></th>
<th><?= $this->translate('Dashboard') ?></th>
<th><?= $this->translate('Dashboard UID') ?></th>
<th><?= $this->translate('PanelID') ?></th>
Expand All @@ -43,6 +45,8 @@
array('title' => sprintf($this->translate('Update Grafana graph %s'), $name))
) ?>
</td>
<td><?= $this->escape($graph->serviceFilter) ?></td>
<td><?= $this->escape($graph->commandFilter) ?></td>
<td><?= $this->escape($graph->dashboard) ?></td>
<td><?= $this->escape($graph->dashboarduid) ?></td>
<td><?= $this->escape($graph->panelId) ?></td>
Expand Down
34 changes: 26 additions & 8 deletions library/Grafana/ProvidedHook/Grapher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 45c29fb

Please sign in to comment.