-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsyslog_process.php
More file actions
724 lines (589 loc) · 21.2 KB
/
Copy pathsyslog_process.php
File metadata and controls
724 lines (589 loc) · 21.2 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
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 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/ |
+-------------------------------------------------------------------------+
*/
if (function_exists('pcntl_async_signals')) {
pcntl_async_signals(true);
} else {
declare(ticks = 100);
}
include(__DIR__ . '/../../include/cli_check.php');
include_once(__DIR__ . '/setup.php');
include_once(__DIR__ . '/functions.php');
include_once(__DIR__ . '/database.php');
syslog_connect();
/**
* Let it run for an hour if it has to, to clear up any big
* bursts of incoming syslog events
*/
ini_set('output_buffering', 'Off');
ini_set('max_execution_time', 3600);
ini_set('memory_limit', '-1');
set_time_limit(3600);
ob_implicit_flush();
global $debug, $syslog_facilities, $syslog_levels;
/**
* @var array<string, mixed> $config Cacti's global configuration array.
*/
global $config;
global $child, $run_id, $phase, $seq_start, $seq_end;
$debug = false;
$forcer = false;
$child = 0;
$run_id = '';
$phase = '';
$seq_start = 0;
$seq_end = 0;
// process calling arguments
$parms = $_SERVER['argv'];
array_shift($parms);
if (cacti_sizeof($parms)) {
foreach ($parms as $parameter) {
if (strpos($parameter, '=')) {
[$arg, $value] = explode('=', $parameter);
} else {
$arg = $parameter;
$value = '';
}
switch ($arg) {
case '--debug':
case '-d':
$debug = true;
break;
case '--force-report':
case '-F':
$forcer = true;
break;
case '--child':
if (preg_match('/^[1-9][0-9]*$/', $value) !== 1) {
print "ERROR: Child identifier must be a positive integer.\n\n";
display_help();
exit(1);
}
$child = (int) $value;
break;
case '--run-id':
if (preg_match('/^[a-f0-9]{32}$/', $value) !== 1) {
print "ERROR: Run identifier must be a 32 character hexadecimal value.\n\n";
display_help();
exit(1);
}
$run_id = $value;
break;
case '--phase':
if (!in_array($value, ['references', 'transfer'], true)) {
print "ERROR: Phase must be one of 'references' or 'transfer'.\n\n";
display_help();
exit(1);
}
$phase = $value;
break;
case '--seq-start':
if (preg_match('/^[0-9]+$/', $value) !== 1) {
print "ERROR: Sequence start must be a non-negative integer.\n\n";
display_help();
exit(1);
}
$seq_start = (int) $value;
break;
case '--seq-end':
if (preg_match('/^[0-9]+$/', $value) !== 1) {
print "ERROR: Sequence end must be a non-negative integer.\n\n";
display_help();
exit(1);
}
$seq_end = (int) $value;
break;
case '--version':
case '-V':
case '-v':
display_version();
exit;
case '--help':
case '-H':
case '-h':
display_help();
exit;
default:
print "ERROR: Invalid Argument: ($arg)\n\n";
display_help();
exit(1);
}
}
}
// install signal handlers for UNIX only
if (function_exists('pcntl_signal')) {
pcntl_signal(SIGTERM, 'sig_handler');
pcntl_signal(SIGINT, 'sig_handler');
}
// record the start time
$start_time = microtime(true);
$start_timestamp = time();
/**
* Read the maximum number of worker processes once. An unset or
* invalid value means single process operation which is the default.
*/
$syslog_max_workers = max(1, (int) read_config_option('syslog_max_workers'));
$syslogdb_default = isset($syslogdb_default) ? $syslogdb_default : '';
/**
* Child worker mode. When launched by the master with --child=N, the
* process executes exactly one phase of work over one seq slice and
* exits. This mirrors the child handling in Cacti's poller_boost.php.
*/
if ($child > 0) {
syslog_worker_main($debug);
// syslog_worker_main() always exits, this is a fail-safe
exit(1);
}
/**
* sanity checks before starting. The first sanity check is
* to see if Syslog has been disabled entirely. If so, then
* exit right away.
*/
if (read_config_option('syslog_enabled') == '') {
$message = 'WARNING: Syslog record transferral and alerting/reporting is disabled.';
cacti_log($message, false, 'SYSLOG');
print $message . PHP_EOL;
exit(1);
}
/**
* sanity checks before starting. The second sanity check is to
* exit if you are a remote data collector and you have not enabled
* syslog to operate remotely. If you have been, then
* if the the rules replication is enabled, get the latest rules
* from the main Cacti data collector.
*/
if ($config['poller_id'] > 1) {
if (read_config_option('syslog_remote_enabled') !== 'on') {
$message = 'WARNING: Syslog is offline and Remote Data Collector Message Processing is disabled!';
cacti_log($message, false, 'SYSLOG', POLLER_VERBOSITY_MEDIUM);
print $message . PHP_EOL;
exit(1);
}
// replicate in syslog tables sync is enabled
syslog_replicate_in();
}
/**
* Register the start of the syslog process, or if it's found to still be
* running exit until such time as the syslog process times out.
*/
if (!register_process_start('syslog', 'master', $config['poller_id'], 1200)) {
exit(0);
}
syslog_status_set('last_polling_time', $start_timestamp);
syslog_status_set('last_start_time', $start_timestamp);
/**
* initialize some key variables if they are not already initialized
* in the Cacti settings table.
*/
syslog_init_variables();
/**
* delete old syslog messages from the syslog table. This
* process may take some time. It's preferred that users
* always use partitioning as it will guarantee the best
* performing syslog database.
*/
syslog_debug('-------------------------------------------------------------------------------------');
if (!syslog_is_partitioned()) {
syslog_debug('Syslog Table is NOT Partitioned');
$phase_start = time();
$phase_micro = microtime(true);
$deleted = syslog_traditional_manage();
syslog_status_record_phase('partition', $phase_start, time(), microtime(true) - $phase_micro, $deleted);
} else {
syslog_debug('Syslog Table IS Partitioned');
$phase_start = time();
$phase_micro = microtime(true);
$deleted = syslog_partition_manage();
syslog_status_record_phase('partition', $phase_start, time(), microtime(true) - $phase_micro, $deleted);
}
syslog_debug('-------------------------------------------------------------------------------------');
/**
* pre-processing includes marking a max_seq to be used
* in the processesing of alerts and stripping domains
* from hostnames in the case that the administrator
* chooses to strip them.
*/
$results = syslog_preprocess_incoming_records();
$max_seq = $results['max_seq'];
$incoming = $results['incoming'];
/**
* place new normalized values in various reference tables
* syslog attempts to normalize things like:
*
* - hostnames
* - facilities
* - priorities
* - programs
*
* To reduce the overall size of the syslog table over
* time and to speed up searching for these various
* columns in the database.
*
* When multiple workers are configured and the platform
* supports them, the per host resolution part is spread
* across parallel worker processes operating on disjoint
* seq slices. The set based reference upserts always
* remain on the master to avoid concurrent
* ON DUPLICATE KEY deadlocks.
*/
$parallel = syslog_parallel_supported();
if ($parallel && $max_seq > 0) {
syslog_debug('Parallel Processing Enabled with ' . $syslog_max_workers . ' Worker(s)');
$run_id = bin2hex(random_bytes(16));
// ROUND 1: resolve hostnames for incoming records in parallel
$host_slices = syslog_compute_slices(1, $max_seq, $syslog_max_workers);
$launched = syslog_launch_workers('references', $host_slices, $run_id, $debug);
/*
* The reference upserts, the removal rules and the alert rules that
* follow all operate on the same incoming rows the workers are
* updating, so the master must not race ahead of its children.
*/
syslog_wait_workers($launched);
$phase_start = time();
$phase_micro = microtime(true);
syslog_normalize_reference_tables($max_seq);
syslog_status_record_phase('references', $phase_start, time(), microtime(true) - $phase_micro, $incoming);
} else {
$phase_start = time();
$phase_micro = microtime(true);
syslog_update_reference_tables($max_seq);
syslog_status_record_phase('references', $phase_start, time(), microtime(true) - $phase_micro, $incoming);
}
/**
* remove records that don't need to to be transferred
*/
$phase_start = time();
$phase_micro = microtime(true);
$results = syslog_remove_items('syslog_incoming', $max_seq);
$removed = $results['removed'];
$xferred = $results['xferred'];
syslog_status_record_phase('removal', $phase_start, time(), microtime(true) - $phase_micro, $removed + $xferred);
/**
* process the syslog rules and generate alerts
*/
$phase_start = time();
$phase_micro = microtime(true);
$results = syslog_process_alerts($max_seq);
$alerts = $results['syslog_alerts'];
$alarms = $results['syslog_alarms'];
syslog_status_record_phase('alerts', $phase_start, time(), microtime(true) - $phase_micro, $alarms);
/**
* Perform any plugin specific actions. Syslog itself does not use
* this information, but other 3rd party plugins may. This could
* be for performing certain maintenance functions that are not
* performed by syslog directly.
*/
api_plugin_hook('plugin_syslog_after_processing');
/**
* move records from incoming to syslog table and remove
* any stale records to to a poller crash
*
* In parallel mode the transfer is spread across workers
* over disjoint seq slices. Slices are recomputed after
* removal rules have deleted their share of rows so
* workers never spawn for empty ranges.
*/
if ($parallel && $max_seq > 0) {
$remaining = (int) syslog_db_fetch_cell_prepared("SELECT COUNT(seq)
FROM `$syslogdb_default`.`syslog_incoming`
WHERE `status` = 1
AND `seq` <= ?",
[$max_seq]);
if ($remaining > 0) {
$min_seq = (int) syslog_db_fetch_cell_prepared("SELECT MIN(seq)
FROM `$syslogdb_default`.`syslog_incoming`
WHERE `status` = 1
AND `seq` <= ?",
[$max_seq]);
$transfer_slices = syslog_compute_slices($min_seq, $max_seq, $syslog_max_workers);
$phase_start = time();
$phase_micro = microtime(true);
$launched = syslog_launch_workers('transfer', $transfer_slices, $run_id, $debug);
/*
* The stale record cleanup, the stats aggregation and the
* master's own exit all assume the transfer is complete, so
* block until every child has recorded its statistics and
* unregistered itself.
*/
syslog_wait_workers($launched);
$stale = syslog_delete_stale_incoming();
$worker_stats = syslog_aggregate_worker_stats($syslog_max_workers);
$moved = $worker_stats['moved'];
syslog_status_record_phase('transfer', $phase_start, time(), microtime(true) - $phase_micro, $moved);
} else {
$moved = 0;
$stale = 0;
syslog_status_record_phase('transfer', time(), time(), 0.0, 0);
}
} else {
$phase_start = time();
$phase_micro = microtime(true);
$results = syslog_incoming_to_syslog($max_seq);
$moved = $results['moved'];
$stale = $results['stale'];
syslog_status_record_phase('transfer', $phase_start, time(), microtime(true) - $phase_micro, $moved);
}
/**
* process any syslog reports that are due to be
* sent.
*/
$phase_start = time();
$phase_micro = microtime(true);
$results = syslog_process_reports();
$reports = $results['total_reports'];
$sentrpts = $results['sent_reports'];
syslog_status_record_phase('reports', $phase_start, time(), microtime(true) - $phase_micro, $sentrpts);
/**
* prune and optimize any tables that are required to
* be optimized. This should be done once a day
*/
syslog_postprocess_tables();
/**
* log messages to the Cacti log and save statistics
* to the settings table
*/
syslog_process_log($start_time, $deleted, $incoming, $removed, $xferred, $alerts, $alarms, $reports);
syslog_status_set('last_end_time', time());
syslog_status_set('last_record_count', $moved);
syslog_status_record_runtime(microtime(true) - $start_time);
/**
* unregister the syslog process entry so the next poller
* run can lock the process.
*/
unregister_process('syslog', 'master', $config['poller_id']);
exit(0);
/**
* sig_handler - handles UNIX signals and logs shutdown events to the Cacti log.
*
* The master terminates any running worker children before unregistering
* itself. Each child unregisters its own process table row.
*
* @param int $signo The signal received by the process.
*
* @return (void)
*/
function sig_handler($signo) {
global $config, $child;
switch ($signo) {
case SIGTERM:
case SIGINT:
if ($child > 0) {
cacti_log("WARNING: Syslog 'child' process $child is shutting down by signal!", false, 'SYSLOG');
unregister_process('syslog', 'child', $child);
exit(1);
}
cacti_log("WARNING: Syslog 'master' is shutting down by signal!", false, 'SYSLOG');
syslog_kill_workers();
unregister_process('syslog', 'master', $config['poller_id']);
exit(1);
default:
// ignore all other signals
}
}
/**
* syslog_worker_main - entry point for a parallel worker child process.
*
* The child registers itself in Cacti's process table, executes exactly
* one phase (references or transfer) over one seq slice, records its
* statistics in the settings table for the master to aggregate, and
* unregisters itself. A child never continues past a failure; it exits
* with a non-zero status so the master can log a warning.
*
* @param bool $debug Whether to emit verbose debug output
*
* @return (void) This function always exits.
*/
function syslog_worker_main($debug = false) {
global $child, $run_id, $phase, $seq_start, $seq_end;
// The command line is the only source of these values, revalidate
if (!syslog_validate_worker_args($child, $run_id, $phase, $seq_start, $seq_end)) {
cacti_log("ERROR: Syslog child $child rejected invalid worker arguments", false, 'SYSLOG');
exit(1);
}
if (!register_process_start('syslog', 'child', $child, 1200)) {
/*
* A previous worker with this number is still registered
* (crashed without unregistering, or the master did not wait
* for it). Never fail silently: the master checks that every
* launched child reported statistics, so leave a breadcrumb
* for that warning too.
*/
cacti_log("WARNING: Syslog child $child could not register, another worker with that number is still registered", false, 'SYSLOG');
exit(1);
}
syslog_status_set('last_worker_start', time());
$child_start = microtime(true);
$success = false;
$moved = 0;
switch ($phase) {
case 'references':
$resolved = syslog_resolve_incoming_hosts($seq_end, $seq_start, $seq_end);
$success = true;
syslog_debug(sprintf('Resolved %5s - Hostname(s) in slice %d-%d', $resolved, $seq_start, $seq_end));
break;
case 'transfer':
$results = syslog_incoming_to_syslog($seq_end, $seq_start, $seq_end);
$moved = $results['moved'];
$success = true;
syslog_debug(sprintf('Moved %5s - Message(s) in slice %d-%d', $moved, $seq_start, $seq_end));
break;
default:
// unreachable, validated above
break;
}
$child_end = microtime(true);
$stats = [
'child' => $child,
'run_id' => $run_id,
'phase' => $phase,
'moved' => $moved,
'resolved' => isset($resolved) ? $resolved : 0,
'runtime' => round($child_end - $child_start, 3),
];
set_config_option('stats_syslog_child_' . $child, json_encode($stats));
syslog_log_child_statistics($child_start, $child, $moved, isset($resolved) ? $resolved : 0);
unregister_process('syslog', 'child', $child);
cacti_log(sprintf('NOTE: Syslog child %s completed phase %s (%d-%d) in %01.2f seconds', $child, $phase, $seq_start, $seq_end, round($child_end - $child_start, 2)), false, 'SYSLOG', ($debug ? POLLER_VERBOSITY_NONE : POLLER_VERBOSITY_MEDIUM));
if ($success) {
exit(0);
}
exit(1);
}
/**
* syslog_parallel_supported - determine whether the current platform and
* configuration can run parallel worker processes.
*
* @return bool true when parallel workers may be launched
*/
function syslog_parallel_supported() {
global $syslog_max_workers;
if ($syslog_max_workers <= 1) {
return false;
}
if ($GLOBALS['config']['cacti_server_os'] == 'win32') {
return false;
}
if (!function_exists('posix_kill') || !function_exists('pcntl_signal')) {
return false;
}
return true;
}
/**
* syslog_launch_workers - spawn the configured worker child processes.
*
* Each child is started detached in the background, exactly as Cacti's
* boost poller launches its children, and coordinates through the
* process table.
*
* @param string $phase The phase to run, references or transfer
* @param array $slices The seq slices to distribute across the children
* @param string $run_id The shared run identifier for this cycle
* @param bool $debug Whether to propagate debug output to children
*
* @return int The number of children launched
*/
function syslog_launch_workers($phase, $slices, $run_id, $debug = false) {
$children = 0;
// Pre-clean any stats from a previous crashed run so aggregation
// only counts children that reported during this cycle. The
// settings table lives in the main Cacti database, not the syslog
// database, so the core helper is required here.
db_execute("DELETE FROM settings
WHERE name LIKE 'stats_syslog_child_%'");
$php_binary = read_config_option('path_php_binary');
if (empty($php_binary)) {
$php_binary = PHP_BINARY;
}
$script = __FILE__;
foreach ($slices as $index => $slice) {
$child_number = $index + 1;
$extra_args = ' -q ' . cacti_escapeshellarg($script) .
' --child=' . $child_number .
' --run-id=' . $run_id .
' --phase=' . $phase .
' --seq-start=' . $slice['start'] .
' --seq-end=' . $slice['end'] .
($debug ? ' --debug' : '');
exec_background($php_binary, $extra_args);
$children++;
}
cacti_log("NOTE: Syslog launched $children '$phase' worker process(es)", false, 'SYSLOG');
// No settle delay here: the caller's syslog_wait_workers() covers the
// registration window with a fast poll instead of a fixed sleep.
return $children;
}
/**
* syslog_kill_workers - signal any registered worker children to
* terminate. Used by the master on shutdown signals.
*
* @return (void)
*/
function syslog_kill_workers() {
$processes = db_fetch_assoc("SELECT pid
FROM processes
WHERE tasktype = 'syslog'
AND taskname = 'child'");
if (!is_array($processes) || !cacti_sizeof($processes)) {
return;
}
foreach ($processes as $process) {
$pid = (int) $process['pid'];
if (cacti_process_still_running($pid)) {
cacti_log("WARNING: Stopping Syslog worker PID $pid due to master shutdown.", false, 'SYSLOG');
cacti_process_kill($pid, SIGTERM);
}
}
}
/**
* display_version - displays version information
*
* @return (void)
*/
function display_version() {
global $config;
if (!function_exists('plugin_syslog_version')) {
include_once($config['base_path'] . '/plugins/syslog/setup.php');
}
$version = plugin_syslog_version();
print 'Syslog Poller, Version ' . trim($version['version']) . ', ' . COPYRIGHT_YEARS . PHP_EOL;
}
/**
* display_help - displays help information
*
* @return (void)
*/
function display_help() {
display_version();
print 'The main Syslog poller process script for Cacti Syslogging.' . PHP_EOL . PHP_EOL;
print 'usage: syslog_process.php [--debug] [--force-report]' . PHP_EOL . PHP_EOL;
print ' syslog_process.php --child=N --run-id=<32hex> --phase=references|transfer --seq-start=N --seq-end=N' . PHP_EOL . PHP_EOL;
print 'options:' . PHP_EOL;
print ' --force-report Send email reports now.' . PHP_EOL;
print ' --debug Provide more verbose debug output.' . PHP_EOL . PHP_EOL;
print 'worker options (used internally by the master process):' . PHP_EOL;
print ' --child The worker process number, a positive integer.' . PHP_EOL;
print ' --run-id The 32 character hexadecimal run identifier.' . PHP_EOL;
print ' --phase The phase to process, references or transfer.' . PHP_EOL;
print ' --seq-start The first seq of the slice to process.' . PHP_EOL;
print ' --seq-end The last seq of the slice to process.' . PHP_EOL . PHP_EOL;
}