Skip to content

Commit

Permalink
Log activation or deactivation of services applied to hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
raviks789 committed Sep 13, 2024
1 parent 853b6ce commit 50e7900
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
8 changes: 8 additions & 0 deletions application/forms/IcingaServiceForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
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;
use Icinga\Module\Director\Objects\IcingaService;
use Icinga\Module\Director\Objects\IcingaServiceSet;
use Icinga\Module\Director\Web\Table\ObjectsTableHost;
use Icinga\Module\Director\Web\Widget\ActivityLogInfo;
use ipl\Html\Html;
use gipfl\IcingaWeb2\Link;
use RuntimeException;
Expand Down Expand Up @@ -286,6 +288,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(),
Expand Down Expand Up @@ -327,6 +332,9 @@ protected function removeFromBlacklist()
$service->getObjectName(),
$host->getObjectName()
);

$host->vars()->set('blacklisted_service', $service->getObjectName());
DirectorActivityLog::logServiceBlacklist($host, $this->getDb(), false);
$this->redirectOnSuccess($msg);
}
}
Expand Down
38 changes: 38 additions & 0 deletions library/Director/Objects/DirectorActivityLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
30 changes: 29 additions & 1 deletion library/Director/Web/Widget/ActivityLogInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 50e7900

Please sign in to comment.