Skip to content

Commit

Permalink
support splitting into multiple graphs via imagefilters
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianbees committed Dec 19, 2024
1 parent 677fada commit 2777765
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
12 changes: 10 additions & 2 deletions application/controllers/IcingadbimgController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class IcingadbimgController extends IcingadbGrafanaController
protected $dashboarduid;
protected $panelId;
protected $orgId;
protected $imagefilter;

/**
* Mainly loading defaults and the configuration.
Expand Down Expand Up @@ -115,6 +116,12 @@ public function init()
$this->dataSource = $this->myConfig->get('datasource', $this->dataSource);
$this->shadows = $this->myConfig->get('shadows', $this->shadows);
$this->custvarconfig = ($this->myConfig->get('custvarconfig', $this->custvarconfig));

$this->imagefilter = $this->hasParam('imagefilter') ? urldecode($this->getParam('imagefilter')) : "";

/**
* Verify the certificate's name against host
*/
$this->SSLVerifyHost = ($this->myConfig->get('ssl_verifyhost', $this->SSLVerifyHost));
$this->SSLVerifyPeer = ($this->myConfig->get('ssl_verifypeer', $this->SSLVerifyPeer));

Expand Down Expand Up @@ -278,7 +285,7 @@ private function getMyimageHtml($serviceName, $hostName, &$imageHtml)

$pngUrl = sprintf(
'%s://%s/render/d-solo/%s/%s?var-hostname=%s&var-service=%s&var-command=%s%s&panelId=%s&orgId=%s'
. '&width=%s&height=%s&theme=%s&from=%s&to=%s',
. '&width=%s&height=%s&theme=%s&from=%s&to=%s%s',
$this->protocol,
$this->grafanaHost,
$this->dashboarduid,
Expand All @@ -293,7 +300,8 @@ private function getMyimageHtml($serviceName, $hostName, &$imageHtml)
$this->height,
$this->grafanaTheme,
urlencode($this->timerange),
urlencode($this->timerangeto)
urlencode($this->timerangeto),
$this->imagefilter
);

// fetch image with curl
Expand Down
31 changes: 23 additions & 8 deletions library/Grafana/ProvidedHook/Icingadb/IcingaDbGrapher.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ private function getGraphConf($serviceName, $serviceCommand = null): ?self
* @return bool
* @throws \Icinga\Exception\ProgrammingError
*/
private function getMyPreviewHtml($serviceName, $hostName, HtmlDocument $previewHtml): bool
private function getMyPreviewHtml($serviceName, $hostName, HtmlDocument $previewHtml, $imageFilter = ""): bool
{
$imgClass = $this->shadows ? "grafana-img grafana-img-shadows" : "grafana-img";

Expand All @@ -249,7 +249,8 @@ private function getMyPreviewHtml($serviceName, $hostName, HtmlDocument $preview
'panelid' => $this->panelId,
'timerange' => urlencode($this->timerange),
'timerangeto' => urlencode($this->timerangeto),
'cachetime' => $this->cacheTime
'cachetime' => $this->cacheTime,
'imagefilter' => urlencode($imageFilter)
]
);
} else {
Expand All @@ -260,7 +261,8 @@ private function getMyPreviewHtml($serviceName, $hostName, HtmlDocument $preview
'panelid' => $this->panelId,
'timerange' => urlencode($this->timerange),
'timerangeto' => urlencode($this->timerangeto),
'cachetime' => $this->cacheTime
'cachetime' => $this->cacheTime,
'imagefilter' => urlencode($imageFilter)
]
);
}
Expand Down Expand Up @@ -458,12 +460,25 @@ public function getPreviewHtml(Model $object, $report = false)
$html = new HtmlDocument();
$this->panelId = $panelid;

// The image value will be returned as reference
$previewHtml = new HtmlDocument();
$res = $this->getMyPreviewHtml($serviceName, $hostName, $previewHtml);
$flattened_vars = $object->vars;
// The $object->vars array is flattened, we unflatten the subarray grafanaimagefiltersarray here:
$i = 0;
$info = [];
while (isset($flattened_vars['grafanaimagefiltersarray[' . $i . ']'])) {
$info[$i] = $flattened_vars['grafanaimagefiltersarray[' . $i . ']'];
$i++;
}


foreach ($info as $value) {

if ($res) {
$html->addHtml($previewHtml);
// The image value will be returned as reference
$previewHtml = new HtmlDocument();
$res = $this->getMyPreviewHtml($serviceName, $hostName, $previewHtml, $value);

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

$returnHtml->add($html);
Expand Down

0 comments on commit 2777765

Please sign in to comment.