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 ,
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 );
410412if ($ 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 ()) {
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
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 }
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 ' ]);
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
515518unregister_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 ' ],
528530
529531servcheck_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
585608function 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