Skip to content

Commit

Permalink
reopen logfile after core rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Feb 20, 2023
1 parent 50328c7 commit 2810442
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,5 +1,8 @@
This file documents the revision history for mod_gearman.

next:
- reopen logfile after core rotation

5.0.2 Sun Feb 5 19:05:17 CET 2023
- fix crash when using multiple result worker in neb module

Expand Down
14 changes: 13 additions & 1 deletion common/utils.c
Expand Up @@ -256,6 +256,7 @@ int set_default_options(mod_gm_opt_t *opt) {

opt->set_queues_by_hand = 0;
opt->result_workers = 1;
opt->lock = NULL;
opt->crypt_key = NULL;
opt->result_queue = NULL;
opt->keyfile = NULL;
Expand Down Expand Up @@ -1445,11 +1446,11 @@ void gm_log( int lvl, const char *text, ... ) {
time_t t;
va_list ap;
struct tm now;
bool locked = false;

if(mod_gm_opt != NULL) {
debug_level = mod_gm_opt->debug_level;
logmode = mod_gm_opt->logmode;
fp = mod_gm_opt->logfile_fp;
}

if(logmode == GM_LOG_MODE_CORE) {
Expand Down Expand Up @@ -1522,6 +1523,14 @@ void gm_log( int lvl, const char *text, ... ) {
return;
}

if(mod_gm_opt != NULL && logmode == GM_LOG_MODE_FILE) {
if(mod_gm_opt->lock != NULL) {
pthread_mutex_lock(mod_gm_opt->lock);
locked = true;
}
fp = mod_gm_opt->logfile_fp;
}

if(logmode == GM_LOG_MODE_FILE && fp != NULL) {
fprintf( fp, "%s%s %s", buffer1, buffer2, buffer3 );
fflush( fp );
Expand All @@ -1534,6 +1543,9 @@ void gm_log( int lvl, const char *text, ... ) {
printf( "%s%s %s", buffer1, buffer2, buffer3 );
}

if(locked == true)
pthread_mutex_unlock(mod_gm_opt->lock);

return;
}

Expand Down
1 change: 1 addition & 0 deletions include/common.h
Expand Up @@ -177,6 +177,7 @@ typedef struct gm_server {
* into this structure
*/
typedef struct mod_gm_opt_struct {
pthread_mutex_t * lock; /**< optional lock when using threaded logging */
int set_queues_by_hand; /**< flag whether there has been queues configured by hand */

char * crypt_key; /**< encryption key used for securing the messages sent over gearman */
Expand Down
44 changes: 38 additions & 6 deletions neb_module_naemon/mod_gearman.c
Expand Up @@ -49,13 +49,15 @@ extern int log_notifications;
/* global variables */
static objectlist * mod_gm_result_list = NULL;
static pthread_mutex_t mod_gm_result_list_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t mod_gm_log_lock = PTHREAD_MUTEX_INITIALIZER;
void *gearman_module_handle=NULL;

int result_threads_running;
pthread_t result_thr[GM_LISTSIZE];
char target_queue[GM_SMALLBUFSIZE];
char temp_buffer[GM_MAX_OUTPUT];
char uniq[GM_SMALLBUFSIZE];
time_t gm_last_log_rotation = -1;

static const char *gearman_worker_source_name(void *source) {
if(!source)
Expand All @@ -74,6 +76,7 @@ static struct check_engine mod_gearman_check_engine = {

static void register_neb_callbacks(void);
static int read_arguments( const char * );
static void init_logging(mod_gm_opt_t *opt);
static int verify_options(mod_gm_opt_t *opt);
static int handle_host_check( int,void * );
static int handle_svc_check( int,void * );
Expand All @@ -83,6 +86,7 @@ static int handle_perfdata(int, void *);
static int handle_export(int, void *);
static void set_target_queue( host *, service * );
static int handle_process_events( int, void * );
static int handle_progam_status_data_events( int, void * );
static void move_results_to_core(struct nm_event_execution_properties *evprop);

int nebmodule_init( int flags, char *args, nebmodule *handle ) {
Expand All @@ -102,6 +106,7 @@ int nebmodule_init( int flags, char *args, nebmodule *handle ) {

mod_gm_opt = gm_malloc(sizeof(mod_gm_opt_t));
set_default_options(mod_gm_opt);
mod_gm_opt->lock = &mod_gm_log_lock;

/* parse arguments */
gm_log( GM_LOG_DEBUG, "Version %s\n", GM_VERSION );
Expand Down Expand Up @@ -171,7 +176,8 @@ int nebmodule_init( int flags, char *args, nebmodule *handle ) {
current_client = client;

/* register callback for process event where everything else starts */
neb_register_callback( NEBCALLBACK_PROCESS_DATA, gearman_module_handle, 0, handle_process_events );
neb_register_callback(NEBCALLBACK_PROCESS_DATA, gearman_module_handle, 0, handle_process_events );
neb_register_callback(NEBCALLBACK_PROGRAM_STATUS_DATA, gearman_module_handle, 0, handle_progam_status_data_events);
schedule_event(1, move_results_to_core, NULL);

/* log at least one line into the core logfile */
Expand Down Expand Up @@ -225,6 +231,7 @@ int nebmodule_deinit( int flags, int reason ) {
/* should be removed already, but just for the case it wasn't */
neb_deregister_callback( NEBCALLBACK_PROCESS_DATA, gearman_module_handle );
neb_deregister_callback( NEBCALLBACK_TIMED_EVENT_DATA, gearman_module_handle );
neb_deregister_callback( NEBCALLBACK_PROGRAM_STATUS_DATA, gearman_module_handle );

/* only if we have hostgroups defined or general hosts enabled */
if ( mod_gm_opt->do_hostchecks == GM_ENABLED && ( mod_gm_opt->hostgroups_num > 0 || mod_gm_opt->hosts == GM_ENABLED ))
Expand All @@ -251,8 +258,6 @@ int nebmodule_deinit( int flags, int reason ) {
neb_deregister_callback( x, gearman_module_handle );
}

neb_deregister_callback( NEBCALLBACK_PROCESS_DATA, gearman_module_handle );

gm_log( GM_LOG_DEBUG, "deregistered callbacks\n" );

/* stop result threads */
Expand Down Expand Up @@ -333,6 +338,21 @@ static void start_threads(void) {
}
}

/* handle process events */
static int handle_progam_status_data_events(int event_type, void *data) {
struct nebstruct_program_status_struct *pd = (struct nebstruct_program_status_struct *)data;
gm_log( GM_LOG_TRACE, "handle_progam_status_data_events(%i, data)\n", event_type );

if (pd->type == NEBTYPE_PROGRAMSTATUS_UPDATE) {
if(pd->last_log_rotation != gm_last_log_rotation) {
gm_last_log_rotation = pd->last_log_rotation;
init_logging(mod_gm_opt);
gm_log( GM_LOG_TRACE, "log file rotated: %i\n", pd->last_log_rotation);
}
}
return NEB_OK;
}

/* handle process events */
static int handle_process_events( int event_type, void *data ) {
int x=0;
Expand Down Expand Up @@ -1128,9 +1148,13 @@ static int read_arguments( const char *args_orig ) {
return(verify);
}


/* verify our option */
static int verify_options(mod_gm_opt_t *opt) {
/* initialize logfile filehandles */
void init_logging(mod_gm_opt_t *opt) {
pthread_mutex_lock(mod_gm_opt->lock);
if(opt->logfile_fp != NULL) {
fclose(opt->logfile_fp);
opt->logfile_fp = NULL;
}

/* open new logfile */
if ( opt->logmode == GM_LOG_MODE_AUTO && opt->logfile ) {
Expand All @@ -1145,6 +1169,14 @@ static int verify_options(mod_gm_opt_t *opt) {
if ( opt->logmode == GM_LOG_MODE_AUTO ) {
opt->logmode = GM_LOG_MODE_CORE;
}
pthread_mutex_unlock(mod_gm_opt->lock);
}

/* verify our option */
static int verify_options(mod_gm_opt_t *opt) {

init_logging(opt);


/* did we get any server? */
if(opt->server_num == 0) {
Expand Down

0 comments on commit 2810442

Please sign in to comment.