Skip to content

Commit 8adf436

Browse files
MSS970Copilot
andauthored
Multiple enhancements within servcheck_test and servcheck_process (#67)
* Refactor certificate expiry handling and notifications Updated certificate expiry date handling and notification logic. The previous certificate expiry date logic retrieves the expiry date as UTC not local. Enhanced the code so that to eliminate unnecessary overwhelming email notifications, for every change in the certificate. It is recommended to be disabled so that to focust on the expiring and expired certificates. Enhanced the code so that to eliminate unnecessary overwhelming email notifications, for every change search string. It is recommended to be disabled so that to focus on the notifications that require action. * Add styles for selected table rows in servcheck_test Added styles for selected table rows to change background and font color. // When a row is selected, set the backgound-color as black and font color as white print "<style> tr.tableRow.selectable.selected, tr.tableRow.selectable.selected td { background-color: #000 !important; color: #fff !important; } </style>"; * Change 'ok' color code in arrays.php * Enhance row selection and hover styles in servcheck_test * Fix typo in comment for row styling * Refactor email sending logic in servcheck_process.php * Update servcheck_test.php Co-authored-by: Copilot <[email protected]> * Update servcheck_process.php Co-authored-by: Copilot <[email protected]> * Update servcheck_test.php Co-authored-by: Copilot <[email protected]> * Refactor CSS for selectable rows to use function Replaced inline CSS for selectable rows with a function call. * Fix typo in comment for email notifications * Update servcheck_process.php * Update servcheck_process.php Co-authored-by: Copilot <[email protected]> * Include local timezone in certificate expiration messages Added local timezone information to certificate expiration notifications. --------- Co-authored-by: Copilot <[email protected]>
1 parent 1c8eff2 commit 8adf436

File tree

3 files changed

+66
-40
lines changed

3 files changed

+66
-40
lines changed

includes/arrays.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
'display' => __('Failing', 'servcheck')
6060
),
6161
'ok' => array(
62-
'color' => '#CCFFCC',
62+
'color' => '#E0FFE0',
6363
'display' => __('Ok', 'servcheck')
6464
),
6565
'disabled' => array(

servcheck_process.php

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,17 @@
255255
if (isset($results['options']['certinfo'][0])) { // curl
256256
servcheck_debug('Returned certificate info: ' . clean_up_lines(var_export($results['options']['certinfo'], true)));
257257
$parsed = date_parse_from_format('M j H:i:s Y e', $results['options']['certinfo'][0]['Expire date']);
258-
$exp = mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $parsed['month'], $parsed['day'], $parsed['year']);
258+
// Prepare to retrieve the local expiry date of certificate instead of UTC date
259+
$dt = new DateTime(
260+
"{$parsed['year']}-{$parsed['month']}-{$parsed['day']} {$parsed['hour']}:{$parsed['minute']}:{$parsed['second']}",
261+
new DateTimeZone('UTC')
262+
);
263+
$local_tz = date_default_timezone_get();
264+
if (empty($local_tz)) {
265+
$local_tz = 'UTC';
266+
}
267+
$dt->setTimezone(new DateTimeZone($local_tz));
268+
$exp = $dt->getTimestamp();
259269
$test['days_left'] = round(($exp - time()) / 86400,1);
260270
$test['expiry_date'] = date(date_time_format(), $exp);
261271
} elseif (isset($results['cert_valid_to'])) {
@@ -416,7 +426,7 @@
416426
} else {
417427
if ($test['notify'] != '') {
418428
servcheck_debug('Time to send email');
419-
plugin_servcheck_send_notification($results, $test, $last_log);
429+
plugin_servcheck_send_notification($results, $test, $last_log, $local_tz);
420430
} else {
421431
servcheck_debug('Time to send email, but email notification for this test is disabled');
422432
}
@@ -586,9 +596,9 @@ function plugin_servcheck_send_notification($results, $test, $last_log) {
586596

587597
if ($test['certexpirenotify'] && $results['result'] == 'ok') {
588598
if ($test['days_left'] < 0) {
589-
$message[0]['text'] .= '<tr><td>Certificate expired:</td><td>' . abs($test['days_left']) . ' days ago</td></tr>' . PHP_EOL;
599+
$message[0]['text'] .= '<tr><td>Certificate expired:</td><td>' . abs($test['days_left']) . ' days ago (' . (isset($test['expiry_date']) ? $test['expiry_date'] . ' ' . $local_tz : 'Invalid Expiry Date') . ')</td></tr>' . PHP_EOL;
590600
} else {
591-
$message[0]['text'] .= '<tr><td>Certificate expires in:</td><td>' . $test['days_left'] . ' days (' . (isset($test['expiry_date']) ? $test['expiry_date'] : 'Invalid Expiry Date') . ')</td></tr>' . PHP_EOL;
601+
$message[0]['text'] .= '<tr><td>Certificate expires in:</td><td>' . $test['days_left'] . ' days (' . (isset($test['expiry_date']) ? $test['expiry_date'] . ' ' . $local_tz : 'Invalid Expiry Date') . ')</td></tr>' . PHP_EOL;
592602
}
593603
}
594604

@@ -653,19 +663,13 @@ function plugin_servcheck_send_notification($results, $test, $last_log) {
653663
$message[2]['text'] .= '</table>' . PHP_EOL;
654664
}
655665

656-
657-
if ($test['notify_certificate']) {
658-
659-
if ($test['certificate_state'] == 'ko') {
660-
if ($test['days_left'] < 0) {
661-
$message[3]['subject'] = '[Cacti servcheck - ' . $test['name'] . '] Certificate expired ' . ($test['days_left'] * -1) . ' ago';
662-
} else if ($test['days_left'] < $cert_expiry_days) {
663-
$message[3]['subject'] = '[Cacti servcheck - ' . $test['name'] . '] Certificate will expire in less than ' . $cert_expiry_days . ' days: ' . $test['name'];
664-
}
665-
}
666-
667-
if ($test['certificate_state'] == 'new') {
668-
$message[3]['subject'] = '[Cacti servcheck - ' . $test['name'] . '] Certificate renewed or changed';
666+
// Skip notifications for 'new' certificate states (e.g., renewals/changes) to avoid unnecessary emails.
667+
// Only send notifications for expiring and expired certificates.
668+
if ($test['notify_certificate'] && $test['certificate_state'] != 'new') {
669+
if ($test['days_left'] < 0) {
670+
$message[3]['subject'] = '[Cacti servcheck - ' . $test['name'] . '] Certificate expired ' . abs($test['days_left']) . ((abs($test['days_left']) <= 1) ? ' day ago' : ' days ago');
671+
} else if ($test['days_left'] <= $cert_expiry_days) {
672+
$message[3]['subject'] = '[Cacti servcheck - ' . $test['name'] . '] Certificate will expire in less than ' . $cert_expiry_days . ' days, (days left: ' . $test['days_left'] . ')';
669673
}
670674

671675
$message[3]['text'] = '<h3>' . $message[3]['subject'] . '</h3>' . PHP_EOL;
@@ -683,9 +687,9 @@ function plugin_servcheck_send_notification($results, $test, $last_log) {
683687

684688
if ($test['certexpirenotify']) {
685689
if ($test['days_left'] < 0) {
686-
$message[3]['text'] .= '<tr><td>Certificate expired:</td><td>' . abs($test['days_left']) . ' days ago</td></tr>' . PHP_EOL;
690+
$message[3]['text'] .= '<tr><td>Certificate expired:</td><td>' . abs($test['days_left']) . ' days ago (' . (isset($test['expiry_date']) ? $test['expiry_date'] . ' ' . $local_tz : 'Invalid Expiry Date') . ')</td></tr>' . PHP_EOL;
687691
} else {
688-
$message[3]['text'] .= '<tr><td>Certificate expires in:</td><td>' . $test['days_left'] . ' days (' . (isset($test['expiry_date']) ? $test['expiry_date'] : 'Invalid Expiry Date') . ')</td></tr>' . PHP_EOL;
692+
$message[3]['text'] .= '<tr><td>Certificate expires in:</td><td>' . $test['days_left'] . ' days (' . (isset($test['expiry_date']) ? $test['expiry_date'] . ' ' . $local_tz : 'Invalid Expiry Date') . ')</td></tr>' . PHP_EOL;
689693
}
690694
}
691695

@@ -698,25 +702,27 @@ function plugin_servcheck_send_notification($results, $test, $last_log) {
698702

699703
$to = array_merge($notify_list, $notify_account, $notify_extra);
700704

701-
if ($servcheck_send_email_separately != 'on') {
702-
$addresses = implode(',', $to);
703-
704-
foreach ($message as $m) {
705-
if ($test['notify_format'] == 'plain') {
706-
$m['text'] = strip_tags($m['text']);
707-
}
708-
709-
plugin_servcheck_send_email($addresses, $m['subject'], $m['text']);
710-
}
711-
} else {
712-
713-
foreach ($message as $m) {
714-
if ($test['notify_format'] == 'plain') {
715-
$m['text'] = strip_tags($m['text']);
705+
if (isset($message) && is_array($message)) {
706+
if ($servcheck_send_email_separately != 'on') {
707+
$addresses = implode(',', $to);
708+
709+
foreach ($message as $m) {
710+
if ($test['notify_format'] == 'plain') {
711+
$m['text'] = strip_tags($m['text']);
712+
}
713+
714+
plugin_servcheck_send_email($addresses, $m['subject'], $m['text']);
716715
}
717-
718-
foreach ($to as $u) {
719-
plugin_servcheck_send_email($u, $m['subject'], $m['text']);
716+
} else {
717+
718+
foreach ($message as $m) {
719+
if ($test['notify_format'] == 'plain') {
720+
$m['text'] = strip_tags($m['text']);
721+
}
722+
723+
foreach ($to as $u) {
724+
plugin_servcheck_send_email($u, $m['subject'], $m['text']);
725+
}
720726
}
721727
}
722728
}

servcheck_test.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ function servcheck_show_history() {
934934
}
935935

936936

937-
print "<tr class='tableRow selectable' style='$style' id='line" . $row['id'] . "'>";
937+
print "<tr class='tableRow selectable' style=\"$style\" id='line{$row['id']}'>";
938938

939939
form_selectable_cell($row['lastcheck'], $row['id']);
940940
form_selectable_cell($row['name'], $row['id']);
@@ -1143,7 +1143,7 @@ function data_list() {
11431143
$style = 'background-color: ' . $servcheck_states['error']['color'] . ';';
11441144
}
11451145

1146-
print "<tr class='tableRow selectable' style='$style' id='line" . $row['id'] . "'>";
1146+
print "<tr class='tableRow selectable' style=\"$style\" id='line{$row['id']}'>";
11471147

11481148
print "<td width='1%' style='padding:0px;white-space:nowrap'>
11491149
<a class='pic' href='" . html_escape($config['url_path'] . 'plugins/servcheck/servcheck_test.php?action=edit&id=' . $row['id']) . "' title='" . __esc('Edit Service Check', 'servcheck') . "'>
@@ -1243,6 +1243,9 @@ function servcheck_filter() {
12431243

12441244
set_page_refresh($refresh);
12451245

1246+
// When a row is selected, set the background-color as black and font color as white and when hovering over a row, the background is light grey
1247+
servcheck_print_selectable_row_css();
1248+
12461249
html_start_box(__('Servcheck Test Management', 'servcheck') , '100%', '', '3', 'center', htmlspecialchars(basename($_SERVER['PHP_SELF'])) . '?action=edit');
12471250
?>
12481251

@@ -1351,9 +1354,26 @@ function clearFilter() {
13511354
}
13521355

13531356

1357+
// Shared CSS for selectable table rows
1358+
function servcheck_print_selectable_row_css() {
1359+
print "<style>
1360+
tr.tableRow.selectable:hover, tr.tableRow.selectable:hover td {
1361+
background-color: #B2B2B2 !important;
1362+
color: inherit !important;
1363+
}
1364+
tr.tableRow.selectable.selected, tr.tableRow.selectable.selected td {
1365+
background-color: #000 !important;
1366+
color: #fff !important;
1367+
}
1368+
</style>";
1369+
}
1370+
13541371
function servcheck_log_filter() {
13551372
global $item_rows;
13561373

1374+
// When a row is selected, set the background-color as black and font color as white and when hovering over a row, the background is light grey
1375+
servcheck_print_selectable_row_css();
1376+
13571377
html_start_box(__('Service Check History', 'servcheck') , '100%', '', '3', 'center', '');
13581378

13591379
?>

0 commit comments

Comments
 (0)