Skip to content

Add and or cleanup some inline comments #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 9, 2024
Merged
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
3 changes: 3 additions & 0 deletions application/controllers/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Icinga\Web\Controller;

/**
* ConfigController for showing the module's configuration
*/
class ConfigController extends Controller
{
public function init()
Expand Down
4 changes: 4 additions & 0 deletions application/controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

use ipl\Web\Url;

/**
* DashboardController for showing graphs for Monitoring Module dashboards
*/
class DashboardController extends Controller
{
public function init()
Expand All @@ -21,6 +24,7 @@ public function init()

public function indexAction()
{
// Redirect to IcingaDB if it is enabled
if (Module::exists('icingadb') && IcingadbSupport::useIcingaDbAsBackend()) {
$this->redirectNow(Url::fromPath('grafana/icingadbdashboard')->setQueryString($this->params));
}
Expand Down
2 changes: 2 additions & 0 deletions application/controllers/GraphController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ public function newAction()
->setIniConfig($this->Config('graphs'))
->setRedirectUrl('grafana/graph')
->handleRequest();

$this->view->form = $graphs;
}

/**
* Remove a graph
*/
Expand Down
3 changes: 3 additions & 0 deletions application/controllers/IcingadbdashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

use ipl\Web\Url;

/**
* IcingadbdashboardController for showing graphs for IcingaDB Module dashboards
*/
class IcingadbdashboardController extends IcingadbGrafanaController
{
public function init()
Expand Down
93 changes: 45 additions & 48 deletions application/controllers/IcingadbimgController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
use ipl\Stdlib\Filter;
use ipl\Web\Url;

/**
* IcingadbimgController loads graphs as images from the grafana-image-renderer.
*/
class IcingadbimgController extends IcingadbGrafanaController
{
protected $host;
Expand All @@ -21,26 +24,26 @@ class IcingadbimgController extends IcingadbGrafanaController
protected $myConfig;
protected $myAuth;
protected $authentication;
protected $grafanaHost = null;
protected $grafanaTheme = null;
protected $protocol = "http";
protected $username = null;
protected $password = null;
protected $apiToken = null;
protected $width = 640;
protected $height = 280;
protected $defaultDashboard = "icinga2-default";
protected $defaultDashboardPanelId = "1";
protected $defaultOrgId = "1";
protected $shadows = false;
protected $dataSource = null;
protected $proxyTimeout = "5";
protected $custvarconfig = "grafana_graph_config";
protected $SSLVerifyPeer = false;
protected $SSLVerifyHost = "0";
protected $cacheTime = 300;
protected $grafanaHost = null;
protected $grafanaTheme = null;
protected $protocol = "http";
protected $username = null;
protected $password = null;
protected $apiToken = null;
protected $width = 640;
protected $height = 280;
protected $defaultdashboarduid;
protected $enableCache = "yes";
protected $defaultDashboard = "icinga2-default";
protected $defaultDashboardPanelId = "1";
protected $defaultOrgId = "1";
protected $shadows = false;
protected $dataSource = null;
protected $proxyTimeout = "5";
protected $custvarconfig = "grafana_graph_config";
protected $SSLVerifyPeer = false;
protected $SSLVerifyHost = "0";
protected $cacheTime = 300;
protected $enableCache = "yes";
protected $customVars;
protected $timerangeto;
protected $object;
Expand All @@ -49,17 +52,21 @@ class IcingadbimgController extends IcingadbGrafanaController
protected $panelId;
protected $orgId;

/**
* Mainly loading defaults and the configuration.
*/
public function init()
{
if (! $this->useIcingadbAsBackend) {
$this->redirectNow(Url::fromPath('grafana/dashboard')->setQueryString($this->params));
}
/* we need at least a host name */

// 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 */
// Save timerange from params for later use
$this->timerange = $this->hasParam('timerange') ? urldecode($this->getParam('timerange')) : null;
if ($this->hasParam('timerangeto')) {
$this->timerangeto = urldecode($this->getParam('timerangeto'));
Expand All @@ -70,16 +77,18 @@ public function init()
// Get the cachetime URL parameter and use the default if not present
$this->cacheTime = $this->hasParam('cachetime') ? $this->getParam('cachetime') : $this->cacheTime;

/* load global configuration */
// Load global configuration
$this->myConfig = Config::module('grafana')->getSection('grafana');
$this->grafanaHost = $this->myConfig->get('host', $this->grafanaHost);
if ($this->grafanaHost == null) {
throw new ConfigurationError(
'No Grafana host configured!'
);
}

$this->protocol = $this->myConfig->get('protocol', $this->protocol);

// Get defaults for Grafana dashboards
$this->defaultDashboard = $this->myConfig->get('defaultdashboard', $this->defaultDashboard);
$this->defaultdashboarduid = $this->myConfig->get('defaultdashboarduid', null);
if (is_null($this->defaultdashboarduid)) {
Expand All @@ -91,43 +100,27 @@ public function init()
'defaultdashboardpanelid',
$this->defaultDashboardPanelId
);

$this->defaultOrgId = $this->myConfig->get('defaultorgid', $this->defaultOrgId);
$this->grafanaTheme = Util::getUserThemeMode(Auth::getInstance()->getUser());
$this->height = $this->myConfig->get('height', $this->height);
$this->width = $this->myConfig->get('width', $this->width);
$this->proxyTimeout = $this->myConfig->get('proxytimeout', $this->proxyTimeout);
$this->enableCache = $this->myConfig->get('enablecache', $this->enableCache);
/**
* Read the global default timerange
*/

// Read the global default timerange
if ($this->timerange == null) {
$this->timerange = $this->config->get('timerange', $this->timerange);
}
/**
* Datasource needed to regex special chars
*/

// Datasource needed to regex special chars
$this->dataSource = $this->myConfig->get('datasource', $this->dataSource);
/**
* Display shadows around graph
*/
$this->shadows = $this->myConfig->get('shadows', $this->shadows);
/**
* Name of the custom variable for graph config
*/
$this->custvarconfig = ($this->myConfig->get('custvarconfig', $this->custvarconfig));
/**
* Verify the certificate's name against host
*/
$this->SSLVerifyHost = ($this->myConfig->get('ssl_verifyhost', $this->SSLVerifyHost));
/**
* Verify the peer's SSL certificate
*/
$this->SSLVerifyPeer = ($this->myConfig->get('ssl_verifypeer', $this->SSLVerifyPeer));

/**
* Username & Password or token
*/

// 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 Down Expand Up @@ -192,7 +185,8 @@ public function indexAction()
$this->customVars = str_replace($search, $replace, $this->customVars);
}

// urlencode values
// urlencode custom variable values
// TODO: Could this be a Util function?
$customVars = "";
foreach (preg_split('/\&/', $this->customVars, -1, PREG_SPLIT_NO_EMPTY) as $param) {
$arr = explode("=", $param);
Expand All @@ -206,7 +200,8 @@ public function indexAction()
}
$this->customVars = $customVars;
}
// replace special chars for graphite

// Replace special chars for graphite
if ($this->dataSource == "graphite") {
$serviceName = Util::graphiteReplace($serviceName);
$hostName = Util::graphiteReplace($hostName);
Expand All @@ -225,8 +220,8 @@ public function indexAction()
$string = wordwrap($this->translate('Error'). ': ' . $imageHtml, 40, "\n");
$lines = explode("\n", $string);
$im = @imagecreate($this->width, $this->height);
$background_color = imagecolorallocate($im, 255, 255, 255); //white background
$text_color = imagecolorallocate($im, 255, 0, 0);//black text
$background_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, 255, 0, 0);
foreach ($lines as $i => $line) {
imagestring($im, 5, 0, 5 + $i * 15, $line, $text_color);
}
Expand Down Expand Up @@ -275,6 +270,7 @@ private function setGraphConf($serviceName, $serviceCommand = null)
private function getMyimageHtml($serviceName, $hostName, &$imageHtml)
{
$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.'
Expand Down Expand Up @@ -352,6 +348,7 @@ private function getMyimageHtml($serviceName, $hostName, &$imageHtml)

curl_close($curl_handle);
$imageHtml = $result;

return true;
}
}
16 changes: 9 additions & 7 deletions application/controllers/IcingadbshowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

class IcingadbshowController extends IcingadbGrafanaController
{
/** @var bool */
protected $showFullscreen;
protected $host;
protected $custvardisable = 'grafana_graph_disable';
Expand All @@ -33,9 +32,8 @@ public function init()
= (bool)$this->_helper->layout()->showFullscreen;
$this->host = $this->getParam('host');
$this->config = Config::module('grafana')->getSection('grafana');
/**
* Name of the custom variable to disable graph
*/

// Name of the custom variable to disable graph
$this->custvardisable = ($this->config->get('custvardisable', $this->custvardisable));
}

Expand All @@ -57,11 +55,10 @@ public function indexAction()
$parameters['timerange'] = $this->getParam('timerange');
}

/* The timerange menu */
$menu = new Timeranges($parameters, 'grafana/icingadbshow');
$this->addControl(new HtmlString($menu->getTimerangeMenu()));

/* first host object for host graph */
// First host object for host graph
$this->object = $this->getHostObject($this->host);
$varsFlat = CustomvarFlat::on($this->getDb());
$this->applyRestrictions($varsFlat);
Expand All @@ -70,7 +67,9 @@ public function indexAction()
->columns(['flatname', 'flatvalue'])
->orderBy('flatname');
$varsFlat->filter(Filter::equal('host.id', $this->object->id));

$customVars = $this->getDb()->fetchPairs($varsFlat->assembleSelect());

if ($this->object->perfdata_enabled == "y"
|| !(isset($customVars[$this->custvardisable])
&& json_decode(strtolower($customVars[$this->custvardisable])) !== false)
Expand All @@ -80,13 +79,15 @@ public function indexAction()
$this->addContent($object);
$this->addContent((new HostDetailExtension())->getPreviewHtml($this->object, true));
}
/* Get all services for this host */

// Get all services for this host
$query = Service::on($this->getDb())->with([
'state',
'icon_image',
'host',
'host.state'
]);

$query->filter(Filter::equal('host.name', $this->host));

$this->applyRestrictions($query);
Expand All @@ -101,6 +102,7 @@ public function indexAction()
->orderBy('flatname');
$varsFlat->filter(Filter::equal('service.id', $service->id));
$customVars = $this->getDb()->fetchPairs($varsFlat->assembleSelect());

if ($this->object->perfdata_enabled == "y"
&& !(isset($customVars[$this->custvardisable])
&& json_decode(strtolower($customVars[$this->custvardisable])) !== false)
Expand Down
Loading