Log activation or deactivation of services applied to hosts

This commit is contained in:
raviks789 2024-09-13 17:41:49 +02:00
parent 853b6ceb84
commit fb43a02c8b
No known key found for this signature in database
3 changed files with 74 additions and 1 deletions

View File

@ -9,6 +9,7 @@ use Icinga\Exception\ProgrammingError;
use Icinga\Module\Director\Auth\Permission;
use Icinga\Module\Director\Data\PropertiesFilter\ArrayCustomVariablesFilter;
use Icinga\Module\Director\Exception\NestingError;
use Icinga\Module\Director\Objects\DirectorActivityLog;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Module\Director\Objects\IcingaHost;
@ -286,6 +287,9 @@ class IcingaServiceForm extends DirectorObjectForm
'host_id' => $host->get('id'),
'service_id' => $service->get('id')
])) {
$host->vars()->set('blacklisted_service', $service->getObjectName());
DirectorActivityLog::logServiceBlacklist($host, $this->getDb());
$msg = sprintf(
$this->translate('%s has been deactivated on %s'),
$service->getObjectName(),
@ -327,6 +331,9 @@ class IcingaServiceForm extends DirectorObjectForm
$service->getObjectName(),
$host->getObjectName()
);
$host->vars()->set('blacklisted_service', $service->getObjectName());
DirectorActivityLog::logServiceBlacklist($host, $this->getDb(), false);
$this->redirectOnSuccess($msg);
}
}

View File

@ -173,6 +173,44 @@ class DirectorActivityLog extends DbObject
return static::create($data)->store($db);
}
public static function logServiceBlacklist(IcingaHost $host, Db $db, bool $blacklist = true)
{
$name = $host->getObjectName();
$type = $host->getTableName();
if ($blacklist) {
$oldProps = json_encode($host->getPlainUnmodifiedObject());
$newProps = $host->toJson(null, true);
} else {
$oldProps = $host->toJson(null, true);
$newProps = json_encode($host->getPlainUnmodifiedObject());
}
$data = [
'object_name' => $name,
'action_name' => self::ACTION_MODIFY,
'author' => static::username(),
'object_type' => $type,
'old_properties' => $oldProps,
'new_properties' => $newProps,
'change_time' => date('Y-m-d H:i:s'),
'parent_checksum' => $db->getLastActivityChecksum()
];
$data['checksum'] = sha1(json_encode($data), true);
$data['parent_checksum'] = hex2bin($data['parent_checksum']);
static::audit($db, [
'action' => self::ACTION_MODIFY,
'object_type' => $type,
'object_name' => $name,
'old_props' => $oldProps,
'new_props' => $newProps
]);
return static::create($data)->store($db);
}
public static function logRemoval(IcingaObject $object, Db $db)
{
$name = $object->getObjectName();

View File

@ -24,6 +24,7 @@ use gipfl\Translation\TranslationHelper;
use gipfl\IcingaWeb2\Url;
use gipfl\IcingaWeb2\Widget\NameValueTable;
use gipfl\IcingaWeb2\Widget\Tabs;
use ipl\Html\Text;
class ActivityLogInfo extends HtmlDocument
{
@ -126,6 +127,26 @@ class ActivityLogInfo extends HtmlDocument
$this->getTabs()->activate($tabName);
$this->add($this->getInfoTable());
if ($this->entry->object_type === 'icinga_host') {
$newBlacklistedService = $this->newObject()->vars()->get('blacklisted_service');
$oldBlackListedService = $this->oldObject()->vars()->get('blacklisted_service');
$action = $newBlacklistedService !== null ? 'deactivated' : 'reactivated';
if ($newBlacklistedService || $oldBlackListedService) {
$this->addHtml(
new HtmlElement('div', null, new Text(sprintf(
'Service %s has been %s on host %s',
$newBlacklistedService ?? $oldBlackListedService,
$action,
$this->newObject()->getObjectName()
)))
);
return $this;
}
}
if ($tabName === 'old') {
// $title = sprintf('%s former config', $this->entry->object_name);
$diffs = IcingaConfigDiff::getDiffs($this->oldConfig(), $this->emptyConfig());
@ -571,7 +592,14 @@ class ActivityLogInfo extends HtmlDocument
$this->translate('Checksum'),
$entry->checksum
);
if ($this->entry->old_properties) {
if (
$this->entry->old_properties
&& (
$this->newObject()->vars()->get('blacklisted_service') === null
&& $this->oldObject()->vars()->get('blacklisted_service') === null
)
) {
$table->addNameValueRow(
$this->translate('Actions'),
$this->getRestoreForm()