/* logs service states */ int log_service_states(int type, time_t *timestamp) { char *temp_buffer = NULL; service *temp_service = NULL; host *temp_host = NULL;; /* bail if we shouldn't be logging initial states */ if(type == INITIAL_STATES && log_initial_states == FALSE) return OK; for(temp_service = service_list; temp_service != NULL; temp_service = temp_service->next) { /* find the associated host */ if((temp_host = temp_service->host_ptr) == NULL) continue; asprintf(&temp_buffer, "%s SERVICE STATE: %s;%s;%s;%s;%d;%s\n", (type == INITIAL_STATES) ? "INITIAL" : "CURRENT", temp_service->host_name, temp_service->description, service_state_name(temp_service->current_state), state_type_name(temp_service->state_type), temp_service->current_attempt, temp_service->plugin_output); write_to_all_logs_with_timestamp(temp_buffer, NSLOG_INFO_MESSAGE, timestamp); my_free(temp_buffer); } return OK; }
/* logs service states */ int log_service_states(int type, time_t *timestamp){ char *temp_buffer=NULL; char *processed_buffer=NULL; service *temp_service=NULL; host *temp_host=NULL;; /* bail if we shouldn't be logging initial states */ if(type==INITIAL_STATES && log_initial_states==FALSE) return OK; for(temp_service=service_list;temp_service!=NULL;temp_service=temp_service->next){ /* find the associated host */ if((temp_host=temp_service->host_ptr)==NULL) continue; /* grab service macros */ clear_volatile_macros(); grab_host_macros(temp_host); grab_service_macros(temp_service); asprintf(&temp_buffer,"%s SERVICE STATE: %s;%s;$SERVICESTATE$;$SERVICESTATETYPE$;$SERVICEATTEMPT$;%s\n",(type==INITIAL_STATES)?"INITIAL":"CURRENT",temp_service->host_name,temp_service->description,temp_service->plugin_output); process_macros(temp_buffer,&processed_buffer,0); write_to_all_logs_with_timestamp(processed_buffer,NSLOG_INFO_MESSAGE,timestamp); my_free(temp_buffer); my_free(processed_buffer); } return OK; }
/* logs host states */ int log_host_states(int type, time_t *timestamp){ char *temp_buffer=NULL; char *processed_buffer=NULL; host *temp_host=NULL;; /* bail if we shouldn't be logging initial states */ if(type==INITIAL_STATES && log_initial_states==FALSE) return OK; for(temp_host=host_list;temp_host!=NULL;temp_host=temp_host->next){ /* grab the host macros */ clear_volatile_macros(); grab_host_macros(temp_host); asprintf(&temp_buffer,"%s HOST STATE: %s;$HOSTSTATE$;$HOSTSTATETYPE$;$HOSTATTEMPT$;%s\n",(type==INITIAL_STATES)?"INITIAL":"CURRENT",temp_host->name,(temp_host->plugin_output==NULL)?"":temp_host->plugin_output); process_macros(temp_buffer,&processed_buffer,0); write_to_all_logs_with_timestamp(processed_buffer,NSLOG_INFO_MESSAGE,timestamp); my_free(temp_buffer); my_free(processed_buffer); } return OK; }
/* logs host states */ int log_host_states(int type, time_t *timestamp) { char *temp_buffer = NULL; host *temp_host = NULL; /* bail if we shouldn't be logging initial states */ if (type == INITIAL_STATES && log_initial_states == FALSE) return OK; for (temp_host = host_list; temp_host != NULL; temp_host = temp_host->next) { dummy = asprintf(&temp_buffer, "%s HOST STATE: %s;%s;%s;%d;%s\n", (type == INITIAL_STATES) ? "INITIAL" : "CURRENT", temp_host->name, host_state_name(temp_host->current_state), state_type_name(temp_host->state_type), temp_host->current_attempt, (temp_host->plugin_output == NULL) ? "" : temp_host->plugin_output ); write_to_all_logs_with_timestamp(temp_buffer, NSLOG_INFO_MESSAGE, timestamp); my_free(temp_buffer); } return OK; }
/* record log file version/info */ int write_log_file_info(time_t *timestamp) { char *temp_buffer = NULL; /* write log version */ asprintf(&temp_buffer, "LOG VERSION: %s\n", LOG_VERSION_2); write_to_all_logs_with_timestamp(temp_buffer, NSLOG_PROCESS_INFO, timestamp); my_free(temp_buffer); return OK; }
/* rotates the main log file */ int rotate_log_file(time_t rotation_time) { char *temp_buffer = NULL; char method_string[16] = ""; char *log_archive = NULL; struct tm *t, tm_s; int rename_result = 0; int stat_result = -1; struct stat log_file_stat; struct stat archive_stat; int archive_stat_result; if(log_rotation_method == LOG_ROTATION_NONE) { return OK; } else if(log_rotation_method == LOG_ROTATION_HOURLY) strcpy(method_string, "HOURLY"); else if(log_rotation_method == LOG_ROTATION_DAILY) strcpy(method_string, "DAILY"); else if(log_rotation_method == LOG_ROTATION_WEEKLY) strcpy(method_string, "WEEKLY"); else if(log_rotation_method == LOG_ROTATION_MONTHLY) strcpy(method_string, "MONTHLY"); else return ERROR; /* update the last log rotation time and status log */ last_log_rotation = time(NULL); update_program_status(FALSE); t = localtime_r(&rotation_time, &tm_s); stat_result = stat(log_file, &log_file_stat); close_log_file(); /* get the archived filename to use */ asprintf(&log_archive, "%s%snagios-%02d-%02d-%d-%02d.log", log_archive_path, (log_archive_path[strlen(log_archive_path) - 1] == '/') ? "" : "/", t->tm_mon + 1, t->tm_mday, t->tm_year + 1900, t->tm_hour); /* HACK: If the archive exists, don't overwrite it. This is a hack because the real problem is that some log rotations are executed early and as a result the next log rotatation is scheduled for the same time as the one that ran early */ archive_stat_result = stat(log_archive, &archive_stat); if((0 == archive_stat_result) || ((-1 == archive_stat_result) && (ENOENT != errno))) { return OK; } /* rotate the log file */ rename_result = my_rename(log_file, log_archive); log_fp = open_log_file(); if (log_fp == NULL) return ERROR; if(rename_result) { my_free(log_archive); return ERROR; } /* record the log rotation after it has been done... */ asprintf(&temp_buffer, "LOG ROTATION: %s\n", method_string); write_to_all_logs_with_timestamp(temp_buffer, NSLOG_PROCESS_INFO, &rotation_time); my_free(temp_buffer); /* record log file version format */ write_log_file_info(&rotation_time); if(stat_result == 0) { chmod(log_file, log_file_stat.st_mode); chown(log_file, log_file_stat.st_uid, log_file_stat.st_gid); } /* log current host and service state if activated */ if(log_current_states==TRUE) { log_host_states(CURRENT_STATES, &rotation_time); log_service_states(CURRENT_STATES, &rotation_time); } /* free memory */ my_free(log_archive); return OK; }
/* rotates the main log file */ int rotate_log_file(time_t rotation_time){ char *temp_buffer=NULL; char method_string[16]=""; char *log_archive=NULL; struct tm *t; int rename_result=0; int stat_result=-1; struct stat log_file_stat; if(log_rotation_method==LOG_ROTATION_NONE){ return OK; } else if(log_rotation_method==LOG_ROTATION_HOURLY) strcpy(method_string,"HOURLY"); else if(log_rotation_method==LOG_ROTATION_DAILY) strcpy(method_string,"DAILY"); else if(log_rotation_method==LOG_ROTATION_WEEKLY) strcpy(method_string,"WEEKLY"); else if(log_rotation_method==LOG_ROTATION_MONTHLY) strcpy(method_string,"MONTHLY"); else return ERROR; /* update the last log rotation time and status log */ last_log_rotation=time(NULL); update_program_status(FALSE); t=localtime(&rotation_time); stat_result = stat(log_file, &log_file_stat); /* get the archived filename to use */ asprintf(&log_archive,"%s%snagios-%02d-%02d-%d-%02d.log",log_archive_path,(log_archive_path[strlen(log_archive_path)-1]=='/')?"":"/",t->tm_mon+1,t->tm_mday,t->tm_year+1900,t->tm_hour); /* rotate the log file */ rename_result=my_rename(log_file,log_archive); if(rename_result){ my_free(log_archive); return ERROR; } #ifdef USE_EVENT_BROKER /* REMOVED - log rotation events are already handled by NEBTYPE_TIMEDEVENT_EXECUTE... */ /* send data to the event broker */ /* broker_log_data(NEBTYPE_LOG_ROTATION,NEBFLAG_NONE,NEBATTR_NONE,log_archive,log_rotation_method,0,NULL); */ #endif /* record the log rotation after it has been done... */ asprintf(&temp_buffer,"LOG ROTATION: %s\n",method_string); write_to_all_logs_with_timestamp(temp_buffer,NSLOG_PROCESS_INFO,&rotation_time); my_free(temp_buffer); /* record log file version format */ write_log_file_info(&rotation_time); if(stat_result==0){ chmod(log_file, log_file_stat.st_mode); chown(log_file, log_file_stat.st_uid, log_file_stat.st_gid); } /* log current host and service state */ log_host_states(CURRENT_STATES,&rotation_time); log_service_states(CURRENT_STATES,&rotation_time); /* free memory */ my_free(log_archive); return OK; }
/* rotates the main log file */ int rotate_log_file(time_t rotation_time) { char *temp_buffer = NULL; char method_string[16] = ""; char *log_archive = NULL; struct tm *t, tm_s; int rename_result = 0; int stat_result = -1; struct stat log_file_stat; if (log_rotation_method == LOG_ROTATION_NONE) { return OK; } else if (log_rotation_method == LOG_ROTATION_HOURLY) strcpy(method_string, "HOURLY"); else if (log_rotation_method == LOG_ROTATION_DAILY) strcpy(method_string, "DAILY"); else if (log_rotation_method == LOG_ROTATION_WEEKLY) strcpy(method_string, "WEEKLY"); else if (log_rotation_method == LOG_ROTATION_MONTHLY) strcpy(method_string, "MONTHLY"); else return ERROR; /* update the last log rotation time and status log */ last_log_rotation = time(NULL); update_program_status(FALSE); t = localtime_r(&rotation_time, &tm_s); stat_result = stat(log_file, &log_file_stat); close_log_file(); /* get the archived filename to use */ dummy = asprintf(&log_archive, "%s%sicinga-%02d-%02d-%d-%02d.log", log_archive_path, (log_archive_path[strlen(log_archive_path)-1] == '/') ? "" : "/", t->tm_mon + 1, t->tm_mday, t->tm_year + 1900, t->tm_hour); /* rotate the log file */ rename_result = my_rename(log_file, log_archive); log_fp = open_log_file(); if (rename_result) { my_free(log_archive); return ERROR; } /* record the log rotation after it has been done... */ dummy = asprintf(&temp_buffer, "LOG ROTATION: %s\n", method_string); write_to_all_logs_with_timestamp(temp_buffer, NSLOG_PROCESS_INFO, &rotation_time); my_free(temp_buffer); /* record log file version format */ write_log_file_info(&rotation_time); if (stat_result == 0) { chmod(log_file, log_file_stat.st_mode); dummy = chown(log_file, log_file_stat.st_uid, log_file_stat.st_gid); } /* log current host and service state if activated*/ if (log_current_states == TRUE) { log_host_states(CURRENT_STATES, &rotation_time); log_service_states(CURRENT_STATES, &rotation_time); } /* free memory */ my_free(log_archive); return OK; }