From e1f2938b44231ce91328a0235b71c77f5d346065 Mon Sep 17 00:00:00 2001 From: xmacan Date: Sat, 7 Feb 2026 21:22:58 +0100 Subject: [PATCH 1/3] new stats, log retention, cpu/mem stats --- CHANGELOG.md | 8 +++ INFO | 2 +- includes/arrays.php | 11 +++- includes/functions.php | 10 ++-- poller_servcheck.php | 2 +- servcheck_process.php | 66 +++++++++++++++++------- servcheck_test.php | 114 ++++++++++++++++++++++++++++------------- setup.php | 98 +++++++++++++++++++++++++++-------- 8 files changed, 227 insertions(+), 84 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f5ce4d6..2123882 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/INFO b/INFO index 26215e9..09523ab 100644 --- a/INFO +++ b/INFO @@ -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 diff --git a/includes/arrays.php b/includes/arrays.php index f61a812..691991c 100644 --- a/includes/arrays.php +++ b/includes/arrays.php @@ -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'), @@ -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', diff --git a/includes/functions.php b/includes/functions.php index 70b9a1d..8dd4f68 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -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) { @@ -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); } @@ -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'] = [ diff --git a/poller_servcheck.php b/poller_servcheck.php index 6e664b7..2d8c2c6 100644 --- a/poller_servcheck.php +++ b/poller_servcheck.php @@ -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]); } diff --git a/servcheck_process.php b/servcheck_process.php index ca049b3..ae07a60 100644 --- a/servcheck_process.php +++ b/servcheck_process.php @@ -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, @@ -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); @@ -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()) { @@ -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'] . ')'; } } @@ -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'); } @@ -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']); @@ -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'], @@ -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']) { @@ -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'; @@ -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) { @@ -571,15 +579,29 @@ 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) { @@ -588,6 +610,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'] != '') { diff --git a/servcheck_test.php b/servcheck_test.php index e588e7f..15691b0 100644 --- a/servcheck_test.php +++ b/servcheck_test.php @@ -154,7 +154,7 @@ function form_actions() { $save['id'] = 0; $save['name'] = 'New Service Check (' . $newid . ')'; - $save['lastcheck'] = '0000-00-00 00:00:00'; + $save['last_check'] = '0000-00-00 00:00:00'; $save['triggered'] = 0; $save['enabled'] = ''; $save['failures'] = 0; @@ -312,6 +312,12 @@ function form_save() { $save['enabled'] = ''; } + if (isset_request_var('run_script')) { + $save['run_script'] = 'on'; + } else { + $save['run_script'] = ''; + } + if (isset_request_var('notify')) { $save['notify'] = 'on'; } else { @@ -793,7 +799,7 @@ function servcheck_log_request_validation() { ], 'sort_column' => [ 'filter' => FILTER_CALLBACK, - 'default' => 'lastcheck', + 'default' => 'last_check', 'options' => ['options' => 'sanitize_search_string'] ], 'sort_direction' => [ @@ -827,11 +833,18 @@ function servcheck_show_history() { $sql_params[] = get_filter_request_var('id'); if (get_request_var('filter') != '') { - $sql_where .= 'AND sl.lastcheck LIKE ?'; + $sql_where .= 'AND sl.last_check LIKE ?'; $sql_params[] = '%' . get_request_var('filter') . '%'; } $sql_order = get_order_string(); + + if ($sql_order == 'ORDER BY `resources` ASC') { + $sql_order = 'ORDER BY `cpu_user` ASC, `cpu_system` ASC, `memory` ASC'; + } elseif ($sql_order == 'ORDER BY `resources` DESC') { + $sql_order = 'ORDER BY `cpu_user` DESC, `cpu_system` DESC, `memory` DESC'; + } + $sql_limit = ' LIMIT ' . ($rows * (get_filter_request_var('page') - 1)) . ',' . $rows; $result = db_fetch_assoc_prepared("SELECT sl.*, st.name @@ -851,7 +864,7 @@ function servcheck_show_history() { $sql_params); $display_text = [ - 'lastcheck' => [ + 'last_check' => [ 'display' => __('Date', 'servcheck') ], 'name' => [ @@ -877,6 +890,14 @@ function servcheck_show_history() { 'curl' => [ 'display' => __('Curl', 'servcheck'), ], + 'returned_data_size' => [ + 'display' => __('Ret. data size', 'servcheck'), + ], + 'resources' => [ + 'display' => __('CPU u/CPU s/Memory', 'servcheck'), + 'sort' => 'ASC', + 'align' => 'right' + ], ]; $columns = cacti_sizeof($display_text); @@ -893,7 +914,7 @@ function servcheck_show_history() { html_start_box('', '100%', '', '3', 'center', ''); - html_header_sort_checkbox($display_text, get_request_var('sort_column'), get_request_var('sort_direction'), false); + html_header_sort($display_text, get_request_var('sort_column'), get_request_var('sort_direction')); if (count($result)) { foreach ($result as $row) { @@ -908,7 +929,7 @@ function servcheck_show_history() { if ($row['cert_expire'] == '0000-00-00 00:00:00' || is_null($row['cert_expire'])) { $days = 'N/A'; } else { - $days = round((strtotime($row['cert_expire']) - strtotime($row['lastcheck'])) / 86400, 1) . ' ' . __('days', 'servcheck'); + $days = round((strtotime($row['cert_expire']) - strtotime($row['last_check'])) / 86400, 1) . ' ' . __('days', 'servcheck'); if ($days <= 0) { $days = __('Expired %s days ago', abs((int)$days), 'servcheck'); @@ -925,7 +946,7 @@ function servcheck_show_history() { print ""; - form_selectable_cell($row['lastcheck'], $row['id']); + form_selectable_cell($row['last_check'], $row['id']); form_selectable_cell($row['name'], $row['id']); form_selectable_cell($row['attempt'], $row['id']); form_selectable_cell($res, $row['id']); @@ -934,6 +955,8 @@ function servcheck_show_history() { form_selectable_cell($row['duration'], $row['id'], '', 'right'); form_selectable_cell($days, $row['id']); form_selectable_cell($row['curl_response'], $row['id']); + form_selectable_cell($row['returned_data_size'] . ' b', $row['id']); + form_selectable_cell($row['cpu_user'] . ' / ' . $row['cpu_system'] . ' / ' . $row['memory'] . 'MB', $row['id'], '', 'right'); form_end_row(); } } @@ -954,6 +977,15 @@ function servcheck_show_graph() { $id = get_filter_request_var('id'); + $count = db_fetch_cell_prepared('SELECT count(id) FROM plugin_servcheck_log + WHERE test_id = ?', + [$id]); + + if ($count < 5) { + print __('Insufficient data, wait a few poller cycles'); + return true; + } + $result = db_fetch_row_prepared('SELECT name FROM plugin_servcheck_test WHERE id = ?', @@ -964,7 +996,7 @@ function servcheck_show_graph() { foreach ($graph_interval as $key => $value) { print '' . ($value) . ':'; servcheck_graph($id, $key); - print '


'; // print line below the graph. + print '


'; } } @@ -1005,13 +1037,20 @@ function data_list() { $sql_order = get_order_string(); - // `statistics` is not a table column, the columns are: + // `statistics` and `resources` are not a table column, the columns are: // `stats_ok` and `stats_bad`, hence, the ORDER BY should be based on these 2 columns if ($sql_order == 'ORDER BY `statistics` ASC') { $sql_order = 'ORDER BY `stats_ok` ASC, `stats_bad` ASC'; } elseif ($sql_order == 'ORDER BY `statistics` DESC') { $sql_order = 'ORDER BY `stats_ok` DESC, `stats_bad` DESC'; } + + if ($sql_order == 'ORDER BY `resources` ASC') { + $sql_order = 'ORDER BY `cpu_user` ASC, `cpu_system` ASC, `memory` ASC'; + } elseif ($sql_order == 'ORDER BY `resources` DESC') { + $sql_order = 'ORDER BY `cpu_user` DESC, `cpu_system` DESC, `memory` DESC'; + } + $sql_limit = ' LIMIT ' . ($rows * (get_request_var('page') - 1)) . ',' . $rows; if (get_request_var('rfilter') != '') { @@ -1044,7 +1083,7 @@ function data_list() { 'sort' => 'ASC', 'align' => 'left' ], - 'lastcheck' => [ + 'last_check' => [ 'display' => __('Last Check (attempt)', 'servcheck'), 'sort' => 'ASC', 'align' => 'right' @@ -1054,7 +1093,7 @@ function data_list() { 'sort' => 'ASC', 'align' => 'right' ], - 'duration' => [ + 'last_duration' => [ 'display' => __('Duration', 'servcheck'), 'sort' => 'ASC', 'align' => 'right' @@ -1064,16 +1103,21 @@ function data_list() { 'sort' => 'ASC', 'align' => 'right' ], - 'result' => [ + 'last_result' => [ 'display' => __('Result', 'servcheck'), 'sort' => 'ASC', 'align' => 'right' ], - 'result_search' => [ + 'last_result_search' => [ 'display' => __('Search result', 'servcheck'), 'sort' => 'ASC', 'align' => 'right' ], + 'resources' => [ + 'display' => __('CPU u/CPU s/Memory', 'servcheck'), + 'sort' => 'ASC', + 'align' => 'right' + ], ]; $columns = cacti_sizeof($display_text); @@ -1093,6 +1137,8 @@ function data_list() { $long_dur = false; $style = ''; +/* +uz se muze odmazat $last_log = db_fetch_row_prepared('SELECT *, (SELECT count(id) FROM plugin_servcheck_log WHERE test_id = ? ) as `count` FROM plugin_servcheck_log @@ -1102,30 +1148,32 @@ function data_list() { if (!$last_log) { $last_log['result'] = 'not yet'; $last_log['result_search'] = 'not yet'; - $last_log['curl_return_code'] = '0'; + $last_log['curl_return_code'] = '0'; // nemusim $last_log['duration'] = '0'; - $last_log['count'] = 0; + $last_log['count'] = 0; // nemusim, vyresim jinak $last_log['attempt'] = 0; } +*/ - if ($last_log['result'] == 'not yet') { + if ($row['last_result'] == 'not yet') { $res = __('Not tested yet', 'servcheck'); - } elseif ($last_log['result'] == 'ok') { + } elseif ($row['last_result'] == 'ok') { $res = 'OK'; } else { - $res = $last_log['result'] . ' (' . $last_log['error'] . ')'; + $res = $row['last_result'] . ' (' . $row['last_error'] . ')'; } + if ($row['enabled'] == '') { $style = 'background-color: ' . $servcheck_states['disabled']['color'] . ';'; - } elseif ($last_log['result'] == 'ok' && $row['triggered_duration'] >= $row['duration_count']) { + } elseif ($row['last_result'] == 'ok' && $row['triggered_duration'] >= $row['duration_count']) { $style = 'background-color: ' . $servcheck_states['duration']['color'] . ';'; $long_dur = true; } elseif ($row['failures'] > 0 && $row['failures'] < $row['downtrigger']) { $style = 'background-color: ' . $servcheck_states['failing']['color'] . ';'; - } elseif ($last_log['result'] == 'ok' && $last_log['result_search'] == 'not ok') { + } elseif ($row['last_result'] == 'ok' && $row['last_result_search'] == 'not ok') { $style = 'background-color: ' . $servcheck_states['warning']['color'] . ';'; - } elseif ($last_log['result'] == 'ok' && strtotime($row['lastcheck']) > 0) { + } elseif ($row['last_result'] == 'ok' && strtotime($row['last_check']) > 0) { $style = 'background-color: ' . $servcheck_states['ok']['color'] . ';'; } else { $style = 'background-color: ' . $servcheck_states['error']['color'] . ';'; @@ -1156,35 +1204,31 @@ function data_list() { "; - if ($last_log['count'] > 4) { - print " - - - "; - } else { - print " - "; - } + print " + + + "; form_selectable_cell($row['name'], $row['id']); - if ($row['lastcheck'] == '0000-00-00 00:00:00') { + if ($row['last_check'] == '0000-00-00 00:00:00') { form_selectable_cell(__('N/A (N/A)', 'servcheck'), $row['id'], '', 'right'); } else { - form_selectable_cell($row['lastcheck'] . ' (' . $last_log['attempt'] . ')', $row['id'], '', 'right'); + form_selectable_cell($row['last_check'] . ' (' . $row['last_attempt'] . ')', $row['id'], '', 'right'); } form_selectable_cell($row['stats_ok'] . ' / ' . $row['stats_bad'], $row['id'], '', 'right'); $tmp = ' (' . $row['failures'] . ' of ' . $row['downtrigger'] . ')'; if ($long_dur) { - form_selectable_cell('' . $last_log['duration'] . '', $row['id'], '', 'right'); + form_selectable_cell('' . $row['last_duration'] . '', $row['id'], '', 'right'); } else { - form_selectable_cell($last_log['duration'], $row['id'], '', 'right'); + form_selectable_cell($row['last_duration'], $row['id'], '', 'right'); } form_selectable_cell($row['triggered'] == '0' ? __('No', 'servcheck') . $tmp : __('Yes', 'servcheck') . $tmp, $row['id'], '', 'right'); form_selectable_cell(substr($res, 0, 30), $row['id'], '', 'right', $res); - form_selectable_cell($text_result_search[$last_log['result_search']], $row['id'], '', 'right'); + form_selectable_cell($text_result_search[$row['last_result_search']], $row['id'], '', 'right'); + form_selectable_cell(round($row['cpu_user'], 2) . ' / ' . round($row['cpu_system'], 2) . ' / ' . $row['memory'] . 'MB', $row['id'], '', 'right'); form_checkbox_cell($row['id'], $row['id']); form_end_row(); diff --git a/setup.php b/setup.php index d58ae71..aa2d90f 100644 --- a/setup.php +++ b/setup.php @@ -78,10 +78,10 @@ function plugin_servcheck_upgrade() { db_add_column('plugin_servcheck_test', array('name' => 'external_id', 'type' => 'varchar(20)', 'NULL' => false, 'default' => '', 'after' => 'notes')); } - // 0.3 contains a lot of changes. I tried to convert old data but for sure make a backup - db_execute('DROP TABLE IF EXISTS plugin_servcheck_contacts'); + // 0.3 contains a lot of changes. I tried to convert old data but for sure make a backup + db_execute('CREATE TABLE plugin_servcheck_test_backup AS SELECT * FROM plugin_servcheck_test'); db_execute('CREATE TABLE plugin_servcheck_log_backup AS SELECT * FROM plugin_servcheck_log'); db_execute('CREATE TABLE plugin_servcheck_proxies_backup AS SELECT * FROM plugin_servcheck_proxies'); @@ -117,7 +117,7 @@ function plugin_servcheck_upgrade() { db_execute('ALTER TABLE plugin_servcheck_log MODIFY curl_return_code int(3) default NULL'); db_execute('ALTER TABLE plugin_servcheck_log MODIFY cert_expire timestamp default "0000-00-00 00:00:00"'); - db_add_column('plugin_servcheck_log', ['name' => 'attempt', 'type' => 'int(2)', 'NULL' => false, 'default' => '0', 'after' => 'lastcheck']); + db_add_column('plugin_servcheck_log', ['name' => 'attempt', 'type' => 'int(2)', 'NULL' => false, 'default' => '0', 'after' => 'enabled']); $exist = db_fetch_cell("SELECT COUNT(*) FROM information_schema.tables @@ -279,16 +279,49 @@ function plugin_servcheck_upgrade() { db_remove_column('plugin_servcheck_proxy', 'username'); db_remove_column('plugin_servcheck_proxy', 'password'); - // Set the new version - db_execute_prepared("UPDATE plugin_config - SET version = ?, author = ?, webpage = ? - WHERE directory = 'servcheck'", - [$info['version'], $info['author'], $info['homepage']] - ); - db_execute('DROP TABLE IF EXISTS plugin_servcheck_processes'); } + if (cacti_version_compare($old, '0.4', '<')) { + + if (db_column_exists('plugin_servcheck_test', 'lastcheck')) { + db_execute('ALTER TABLE plugin_servcheck_test RENAME COLUMN lastcheck TO last_check'); + } + + if (db_column_exists('plugin_servcheck_log', 'lastcheck')) { + db_execute('ALTER TABLE plugin_servcheck_log RENAME COLUMN lastcheck TO last_check'); + } + + if (!db_column_exists('plugin_servcheck_test', 'cpu_user')) { + api_plugin_db_add_column('servcheck', 'plugin_servcheck_test', ['name' => 'cpu_user', 'type' => 'double', 'NULL' => false, 'default' => '0', 'after' => 'triggered_duration']); + api_plugin_db_add_column('servcheck', 'plugin_servcheck_test', ['name' => 'cpu_system', 'type' => 'double', 'NULL' => false, 'default' => '0', 'after' => 'cpu_user']); + api_plugin_db_add_column('servcheck', 'plugin_servcheck_test', ['name' => 'memory', 'type' => 'int(11)', 'NULL' => false, 'default' => '0', 'after' => 'cpu_system']); + api_plugin_db_add_column('servcheck', 'plugin_servcheck_test', ['name' => 'last_result', 'type' => 'varchar(20)', 'NULL' => false, 'default' => 'not yet', 'after' => 'last_exp_notify']); + api_plugin_db_add_column('servcheck', 'plugin_servcheck_test', ['name' => 'last_result_search', 'type' => 'varchar(20)', 'NULL' => false, 'default' => 'not yet', 'after' => 'last_result']); + api_plugin_db_add_column('servcheck', 'plugin_servcheck_test', ['name' => 'last_attempt', 'type' => 'int(2)', 'NULL' => false, 'default' => '0', 'after' => 'last_result_search']); + api_plugin_db_add_column('servcheck', 'plugin_servcheck_test', ['name' => 'last_error', 'type' => 'varchar(256)', 'NULL' => true, 'default' => 'NULL', 'after' => 'last_attempt']); + api_plugin_db_add_column('servcheck', 'plugin_servcheck_test', ['name' => 'run_script', 'type' => 'varchar(2)', 'NULL' => false, 'default' => 'on', 'after' => 'attempt']); + } + + if (!db_column_exists('plugin_servcheck_test', 'last_duration')) { + api_plugin_db_add_column('servcheck', 'plugin_servcheck_test', ['name' => 'last_duration', 'type' => 'float', 'NULL' => false, 'default' => '0', 'after' => 'memory']); + } + + if (!db_column_exists('plugin_servcheck_log', 'cpu_user')) { + api_plugin_db_add_column('servcheck', 'plugin_servcheck_log', ['name' => 'cpu_user', 'type' => 'double', 'NULL' => false, 'default' => '0', 'after' => 'duration']); + api_plugin_db_add_column('servcheck', 'plugin_servcheck_log', ['name' => 'cpu_system', 'type' => 'double', 'NULL' => false, 'default' => '0', 'after' => 'cpu_user']); + api_plugin_db_add_column('servcheck', 'plugin_servcheck_log', ['name' => 'memory', 'type' => 'int(11)', 'NULL' => false, 'default' => '0', 'after' => 'cpu_system']); + api_plugin_db_add_column('servcheck', 'plugin_servcheck_log', ['name' => 'returned_data_size', 'type' => 'int(11)', 'NULL' => false, 'default' => '0', 'after' => 'memory']); + } + } + + // Set the new version + db_execute_prepared("UPDATE plugin_config + SET version = ?, author = ?, webpage = ? + WHERE directory = 'servcheck'", + [$info['version'], $info['author'], $info['homepage']] + ); + return true; } @@ -304,10 +337,11 @@ function plugin_servcheck_setup_table() { $data['columns'][] = ['name' => 'id', 'type' => 'int(11)', 'NULL' => false, 'auto_increment' => true]; $data['columns'][] = ['name' => 'type', 'type' => 'varchar(30)', 'NULL' => false, 'default' => 'web_http']; $data['columns'][] = ['name' => 'notify', 'type' => 'char(2)', 'NULL' => false, 'default' => 'on']; - $data['columns'][] = ['name' => 'attempt', 'type' => 'int(2)', 'NULL' => false, 'default' => '3']; $data['columns'][] = ['name' => 'name', 'type' => 'varchar(64)', 'NULL' => false, 'default' => '']; $data['columns'][] = ['name' => 'poller_id', 'type' => 'int(11)', 'NULL' => false, 'unsigned' => true, 'default' => '1']; $data['columns'][] = ['name' => 'enabled', 'type' => 'varchar(2)', 'NULL' => false, 'default' => 'on']; + $data['columns'][] = ['name' => 'attempt', 'type' => 'int(2)', 'NULL' => false, 'default' => '3']; + $data['columns'][] = ['name' => 'run_script', 'type' => 'varchar(2)', 'NULL' => false, 'default' => 'on']; $data['columns'][] = ['name' => 'hostname', 'type' => 'varchar(120)', 'NULL' => false, 'default' => '']; $data['columns'][] = ['name' => 'ipaddress', 'type' => 'varchar(46)', 'NULL' => false, 'default' => '']; $data['columns'][] = ['name' => 'path', 'type' => 'varchar(256)', 'NULL' => false]; @@ -321,6 +355,7 @@ function plugin_servcheck_setup_table() { $data['columns'][] = ['name' => 'requiresauth', 'type' => 'varchar(2)', 'NULL' => false, 'default' => '']; $data['columns'][] = ['name' => 'proxy_id', 'type' => 'int(11)', 'NULL' => false, 'unsigned' => true, 'default' => '0']; $data['columns'][] = ['name' => 'ca_id', 'type' => 'int(11)', 'NULL' => false, 'unsigned' => true, 'default' => '0']; + $data['columns'][] = ['name' => 'cred_id', 'type' => 'int(11)', 'NULL' => false, 'unsigned' => true, 'default' => '0']; $data['columns'][] = ['name' => 'checkcert', 'type' => 'char(2)', 'NULL' => false, 'default' => 'on']; $data['columns'][] = ['name' => 'certexpirenotify', 'type' => 'char(2)', 'NULL' => false, 'default' => 'on']; $data['columns'][] = ['name' => 'notify_list', 'type' => 'int(10)', 'NULL' => false, 'unsigned' => true, 'default' => '0']; @@ -338,13 +373,19 @@ function plugin_servcheck_setup_table() { $data['columns'][] = ['name' => 'failures', 'type' => 'int(11)', 'NULL' => false, 'unsigned' => true, 'default' => '0']; $data['columns'][] = ['name' => 'triggered', 'type' => 'int(11)', 'NULL' => false, 'unsigned' => true, 'default' => '0']; $data['columns'][] = ['name' => 'triggered_duration', 'type' => 'int(11)', 'NULL' => false, 'unsigned' => true, 'default' => '0']; - $data['columns'][] = ['name' => 'lastcheck', 'type' => 'timestamp', 'NULL' => false, 'default' => '0000-00-00 00:00:00']; + $data['columns'][] = ['name' => 'cpu_user', 'type' => 'double', 'NULL' => false, 'default' => '0']; + $data['columns'][] = ['name' => 'cpu_system', 'type' => 'double', 'NULL' => false, 'default' => '0']; + $data['columns'][] = ['name' => 'memory', 'type' => 'int(11)', 'NULL' => false, 'default' => '0']; + $data['columns'][] = ['name' => 'last_check', 'type' => 'timestamp', 'NULL' => false, 'default' => '0000-00-00 00:00:00']; $data['columns'][] = ['name' => 'last_exp_notify', 'type' => 'timestamp', 'NULL' => false, 'default' => '0000-00-00 00:00:00']; + $data['columns'][] = ['name' => 'last_result', 'type' => 'varchar(20)', 'NULL' => false, 'default' => 'not yet']; + $data['columns'][] = ['name' => 'last_result_search', 'type' => 'varchar(20)', 'NULL' => false, 'default' => 'not yet']; + $data['columns'][] = ['name' => 'last_attempt', 'type' => 'int(2)', 'NULL' => false, 'default' => '0']; + $data['columns'][] = ['name' => 'last_error', 'type' => 'varchar(256)', 'NULL' => true, 'default' => 'NULL']; $data['columns'][] = ['name' => 'last_returned_data', 'type' => 'blob', 'NULL' => true, 'default' => '']; - $data['columns'][] = ['name' => 'cred_id', 'type' => 'int(11)', 'NULL' => false, 'unsigned' => true, 'default' => '0']; - + $data['columns'][] = ['name' => 'last_duration', 'type' => 'float', 'NULL' => false, 'default' => '0']; $data['primary'] = 'id'; - $data['keys'][] = ['name' => 'lastcheck', 'columns' => 'lastcheck']; + $data['keys'][] = ['name' => 'last_check', 'columns' => 'last_check']; $data['keys'][] = ['name' => 'triggered', 'columns' => 'triggered']; $data['keys'][] = ['name' => 'enabled', 'columns' => 'enabled']; $data['type'] = 'InnoDB'; @@ -355,7 +396,6 @@ function plugin_servcheck_setup_table() { $data = []; $data['columns'][] = ['name' => 'id', 'type' => 'int(11)', 'NULL' => false, 'auto_increment' => true]; $data['columns'][] = ['name' => 'test_id', 'type' => 'int(11)', 'NULL' => false, 'unsigned' => true, 'default' => '0']; - $data['columns'][] = ['name' => 'lastcheck', 'type' => 'timestamp', 'NULL' => false, 'default' => '0000-00-00 00:00:00']; $data['columns'][] = ['name' => 'attempt', 'type' => 'int(2)', 'NULL' => false, 'default' => '0']; $data['columns'][] = ['name' => 'result', 'type' => "enum('ok','not yet','error')", 'NULL' => false, 'default' => 'not yet']; $data['columns'][] = ['name' => 'result_search', 'type' => "enum('ok','not ok','failed ok','failed not ok', 'maint ok','not yet', 'not tested')", 'NULL' => false, 'default' => 'not yet']; @@ -363,10 +403,14 @@ function plugin_servcheck_setup_table() { $data['columns'][] = ['name' => 'cert_expire', 'type' => 'timestamp', 'NULL' => false, 'default' => '0000-00-00 00:00:00']; $data['columns'][] = ['name' => 'error', 'type' => 'varchar(256)', 'NULL' => true, 'default' => 'NULL']; $data['columns'][] = ['name' => 'duration', 'type' => 'float', 'NULL' => false, 'default' => 0]; - + $data['columns'][] = ['name' => 'cpu_user', 'type' => 'double', 'NULL' => false, 'default' => '0']; + $data['columns'][] = ['name' => 'cpu_system', 'type' => 'double', 'NULL' => false, 'default' => '0']; + $data['columns'][] = ['name' => 'memory', 'type' => 'int(11)', 'NULL' => false, 'default' => '0']; + $data['columns'][] = ['name' => 'returned_data_size', 'type' => 'int(11)', 'NULL' => false, 'default' => '0']; + $data['columns'][] = ['name' => 'last_check', 'type' => 'timestamp', 'NULL' => false, 'default' => '0000-00-00 00:00:00']; $data['primary'] = 'id'; $data['keys'][] = ['name' => 'test_id', 'columns' => 'test_id']; - $data['keys'][] = ['name' => 'lastcheck', 'columns' => 'lastcheck']; + $data['keys'][] = ['name' => 'last_check', 'columns' => 'last_check']; $data['keys'][] = ['name' => 'result', 'columns' => 'result']; $data['type'] = 'InnoDB'; $data['comment'] = 'Holds servcheck Service Check Logs'; @@ -595,7 +639,7 @@ function servcheck_config_settings() { ], 'servcheck_enable_scripts' => [ 'friendly_name' => __('Enable Command Execution', 'servcheck'), - 'description' => __('Checking this box will enable the ability to run commands on Servcheck events.', 'servcheck'), + 'description' => __('Checking this box will enable the ability to run commands on Servcheck events. You can enable or disable it for each test.', 'servcheck'), 'method' => 'checkbox', 'default' => '' ], @@ -644,6 +688,20 @@ function servcheck_config_settings() { '60' => __('60 days in advance', 'servcheck'), '90' => __('90 days in advance', 'servcheck'), ] - ] + ], + 'servcheck_data_retention' => [ + 'friendly_name' => __('How long to keep logs', 'servcheck'), + 'description' => __('Enter the period after which older logs will be automatically deleted.', 'servcheck'), + 'method' => 'drop_array', + 'default' => 8, + 'array' => [ + '0' => __('Never', 'servcheck'), + '1' => __('%d hours', 24, 'servcheck'), + '7' => __('%d days', 7, 'servcheck'), + '90' => __('%d days', 90, 'servcheck'), + '180' => __('%d days', 180, 'servcheck'), + '365' => __('%d year', 1, 'servcheck'), + ] + ], ]; } From 4e2c1689f28f45106e5fe468f346693b928cfcb5 Mon Sep 17 00:00:00 2001 From: xmacan Date: Sat, 7 Feb 2026 21:49:09 +0100 Subject: [PATCH 2/3] remove old code --- servcheck_test.php | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/servcheck_test.php b/servcheck_test.php index 15691b0..d38af75 100644 --- a/servcheck_test.php +++ b/servcheck_test.php @@ -1137,24 +1137,6 @@ function data_list() { $long_dur = false; $style = ''; -/* -uz se muze odmazat - $last_log = db_fetch_row_prepared('SELECT *, - (SELECT count(id) FROM plugin_servcheck_log WHERE test_id = ? ) as `count` - FROM plugin_servcheck_log - WHERE test_id = ? ORDER BY id DESC LIMIT 1', - [$row['id'], $row['id']]); - - if (!$last_log) { - $last_log['result'] = 'not yet'; - $last_log['result_search'] = 'not yet'; - $last_log['curl_return_code'] = '0'; // nemusim - $last_log['duration'] = '0'; - $last_log['count'] = 0; // nemusim, vyresim jinak - $last_log['attempt'] = 0; - } -*/ - if ($row['last_result'] == 'not yet') { $res = __('Not tested yet', 'servcheck'); } elseif ($row['last_result'] == 'ok') { From 18389faec7cbaaa0113e279bab2f9b5f2033af34 Mon Sep 17 00:00:00 2001 From: xmacan Date: Sun, 8 Feb 2026 09:31:08 +0100 Subject: [PATCH 3/3] csfixer --- servcheck_process.php | 1 + servcheck_test.php | 2 +- setup.php | 3 +-- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/servcheck_process.php b/servcheck_process.php index ae07a60..33e9217 100644 --- a/servcheck_process.php +++ b/servcheck_process.php @@ -595,6 +595,7 @@ function update_statistics(&$test, &$results, $new_notify_expire, $rusage) { ); $retention = read_config_option('servcheck_data_retention'); + if ($retention > 0) { servcheck_debug('Deletion of logs from this test older than ' . $retention . ' days'); diff --git a/servcheck_test.php b/servcheck_test.php index d38af75..19355b1 100644 --- a/servcheck_test.php +++ b/servcheck_test.php @@ -983,6 +983,7 @@ function servcheck_show_graph() { if ($count < 5) { print __('Insufficient data, wait a few poller cycles'); + return true; } @@ -1145,7 +1146,6 @@ function data_list() { $res = $row['last_result'] . ' (' . $row['last_error'] . ')'; } - if ($row['enabled'] == '') { $style = 'background-color: ' . $servcheck_states['disabled']['color'] . ';'; } elseif ($row['last_result'] == 'ok' && $row['triggered_duration'] >= $row['duration_count']) { diff --git a/setup.php b/setup.php index aa2d90f..c17960a 100644 --- a/setup.php +++ b/setup.php @@ -75,7 +75,7 @@ function plugin_servcheck_upgrade() { } if (!db_column_exists('plugin_servcheck_test', 'external_id')) { - db_add_column('plugin_servcheck_test', array('name' => 'external_id', 'type' => 'varchar(20)', 'NULL' => false, 'default' => '', 'after' => 'notes')); + db_add_column('plugin_servcheck_test', ['name' => 'external_id', 'type' => 'varchar(20)', 'NULL' => false, 'default' => '', 'after' => 'notes']); } db_execute('DROP TABLE IF EXISTS plugin_servcheck_contacts'); @@ -283,7 +283,6 @@ function plugin_servcheck_upgrade() { } if (cacti_version_compare($old, '0.4', '<')) { - if (db_column_exists('plugin_servcheck_test', 'lastcheck')) { db_execute('ALTER TABLE plugin_servcheck_test RENAME COLUMN lastcheck TO last_check'); }