Skip to content

Commit 156815d

Browse files
authored
Fix: certificate check does not function as expected (#36)
* Update poller_servcheck.php /* let PHP run just as long as it has to */ // modified by MSS on 2025-03-08 //ini_set('max_execution_time', '120'); * Update servcheck_process.php Fix a bug in the certificate check * Update servcheck_test.php Enhancement on the graph page * Update setup.php Add 30 days to check the certificate validity * Update functions.php Finetune the graph display. * Update tests.php Fix bugs related to CA certificate Fix typos * Update CHANGELOG.md Fix certificate check does not function as expected #35 * Update servcheck_process.php Revise the date format. * Update servcheck_process.php Finetuning the date format * Update CHANGELOG.md Fix certificate check does not function as expected #36 * Update CHANGELOG.md * issue#36: Fix certificate check does not function as expected * Update functions.php * issue#36: Fix certificate check does not function as expected * Update tests.php * issue#36: Fix certificate check does not function as expected * Update servcheck_process.php removing undesired commented lines. * Update poller_servcheck.php /* let PHP run just as long as it has to */ ini_set('max_execution_time', '270'); and removing sleep(2) after exec background * Update CHANGELOG.md issue#35: Fix certificate check does not function as expected * Update servcheck_process.php (isset($test['expiry_date']) ? $test['expiry_date'] : 'Invalid Expiry Date')
1 parent 746fc1c commit 156815d

7 files changed

+62
-38
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
--- develop ---
44

5+
* issue#35: Fix certificate check does not function as expected
56
* issue#2: Fix graph legend overlap
67
* issue#31: Fix poller_servcheck.php does not run any test when it runs automatically from cacti poller
78
* issue: Unable to save poller for service check

Diff for: includes/functions.php

+19
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,25 @@ function plugin_servcheck_graph ($id, $interval) {
212212

213213
$chart_data = json_encode($chart);
214214

215+
// The below block can be used as part of the debuggin to display the data retrieved along with the graph.
216+
// Uncomment/Comment as required.
217+
/*
218+
print '<style>
219+
.json-output {
220+
max-width: 800px;
221+
overflow-wrap: break-word;
222+
word-wrap: break-word;
223+
white-space: pre-wrap;
224+
border: 1px solid #ccc;
225+
padding: 10px;
226+
background-color: #f9f9f9;
227+
}
228+
</style>';
229+
print '<pre class="json-output">';
230+
var_dump($chart_data);
231+
print '</pre>';
232+
*/
233+
215234
$content = '<div id="chart_' . $xid. '"></div>';
216235
$content .= '<div id="legend_container_' . $xid. '" style="display: flex; flex-direction: column; align-items: flex-start;">';
217236
$content .= '<div id="legend_' . $xid. '" style="margin-top: 10px;"></div>';

Diff for: includes/tests.php

+11-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
+-------------------------------------------------------------------------+
2323
*/
2424

25-
$user_agent = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1';
25+
// $user_agent can be of user's choice e.g. Linux or Windows based
26+
$user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36';
27+
2628
$ca_info = $config['base_path'] . '/plugins/servcheck/ca-bundle.crt';
2729

2830
function curl_try ($test) {
@@ -112,8 +114,10 @@ function curl_try ($test) {
112114
$process = curl_init($url);
113115

114116
if ($test['ca'] > 0) {
115-
$ca_info = '/tmp/cert' . $test['ca'] . '.pem';
117+
$ca_info = $config['base_path'] . '/plugins/servcheck/ca_cert_' . $test['ca'] . '.pem'; // The folder /plugins/servcheck does exist, hence the ca_cert_x.pem can be created here
116118
plugin_servcheck_debug('Preparing own CA chain file ' . $ca_info , $test);
119+
// CURLOPT_CAINFO is to updated based on the custom CA certificate
120+
$options[CURLOPT_CAINFO] = $ca_info;
117121

118122
$cert = db_fetch_cell_prepared('SELECT cert FROM plugin_servcheck_ca WHERE id = ?',
119123
array($test['ca']));
@@ -187,7 +191,6 @@ function curl_try ($test) {
187191
$options[CURLOPT_CERTINFO] = true;
188192
}
189193

190-
191194
plugin_servcheck_debug('cURL options: ' . clean_up_lines(var_export($options, true)));
192195

193196
curl_setopt_array($process,$options);
@@ -370,7 +373,7 @@ function dns_try ($test) {
370373
/*
371374
there are 2 problems:
372375
- when to terminate the connection - curl can't easily set "disconnect on first received message".
373-
I callcall back and if any data is returned, the connection is terminated (42).
376+
I call back and if any data is returned, the connection is terminated (42).
374377
If no data is returned, the test timeouts (28)
375378
- the data is not returned the same way as with other services, I have to capture it in a file
376379
*/
@@ -552,9 +555,11 @@ function doh_try ($test) {
552555

553556
$process = curl_init($url);
554557

555-
if ($test['ca'] > 0) {
556-
$ca_info = '/tmp/cert' . $test['ca'] . '.pem';
558+
if ($test['ca'] > 0) {
559+
$ca_info = $config['base_path'] . '/plugins/servcheck/cert_' . $test['ca'] . '.pem'; // The folder /plugins/servcheck does exist, hence the ca_cert_x.pem can be created here
557560
plugin_servcheck_debug('Preparing own CA chain file ' . $ca_info , $test);
561+
// CURLOPT_CAINFO is to updated based on the custom CA certificate
562+
$options[CURLOPT_CAINFO] = $ca_info;
558563

559564
$cert = db_fetch_cell_prepared('SELECT cert FROM plugin_servcheck_ca WHERE id = ?',
560565
array($test['ca']));

Diff for: poller_servcheck.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424

2525
/* let PHP run just as long as it has to */
26-
ini_set('max_execution_time', '120');
26+
ini_set('max_execution_time', '270');
2727

2828
$dir = dirname(__FILE__);
2929
chdir($dir);
@@ -125,7 +125,7 @@
125125
$sql_condition,
126126
array($poller_id));
127127

128-
$max_processes = 12;
128+
$max_processes = 32;
129129

130130
if (cacti_sizeof($tests)) {
131131
foreach($tests as $test) {
@@ -140,10 +140,8 @@
140140
$command_string = read_config_option('path_php_binary');
141141
$extra_args = '-q "' . $config['base_path'] . '/plugins/servcheck/servcheck_process.php" --id=' . $test['id'] . ($debug ? ' --debug':'');
142142
exec_background($command_string, $extra_args);
143-
144-
sleep(2);
145143
} else {
146-
sleep(2);
144+
sleep(1);
147145

148146
db_execute_prepared('DELETE FROM plugin_servcheck_processes
149147
WHERE time < FROM_UNIXTIME(?)

Diff for: servcheck_process.php

+21-22
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,9 @@
207207
$parsed = date_parse_from_format('M j H:i:s Y e', $results['options']['certinfo'][0]['Expire date']);
208208
$exp = mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $parsed['month'], $parsed['day'], $parsed['year']);
209209
$test['days'] = round(($exp - time()) / 86400);
210-
$test['expiry_date'] = $parsed['day'] . '. ' . $parsed['month'] . '. ' . $parsed['year'];
210+
$test['expiry_date'] = str_pad($parsed['day'], 2, '0', STR_PAD_LEFT) . '-' .
211+
date('M', mktime(0, 0, 0, $parsed['month'], 10)) . '-' .
212+
$parsed['year'];
211213
}
212214
}
213215

@@ -230,7 +232,7 @@
230232
}
231233

232234
if ($last_log['result'] != $results['result'] || $last_log['result_search'] != $results['result_search'] ||
233-
($test['certexpirenotify'] && $test_expiry_days > 0 && $test['days'] < $cert_expiry_days)) {
235+
($test['certexpirenotify'] && $cert_expiry_days > 0 && $test['days'] < $cert_expiry_days)) {
234236

235237
plugin_servcheck_debug('Checking for trigger', $test);
236238

@@ -270,7 +272,7 @@
270272
array($test['id']));
271273

272274
if ($new_notify < time()) {
273-
plugin_servcheck_debug('Certificate expired soon, will notify about expiration', $test);
275+
plugin_servcheck_debug('Certificate will expire soon, will notify about expiration', $test);
274276

275277
$sendemail = true;
276278
$new_notify_expire = true;
@@ -456,7 +458,7 @@ function plugin_servcheck_send_notification($results, $test, $type, $last_log) {
456458
$message[0]['text'] .= 'Total Time: ' . $results['options']['total_time'] . PHP_EOL;
457459

458460
if ($test['certexpirenotify']) {
459-
$message[0]['text'] .= 'Certificate expire in ' . $test['days'] . ' days (' . $test['expiry_date'] . ')' . PHP_EOL;
461+
$message[0]['text'] .= 'Certificate expires in ' . $test['days'] . ' days (' . (isset($test['expiry_date']) ? $test['expiry_date'] : 'Invalid Expiry Date') . ')' . PHP_EOL;
460462
}
461463
if ($test['notes'] != '') {
462464
$message[0]['text'] .= PHP_EOL . 'Notes: ' . $test['notes'] . PHP_EOL;
@@ -465,18 +467,18 @@ function plugin_servcheck_send_notification($results, $test, $type, $last_log) {
465467

466468
// search string notification
467469
if ($last_log['result_search'] != $results['result_search']) {
468-
$message[1]['subject'] = '[Cacti servcheck] Service ' . $test['display_name'] . ' - changed search string';
470+
$message[1]['subject'] = '[Cacti servcheck] Service: ' . $test['display_name'] . ' search result is different than last check';
469471

470472
$message[1]['text'] = 'Hostname: ' . $test['hostname'] . PHP_EOL;
471473

472474
if (!is_null($test['path'])) {
473475
$message[1]['text'] .= 'Path: ' . $test['path'] . PHP_EOL;
474476
}
475477

476-
$message[1]['text'] .= 'Date: ' . date('F j, Y - h:i:s', $results['time']) . PHP_EOL;
478+
$message[1]['text'] .= 'Date: ' . date('d-b-Y h:i:s p', $results['time']) . PHP_EOL;
477479

478480
if ($test['certexpirenotify']) {
479-
$message[1]['text'] .= 'Certificate expire in ' . $test['days'] . ' days (' . $test['expiry_date'] . ')' . PHP_EOL;
481+
$message[1]['text'] .= 'Certificate expires in ' . $test['days'] . ' days (' . (isset($test['expiry_date']) ? $test['expiry_date'] : 'Invalid Expiry Date') . ')' . PHP_EOL;
480482
}
481483

482484
if (!is_null($test['http_code'])) {
@@ -493,18 +495,16 @@ function plugin_servcheck_send_notification($results, $test, $type, $last_log) {
493495
}
494496

495497
if ($test['certexpirenotify'] && $cert_expiry_days > 0 && $test['days'] < $cert_expiry_days) {
496-
$message[2]['subject'] = '[Cacti servcheck] Certificate will expired in less than ' . $cert_expiry_days . ' days: ' . $test['display_name'];
498+
$message[2]['subject'] = '[Cacti servcheck] Certificate will expire in less than ' . $cert_expiry_days . ' days: ' . $test['display_name'];
497499
$message[2]['text'] = 'Site ' . $test['display_name'] . PHP_EOL;
498500

499501
$message[2]['text'] .= 'Hostname: ' . $test['hostname'] . PHP_EOL;
500502

501503
if (!is_null($test['path'])) {
502504
$message[2]['text'] .= 'Path: ' . $test['path'] . PHP_EOL;
503505
}
504-
505-
$message[2]['text'] .= 'Certificate expiration date:' . $test['expiry_date'] . PHP_EOL;
506-
507-
$message[2]['text'] .= 'Date: ' . date('F j, Y - h:i:s', $results['time']) . PHP_EOL;
506+
$message[2]['text'] .= 'Certificate expiry date:' . (isset($test['expiry_date']) ? $test['expiry_date'] : 'Invalid Expiry Date') . PHP_EOL;
507+
$message[2]['text'] .= 'Date: ' . date('d-b-Y h:i:s p', $results['time']) . PHP_EOL;
508508

509509
if ($test['notes'] != '') {
510510
$message[2]['text'] .= PHP_EOL . 'Notes: ' . $test['notes'] . PHP_EOL;
@@ -530,10 +530,10 @@ function plugin_servcheck_send_notification($results, $test, $type, $last_log) {
530530
}
531531

532532
$message[0]['text'] .= '<tr><td>Status:</td><td>' . ($results['result'] == 'ok' ? 'Recovering' : 'Down') . '</td></tr>' . PHP_EOL;
533-
$message[0]['text'] .= '<tr><td>Date:</td><td>' . date('F j, Y - h:i:s', $results['time']) . '</td></tr>' . PHP_EOL;
533+
$message[0]['text'] .= '<tr><td>Date:</td><td>' . date('d-b-Y h:i:s p', $results['time']) . '</td></tr>' . PHP_EOL;
534534

535535
if ($test['certexpirenotify']) {
536-
$message[0]['text'] .= '<tr><td>Certificate expire in: </td><td> ' . $test['days'] . ' days (' . $test['expiry_date'] . ')' . '</td></tr>' . PHP_EOL;
536+
$message[0]['text'] .= '<tr><td>Certificate expires in: </td><td> ' . $test['days'] . ' days (' . (isset($test['expiry_date']) ? $test['expiry_date'] : 'Invalid Expiry Date') . ')' . '</td></tr>' . PHP_EOL;
537537
}
538538

539539
if (isset($results['options']['http_code'])) {
@@ -567,7 +567,7 @@ function plugin_servcheck_send_notification($results, $test, $type, $last_log) {
567567

568568
// search string notification
569569
if ($last_log['result_search'] != $results['result_search']) {
570-
$message[1]['subject'] = '[Cacti servcheck] Service ' . $test['display_name'] . ' - changed search string';
570+
$message[1]['subject'] = '[Cacti servcheck] Service ' . $test['display_name'] . ' search result is different than last check';
571571

572572
$message[1]['text'] = '<h3>' . $message[1]['subject'] . '</h3>' . PHP_EOL;
573573
$message[1]['text'] .= '<hr>';
@@ -579,11 +579,10 @@ function plugin_servcheck_send_notification($results, $test, $type, $last_log) {
579579
if (!is_null($test['path'])) {
580580
$message[1]['text'] .= '<tr><td>Path:</td><td>' . $test['path'] . '</td></tr>' . PHP_EOL;
581581
}
582-
583-
$message[1]['text'] .= '<tr><td>Date:</td><td>' . date('F j, Y - h:i:s', $results['time']) . '</td></tr>' . PHP_EOL;
582+
$message[1]['text'] .= '<tr><td>Date:</td><td>' . date('d-b-Y h:i:s p', $results['time']) . '</td></tr>' . PHP_EOL;
584583

585584
if ($test['certexpirenotify']) {
586-
$message[1]['text'] .= '<tr><td>Certificate expire in: </td><td> ' . $test['days'] . ' days' . '</td></tr>' . PHP_EOL;
585+
$message[1]['text'] .= '<tr><td>Certificate expires in: </td><td> ' . $test['days'] . ' days (' . (isset($test['expiry_date']) ? $test['expiry_date'] : 'Invalid Expiry Date') . ')' . '</td></tr>' . PHP_EOL;
587586
}
588587

589588
$message[1]['text'] .= '<tr><td>Previous search:</td><td>' . $last_log['result_search'] . '</td></tr>' . PHP_EOL;
@@ -595,8 +594,7 @@ function plugin_servcheck_send_notification($results, $test, $type, $last_log) {
595594
}
596595

597596
if ($test['certexpirenotify'] && $cert_expiry_days > 0 && $test['days'] < $cert_expiry_days) {
598-
599-
$message[2]['subject'] = '[Cacti servcheck] Certificate will expired in less than ' . $cert_expiry_days . ' days: ' . $test['display_name'];
597+
$message[2]['subject'] = '[Cacti servcheck] Certificate will expire in less than ' . $cert_expiry_days . ' days: ' . $test['display_name'];
600598
$message[2]['text'] = '<h3>' . $message[2]['subject'] . '</h3>' . PHP_EOL;
601599
$message[2]['text'] .= '<hr>';
602600

@@ -607,9 +605,10 @@ function plugin_servcheck_send_notification($results, $test, $type, $last_log) {
607605
if (!is_null($test['path'])) {
608606
$message[2]['text'] .= '<tr><td>Path:</td><td>' . $test['path'] . '</td></tr>' . PHP_EOL;
609607
}
610-
$message[2]['text'] .= '<tr><td>Date:</td><td>' . date('F j, Y - h:i:s', $results['time']) . '</td></tr>' . PHP_EOL;
608+
$message[2]['text'] .= '<tr><td>Date:</td><td>' . date('d-b-Y h:i:s p', $results['time']) . '</td></tr>' . PHP_EOL;
609+
610+
$message[2]['text'] .= '<tr><td>Certificate expires in: </td><td> ' . $test['days'] . ' days (' . (isset($test['expiry_date']) ? $test['expiry_date'] : 'Invalid Expiry Date') . ')' . '</td></tr>' . PHP_EOL;
611611

612-
$message[2]['text'] .= '<tr><td>Certificate expire in: </td><td> ' . $test['days'] . ' days (' . $test['expiry_date'] . ')' . '</td></tr>' . PHP_EOL;
613612
$message[2]['text'] .= '</table>' . PHP_EOL;
614613

615614
if ($test['notes'] != '') {

Diff for: servcheck_test.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -918,12 +918,12 @@ function servcheck_show_graph() {
918918
WHERE id = ?',
919919
array($id));
920920

921-
print '<br/><br/><b>' . html_escape($result['display_name']) . ':</b><br/>';
921+
print '<b>' . html_escape($result['display_name']) . ':</b><br/>';
922922

923923
foreach ($graph_interval as $key => $value) {
924-
print ($value) . ': ';
924+
print '<b>' . ($value) . ':</b>';
925925
plugin_servcheck_graph ($id, $key);
926-
print '<br/><br/>';
926+
print '<br/><hr style="width: 50%; margin-left: 0; margin-right: auto;"><br/>'; //print line below the graph.
927927
}
928928
}
929929

@@ -944,9 +944,10 @@ function servcheck_show_last_data() {
944944
WHERE id = ?',
945945
array($id));
946946

947-
print '<br/><br/><b>' . __('Last returned data of test', 'servcheck') . ' ' . html_escape($result['display_name']) . ':</b><br/>';
947+
print '<b>' . __('Last returned data of test', 'servcheck') . ' ' . html_escape($result['display_name']) . ':</b><br/>';
948948

949-
print '<pre>' . html_escape($result['last_returned_data']) . '</pre>';
949+
//print '<pre>' . html_escape($result['last_returned_data']) . '</pre>';
950+
print '<style> pre {white-space: pre-wrap; word-wrap: break-word; overflow-wrap: break-word; max-width: 25%; width: 25%}</style><pre>' . html_escape($result['last_returned_data']) . '</pre>';
950951
}
951952

952953
function list_tests() {

Diff for: setup.php

+1
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ function servcheck_config_settings() {
357357
'3' => __('3 days before', 'servcheck'),
358358
'7' => __('1 week before', 'servcheck'),
359359
'21' => __('3 weeks before', 'servcheck'),
360+
'30' => __('30 days before', 'servcheck'),
360361
),
361362
'default' => 7
362363
),

0 commit comments

Comments
 (0)