/** * init_files - Open log files * * Returns : * 0 on success, !0 on failure */ int init_files(void) { int rc = 0; /* lp_diag log file */ if (lp_error_log_fd != STDOUT_FILENO) { /* 1 = stdout */ rotate_log_file(lp_error_log_file); lp_error_log_fd = open(lp_error_log_file, O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP); if (lp_error_log_fd == -1) dbg("Could not open log file \"%s\"", lp_error_log_file); } /* open event log file */ rotate_log_file(lp_event_log_file); lp_event_log_fd = open(lp_event_log_file, O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP); if (lp_event_log_fd == -1) { log_msg("Could not open indicator event log file \"%s\"", lp_event_log_file); return -1; } return rc; }
int main (int argc, char **argv) { time_t rotation_time; struct stat stat_info, stat_new; char *log_filename_localtime = NULL; char *temp_command = NULL; struct tm *t; plan_tests(14); rotation_time = (time_t)1242949698; t=localtime(&rotation_time); asprintf(&log_filename_localtime,"var/nagios-%02d-%02d-%d-%02d.log", t->tm_mon+1,t->tm_mday,t->tm_year+1900,t->tm_hour); log_rotation_method=5; ok(rotate_log_file(rotation_time) == ERROR, "Got error for a bad log_rotation_method"); log_file="renamefailure"; log_rotation_method=LOG_ROTATION_HOURLY; ok(rotate_log_file(rotation_time) == ERROR, "Got an error with rename"); ok( strcmp(saved_dest, log_filename_localtime)==0, "Got an hourly rotation" ); log_file="var/nagios.log"; log_rotation_method=LOG_ROTATION_HOURLY; ok(system("cp var/nagios.log.dummy var/nagios.log")==0, "Copied in dummy nagios.log for archiving"); ok(rotate_log_file(rotation_time) == OK, "Log rotation should work happily"); ok( system("diff var/nagios.log var/nagios.log.expected > /dev/null")==0, "Got correct contents of nagios.log"); asprintf(&temp_command, "diff var/nagios.log.dummy %s", log_filename_localtime); ok( system(temp_command)==0, "nagios log archived correctly" ); unlink(log_filename_localtime); ok( system("chmod 777 var/nagios.log")==0, "Changed mode of nagios.log" ); ok( stat("var/nagios.log", &stat_info) == 0, "Got stat info for log file" ); ok( rotate_log_file(rotation_time) == OK, "Log rotate to check if mode is retained" ); ok( stat(log_filename_localtime, &stat_new) == 0, "Got new stat info for archived log file" ); ok( stat_info.st_mode == stat_new.st_mode, "Mode for archived file same as original log file" ); ok( stat("var/nagios.log", &stat_new) == 0, "Got new stat info for new log file" ); ok( stat_info.st_mode == stat_new.st_mode, "Mode for new log file kept same as original log file" ); return exit_status (); }
/* this is the main event handler loop */ static void event_execution_loop(void) { while (!sigshutdown && !sigrestart) { if (sigrotate == TRUE) { sigrotate = FALSE; rotate_log_file(time(NULL)); update_program_status(FALSE); } if (event_poll()) break; } }
int main (int argc, char **argv) { time_t rotation_time; struct stat stat_info, stat_new; plan_tests(14); rotation_time = (time_t)1242949698; log_rotation_method=5; ok(rotate_log_file(rotation_time) == ERROR, "Got error for a bad log_rotation_method"); log_file="renamefailure"; log_rotation_method=LOG_ROTATION_HOURLY; ok(rotate_log_file(rotation_time) == ERROR, "Got an error with rename"); ok( strcmp(saved_dest, "var/nagios-05-22-2009-00.log")==0, "Got an hourly rotation" ); log_file="var/nagios.log"; log_rotation_method=LOG_ROTATION_HOURLY; ok(system("cp var/nagios.log.dummy var/nagios.log")==0, "Copied in dummy nagios.log for archiving"); ok(rotate_log_file(rotation_time) == OK, "Log rotation should work happily"); ok( system("diff var/nagios.log var/nagios.log.expected > /dev/null")==0, "Got correct contents of nagios.log"); ok( system("diff var/nagios.log.dummy var/nagios-05-22-2009-00.log")==0, "nagios log archived correctly" ); unlink("var/nagios-05-22-2009-00.log"); ok( system("chmod 777 var/nagios.log")==0, "Changed mode of nagios.log" ); ok( stat("var/nagios.log", &stat_info) == 0, "Got stat info for log file" ); ok( rotate_log_file(rotation_time) == OK, "Log rotate to check if mode is retained" ); ok( stat("var/nagios-05-22-2009-00.log", &stat_new) == 0, "Got new stat info for archived log file" ); ok( stat_info.st_mode == stat_new.st_mode, "Mode for archived file same as original log file" ); ok( stat("var/nagios.log", &stat_new) == 0, "Got new stat info for new log file" ); ok( stat_info.st_mode == stat_new.st_mode, "Mode for new log file kept same as original log file" ); return exit_status (); }
static void default_logger_log(grn_ctx *ctx, grn_log_level level, const char *timestamp, const char *title, const char *message, const char *location, void *user_data) { const char slev[] = " EACewnid-"; if (default_logger_path) { CRITICAL_SECTION_ENTER(default_logger_lock); if (!default_logger_file) { default_logger_file = grn_fopen(default_logger_path, "a"); default_logger_size = 0; if (default_logger_file) { struct stat stat; if (fstat(fileno(default_logger_file), &stat) != -1) { default_logger_size = stat.st_size; } } } if (default_logger_file) { char label = *(slev + level); int written; if (location && *location) { if (title && *title) { written = fprintf(default_logger_file, "%s|%c|%s: %s %s\n", timestamp, label, location, title, message); } else { written = fprintf(default_logger_file, "%s|%c|%s: %s\n", timestamp, label, location, message); } } else { written = fprintf(default_logger_file, "%s|%c|%s %s\n", timestamp, label, title, message); } if (written > 0) { default_logger_size += written; if (LOGGER_NEED_ROTATE(default_logger_size, default_logger_rotate_threshold_size)) { fclose(default_logger_file); default_logger_file = NULL; rotate_log_file(ctx, default_logger_path); } else { fflush(default_logger_file); } } } CRITICAL_SECTION_LEAVE(default_logger_lock); } }
static int log_print(int level, const char *str, int str_len){ if(!(s_log_flag & (1 << level))){ return 0; } rotate_log_file(0); struct timeval t; gettimeofday(&t, NULL); struct tm *tm = localtime(&t.tv_sec); const char *level_msg = s_level_msg[level]; printf("[%04d-%02d-%02d %02d:%02d:%02d.%06ld][%s]", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, t.tv_usec, level_msg); fwrite(str, 1, str_len, stdout); printf("\n"); fflush(stdout); return 1; }
static void default_query_logger_log(grn_ctx *ctx, unsigned int flag, const char *timestamp, const char *info, const char *message, void *user_data) { if (default_query_logger_path) { CRITICAL_SECTION_ENTER(default_query_logger_lock); if (!default_query_logger_file) { default_query_logger_file = grn_fopen(default_query_logger_path, "a"); default_query_logger_size = 0; if (default_query_logger_file) { struct stat stat; if (fstat(fileno(default_query_logger_file), &stat) != -1) { default_query_logger_size = stat.st_size; } } } if (default_query_logger_file) { int written; written = fprintf(default_query_logger_file, "%s|%s%s\n", timestamp, info, message); if (written > 0) { default_query_logger_size += written; if (LOGGER_NEED_ROTATE(default_query_logger_size, default_query_logger_rotate_threshold_size)) { fclose(default_query_logger_file); default_query_logger_file = NULL; rotate_log_file(ctx, default_query_logger_path); } else { fflush(default_query_logger_file); } } } CRITICAL_SECTION_LEAVE(default_query_logger_lock); } }
/* this is the main event handler loop */ int event_execution_loop(void) { timed_event *temp_event, *last_event = NULL; time_t last_time = 0L; time_t current_time = 0L; time_t last_status_update = 0L; int poll_time_ms; log_debug_info(DEBUGL_FUNCTIONS, 0, "event_execution_loop() start\n"); time(&last_time); while (1) { struct timeval now; const struct timeval *event_runtime; int inputs; /* super-priority (hardcoded) events come first */ /* see if we should exit or restart (a signal was encountered) */ if (sigshutdown == TRUE || sigrestart == TRUE) break; /* get the current time */ time(¤t_time); if (sigrotate == TRUE) { rotate_log_file(current_time); update_program_status(FALSE); } /* hey, wait a second... we traveled back in time! */ if (current_time < last_time) compensate_for_system_time_change((unsigned long)last_time, (unsigned long)current_time); /* else if the time advanced over the specified threshold, try and compensate... */ else if ((current_time - last_time) >= time_change_threshold) compensate_for_system_time_change((unsigned long)last_time, (unsigned long)current_time); /* get next scheduled event */ current_event = temp_event = (timed_event *)squeue_peek(nagios_squeue); /* if we don't have any events to handle, exit */ if (!temp_event) { log_debug_info(DEBUGL_EVENTS, 0, "There aren't any events that need to be handled! Exiting...\n"); break; } /* keep track of the last time */ last_time = current_time; /* update status information occassionally - NagVis watches the NDOUtils DB to see if Nagios is alive */ if ((unsigned long)(current_time - last_status_update) > 5) { last_status_update = current_time; update_program_status(FALSE); } event_runtime = squeue_event_runtime(temp_event->sq_event); if (temp_event != last_event) { log_debug_info(DEBUGL_EVENTS, 1, "** Event Check Loop\n"); log_debug_info(DEBUGL_EVENTS, 1, "Next Event Time: %s", ctime(&temp_event->run_time)); log_debug_info(DEBUGL_EVENTS, 1, "Current/Max Service Checks: %d/%d (%.3lf%% saturation)\n", currently_running_service_checks, max_parallel_service_checks, ((float)currently_running_service_checks / (float)max_parallel_service_checks) * 100); } last_event = temp_event; gettimeofday(&now, NULL); poll_time_ms = tv_delta_msec(&now, event_runtime); if (poll_time_ms < 0) poll_time_ms = 0; else if (poll_time_ms >= 1500) poll_time_ms = 1500; log_debug_info(DEBUGL_SCHEDULING, 2, "## Polling %dms; sockets=%d; events=%u; iobs=%p\n", poll_time_ms, iobroker_get_num_fds(nagios_iobs), squeue_size(nagios_squeue), nagios_iobs); inputs = iobroker_poll(nagios_iobs, poll_time_ms); if (inputs < 0 && errno != EINTR) { logit(NSLOG_RUNTIME_ERROR, TRUE, "Error: Polling for input on %p failed: %s", nagios_iobs, iobroker_strerror(inputs)); break; } log_debug_info(DEBUGL_IPC, 2, "## %d descriptors had input\n", inputs); /* * if the event we peaked was removed from the queue from * one of the I/O operations, we must take care not to * try to run at, as we're (almost) sure to access free'd * or invalid memory if we do. */ if (!current_event) { log_debug_info(DEBUGL_EVENTS, 0, "Event was cancelled by iobroker input\n"); continue; } gettimeofday(&now, NULL); if (tv_delta_msec(&now, event_runtime) >= 0) continue; /* move on if we shouldn't run this event */ if (should_run_event(temp_event) == FALSE) continue; /* handle the event */ handle_timed_event(temp_event); /* * we must remove the entry we've peeked, or * we'll keep getting the same one over and over. * This also maintains sync with broker modules. */ remove_event(nagios_squeue, temp_event); /* reschedule the event if necessary */ if (temp_event->recurring == TRUE) reschedule_event(nagios_squeue, temp_event); /* else free memory associated with the event */ else my_free(temp_event); } log_debug_info(DEBUGL_FUNCTIONS, 0, "event_execution_loop() end\n"); return OK; }