summaryrefslogtreecommitdiffstats
path: root/application/clicommands/CleanupCommand.php
blob: f0041c8a710c4cbe0688dbb0984760dcd153e733 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php

namespace Icinga\Module\Businessprocess\Clicommands;

use Exception;
use Icinga\Application\Logger;
use Icinga\Application\Modules\Module;
use Icinga\Cli\Command;
use Icinga\Module\Businessprocess\Modification\NodeRemoveAction;
use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport;
use Icinga\Module\Businessprocess\State\IcingaDbState;
use Icinga\Module\Businessprocess\State\MonitoringState;
use Icinga\Module\Businessprocess\Storage\LegacyStorage;

class CleanupCommand extends Command
{
    /**
     * @var LegacyStorage
     */
    protected $storage;

    protected $defaultActionName = 'cleanup';

    public function init()
    {
        $this->storage = LegacyStorage::getInstance();
    }

    /**
     * Cleanup all missing monitoring nodes from the specified config name
     * If no config name is specified, the missing nodes are cleaned from all available configs.
     * Invalid config files and file names are ignored
     *
     * USAGE
     *
     * icingacli businessprocess cleanup [<config-name>]
     *
     * OPTIONS
     *
     *   <config-name>
     */
    public function cleanupAction(): void
    {
        $configNames = (array) $this->params->shift() ?: $this->storage->listAllProcessNames();
        $foundMissingNode = false;
        foreach ($configNames as $configName) {
            if (! $this->storage->hasProcess($configName)) {
                continue;
            }

            try {
                $bp = $this->storage->loadProcess($configName);
            } catch (Exception $e) {
                Logger::error(
                    'Failed to scan the %s.conf file for missing nodes. Faulty config found.',
                    $configName
                );

                continue;
            }

            if (Module::exists('icingadb')
                && (! $bp->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend())
            ) {
                IcingaDbState::apply($bp);
            } else {
                MonitoringState::apply($bp);
            }

            $removedNodes = [];
            foreach (array_keys($bp->getMissingChildren()) as $missingNode) {
                $node = $bp->getNode($missingNode);
                $remove = new NodeRemoveAction($node);

                try {
                    if ($remove->appliesTo($bp)) {
                        $remove->applyTo($bp);
                        $removedNodes[] = $node->getName();
                        $this->storage->storeProcess($bp);
                        $bp->clearAppliedChanges();

                        $foundMissingNode = true;
                    }
                } catch (Exception $e) {
                    Logger::error(sprintf('(%s.conf) %s', $configName, $e->getMessage()));

                    continue;
                }
            }

            if (! empty($removedNodes)) {
                echo sprintf(
                    'Removed following %d missing node(s) from %s.conf successfully:',
                    count($removedNodes),
                    $configName
                );

                echo "\n" . implode("\n", $removedNodes) . "\n\n";
            }
        }

        if (! $foundMissingNode) {
            echo "No missing node found.\n";
        }
    }
}