From 70a61a3b37965aefcfe70d5df8be25ab43aa2deb Mon Sep 17 00:00:00 2001 From: Mikesch-mp Date: Fri, 21 Apr 2017 12:22:43 +0200 Subject: [PATCH] Access mode can be proxy or direct now --- README.md | 15 ++-- .../forms/Config/GeneralConfigForm.php | 13 +++- library/Grafana/ProvidedHook/Grapher.php | 78 ++++++++++++------- module.info | 6 +- 4 files changed, 76 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 21cd9ae..21995de 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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`. @@ -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: @@ -104,6 +104,7 @@ enableLink = "yes" defaultdashboard = "icinga2-default" datasource = "influxdb" defaultdashboardstore = "db" +accessmode = "proxy" ``` ### Graph Configuration @@ -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`. diff --git a/application/forms/Config/GeneralConfigForm.php b/application/forms/Config/GeneralConfigForm.php index 3eba748..331f5cd 100644 --- a/application/forms/Config/GeneralConfigForm.php +++ b/application/forms/Config/GeneralConfigForm.php @@ -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.') + ) + ); } } diff --git a/library/Grafana/ProvidedHook/Grapher.php b/library/Grafana/ProvidedHook/Grapher.php index f9c351d..ff72142 100644 --- a/library/Grafana/ProvidedHook/Grapher.php +++ b/library/Grafana/ProvidedHook/Grapher.php @@ -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', @@ -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) @@ -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 = [ @@ -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, @@ -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 = '%s'; - return sprintf( - $imghtml, - $img, - rawurlencode($serviceName), - $this->width, - $this->height - ); + $img = 'data:image/png;base64,'.base64_encode($imgBinary); + $imghtml = '%s'; + return sprintf( + $imghtml, + $img, + rawurlencode($serviceName), + $this->width, + $this->height + ); + } else { + $imghtml = '%s'; + 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) diff --git a/module.info b/module.info index d5b2ebb..3d52784 100644 --- a/module.info +++ b/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. @@ -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: @@ -20,3 +22,5 @@ Description: Grafana - A perfdata visualisation module Peter N. Alex + + Michael F.