Skip to content

Commit

Permalink
Access mode can be proxy or direct now
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikesch-mp committed Apr 21, 2017
1 parent b2fc894 commit 70a61a3
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 36 deletions.
15 changes: 8 additions & 7 deletions README.md
Expand Up @@ -22,8 +22,7 @@ Add Grafana graphs into Icinga Web 2 to display performance metrics.
* [Icinga Web 2](https://www.icinga.com/products/icinga-web-2/) (>= 2.4.1)
* [Grafana](https://grafana.com/) (>= 4.1)
* [InfluxDB](https://docs.influxdata.com/influxdb/), [Graphite](https://graphiteapp.org) or [PNP](https://docs.pnp4nagios.org/) (untested) as backend for Grafana

* PHP with curl enabled
* [PHP](https://www.php.net) with curl enabled (for proxy mode)

## Installation

Expand All @@ -36,13 +35,13 @@ cd /usr/share/icingaweb2/modules
git clone https://github.com/Mikesch-mp/icingaweb2-module-grafana.git grafana
```

Tarball download (latest [release](https://github.com/Mikesch-mp/icingaweb2-module-grafana/releases)):
Tarball download (latest [release](https://github.com/Mikesch-mp/icingaweb2-module-grafana/releases/latest)):

```
cd /usr/share/icingaweb2/modules
wget https://github.com/Mikesch-mp/icingaweb2-module-grafana/archive/v1.0.10.zip
unzip v1.0.10.zip
mv icingaweb2-module-grafana-1.0.10 grafana
wget https://github.com/Mikesch-mp/icingaweb2-module-grafana/archive/v1.1.0.zip
unzip v1.1.0.zip
mv icingaweb2-module-grafana-1.1.0 grafana
```

Enable the module in the Icinga Web 2 frontend in `Configuration -> Modules -> grafana -> enable`.
Expand Down Expand Up @@ -86,6 +85,7 @@ enableLink | **Optional.** Enable/disable graph with a rendered URL to t
datasource | **Required.** 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. **Important: `panelID` must be set to `1`!** Defaults to `icinga2-default`.
defaultdashboardstore | **Optional.** Grafana backend (file or database). Defaults to `Database`.
accessmode | **Optional.** Controlls if module proxies all graphs or user loads graphs directly. Direct access speeds up page load, needs `auth.anonymous` enabled in Grafana. Defaults to `proxy`.


Example:
Expand All @@ -104,6 +104,7 @@ enableLink = "yes"
defaultdashboard = "icinga2-default"
datasource = "influxdb"
defaultdashboardstore = "db"
accessmode = "proxy"
```

### Graph Configuration
Expand Down Expand Up @@ -137,7 +138,7 @@ width = "150"

## FAQ

### Search Order
### Search order

This module prefers the `service name`, then looks for an optional `parametrized service nam` and for the `service check command name`.

Expand Down
13 changes: 12 additions & 1 deletion application/forms/Config/GeneralConfigForm.php
Expand Up @@ -151,7 +151,18 @@ public function createElements(array $formData)
'description' => $this->translate('Grafana Datasource Type.')
)
);

$this->addElement(
'select',
'grafana_accessmode',
array(
'label' => $this->translate('Grafana access'),
'multiOptions' => array(
'direct' => $this->translate('Direct'),
'proxy' => $this->translate('Proxy'),
),
'description' => $this->translate('User access Grafana directly or module proxies graphs.')
)
);
}
}

78 changes: 51 additions & 27 deletions library/Grafana/ProvidedHook/Grapher.php
Expand Up @@ -29,6 +29,7 @@ class Grapher extends GrapherHook
protected $defaultDashboard = "icinga2-default";
protected $defaultDashboardStore = "db";
protected $datasource = null;
protected $accessmode = "proxy";
protected $timeranges = [
'5m' => '5 minutes',
'15m' => '15 minutes',
Expand Down Expand Up @@ -77,7 +78,7 @@ protected function init()
$this->defaultDashboard = $this->config->get('defaultdashboard', $this->defaultDashboard);
$this->defaultDashboardStore = $this->config->get('defaultdashboardstore', $this->defaultDashboardStore);
$this->datasource = $this->config->get('datasource', $this->datasource);
$this->view = Icinga::app()->getViewRenderer()->view;
$this->accessmode = $this->config->get('accessmode', $this->accessmode);
if($this->username != null)
{
if($this->password != null)
Expand Down Expand Up @@ -127,6 +128,7 @@ private function getGraphConf($serviceName, $serviceCommand)

private function getTimerangeLink($object, $rangeName, $timeRange)
{
$this->view = Icinga::app()->getViewRenderer()->view;
if ($object instanceof Host)
{
$array = [
Expand Down Expand Up @@ -157,10 +159,10 @@ private function getTimerangeLink($object, $rangeName, $timeRange)

private function getPreviewImage($serviceName, $hostName)
{
$pngUrl = sprintf(
'%s://%s%s/render/dashboard-solo/%s/%s?var-hostname=%s&var-service=%s%s&panelId=%s&width=%s&height=%s&theme=light&from=now-%s&to=now',
if ($this->accessmode == "proxy") {
$pngUrl = sprintf(
'%s://%s/render/dashboard-solo/%s/%s?var-hostname=%s&var-service=%s%s&panelId=%s&width=%s&height=%s&theme=light&from=now-%s&to=now',
$this->protocol,
$this->auth,
$this->grafanaHost,
$this->dashboardstore,
$this->dashboard,
Expand All @@ -171,32 +173,54 @@ private function getPreviewImage($serviceName, $hostName)
$this->width,
$this->height,
$this->timerange
);
);

// fetch image with curl
$curl_handle = curl_init();
curl_setopt($curl_handle,CURLOPT_URL,$pngUrl);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl_handle,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($curl_handle,CURLOPT_TIMEOUT,5);
$imgBinary = curl_exec($curl_handle);
if(curl_error($curl_handle))
{
return 'Graph currently unavailable: :' . curl_error($curl_handle);
}
curl_close($curl_handle);
// fetch image with curl
$curl_handle = curl_init();
curl_setopt($curl_handle,CURLOPT_URL,$pngUrl);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl_handle,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($curl_handle,CURLOPT_TIMEOUT,5);
curl_setopt($curl_handle, CURLOPT_USERPWD, "$this->auth");
curl_setopt($curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
$imgBinary = curl_exec($curl_handle);
if(curl_error($curl_handle))
{
return 'Graph currently unavailable: :' . curl_error($curl_handle);
}
curl_close($curl_handle);


$img = 'data:image/png;base64,'.base64_encode($imgBinary);
$imghtml = '<img src="%s" alt="%s" width="%d" height="%d" />';
return sprintf(
$imghtml,
$img,
rawurlencode($serviceName),
$this->width,
$this->height
);
$img = 'data:image/png;base64,'.base64_encode($imgBinary);
$imghtml = '<img src="%s" alt="%s" width="%d" height="%d" />';
return sprintf(
$imghtml,
$img,
rawurlencode($serviceName),
$this->width,
$this->height
);
} else {
$imghtml = '<img src="%s://%s/render/dashboard-solo/%s/%s?var-hostname=%s&var-service=%s%s&panelId=%s&width=%s&height=%s&theme=light&from=now-%s&to=now" alt="%s" width="%d" height="%d" />';
return sprintf(
$imghtml,
$this->protocol,
$this->grafanaHost,
$this->dashboardstore,
$this->dashboard,
urlencode($hostName),
rawurlencode($serviceName),
$this->customVars,
$this->panelId,
$this->width,
$this->height,
$this->timerange,
rawurlencode($serviceName),
$this->width,
$this->height
);
}
}

public function has(MonitoredObject $object)
Expand Down
6 changes: 5 additions & 1 deletion module.info
@@ -1,5 +1,5 @@
Name: Grafana
Version: 1.0.11
Version: 1.1.0
Depends: monitoring
Description: Grafana - A perfdata visualisation module
Shows Grafana graphs for captured metrics.
Expand All @@ -10,6 +10,8 @@ Description: Grafana - A perfdata visualisation module

* InfluxDB or Graphite as datasource backend.

* PHP with curl enabled (for proxy mode)


Thanks for bug repoting/fixing or enhancements goes to:

Expand All @@ -20,3 +22,5 @@ Description: Grafana - A perfdata visualisation module
Peter N.

Alex

Michael F.

0 comments on commit 70a61a3

Please sign in to comment.