Skip to content

Commit

Permalink
Remove enableLink configuration and permission
Browse files Browse the repository at this point in the history
  • Loading branch information
martialblog committed Dec 4, 2024
1 parent 2a753dd commit cfd5d6d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 72 deletions.
16 changes: 1 addition & 15 deletions application/forms/Config/GeneralConfigForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,22 +271,8 @@ public function createElements(array $formData)
'description' => $this->translate('The default graph width in pixels.')
]
);
$this->addElement(
'select',
'grafana_enableLink',
[
'label' => $this->translate('Enable link'),
'value' => 'no',
'multiOptions' => [
'yes' => $this->translate('Yes'),
'no' => $this->translate('No'),
],
'description' => $this->translate('Image is an link to the dashboard on the Grafana server.'),
'class' => 'autosubmit'
]
);
}
if (isset($formData['grafana_enableLink']) && ( $formData['grafana_enableLink'] === 'yes') && ( $formData['grafana_accessmode'] != 'iframe' )) {
if (( $formData['grafana_accessmode'] != 'iframe' )) {
$this->addElement(
'select',
'grafana_usepublic',
Expand Down
1 change: 0 additions & 1 deletion configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
$this->providePermission('grafana/graph', $this->translate('Allow to view graphs in dashboards.'));
$this->providePermission('grafana/debug', $this->translate('Allow to see module debug information.'));
$this->providePermission('grafana/showall', $this->translate('Allow access to see all graphs of a host.'));
$this->providePermission('grafana/enablelink', $this->translate('Allow to follow links to Grafana.'));

$this->provideConfigTab('config', [
'title' => 'Configuration',
Expand Down
5 changes: 0 additions & 5 deletions doc/03-module-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ height = "280"
width = "640"
timerange = "3h"
timerangeAll = "1M/M"
enableLink = "yes"
defaultorgid = "1"
defaultdashboard = "icinga2-default"
defaultdashboardpanelid = "1"
Expand Down Expand Up @@ -66,7 +65,6 @@ ssl_verifyhost = "0"
|width | **Optional.** Global graph width in pixel. Defaults to `640`.|
|timerange | **Optional.** Global time range for graphs. Defaults to `6h`.|
|timerangeAll | **Optional.** Time range for all graphs feature. Defaults to `Previous week`.|
|enableLink | **Optional.** Enable/disable graph with a rendered URL to the Grafana dashboard. Defaults to `yes`.|
|datasource | **Required for Grafana 4.x only.** Type of the Grafana datasource (`influxdb`, `graphite` or `pnp`). Defaults to `influxdb`.|
|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`.|
|defaultdashboarduid | **Required for Grafana 5** The UID of the default dashboard for **Grafana 5**.
Expand Down Expand Up @@ -118,9 +116,6 @@ This option can be overwritten by a graph configuration.
### timerangeAll
Time range for all graphs feature. Defaults to `Previous week`

### enableLink
Enable or disable the graphs as a link to the Grafana Server.

### datasource
The datasource that Grafana server uses. Can be InfluxDB, Graphite.

Expand Down
100 changes: 49 additions & 51 deletions library/Grafana/ProvidedHook/Icingadb/IcingaDbGrapher.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
use ipl\Web\Widget\Link;
use ipl\Web\Widget\Icon;

/**
* IcingaDbGrapher contains methods for retrieving and rendering the data from Grafana
*/
trait IcingaDbGrapher
{
use Database;
Expand All @@ -51,7 +54,6 @@ trait IcingaDbGrapher
protected $apiToken = null;
protected $width = 640;
protected $height = 280;
protected $enableLink = true;
protected $defaultDashboard = "icinga2-default";
protected $defaultDashboardPanelId = "1";
protected $defaultOrgId = "1";
Expand Down Expand Up @@ -95,21 +97,19 @@ protected function init()
}

$this->protocol = $this->config->get('protocol', $this->protocol);
$this->enableLink = $this->config->get('enableLink', $this->enableLink);
if ($this->enableLink === "yes" && $this->permission->hasPermission('grafana/enablelink')) {
$this->usePublic = $this->config->get('usepublic', $this->usePublic);
if ($this->usePublic === "yes") {
$this->publicHost = $this->config->get('publichost', $this->publicHost);
if ($this->publicHost === null) {
throw new ConfigurationError(
'No Grafana public host configured!'
);
}
$this->publicProtocol = $this->config->get('publicprotocol', $this->publicProtocol);
} else {
$this->publicHost = $this->grafanaHost;
$this->publicProtocol = $this->protocol;

$this->usePublic = $this->config->get('usepublic', $this->usePublic);
if ($this->usePublic === "yes") {
$this->publicHost = $this->config->get('publichost', $this->publicHost);
if ($this->publicHost === null) {
throw new ConfigurationError(
'No Grafana public host configured!'
);
}
$this->publicProtocol = $this->config->get('publicprotocol', $this->publicProtocol);
} else {
$this->publicHost = $this->grafanaHost;
$this->publicProtocol = $this->protocol;
}

// Confid needed for Grafana
Expand Down Expand Up @@ -342,16 +342,8 @@ private function getMyPreviewHtml($serviceName, $hostName, HtmlDocument $preview
);

$previewHtml->add($iframeHtml);
// Add a link to Grafana in the title
$this->title->add(new Link(
new Icon(
'arrow-up-right-from-square',
['title' => 'View in Grafana']
),
str_replace('/d-solo/', '/d/', $iFramesrc),
['target' => '_blank', 'class' => 'external-link']
));
}

return true;
}

Expand Down Expand Up @@ -471,41 +463,47 @@ public function getPreviewHtml(Model $object, $report = false)
$html = new HtmlDocument();
$this->panelId = $panelid;

//image value will be returned as reference
// The image value will be returned as reference
$previewHtml = new HtmlDocument();
$res = $this->getMyPreviewHtml($serviceName, $hostName, $previewHtml);
//do not render URLs on error or if disabled
if (! $res
|| $this->enableLink === "no"
|| ! $this->permission->hasPermission('grafana/enablelink')) {
$html->addHtml($previewHtml);
} else {
$url = sprintf(
$this->GRAFANA_URL,
$this->publicProtocol,
$this->publicHost,
$this->dashboarduid,
$this->dashboard,
rawurlencode(($this->dataSource === "graphite" ? Util::graphiteReplace($hostName) : $hostName)),
rawurlencode(
($this->dataSource === "graphite" ? Util::graphiteReplace($serviceName) : $serviceName)
),
rawurlencode($object->checkcommand_name),
$this->customVars,
urlencode($this->timerange),
urlencode($this->timerangeto),
$this->orgId,
$this->panelId
);

$link = new Link($previewHtml, $url, ["target" => "_blank"]);

$html->add($link);
if ($res) {
$html->addHtml($previewHtml);
}

$return_html->add($html);
}

// URL for link to external Grafana
$url = sprintf(
$this->GRAFANA_URL,
$this->publicProtocol,
$this->publicHost,
$this->dashboarduid,
$this->dashboard,
rawurlencode(($this->dataSource === "graphite" ? Util::graphiteReplace($hostName) : $hostName)),
rawurlencode(
($this->dataSource === "graphite" ? Util::graphiteReplace($serviceName) : $serviceName)
),
rawurlencode($object->checkcommand_name),
$this->customVars,
urlencode($this->timerange),
urlencode($this->timerangeto),
$this->orgId,
$this->panelId
);

// Add a link to Grafana in the title
$this->title->add(new Link(
new Icon(
'arrow-up-right-from-square',
['title' => 'View in Grafana']
),
str_replace('/d-solo/', '/d/', $url),
['target' => '_blank', 'class' => 'external-link']
));

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

0 comments on commit cfd5d6d

Please sign in to comment.