diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d6d02d..bd34b51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * issue#17: Remove dependency on thold plugin, notify lists remain if thold is installed * issue#15: Fix DNS test issue * issue#19: Rename old thold names, fix incorrect variable +* issue: Fix incorrect result logic * feature#14: add settings tab, add send email separately option --- 0.1 --- diff --git a/INFO b/INFO index a84f59c..2b763b6 100644 --- a/INFO +++ b/INFO @@ -24,7 +24,7 @@ name = servcheck version = 0.2 longname = Service Monitor author = The Cacti Group, Petr Macek -email = deverlopers@cacti.net, petr.macek@kostax.cz +email = developers@cacti.net, petr.macek@kostax.cz homepage = https://cacti.net compat = 1.2.24 capabilities = online_view:1, online_mgmt:1, offline_view:0, offline_mgmt:0, remote_collect:1 diff --git a/poller_servcheck.php b/poller_servcheck.php index b71ecfd..b2a7ffe 100644 --- a/poller_servcheck.php +++ b/poller_servcheck.php @@ -75,7 +75,7 @@ display_help(); exit; default: - print "ERROR: Invalid Parameter " . $parameter . "\n\n"; + print 'ERROR: Invalid Parameter ' . $parameter . PHP_EOL . PHP_EOL; display_help(); exit; } @@ -88,7 +88,7 @@ plugin_servcheck_check_debug(); -print "Running Service Checks\n"; +print 'Running Service Checks' . PHP_EOL; // Remove old logs $t = time() - (86400 * 30); @@ -109,25 +109,25 @@ AND poller_id = ?', array($poller_id)); -$max = 12; +$max_processes = 12; if (cacti_sizeof($tests)) { foreach($tests as $test) { - $total = db_fetch_cell_prepared('SELECT COUNT(id) + $running_processes = db_fetch_cell_prepared('SELECT COUNT(id) FROM plugin_servcheck_processes WHERE poller_id = ?', array($poller_id)); - if ($max - $total > 0) { + if ($max_processes - $running_processes > 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); + sleep(2); } else { - usleep(10000); + sleep(2); db_execute_prepared('DELETE FROM plugin_servcheck_processes WHERE time < FROM_UNIXTIME(?) @@ -155,10 +155,38 @@ } } +// stats +$stat_ok = 0; +$stat_ko = 0; +$stat_search_ok = 0; +$stat_search_ko = 0; + +foreach ($tests as $test) { + $test_last = db_fetch_row_prepared('SELECT result, result_search + FROM plugin_servcheck_log + WHERE test_id = ? + ORDER BY id DESC LIMIT 1', + array($test['id'])); + + if ($test_last['result'] == 'ok' || $test_last['result'] == 'not yet') { + $stat_ok++; + } else { + $stat_ko++; + } + + if ($test_last['result_search'] == 'ok' || $test_last['result_search'] == 'not yet ' || $test_last['result_search'] == 'not tested') { + $stat_search_ok++; + } else { + $stat_search_ko++; + } +} + $end = microtime(true); $ttime = round($end - $start, 2); -$stats = 'Time:' . $ttime . ' Checks:' . sizeof($tests); +$stats = 'Time:' . $ttime . ' Checks:' . cacti_sizeof($tests) . + ' Results(ok/problem):' . $stat_ok . '/' . $stat_ko . + ' Search results(ok/problem):' . $stat_search_ok . '/' . $stat_search_ko; cacti_log("SERVCHECK STATS: $stats", false, 'SYSTEM'); @@ -178,20 +206,19 @@ function display_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"; + $info = plugin_servcheck_version(); + print 'Cacti Service Check Master Process, Version ' . $info['version'] . ', ' . COPYRIGHT_YEARS . PHP_EOL; } /** * display_help - displays the usage of the function */ function display_help () { - display_version(); + 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"; + print PHP_EOL . 'usage: poller_servcheck.php [--debug] [--force]' . PHP_EOL . PHP_EOL; + print 'This binary will exec all the Service check child processes.' . PHP_EOL . PHP_EOL; + print '--force - Force all the service checks to run now' . PHP_EOL . PHP_EOL; + print '--debug - Display verbose output during execution' . PHP_EOL . PHP_EOL; } diff --git a/servcheck_process.php b/servcheck_process.php index d88f4ee..5b4f5c9 100644 --- a/servcheck_process.php +++ b/servcheck_process.php @@ -44,7 +44,7 @@ $debug = false; $force = false; -$test_id = ''; +$test_id = 0; $poller_interval = read_config_option('poller_interval'); $cert_expiry_days = read_config_option('servcheck_certificate_expiry_days'); @@ -62,7 +62,7 @@ switch ($arg) { case '--id': - $test_id = $value; + $test_id = intval($value); break; case '-d': case '--debug': @@ -84,7 +84,7 @@ $force = true; break; default: - print 'ERROR: Invalid Parameter ' . $parameter . "\n\n"; + print 'ERROR: Invalid Parameter ' . $parameter . PHP_EOL . PHP_EOL; display_help(); exit; } @@ -92,11 +92,11 @@ } if (!function_exists('curl_init')) { - print "FATAL: You must install php-curl to use this Plugin" . PHP_EOL; + print 'FATAL: You must install php-curl to use this Plugin' . PHP_EOL; } -if (empty($test_id)) { - print "ERROR: You must specify a test id\n"; +if (empty($test_id) || !is_int($test_id)) { + print 'ERROR: You must specify a test id' . PHP_EOL; exit(1); } @@ -113,9 +113,8 @@ WHERE id = ? ' . $enabled, array(($poller_interval-10), $test_id)); - if (!cacti_sizeof($test)) { - print "ERROR: Test not Found\n"; + print 'ERROR: Test not Found' . PHP_EOL; exit(1); } @@ -123,7 +122,7 @@ array($test['poller_id'])); if ($poller == false) { - print "Selected poller not found, changing to poller 1.\n"; + print 'Selected poller not found, changing to poller 1' . PHP_EOL; db_execute_prepared('UPDATE plugin_servcheck_test SET poller_id = 1 WHERE id = ?', @@ -186,7 +185,7 @@ if (cacti_sizeof($results) == 0) { plugin_servcheck_debug('Unknown error for test ' . $test['id'], $test); - exit('Unknown errof for test ' . $test['id']); + exit('Unknown error for test ' . $test['id']); } plugin_servcheck_debug('failures:'. $test['stats_bad'] . ', triggered:' . $test['triggered'], $test); @@ -196,7 +195,7 @@ if (isset($results['options']['certinfo'][0])) { plugin_servcheck_debug('Returned certificate info: ' . clean_up_lines(var_export($results['options']['certinfo'], true)) , $test); - $parsed = date_parse_from_format("M j H:i:s Y e", $results['options']['certinfo'][0]['Expire date']); + $parsed = date_parse_from_format('M j H:i:s Y e', $results['options']['certinfo'][0]['Expire date']); $exp = mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $parsed['month'], $parsed['day'], $parsed['year']); $test['days'] = round(($exp - time()) / 86400); $test['expiry_date'] = $parsed['day'] . '. ' . $parsed['month'] . '. ' . $parsed['year']; @@ -205,9 +204,9 @@ $test['status_change'] = false; -$last_log = db_fetch_row_prepared("SELECT * +$last_log = db_fetch_row_prepared('SELECT * FROM plugin_servcheck_log - WHERE test_id = ? ORDER BY id DESC LIMIT 1", + WHERE test_id = ? ORDER BY id DESC LIMIT 1', array ($test['id'])); if (!$last_log) { @@ -221,13 +220,11 @@ $test['stats_bad'] += 1; } - if ($last_log['result'] != $results['result'] || $last_log['result_search'] != $results['result_search'] || ($test['certexpirenotify'] && $test_expiry_days > 0 && $test['days'] < $cert_expiry_days)) { plugin_servcheck_debug('Checking for trigger', $test); - $sendemail = false; if ($results['result'] != 'ok') { @@ -241,21 +238,20 @@ } if ($results['result'] == 'ok') { - if ($test['failures'] == 0 && $test['triggered'] == 1) { + if ($test['triggered'] == 1) { $sendemail = true; - $test['triggered'] = 0; $test['status_change'] = true; - $test['failures'] = 0; } - } + $test['triggered'] = 0; + $test['failures'] = 0; + + } if ($last_log['result_search'] != $results['result_search']) { $sendemail = true; } - - if ($test['certexpirenotify'] && $cert_expiry_days > 0 && $test['days'] < $cert_expiry_days) { // notify once per day @@ -272,8 +268,6 @@ } } - - if ($sendemail) { plugin_servcheck_debug('Time to send email', $test); @@ -311,16 +305,24 @@ } } else { plugin_servcheck_debug('Not checking for trigger', $test); + + if ($results['result'] != 'ok') { + $test['failures']++; + $test['triggered'] = 1; + } else { + $test['triggered'] = 0; + $test['failures'] = 0; + } } plugin_servcheck_debug('Updating Statistics', $test); -db_execute_prepared("INSERT INTO plugin_servcheck_log +db_execute_prepared('INSERT INTO plugin_servcheck_log (test_id, lastcheck, cert_expire, result, http_code, error, total_time, namelookup_time, connect_time, redirect_time, redirect_count, size_download, speed_download, result_search, curl_return_code) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', array($test['id'], date('Y-m-d H:i:s', $results['time']), date('Y-m-d H:i:s', $exp), $results['result'], $results['options']['http_code'], $results['error'], diff --git a/servcheck_test.php b/servcheck_test.php index 418eb83..62878c9 100644 --- a/servcheck_test.php +++ b/servcheck_test.php @@ -1095,6 +1095,8 @@ function list_tests() { if ($row['enabled'] == '') { $style = "color:rgba(10,10,10,0.8);background-color:rgba(205, 207, 196, 0.6)"; + } elseif ($row['failures'] > 0 && $row['failures'] < $row['downtrigger']) { + $style = "color:rgba(10,10,10,0.8);background-color:rgba(242, 242, 36, 0.6);"; } elseif ($last_log['result'] != 'ok' && strtotime($row['lastcheck']) > 0) { $style = "color:rgba(10,10,10,0.8);background-color:rgba(242, 25, 36, 0.6);"; } else {