Skip to content

Commit 413ec24

Browse files
xmacanTheWitness
andauthored
new stats, log retention, cpu/mem stats (#74)
* new stats, log retention, cpu/mem stats * remove old code * csfixer --------- Co-authored-by: TheWitness <thewitness@cacti.net>
1 parent a541c74 commit 413ec24

8 files changed

Lines changed: 222 additions & 97 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
--- develop ---
44

5+
--- 0.4 ---
6+
7+
* feature: Add cpu/memory statistics
8+
* feature: Better process control
9+
* feature: Add return data size stats
10+
* feature: Add log renention
11+
* feature: Command run only for selected tests
12+
513
--- 0.3 ---
614

715
! IMPORTANT - A lot of changes in 0.3. I tried to convert old data.

INFO

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
[info]
2323
name = servcheck
24-
version = 0.3
24+
version = 0.4
2525
longname = Service Monitor
2626
author = The Cacti Group, Petr Macek
2727
email = developers@cacti.net, petr.macek@kostax.cz

includes/arrays.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
'web_http' => __('HTTP plaintext, default port 80', 'servcheck'),
7373
'web_https' => __('HTTP encrypted (HTTPS), default port 443', 'servcheck'),
7474
'mail_smtp' => __('SMTP plaintext, default port 25 (or 587 for submission)', 'servcheck'),
75-
'mail_smtptls' => __('SMTP with STARTTLS, default port 25(or 587 for submission)', 'servcheck'),
75+
'mail_smtptls' => __('SMTP with STARTTLS, default port 25 (or 587 for submission)', 'servcheck'),
7676
'mail_smtps' => __('SMTP encrypted (SMTPS), default port 465', 'servcheck'),
7777
'mail_imap' => __('IMAP plaintext, default port 143', 'servcheck'),
7878
'mail_imaptls' => __('IMAP with STARTTLS, default port 143', 'servcheck'),
@@ -390,11 +390,18 @@
390390
],
391391
'enabled' => [
392392
'method' => 'checkbox',
393-
'friendly_name' => __('Enable Service Check', 'servcheck'),
393+
'friendly_name' => __('Enable test', 'servcheck'),
394394
'description' => __('Uncheck this box to disable this test from being checked.', 'servcheck'),
395395
'value' => '|arg1:enabled|',
396396
'default' => 'on',
397397
],
398+
'run_script' => [
399+
'method' => 'checkbox',
400+
'friendly_name' => __('Enable run script for this test', 'servcheck'),
401+
'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'),
402+
'value' => '|arg1:run_script|',
403+
'default' => 'on',
404+
],
398405
'poller_id' => [
399406
'friendly_name' => __('Poller', 'servcheck'),
400407
'method' => 'drop_sql',

includes/functions.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ function servcheck_graph($id, $interval) {
7575
global $config, $graph_interval;
7676

7777
$result = db_fetch_assoc_prepared('SELECT
78-
lastcheck, duration FROM plugin_servcheck_log
78+
last_check, duration FROM plugin_servcheck_log
7979
WHERE test_id = ? AND
80-
lastcheck > DATE_SUB(NOW(), INTERVAL ? HOUR)
80+
last_check > DATE_SUB(NOW(), INTERVAL ? HOUR)
8181
ORDER BY id', [$id, $interval]);
8282

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

9191
foreach ($result as $row) {
92-
$lastcheck[] = $row['lastcheck'];
92+
$last_check[] = $row['last_check'];
9393
$duration[] = round($row['duration'], 5);
9494
}
9595

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

117117
// Add the X Axis first
118-
$columns[] = array_merge(['x'], $lastcheck);
118+
$columns[] = array_merge(['x'], $last_check);
119119
$columns[] = array_merge(['Duration'], $duration);
120-
// $columns[] = array_merge(array('Connect'), $connect_time);
121-
// $columns[] = array_merge(array('DNS '), $namelookup_time);
122120

123121
// Setup the Axis
124122
$axis['x'] = [

poller_servcheck.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@
286286
$t = time() - (86400 * 30);
287287

288288
db_execute_prepared('DELETE FROM plugin_servcheck_log
289-
WHERE lastcheck < FROM_UNIXTIME(?)',
289+
WHERE last_check < FROM_UNIXTIME(?)',
290290
[$t]);
291291
}
292292

servcheck_process.php

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
$enabled = 'AND enabled = "on"';
138138
}
139139

140-
$test = db_fetch_row_prepared('SELECT *, UNIX_TIMESTAMP(DATE_ADD(lastcheck,
140+
$test = db_fetch_row_prepared('SELECT *, UNIX_TIMESTAMP(DATE_ADD(last_check,
141141
INTERVAL (? * how_often) SECOND)) AS next_run
142142
FROM plugin_servcheck_test
143143
WHERE id = ? ' . $enabled,
@@ -310,11 +310,13 @@
310310
"{$parsed['year']}-{$parsed['month']}-{$parsed['day']} {$parsed['hour']}:{$parsed['minute']}:{$parsed['second']}",
311311
new DateTimeZone('UTC')
312312
);
313+
313314
$local_tz = date_default_timezone_get();
314315

315316
if (empty($local_tz)) {
316317
$local_tz = 'UTC';
317318
}
319+
318320
$dt->setTimezone(new DateTimeZone($local_tz));
319321
$exp = $dt->getTimestamp();
320322
$test['days_left'] = round(($exp - time()) / 86400,1);
@@ -410,7 +412,7 @@
410412
if ($test['certexpirenotify'] && $results['result'] == 'ok') {
411413
if (isset($last_log['cert_expire']) &&
412414
$last_log['cert_expire'] != '0000-00-00 00:00:00' && !is_null($last_log['cert_expire'])) {
413-
$days_before = round((strtotime($last_log['cert_expire']) - strtotime($last_log['lastcheck'])) / 86400,1);
415+
$days_before = round((strtotime($last_log['cert_expire']) - strtotime($last_log['last_check'])) / 86400,1);
414416

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

438440
if ($test['duration_count'] > 1) {
439-
$durations = db_fetch_assoc_prepared('SELECT duration, lastcheck, result
441+
$durations = db_fetch_assoc_prepared('SELECT duration, last_check, result
440442
FROM plugin_servcheck_log
441443
WHERE test_id = ?
442444
ORDER BY id DESC LIMIT ' . ($test['duration_count'] - 1),
443445
[$test['id']]);
444446

445447
foreach ($durations as $d) {
446-
$test['durs'][] = $d['duration'] . ' (' . $d['lastcheck'] . ')';
448+
$test['durs'][] = $d['duration'] . ' (' . $d['last_check'] . ')';
447449
}
448450
}
449451

@@ -475,7 +477,7 @@
475477
} else {
476478
if ($test['notify'] != '') {
477479
servcheck_debug('Time to send email');
478-
plugin_servcheck_send_notification($results, $test, $last_log, $local_tz);
480+
plugin_servcheck_send_notification($results, $test, $last_log);
479481
} else {
480482
servcheck_debug('Time to send email, but email notification for this test is disabled');
481483
}
@@ -484,7 +486,7 @@
484486
$command = read_config_option('servcheck_change_command');
485487
$command_enable = read_config_option('servcheck_enable_scripts');
486488

487-
if ($command_enable && $command != '') {
489+
if ($command_enable && $command != '' && $test['run_script'] == 'on') {
488490
servcheck_debug('Time to run command');
489491

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

513-
update_statistics($test, $results, $new_notify_expire);
515+
$rusage = getrusage();
516+
update_statistics($test, $results, $new_notify_expire, $rusage);
514517

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

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

529531
servcheck_debug($stats);
530532

531-
function update_statistics(&$test, &$results, $new_notify_expire) {
533+
function update_statistics(&$test, &$results, $new_notify_expire, $rusage) {
532534
servcheck_debug('Updating Statistics');
533535

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

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

557559
db_execute_prepared('INSERT INTO plugin_servcheck_log
558-
(test_id, duration, lastcheck, cert_expire, result, error, result_search, curl_response, attempt)
559-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)',
560+
(test_id, duration, last_check, cert_expire, result, error, result_search,
561+
curl_response, attempt, cpu_user, cpu_system, memory, returned_data_size)
562+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
560563
[$test['id'], $results['duration'], date('Y-m-d H:i:s', $results['time']), $save_exp,
561-
$results['result'], $results['error'], $results['result_search'], $curl, $results['x']]
564+
$results['result'], $results['error'], $results['result_search'], $curl, $results['x'],
565+
$rusage['ru_utime.tv_sec'] + $rusage['ru_utime.tv_usec'] / 1E6,
566+
$rusage['ru_stime.tv_sec'] + $rusage['ru_stime.tv_usec'] / 1E6,
567+
memory_get_peak_usage(true) / 1024 / 1024,
568+
strlen($results['data'])
569+
]
562570
);
563571

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

573581
db_execute_prepared('UPDATE plugin_servcheck_test
574-
SET triggered = ?, triggered_duration = ?, failures = ?, lastcheck = ?, last_exp_notify = ?,
575-
stats_ok = ?, stats_bad = ?, last_returned_data = ?
582+
SET triggered = ?, triggered_duration = ?, failures = ?, last_check = ?, last_exp_notify = ?,
583+
stats_ok = ?, stats_bad = ?, last_returned_data = ?, last_duration = ?,
584+
last_result = ?, last_result_search = ?, last_attempt = ?, last_error = ?,
585+
cpu_user = ?, cpu_system = ?,
586+
memory = ?
576587
WHERE id = ?',
577-
[$test['triggered'], $test['triggered_duration'], $test['failures'],
578-
date('Y-m-d H:i:s', $results['time']), $exp_notify,
579-
$test['stats_ok'], $test['stats_bad'],
580-
$results['data'], $test['id']
588+
[$test['triggered'], $test['triggered_duration'], $test['failures'], date('Y-m-d H:i:s', $results['time']), $exp_notify,
589+
$test['stats_ok'], $test['stats_bad'], $results['data'], $results['duration'],
590+
$results['result'], $results['result_search'], $results['x'], $results['error'],
591+
$rusage['ru_utime.tv_sec'] + $rusage['ru_utime.tv_usec'] / 1E6, $rusage['ru_stime.tv_sec'] + $rusage['ru_stime.tv_usec'] / 1E6,
592+
memory_get_peak_usage(true) / 1024 / 1024,
593+
$test['id']
581594
]
582595
);
596+
597+
$retention = read_config_option('servcheck_data_retention');
598+
599+
if ($retention > 0) {
600+
servcheck_debug('Deletion of logs from this test older than ' . $retention . ' days');
601+
602+
db_execute_prepared('DELETE FROM plugin_servcheck_log
603+
WHERE test_id = ? AND last_check < now() - INTERVAL ? day',
604+
[$test['id'], $retention]);
605+
}
583606
}
584607

585608
function plugin_servcheck_send_notification($results, $test, $last_log) {
@@ -588,6 +611,12 @@ function plugin_servcheck_send_notification($results, $test, $last_log) {
588611
$notify_extra = [];
589612
$notify_account = [];
590613

614+
$local_tz = date_default_timezone_get();
615+
616+
if (empty($local_tz)) {
617+
$local_tz = 'UTC';
618+
}
619+
591620
$servcheck_send_email_separately = read_config_option('servcheck_send_email_separately');
592621

593622
if ($test['notify_accounts'] != '') {

0 commit comments

Comments
 (0)