From fb43a02c8b33dbcbe042c0c5f9df1a9dd69edb35 Mon Sep 17 00:00:00 2001 From: raviks789 Date: Fri, 13 Sep 2024 17:41:49 +0200 Subject: [PATCH] Log activation or deactivation of services applied to hosts --- application/forms/IcingaServiceForm.php | 7 ++++ .../Director/Objects/DirectorActivityLog.php | 38 +++++++++++++++++++ .../Director/Web/Widget/ActivityLogInfo.php | 30 ++++++++++++++- 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/application/forms/IcingaServiceForm.php b/application/forms/IcingaServiceForm.php index 5744d8d36..6c02e3e49 100644 --- a/application/forms/IcingaServiceForm.php +++ b/application/forms/IcingaServiceForm.php @@ -9,6 +9,7 @@ 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 @@ protected function blacklist() '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 @@ protected function removeFromBlacklist() $service->getObjectName(), $host->getObjectName() ); + + $host->vars()->set('blacklisted_service', $service->getObjectName()); + DirectorActivityLog::logServiceBlacklist($host, $this->getDb(), false); $this->redirectOnSuccess($msg); } } diff --git a/library/Director/Objects/DirectorActivityLog.php b/library/Director/Objects/DirectorActivityLog.php index 2cecc2eec..1f87d4a6f 100644 --- a/library/Director/Objects/DirectorActivityLog.php +++ b/library/Director/Objects/DirectorActivityLog.php @@ -173,6 +173,44 @@ public static function logModification(IcingaObject $object, Db $db) 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(); diff --git a/library/Director/Web/Widget/ActivityLogInfo.php b/library/Director/Web/Widget/ActivityLogInfo.php index 2b64fd42b..c532e22aa 100644 --- a/library/Director/Web/Widget/ActivityLogInfo.php +++ b/library/Director/Web/Widget/ActivityLogInfo.php @@ -24,6 +24,7 @@ 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 @@ public function showTab($tabName) $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 @@ public function getInfoTable() $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()