Skip to content

Rework caching #23

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 4, 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
38 changes: 17 additions & 21 deletions application/controllers/IcingadbimgController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ class IcingadbimgController extends IcingadbGrafanaController
protected $custvarconfig = "grafana_graph_config";
protected $SSLVerifyPeer = false;
protected $SSLVerifyHost = "0";
protected $cacheTime;
protected $cacheTime = 300;
protected $defaultdashboarduid;
protected $refresh = "yes";
protected $enableCache = "yes";
protected $customVars;
protected $timerangeto;
protected $object;
Expand All @@ -68,7 +68,9 @@ public function init()
} else {
$this->timerangeto = strpos($this->timerange ?? '', '/') ? 'now-' . $this->timerange ?? '' : "now";
}
$this->cacheTime = $this->hasParam('cachetime') ? $this->getParam('cachetime') : 300;

// 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 */
$this->myConfig = Config::module('grafana')->getSection('grafana');
Expand Down Expand Up @@ -97,7 +99,7 @@ public function init()
$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->refresh = $this->myConfig->get('indirectproxyrefresh', $this->refresh);
$this->enableCache = $this->myConfig->get('enablecache', $this->enableCache);
/**
* Read the global default timerange
*/
Expand Down Expand Up @@ -214,21 +216,15 @@ public function indexAction()
}

$imageHtml = "";
$res = $this->getMyimageHtml($serviceName, $hostName, $imageHtml);
header('Pragma: public');
if ($this->refresh == "yes") {
header('Pragma: public');
header("Expires: ".gmdate("D, d M Y H:i:s", time() + $this->cacheTime)." GMT");
header('Cache-Control: max-age='.$this->cacheTime).', public';
} else {
header("Expires: ".gmdate("D, d M Y H:i:s", time() + 365*86440)." GMT");
header('Cache-Control: max-age='. (365*86440));
$result = $this->getMyimageHtml($serviceName, $hostName, $imageHtml);

if ($this->enableCache === 'yes') {
header('Cache-Control: public, max-age=' . $this->cacheTime);
}

header("Content-type: image/png");
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);

if (! $result) {
$string = wordwrap($this->translate('Error'). ': ' . $imageHtml, 40, "\n");
$lines = explode("\n", $string);
$im = @imagecreate($this->width, $this->height);
Expand Down Expand Up @@ -368,11 +364,11 @@ private function getMyimageHtml($serviceName, $hostName, &$imageHtml)
}

curl_setopt_array($curl_handle, $curl_opts);
$res = curl_exec($curl_handle);
$result = curl_exec($curl_handle);

$statusCode = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);

if ($res === false) {
if ($result === false) {
$imageHtml .=$this->translate('Cannot fetch graph with curl') .': '. curl_error($curl_handle). '.';

//provide a hint for 'Failed to connect to ...: Permission denied'
Expand All @@ -383,7 +379,7 @@ private function getMyimageHtml($serviceName, $hostName, &$imageHtml)
}

if ($statusCode > 299) {
$error = @json_decode($res);
$error = @json_decode($result);
$imageHtml .= $this->translate('Cannot fetch Grafana graph')
. ": "
. Util::httpStatusCodeToString($statusCode)
Expand All @@ -395,7 +391,7 @@ private function getMyimageHtml($serviceName, $hostName, &$imageHtml)
}

curl_close($curl_handle);
$imageHtml = $res;
$imageHtml = $result;
return true;
}

Expand Down
6 changes: 3 additions & 3 deletions application/forms/Config/GeneralConfigForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,15 @@ public function createElements(array $formData)
if (isset($formData['grafana_accessmode']) && $formData['grafana_accessmode'] === 'indirectproxy') {
$this->addElement(
'select',
'grafana_indirectproxyrefresh',
'grafana_enablecache',
[
'label' => $this->translate('Refresh on indirect proxy'),
'label' => $this->translate('Enable cache'),
'value' => 'yes',
'multiOptions' => [
'yes' => $this->translate('Yes'),
'no' => $this->translate('No'),
],
'description' => $this->translate('Refresh graphs on indirect proxy mode.')
'description' => $this->translate('Enable cache on indirect proxy mode.')
]
);
}
Expand Down
11 changes: 6 additions & 5 deletions doc/03-module-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ datasource = "influxdb"
defaultdashboardstore = "db"
accessmode = "proxy"
timeout = "5"
directrefresh = "no"
enablecache = "no"
usepublic = "no"
publichost = "otherhost:3000"
publicprotocol = "http"
Expand Down Expand Up @@ -80,7 +80,7 @@ ssl_verifyhost = "0"
|username | **Proxy with basic only** **Required** HTTP Basic Auth user name to access Grafana.|
|password | **Proxy with basic only** **Required** HTTP Basic Auth password to access Grafana. Requires the username setting.|
|apitoken | **Proxy with token only** **Required** Token of a Service Account to access Grafana.|
|indirectproxyrefresh | **Indirect Proxy Only** **Optional.** Refresh graphs on indirect proxy mode. Defaults to `yes`.|
|enablecache | **Indirect Proxy Only** **Optional.** Enables or disable caching graphs. Defaults to `yes`. |
|usepublic | **Optional** Enable usage of publichost/protocol. Defaults to `no`.|
|publichost | **Optional** Use a different host for the graph links.|
|publicprotocol | **Optional** Use a different protocol for the graph links.|
Expand Down Expand Up @@ -190,9 +190,10 @@ The password used to authenticate to Grafana server.
For token access you need to create a Service Account and assign it a token.
See the [Grafana Docs](https://grafana.com/docs/grafana/latest/administration/service-accounts/).

### indirectproxyrefresh
Only for `indirectproxy` access mode. Enables or disable graph refresh with an interval of the service interval.
Defaults to `yes`.
### enablecache
Only for `indirectproxy` access mode. Enables or disable caching graphs. Defaults to `yes`.

Note that depending on your browser images might be cached implicitly.

### usepublic
Enables/Disables the usage of a `public` URL to the Grafana server.
Expand Down
4 changes: 2 additions & 2 deletions library/Grafana/ProvidedHook/Icingadb/IcingaDbGrapher.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,8 @@ private function getMyPreviewHtml($serviceName, $hostName, HtmlDocument $preview
public function getPreviewHtml(Model $object, $report = false)
{
$this->object = $object;
//$this->cacheTime = round($object->state->next_check - $object->state->last_update);
$this->cacheTime = 0;
// Use the cachetime based on the check intervals
$this->cacheTime = $object->check_interval;

if ($object instanceof Host) {
$serviceName = $object->checkcommand_name;
Expand Down
Loading