Skip to content

Commit

Permalink
reopen logfile if cores last_log_rotation changed
Browse files Browse the repository at this point in the history
since we no longer SIGHUP the core to rotate the logfile, we need to reopen the logfile in the module manually.

references:
 - ConSol-Monitoring/omd#146
  • Loading branch information
sni committed Feb 21, 2023
1 parent d5c2033 commit ba2fdfc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 */
Expand All @@ -124,4 +135,3 @@ void logger(int priority, const char *loginfo, ...)
unlock_mutex_or_die(&g_log_file_mutex);
}
}

1 change: 1 addition & 0 deletions src/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 16 additions & 0 deletions src/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -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__)));

Expand Down Expand Up @@ -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]++;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}


Expand Down

0 comments on commit ba2fdfc

Please sign in to comment.