summaryrefslogtreecommitdiffstats
path: root/library/Director/Db/Housekeeping.php
blob: 82fd6b9758f63aad3a5070c73e039ae4de56e053 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php

namespace Icinga\Module\Director\Db;

use Icinga\Exception\NotFoundError;
use Icinga\Module\Director\Db;

class Housekeeping
{
    /**
     * @var Db
     */
    protected $connection;

    /**
     * @var \Zend_Db_Adapter_Abstract
     */
    protected $db;

    /**
     * @var int
     */
    protected $version;

    public function __construct(Db $connection)
    {
        $this->connection = $connection;
        $this->db = $connection->getDbAdapter();
    }

    public function getTaskSummary()
    {
        $summary = array();
        foreach ($this->listTasks() as $name => $title) {
            $func = 'count' . ucfirst($name);
            $summary[$name] = (object) array(
                'name'  => $name,
                'title' => $title,
                'count' => $this->$func()
            );
        }

        return $summary;
    }

    public function listTasks()
    {
        return array(
            'oldUndeployedConfigs'       => N_('Undeployed configurations'),
            'unusedFiles'                => N_('Unused rendered files'),
            'unlinkedImportedRowSets'    => N_('Unlinked imported row sets'),
            'unlinkedImportedRows'       => N_('Unlinked imported rows'),
            'unlinkedImportedProperties' => N_('Unlinked imported properties'),
            'resolveCache'               => N_('(Host) group resolve cache'),
        );
    }

    public function getPendingTaskSummary()
    {
        return array_filter(
            $this->getTaskSummary(),
            function ($task) {
                return $task->count > 0;
            }
        );
    }

    public function hasPendingTasks()
    {
        return count($this->getPendingTaskSummary()) > 0;
    }

    public function runAllTasks()
    {
        $result = array();

        foreach ($this->listTasks() as $name => $task) {
            $this->runTask($name);
        }

        return $this;
    }

    public function runTask($name)
    {
        $func = 'wipe' . ucfirst($name);
        if (!method_exists($this, $func)) {
            throw new NotFoundError(
                'There is no such task: %s',
                $name
            );
        }

        return $this->$func();
    }

    public function countOldUndeployedConfigs()
    {
        $conn = $this->connection;
        $lastActivity = $conn->getLastActivityChecksum();

        $sql = 'SELECT COUNT(*) FROM director_generated_config c'
             . '  LEFT JOIN director_deployment_log d ON c.checksum = d.config_checksum'
             . ' WHERE d.config_checksum IS NULL'
             . '   AND ? != ' . $conn->dbHexFunc('c.last_activity_checksum');

        return $this->db->fetchOne($sql, $lastActivity);
    }

    public function wipeOldUndeployedConfigs()
    {
        $conn = $this->connection;
        $lastActivity = $conn->getLastActivityChecksum();

        if ($this->connection->isPgsql()) {
            $sql = 'DELETE FROM director_generated_config'
                . ' USING director_generated_config AS c'
                . ' LEFT JOIN director_deployment_log d ON c.checksum = d.config_checksum'
                . ' WHERE director_generated_config.checksum = c.checksum'
                . '   AND d.config_checksum IS NULL'
                . '   AND ? != ' . $conn->dbHexFunc('c.last_activity_checksum');
        } else {
            $sql = 'DELETE c.* FROM director_generated_config c'
                . ' LEFT JOIN director_deployment_log d ON c.checksum = d.config_checksum'
                . ' WHERE d.config_checksum IS NULL'
                . '   AND ? != ' . $conn->dbHexFunc('c.last_activity_checksum');
        }

        return $this->db->query($sql, $lastActivity);
    }

    public function countUnusedFiles()
    {
        $sql = 'SELECT COUNT(*) FROM director_generated_file f'
             . ' LEFT JOIN director_generated_config_file cf ON f.checksum = cf.file_checksum'
             . ' WHERE cf.file_checksum IS NULL';

        return $this->db->fetchOne($sql);
    }

    public function wipeUnusedFiles()
    {
        if ($this->connection->isPgsql()) {
            $sql = 'DELETE FROM director_generated_file'
                . ' USING director_generated_file AS f'
                . ' LEFT JOIN director_generated_config_file cf ON f.checksum = cf.file_checksum'
                . ' WHERE director_generated_file.checksum = f.checksum'
                . ' AND cf.file_checksum IS NULL';
        } else {
            $sql = 'DELETE f FROM director_generated_file f'
                . ' LEFT JOIN director_generated_config_file cf ON f.checksum = cf.file_checksum'
                . ' WHERE cf.file_checksum IS NULL';
        }

        return $this->db->exec($sql);
    }

    public function countUnlinkedImportedRowSets()
    {
        $sql = 'SELECT COUNT(*) FROM imported_rowset rs LEFT JOIN import_run r'
            . ' ON r.rowset_checksum = rs.checksum WHERE r.id IS NULL';

        return $this->db->fetchOne($sql);
    }

    public function wipeUnlinkedImportedRowSets()
    {
        // This one removes imported_rowset and imported_rowset_row
        // entries no longer used by any historic import<F12>
        if ($this->connection->isPgsql()) {
            $sql = 'DELETE FROM imported_rowset'
                . ' USING  imported_rowset AS rs'
                . ' LEFT JOIN import_run r ON r.rowset_checksum = rs.checksum'
                . ' WHERE imported_rowset.checksum = rs.checksum'
                . ' AND r.id IS NULL';
        } else {
            $sql = 'DELETE rs.* FROM imported_rowset rs'
                . ' LEFT JOIN import_run r ON r.rowset_checksum = rs.checksum'
                . ' WHERE r.id IS NULL';
        }

        return $this->db->exec($sql);
    }

    public function countUnlinkedImportedRows()
    {
        $sql = 'SELECT COUNT(*) FROM imported_row r LEFT JOIN imported_rowset_row rsr'
            . ' ON rsr.row_checksum = r.checksum WHERE rsr.row_checksum IS NULL';

        return $this->db->fetchOne($sql);
    }

    public function wipeUnlinkedImportedRows()
    {
        // This query removes imported_row and imported_row_property columns
        // without related rowset
        if ($this->connection->isPgsql()) {
            $sql = 'DELETE FROM imported_row'
                . ' USING imported_row AS r'
                . ' LEFT JOIN imported_rowset_row rsr ON rsr.row_checksum = r.checksum'
                . ' WHERE imported_row.checksum = r.checksum'
                . ' AND rsr.row_checksum IS NULL';
        } else {
            $sql = 'DELETE r.* FROM imported_row r'
                . ' LEFT JOIN imported_rowset_row rsr ON rsr.row_checksum = r.checksum'
                . ' WHERE rsr.row_checksum IS NULL';
        }

        return $this->db->exec($sql);
    }

    public function countUnlinkedImportedProperties()
    {
        $sql = 'SELECT COUNT(*) FROM imported_property p LEFT JOIN imported_row_property rp'
            . ' ON rp.property_checksum = p.checksum WHERE rp.property_checksum IS NULL';

        return $this->db->fetchOne($sql);
    }

    public function wipeUnlinkedImportedProperties()
    {
        // This query removes unlinked imported properties
        if ($this->connection->isPgsql()) {
            $sql = 'DELETE FROM imported_property'
                . ' USING imported_property AS p'
                . ' LEFT JOIN imported_row_property rp ON rp.property_checksum = p.checksum'
                . ' WHERE imported_property.checksum = p.checksum'
                . ' AND rp.property_checksum IS NULL';
        } else {
            $sql = 'DELETE p.* FROM imported_property p'
                . ' LEFT JOIN imported_row_property rp ON rp.property_checksum = p.checksum'
                . ' WHERE rp.property_checksum IS NULL';
        }

        return $this->db->exec($sql);
    }

    public function countResolveCache()
    {
        $helper = MembershipHousekeeping::instance('host', $this->connection);
        return array_sum($helper->check());
    }

    public function wipeResolveCache()
    {
        $helper = MembershipHousekeeping::instance('host', $this->connection);
        return $helper->update();
    }
}