Skip to content

Commit fb43a02

Browse files
committed
Log activation or deactivation of services applied to hosts
1 parent 853b6ce commit fb43a02

3 files changed

Lines changed: 74 additions & 1 deletion

File tree

application/forms/IcingaServiceForm.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Icinga\Module\Director\Auth\Permission;
1010
use Icinga\Module\Director\Data\PropertiesFilter\ArrayCustomVariablesFilter;
1111
use Icinga\Module\Director\Exception\NestingError;
12+
use Icinga\Module\Director\Objects\DirectorActivityLog;
1213
use Icinga\Module\Director\Objects\IcingaObject;
1314
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
1415
use Icinga\Module\Director\Objects\IcingaHost;
@@ -286,6 +287,9 @@ protected function blacklist()
286287
'host_id' => $host->get('id'),
287288
'service_id' => $service->get('id')
288289
])) {
290+
$host->vars()->set('blacklisted_service', $service->getObjectName());
291+
DirectorActivityLog::logServiceBlacklist($host, $this->getDb());
292+
289293
$msg = sprintf(
290294
$this->translate('%s has been deactivated on %s'),
291295
$service->getObjectName(),
@@ -327,6 +331,9 @@ protected function removeFromBlacklist()
327331
$service->getObjectName(),
328332
$host->getObjectName()
329333
);
334+
335+
$host->vars()->set('blacklisted_service', $service->getObjectName());
336+
DirectorActivityLog::logServiceBlacklist($host, $this->getDb(), false);
330337
$this->redirectOnSuccess($msg);
331338
}
332339
}

library/Director/Objects/DirectorActivityLog.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,44 @@ public static function logModification(IcingaObject $object, Db $db)
173173
return static::create($data)->store($db);
174174
}
175175

176+
public static function logServiceBlacklist(IcingaHost $host, Db $db, bool $blacklist = true)
177+
{
178+
$name = $host->getObjectName();
179+
$type = $host->getTableName();
180+
181+
if ($blacklist) {
182+
$oldProps = json_encode($host->getPlainUnmodifiedObject());
183+
$newProps = $host->toJson(null, true);
184+
} else {
185+
$oldProps = $host->toJson(null, true);
186+
$newProps = json_encode($host->getPlainUnmodifiedObject());
187+
}
188+
189+
$data = [
190+
'object_name' => $name,
191+
'action_name' => self::ACTION_MODIFY,
192+
'author' => static::username(),
193+
'object_type' => $type,
194+
'old_properties' => $oldProps,
195+
'new_properties' => $newProps,
196+
'change_time' => date('Y-m-d H:i:s'),
197+
'parent_checksum' => $db->getLastActivityChecksum()
198+
];
199+
200+
$data['checksum'] = sha1(json_encode($data), true);
201+
$data['parent_checksum'] = hex2bin($data['parent_checksum']);
202+
203+
static::audit($db, [
204+
'action' => self::ACTION_MODIFY,
205+
'object_type' => $type,
206+
'object_name' => $name,
207+
'old_props' => $oldProps,
208+
'new_props' => $newProps
209+
]);
210+
211+
return static::create($data)->store($db);
212+
}
213+
176214
public static function logRemoval(IcingaObject $object, Db $db)
177215
{
178216
$name = $object->getObjectName();

library/Director/Web/Widget/ActivityLogInfo.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use gipfl\IcingaWeb2\Url;
2525
use gipfl\IcingaWeb2\Widget\NameValueTable;
2626
use gipfl\IcingaWeb2\Widget\Tabs;
27+
use ipl\Html\Text;
2728

2829
class ActivityLogInfo extends HtmlDocument
2930
{
@@ -126,6 +127,26 @@ public function showTab($tabName)
126127

127128
$this->getTabs()->activate($tabName);
128129
$this->add($this->getInfoTable());
130+
131+
if ($this->entry->object_type === 'icinga_host') {
132+
$newBlacklistedService = $this->newObject()->vars()->get('blacklisted_service');
133+
$oldBlackListedService = $this->oldObject()->vars()->get('blacklisted_service');
134+
$action = $newBlacklistedService !== null ? 'deactivated' : 'reactivated';
135+
136+
if ($newBlacklistedService || $oldBlackListedService) {
137+
$this->addHtml(
138+
new HtmlElement('div', null, new Text(sprintf(
139+
'Service %s has been %s on host %s',
140+
$newBlacklistedService ?? $oldBlackListedService,
141+
$action,
142+
$this->newObject()->getObjectName()
143+
)))
144+
);
145+
146+
return $this;
147+
}
148+
}
149+
129150
if ($tabName === 'old') {
130151
// $title = sprintf('%s former config', $this->entry->object_name);
131152
$diffs = IcingaConfigDiff::getDiffs($this->oldConfig(), $this->emptyConfig());
@@ -571,7 +592,14 @@ public function getInfoTable()
571592
$this->translate('Checksum'),
572593
$entry->checksum
573594
);
574-
if ($this->entry->old_properties) {
595+
596+
if (
597+
$this->entry->old_properties
598+
&& (
599+
$this->newObject()->vars()->get('blacklisted_service') === null
600+
&& $this->oldObject()->vars()->get('blacklisted_service') === null
601+
)
602+
) {
575603
$table->addNameValueRow(
576604
$this->translate('Actions'),
577605
$this->getRestoreForm()

0 commit comments

Comments
 (0)