Skip to content

Commit

Permalink
Fix for multiple panelids without link.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikesch-mp committed Mar 30, 2017
1 parent 8a74b19 commit a87b879
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 34 deletions.
9 changes: 9 additions & 0 deletions icingaexchange.yml
Expand Up @@ -34,3 +34,12 @@ releases:
url: "https://github.com/Mikesch-mp/icingaweb2-module-grafana/archive/v1.0.4.tar.gz"
description: Module file for Icinga Web 2
checksum: 0bf0c1f587b2f3c54be164348be19e4f
-
name: v1.0.i4
description: "Update 1.0.4"
files:
-
name: icingaweb2-module-grafana-1.0.4.tar.gz
url: "https://github.com/Mikesch-mp/icingaweb2-module-grafana/archive/v1.0.4.tar.gz"
description: Module file for Icinga Web 2
checksum: 2896c7e4430d58bdcfe943a3fdd370e0
87 changes: 54 additions & 33 deletions library/Grafana/ProvidedHook/Grapher.php
Expand Up @@ -46,13 +46,19 @@ protected function init()
$this->defaultDashboard = $this->config->get('defaultdashboard', $this->defaultDashboard);
$this->datasource = $this->config->get('datasource', $this->datasource);

if($this->username != null){
if($this->password != null){
if($this->username != null)
{
if($this->password != null)
{
$this->auth = $this->username.":".$this->password."@";
} else {
$this->auth = $this->username."@";
}
} else {
else
{
$this->auth = $this->username."@";
}
}
else
{
$this->auth = "";
}
}
Expand All @@ -62,10 +68,12 @@ private function getGraphConf($serviceName, $serviceCommand)

$graphconfig = Config::module('grafana', 'graphs');
$this->graphconfig = $graphconfig;
if ($this->graphconfig->hasSection(strtok($serviceName, ' ')) && ($this->graphconfig->hasSection($serviceName) == False )) {
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 )) {
if ($this->graphconfig->hasSection(strtok($serviceName, ' ')) == False && ($this->graphconfig->hasSection($serviceName) == False ))
{
$serviceName = $serviceCommand;
}

Expand Down Expand Up @@ -96,7 +104,8 @@ private function getPreviewImage($serviceName, $hostName)
$ctx = stream_context_create(array('ssl' => array("verify_peer"=>false, "verify_peer_name"=>false), 'http' => array('method' => 'GET', 'timeout' => 5)));
$imgBinary = @file_get_contents($pngUrl, false, $ctx);
$error = error_get_last();
if ($error !== null) {
if ($error !== null)
{
return "Graph currently unavailable: ".$error["message"];
}

Expand All @@ -113,64 +122,76 @@ private function getPreviewImage($serviceName, $hostName)

public function has(MonitoredObject $object)
{
if (($object instanceof Host)||($object instanceof Service)) {
if (($object instanceof Host)||($object instanceof Service))
{
return true;
} else {
}
else
{
return false;
}
}

public function getPreviewHtml(MonitoredObject $object)
{
// enable_perfdata = true ?
if (! $object->process_perfdata) {
if (! $object->process_perfdata)
{
return '';
}

if ($object instanceof Host) {
if ($object instanceof Host)
{
$serviceName = $object->check_command;
$hostName = $object->host_name;
} elseif ($object instanceof Service) {
}
elseif ($object instanceof Service)
{
$serviceName = $object->service_description;
$hostName = $object->host->getName();
}

$this->getGraphConf($serviceName, $object->check_command);


if ($this->datasource == "graphite") {
if ($this->datasource == "graphite")
{
$serviceName = preg_replace('/[^a-zA-Z0-9\*\-:]/', '_', $serviceName);
$hostName = preg_replace('/[^a-zA-Z0-9\*\-:]/', '_', $hostName);
}

$return_html = "";

foreach(explode(',' , $this->panelId) as $panelid) {

$this->panelId = $panelid;

if ($this->enableLink == "no")
{
return $this->getPreviewImage($serviceName, $hostName);
$html = $this->getPreviewImage($serviceName, $hostName);
}
else
{
$html = '<a href="%s://%s/dashboard/db/%s?var-hostname=%s&var-service=%s&from=now-%s&to=now';

$html = '<a href="%s://%s/dashboard/db/%s?var-hostname=%s&var-service=%s&from=now-%s&to=now';

if ( $this->dashboard != $this->defaultDashboard )
{
$html .= '&panelId=' . $this->panelId .'&fullscreen';
}
if ( $this->dashboard != $this->defaultDashboard )
{
$html .= '&panelId=' . $this->panelId .'&fullscreen';
}

$html .= '"target="_blank">%s</a>';

$html = sprintf(
$html,
$this->protocol,
$this->grafanaHost,
$this->dashboard,
urlencode($hostName),
rawurlencode($serviceName),
$this->timerange,
$this->getPreviewImage($serviceName, $hostName)
);
$html .= '"target="_blank">%s</a>';

$html = sprintf(
$html,
$this->protocol,
$this->grafanaHost,
$this->dashboard,
urlencode($hostName),
rawurlencode($serviceName),
$this->timerange,
$this->getPreviewImage($serviceName, $hostName)
);
}
$return_html .= $html;
}
return $return_html;
Expand Down
9 changes: 8 additions & 1 deletion module.info
@@ -1,5 +1,5 @@
Name: Grafana
Version: 1.0.5
Version: 1.0.6
Depends: monitoring
Description: Grafana - A perfdata visualisation module
Shows Grafana graphs for captured metrics.
Expand All @@ -9,3 +9,10 @@ Description: Grafana - A perfdata visualisation module
* Grafana (>= 4.1.0) with basic auth enabled or anonymous access

* InfluxDB or Graphite as datasource backend.


Thanks for bug repoting/fixing goes to:

Thilo W.

Robert G.

0 comments on commit a87b879

Please sign in to comment.