Skip to content

Commit 485adaf

Browse files
xmacanTheWitness
andauthored
formatting, fix result logic, version 0.2 (#22)
* formatting, fix result logic, version 0.2 * Update CHANGELOG.md --------- Co-authored-by: TheWitness <[email protected]>
1 parent 77948ee commit 485adaf

File tree

5 files changed

+74
-42
lines changed

5 files changed

+74
-42
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* issue#17: Remove dependency on thold plugin, notify lists remain if thold is installed
66
* issue#15: Fix DNS test issue
77
* issue#19: Rename old thold names, fix incorrect variable
8+
* issue: Fix incorrect result logic
89
* feature#14: add settings tab, add send email separately option
910

1011
--- 0.1 ---

INFO

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ name = servcheck
2424
version = 0.2
2525
longname = Service Monitor
2626
author = The Cacti Group, Petr Macek
27-
email = deverlopers@cacti.net, [email protected]
27+
email = developers@cacti.net, [email protected]
2828
homepage = https://cacti.net
2929
compat = 1.2.24
3030
capabilities = online_view:1, online_mgmt:1, offline_view:0, offline_mgmt:0, remote_collect:1

poller_servcheck.php

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
display_help();
7676
exit;
7777
default:
78-
print "ERROR: Invalid Parameter " . $parameter . "\n\n";
78+
print 'ERROR: Invalid Parameter ' . $parameter . PHP_EOL . PHP_EOL;
7979
display_help();
8080
exit;
8181
}
@@ -88,7 +88,7 @@
8888

8989
plugin_servcheck_check_debug();
9090

91-
print "Running Service Checks\n";
91+
print 'Running Service Checks' . PHP_EOL;
9292

9393
// Remove old logs
9494
$t = time() - (86400 * 30);
@@ -109,25 +109,25 @@
109109
AND poller_id = ?',
110110
array($poller_id));
111111

112-
$max = 12;
112+
$max_processes = 12;
113113

114114
if (cacti_sizeof($tests)) {
115115
foreach($tests as $test) {
116-
$total = db_fetch_cell_prepared('SELECT COUNT(id)
116+
$running_processes = db_fetch_cell_prepared('SELECT COUNT(id)
117117
FROM plugin_servcheck_processes
118118
WHERE poller_id = ?',
119119
array($poller_id));
120120

121-
if ($max - $total > 0) {
121+
if ($max_processes - $running_processes > 0) {
122122
plugin_servcheck_debug('Launching Service Check ' . $test['display_name'], $test);
123123

124124
$command_string = read_config_option('path_php_binary');
125125
$extra_args = '-q "' . $config['base_path'] . '/plugins/servcheck/servcheck_process.php" --id=' . $test['id'] . ($debug ? ' --debug':'');
126126
exec_background($command_string, $extra_args);
127127

128-
usleep(10000);
128+
sleep(2);
129129
} else {
130-
usleep(10000);
130+
sleep(2);
131131

132132
db_execute_prepared('DELETE FROM plugin_servcheck_processes
133133
WHERE time < FROM_UNIXTIME(?)
@@ -155,10 +155,38 @@
155155
}
156156
}
157157

158+
// stats
159+
$stat_ok = 0;
160+
$stat_ko = 0;
161+
$stat_search_ok = 0;
162+
$stat_search_ko = 0;
163+
164+
foreach ($tests as $test) {
165+
$test_last = db_fetch_row_prepared('SELECT result, result_search
166+
FROM plugin_servcheck_log
167+
WHERE test_id = ?
168+
ORDER BY id DESC LIMIT 1',
169+
array($test['id']));
170+
171+
if ($test_last['result'] == 'ok' || $test_last['result'] == 'not yet') {
172+
$stat_ok++;
173+
} else {
174+
$stat_ko++;
175+
}
176+
177+
if ($test_last['result_search'] == 'ok' || $test_last['result_search'] == 'not yet ' || $test_last['result_search'] == 'not tested') {
178+
$stat_search_ok++;
179+
} else {
180+
$stat_search_ko++;
181+
}
182+
}
183+
158184
$end = microtime(true);
159185
$ttime = round($end - $start, 2);
160186

161-
$stats = 'Time:' . $ttime . ' Checks:' . sizeof($tests);
187+
$stats = 'Time:' . $ttime . ' Checks:' . cacti_sizeof($tests) .
188+
' Results(ok/problem):' . $stat_ok . '/' . $stat_ko .
189+
' Search results(ok/problem):' . $stat_search_ok . '/' . $stat_search_ko;
162190

163191
cacti_log("SERVCHECK STATS: $stats", false, 'SYSTEM');
164192

@@ -178,20 +206,19 @@ function display_version() {
178206
include_once($config['base_path'] . '/plugins/servcheck/setup.php');
179207
}
180208

181-
$info = plugin_servcheck_version();
182-
183-
print "Cacti Service Check Master Process, Version " . $info['version'] . ", " . COPYRIGHT_YEARS . "\n";
209+
$info = plugin_servcheck_version();
210+
print 'Cacti Service Check Master Process, Version ' . $info['version'] . ', ' . COPYRIGHT_YEARS . PHP_EOL;
184211
}
185212

186213
/**
187214
* display_help - displays the usage of the function
188215
*/
189216
function display_help () {
190-
display_version();
217+
display_version();
191218

192-
print "\nusage: poller_servcheck.php [--debug] [--force]\n\n";
193-
print "This binary will exec all the Service check child processes.\n\n";
194-
print "--force - Force all the service checks to run now\n";
195-
print "--debug - Display verbose output during execution\n\n";
219+
print PHP_EOL . 'usage: poller_servcheck.php [--debug] [--force]' . PHP_EOL . PHP_EOL;
220+
print 'This binary will exec all the Service check child processes.' . PHP_EOL . PHP_EOL;
221+
print '--force - Force all the service checks to run now' . PHP_EOL . PHP_EOL;
222+
print '--debug - Display verbose output during execution' . PHP_EOL . PHP_EOL;
196223
}
197224

servcheck_process.php

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
$debug = false;
4646
$force = false;
47-
$test_id = '';
47+
$test_id = 0;
4848

4949
$poller_interval = read_config_option('poller_interval');
5050
$cert_expiry_days = read_config_option('servcheck_certificate_expiry_days');
@@ -62,7 +62,7 @@
6262

6363
switch ($arg) {
6464
case '--id':
65-
$test_id = $value;
65+
$test_id = intval($value);
6666
break;
6767
case '-d':
6868
case '--debug':
@@ -84,19 +84,19 @@
8484
$force = true;
8585
break;
8686
default:
87-
print 'ERROR: Invalid Parameter ' . $parameter . "\n\n";
87+
print 'ERROR: Invalid Parameter ' . $parameter . PHP_EOL . PHP_EOL;
8888
display_help();
8989
exit;
9090
}
9191
}
9292
}
9393

9494
if (!function_exists('curl_init')) {
95-
print "FATAL: You must install php-curl to use this Plugin" . PHP_EOL;
95+
print 'FATAL: You must install php-curl to use this Plugin' . PHP_EOL;
9696
}
9797

98-
if (empty($test_id)) {
99-
print "ERROR: You must specify a test id\n";
98+
if (empty($test_id) || !is_int($test_id)) {
99+
print 'ERROR: You must specify a test id' . PHP_EOL;
100100
exit(1);
101101
}
102102

@@ -113,17 +113,16 @@
113113
WHERE id = ? ' . $enabled,
114114
array(($poller_interval-10), $test_id));
115115

116-
117116
if (!cacti_sizeof($test)) {
118-
print "ERROR: Test not Found\n";
117+
print 'ERROR: Test not Found' . PHP_EOL;
119118
exit(1);
120119
}
121120

122121
$poller = db_fetch_cell_prepared('SELECT * FROM poller WHERE id = ?',
123122
array($test['poller_id']));
124123

125124
if ($poller == false) {
126-
print "Selected poller not found, changing to poller 1.\n";
125+
print 'Selected poller not found, changing to poller 1' . PHP_EOL;
127126
db_execute_prepared('UPDATE plugin_servcheck_test
128127
SET poller_id = 1
129128
WHERE id = ?',
@@ -186,7 +185,7 @@
186185

187186
if (cacti_sizeof($results) == 0) {
188187
plugin_servcheck_debug('Unknown error for test ' . $test['id'], $test);
189-
exit('Unknown errof for test ' . $test['id']);
188+
exit('Unknown error for test ' . $test['id']);
190189
}
191190

192191
plugin_servcheck_debug('failures:'. $test['stats_bad'] . ', triggered:' . $test['triggered'], $test);
@@ -196,7 +195,7 @@
196195
if (isset($results['options']['certinfo'][0])) {
197196
plugin_servcheck_debug('Returned certificate info: ' . clean_up_lines(var_export($results['options']['certinfo'], true)) , $test);
198197

199-
$parsed = date_parse_from_format("M j H:i:s Y e", $results['options']['certinfo'][0]['Expire date']);
198+
$parsed = date_parse_from_format('M j H:i:s Y e', $results['options']['certinfo'][0]['Expire date']);
200199
$exp = mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $parsed['month'], $parsed['day'], $parsed['year']);
201200
$test['days'] = round(($exp - time()) / 86400);
202201
$test['expiry_date'] = $parsed['day'] . '. ' . $parsed['month'] . '. ' . $parsed['year'];
@@ -205,9 +204,9 @@
205204

206205
$test['status_change'] = false;
207206

208-
$last_log = db_fetch_row_prepared("SELECT *
207+
$last_log = db_fetch_row_prepared('SELECT *
209208
FROM plugin_servcheck_log
210-
WHERE test_id = ? ORDER BY id DESC LIMIT 1",
209+
WHERE test_id = ? ORDER BY id DESC LIMIT 1',
211210
array ($test['id']));
212211

213212
if (!$last_log) {
@@ -221,13 +220,11 @@
221220
$test['stats_bad'] += 1;
222221
}
223222

224-
225223
if ($last_log['result'] != $results['result'] || $last_log['result_search'] != $results['result_search'] ||
226224
($test['certexpirenotify'] && $test_expiry_days > 0 && $test['days'] < $cert_expiry_days)) {
227225

228226
plugin_servcheck_debug('Checking for trigger', $test);
229227

230-
231228
$sendemail = false;
232229

233230
if ($results['result'] != 'ok') {
@@ -241,21 +238,20 @@
241238
}
242239

243240
if ($results['result'] == 'ok') {
244-
if ($test['failures'] == 0 && $test['triggered'] == 1) {
241+
if ($test['triggered'] == 1) {
245242
$sendemail = true;
246-
$test['triggered'] = 0;
247243
$test['status_change'] = true;
248-
$test['failures'] = 0;
249244
}
250-
}
245+
$test['triggered'] = 0;
246+
$test['failures'] = 0;
247+
251248

249+
}
252250

253251
if ($last_log['result_search'] != $results['result_search']) {
254252
$sendemail = true;
255253
}
256254

257-
258-
259255
if ($test['certexpirenotify'] && $cert_expiry_days > 0 && $test['days'] < $cert_expiry_days) {
260256

261257
// notify once per day
@@ -272,8 +268,6 @@
272268
}
273269
}
274270

275-
276-
277271
if ($sendemail) {
278272
plugin_servcheck_debug('Time to send email', $test);
279273

@@ -311,16 +305,24 @@
311305
}
312306
} else {
313307
plugin_servcheck_debug('Not checking for trigger', $test);
308+
309+
if ($results['result'] != 'ok') {
310+
$test['failures']++;
311+
$test['triggered'] = 1;
312+
} else {
313+
$test['triggered'] = 0;
314+
$test['failures'] = 0;
315+
}
314316
}
315317

316318
plugin_servcheck_debug('Updating Statistics', $test);
317319

318-
db_execute_prepared("INSERT INTO plugin_servcheck_log
320+
db_execute_prepared('INSERT INTO plugin_servcheck_log
319321
(test_id, lastcheck, cert_expire, result, http_code, error,
320322
total_time, namelookup_time, connect_time, redirect_time,
321323
redirect_count, size_download, speed_download, result_search,
322324
curl_return_code)
323-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
325+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
324326
array($test['id'], date('Y-m-d H:i:s', $results['time']), date('Y-m-d H:i:s', $exp),
325327
$results['result'],
326328
$results['options']['http_code'], $results['error'],

servcheck_test.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,8 @@ function list_tests() {
10951095

10961096
if ($row['enabled'] == '') {
10971097
$style = "color:rgba(10,10,10,0.8);background-color:rgba(205, 207, 196, 0.6)";
1098+
} elseif ($row['failures'] > 0 && $row['failures'] < $row['downtrigger']) {
1099+
$style = "color:rgba(10,10,10,0.8);background-color:rgba(242, 242, 36, 0.6);";
10981100
} elseif ($last_log['result'] != 'ok' && strtotime($row['lastcheck']) > 0) {
10991101
$style = "color:rgba(10,10,10,0.8);background-color:rgba(242, 25, 36, 0.6);";
11001102
} else {

0 commit comments

Comments
 (0)