Skip to content

Commit f1be9e5

Browse files
committed
Add external link to Grafana header via icon
- Adds a link via an icon in headline for both iframe and proxy mode - Remove enableLink configuration and permission
1 parent 4296549 commit f1be9e5

File tree

5 files changed

+65
-69
lines changed

5 files changed

+65
-69
lines changed

application/forms/Config/GeneralConfigForm.php

+1-15
Original file line numberDiff line numberDiff line change
@@ -271,22 +271,8 @@ public function createElements(array $formData)
271271
'description' => $this->translate('The default graph width in pixels.')
272272
]
273273
);
274-
$this->addElement(
275-
'select',
276-
'grafana_enableLink',
277-
[
278-
'label' => $this->translate('Enable link'),
279-
'value' => 'no',
280-
'multiOptions' => [
281-
'yes' => $this->translate('Yes'),
282-
'no' => $this->translate('No'),
283-
],
284-
'description' => $this->translate('Image is an link to the dashboard on the Grafana server.'),
285-
'class' => 'autosubmit'
286-
]
287-
);
288274
}
289-
if (isset($formData['grafana_enableLink']) && ( $formData['grafana_enableLink'] === 'yes') && ( $formData['grafana_accessmode'] != 'iframe' )) {
275+
if (( $formData['grafana_accessmode'] != 'iframe' )) {
290276
$this->addElement(
291277
'select',
292278
'grafana_usepublic',

configuration.php

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
$this->providePermission('grafana/graph', $this->translate('Allow to view graphs in dashboards.'));
1010
$this->providePermission('grafana/debug', $this->translate('Allow to see module debug information.'));
1111
$this->providePermission('grafana/showall', $this->translate('Allow access to see all graphs of a host.'));
12-
$this->providePermission('grafana/enablelink', $this->translate('Allow to follow links to Grafana.'));
1312

1413
$this->provideConfigTab('config', [
1514
'title' => 'Configuration',

doc/03-module-configuration.md

-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ height = "280"
3535
width = "640"
3636
timerange = "3h"
3737
timerangeAll = "1M/M"
38-
enableLink = "yes"
3938
defaultorgid = "1"
4039
defaultdashboard = "icinga2-default"
4140
defaultdashboardpanelid = "1"
@@ -66,7 +65,6 @@ ssl_verifyhost = "0"
6665
|width | **Optional.** Global graph width in pixel. Defaults to `640`.|
6766
|timerange | **Optional.** Global time range for graphs. Defaults to `6h`.|
6867
|timerangeAll | **Optional.** Time range for all graphs feature. Defaults to `Previous week`.|
69-
|enableLink | **Optional.** Enable/disable graph with a rendered URL to the Grafana dashboard. Defaults to `yes`.|
7068
|datasource | **Required for Grafana 4.x only.** Type of the Grafana datasource (`influxdb`, `graphite` or `pnp`). Defaults to `influxdb`.|
7169
|defaultdashboard | **Required.** Name of the default dashboard which will be shown for unconfigured graphs. Set to `none` to hide the module output. Defaults to `icinga2-default`.|
7270
|defaultdashboarduid | **Required for Grafana 5** The UID of the default dashboard for **Grafana 5**.
@@ -118,9 +116,6 @@ This option can be overwritten by a graph configuration.
118116
### timerangeAll
119117
Time range for all graphs feature. Defaults to `Previous week`
120118

121-
### enableLink
122-
Enable or disable the graphs as a link to the Grafana Server.
123-
124119
### datasource
125120
The datasource that Grafana server uses. Can be InfluxDB, Graphite.
126121

library/Grafana/ProvidedHook/Icingadb/IcingaDbGrapher.php

+58-48
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,20 @@
2323
use ipl\Stdlib\Filter;
2424
use ipl\Web\Url;
2525
use ipl\Web\Widget\Link;
26+
use ipl\Web\Widget\Icon;
2627

28+
/**
29+
* IcingaDbGrapher contains methods for retrieving and rendering the data from Grafana
30+
*/
2731
trait IcingaDbGrapher
2832
{
2933
use Database;
3034
use IcingaDbAuth;
3135

36+
// Could be constants in the future, but for now we want to keep compatibility
37+
protected $GRAFANA_URL = "%s://%s/d/%s/%s?var-hostname=%s&var-service=%s&var-command=%s%s&from=%s&to=%s&orgId=%s&viewPanel=%s";
38+
protected $GRAFANA_URL_SOLO = "%s://%s/d-solo/%s/%s?var-hostname=%s&var-service=%s&var-command=%s%s&panelId=%s&orgId=%s&theme=%s&from=%s&to=%s";
39+
3240
protected $config;
3341
protected $graphConfig;
3442
protected $auth;
@@ -46,7 +54,6 @@ trait IcingaDbGrapher
4654
protected $apiToken = null;
4755
protected $width = 640;
4856
protected $height = 280;
49-
protected $enableLink = true;
5057
protected $defaultDashboard = "icinga2-default";
5158
protected $defaultDashboardPanelId = "1";
5259
protected $defaultOrgId = "1";
@@ -81,6 +88,7 @@ protected function init()
8188
$this->title = Html::tag("h2", "Performance Graph");
8289
$this->permission = Auth::getInstance();
8390
$this->config = Config::module('grafana')->getSection('grafana');
91+
8492
$this->grafanaVersion = $this->config->get('version', $this->grafanaVersion);
8593
$this->grafanaHost = $this->config->get('host', $this->grafanaHost);
8694
if ($this->grafanaHost === null) {
@@ -89,21 +97,20 @@ protected function init()
8997
);
9098
}
9199

100+
// Set the protocol and host
92101
$this->protocol = $this->config->get('protocol', $this->protocol);
93-
$this->enableLink = $this->config->get('enableLink', $this->enableLink);
94-
if ($this->enableLink === "yes" && $this->permission->hasPermission('grafana/enablelink')) {
95-
$this->usePublic = $this->config->get('usepublic', $this->usePublic);
96-
if ($this->usePublic === "yes") {
97-
$this->publicHost = $this->config->get('publichost', $this->publicHost);
98-
if ($this->publicHost === null) {
99-
throw new ConfigurationError(
100-
'No Grafana public host configured!'
101-
);
102-
}
103-
$this->publicProtocol = $this->config->get('publicprotocol', $this->publicProtocol);
104-
} else {
105-
$this->publicHost = $this->grafanaHost;
106-
$this->publicProtocol = $this->protocol;
102+
$this->publicHost = $this->grafanaHost;
103+
$this->publicProtocol = $this->protocol;
104+
105+
// Override protocol and host if usePublic is set
106+
$this->usePublic = $this->config->get('usepublic', $this->usePublic);
107+
if ($this->usePublic === 'yes') {
108+
$this->publicHost = $this->config->get('publichost', $this->publicHost);
109+
$this->publicProtocol = $this->config->get('publicprotocol', $this->publicProtocol);
110+
if ($this->publicHost === null) {
111+
throw new ConfigurationError(
112+
'No Grafana public host configured!'
113+
);
107114
}
108115
}
109116

@@ -120,6 +127,7 @@ protected function init()
120127
'defaultdashboardpanelid',
121128
$this->defaultDashboardPanelId
122129
);
130+
123131
$this->defaultOrgId = $this->config->get('defaultorgid', $this->defaultOrgId);
124132
$this->grafanaTheme = $this->getUserThemeMode();
125133
$this->height = $this->config->get('height', $this->height);
@@ -164,7 +172,6 @@ protected function init()
164172
/**
165173
* Username & Password or token
166174
*/
167-
168175
$this->apiToken = $this->config->get('apitoken', $this->apiToken);
169176
$this->authentication = $this->config->get('authentication');
170177
if ($this->apiToken === null && $this->authentication === "token") {
@@ -310,8 +317,7 @@ private function getMyPreviewHtml($serviceName, $hostName, HtmlDocument $preview
310317
$previewHtml->add($imgHtml);
311318
} elseif ($this->accessMode === "iframe") {
312319
$iFramesrc = sprintf(
313-
"%s://%s/d-solo/%s/%s?" .
314-
"var-hostname=%s&var-service=%s&var-command=%s%s&panelId=%s&orgId=%s&theme=%s&from=%s&to=%s",
320+
$this->GRAFANA_URL_SOLO,
315321
$this->protocol,
316322
$this->grafanaHost,
317323
$this->dashboarduid,
@@ -339,6 +345,7 @@ private function getMyPreviewHtml($serviceName, $hostName, HtmlDocument $preview
339345

340346
$previewHtml->add($iframeHtml);
341347
}
348+
342349
return true;
343350
}
344351

@@ -444,6 +451,35 @@ public function getPreviewHtml(Model $object, $report = false)
444451

445452
$return_html = new HtmlDocument();
446453

454+
// URL for link to external Grafana
455+
$url = sprintf(
456+
$this->GRAFANA_URL,
457+
$this->publicProtocol,
458+
$this->publicHost,
459+
$this->dashboarduid,
460+
$this->dashboard,
461+
rawurlencode(($this->dataSource === 'graphite' ? Util::graphiteReplace($hostName) : $hostName)),
462+
rawurlencode(
463+
($this->dataSource === 'graphite' ? Util::graphiteReplace($serviceName) : $serviceName)
464+
),
465+
rawurlencode($object->checkcommand_name),
466+
$this->customVars,
467+
urlencode($this->timerange),
468+
urlencode($this->timerangeto),
469+
$this->orgId,
470+
$this->panelId
471+
);
472+
473+
// Add a link to Grafana in the title
474+
$this->title->add(new Link(
475+
new Icon(
476+
'arrow-up-right-from-square',
477+
['title' => 'View in Grafana']
478+
),
479+
str_replace('/d-solo/', '/d/', $url),
480+
['target' => '_blank', 'class' => 'external-link']
481+
));
482+
447483
// Hide menu if in reporting or compact mode
448484
$menu = "";
449485

@@ -458,44 +494,18 @@ public function getPreviewHtml(Model $object, $report = false)
458494
$html = new HtmlDocument();
459495
$this->panelId = $panelid;
460496

461-
//image value will be returned as reference
497+
// The image value will be returned as reference
462498
$previewHtml = new HtmlDocument();
463499
$res = $this->getMyPreviewHtml($serviceName, $hostName, $previewHtml);
464-
//do not render URLs on error or if disabled
465-
if (! $res
466-
|| $this->enableLink === "no"
467-
|| ! $this->permission->hasPermission('grafana/enablelink')) {
468-
$html->addHtml($previewHtml);
469-
} else {
470-
$urlFormat = "%s://%s/d/%s/%s" .
471-
"?var-hostname=%s&var-service=%s&var-command=%s%s&from=%s&to=%s&orgId=%s&viewPanel=%s";
472-
473-
$url = sprintf(
474-
$urlFormat,
475-
$this->publicProtocol,
476-
$this->publicHost,
477-
$this->dashboarduid,
478-
$this->dashboard,
479-
rawurlencode(($this->dataSource === "graphite" ? Util::graphiteReplace($hostName) : $hostName)),
480-
rawurlencode(
481-
($this->dataSource === "graphite" ? Util::graphiteReplace($serviceName) : $serviceName)
482-
),
483-
rawurlencode($object->checkcommand_name),
484-
$this->customVars,
485-
urlencode($this->timerange),
486-
urlencode($this->timerangeto),
487-
$this->orgId,
488-
$this->panelId
489-
);
490500

491-
$link = new Link($previewHtml, $url, ["target" => "_blank"]);
492-
493-
$html->add($link);
501+
if ($res) {
502+
$html->addHtml($previewHtml);
494503
}
495504

496505
$return_html->add($html);
497506
}
498507

508+
// Add a data table with runtime information and configuration for debugging purposes
499509
if ($this->debug && $this->permission->hasPermission('grafana/debug') && $report === false) {
500510
if ($this->accessMode === "indirectproxy") {
501511
$usedUrl = $this->pngUrl;

public/css/module.less

+6
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@
5252
padding: 0;
5353
}
5454

55+
/* Icon to external pages like Grafana */
56+
.external-link {
57+
margin: 0 0.33em;
58+
font-size: 0.77em;
59+
}
60+
5561
/* NAVIGATION */
5662
.grafana-menu-navigation {
5763
list-style: none;

0 commit comments

Comments
 (0)