forked from Cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
poller_maintenance.php
460 lines (393 loc) · 13.7 KB
/
poller_maintenance.php
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2015 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDTool-based Graphing Solution |
+-------------------------------------------------------------------------+
*/
/* we are not talking to the browser */
$no_http_headers = true;
/* do NOT run this script through a web browser */
if (!isset ($_SERVER['argv'][0]) || isset ($_SERVER['REQUEST_METHOD']) || isset ($_SERVER['REMOTE_ADDR'])) {
die('<br><strong>This script is only meant to run at the command line.</strong>');
}
/* let PHP run just as long as it has to */
ini_set('max_execution_time', '0');
error_reporting(E_ALL);
$dir = dirname(__FILE__);
chdir($dir);
/* record the start time */
list($micro,$seconds) = explode(' ', microtime());
$poller_start = $seconds + $micro;
include ('./include/global.php');
global $config, $database_default, $archived, $purged;
/* process calling arguments */
$parms = $_SERVER['argv'];
array_shift($parms);
$debug = FALSE;
$force = FALSE;
$archived = 0;
$purged = 0;
foreach ($parms as $parameter) {
@list ($arg, $value) = @explode('=', $parameter);
switch ($arg) {
case '-h' :
case '-v' :
case '--version' :
case '--help' :
display_help();
exit;
case '--force' :
$force = true;
break;
case '--debug' :
$debug = true;
break;
default :
print 'ERROR: Invalid Parameter ' . $parameter . "\n\n";
display_help();
exit;
}
}
maint_debug('Checking for Purge Actions');
/* are my tables already present? */
$purge = db_fetch_cell('SELECT count(*) FROM data_source_purge_action');
/* if the table that holds the actions is present, work on it */
if ($purge) {
maint_debug("Purging Required - Files Found $purge");
/* take the purge in steps */
while (true) {
maint_debug('Grabbing 1000 RRDfiles to Remove');
$file_array = db_fetch_assoc('SELECT id, name, local_data_id, action
FROM data_source_purge_action
ORDER BY name
LIMIT 1000');
if (sizeof($file_array) == 0) {
break;
}
if (sizeof($file_array) || $force) {
/* there's something to do for us now */
remove_files($file_array);
if ($force) {
cleanup_ds_and_graphs();
}
}
}
/* record the start time */
list($micro,$seconds) = explode(' ', microtime());
$poller_end = $seconds + $micro;
$string = sprintf('RRDMAINT STATS: Time:%4.4f Purged:%s Archived:%s', ($poller_end - $poller_start), $purged, $archived);
cacti_log($string, true, 'SYSTEM');
}
/* removing security tokens older than 90 days */
if (read_config_option('auth_cache_enabled') == 'on') {
db_execute("DELETE FROM user_auth_cache WHERE last_update<'" . date('Y-m-d H:i:s', time()-(86400*90)) . "'");
}else{
db_execute('TRUNCATE TABLE user_auth_cache');
}
// Check the realtime cache and poller
realtime_purge_cache();
// Check expired accounts
secpass_check_expired();
// Check whether the cacti log needs rotating
if (read_config_option('logrotate_enabled') == 'on') {
if (date('G') == 0 && date('i') < 5 && (time() - read_config_option('logrotate_lastrun') > 3600)) {
logrotate_rotatenow();
}
}
/** realtime_purge_cache() - Thsi function will purge files in the realtime directory
* that are older than 2 hours without changes */
function realtime_purge_cache() {
/* remove all Realtime files over than 2 hours */
if (read_config_option('realtime_cache_path') != '') {
$cache_path = read_config_option('realtime_cache_path');
if (is_dir($cache_path) && is_writeable($cache_path)) {
foreach (new DirectoryIterator($cache_path) as $fileInfo) {
if ($fileInfo->isDot()) {
continue;
}
if (time() - $fileInfo->getCTime() >= 2*60*60) {
unlink($fileInfo->getRealPath());
}
}
}
}
db_execute("DELETE FROM poller_output_realtime WHERE time<FROM_UNIXTIME(UNIX_TIMESTAMP()-300)");
}
/*
* logrotate_rotatenow
* Rotates the cacti log
*/
function logrotate_rotatenow () {
global $config;
$log = $config['base_path'] . '/log/cacti.log';
set_config_option('logrotate_lastrun', time());
clearstatcache();
if (is_writable($config['base_path'] . '/log/') && is_writable($log)) {
$perms = octdec(substr(decoct( fileperms($log) ), 2));
$owner = fileowner($log);
$group = filegroup($log);
if ($owner !== FALSE) {
$ext = date('Ymd');
if (file_exists($log . '-' . $ext)) {
$ext = date('YmdHis');
}
if (rename($log, $log . '-' . $ext)) {
touch($log);
chown($log, $owner);
chgrp($log, $group);
chmod($log, $perms);
cacti_log('Cacti Log Rotation - Created Log cacti.log-' . $ext);
} else {
cacti_log('Cacti Log Rotation - ERROR: Could not rename cacti.log to ' . $log . '-' . $ext);
}
} else {
cacti_log('Cacti Log Rotation - ERROR: Permissions issue. Please check your log directory');
}
} else {
cacti_log('Cacti Log Rotation - ERROR: Permissions issue. Directory / Log not writable.');
}
logrotate_cleanold();
}
/*
* logrotate_cleanold
* Cleans up any old log files that should be removed
*/
function logrotate_cleanold () {
global $config;
$dir = scandir($config['base_path'] . '/log/');
$r = read_config_option('logrotate_retain');
if ($r == '' || $r < 0) {
$r = 7;
}
if ($r > 365) {
$r = 365;
}
if ($r == 0) {
return;
}
foreach ($dir as $d) {
if (substr($d, 0, 10) == "cacti.log-" && strlen($d) >= 18) {
$e = date('Ymd', time() - ($r * 86400));
$f = substr($d, 10, 8);
if ($f < $e) {
if (is_writable($config['base_path'] . '/log/' . $d)) {
@unlink($config['base_path'] . '/log/' . $d);
cacti_log('Cacti Log Rotation - Purging Log : ' . $d);
} else {
cacti_log('Cacti Log Rotation - ERROR: Can not purge log : ' . $d);
}
}
}
}
clearstatcache();
}
/*
* secpass_check_expired
* Checks user accounts to determine if the accounts and/or their passwords should be expired
*/
function secpass_check_expired () {
maint_debug('Checking for Account / Password expiration');
// Expire Old Accounts
$e = read_config_option('secpass_expireaccount');
if ($e > 0 && is_numeric($e)) {
$t = time();
db_execute("UPDATE user_auth SET lastlogin = $t WHERE lastlogin = -1 AND realm = 0 AND enabled = 'on'");
$t = $t - (intval($e) * 86400);
db_execute("UPDATE user_auth SET enabled = '' WHERE realm = 0 AND enabled = 'on' AND lastlogin < $t AND id > 1");
}
$e = read_config_option('secpass_expirepass');
if ($e > 0 && is_numeric($e)) {
$t = time();
db_execute("UPDATE user_auth SET lastchange = $t WHERE lastchange = -1 AND realm = 0 AND enabled = 'on'");
$t = $t - (intval($e) * 86400);
db_execute("UPDATE user_auth SET must_change_password = 'on' WHERE realm = 0 AND enabled = 'on' AND lastchange < $t");
}
}
/*
* remove_files
* remove all unwanted files; the list is given by table data_source_purge_action
*/
function remove_files($file_array) {
global $config, $debug, $archived, $purged;
include_once ($config['library_path'] . '/api_graph.php');
include_once ($config['library_path'] . '/api_data_source.php');
maint_debug('RRDClean is now running on ' . sizeof($file_array) . ' items');
/* determine the location of the RRA files */
if (isset ($config['rra_path'])) {
$rra_path = $config['rra_path'];
} else {
$rra_path = $config['base_path'] . '/rra';
}
/* let's prepare the archive directory */
$rrd_archive = read_config_option('rrd_archive', TRUE);
if ($rrd_archive == '') {
$rrd_archive = $rra_path . '/archive';
}
rrdclean_create_path($rrd_archive);
/* now scan the files */
foreach ($file_array as $file) {
$source_file = $rra_path . '/' . $file['name'];
switch ($file['action']) {
case '1' :
if (unlink($source_file)) {
maint_debug('Deleted: ' . $file['name']);
} else {
cacti_log($file['name'] . " ERROR: RRDfile Maintenance unable to delete from $rra_path!", true, 'MAINT');
}
$purged++;
break;
case '3' :
$target_file = $rrd_archive . '/' . $file['name'];
$target_dir = dirname($target_file);
if (!is_dir($target_dir)) {
rrdclean_create_path($target_dir);
}
if (rename($source_file, $target_file)) {
maint_debug('Moved: ' . $file['name'] . ' to: ' . $rrd_archive);
} else {
cacti_log($file['name'] . " ERROR: RRDfile Maintenance unable to move to $rrd_archive!", true, 'MAINT');
}
$archived++;
break;
}
/* drop from data_source_purge_action table */
db_execute_prepared('DELETE FROM `data_source_purge_action` WHERE name = ?', array($file['name']));
maint_debug('Delete from data_source_purge_action: ' . $file['name']);
//fetch all local_graph_id's according to this data source
$lgis = db_fetch_assoc_prepared('SELECT DISTINCT gl.id
FROM graph_local AS gl
INNER JOIN graph_templates_item AS gti
ON gl.id = gti.local_graph_id
INNER JOIN data_template_rrd AS dtr
ON dtr.id=gti.task_item_id
INNER JOIN data_local AS dl
ON dtr.local_data_id=dl.id
WHERE (local_data_id=?)', array($file['local_data_id']));
if (sizeof($lgis)) {
/* anything found? */
maint_debug('Processing ' . sizeof($lgis) . ' Graphs for data source id: ' . $file['local_data_id']);
/* get them all */
foreach ($lgis as $item) {
$remove_lgis[] = $item['id'];
maint_debug('remove local_graph_id=' . $item['id']);
}
/* and remove them in a single run */
if (!empty ($remove_lgis)) {
api_graph_remove_multi($remove_lgis);
}
}
/* remove related data source if any */
if ($file['local_data_id'] > 0) {
maint_debug('Removing Data Source: ' . $file['local_data_id']);
api_data_source_remove($file['local_data_id']);
}
}
maint_debug('RRDClean has finished a purge pass of ' . sizeof($file_array) . ' items');
}
function rrdclean_create_path($path) {
global $config;
if (!is_dir($path)) {
if (mkdir($path, 0775)) {
if ($config['cacti_server_os'] != 'win32') {
$owner_id = fileowner($config['rra_path']);
$group_id = filegroup($config['rra_path']);
// NOTE: chown/chgrp fails for non-root users, checking their
// result is therefore irrevelevant
@chown($path, $owner_id);
@chgrp($path, $group_id);
}
}else{
cacti_log("ERROR: RRDfile Maintenence unable to create directory '" . $path . "'", false, 'MAINT');
}
}
// if path existed, we can return true
return is_dir($path) && is_writable($path);
}
/*
* cleanup_ds_and_graphs - courtesy John Rembo
*/
function cleanup_ds_and_graphs() {
global $config;
include_once ($config['library_path'] . '/rrd.php');
include_once ($config['library_path'] . '/utility.php');
include_once ($config['library_path'] . '/api_graph.php');
include_once ($config['library_path'] . '/api_data_source.php');
include_once ($config['library_path'] . '/functions.php');
$remove_ldis = array ();
$remove_lgis = array ();
maint_debug('RRDClean now cleans up all data sources and graphs');
//fetch all local_data_id's which have appropriate data-sources
$rrds = db_fetch_assoc("SELECT local_data_id, name_cache, data_source_path
FROM data_template_data
WHERE name_cache > ''");
//filter those whose rrd files doesn't exist
foreach ($rrds as $item) {
$ldi = $item['local_data_id'];
$name = $item['name_cache'];
$ds_pth = $item['data_source_path'];
$real_pth = str_replace('<path_rra>', $config['rra_path'], $ds_pth);
if (!file_exists($real_pth)) {
if (!in_array($ldi, $remove_ldis)) {
$remove_ldis[] = $ldi;
maint_debug("RRD file is missing for data source name: $name (local_data_id=$ldi)");
}
}
}
if (empty ($remove_ldis)) {
maint_debug('No missing rrd files found');
return 0;
}
maint_debug('Processing Graphs');
//fetch all local_graph_id's according to filtered rrds
$lgis = db_fetch_assoc('SELECT DISTINCT gl.id
FROM graph_local AS gl
INNER JOIN graph_templates_item AS gti
ON gl.id=gti.local_graph_id
INNER JOIN data_template_rrd AS dtr
ON dtr.id=gti.task_item_id
INNER JOIN data_local AS dl
ON dtr.local_data_id=dl.id
WHERE (' . array_to_sql_or($remove_ldis, 'local_data_id') . ')');
foreach ($lgis as $item) {
$remove_lgis[] = $item['id'];
maint_debug('RRD file missing for local_graph_id=' . $item['id']);
}
if (!empty ($remove_lgis)) {
maint_debug('removing graphs');
api_graph_remove_multi($remove_lgis);
}
maint_debug('removing data sources');
api_data_source_remove_multi($remove_ldis);
maint_debug('removed graphs:' . count($remove_lgis) . ' removed data-sources:' . count($remove_ldis));
}
function maint_debug($message) {
global $debug;
if ($debug) {
echo trim($message) . "\n";
}
}
/*
* display_help
* displays the usage of the function
*/
function display_help() {
$version = db_fetch_cell('SELECT cacti FROM version');
print "Cacti Maintenance Script, Version $version, " . COPYRIGHT_YEARS . "\n\n";
print "usage: poller_maintenance.php [--force] [--debug] [--help] [--version]\n\n";
print "--force - force execution, e.g. for testing\n";
print "--debug - debug execution, e.g. for testing\n\n";
print "-v --version - Display this help message\n";
print "-h --help - display this help message\n";
}