Skip to content
This repository has been archived by the owner on Jan 15, 2025. It is now read-only.

Add compatibility to default icinga2 graphite writer. #237

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 47 additions & 27 deletions application/controllers/ImgController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ImgController extends MonitoringAwareController
{
protected $host;
protected $service;
protected $graphType = 'services';
protected $timerange;
protected $myConfig;
protected $myAuth;
Expand All @@ -34,6 +35,8 @@ class ImgController extends MonitoringAwareController
protected $height = 280;
protected $defaultDashboard = "icinga2-default";
protected $defaultDashboardPanelId = "1";
protected $defaultDashboardPanelIdHost = "2";
protected $defaultDashboardPanelIdServices = "1";
protected $defaultOrgId = "1";
protected $shadows = false;
protected $defaultDashboardStore = "db";
Expand All @@ -50,21 +53,24 @@ class ImgController extends MonitoringAwareController

public function init()
{

/* we need at least a host name */
if (is_null($this->getParam('host')))
{
throw new \Error('No host given!');
}

/* save timerange from params for later use */
$this->timerange = $this->hasParam('timerange') ? urldecode($this->getParam('timerange')) : null;
$this->timerange = $this->hasParam('timerange')
? urldecode($this->getParam('timerange'))
: null;
if($this->hasParam('timerangeto')) {
$this->timerangeto = urldecode($this->getParam('timerangeto'));
} else {
$this->timerangeto = strpos($this->timerange, '/') ? 'now-' . $this->timerange : "now";
}
$this->cacheTime = $this->hasParam('cachetime') ? $this->getParam('cachetime') : 300;
$this->cacheTime = $this->hasParam('cachetime')
? $this->getParam('cachetime')
: 300;

/* load global configuration */
$this->myConfig = Config::module('grafana')->getSection('grafana');
Expand All @@ -79,12 +85,15 @@ public function init()

$this->defaultDashboard = $this->myConfig->get('defaultdashboard', $this->defaultDashboard);
$this->defaultdashboarduid = $this->myConfig->get('defaultdashboarduid', NULL);
if ($this->grafanaVersion == "1" && is_null($this->defaultdashboarduid)) {
if ($this->grafanaVersion == "1"
&& is_null($this->defaultdashboarduid)) {
throw new ConfigurationError(
'Usage of Grafana 5 is configured but no UID for default dashboard found!'
);
}
$this->defaultDashboardPanelId = $this->myConfig->get('defaultdashboardpanelid', $this->defaultDashboardPanelId);
$this->defaultDashboardPanelIdHost = $this->myConfig->get('defaultdashboardpanelid_host', $this->defaultDashboardPanelIdHost);
$this->defaultDashboardPanelIdServices = $this->myConfig->get('defaultdashboardpanelid_services', $this->defaultDashboardPanelIdServices);
$this->defaultOrgId = $this->myConfig->get('defaultorgid', $this->defaultOrgId);
$this->grafanaTheme = $this->myConfig->get('theme', $this->grafanaTheme);
$this->defaultDashboardStore = $this->myConfig->get('defaultdashboardstore', $this->defaultDashboardStore);
Expand Down Expand Up @@ -122,7 +131,6 @@ public function init()
/**
* Username & Password or token
*/

$this->apiToken = $this->myConfig->get('apitoken', $this->apiToken);
$this->authentication = $this->myConfig->get('authentication');
if ($this->apiToken == null && $this->authentication == "token") {
Expand All @@ -142,7 +150,6 @@ public function init()
$this->myAuth = "";
}
}

}

public function indexAction()
Expand All @@ -160,7 +167,8 @@ public function indexAction()
$hostName = $this->object->getName();
}

if (array_key_exists($this->custvarconfig, $this->object->customvars) && ! empty($this->object->customvars[$this->custvarconfig])) {
if (array_key_exists($this->custvarconfig, $this->object->customvars)
&& ! empty($this->object->customvars[$this->custvarconfig])) {
$this->setGraphConf($this->object->customvars[$this->custvarconfig]);
} else {
$this->setGraphConf($serviceName, $this->object->check_command);
Expand All @@ -175,7 +183,7 @@ public function indexAction()
$this->customVars = str_replace($search, $replace, $this->customVars);
}

// urlencodee values
// urlencode values
$customVars = "";
foreach (preg_split('/\&/', $this->customVars, -1, PREG_SPLIT_NO_EMPTY) as $param) {
$arr = explode("=", $param);
Expand All @@ -196,7 +204,7 @@ public function indexAction()
}

$imageHtml = "";
$res = $this->getMyimageHtml($serviceName, $hostName, $imageHtml);
$res = $this->getMyImageHtml($serviceName, $hostName, $imageHtml);
header('Pragma: public');
if($this->refresh == "yes") {
header('Pragma: public');
Expand All @@ -207,8 +215,7 @@ public function indexAction()
header('Cache-Control: max-age='. (365*86440));
}
header("Content-type: image/png");
if (! $res)
{
if (! $res) {
// set expire to now and max age to 1 minute
header("Expires: ".gmdate("D, d M Y H:i:s", time())." GMT");
header('Cache-Control: max-age='. 120);
Expand Down Expand Up @@ -266,47 +273,58 @@ private function setGraphConf($serviceName, $serviceCommand = NULL)
{
$graphConfig = Config::module('grafana', 'graphs');

if ($graphConfig->hasSection(strtok($serviceName, ' ')) && ($graphConfig->hasSection($serviceName) == False)) {
if ($graphConfig->hasSection(strtok($serviceName, ' '))
&& ($graphConfig->hasSection($serviceName) == False)) {
$serviceName = strtok($serviceName, ' ');
}
if ($graphConfig->hasSection(strtok($serviceName, ' ')) == False && ($graphConfig->hasSection($serviceName) == False)) {
if ($graphConfig->hasSection(strtok($serviceName, ' ')) == False
&& ($graphConfig->hasSection($serviceName) == False)) {
$serviceName = $serviceCommand;
if($graphConfig->hasSection($serviceCommand) == False && $this->defaultDashboard == 'none') {
if($graphConfig->hasSection($serviceCommand) == False
&& $this->defaultDashboard == 'none') {
return NULL;
}
}

$this->dashboard = $graphConfig->get($serviceName, 'dashboard', $this->defaultDashboard);
if ($this->grafanaVersion == "1")
{
if ($this->grafanaVersion == "1") {
$this->dashboarduid = $graphConfig->get($serviceName, 'dashboarduid', $this->defaultdashboarduid);
} else {
$this->dashboardstore = $graphConfig->get($serviceName, 'dashboardstore', $this->defaultDashboardStore);
}
$this->panelId = $this->hasParam('panelid') ? $this->getParam('panelid') : $graphConfig->get($serviceName, 'panelId', $this->defaultDashboardPanelId);
$this->panelId = $this->hasParam('panelid')
? $this->getParam('panelid')
: $graphConfig->get($serviceName, 'panelId', $this->defaultDashboardPanelId);
$this->panelIdHost = $this->hasParam('panelidhost')
? $this->getParam('panelidhost')
: $graphConfig->get($serviceName, 'panelIdHost', $this->defaultDashboardPanelIdHost);
$this->panelIdServices = $this->hasParam('panelidservices')
? $this->getParam('panelidservices')
: $graphConfig->get($serviceName, 'panelIdServices', $this->defaultDashboardPanelIdServices);
$this->orgId = $graphConfig->get($serviceName, 'orgId', $this->defaultOrgId);
$this->customVars = $graphConfig->get($serviceName, 'customVars', '');
$this->height = $graphConfig->get($serviceName, 'height', $this->height);
$this->width = $graphConfig->get($serviceName, 'width', $this->width);

}
private function getMyimageHtml($serviceName, $hostName, &$imageHtml)

private function getMyImageHtml($serviceName, $hostName, &$imageHtml, $graphType )
{
$imgClass = $this->shadows ? "grafana-img grafana-img-shadows" : "grafana-img";
// Test whether curl is loaded
if (extension_loaded('curl') === false) {
$imageHtml = $this->translate('CURL extension is missing. Please install CURL for PHP and ensure it is loaded.');
return false;
}
if ($this->grafanaVersion == "1")
{

if ($this->grafanaVersion == "1") {
$this->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',
'%s://%s/render/d-solo/%s/%s?var-hostname=%s&var-type=%s&var-service=%s&var-command=%s%s&panelId=%s&orgId=%s&width=%s&height=%s&theme=%s&from=%s&to=%s',
$this->protocol,
$this->grafanaHost,
$this->dashboarduid,
$this->dashboard,
rawurlencode($hostName),
rawurlencode($graphType),
rawurlencode($serviceName),
rawurlencode($this->object->check_command),
$this->customVars,
Expand All @@ -319,14 +337,14 @@ private function getMyimageHtml($serviceName, $hostName, &$imageHtml)
urlencode($this->timerangeto)
);
} else {

$this->pngUrl = sprintf(
'%s://%s/render/dashboard-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',
'%s://%s/render/dashboard-solo/%s/%s?var-hostname=%s&var-type=%s&var-service=%s&var-command=%s%s&panelId=%s&orgId=%s&width=%s&height=%s&theme=%s&from=%s&to=%s',
$this->protocol,
$this->grafanaHost,
$this->dashboardstore,
$this->dashboard,
rawurlencode($hostName),
rawurlencode($graphType),
rawurlencode($serviceName),
rawurlencode($this->object->check_command),
$this->customVars,
Expand All @@ -353,7 +371,9 @@ private function getMyimageHtml($serviceName, $hostName, &$imageHtml)
);

if ($this->authentication == "token") {
$curl_opts[CURLOPT_HTTPHEADER] = array('Content-Type: application/json' , "Authorization: Bearer ". $this->apiToken);
$curl_opts[CURLOPT_HTTPHEADER] = array(
'Content-Type: application/json',
"Authorization: Bearer ". $this->apiToken);
} else {
$curl_opts[CURLOPT_USERPWD] = "$this->myAuth";
}
Expand All @@ -366,7 +386,7 @@ private function getMyimageHtml($serviceName, $hostName, &$imageHtml)
if ($res === false) {
$imageHtml .=$this->translate('Cannot fetch graph with curl') .': '. curl_error($curl_handle). '.';

//provide a hint for 'Failed to connect to ...: Permission denied'
// provide a hint for 'Failed to connect to ...: Permission denied'
if (curl_errno($curl_handle) == 7) {
$imageHtml .= $this->translate(' Check SELinux/Firewall.');
}
Expand All @@ -384,4 +404,4 @@ private function getMyimageHtml($serviceName, $hostName, &$imageHtml)
$imageHtml = $res;
return true;
}
}
}
Loading