Skip to content

Commit

Permalink
formatting, fix result logic, version 0.2 (#22)
Browse files Browse the repository at this point in the history
* formatting, fix result logic, version 0.2

* Update CHANGELOG.md

---------

Co-authored-by: TheWitness <[email protected]>
  • Loading branch information
xmacan and TheWitness authored Nov 2, 2024
1 parent 77948ee commit 485adaf
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 42 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ---
Expand Down
2 changes: 1 addition & 1 deletion INFO
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ name = servcheck
version = 0.2
longname = Service Monitor
author = The Cacti Group, Petr Macek
email = deverlopers@cacti.net, [email protected]
email = developers@cacti.net, [email protected]
homepage = https://cacti.net
compat = 1.2.24
capabilities = online_view:1, online_mgmt:1, offline_view:0, offline_mgmt:0, remote_collect:1
59 changes: 43 additions & 16 deletions poller_servcheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
Expand All @@ -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(?)
Expand Down Expand Up @@ -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');

Expand All @@ -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;
}

52 changes: 27 additions & 25 deletions servcheck_process.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -62,7 +62,7 @@

switch ($arg) {
case '--id':
$test_id = $value;
$test_id = intval($value);
break;
case '-d':
case '--debug':
Expand All @@ -84,19 +84,19 @@
$force = true;
break;
default:
print 'ERROR: Invalid Parameter ' . $parameter . "\n\n";
print 'ERROR: Invalid Parameter ' . $parameter . PHP_EOL . PHP_EOL;
display_help();
exit;
}
}
}

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);
}

Expand All @@ -113,17 +113,16 @@
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);
}

$poller = db_fetch_cell_prepared('SELECT * FROM poller WHERE id = ?',
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 = ?',
Expand Down Expand Up @@ -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);
Expand All @@ -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'];
Expand All @@ -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) {
Expand All @@ -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') {
Expand All @@ -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
Expand All @@ -272,8 +268,6 @@
}
}



if ($sendemail) {
plugin_servcheck_debug('Time to send email', $test);

Expand Down Expand Up @@ -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'],
Expand Down
2 changes: 2 additions & 0 deletions servcheck_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 485adaf

Please sign in to comment.