1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
<?php
/* Icinga Web 2 - EventDB | (c) 2017 Icinga Development Team | GPLv2+ */
namespace Icinga\Module\Eventdb\ProvidedHook\Monitoring;
use Icinga\Application\Config;
use Icinga\Application\Logger;
use Icinga\Authentication\Auth;
use Icinga\Data\Filter\Filter;
use Icinga\Data\Filter\FilterParseException;
use Icinga\Exception\InvalidPropertyException;
use Icinga\Module\Eventdb\Data\LegacyFilterParser;
use Icinga\Module\Monitoring\Object\Host;
use Icinga\Module\Monitoring\Object\MonitoredObject;
use Icinga\Module\Monitoring\Object\Service;
use Icinga\Web\Navigation\Navigation;
use Icinga\Web\Url;
use Icinga\Web\UrlParams;
class EventdbActionHook
{
protected static $wantCache = true;
protected static $cachedNav = array();
protected static $customFilters = array();
public static function wantCache($bool=true)
{
static::$wantCache = $bool;
}
/**
* @param MonitoredObject $object
* @param bool $no_cache
*
* @return null|Filter
*/
public static function getCustomFilter(MonitoredObject $object)
{
if (! Auth::getInstance()->hasPermission('eventdb/events')) {
return null;
}
$objectKey = static::getObjectKey($object);
// check cache if the filter already have been rendered
if (static::$wantCache && array_key_exists($objectKey, self::$customFilters)) {
return self::$customFilters[$objectKey];
}
$config = static::config();
$custom_var = $config->get('custom_var', null);
$service = null;
$edb_filter = null;
if ($custom_var !== null && $object instanceof Service) {
$edb_filter = $object->{'_service_' . $custom_var . '_filter'};
$service = $object->service_description;
} elseif ($custom_var !== null && $object instanceof Host) {
$edb_filter = $object->{'_host_' . $custom_var . '_filter'};
}
$customFilter = null;
if ($edb_filter !== null) {
if (LegacyFilterParser::isJsonFilter($edb_filter)) {
try {
$customFilter = LegacyFilterParser::parse(
$edb_filter,
$object->host_name,
$service
);
} catch (InvalidPropertyException $e) {
Logger::warning($e->getMessage());
}
} else {
try {
$customFilter = Filter::fromQueryString($edb_filter);
if (! in_array('host_name', $customFilter->listFilteredColumns())) {
$customFilter = $customFilter->andFilter(Filter::expression('host_name', '=', $object->host_name));
}
} catch (FilterParseException $e) {
Logger::warning('Could not parse custom EventDB filter: %s (%s)', $edb_filter, $e->getMessage());
}
}
}
return self::$customFilters[$objectKey] = $customFilter;
}
/**
* @param MonitoredObject $object Host or Service to render for
* @param bool $no_cache Only for testing - to avoid caching
*
* @return array|Navigation
*/
public static function getActions(MonitoredObject $object)
{
if (! Auth::getInstance()->hasPermission('eventdb/events')) {
return array();
}
$objectKey = static::getObjectKey($object);
// check cache if the buttons already have been rendered
if (static::$wantCache && array_key_exists($objectKey, self::$cachedNav)) {
return self::$cachedNav[$objectKey];
}
$nav = new Navigation();
$config = static::config();
$custom_var = $config->get('custom_var', null);
$edb_cv = null;
$always_on = null;
if ($custom_var !== null && $object instanceof Service) {
$edb_cv = $object->{'_service_' . $custom_var};
$always_on = $config->get('always_on_service', 0);
} elseif ($custom_var !== null && $object instanceof Host) {
$edb_cv = $object->{'_host_' . $custom_var};
$always_on = $config->get('always_on_host', 0);
}
$customFilter = static::getCustomFilter($object);
if ($customFilter !== null) {
$params = UrlParams::fromQueryString($customFilter->toQueryString());
$nav->addItem(
'events_filtered',
array(
'label' => mt('eventdb', 'Filtered events'),
'url' => Url::fromPath('eventdb/events')->setParams($params),
'icon' => 'tasks',
'class' => 'action-link',
'priority' => 1
)
);
}
// show access to all events, if (or)
// - custom_var is not configured
// - always_on is configured
// - custom_var is configured and set on object (to any value)
if ($custom_var === null || ! empty($edb_cv) || ! empty($always_on)) {
$nav->addItem(
'events',
array(
'label' => mt('eventdb', 'All events for host'),
'url' => Url::fromPath(
'eventdb/events',
array(
'host_name' => $object->host_name,
)
),
'icon' => 'tasks',
'class' => 'action-link',
'priority' => 99
)
);
}
return self::$cachedNav[$objectKey] = $nav;
}
protected static function getObjectKey(MonitoredObject $object)
{
$type = $object->getType();
$objectKey = sprintf('%!%', $type, $object->host_name);
if ($type === 'service') {
$objectKey .= '!' . $object->service_description;
}
return $objectKey;
}
protected static function config()
{
return Config::module('eventdb')->getSection('monitoring');
}
}
|