Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

--- develop ---

--- 0.4 ---

* feature: Add cpu/memory statistics
* feature: Better process control
* feature: Add return data size stats
* feature: Add log renention
* feature: Command run only for selected tests

--- 0.3 ---

! IMPORTANT - A lot of changes in 0.3. I tried to convert old data.
Expand Down
2 changes: 1 addition & 1 deletion INFO
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

[info]
name = servcheck
version = 0.3
version = 0.4
longname = Service Monitor
author = The Cacti Group, Petr Macek
email = developers@cacti.net, petr.macek@kostax.cz
Expand Down
11 changes: 9 additions & 2 deletions includes/arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
'web_http' => __('HTTP plaintext, default port 80', 'servcheck'),
'web_https' => __('HTTP encrypted (HTTPS), default port 443', 'servcheck'),
'mail_smtp' => __('SMTP plaintext, default port 25 (or 587 for submission)', 'servcheck'),
'mail_smtptls' => __('SMTP with STARTTLS, default port 25(or 587 for submission)', 'servcheck'),
'mail_smtptls' => __('SMTP with STARTTLS, default port 25 (or 587 for submission)', 'servcheck'),
'mail_smtps' => __('SMTP encrypted (SMTPS), default port 465', 'servcheck'),
'mail_imap' => __('IMAP plaintext, default port 143', 'servcheck'),
'mail_imaptls' => __('IMAP with STARTTLS, default port 143', 'servcheck'),
Expand Down Expand Up @@ -390,11 +390,18 @@
],
'enabled' => [
'method' => 'checkbox',
'friendly_name' => __('Enable Service Check', 'servcheck'),
'friendly_name' => __('Enable test', 'servcheck'),
'description' => __('Uncheck this box to disable this test from being checked.', 'servcheck'),
'value' => '|arg1:enabled|',
'default' => 'on',
],
'run_script' => [
'method' => 'checkbox',
'friendly_name' => __('Enable run script for this test', 'servcheck'),
'description' => __('If the global Command Execution function is enabled, you can set here whether the script should be run for this test or not.', 'servcheck'),
'value' => '|arg1:run_script|',
'default' => 'on',
],
'poller_id' => [
'friendly_name' => __('Poller', 'servcheck'),
'method' => 'drop_sql',
Expand Down
10 changes: 4 additions & 6 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ function servcheck_graph($id, $interval) {
global $config, $graph_interval;

$result = db_fetch_assoc_prepared('SELECT
lastcheck, duration FROM plugin_servcheck_log
last_check, duration FROM plugin_servcheck_log
WHERE test_id = ? AND
lastcheck > DATE_SUB(NOW(), INTERVAL ? HOUR)
last_check > DATE_SUB(NOW(), INTERVAL ? HOUR)
ORDER BY id', [$id, $interval]);

if (cacti_sizeof($result) < 5) {
Expand All @@ -89,7 +89,7 @@ function servcheck_graph($id, $interval) {
$xid = 'xx' . substr(md5($graph_interval[$interval]), 0, 7);

foreach ($result as $row) {
$lastcheck[] = $row['lastcheck'];
$last_check[] = $row['last_check'];
$duration[] = round($row['duration'], 5);
}

Expand All @@ -115,10 +115,8 @@ function servcheck_graph($id, $interval) {
$axes = [];

// Add the X Axis first
$columns[] = array_merge(['x'], $lastcheck);
$columns[] = array_merge(['x'], $last_check);
$columns[] = array_merge(['Duration'], $duration);
// $columns[] = array_merge(array('Connect'), $connect_time);
// $columns[] = array_merge(array('DNS '), $namelookup_time);

// Setup the Axis
$axis['x'] = [
Expand Down
2 changes: 1 addition & 1 deletion poller_servcheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@
$t = time() - (86400 * 30);

db_execute_prepared('DELETE FROM plugin_servcheck_log
WHERE lastcheck < FROM_UNIXTIME(?)',
WHERE last_check < FROM_UNIXTIME(?)',
[$t]);
}

Expand Down
67 changes: 48 additions & 19 deletions servcheck_process.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
$enabled = 'AND enabled = "on"';
}

$test = db_fetch_row_prepared('SELECT *, UNIX_TIMESTAMP(DATE_ADD(lastcheck,
$test = db_fetch_row_prepared('SELECT *, UNIX_TIMESTAMP(DATE_ADD(last_check,
INTERVAL (? * how_often) SECOND)) AS next_run
FROM plugin_servcheck_test
WHERE id = ? ' . $enabled,
Expand Down Expand Up @@ -310,11 +310,13 @@
"{$parsed['year']}-{$parsed['month']}-{$parsed['day']} {$parsed['hour']}:{$parsed['minute']}:{$parsed['second']}",
new DateTimeZone('UTC')
);

$local_tz = date_default_timezone_get();

if (empty($local_tz)) {
$local_tz = 'UTC';
}

$dt->setTimezone(new DateTimeZone($local_tz));
$exp = $dt->getTimestamp();
$test['days_left'] = round(($exp - time()) / 86400,1);
Expand Down Expand Up @@ -410,7 +412,7 @@
if ($test['certexpirenotify'] && $results['result'] == 'ok') {
if (isset($last_log['cert_expire']) &&
$last_log['cert_expire'] != '0000-00-00 00:00:00' && !is_null($last_log['cert_expire'])) {
$days_before = round((strtotime($last_log['cert_expire']) - strtotime($last_log['lastcheck'])) / 86400,1);
$days_before = round((strtotime($last_log['cert_expire']) - strtotime($last_log['last_check'])) / 86400,1);

if ($test['days_left'] > 0 && $test['days_left'] > $days_before) {
if (!servcheck_summer_time_changed()) {
Expand All @@ -436,14 +438,14 @@
$test['durs'][] = $results['duration'] . ' (' . date('Y-m-d H:i:s', $results['time']) . ')';

if ($test['duration_count'] > 1) {
$durations = db_fetch_assoc_prepared('SELECT duration, lastcheck, result
$durations = db_fetch_assoc_prepared('SELECT duration, last_check, result
FROM plugin_servcheck_log
WHERE test_id = ?
ORDER BY id DESC LIMIT ' . ($test['duration_count'] - 1),
[$test['id']]);

foreach ($durations as $d) {
$test['durs'][] = $d['duration'] . ' (' . $d['lastcheck'] . ')';
$test['durs'][] = $d['duration'] . ' (' . $d['last_check'] . ')';
}
}

Expand Down Expand Up @@ -475,7 +477,7 @@
} else {
if ($test['notify'] != '') {
servcheck_debug('Time to send email');
plugin_servcheck_send_notification($results, $test, $last_log, $local_tz);
plugin_servcheck_send_notification($results, $test, $last_log);
} else {
servcheck_debug('Time to send email, but email notification for this test is disabled');
}
Expand All @@ -484,7 +486,7 @@
$command = read_config_option('servcheck_change_command');
$command_enable = read_config_option('servcheck_enable_scripts');

if ($command_enable && $command != '') {
if ($command_enable && $command != '' && $test['run_script'] == 'on') {
servcheck_debug('Time to run command');

putenv('SERVCHECK_TEST_NAME=' . $test['name']);
Expand All @@ -510,12 +512,12 @@
servcheck_debug('Nothing triggered');
}

update_statistics($test, $results, $new_notify_expire);
$rusage = getrusage();
update_statistics($test, $results, $new_notify_expire, $rusage);

unregister_process('servcheck', "child:$poller_id", $test_id);

$end = microtime(true);
$rusage = getrusage();
$stats = sprintf('Time:%.2f, Stats:%s/%s, Down triggered:%s, Duration triggered:%s, Memory:%s MB, CPUuser:%.2f CPUsystem:%.2f',
$end - $start,
$test['stats_ok'],
Expand All @@ -528,7 +530,7 @@

servcheck_debug($stats);

function update_statistics(&$test, &$results, $new_notify_expire) {
function update_statistics(&$test, &$results, $new_notify_expire, $rusage) {
servcheck_debug('Updating Statistics');

if ($results['curl']) {
Expand All @@ -538,7 +540,7 @@ function update_statistics(&$test, &$results, $new_notify_expire) {

$curl = 'HTTP code: ' . $results['options']['http_code'] . ', DNS time: ' . round($results['options']['namelookup_time'], 3) . ', ';
$curl .= 'Conn. time: ' . round($results['options']['connect_time'],3) . ', Redir. time: ' . round($results['options']['redirect_time'], 3) . ', ';
$curl .= 'Redir. count: ' . $results['options']['redirect_time'] . ', Download: ' . round($results['options']['size_download'], 3) . ', ';
$curl .= 'Redir. count: ' . $results['options']['redirect_time'] . ', ';
$curl .= 'Speed: ' . $results['options']['speed_download'] . ', CURL code: ' . $results['curl_return'];
} else {
$curl = 'N/A';
Expand All @@ -555,10 +557,16 @@ function update_statistics(&$test, &$results, $new_notify_expire) {
}

db_execute_prepared('INSERT INTO plugin_servcheck_log
(test_id, duration, lastcheck, cert_expire, result, error, result_search, curl_response, attempt)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)',
(test_id, duration, last_check, cert_expire, result, error, result_search,
curl_response, attempt, cpu_user, cpu_system, memory, returned_data_size)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
[$test['id'], $results['duration'], date('Y-m-d H:i:s', $results['time']), $save_exp,
$results['result'], $results['error'], $results['result_search'], $curl, $results['x']]
$results['result'], $results['error'], $results['result_search'], $curl, $results['x'],
$rusage['ru_utime.tv_sec'] + $rusage['ru_utime.tv_usec'] / 1E6,
$rusage['ru_stime.tv_sec'] + $rusage['ru_stime.tv_usec'] / 1E6,
memory_get_peak_usage(true) / 1024 / 1024,
strlen($results['data'])
]
);

if ($new_notify_expire) {
Expand All @@ -571,15 +579,30 @@ function update_statistics(&$test, &$results, $new_notify_expire) {
}

db_execute_prepared('UPDATE plugin_servcheck_test
SET triggered = ?, triggered_duration = ?, failures = ?, lastcheck = ?, last_exp_notify = ?,
stats_ok = ?, stats_bad = ?, last_returned_data = ?
SET triggered = ?, triggered_duration = ?, failures = ?, last_check = ?, last_exp_notify = ?,
stats_ok = ?, stats_bad = ?, last_returned_data = ?, last_duration = ?,
last_result = ?, last_result_search = ?, last_attempt = ?, last_error = ?,
cpu_user = ?, cpu_system = ?,
memory = ?
WHERE id = ?',
[$test['triggered'], $test['triggered_duration'], $test['failures'],
date('Y-m-d H:i:s', $results['time']), $exp_notify,
$test['stats_ok'], $test['stats_bad'],
$results['data'], $test['id']
[$test['triggered'], $test['triggered_duration'], $test['failures'], date('Y-m-d H:i:s', $results['time']), $exp_notify,
$test['stats_ok'], $test['stats_bad'], $results['data'], $results['duration'],
$results['result'], $results['result_search'], $results['x'], $results['error'],
$rusage['ru_utime.tv_sec'] + $rusage['ru_utime.tv_usec'] / 1E6, $rusage['ru_stime.tv_sec'] + $rusage['ru_stime.tv_usec'] / 1E6,
memory_get_peak_usage(true) / 1024 / 1024,
$test['id']
]
);

$retention = read_config_option('servcheck_data_retention');

if ($retention > 0) {
servcheck_debug('Deletion of logs from this test older than ' . $retention . ' days');

db_execute_prepared('DELETE FROM plugin_servcheck_log
WHERE test_id = ? AND last_check < now() - INTERVAL ? day',
[$test['id'], $retention]);
}
}

function plugin_servcheck_send_notification($results, $test, $last_log) {
Expand All @@ -588,6 +611,12 @@ function plugin_servcheck_send_notification($results, $test, $last_log) {
$notify_extra = [];
$notify_account = [];

$local_tz = date_default_timezone_get();

if (empty($local_tz)) {
$local_tz = 'UTC';
}

$servcheck_send_email_separately = read_config_option('servcheck_send_email_separately');

if ($test['notify_accounts'] != '') {
Expand Down
Loading