-
-
Notifications
You must be signed in to change notification settings - Fork 397
/
dsdebug.php
375 lines (306 loc) · 12.1 KB
/
dsdebug.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
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2024 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/ |
+-------------------------------------------------------------------------+
*/
/* dsstats_debug - this simple routine print's a standard message to the console
when running in debug mode.
@returns - NULL */
function dsdebug_debug($message) {
global $debug;
if ($debug) {
print 'DSDEBUG: ' . $message . "\n";
}
}
/* log_dsstats_statistics - provides generic timing message to both the Cacti log and the settings
table so that the statistics can be graphed as well.
@arg $type - (string) the type of statistics to log, either 'HOURLY', 'DAILY' or 'MAJOR'.
@returns - null */
function log_dsdebug_statistics($type, $checks, $issues) {
global $start;
/* take time and log performance data */
$end = microtime(true);
$cacti_stats = sprintf('ChecksPerformed:%d, TotalIssues:%d, Time:%01.4f ', $checks, $issues, round($end-$start,4));
/* log to the database */
set_config_option('stats_dsdebug_' . $type, $cacti_stats);
/* log to the logfile */
cacti_log('DSDEBUG STATS: Type:' . $type . ', ' . $cacti_stats , true, 'SYSTEM');
}
/* dsstats_error_handler - this routine logs all PHP error transactions
to make sure they are properly logged.
@arg $errno - (int) The errornum reported by the system
@arg $errmsg - (string) The error message provides by the error
@arg $filename - (string) The filename that encountered the error
@arg $linenum - (int) The line number where the error occurred
@arg $vars - (mixed) The current state of PHP variables.
@returns - (bool) always returns true for some reason */
function dsdebug_error_handler($errno, $errmsg, $filename, $linenum, $vars = []) {
if (read_config_option('log_verbosity') >= POLLER_VERBOSITY_DEBUG) {
/* define all error types */
$errortype = array(
E_ERROR => 'Error',
E_WARNING => 'Warning',
E_PARSE => 'Parsing Error',
E_NOTICE => 'Notice',
E_CORE_ERROR => 'Core Error',
E_CORE_WARNING => 'Core Warning',
E_COMPILE_ERROR => 'Compile Error',
E_COMPILE_WARNING => 'Compile Warning',
E_USER_ERROR => 'User Error',
E_USER_WARNING => 'User Warning',
E_USER_NOTICE => 'User Notice',
E_STRICT => 'Runtime Notice'
);
if (defined('E_RECOVERABLE_ERROR')) {
$errortype[E_RECOVERABLE_ERROR] = 'Catchable Fatal Error';
}
/* create an error string for the log */
$err = "ERRNO:'" . $errno . "' TYPE:'" . $errortype[$errno] .
"' MESSAGE:'" . $errmsg . "' IN FILE:'" . $filename .
"' LINE NO:'" . $linenum . "'";
/* let's ignore some lesser issues */
if (substr_count($errmsg, 'date_default_timezone')) return;
if (substr_count($errmsg, 'Only variables')) return;
/* log the error to the Cacti log */
cacti_log('PROGERR: ' . $err, false, 'DSDEBUG');
}
return;
}
function dsdebug_poller_output(&$rrd_update_array) {
global $config, $ds_types, $ds_last, $ds_steps, $ds_multi;
/* suppress warnings */
if (defined('E_DEPRECATED')) {
error_reporting(E_ALL ^ E_DEPRECATED);
} else {
error_reporting(E_ALL);
}
/* install the dsstats error handler */
set_error_handler('dsdebug_error_handler');
/* do not make any calculations unless enabled */
$checks = db_fetch_assoc('SELECT * FROM data_debug WHERE `done` = 0');
if (cacti_sizeof($checks)) {
if (cacti_sizeof($rrd_update_array)) {
foreach ($checks as $c) {
foreach($rrd_update_array as $item) {
if ($c['datasource'] == $item['local_data_id']) {
if (isset($item['times'][key($item['times'])])) {
$c['info'] = cacti_unserialize($c['info']);
$c['info']['last_result'] = $item['times'][key($item['times'])];
$c['info'] = serialize($c['info']);
db_execute_prepared('UPDATE data_debug SET `info` = ? WHERE `id` = ?', array($c['info'], $c['id']));
}
}
}
}
}
}
/* restore original error handler */
restore_error_handler();
}
function dsdebug_poller_bottom() {
global $config, $start;
/* install the dsstats error handler */
set_error_handler('dsdebug_error_handler');
/* take time and log performance data */
$start = microtime(true);
if (!db_table_exists('data_debug')) {
return true;
}
$checks = db_fetch_assoc('SELECT *
FROM data_debug
WHERE `done` = 0');
if (!empty($checks)) {
clearstatcache();
$total_issues = 0;
foreach ($checks as $c) {
$c['issue'] = array();
$info = cacti_unserialize($c['info']);
$dtd = db_fetch_row_prepared('SELECT *
FROM data_template_data
WHERE local_data_id = ?',
array($c['datasource']));
if (!isset($dtd['local_data_id'])) {
$c['issue'][] = __('Data Source ID %s does not exist', $c['datasource']);
$c['done'] = 1;
$total_issues++;
} else {
if (read_config_option('boost_rrd_update_enable') == 'on') {
boost_process_poller_output($c['datasource']);
}
$real_path = str_replace('<path_rra>', $config['rra_path'], $dtd['data_source_path']);
// rrd_folder_writable
$info['rrd_folder_writable'] = (is_resource_writable(dirname($real_path) . '/') ? 1 : 0);
// rrd_exists
$info['rrd_exists'] = (file_exists($real_path) ? 1 : 0);
// rrd_writable
$info['rrd_writable'] = (is_resource_writable($real_path) ? 1 : 0);
// active
$info['active'] = $dtd['active'];
// owner
if ($config['cacti_server_os'] == 'win32') {
$info['owner'] = '<unable to determine on windows>';
} else {
$o = posix_getpwuid(fileowner($real_path));
$o = $o['name'];
$g = posix_getgrgid(filegroup($real_path));
$g = $g['name'];
$info['owner'] = $o . ':' . $g;
}
// poller_runas
$info['runas_poller'] = get_running_user();
// convert_name
$info['convert_name'] = (strpos('|', get_data_source_title($c['datasource'])) === false ? 1 : 0);
// last_result (processed by hook)
if (is_array($info['last_result']) && !empty($info['last_result']) && $info['valid_data'] == '') {
$info['valid_data'] = 1;
foreach ($info['last_result'] as $k => $l) {
if ($l == 'U') {
cacti_log('Bad Data Found for Data Source ID ' . $c['datasource'], false, 'DSDEBUG');
$info['valid_data'] = 0;
$total_issues++;
}
}
}
// rrd_match
$rrdinfo = rrdtool_function_info($c['datasource']);
if (cacti_sizeof($rrdinfo)) {
$comp = rrdtool_cacti_compare($c['datasource'], $rrdinfo);
$info['rrd_match'] = (is_array($comp) && empty($comp) ? 1 : 0);
$info['rrd_match_array'] = $comp;
$info['rrd_info'] = $rrdinfo;
// rra_timestamp
if ($info['rra_timestamp'] != ''
&& isset($rrdinfo['last_update'])
&& $info['rra_timestamp'] != $rrdinfo['last_update']) {
$info['rra_timestamp2'] = $rrdinfo['last_update'];
}
if (isset($rrdinfo['last_update']) && $info['rra_timestamp'] == '') {
$info['rra_timestamp'] = $rrdinfo['last_update'];
}
$c['done'] = 1;
$f = array();
foreach ($info as $k => $v) {
if ($v === '') {
$c['done'] = 0;
$f[] = $k;
}
}
if ($c['started'] < time() - ($dtd['rrd_step'] * 5)) {
$c['done'] = 1;
$c['issue'][] = __('Debug not completed after 5 pollings');
$c['issue'][] = __('Failed fields: ') . implode(', ', $f);
$total_issues++;
}
// Try to determine issue
// Not set as Active
// Log permanent fails first
if ($info['active'] != 'on') {
$c['issue'][] = __('Data Source is not set as Active');
$c['done'] = 1;
$total_issues++;
}
// File Permissions
if ((!$info['rrd_exists'] || !$info['rrd_writable']) && !$info['rrd_folder_writable']) {
$c['issue'][] = __('RRDfile Folder (rra) is not writable by Poller. Folder owner: %s. Poller runs as: %s', $o, $info['runas_poller']);
$c['done'] = 1;
$total_issues++;
} elseif (!$info['rrd_writable']) {
$c['issue'][] = __('RRDfile is not writable by Poller. RRDfile owner: %s. Poller runs as %s', $o, $info['runas_poller']);
$c['done'] = 1;
$total_issues++;
}
// For errors that only appear after so many errors next
if ($c['done'] == 1) {
if ($info['rrd_match'] == 0) {
$c['issue'][] = __('RRDfile does not match Data Source');
$total_issues++;
}
if ($info['rra_timestamp2'] == '') {
$c['issue'][] = __('RRDfile not updated after polling');
$total_issues++;
}
if (is_array($info['last_result']) && !empty($info['last_result'])) {
foreach ($info['last_result'] as $k => $l) {
if ($l == 'U') {
$c['issue'][] = __('Data Source returned Bad Results for ' . $k);
$total_issues++;
}
}
} elseif ($info['last_result'] == '') {
$c['issue'][] = __('Data Source was not polled');
$total_issues++;
}
if ($c['issue'] == '') {
$c['issue'][] = __('No issues found');
}
}
} else {
$c['issue'][] = __('RRDfile not created yet');
$total_issues++;
}
$info = serialize($info);
db_execute_prepared('UPDATE data_debug
SET `done` = ?, `info` = ?, `issue` = ?
WHERE id = ?',
array($c['done'], $info, trim(implode("\n", $c['issue'])), $c['id']));
}
}
log_dsdebug_statistics('poller', cacti_sizeof($checks), $total_issues);
}
restore_error_handler();
}
function dsdebug_run_repair($id) {
$check = db_fetch_row_prepared('SELECT *
FROM data_debug
WHERE datasource = ?',
array($id));
if (cacti_sizeof($check)) {
$check['info'] = cacti_unserialize($check['info']);
if (isset($check['info']['rrd_match_array']['tune'])) {
$path = get_data_source_path($id, true);
if (is_writeable($path)) {
$rrdtool_path = read_config_option('path_rrdtool');
$failures = 0;
$failure_data = '';
foreach($check['info']['rrd_match_array']['tune'] AS $options) {
$command = $rrdtool_path . ' tune ' . $options;
$output = rrdtool_execute('tune ' . $options, false, RRDTOOL_OUTPUT_RETURN_STDERR);
if ($output == '') {
cacti_log("RRDfile repair command succeeded for DS[$id] Command[$command]", false, 'DSDEBUG');
} else {
cacti_log("ERROR: RRDfile repair command failed for DS[$id] Command[$command] Output[$output]", false, 'DSDEBUG');
$failures++;
}
}
if ($failures == 0) {
return true;
}
} else {
cacti_log("ERROR: RRDfile Repair Command Failed for DS[$id] Output[Unable to write RRDfile]", false, 'DSDEBUG');
}
} else {
cacti_log("ERROR: RRDfile Repair Command Could not be run for DS[$id] Output[No tune recommendation found]", false, 'DSDEBUG');
}
} else {
cacti_log("ERROR: RRDfile Repair Command Could not be run for DS[$id] Output[No Data Source debug information found]", false, 'DSDEBUG');
}
return false;
}