diff --git a/src/logger.c b/src/logger.c index 4094a7c..7ac0c9d 100644 --- a/src/logger.c +++ b/src/logger.c @@ -82,6 +82,17 @@ void open_logfile() logger(LG_WARN, "Cannot open logfile %s: %s", g_logfile_path, strerror(errno)); } +void reopen_logfile() +{ + lock_mutex_or_die(&g_log_file_mutex); + if(g_logfile) + fclose(g_logfile); + g_logfile = fopen(g_logfile_path, "a"); + if (!g_logfile) + logger(LG_WARN, "Cannot open logfile %s: %s", g_logfile_path, strerror(errno)); + unlock_mutex_or_die(&g_log_file_mutex); +} + void close_logfile() { lock_mutex_or_die(&g_log_file_mutex); @@ -112,7 +123,7 @@ void logger(int priority, const char *loginfo, ...) char timestring[64]; time_t now_t = time(0); struct tm now; localtime_r(&now_t, &now); - strftime(timestring, 64, "%F %T ", &now); + strftime(timestring, 64, "%F %T ", &now); fputs(timestring, g_logfile); /* write log message */ @@ -124,4 +135,3 @@ void logger(int priority, const char *loginfo, ...) unlock_mutex_or_die(&g_log_file_mutex); } } - diff --git a/src/logger.h b/src/logger.h index 3c1ed58..b9d4b20 100644 --- a/src/logger.h +++ b/src/logger.h @@ -43,6 +43,7 @@ void logger(int priority, const char *loginfo, ...); void initialize_logger(); void open_logfile(); void close_logfile(); +void reopen_logfile(); #ifdef __cplusplus } diff --git a/src/module.c b/src/module.c index fc4370e..b8801ed 100644 --- a/src/module.c +++ b/src/module.c @@ -96,6 +96,7 @@ char g_hidden_custom_var_prefix[256]; int g_service_authorization = AUTH_LOOSE; int g_group_authorization = AUTH_STRICT; int g_data_encoding = ENCODING_UTF8; +time_t g_last_log_rotation = -1; void *client_thread(void *data __attribute__ ((__unused__))); @@ -458,6 +459,19 @@ int broker_program(int event_type __attribute__ ((__unused__)), void *data __att return 0; } +int broker_program_data(int event_type __attribute__ ((__unused__)), void *data) +{ + // check cores last_log_rotation if we should reopen our logfile + struct nebstruct_program_status_struct *pd = (struct nebstruct_program_status_struct *)data; + if (pd->type == NEBTYPE_PROGRAMSTATUS_UPDATE) { + if(pd->last_log_rotation != g_last_log_rotation) { + g_last_log_rotation = pd->last_log_rotation; + reopen_logfile(); + } + } + return 0; +} + static void schedule_timeperiods_cache_update(struct nm_event_execution_properties *evprop) { g_counters[COUNTER_NEB_CALLBACKS]++; @@ -552,6 +566,7 @@ void register_callbacks() neb_register_callback(NEBCALLBACK_STATE_CHANGE_DATA, g_nagios_handle, 0, broker_state); // only for trigger 'state' neb_register_callback(NEBCALLBACK_ADAPTIVE_PROGRAM_DATA, g_nagios_handle, 0, broker_program); // only for trigger 'program' neb_register_callback(NEBCALLBACK_PROCESS_DATA, g_nagios_handle, 0, broker_process); // used for starting threads + neb_register_callback(NEBCALLBACK_PROGRAM_STATUS_DATA, g_nagios_handle, 0, broker_program_data); // used for reopening logfile schedule_event(1, schedule_timeperiods_cache_update, NULL); } @@ -567,6 +582,7 @@ void deregister_callbacks() neb_deregister_callback(NEBCALLBACK_STATE_CHANGE_DATA, broker_state); neb_deregister_callback(NEBCALLBACK_ADAPTIVE_PROGRAM_DATA, broker_program); neb_deregister_callback(NEBCALLBACK_PROCESS_DATA, broker_program); + neb_deregister_callback(NEBCALLBACK_PROGRAM_STATUS_DATA, broker_program); }