forked from Cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
poller_spikekill.php
executable file
·297 lines (231 loc) · 8.19 KB
/
poller_spikekill.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
#!/usr/bin/env php
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2023 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 |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
require(__DIR__ . '/include/cli_check.php');
ini_set('memory_limit', '-1');
/* process calling arguments */
$parms = $_SERVER['argv'];
array_shift($parms);
global $debug, $start, $seed, $forcerun;
$debug = false;
$forcerun = false;
$templates = false;
$kills = 0;
if (cacti_sizeof($parms)) {
foreach ($parms as $parameter) {
if (strpos($parameter, '=')) {
list($arg, $value) = explode('=', $parameter);
} else {
$arg = $parameter;
$value = '';
}
switch ($arg) {
case '-d':
case '--debug':
$debug = true;
break;
case '--templates':
$templates = $value;
break;
case '-f':
case '--force':
$forcerun = true;
break;
case '--version':
case '-V':
case '-v':
display_version();
exit(0);
case '--help':
case '-H':
case '-h':
display_help();
exit(0);
default:
print 'ERROR: Invalid Parameter ' . $parameter . PHP_EOL . PHP_EOL;
display_help();
exit(1);
}
}
}
/* silently end if the registered process is still running, or process table missing */
if (!register_process_start('spikekill', 'master', 0, read_config_option('spikekill_timeout'))) {
exit(0);
}
print 'NOTE: SpikeKill Running' . PHP_EOL;
if (!$templates) {
$templates = db_fetch_cell("SELECT value FROM settings WHERE name='spikekill_templates'");
if ($templates != '') {
$templates = explode(',', $templates);
}
} else {
$templates = explode(',', $templates);
}
if (!cacti_sizeof($templates)) {
print 'ERROR: No valid Graph Templates selected' . PHP_EOL . PHP_EOL;
unregister_process('spikekill', 'master', 0);
exit(1);
} else {
foreach ($templates as $template) {
if (!is_numeric($template)) {
print "ERROR: Graph Template '" . $template . "' Invalid" . PHP_EOL . PHP_EOL;
unregister_process('spikekill', 'master', 0);
exit(1);
}
}
}
if (timeToRun()) {
debug('Starting Spikekill Process');
$start = microtime(true);
$graphs = kill_spikes($templates, $kills);
$purges = 0;
if (read_config_option('spikekill_purge') > 0) {
$purges = purge_spike_backups();
}
$end = microtime(true);
$cacti_stats = sprintf(
'Time:%01.4f ' .
'Graphs:%s ' .
'Purges:%s ' .
'Kills:%s',
round($end - $start,2),
$graphs,
$purges,
$kills);
/* log to the database */
db_execute_prepared('REPLACE INTO settings (name,value) VALUES ("stats_spikekill", ?)', array($cacti_stats));
/* log to the logfile */
cacti_log('SPIKEKILL STATS: ' . $cacti_stats , true, 'SYSTEM');
}
print 'NOTE: SpikeKill Finished' . PHP_EOL;
unregister_process('spikekill', 'master', 0);
exit(0);
function timeToRun() {
global $forcerun;
$lastrun = read_config_option('spikekill_lastrun');
$frequency = read_config_option('spikekill_batch') * 3600;
$basetime = strtotime(read_config_option('spikekill_basetime'));
$baseupper = 300;
$baselower = $frequency - 300;
$now = time();
debug("LastRun:'$lastrun', Frequency:'$frequency', BaseTime:'" . date('Y-m-d H:i:s', $basetime) . "', BaseUpper:'$baseupper', BaseLower:'$baselower', Now:'" . date('Y-m-d H:i:s', $now) . "'");
if ($frequency > 0 && ($now - $lastrun > $frequency)) {
debug("Frequency is '$frequency' Seconds");
$nowfreq = $now % $frequency;
debug("Now Frequency is '$nowfreq'");
if ((empty($lastrun)) && ($nowfreq > $baseupper) && ($nowfreq < $baselower)) {
debug('Time to Run');
db_execute_prepared('REPLACE INTO settings (name,value) VALUES ("spikekill_lastrun", ?)', array(time()));
return true;
}
if (($now - $lastrun > 3600) && ($nowfreq > $baseupper) && ($nowfreq < $baselower)) {
debug('Time to Run');
db_execute_prepared('REPLACE INTO settings (name,value) VALUES ("spikekill_lastrun", ?)', array(time()));
return true;
} else {
debug('Not Time to Run');
return false;
}
} elseif ($forcerun) {
debug('Force to Run');
db_execute_prepared('REPLACE INTO settings (name,value) VALUES ("spikekill_lastrun", ?', array(time()));
return true;
} else {
debug('Not time to Run');
return false;
}
}
function debug($message) {
global $debug;
if ($debug) {
print 'DEBUG: ' . trim($message) . PHP_EOL;
}
}
function purge_spike_backups() {
$directory = read_config_option('spikekill_backupdir');
$retention = read_config_option('spikekil_backup');
$purges = 0;
if (empty($retention)) {
return false;
}
$earlytime = time() - $retention;
if ($directory != '' && is_dir($directory) && is_writable($directory)) {
$files = array_diff(scandir($directory), array('.', '..'));
if (cacti_sizeof($files)) {
foreach ($files as $file) {
$filepath = $directory . '/' . $file;
if (is_file($filepath) && strpos($filepath, 'rrd') !== false) {
$mtime = filemtime($filepath);
if ($mtime < $earlytime) {
if (is_writable($filepath)) {
unlink($filepath);
$purges++;
} else {
cacti_log('Unable to remove ' . $filepath . ' due to write permissions', 'SPIKES');
}
}
}
}
}
}
return $purges;
}
function kill_spikes($templates, &$found) {
global $debug, $config;
$rrdfiles = array_rekey(db_fetch_assoc('SELECT DISTINCT rrd_path
FROM graph_templates AS gt
INNER JOIN graph_templates_item AS gti
ON gt.id=gti.graph_template_id
INNER JOIN data_template_rrd AS dtr
ON gti.task_item_id=dtr.id
INNER JOIN poller_item AS pi ON pi.local_data_id=dtr.local_data_id
WHERE gt.id IN (' . implode(',', $templates) . ')'), 'rrd_path', 'rrd_path');
if (cacti_sizeof($rrdfiles)) {
foreach ($rrdfiles as $f) {
debug("Removing Spikes from '$f'");
$response = exec(cacti_escapeshellcmd(read_config_option('path_php_binary')) . ' -q ' .
cacti_escapeshellarg(CACTI_PATH_CLI . '/removespikes.php') . ' --rrdfile=' . $f . ($debug ? ' --debug':''));
if (substr_count($response, 'Spikes Found and Remediated')) {
$found++;
}
debug(str_replace('NOTE: ', '', $response));
}
}
return cacti_sizeof($rrdfiles);
}
/* display_version - displays version information */
function display_version() {
$version = get_cacti_cli_version();
print "Cacti SpikeKiller Batch Poller, Version $version, " . COPYRIGHT_YEARS . PHP_EOL;
}
function display_help() {
display_version();
print PHP_EOL . 'usage: poller_spikekill.php [--templates=N,N,...] [--force] [--debug]' . PHP_EOL . PHP_EOL;
print "Cacti's SpikeKill batch removal poller. This poller will remove spikes" . PHP_EOL;
print "in Cacti's RRDfiles based upon the settings maintained in Cacti's database." . PHP_EOL . PHP_EOL;
print 'Optional:' . PHP_EOL;
print ' --templates=N,N,... - Only despike the templates provided.' . PHP_EOL;
print ' --force - Force running the despiking immediately.' . PHP_EOL;
print ' --debug - Display verbose output during execution.' . PHP_EOL;
}