-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy paththold_notify.php
More file actions
240 lines (195 loc) · 6.68 KB
/
Copy paththold_notify.php
File metadata and controls
240 lines (195 loc) · 6.68 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
<?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);
}
chdir(__DIR__);
chdir('../../');
require_once('./include/cli_check.php');
require_once($config['base_path'] . '/lib/rrd.php');
require($config['base_path'] . '/plugins/thold/includes/arrays.php');
require_once($config['base_path'] . '/plugins/thold/thold_functions.php');
require_once($config['library_path'] . '/snmp.php');
require_once($config['base_path'] . '/lib/time.php');
// install signal handlers for Linux/UNIX only
if (function_exists('pcntl_signal')) {
pcntl_signal(SIGTERM, 'sig_handler');
pcntl_signal(SIGINT, 'sig_handler');
}
// help with microtime(true)
// ini_set('precision', 16);
// process calling arguments
$parms = $_SERVER['argv'];
array_shift($parms);
$thread = false;
$debug = false;
global $thread;
if (sizeof($parms)) {
foreach ($parms as $parameter) {
if (strpos($parameter, '=')) {
[$arg, $value] = explode('=', $parameter);
} else {
$arg = $parameter;
$value = '';
}
switch ($arg) {
case '-d':
case '--debug':
$debug = true;
break;
case '--thread':
$thread = $value;
if (!is_numeric($thread) || $thread <= 0) {
print 'FATAL: The Thread ID must be numeric and greater than 0.' . PHP_EOL;
display_help();
exit(1);
}
break;
case '-v':
case '--version':
case '-V':
display_version();
exit;
case '--help':
case '-h':
case '-H':
display_help();
exit;
default:
print 'ERROR: Invalid Parameter ' . $parameter . "\n\n";
display_help();
}
}
}
// Record start time for the pid's processing
$start = microtime(true);
// This is where we can parallelize
$collector = ($thread === false);
$pid = 0;
$total_rows = 0;
if ($collector) {
thold_cli_debug('Thold Notification Main Collector Started');
$thread = 1;
} else {
thold_cli_debug("Thold Notification Child Thread $thread Started");
}
$timeout = 9999999999;
// kill any running services that have run outside of their timeout
if (!register_process_start('thold_notify', 'child', $thread, $timeout)) {
$running_pid = db_fetch_cell_prepared('SELECT pid
FROM processes
WHERE tasktype = "thold_notify"
AND taskname = "child"
AND taskid = ?',
[$thread]);
if ($config['cacti_server_os'] == 'unix' && function_exists('posix_getpgid')) {
$running = posix_getpgid($running_pid);
} elseif ($config['cacti_server_os'] == 'unix' && function_exists('posix_kill')) {
$running = posix_kill($running_pid, 0);
} else {
/*
* Without a way to ask whether the recorded process is alive, assume
* it is. Carrying on regardless is what let a second instance run
* beside the first and mail the same queue twice.
*/
$running = true;
}
if ($running) {
exit(1);
}
unregister_process('thold_notify', 'child', $thread);
register_process_start('thold_notify', 'child', $thread, $timeout);
}
/*
* Claim the queue only once this instance is the registered one, and only the
* rows nobody else holds. Claiming before the registration above meant a
* second instance stamped its own identifier over the first instance's rows
* even in the case where it went on to exit.
*/
if ($collector) {
$pid = getmypid();
db_execute_prepared('UPDATE notification_queue
SET process_id = ?
WHERE event_processed = 0
AND process_id = 0',
[$pid]);
$total_rows = db_affected_rows();
}
// Drain only what was claimed. Passing nothing selected every unprocessed row,
// so two overlapping runs both mailed the same notifications.
thold_notification_execute($pid);
$end = microtime(true);
cacti_log(sprintf('THOLD NOTIFY STATS: Time:%0.2f Notifications:%s', $end - $start, $total_rows), false, 'SYSTEM');
unregister_process('thold_notify', 'child', $thread);
exit(0);
/**
* sig_handler - provides a generic means to catch exceptions to the Cacti log.
*
* @param $signo - (int) the signal that was thrown by the interface.
*
* @returns - null
*/
function sig_handler($signo) {
global $thread;
switch ($signo) {
case SIGTERM:
case SIGINT:
thold_cacti_log('WARNING: Thold Daemon Notification Child Process with PID[' . getmypid() . '] terminated by user', $thread);
unregister_process('thold_notify', 'child', $thread);
exit;
break;
default:
// ignore all other signals
}
}
function thold_daemon_debug($message, $thread) {
global $debug;
$daemon_debug = read_config_option('thold_daemon_debug');
if ($debug || $daemon_debug) {
thold_cacti_log($message, $thread);
}
}
function thold_cli_debug($string) {
global $debug;
if ($debug) {
$output = date('Y-m-d H:i:s') . ' DEBUG: ' . trim($string);
print $output . PHP_EOL;
}
}
function display_version() {
global $config;
if (!function_exists('plugin_thold_version')) {
include_once($config['base_path'] . '/plugins/thold/setup.php');
}
$info = plugin_thold_version();
print 'Threshold Notification Processor, Version ' . $info['version'] . ', ' . COPYRIGHT_YEARS . PHP_EOL;
}
// display_help - displays the usage of the function
function display_help() {
display_version();
print PHP_EOL . 'usage: thold_notify.php [--thread=N] [--debug]' . PHP_EOL . PHP_EOL;
print 'The Threshold Notification Processor for the Thold Plugin.' . PHP_EOL;
}