-
Notifications
You must be signed in to change notification settings - Fork 3
/
poller_servcheck.php
197 lines (160 loc) · 5.42 KB
/
poller_servcheck.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
<?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/ |
+-------------------------------------------------------------------------+
*/
/* let PHP run just as long as it has to */
ini_set('max_execution_time', '55');
$dir = dirname(__FILE__);
chdir($dir);
if (strpos($dir, 'plugins') !== false) {
chdir('../../');
}
include('./include/cli_check.php');
include_once($config['base_path'] . '/plugins/servcheck/includes/functions.php');
include_once($config['base_path'] . '/lib/poller.php');
/* process calling arguments */
$parms = $_SERVER['argv'];
array_shift($parms);
$debug = false;
$force = false;
$start = microtime(true);
$poller_id = $config['poller_id'];
if (cacti_sizeof($parms)) {
foreach($parms as $parameter) {
if (strpos($parameter, '=')) {
list($arg, $value) = explode('=', $parameter);
} else {
$arg = $parameter;
$value = '';
}
switch ($arg) {
case '-f':
case '--force':
$force = true;
break;
case '-d':
case '--debug':
$debug = true;
break;
case '--version':
case '-V':
case '-v':
display_version();
exit;
case '--help':
case '-H':
case '-h':
display_help();
exit;
default:
print "ERROR: Invalid Parameter " . $parameter . "\n\n";
display_help();
exit;
}
}
}
if (!function_exists('curl_init')) {
print "FATAL: You must install php-curl to use this Plugin" . PHP_EOL;
}
plugin_servcheck_check_debug();
print "Running Service Checks\n";
// Remove old logs
$t = time() - (86400 * 30);
if ($poller_id == 1) {
db_execute_prepared('DELETE FROM plugin_servcheck_log
WHERE lastcheck < FROM_UNIXTIME(?)',
array($t));
db_execute_prepared('DELETE FROM plugin_servcheck_processes
WHERE time < FROM_UNIXTIME(?)',
array(time() - 15));
}
$tests = db_fetch_assoc_prepared('SELECT *
FROM plugin_servcheck_test
WHERE enabled = "on"
AND poller_id = ?',
array($poller_id));
$max = 12;
if (cacti_sizeof($tests)) {
foreach($tests as $test) {
$total = db_fetch_cell_prepared('SELECT COUNT(id)
FROM plugin_servcheck_processes
WHERE poller_id = ?',
array($poller_id));
if ($max - $total > 0) {
plugin_servcheck_debug('Launching Service Check ' . $test['display_name'], $test);
$command_string = read_config_option('path_php_binary');
$extra_args = '-q "' . $config['base_path'] . '/plugins/servcheck/servcheck_process.php" --id=' . $test['id'] . ($debug ? ' --debug':'');
exec_background($command_string, $extra_args);
usleep(10000);
} else {
usleep(10000);
db_execute_prepared('DELETE FROM plugin_servcheck_processes
WHERE time < FROM_UNIXTIME(?)
AND poller_id = ?',
array(time() - 15, $poller_id));
}
}
}
while(true) {
db_execute_prepared('DELETE FROM plugin_servcheck_processes
WHERE time < FROM_UNIXTIME(?)
AND poller_id = ?',
array(time() - 15, $poller_id));
$running = db_fetch_cell_prepared('SELECT COUNT(*)
FROM plugin_servcheck_processes
WHERE poller_id = ?',
array($poller_id));
if ($running == 0) {
break;
} else {
sleep(1);
}
}
$end = microtime(true);
$ttime = round($end - $start, 2);
$stats = 'Time:' . $ttime . ' Checks:' . sizeof($tests);
cacti_log("SERVCHECK STATS: $stats", false, 'SYSTEM');
if ($poller_id == 1) {
set_config_option('stats_servcheck', $stats);
}
set_config_option('stats_servcheck_' . $poller_id, $stats);
/**
* display_version - displays version information
*/
function display_version() {
global $config;
if (!function_exists('plugin_servcheck_version')) {
include_once($config['base_path'] . '/plugins/servcheck/setup.php');
}
$info = plugin_servcheck_version();
print "Cacti Service Check Master Process, Version " . $info['version'] . ", " . COPYRIGHT_YEARS . "\n";
}
/**
* display_help - displays the usage of the function
*/
function display_help () {
display_version();
print "\nusage: poller_servcheck.php [--debug] [--force]\n\n";
print "This binary will exec all the Service check child processes.\n\n";
print "--force - Force all the service checks to run now\n";
print "--debug - Display verbose output during execution\n\n";
}