Skip to content
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

Log activation or deactivation of services applied to hosts #2915

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
7 changes: 7 additions & 0 deletions application/forms/IcingaServiceForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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);
}
}
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
Loading