-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathpoller_intropage.php
More file actions
373 lines (306 loc) · 12 KB
/
Copy pathpoller_intropage.php
File metadata and controls
373 lines (306 loc) · 12 KB
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
<?php
/* vim: ts=4
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group, Inc. |
| Copyright (C) 2004-2025 Petr Macek |
| |
| 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. |
+-------------------------------------------------------------------------+
| https://github.com/xmacan/ |
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
$dir = __DIR__;
chdir($dir);
include('../../include/cli_check.php');
global $config;
include_once($config['base_path'] . '/lib/reports.php');
include_once($config['base_path'] . '/plugins/intropage/includes/functions.php');
// let PHP run just as long as it has to
ini_set('max_execution_time', '0');
error_reporting(E_ALL);
// record the start time
$poller_start = microtime(true);
$start_date = date('Y-m-d H:i:s');
$force = false;
$debug = false;
$checks = 0;
global $config, $database_default, $purged_r, $purged_n;
$run_from_poller = true;
// process calling arguments
$parms = $_SERVER['argv'];
array_shift($parms);
if (cacti_sizeof($parms)) {
$shortopts = 'VvHh';
$longopts = [
'force',
'debug',
'version',
'help'
];
$options = getopt($shortopts, $longopts);
foreach ($options as $arg => $value) {
switch($arg) {
case 'force':
$force = true;
break;
case 'debug':
$debug = 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 Argument: ($arg)" . PHP_EOL . PHP_EOL;
display_help();
exit(1);
}
}
}
intropage_debug('Intropage Starting Checks');
// silently end if the registered process is still running, or process table missing
if (function_exists('register_process_start')) {
if (!register_process_start('intropage', 'master', $config['poller_id'], (int) read_config_option('intropage_timeout'))) {
intropage_debug('Another Intropage Process Still Running');
exit(0);
}
}
// Make intropage first to gather correct stats
intropage_correct_load_order();
$stats = intropage_gather_stats();
$poller_end = microtime(true);
$pstats = 'Time:' . round($poller_end - $poller_start, 2) . ', Checks:' . $stats['checks'] . ', Panels:' . $stats['panels'] . ', Trends:' . $stats['trends'];
cacti_log('INTROPAGE STATS: ' . $pstats, false, 'SYSTEM');
set_config_option('stats_intropage', $pstats);
if (function_exists('unregister_process')) {
unregister_process('intropage', 'master', $config['poller_id']);
}
exit(0);
/**
* Ensures the Intropage plugin's hook execution order is first among
* registered plugins, moving it up repeatedly until its plugin_config
* id is 1, so its stats reflect other plugins' completed poller
* activity. Called from this script's main flow before gathering
* stats.
*
* @return void
*/
function intropage_correct_load_order(): void {
while (true) {
$intro_order = db_fetch_cell('SELECT id FROM plugin_config WHERE directory="intropage"');
if ($intro_order > 1) {
api_plugin_moveup('intropage');
} else {
break;
}
}
}
/**
* Runs each due panel's trend-collection and data-update functions
* (per-panel refresh/trend intervals), impersonating the owning user
* via a faked session variable where needed, then purges old trend
* records and refreshes the auto-refresh threshold timestamp. Called
* from this script's main flow as the core poller collection step.
*
* @return array Summary counts: 'checks' (panels processed),
* 'panels' (data-update functions run), and 'trends'
* (trend functions run).
*
* @global array $config Cacti global configuration array
* (declared but not directly used
* here).
* @global bool $force Reserved/declared for parity with
* this script's other functions; not
* used directly here.
* @global int $checks Reset to 0 here, then incremented for
* each panel processed; the final
* count is included in the returned
* summary.
* @global bool $run_from_poller Reserved/declared for parity with
* this script's other functions; not
* used directly here.
*/
function intropage_gather_stats(): array {
global $config, $force, $checks, $run_from_poller;
$logging = read_config_option('log_verbosity', true);
$trends = 0;
$pdata = 0;
$checks = 0;
$panels = initialize_panel_library();
$upanels = db_fetch_assoc('SELECT pd.panel_id, pd.name, ud.id, ud.user_id, level,
pd.refresh, ud.refresh_interval, pd.update_func
FROM plugin_intropage_panel_definition AS pd
LEFT JOIN plugin_intropage_panel_data AS ud
ON pd.panel_id = ud.panel_id
WHERE UNIX_TIMESTAMP(last_update) < UNIX_TIMESTAMP() - refresh_interval
OR (last_update IS NULL AND level = 0)');
$tpanels = db_fetch_assoc('SELECT pd.panel_id, pd.name, ud.id, ud.user_id, level,
pd.refresh, ud.trend_interval, ud.refresh_interval, pd.trends_func
FROM plugin_intropage_panel_definition AS pd
LEFT JOIN plugin_intropage_panel_data AS ud
ON pd.panel_id = ud.panel_id
WHERE UNIX_TIMESTAMP(last_trend_update) < UNIX_TIMESTAMP() - trend_interval
AND pd.trends_func != ""
OR (last_update IS NULL AND level = 0)');
if (cacti_sizeof($tpanels)) {
$done_trends = [];
foreach ($tpanels as $panel) {
$start = microtime(true);
// Get trends next
if (isset($panel['trends_func']) && $panel['trends_func'] != '' && is_panel_enabled($panel['panel_id'])) {
$function = $panel['trends_func'];
$function_name = (string) $function;
if (function_exists($function)) {
db_execute_prepared('UPDATE plugin_intropage_panel_data
SET last_trend_update = NOW()
WHERE id = ?',
[$panel['id']]);
// we need run it only once. Example - graph host panel gathers data for all users
if (!array_key_exists($panel['panel_id'], $done_trends)) {
$function();
$trends++;
}
$done_trends[$panel['panel_id']] = 1;
if ($logging >= 5) {
cacti_log(sprintf('DEBUG: gathering trend function:%s, duration:%4.3f', $function_name, microtime(true) - $start), false, 'INTROPAGE');
}
intropage_debug(sprintf('gathering trend function:%s, duration:%4.3f', $function_name, microtime(true) - $start));
} else {
cacti_log('WARNING: Unable to find update function ' . $function . ' for panel ' . $panel['name'], false, 'INTROPAGE');
}
}
}
}
foreach ($upanels as $upanel) {
$start = microtime(true);
// Fake a session variable
if ($upanel['user_id'] > 0) {
$_SESSION['sess_user_id'] = $upanel['user_id'];
}
// Required plugin is not installed, but entry not purged
if (!isset($panels[$upanel['panel_id']])) {
continue;
}
$panel = $panels[$upanel['panel_id']];
if ($upanel['refresh_interval'] == 0) {
db_execute_prepared('UPDATE plugin_intropage_panel_data
SET refresh_interval = ?
WHERE id = ?',
[$panel['refresh'], $upanel['id']]);
$upanel['refresh_interval'] = $panel['refresh'];
}
// process normal panels
if (isset($panel['update_func']) && $panel['update_func'] != '' && is_panel_enabled($upanel['panel_id'])) {
$qpanel = get_panel($upanel['panel_id'], $upanel['user_id']);
$function = $panel['update_func'];
$function_name = (string) $function;
if (function_exists($function)) {
$data = $function($qpanel, $upanel['user_id']);
if (is_string($data)) {
cacti_log(sprintf('WARNING: Problem with data gathering, function %s, returned (string) %s', $function_name, $data), false, 'INTROPAGE');
} elseif (is_int($data) && $data != 0) {
cacti_log(sprintf('WARNING: Problem with data gathering, function %s, returned (int) %d', $function_name, $data), false, 'INTROPAGE');
}
if ($logging >= 5) {
cacti_log(sprintf('DEBUG: gathering data function:%s, duration:%4.3f', $function_name, microtime(true) - $start), false, 'INTROPAGE');
}
intropage_debug(sprintf('gathering data function:%s, duration:%4.3f', $function_name, microtime(true) - $start));
} else {
cacti_log('WARNING: Unable to find update function ' . $function . ' for panel ' . $panel['name'], false, 'INTROPAGE');
}
$pdata++;
}
$checks++;
}
// end of gathering data
// cleaning old data
intropage_debug('Purging old Intropage Trends');
db_execute("DELETE FROM plugin_intropage_trends
WHERE cur_timestamp < date_sub(now(), INTERVAL 2 DAY) AND
name IN (
'poller', 'cpuload', 'failed_polls', 'thold_trig', 'thold_brea', 'thold_disa',
'host_down', 'host_reco', 'host_disa',
'poller_output',
'syslog_incoming', 'syslog_total', 'syslog_alert', 'syslog_levels',
'dsstats_all', 'dsstats_null', 'boost_mem_size', 'boost_pending'
)");
// automatic autorefresh
intropage_debug('Checking Triggered Thresholds');
db_execute("UPDATE plugin_intropage_trends
SET cur_timestamp = now()
WHERE name = 'ar_poller_finish'");
return ['checks' => $checks, 'panels' => $pdata, 'trends' => $trends];
}
/**
* Prints a debug message to stdout when CLI debug output is enabled.
* Called throughout this script to report collection progress.
*
* @param string $message The debug message to print.
*
* @return void
*
* @global bool $debug Whether debug output ('--debug' CLI flag) is
* enabled; when false, this function is a no-op.
*/
function intropage_debug(string $message): void {
global $debug;
if ($debug) {
print trim($message) . PHP_EOL;
}
}
/**
* Prints this poller script's version and copyright banner, loading the
* plugin's version info from setup.php if not already available. Called
* from display_help() and when invoked with '--version'/'-v'/'-V'.
*
* @return void
*
* @global array $config Cacti global configuration array; used to
* locate and include setup.php.
*/
function display_version(): void {
global $config;
if (!function_exists('plugin_intropage_version')) {
include_once($config['base_path'] . '/plugins/intropage/setup.php');
}
$info = plugin_intropage_version();
print 'Cacti Intropage Poller, Version ' . $info['version'] . ', ' . COPYRIGHT_YEARS . PHP_EOL;
}
/*
* display_help
* displays the usage of the function
*/
/**
* Prints the version banner followed by this script's command-line
* usage summary. Called when invoked with '--help'/'-h'/'-H' or with
* an invalid argument.
*
* @return void
*/
function display_help(): void {
display_version();
print PHP_EOL;
print 'usage: poller_intropage.php [--force] [--debug]' . PHP_EOL . PHP_EOL;
print ' --force - force execution, e.g. for testing' . PHP_EOL;
print ' --debug - debug execution, e.g. for testing' . PHP_EOL . PHP_EOL;
}