Ejemplo n.º 1
0
/** @brief Read's log data for defined timerange and stores the entries into entry_list struct
 *  @param [out] entry_list returns a filled entry list of requested log data
 *  @param [in] filter_list a list of filters of type logfilter struct
 *  @param [out] error_text returns a error string in case of an error execpt on READLOG_ERROR_MEMORY
 *  @param [in] search_string a string you are searching for
 *		Set to NULL to disable search function
 *  @param [in] reverse this bool defines which order the log entries should return
 *  @param [in] ts_start defines the start timestamp for log entries
 *	@arg >=0 means unix timestamp
 *  @param [in] ts_end defines the end timestamp for log entries
 *	@arg >=0 means unix timestamp
 *  @return
 *	@retval READLOG_OK
 *	@retval READLOG_ERROR_WARNING
 *	@retval READLOG_ERROR_FATAL
 *	@retval READLOG_ERROR_MEMORY
 *	@retval READLOG_ERROR_FILTER
 *  @author Ricardo Bartels
 *
 *  This functions reads a  \c log_file and and try's (if set) to filter for a search string.
 *  This search string uses regular expressions. The reverse option defines if you want
 *  have your log entries returned in normal or revers order. Normal order for returning
 *  would be from the newest entry to the oldest. You can also set a time "window". This
 *  defines if you want to exclude entries which are outside of these "window". Then only
 *  entries will be returned which are between start and end. Very useful if user has all
 *  entries in one log file.
**/
int get_log_entries(logentry **entry_list, logfilter **filter_list, char **error_text, char *search_string, int reverse, time_t ts_start, time_t ts_end) {
	char *input = NULL;
	char *temp_buffer = NULL;
	char *search_regex = NULL;
	char log_file_name[MAX_FILENAME_LENGTH];
	char ts_buffer[16];
	int type = 0;
	int regex_i = 0, i = 0, len = 0;
	int file_num = 1;
	int file = 0;
	int in_range = FALSE;
	int return_val = READLOG_OK;
	int data_found = FALSE;
	int open_read_failed = FALSE;
	short keep_entry = TRUE;
	time_t timestamp = 0L;
	time_t last_timestamp = 0L;
	mmapfile *thefile = NULL;
	logentry *temp_entry = NULL;
	logentry *last_entry = NULL;
	regex_t preg;
	logfilter *temp_filter;
	DIR *dirp;
	struct dirent *dptr;
	struct file_data files[10000];
#ifdef HAVE_ZLIB_H
	gzFile gzfile = NULL;
	char gz_buffer[MAX_COMMAND_BUFFER * 2];
#else
	read_gzip_logs = FALSE;
#endif

	/* empty error_text */
	if (*error_text != NULL)
		my_free(*error_text);

	/* bail out if one timestamp is negative */
	if (ts_start < 0 || ts_end < 0) {
		*error_text = strdup("start or end timestamp are invalid. Check submited date information");
		return READLOG_ERROR_FATAL;
	}

	/* check if search_string is set */
	if (search_string != NULL) {

		/* allocate for 3 extra chars, ^, $ and \0 */
		search_regex = malloc(sizeof(char) * (strlen(search_string) * 2 + 3));
		len = strlen(search_string);
		for (i = 0; i < len; i++, regex_i++) {
			if (search_string[i] == '*') {
				search_regex[regex_i++] = '.';
				search_regex[regex_i] = '*';
			} else
				search_regex[regex_i] = search_string[i];
		}

		search_regex[regex_i] = '\0';

		/* check and compile regex, return error on failure */
		if (regcomp(&preg, search_regex, REG_ICASE | REG_NOSUB) != 0) {
			regfree(&preg);
			my_free(search_regex);
			*error_text = strdup("It seems like that reagular expressions don't like what you searched for. Please change your search string.");
			return READLOG_ERROR_FATAL;
		}

		my_free(search_regex);
	}

	/* initialize file data array */
	for (i=0;i<10000;i++)
		files[i].file_name = NULL;

	/* try to open log_archive_path, return if it fails */
	if ((dirp=opendir(log_archive_path)) == NULL){

		if (search_string != NULL)
			regfree(&preg);

		asprintf(&temp_buffer, "Unable to open \"log_archive_path\" -> \"%s\"!!!", log_archive_path);
		*error_text = strdup(temp_buffer);
		my_free(temp_buffer);

		return READLOG_ERROR_FATAL;

	} else {

		/* read every dir entry */
		while ((dptr=readdir(dirp)) != NULL) {

			/* filter dir for icinga / nagios log files */
			if ((strncmp("icinga-",dptr->d_name,7) == 0 || strncmp("nagios-",dptr->d_name,7) == 0 ) &&
			    ((strstr(dptr->d_name, ".log") && strlen(dptr->d_name) == 24 ) ||
			    (read_gzip_logs == TRUE && strstr(dptr->d_name, ".log.gz") && strlen(dptr->d_name) == 27 )))
				files[file_num++].file_name = strdup(dptr->d_name);
		}
		closedir(dirp);
	}

	/* sort log files, newest first */
	qsort((void *)files, file_num, sizeof(struct file_data), sort_icinga_logfiles_by_name);

	/* define which log files to use */
	for (i=0; i< file_num; i++) {

		/* first log file is always the current log file */
		if (i == 0) {
			strncpy(log_file_name, log_file, sizeof(log_file_name) -1);
			log_file_name[sizeof(log_file_name)-1] = '\x0';

		/* return full path of logfile and store first timestamp of last file */
		} else {
			snprintf(log_file_name, sizeof(log_file_name) -1, "%s%s",log_archive_path, files[i].file_name);
			log_file_name[sizeof(log_file_name)-1] = '\x0';

			last_timestamp = timestamp;
		}

		/* free file entry and set to NULL. if valid file is found, entry gets refilled */
		my_free(files[i].file_name);

		/* we found data and we are out of range again, file must be older then ts_start. stop checking files */
		if (data_found == TRUE && in_range == FALSE)
			continue;

		/* try to open log file, or throw error and try next log file */
		open_read_failed = FALSE;
		if (read_gzip_logs == TRUE && strstr(log_file_name, ".log.gz")) {
#ifdef HAVE_ZLIB_H
			if ((gzfile = gzopen(log_file_name, "r")) == NULL)
#endif
				open_read_failed = TRUE;
		} else {
			if((file=open(log_file_name, O_RDONLY)) < -1)
				open_read_failed = TRUE;
		}
		if(open_read_failed == TRUE) {

			if (*error_text == NULL) {
				asprintf(&temp_buffer, "Unable to open log file \"%s\" !!!", log_file_name);
				*error_text = strdup(temp_buffer);
				my_free(temp_buffer);
			}

			return_val = READLOG_ERROR_WARNING;

			continue;
		}

		/* read first 16 bytes to get first timestamp, or throw error if data is not 16 bytes log (empty file) */
		open_read_failed = FALSE;
		if (read_gzip_logs == TRUE && strstr(log_file_name, ".log.gz")) {
#ifdef HAVE_ZLIB_H
			if(gzread(gzfile,ts_buffer,16) != 16)
#endif
				open_read_failed = TRUE;
		} else {
			if(read(file,ts_buffer,16) != 16)
				open_read_failed = TRUE;
		}
		if(open_read_failed == TRUE) {

			if (*error_text == NULL) {
				asprintf(&temp_buffer, "Log file \"%s\" invalid! No timestamp found within first 16 bytes!", log_file_name);
				*error_text = strdup(temp_buffer);
				my_free(temp_buffer);
			}

			return_val = READLOG_ERROR_WARNING;

#ifdef HAVE_ZLIB_H
			if (read_gzip_logs == TRUE && strstr(log_file_name, ".log.gz"))
				gzclose(gzfile);
			else
#endif
				close(file);
			continue;
		}

#ifdef HAVE_ZLIB_H
		if (read_gzip_logs == TRUE && strstr(log_file_name, ".log.gz"))
			gzclose(gzfile);
		else
#endif
			close(file);

		/* get first timestamp */
		temp_buffer = strtok(ts_buffer, "]");
		timestamp = (temp_buffer == NULL) ? 0L : strtoul(temp_buffer + 1, NULL, 10);


		/* if first (oldest) timestamp in file is newer then ts_end, skip file */
		if (timestamp > ts_end)
			continue;

		in_range = TRUE;

		/* the priviouse file holds range for ts_start */
		if (last_timestamp != 0L && last_timestamp < ts_start)
			in_range = FALSE;

		/* keep file if in range */
		if(in_range == TRUE) {
			files[i].file_name = strdup(log_file_name);
			data_found = TRUE;
		}
	}

	/* read all log files we found earlier in reverse order, starting with the oldest */
	for (i=file_num; i >= 0; i--) {

		/* if file name is empty try next file */
		if (files[i].file_name == NULL)
			continue;

		/* try to open log file */
		if (read_gzip_logs == TRUE && strstr(files[i].file_name, ".log.gz")) {
#ifdef HAVE_ZLIB_H
			if ((gzfile = gzopen(files[i].file_name, "r")) == NULL)
#endif
				continue;
		} else {
			if ((thefile = mmap_fopen(files[i].file_name)) == NULL)
				continue;
		}

		while (1) {

			/* free memory */
			my_free(input);

			if (read_gzip_logs == TRUE && strstr(files[i].file_name, ".log.gz")) {
#ifdef HAVE_ZLIB_H
				if ((gzgets(gzfile, gz_buffer, MAX_COMMAND_BUFFER * 2 + 1)) == NULL)
#endif
					break;
			} else {
				if ((input = mmap_fgets(thefile)) == NULL)
					break;
			}

#ifdef HAVE_ZLIB_H
			if (read_gzip_logs == TRUE && strstr(files[i].file_name, ".log.gz")) {
				gz_buffer[MAX_COMMAND_BUFFER * 2 - 1] = '\0';
				input = strdup(gz_buffer);
			}
#endif
			strip(input);

			if ((int)strlen(input) == 0)
				continue;

			/* get timestamp */
			temp_buffer = strtok(input, "]");

			if (temp_buffer == NULL)
				continue;

			timestamp = strtoul(temp_buffer + 1, NULL, 10);

			/* skip line if out of range */
			if ((ts_end >= 0 && timestamp > ts_end) || (ts_start >= 0 && timestamp < ts_start))
				continue;

			/* get log entry text */
			temp_buffer = strtok(NULL, "\n");

			if (temp_buffer == NULL)
				continue;

			/* if we search for something, check if it entry matches search_string */
			if (search_string != NULL) {
				if (regexec(&preg, temp_buffer, 0, NULL, 0) == REG_NOMATCH)
					continue;
			}

			/* categorize log entry */
			if (strstr(temp_buffer, " starting..."))
				type = LOGENTRY_STARTUP;
			else if (strstr(temp_buffer, " shutting down..."))
				type = LOGENTRY_SHUTDOWN;
			else if (strstr(temp_buffer, "Bailing out"))
				type = LOGENTRY_BAILOUT;
			else if (strstr(temp_buffer, " restarting..."))
				type = LOGENTRY_RESTART;
			else if (strstr(temp_buffer, "HOST ALERT:") && strstr(temp_buffer, ";DOWN;"))
				type = LOGENTRY_HOST_DOWN;
			else if (strstr(temp_buffer, "HOST ALERT:") && strstr(temp_buffer, ";UNREACHABLE;"))
				type = LOGENTRY_HOST_UNREACHABLE;
			else if (strstr(temp_buffer, "HOST ALERT:") && strstr(temp_buffer, ";RECOVERY;"))
				type = LOGENTRY_HOST_RECOVERY;
			else if (strstr(temp_buffer, "HOST ALERT:") && strstr(temp_buffer, ";UP;"))
				type = LOGENTRY_HOST_UP;
			else if (strstr(temp_buffer, "HOST NOTIFICATION:"))
				type = LOGENTRY_HOST_NOTIFICATION;
			else if (strstr(temp_buffer, "SERVICE ALERT:") && strstr(temp_buffer, ";CRITICAL;"))
				type = LOGENTRY_SERVICE_CRITICAL;
			else if (strstr(temp_buffer, "SERVICE ALERT:") && strstr(temp_buffer, ";WARNING;"))
				type = LOGENTRY_SERVICE_WARNING;
			else if (strstr(temp_buffer, "SERVICE ALERT:") && strstr(temp_buffer, ";UNKNOWN;"))
				type = LOGENTRY_SERVICE_UNKNOWN;
			else if (strstr(temp_buffer, "SERVICE ALERT:") && strstr(temp_buffer, ";RECOVERY;"))
				type = LOGENTRY_SERVICE_RECOVERY;
			else if (strstr(temp_buffer, "SERVICE ALERT:") && strstr(temp_buffer, ";OK;"))
				type = LOGENTRY_SERVICE_OK;
			else if (strstr(temp_buffer, "SERVICE NOTIFICATION:"))
				type = LOGENTRY_SERVICE_NOTIFICATION;
			else if (strstr(temp_buffer, "SERVICE EVENT HANDLER:"))
				type = LOGENTRY_SERVICE_EVENT_HANDLER;
			else if (strstr(temp_buffer, "HOST EVENT HANDLER:"))
				type = LOGENTRY_HOST_EVENT_HANDLER;
			else if (strstr(temp_buffer, "EXTERNAL COMMAND:"))
				type = LOGENTRY_EXTERNAL_COMMAND;
			else if (strstr(temp_buffer, "PASSIVE SERVICE CHECK:"))
				type = LOGENTRY_PASSIVE_SERVICE_CHECK;
			else if (strstr(temp_buffer, "PASSIVE HOST CHECK:"))
				type = LOGENTRY_PASSIVE_HOST_CHECK;
			else if (strstr(temp_buffer, "LOG ROTATION:"))
				type = LOGENTRY_LOG_ROTATION;
			else if (strstr(temp_buffer, "active mode..."))
				type = LOGENTRY_ACTIVE_MODE;
			else if (strstr(temp_buffer, "standby mode..."))
				type = LOGENTRY_STANDBY_MODE;
			else if (strstr(temp_buffer, "SERVICE FLAPPING ALERT:") && strstr(temp_buffer, ";STARTED;"))
				type = LOGENTRY_SERVICE_FLAPPING_STARTED;
			else if (strstr(temp_buffer, "SERVICE FLAPPING ALERT:") && strstr(temp_buffer, ";STOPPED;"))
				type = LOGENTRY_SERVICE_FLAPPING_STOPPED;
			else if (strstr(temp_buffer, "SERVICE FLAPPING ALERT:") && strstr(temp_buffer, ";DISABLED;"))
				type = LOGENTRY_SERVICE_FLAPPING_DISABLED;
			else if (strstr(temp_buffer, "HOST FLAPPING ALERT:") && strstr(temp_buffer, ";STARTED;"))
				type = LOGENTRY_HOST_FLAPPING_STARTED;
			else if (strstr(temp_buffer, "HOST FLAPPING ALERT:") && strstr(temp_buffer, ";STOPPED;"))
				type = LOGENTRY_HOST_FLAPPING_STOPPED;
			else if (strstr(temp_buffer, "HOST FLAPPING ALERT:") && strstr(temp_buffer, ";DISABLED;"))
				type = LOGENTRY_HOST_FLAPPING_DISABLED;
			else if (strstr(temp_buffer, "SERVICE DOWNTIME ALERT:") && strstr(temp_buffer, ";STARTED;"))
				type = LOGENTRY_SERVICE_DOWNTIME_STARTED;
			else if (strstr(temp_buffer, "SERVICE DOWNTIME ALERT:") && strstr(temp_buffer, ";STOPPED;"))
				type = LOGENTRY_SERVICE_DOWNTIME_STOPPED;
			else if (strstr(temp_buffer, "SERVICE DOWNTIME ALERT:") && strstr(temp_buffer, ";CANCELLED;"))
				type = LOGENTRY_SERVICE_DOWNTIME_CANCELLED;
			else if (strstr(temp_buffer, "HOST DOWNTIME ALERT:") && strstr(temp_buffer, ";STARTED;"))
				type = LOGENTRY_HOST_DOWNTIME_STARTED;
			else if (strstr(temp_buffer, "HOST DOWNTIME ALERT:") && strstr(temp_buffer, ";STOPPED;"))
				type = LOGENTRY_HOST_DOWNTIME_STOPPED;
			else if (strstr(temp_buffer, "HOST DOWNTIME ALERT:") && strstr(temp_buffer, ";CANCELLED;"))
				type = LOGENTRY_HOST_DOWNTIME_CANCELLED;
			else if (strstr(temp_buffer, "INITIAL SERVICE STATE:"))
				type = LOGENTRY_SERVICE_INITIAL_STATE;
			else if (strstr(temp_buffer, "INITIAL HOST STATE:"))
				type = LOGENTRY_HOST_INITIAL_STATE;
			else if (strstr(temp_buffer, "CURRENT SERVICE STATE:"))
				type = LOGENTRY_SERVICE_CURRENT_STATE;
			else if (strstr(temp_buffer, "CURRENT HOST STATE:"))
				type = LOGENTRY_HOST_CURRENT_STATE;
			else if (strstr(temp_buffer, "error executing command"))
				type = LOGENTRY_ERROR_COMMAND_EXECUTION;
			else if (strstr(temp_buffer, "idomod:"))
				type = LOGENTRY_IDOMOD;
			else if (strstr(temp_buffer, "npcdmod:"))
				type = LOGENTRY_NPCDMOD;
			else if (strstr(temp_buffer, "Auto-save of"))
				type = LOGENTRY_AUTOSAVE;
			else if (strstr(temp_buffer, "Warning:"))
				type = LOGENTRY_SYSTEM_WARNING;
			else
				type = LOGENTRY_UNDEFINED;

			/* apply filters */
			if (*filter_list != NULL) {
				keep_entry = FALSE;
				for (temp_filter = *filter_list; temp_filter != NULL; temp_filter = temp_filter->next) {
					if (temp_filter->include != 0) {
						if (temp_filter->include == type) {
							keep_entry = TRUE;
							break;
						}
					} else if (temp_filter->exclude != 0) {
						if (temp_filter->exclude == type) {
							keep_entry = FALSE;
							break;
						} else
							keep_entry = TRUE;
					}
				}
				if (keep_entry == FALSE)
					continue;
			}

			/* initialzie */
			/* allocate memory for a new log entry */
			temp_entry = (logentry *)malloc(sizeof(logentry));
			if (temp_entry == NULL) {

				mmap_fclose(thefile);
				return READLOG_ERROR_MEMORY;
			}

			temp_entry->timestamp = 0L;
			temp_entry->type = 0;
			temp_entry->entry_text = NULL;
			temp_entry->next = NULL;


			temp_entry->timestamp = timestamp;
			temp_entry->type = type;
			temp_entry->entry_text = strdup(temp_buffer);

			if (reverse == TRUE) {
				if (*entry_list == NULL) {
					*entry_list = temp_entry;
					last_entry = *entry_list;
				} else {
					last_entry->next = temp_entry;
					last_entry = temp_entry;
				}
			} else {
				temp_entry->next = *entry_list;
				*entry_list = temp_entry;
			}
		}

#ifdef HAVE_ZLIB_H
		if (read_gzip_logs == TRUE && strstr(files[i].file_name, ".log.gz"))
			gzclose(gzfile);
		else
#endif
			mmap_fclose(thefile);
	}

	for (i=0; i< file_num;i++)
		my_free(files[i].file_name);

	if (search_string != NULL)
		regfree(&preg);

	return return_val;
}
Ejemplo n.º 2
0
/* display the contents of the log file */
int display_log(void) {
	char *input = NULL;
	char image[MAX_INPUT_BUFFER];
	char image_alt[MAX_INPUT_BUFFER];
	time_t t;
	char *temp_buffer = NULL;
	char date_time[MAX_DATETIME_LENGTH];
	int error = FALSE;
	mmapfile *thefile = NULL;
	char last_message_date[MAX_INPUT_BUFFER] = "";
	char current_message_date[MAX_INPUT_BUFFER] = "";
	struct tm *time_ptr = NULL;


	/* check to see if the user is authorized to view the log file */
	if(is_authorized_for_system_information(&current_authdata) == FALSE) {
		printf("<HR>\n");
		printf("<DIV CLASS='errorMessage'>%s</DIV><br><br>\n",_("It appears as though you do not have permission to view the log file..."));
		printf("<DIV CLASS='errorDescription'>%s</DIV>\n",_("If you believe this is an error, check the HTTP server authentication requirements for accessing this CGI<br>and check the authorization options in your CGI configuration file."));
		printf("<HR>\n");
		return ERROR;
		}

	error = FALSE;

	if(use_lifo == TRUE) {
		error = read_file_into_lifo(log_file_to_use);
		if(error != LIFO_OK) {
			if(error == LIFO_ERROR_MEMORY) {
				printf("<P><DIV CLASS='warningMessage'>Not enough memory to reverse log file - displaying log in natural order...</DIV></P>");
				error = FALSE;
				}
			else
				error = TRUE;
			use_lifo = FALSE;
			}
		else
			error = FALSE;
		}

	if(use_lifo == FALSE) {

		if((thefile = mmap_fopen(log_file_to_use)) == NULL) {
			printf("<HR>\n");
			printf("<P><DIV CLASS='errorMessage'>%s</DIV></P>", log_file_to_use,_("Error: Could not open log file '%s' for reading!"));
			printf("<HR>\n");
			error = TRUE;
			}
		}

	if(error == FALSE) {

		printf("<P><DIV CLASS='logEntries'>\n");

		while(1) {

			free(input);

			if(use_lifo == TRUE) {
				if((input = pop_lifo()) == NULL)
					break;
				}
			else if((input = mmap_fgets(thefile)) == NULL)
				break;

			strip(input);

			if(strstr(input, " starting...")) {
				strcpy(image, START_ICON);
				strcpy(image_alt, START_ICON_ALT);
				}
			else if(strstr(input, " shutting down...")) {
				strcpy(image, STOP_ICON);
				strcpy(image_alt, STOP_ICON_ALT);
				}
			else if(strstr(input, "Bailing out")) {
				strcpy(image, STOP_ICON);
				strcpy(image_alt, STOP_ICON_ALT);
				}
			else if(strstr(input, " restarting...")) {
				strcpy(image, RESTART_ICON);
				strcpy(image_alt, RESTART_ICON_ALT);
				}
			else if(strstr(input, "HOST ALERT:") && strstr(input, ";DOWN;")) {
				strcpy(image, HOST_DOWN_ICON);
				strcpy(image_alt, HOST_DOWN_ICON_ALT);
				}
			else if(strstr(input, "HOST ALERT:") && strstr(input, ";UNREACHABLE;")) {
				strcpy(image, HOST_UNREACHABLE_ICON);
				strcpy(image_alt, HOST_UNREACHABLE_ICON_ALT);
				}
			else if(strstr(input, "HOST ALERT:") && (strstr(input, ";RECOVERY;") || strstr(input, ";UP;"))) {
				strcpy(image, HOST_UP_ICON);
				strcpy(image_alt, HOST_UP_ICON_ALT);
				}
			else if(strstr(input, "HOST NOTIFICATION:")) {
				strcpy(image, HOST_NOTIFICATION_ICON);
				strcpy(image_alt, HOST_NOTIFICATION_ICON_ALT);
				}
			else if(strstr(input, "SERVICE ALERT:") && strstr(input, ";CRITICAL;")) {
				strcpy(image, CRITICAL_ICON);
				strcpy(image_alt, CRITICAL_ICON_ALT);
				}
			else if(strstr(input, "SERVICE ALERT:") && strstr(input, ";WARNING;")) {
				strcpy(image, WARNING_ICON);
				strcpy(image_alt, WARNING_ICON_ALT);
				}
			else if(strstr(input, "SERVICE ALERT:") && strstr(input, ";UNKNOWN;")) {
				strcpy(image, UNKNOWN_ICON);
				strcpy(image_alt, UNKNOWN_ICON_ALT);
				}
			else if(strstr(input, "SERVICE ALERT:") && (strstr(input, ";RECOVERY;") || strstr(input, ";OK;"))) {
				strcpy(image, OK_ICON);
				strcpy(image_alt, OK_ICON_ALT);
				}
			else if(strstr(input, "SERVICE NOTIFICATION:")) {
				strcpy(image, NOTIFICATION_ICON);
				strcpy(image_alt, NOTIFICATION_ICON_ALT);
				}
			else if(strstr(input, "SERVICE EVENT HANDLER:")) {
				strcpy(image, SERVICE_EVENT_ICON);
				strcpy(image_alt, SERVICE_EVENT_ICON_ALT);
				}
			else if(strstr(input, "HOST EVENT HANDLER:")) {
				strcpy(image, HOST_EVENT_ICON);
				strcpy(image_alt, HOST_EVENT_ICON_ALT);
				}
			else if(strstr(input, "EXTERNAL COMMAND:")) {
				strcpy(image, EXTERNAL_COMMAND_ICON);
				strcpy(image_alt, EXTERNAL_COMMAND_ICON_ALT);
				}
			else if(strstr(input, "PASSIVE SERVICE CHECK:")) {
				strcpy(image, PASSIVE_ICON);
				strcpy(image_alt, "Passive Service Check");
				}
			else if(strstr(input, "PASSIVE HOST CHECK:")) {
				strcpy(image, PASSIVE_ICON);
				strcpy(image_alt, "Passive Host Check");
				}
			else if(strstr(input, "LOG ROTATION:")) {
				strcpy(image, LOG_ROTATION_ICON);
				strcpy(image_alt, LOG_ROTATION_ICON_ALT);
				}
			else if(strstr(input, "active mode...")) {
				strcpy(image, ACTIVE_ICON);
				strcpy(image_alt, ACTIVE_ICON_ALT);
				}
			else if(strstr(input, "standby mode...")) {
				strcpy(image, STANDBY_ICON);
				strcpy(image_alt, STANDBY_ICON_ALT);
				}
			else if(strstr(input, "SERVICE FLAPPING ALERT:") && strstr(input, ";STARTED;")) {
				strcpy(image, FLAPPING_ICON);
				strcpy(image_alt, "Service started flapping");
				}
			else if(strstr(input, "SERVICE FLAPPING ALERT:") && strstr(input, ";STOPPED;")) {
				strcpy(image, FLAPPING_ICON);
				strcpy(image_alt, "Service stopped flapping");
				}
			else if(strstr(input, "SERVICE FLAPPING ALERT:") && strstr(input, ";DISABLED;")) {
				strcpy(image, FLAPPING_ICON);
				strcpy(image_alt, "Service flap detection disabled");
				}
			else if(strstr(input, "HOST FLAPPING ALERT:") && strstr(input, ";STARTED;")) {
				strcpy(image, FLAPPING_ICON);
				strcpy(image_alt, "Host started flapping");
				}
			else if(strstr(input, "HOST FLAPPING ALERT:") && strstr(input, ";STOPPED;")) {
				strcpy(image, FLAPPING_ICON);
				strcpy(image_alt, "Host stopped flapping");
				}
			else if(strstr(input, "HOST FLAPPING ALERT:") && strstr(input, ";DISABLED;")) {
				strcpy(image, FLAPPING_ICON);
				strcpy(image_alt, "Host flap detection disabled");
				}
			else if(strstr(input, "SERVICE DOWNTIME ALERT:") && strstr(input, ";STARTED;")) {
				strcpy(image, SCHEDULED_DOWNTIME_ICON);
				strcpy(image_alt, "Service entered a period of scheduled downtime");
				}
			else if(strstr(input, "SERVICE DOWNTIME ALERT:") && strstr(input, ";STOPPED;")) {
				strcpy(image, SCHEDULED_DOWNTIME_ICON);
				strcpy(image_alt, "Service exited a period of scheduled downtime");
				}
			else if(strstr(input, "SERVICE DOWNTIME ALERT:") && strstr(input, ";CANCELLED;")) {
				strcpy(image, SCHEDULED_DOWNTIME_ICON);
				strcpy(image_alt, "Service scheduled downtime has been cancelled");
				}
			else if(strstr(input, "HOST DOWNTIME ALERT:") && strstr(input, ";STARTED;")) {
				strcpy(image, SCHEDULED_DOWNTIME_ICON);
				strcpy(image_alt, "Host entered a period of scheduled downtime");
				}
			else if(strstr(input, "HOST DOWNTIME ALERT:") && strstr(input, ";STOPPED;")) {
				strcpy(image, SCHEDULED_DOWNTIME_ICON);
				strcpy(image_alt, "Host exited a period of scheduled downtime");
				}
			else if(strstr(input, "HOST DOWNTIME ALERT:") && strstr(input, ";CANCELLED;")) {
				strcpy(image, SCHEDULED_DOWNTIME_ICON);
				strcpy(image_alt, "Host scheduled downtime has been cancelled");
				}
			else {
				strcpy(image, INFO_ICON);
				strcpy(image_alt, INFO_ICON_ALT);
				}

			temp_buffer = strtok(input, "]");
			t = (temp_buffer == NULL) ? 0L : strtoul(temp_buffer + 1, NULL, 10);

			time_ptr = localtime(&t);
			strftime(current_message_date, sizeof(current_message_date), "%B %d, %Y %H:00\n", time_ptr);
			current_message_date[sizeof(current_message_date) - 1] = '\x0';

			if(strcmp(last_message_date, current_message_date) != 0 && display_timebreaks == TRUE) {
				printf("<BR CLEAR='all'>\n");
				printf("<DIV CLASS='dateTimeBreak'>\n");
				printf("<table border=0 width=95%%><tr>");
				printf("<td width=40%%><hr width=100%%></td>");
				printf("<td align=center CLASS='dateTimeBreak'>%s</td>", current_message_date);
				printf("<td width=40%%><hr width=100%%></td>");
				printf("</tr></table>\n");
				printf("</DIV>\n");
				printf("<BR CLEAR='all'><DIV CLASS='logEntries'>\n");
				strncpy(last_message_date, current_message_date, sizeof(last_message_date));
				last_message_date[sizeof(last_message_date) - 1] = '\x0';
				}

			get_time_string(&t, date_time, (int)sizeof(date_time), SHORT_DATE_TIME);
			strip(date_time);

			temp_buffer = strtok(NULL, "\n");

			if(display_frills == TRUE)
				printf("<img align='left' src='%s%s' alt='%s' title='%s'>", url_images_path, image, image_alt, image_alt);
			printf("[%s] %s", date_time, (temp_buffer == NULL) ? "" : html_encode(temp_buffer, FALSE));
			if(enable_splunk_integration == TRUE) {
				printf("&nbsp;&nbsp;&nbsp;");
				display_splunk_generic_url(temp_buffer, 2);
				}
			printf("<br clear='all'>\n");
			}

		printf("</DIV></P>\n");
		printf("<HR>\n");

		free(input);

		if(use_lifo == FALSE)
			mmap_fclose(thefile);
		}

	if(use_lifo == TRUE)
		free_lifo_memory();

	return OK;
	}
Ejemplo n.º 3
0
void get_history(void) {
	mmapfile *thefile = NULL;
	char image[MAX_INPUT_BUFFER];
	char image_alt[MAX_INPUT_BUFFER];
	char *input = NULL;
	char *input2 = NULL;
	char match1[MAX_INPUT_BUFFER];
	char match2[MAX_INPUT_BUFFER];
	int found_line = FALSE;
	int system_message = FALSE;
	int display_line = FALSE;
	time_t t;
	char date_time[MAX_DATETIME_LENGTH];
	char *temp_buffer = NULL;
	int history_type = SERVICE_HISTORY;
	int history_detail_type = HISTORY_SERVICE_CRITICAL;
	char *entry_host_name = NULL;
	char *entry_service_desc = NULL;
	host *temp_host = NULL;
	service *temp_service = NULL;
	int result = 0;

	char last_message_date[MAX_INPUT_BUFFER] = "";
	char current_message_date[MAX_INPUT_BUFFER] = "";
	struct tm *time_ptr = NULL;


	if(use_lifo == TRUE) {
		result = read_file_into_lifo(log_file_to_use);
		if(result != LIFO_OK) {
			if(result == LIFO_ERROR_MEMORY) {
				printf("<P><DIV CLASS='warningMessage'>Not enough memory to reverse log file - displaying history in natural order...</DIV></P>\n");
				}
			else if(result == LIFO_ERROR_FILE) {
				printf("<HR><P><DIV CLASS='errorMessage'>Error: Cannot open log file '%s' for reading!</DIV></P><HR>", log_file_to_use);
				return;
				}
			use_lifo = FALSE;
			}
		}

	if(use_lifo == FALSE) {

		if((thefile = mmap_fopen(log_file_to_use)) == NULL) {
			printf("<HR><P><DIV CLASS='errorMessage'>Error: Cannot open log file '%s' for reading!</DIV></P><HR>", log_file_to_use);
			return;
			}
		}

	printf("<P><DIV CLASS='logEntries'>\n");

	while(1) {

		my_free(input);
		my_free(input2);

		if(use_lifo == TRUE) {
			if((input = pop_lifo()) == NULL)
				break;
			}
		else {
			if((input = mmap_fgets(thefile)) == NULL)
				break;
			}

		strip(input);

		strcpy(image, "");
		strcpy(image_alt, "");
		system_message = FALSE;

		if((input2 = (char *)strdup(input)) == NULL)
			continue;

		/* service state alerts */
		if(strstr(input, "SERVICE ALERT:")) {

			history_type = SERVICE_HISTORY;

			/* get host and service names */
			temp_buffer = my_strtok(input2, "]");
			temp_buffer = my_strtok(NULL, ":");
			temp_buffer = my_strtok(NULL, ";");
			if(temp_buffer)
				entry_host_name = strdup(temp_buffer + 1);
			else
				entry_host_name = NULL;
			temp_buffer = my_strtok(NULL, ";");
			if(temp_buffer)
				entry_service_desc = strdup(temp_buffer);
			else
				entry_service_desc = NULL;

			if(strstr(input, ";CRITICAL;")) {
				strncpy(image, CRITICAL_ICON, sizeof(image));
				strncpy(image_alt, CRITICAL_ICON_ALT, sizeof(image_alt));
				history_detail_type = HISTORY_SERVICE_CRITICAL;
				}
			else if(strstr(input, ";WARNING;")) {
				strncpy(image, WARNING_ICON, sizeof(image));
				strncpy(image_alt, WARNING_ICON_ALT, sizeof(image_alt));
				history_detail_type = HISTORY_SERVICE_WARNING;
				}
			else if(strstr(input, ";UNKNOWN;")) {
				strncpy(image, UNKNOWN_ICON, sizeof(image));
				strncpy(image_alt, UNKNOWN_ICON_ALT, sizeof(image_alt));
				history_detail_type = HISTORY_SERVICE_UNKNOWN;
				}
			else if(strstr(input, ";RECOVERY;") || strstr(input, ";OK;")) {
				strncpy(image, OK_ICON, sizeof(image));
				strncpy(image_alt, OK_ICON_ALT, sizeof(image_alt));
				history_detail_type = HISTORY_SERVICE_RECOVERY;
				}
			}

		/* service flapping alerts */
		else if(strstr(input, "SERVICE FLAPPING ALERT:")) {

			if(display_flapping_alerts == FALSE)
				continue;

			history_type = SERVICE_FLAPPING_HISTORY;

			/* get host and service names */
			temp_buffer = my_strtok(input2, "]");
			temp_buffer = my_strtok(NULL, ":");
			temp_buffer = my_strtok(NULL, ";");
			if(temp_buffer)
				entry_host_name = strdup(temp_buffer + 1);
			else
				entry_host_name = NULL;
			temp_buffer = my_strtok(NULL, ";");
			if(temp_buffer)
				entry_service_desc = strdup(temp_buffer);
			else
				entry_service_desc = NULL;

			strncpy(image, FLAPPING_ICON, sizeof(image));

			if(strstr(input, ";STARTED;"))
				strncpy(image_alt, "Service started flapping", sizeof(image_alt));
			else if(strstr(input, ";STOPPED;"))
				strncpy(image_alt, "Service stopped flapping", sizeof(image_alt));
			else if(strstr(input, ";DISABLED;"))
				strncpy(image_alt, "Service flap detection disabled", sizeof(image_alt));
			}

		/* service downtime alerts */
		else if(strstr(input, "SERVICE DOWNTIME ALERT:")) {

			if(display_downtime_alerts == FALSE)
				continue;

			history_type = SERVICE_DOWNTIME_HISTORY;

			/* get host and service names */
			temp_buffer = my_strtok(input2, "]");
			temp_buffer = my_strtok(NULL, ":");
			temp_buffer = my_strtok(NULL, ";");
			if(temp_buffer)
				entry_host_name = strdup(temp_buffer + 1);
			else
				entry_host_name = NULL;
			temp_buffer = my_strtok(NULL, ";");
			if(temp_buffer)
				entry_service_desc = strdup(temp_buffer);
			else
				entry_service_desc = NULL;

			strncpy(image, SCHEDULED_DOWNTIME_ICON, sizeof(image));

			if(strstr(input, ";STARTED;"))
				strncpy(image_alt, "Service entered a period of scheduled downtime", sizeof(image_alt));
			else if(strstr(input, ";STOPPED;"))
				strncpy(image_alt, "Service exited from a period of scheduled downtime", sizeof(image_alt));
			else if(strstr(input, ";CANCELLED;"))
				strncpy(image_alt, "Service scheduled downtime has been cancelled", sizeof(image_alt));
			}

		/* host state alerts */
		else if(strstr(input, "HOST ALERT:")) {

			history_type = HOST_HISTORY;

			/* get host name */
			temp_buffer = my_strtok(input2, "]");
			temp_buffer = my_strtok(NULL, ":");
			temp_buffer = my_strtok(NULL, ";");
			if(temp_buffer)
				entry_host_name = strdup(temp_buffer + 1);
			else
				entry_host_name = NULL;

			if(strstr(input, ";DOWN;")) {
				strncpy(image, HOST_DOWN_ICON, sizeof(image));
				strncpy(image_alt, HOST_DOWN_ICON_ALT, sizeof(image_alt));
				history_detail_type = HISTORY_HOST_DOWN;
				}
			else if(strstr(input, ";UNREACHABLE;")) {
				strncpy(image, HOST_UNREACHABLE_ICON, sizeof(image));
				strncpy(image_alt, HOST_UNREACHABLE_ICON_ALT, sizeof(image_alt));
				history_detail_type = HISTORY_HOST_UNREACHABLE;
				}
			else if(strstr(input, ";RECOVERY") || strstr(input, ";UP;")) {
				strncpy(image, HOST_UP_ICON, sizeof(image));
				strncpy(image_alt, HOST_UP_ICON_ALT, sizeof(image_alt));
				history_detail_type = HISTORY_HOST_RECOVERY;
				}
			}

		/* host flapping alerts */
		else if(strstr(input, "HOST FLAPPING ALERT:")) {

			if(display_flapping_alerts == FALSE)
				continue;

			history_type = HOST_FLAPPING_HISTORY;

			/* get host name */
			temp_buffer = my_strtok(input2, "]");
			temp_buffer = my_strtok(NULL, ":");
			temp_buffer = my_strtok(NULL, ";");
			if(temp_buffer)
				entry_host_name = strdup(temp_buffer + 1);
			else
				entry_host_name = NULL;

			strncpy(image, FLAPPING_ICON, sizeof(image));

			if(strstr(input, ";STARTED;"))
				strncpy(image_alt, "Host started flapping", sizeof(image_alt));
			else if(strstr(input, ";STOPPED;"))
				strncpy(image_alt, "Host stopped flapping", sizeof(image_alt));
			else if(strstr(input, ";DISABLED;"))
				strncpy(image_alt, "Host flap detection disabled", sizeof(image_alt));
			}

		/* host downtime alerts */
		else if(strstr(input, "HOST DOWNTIME ALERT:")) {

			if(display_downtime_alerts == FALSE)
				continue;

			history_type = HOST_DOWNTIME_HISTORY;

			/* get host name */
			temp_buffer = my_strtok(input2, "]");
			temp_buffer = my_strtok(NULL, ":");
			temp_buffer = my_strtok(NULL, ";");
			if(temp_buffer)
				entry_host_name = strdup(temp_buffer + 1);
			else
				entry_host_name = NULL;

			strncpy(image, SCHEDULED_DOWNTIME_ICON, sizeof(image));

			if(strstr(input, ";STARTED;"))
				strncpy(image_alt, "Host entered a period of scheduled downtime", sizeof(image_alt));
			else if(strstr(input, ";STOPPED;"))
				strncpy(image_alt, "Host exited from a period of scheduled downtime", sizeof(image_alt));
			else if(strstr(input, ";CANCELLED;"))
				strncpy(image_alt, "Host scheduled downtime has been cancelled", sizeof(image_alt));
			}

		else if(display_system_messages == FALSE)
			continue;

		/* program start */
		else if(strstr(input, " starting...")) {
			strncpy(image, START_ICON, sizeof(image));
			strncpy(image_alt, START_ICON_ALT, sizeof(image_alt));
			system_message = TRUE;
			}

		/* normal program termination */
		else if(strstr(input, " shutting down...")) {
			strncpy(image, STOP_ICON, sizeof(image));
			strncpy(image_alt, STOP_ICON_ALT, sizeof(image_alt));
			system_message = TRUE;
			}

		/* abnormal program termination */
		else if(strstr(input, "Bailing out")) {
			strncpy(image, STOP_ICON, sizeof(image));
			strncpy(image_alt, STOP_ICON_ALT, sizeof(image_alt));
			system_message = TRUE;
			}

		/* program restart */
		else if(strstr(input, " restarting...")) {
			strncpy(image, RESTART_ICON, sizeof(image));
			strncpy(image_alt, RESTART_ICON_ALT, sizeof(image_alt));
			system_message = TRUE;
			}

		image[sizeof(image) - 1] = '\x0';
		image_alt[sizeof(image_alt) - 1] = '\x0';

		/* get the timestamp */
		temp_buffer = strtok(input, "]");
		t = (temp_buffer == NULL) ? 0L : strtoul(temp_buffer + 1, NULL, 10);

		time_ptr = localtime(&t);
		strftime(current_message_date, sizeof(current_message_date), "%B %d, %Y %H:00\n", time_ptr);
		current_message_date[sizeof(current_message_date) - 1] = '\x0';

		get_time_string(&t, date_time, sizeof(date_time), SHORT_DATE_TIME);
		strip(date_time);

		temp_buffer = strtok(NULL, "\n");

		if(strcmp(image, "")) {

			display_line = FALSE;

			if(system_message == TRUE)
				display_line = TRUE;

			else if(display_type == DISPLAY_HOSTS) {

				if(history_type == HOST_HISTORY || history_type == SERVICE_HISTORY) {
					snprintf(match1, sizeof( match1), 
							" HOST ALERT: %s;", host_name);
					snprintf(match2, sizeof( match2), 
							" SERVICE ALERT: %s;", host_name);
					}
				else if(history_type == HOST_FLAPPING_HISTORY || history_type == SERVICE_FLAPPING_HISTORY) {
					snprintf(match1, sizeof( match1), 
							" HOST FLAPPING ALERT: %s;", host_name);
					snprintf(match2, sizeof( match2), 
							" SERVICE FLAPPING ALERT: %s;", host_name);
					}
				else if(history_type == HOST_DOWNTIME_HISTORY || history_type == SERVICE_DOWNTIME_HISTORY) {
					snprintf(match1, sizeof( match1), 
							" HOST DOWNTIME ALERT: %s;", host_name);
					snprintf(match2, sizeof( match2), 
							" SERVICE DOWNTIME ALERT: %s;", host_name);
					}

				if(show_all_hosts == TRUE)
					display_line = TRUE;
				else if(strstr(temp_buffer, match1))
					display_line = TRUE;
				else if(strstr(temp_buffer, match2))
					display_line = TRUE;

				if(display_line == TRUE) {
					if(history_options == HISTORY_ALL)
						display_line = TRUE;
					else if(history_options == HISTORY_HOST_ALL && (history_type == HOST_HISTORY || history_type == HOST_FLAPPING_HISTORY || history_type == HOST_DOWNTIME_HISTORY))
						display_line = TRUE;
					else if(history_options == HISTORY_SERVICE_ALL && (history_type == SERVICE_HISTORY || history_type == SERVICE_FLAPPING_HISTORY || history_type == SERVICE_DOWNTIME_HISTORY))
						display_line = TRUE;
					else if((history_type == HOST_HISTORY || history_type == SERVICE_HISTORY) && (history_detail_type & history_options))
						display_line = TRUE;
					else
						display_line = FALSE;
					}

				/* check alert state types */
				if(display_line == TRUE && (history_type == HOST_HISTORY || history_type == SERVICE_HISTORY)) {
					if(state_options == STATE_ALL)
						display_line = TRUE;
					else if((state_options & STATE_SOFT) && strstr(temp_buffer, ";SOFT;"))
						display_line = TRUE;
					else if((state_options & STATE_HARD) && strstr(temp_buffer, ";HARD;"))
						display_line = TRUE;
					else
						display_line = FALSE;
					}
				}

			else if(display_type == DISPLAY_SERVICES) {

				if(history_type == SERVICE_HISTORY)
					snprintf(match1, sizeof( match1), " SERVICE ALERT: %s;%s;", host_name, svc_description);
				else if(history_type == SERVICE_FLAPPING_HISTORY)
					snprintf(match1, sizeof( match1), " SERVICE FLAPPING ALERT: %s;%s;", host_name, svc_description);
				else if(history_type == SERVICE_DOWNTIME_HISTORY)
					snprintf(match1, sizeof( match1), " SERVICE DOWNTIME ALERT: %s;%s;", host_name, svc_description);

				if(strstr(temp_buffer, match1) && (history_type == SERVICE_HISTORY || history_type == SERVICE_FLAPPING_HISTORY || history_type == SERVICE_DOWNTIME_HISTORY))
					display_line = TRUE;

				if(display_line == TRUE) {
					if(history_options == HISTORY_ALL || history_options == HISTORY_SERVICE_ALL)
						display_line = TRUE;
					else if(history_options & history_detail_type)
						display_line = TRUE;
					else
						display_line = FALSE;
					}

				/* check alert state type */
				if(display_line == TRUE && history_type == SERVICE_HISTORY) {

					if(state_options == STATE_ALL)
						display_line = TRUE;
					else if((state_options & STATE_SOFT) && strstr(temp_buffer, ";SOFT;"))
						display_line = TRUE;
					else if((state_options & STATE_HARD) && strstr(temp_buffer, ";HARD;"))
						display_line = TRUE;
					else
						display_line = FALSE;
					}
				}


			/* make sure user is authorized to view this host or service information */
			if(system_message == FALSE) {

				if(history_type == HOST_HISTORY || history_type == HOST_FLAPPING_HISTORY || history_type == HOST_DOWNTIME_HISTORY) {
					temp_host = find_host(entry_host_name);
					if(is_authorized_for_host(temp_host, &current_authdata) == FALSE)
						display_line = FALSE;

					}
				else {
					temp_service = find_service(entry_host_name, entry_service_desc);
					if(is_authorized_for_service(temp_service, &current_authdata) == FALSE)
						display_line = FALSE;
					}
				}

			/* display the entry if we should... */
			if(display_line == TRUE) {

				if(strcmp(last_message_date, current_message_date) != 0 && display_timebreaks == TRUE) {
					printf("</DIV><BR CLEAR='all' />\n");
					printf("<DIV CLASS='dateTimeBreak'>\n");
					printf("<table border=0 width=95%%><tr>");
					printf("<td width=40%%><hr width=100%%></td>");
					printf("<td align=center CLASS='dateTimeBreak'>%s</td>", current_message_date);
					printf("<td width=40%%><hr width=100%%></td>");
					printf("</tr></table>\n");
					printf("</DIV>\n");
					printf("<BR CLEAR='all' /><DIV CLASS='logEntries'>\n");
					strncpy(last_message_date, current_message_date, sizeof(last_message_date));
					last_message_date[sizeof(last_message_date) - 1] = '\x0';
					}

				if(display_frills == TRUE)
					printf("<img align='left' src='%s%s' alt='%s' title='%s' />", url_images_path, image, image_alt, image_alt);
				printf("[%s] %s", date_time, html_encode(temp_buffer, FALSE));
				if(enable_splunk_integration == TRUE) {
					printf("&nbsp;&nbsp;&nbsp;");
					display_splunk_generic_url(temp_buffer, 2);
					}
				printf("<br clear='all' />\n");

				found_line = TRUE;
				}
			}

		/* free memory */
		free(entry_host_name);
		entry_host_name = NULL;
		free(entry_service_desc);
		entry_service_desc = NULL;
		}

	printf("</DIV></P>\n");

	if(found_line == FALSE) {
		printf("<HR>\n");
		printf("<P><DIV CLASS='warningMessage'>No history information was found ");
		if(display_type == DISPLAY_HOSTS)
			printf("%s", (show_all_hosts == TRUE) ? "" : "for this host ");
		else
			printf("for this service ");
		printf("in %s log file</DIV></P>", (log_archive == 0) ? "the current" : "this archived");
		}

	printf("<HR>\n");

	my_free(input);
	my_free(input2);

	if(use_lifo == TRUE)
		free_lifo_memory();
	else
		mmap_fclose(thefile);

	return;
	}
Ejemplo n.º 4
0
/* read all program, host, and service status information */
int xsddefault_read_status_data(char *config_file, int options) {
#ifdef NO_MMAP
	char input[MAX_PLUGIN_OUTPUT_LENGTH] = "";
	FILE *fp = NULL;
#else
	char *input = NULL;
	mmapfile *thefile = NULL;
#endif
	int data_type = XSDDEFAULT_NO_DATA;
	hoststatus *temp_hoststatus = NULL;
	servicestatus *temp_servicestatus = NULL;
	char *var = NULL;
	char *val = NULL;
	char *ptr = NULL;
	int result = 0;
	/* comment and downtime vars */
	unsigned long comment_id = 0;
	int persistent = FALSE;
	int expires = FALSE;
	time_t expire_time = 0L;
	int entry_type = USER_COMMENT;
	int source = COMMENTSOURCE_INTERNAL;
	time_t entry_time = 0L;
	char *host_name = NULL;
	char *service_description = NULL;
	char *author = NULL;
	char *comment_data = NULL;
	unsigned long downtime_id = 0;
	time_t start_time = 0L;
	time_t flex_downtime_start = 0L;
	time_t end_time = 0L;
	int fixed = FALSE;
	unsigned long triggered_by = 0;
	unsigned long duration = 0L;
	int x = 0;
	int is_in_effect = FALSE;


	/* initialize some vars */
	for(x = 0; x < MAX_CHECK_STATS_TYPES; x++) {
		program_stats[x][0] = 0;
		program_stats[x][1] = 0;
		program_stats[x][2] = 0;
		}

	/* grab configuration data */
	result = xsddefault_grab_config_info(config_file);
	if(result == ERROR)
		return ERROR;

	/* open the status file for reading */
#ifdef NO_MMAP
	if((fp = fopen(xsddefault_status_log, "r")) == NULL)
		return ERROR;
#else
	if((thefile = mmap_fopen(xsddefault_status_log)) == NULL)
		return ERROR;
#endif

	/* Big speedup when reading status.dat in bulk */
	defer_downtime_sorting = 1;
	defer_comment_sorting = 1;

	/* read all lines in the status file */
	while(1) {

#ifdef NO_MMAP
		strcpy(input, "");
		if(fgets(input, sizeof(input), fp) == NULL)
			break;
#else
		/* free memory */
		my_free(input);

		/* read the next line */
		if((input = mmap_fgets(thefile)) == NULL)
			break;
#endif

		strip(input);

		/* skip blank lines and comments */
		if(input[0] == '#' || input[0] == '\x0')
			continue;

		else if(!strcmp(input, "info {"))
			data_type = XSDDEFAULT_INFO_DATA;
		else if(!strcmp(input, "programstatus {"))
			data_type = XSDDEFAULT_PROGRAMSTATUS_DATA;
		else if(!strcmp(input, "hoststatus {")) {
			data_type = XSDDEFAULT_HOSTSTATUS_DATA;
			temp_hoststatus = (hoststatus *)calloc(1, sizeof(hoststatus));
			}
		else if(!strcmp(input, "servicestatus {")) {
			data_type = XSDDEFAULT_SERVICESTATUS_DATA;
			temp_servicestatus = (servicestatus *)calloc(1, sizeof(servicestatus));
			}
		else if(!strcmp(input, "contactstatus {")) {
			data_type = XSDDEFAULT_CONTACTSTATUS_DATA;
			/* unimplemented */
			}
		else if(!strcmp(input, "hostcomment {"))
			data_type = XSDDEFAULT_HOSTCOMMENT_DATA;
		else if(!strcmp(input, "servicecomment {"))
			data_type = XSDDEFAULT_SERVICECOMMENT_DATA;
		else if(!strcmp(input, "hostdowntime {"))
			data_type = XSDDEFAULT_HOSTDOWNTIME_DATA;
		else if(!strcmp(input, "servicedowntime {"))
			data_type = XSDDEFAULT_SERVICEDOWNTIME_DATA;

		else if(!strcmp(input, "}")) {

			switch(data_type) {

				case XSDDEFAULT_INFO_DATA:
					break;

				case XSDDEFAULT_PROGRAMSTATUS_DATA:
					break;

				case XSDDEFAULT_HOSTSTATUS_DATA:
					add_host_status(temp_hoststatus);
					temp_hoststatus = NULL;
					break;

				case XSDDEFAULT_SERVICESTATUS_DATA:
					add_service_status(temp_servicestatus);
					temp_servicestatus = NULL;
					break;

				case XSDDEFAULT_CONTACTSTATUS_DATA:
					/* unimplemented */
					break;

				case XSDDEFAULT_HOSTCOMMENT_DATA:
				case XSDDEFAULT_SERVICECOMMENT_DATA:

					/* add the comment */
					add_comment((data_type == XSDDEFAULT_HOSTCOMMENT_DATA) ? HOST_COMMENT : SERVICE_COMMENT, entry_type, host_name, service_description, entry_time, author, comment_data, comment_id, persistent, expires, expire_time, source);

					/* free temp memory */
					my_free(host_name);
					my_free(service_description);
					my_free(author);
					my_free(comment_data);

					/* reset defaults */
					entry_type = USER_COMMENT;
					comment_id = 0;
					source = COMMENTSOURCE_INTERNAL;
					persistent = FALSE;
					entry_time = 0L;
					expires = FALSE;
					expire_time = 0L;

					break;

				case XSDDEFAULT_HOSTDOWNTIME_DATA:
				case XSDDEFAULT_SERVICEDOWNTIME_DATA:

					/* add the downtime */
					if(data_type == XSDDEFAULT_HOSTDOWNTIME_DATA)
						add_host_downtime(host_name, entry_time, author, comment_data, start_time, flex_downtime_start, end_time, fixed, triggered_by, duration, downtime_id, is_in_effect);
					else
						add_service_downtime(host_name, service_description, entry_time, author, comment_data, start_time, flex_downtime_start, end_time, fixed, triggered_by, duration, downtime_id, is_in_effect);

					/* free temp memory */
					my_free(host_name);
					my_free(service_description);
					my_free(author);
					my_free(comment_data);

					/* reset defaults */
					downtime_id = 0;
					entry_time = 0L;
					start_time = 0L;
					end_time = 0L;
					fixed = FALSE;
					triggered_by = 0;
					duration = 0L;
					is_in_effect = FALSE;

					break;

				default:
					break;
				}

			data_type = XSDDEFAULT_NO_DATA;
			}

		else if(data_type != XSDDEFAULT_NO_DATA) {

			var = strtok(input, "=");
			val = strtok(NULL, "\n");
			if(val == NULL)
				continue;

			switch(data_type) {

				case XSDDEFAULT_INFO_DATA:
					break;

				case XSDDEFAULT_PROGRAMSTATUS_DATA:
					/* NOTE: some vars are not read, as they are not used by the CGIs (modified attributes, event handler commands, etc.) */
					if(!strcmp(var, "nagios_pid"))
						nagios_pid = atoi(val);
					else if(!strcmp(var, "daemon_mode"))
						daemon_mode = (atoi(val) > 0) ? TRUE : FALSE;
					else if(!strcmp(var, "program_start"))
						program_start = strtoul(val, NULL, 10);
					else if(!strcmp(var, "last_log_rotation"))
						last_log_rotation = strtoul(val, NULL, 10);
					else if(!strcmp(var, "enable_notifications"))
						enable_notifications = (atoi(val) > 0) ? TRUE : FALSE;
					else if(!strcmp(var, "active_service_checks_enabled"))
						execute_service_checks = (atoi(val) > 0) ? TRUE : FALSE;
					else if(!strcmp(var, "passive_service_checks_enabled"))
						accept_passive_service_checks = (atoi(val) > 0) ? TRUE : FALSE;
					else if(!strcmp(var, "active_host_checks_enabled"))
						execute_host_checks = (atoi(val) > 0) ? TRUE : FALSE;
					else if(!strcmp(var, "passive_host_checks_enabled"))
						accept_passive_host_checks = (atoi(val) > 0) ? TRUE : FALSE;
					else if(!strcmp(var, "enable_event_handlers"))
						enable_event_handlers = (atoi(val) > 0) ? TRUE : FALSE;
					else if(!strcmp(var, "obsess_over_services"))
						obsess_over_services = (atoi(val) > 0) ? TRUE : FALSE;
					else if(!strcmp(var, "obsess_over_hosts"))
						obsess_over_hosts = (atoi(val) > 0) ? TRUE : FALSE;
					else if(!strcmp(var, "check_service_freshness"))
						check_service_freshness = (atoi(val) > 0) ? TRUE : FALSE;
					else if(!strcmp(var, "check_host_freshness"))
						check_host_freshness = (atoi(val) > 0) ? TRUE : FALSE;
					else if(!strcmp(var, "enable_flap_detection"))
						enable_flap_detection = (atoi(val) > 0) ? TRUE : FALSE;
					else if(!strcmp(var, "process_performance_data"))
						process_performance_data = (atoi(val) > 0) ? TRUE : FALSE;

					else if(strstr(var, "_stats")) {

						x = -1;
						if(!strcmp(var, "active_scheduled_host_check_stats"))
							x = ACTIVE_SCHEDULED_HOST_CHECK_STATS;
						if(!strcmp(var, "active_ondemand_host_check_stats"))
							x = ACTIVE_ONDEMAND_HOST_CHECK_STATS;
						if(!strcmp(var, "passive_host_check_stats"))
							x = PASSIVE_HOST_CHECK_STATS;
						if(!strcmp(var, "active_scheduled_service_check_stats"))
							x = ACTIVE_SCHEDULED_SERVICE_CHECK_STATS;
						if(!strcmp(var, "active_ondemand_service_check_stats"))
							x = ACTIVE_ONDEMAND_SERVICE_CHECK_STATS;
						if(!strcmp(var, "passive_service_check_stats"))
							x = PASSIVE_SERVICE_CHECK_STATS;
						if(!strcmp(var, "cached_host_check_stats"))
							x = ACTIVE_CACHED_HOST_CHECK_STATS;
						if(!strcmp(var, "cached_service_check_stats"))
							x = ACTIVE_CACHED_SERVICE_CHECK_STATS;
						if(!strcmp(var, "external_command_stats"))
							x = EXTERNAL_COMMAND_STATS;
						if(!strcmp(var, "parallel_host_check_stats"))
							x = PARALLEL_HOST_CHECK_STATS;
						if(!strcmp(var, "serial_host_check_stats"))
							x = SERIAL_HOST_CHECK_STATS;

						if(x >= 0) {
							if((ptr = strtok(val, ","))) {
								program_stats[x][0] = atoi(ptr);
								if((ptr = strtok(NULL, ","))) {
									program_stats[x][1] = atoi(ptr);
									if((ptr = strtok(NULL, "\n")))
										program_stats[x][2] = atoi(ptr);
									}
								}
							}
						}
					break;

				case XSDDEFAULT_HOSTSTATUS_DATA:
					/* NOTE: some vars are not read, as they are not used by the CGIs (modified attributes, event handler commands, etc.) */
					if(temp_hoststatus != NULL) {
						if(!strcmp(var, "host_name"))
							temp_hoststatus->host_name = (char *)strdup(val);
						else if(!strcmp(var, "has_been_checked"))
							temp_hoststatus->has_been_checked = (atoi(val) > 0) ? TRUE : FALSE;
						else if(!strcmp(var, "should_be_scheduled"))
							temp_hoststatus->should_be_scheduled = (atoi(val) > 0) ? TRUE : FALSE;
						else if(!strcmp(var, "check_execution_time"))
							temp_hoststatus->execution_time = strtod(val, NULL);
						else if(!strcmp(var, "check_latency"))
							temp_hoststatus->latency = strtod(val, NULL);
						else if(!strcmp(var, "check_type"))
							temp_hoststatus->check_type = atoi(val);
						else if(!strcmp(var, "current_state"))
							temp_hoststatus->status = atoi(val);
						else if(!strcmp(var, "last_hard_state"))
							temp_hoststatus->last_hard_state = atoi(val);
						else if(!strcmp(var, "plugin_output")) {
							temp_hoststatus->plugin_output = (char *)strdup(val);
							unescape_newlines(temp_hoststatus->plugin_output);
							}
						else if(!strcmp(var, "long_plugin_output")) {
							temp_hoststatus->long_plugin_output = (char *)strdup(val);
							unescape_newlines(temp_hoststatus->long_plugin_output);
							}
						else if(!strcmp(var, "performance_data"))
							temp_hoststatus->perf_data = (char *)strdup(val);
						else if(!strcmp(var, "current_attempt"))
							temp_hoststatus->current_attempt = atoi(val);
						else if(!strcmp(var, "max_attempts"))
							temp_hoststatus->max_attempts = atoi(val);
						else if(!strcmp(var, "last_check"))
							temp_hoststatus->last_check = strtoul(val, NULL, 10);
						else if(!strcmp(var, "next_check"))
							temp_hoststatus->next_check = strtoul(val, NULL, 10);
						else if(!strcmp(var, "check_options"))
							temp_hoststatus->check_options = atoi(val);
						else if(!strcmp(var, "current_attempt"))
							temp_hoststatus->current_attempt = (atoi(val) > 0) ? TRUE : FALSE;
						else if(!strcmp(var, "state_type"))
							temp_hoststatus->state_type = atoi(val);
						else if(!strcmp(var, "last_state_change"))
							temp_hoststatus->last_state_change = strtoul(val, NULL, 10);
						else if(!strcmp(var, "last_hard_state_change"))
							temp_hoststatus->last_hard_state_change = strtoul(val, NULL, 10);
						else if(!strcmp(var, "last_time_up"))
							temp_hoststatus->last_time_up = strtoul(val, NULL, 10);
						else if(!strcmp(var, "last_time_down"))
							temp_hoststatus->last_time_down = strtoul(val, NULL, 10);
						else if(!strcmp(var, "last_time_unreachable"))
							temp_hoststatus->last_time_unreachable = strtoul(val, NULL, 10);
						else if(!strcmp(var, "last_notification"))
							temp_hoststatus->last_notification = strtoul(val, NULL, 10);
						else if(!strcmp(var, "next_notification"))
							temp_hoststatus->next_notification = strtoul(val, NULL, 10);
						else if(!strcmp(var, "no_more_notifications"))
							temp_hoststatus->no_more_notifications = (atoi(val) > 0) ? TRUE : FALSE;
						else if(!strcmp(var, "current_notification_number"))
							temp_hoststatus->current_notification_number = atoi(val);
						else if(!strcmp(var, "notifications_enabled"))
							temp_hoststatus->notifications_enabled = (atoi(val) > 0) ? TRUE : FALSE;
						else if(!strcmp(var, "problem_has_been_acknowledged"))
							temp_hoststatus->problem_has_been_acknowledged = (atoi(val) > 0) ? TRUE : FALSE;
						else if(!strcmp(var, "acknowledgement_type"))
							temp_hoststatus->acknowledgement_type = atoi(val);
						else if(!strcmp(var, "active_checks_enabled"))
							temp_hoststatus->checks_enabled = (atoi(val) > 0) ? TRUE : FALSE;
						else if(!strcmp(var, "passive_checks_enabled"))
							temp_hoststatus->accept_passive_checks = (atoi(val) > 0) ? TRUE : FALSE;
						else if(!strcmp(var, "event_handler_enabled"))
							temp_hoststatus->event_handler_enabled = (atoi(val) > 0) ? TRUE : FALSE;
						else if(!strcmp(var, "flap_detection_enabled"))
							temp_hoststatus->flap_detection_enabled = (atoi(val) > 0) ? TRUE : FALSE;
						else if(!strcmp(var, "process_performance_data"))
							temp_hoststatus->process_performance_data = (atoi(val) > 0) ? TRUE : FALSE;
						else if(!strcmp(var, "obsess_over_host") || !strcmp(var, "obsess"))
							temp_hoststatus->obsess = (atoi(val) > 0) ? TRUE : FALSE;
						else if(!strcmp(var, "last_update"))
							temp_hoststatus->last_update = strtoul(val, NULL, 10);
						else if(!strcmp(var, "is_flapping"))
							temp_hoststatus->is_flapping = (atoi(val) > 0) ? TRUE : FALSE;
						else if(!strcmp(var, "percent_state_change"))
							temp_hoststatus->percent_state_change = strtod(val, NULL);
						else if(!strcmp(var, "scheduled_downtime_depth"))
							temp_hoststatus->scheduled_downtime_depth = atoi(val);
						}
					break;

				case XSDDEFAULT_SERVICESTATUS_DATA:
					/* NOTE: some vars are not read, as they are not used by the CGIs (modified attributes, event handler commands, etc.) */
					if(temp_servicestatus != NULL) {
						if(!strcmp(var, "host_name"))
							temp_servicestatus->host_name = (char *)strdup(val);
						else if(!strcmp(var, "service_description"))
							temp_servicestatus->description = (char *)strdup(val);
						else if(!strcmp(var, "has_been_checked"))
							temp_servicestatus->has_been_checked = (atoi(val) > 0) ? TRUE : FALSE;
						else if(!strcmp(var, "should_be_scheduled"))
							temp_servicestatus->should_be_scheduled = (atoi(val) > 0) ? TRUE : FALSE;
						else if(!strcmp(var, "check_execution_time"))
							temp_servicestatus->execution_time = strtod(val, NULL);
						else if(!strcmp(var, "check_latency"))
							temp_servicestatus->latency = strtod(val, NULL);
						else if(!strcmp(var, "check_type"))
							temp_servicestatus->check_type = atoi(val);
						else if(!strcmp(var, "current_state"))
							temp_servicestatus->status = atoi(val);
						else if(!strcmp(var, "last_hard_state"))
							temp_servicestatus->last_hard_state = atoi(val);
						else if(!strcmp(var, "current_attempt"))
							temp_servicestatus->current_attempt = atoi(val);
						else if(!strcmp(var, "max_attempts"))
							temp_servicestatus->max_attempts = atoi(val);
						else if(!strcmp(var, "state_type"))
							temp_servicestatus->state_type = atoi(val);
						else if(!strcmp(var, "last_state_change"))
							temp_servicestatus->last_state_change = strtoul(val, NULL, 10);
						else if(!strcmp(var, "last_hard_state_change"))
							temp_servicestatus->last_hard_state_change = strtoul(val, NULL, 10);
						else if(!strcmp(var, "last_time_ok"))
							temp_servicestatus->last_time_ok = strtoul(val, NULL, 10);
						else if(!strcmp(var, "last_time_warning"))
							temp_servicestatus->last_time_warning = strtoul(val, NULL, 10);
						else if(!strcmp(var, "last_time_unknown"))
							temp_servicestatus->last_time_unknown = strtoul(val, NULL, 10);
						else if(!strcmp(var, "last_time_critical"))
							temp_servicestatus->last_time_critical = strtoul(val, NULL, 10);
						else if(!strcmp(var, "plugin_output")) {
							temp_servicestatus->plugin_output = (char *)strdup(val);
							unescape_newlines(temp_servicestatus->plugin_output);
							}
						else if(!strcmp(var, "long_plugin_output")) {
							temp_servicestatus->long_plugin_output = (char *)strdup(val);
							unescape_newlines(temp_servicestatus->long_plugin_output);
							}
						else if(!strcmp(var, "performance_data"))
							temp_servicestatus->perf_data = (char *)strdup(val);
						else if(!strcmp(var, "last_check"))
							temp_servicestatus->last_check = strtoul(val, NULL, 10);
						else if(!strcmp(var, "next_check"))
							temp_servicestatus->next_check = strtoul(val, NULL, 10);
						else if(!strcmp(var, "check_options"))
							temp_servicestatus->check_options = atoi(val);
						else if(!strcmp(var, "current_notification_number"))
							temp_servicestatus->current_notification_number = atoi(val);
						else if(!strcmp(var, "last_notification"))
							temp_servicestatus->last_notification = strtoul(val, NULL, 10);
						else if(!strcmp(var, "next_notification"))
							temp_servicestatus->next_notification = strtoul(val, NULL, 10);
						else if(!strcmp(var, "no_more_notifications"))
							temp_servicestatus->no_more_notifications = (atoi(val) > 0) ? TRUE : FALSE;
						else if(!strcmp(var, "notifications_enabled"))
							temp_servicestatus->notifications_enabled = (atoi(val) > 0) ? TRUE : FALSE;
						else if(!strcmp(var, "active_checks_enabled"))
							temp_servicestatus->checks_enabled = (atoi(val) > 0) ? TRUE : FALSE;
						else if(!strcmp(var, "passive_checks_enabled"))
							temp_servicestatus->accept_passive_checks = (atoi(val) > 0) ? TRUE : FALSE;
						else if(!strcmp(var, "event_handler_enabled"))
							temp_servicestatus->event_handler_enabled = (atoi(val) > 0) ? TRUE : FALSE;
						else if(!strcmp(var, "problem_has_been_acknowledged"))
							temp_servicestatus->problem_has_been_acknowledged = (atoi(val) > 0) ? TRUE : FALSE;
						else if(!strcmp(var, "acknowledgement_type"))
							temp_servicestatus->acknowledgement_type = atoi(val);
						else if(!strcmp(var, "flap_detection_enabled"))
							temp_servicestatus->flap_detection_enabled = (atoi(val) > 0) ? TRUE : FALSE;
						else if(!strcmp(var, "process_performance_data"))
							temp_servicestatus->process_performance_data = (atoi(val) > 0) ? TRUE : FALSE;
						else if(!strcmp(var, "obsess_over_service") || !strcmp(var, "obsess"))
							temp_servicestatus->obsess = (atoi(val) > 0) ? TRUE : FALSE;
						else if(!strcmp(var, "last_update"))
							temp_servicestatus->last_update = strtoul(val, NULL, 10);
						else if(!strcmp(var, "is_flapping"))
							temp_servicestatus->is_flapping = (atoi(val) > 0) ? TRUE : FALSE;
						else if(!strcmp(var, "percent_state_change"))
							temp_servicestatus->percent_state_change = strtod(val, NULL);
						else if(!strcmp(var, "scheduled_downtime_depth"))
							temp_servicestatus->scheduled_downtime_depth = atoi(val);
						}
					break;

				case XSDDEFAULT_CONTACTSTATUS_DATA:
					/* unimplemented */
					break;

				case XSDDEFAULT_HOSTCOMMENT_DATA:
				case XSDDEFAULT_SERVICECOMMENT_DATA:
					if(!strcmp(var, "host_name"))
						host_name = (char *)strdup(val);
					else if(!strcmp(var, "service_description"))
						service_description = (char *)strdup(val);
					else if(!strcmp(var, "entry_type"))
						entry_type = atoi(val);
					else if(!strcmp(var, "comment_id"))
						comment_id = strtoul(val, NULL, 10);
					else if(!strcmp(var, "source"))
						source = atoi(val);
					else if(!strcmp(var, "persistent"))
						persistent = (atoi(val) > 0) ? TRUE : FALSE;
					else if(!strcmp(var, "entry_time"))
						entry_time = strtoul(val, NULL, 10);
					else if(!strcmp(var, "expires"))
						expires = (atoi(val) > 0) ? TRUE : FALSE;
					else if(!strcmp(var, "expire_time"))
						expire_time = strtoul(val, NULL, 10);
					else if(!strcmp(var, "author"))
						author = (char *)strdup(val);
					else if(!strcmp(var, "comment_data"))
						comment_data = (char *)strdup(val);
					break;

				case XSDDEFAULT_HOSTDOWNTIME_DATA:
				case XSDDEFAULT_SERVICEDOWNTIME_DATA:
					if(!strcmp(var, "host_name"))
						host_name = (char *)strdup(val);
					else if(!strcmp(var, "service_description"))
						service_description = (char *)strdup(val);
					else if(!strcmp(var, "downtime_id"))
						downtime_id = strtoul(val, NULL, 10);
					else if(!strcmp(var, "comment_id"))
						comment_id = strtoul(val, NULL, 10);
					else if(!strcmp(var, "entry_time"))
						entry_time = strtoul(val, NULL, 10);
					else if(!strcmp(var, "start_time"))
						start_time = strtoul(val, NULL, 10);
					else if(!strcmp(var, "flex_downtime_start"))
						flex_downtime_start = strtoul(val, NULL, 10);
					else if(!strcmp(var, "end_time"))
						end_time = strtoul(val, NULL, 10);
					else if(!strcmp(var, "fixed"))
						fixed = (atoi(val) > 0) ? TRUE : FALSE;
					else if(!strcmp(var, "triggered_by"))
						triggered_by = strtoul(val, NULL, 10);
					else if(!strcmp(var, "duration"))
						duration = strtoul(val, NULL, 10);
					else if(!strcmp(var, "is_in_effect"))
						is_in_effect = (atoi(val) > 0) ? TRUE : FALSE;
					else if(!strcmp(var, "author"))
						author = (char *)strdup(val);
					else if(!strcmp(var, "comment"))
						comment_data = (char *)strdup(val);
					break;

				default:
					break;
				}

			}
		}

	/* free memory and close the file */
#ifdef NO_MMAP
	fclose(fp);
#else
	my_free(input);
	mmap_fclose(thefile);
#endif

	/* free memory */
	my_free(xsddefault_status_log);
	my_free(xsddefault_temp_file);

	if(sort_downtime() != OK)
		return ERROR;
	if(sort_comments() != OK)
		return ERROR;

	return OK;
	}
Ejemplo n.º 5
0
void display_notifications(void) {
    mmapfile *thefile=NULL;
    char *input=NULL;
    char *temp_buffer;
    char date_time[MAX_DATETIME_LENGTH];
    char alert_level[MAX_INPUT_BUFFER];
    char alert_level_class[MAX_INPUT_BUFFER];
    char contact_name[MAX_INPUT_BUFFER];
    char service_name[MAX_INPUT_BUFFER];
    char host_name[MAX_INPUT_BUFFER];
    char method_name[MAX_INPUT_BUFFER];
    int show_entry;
    int total_notifications;
    int notification_type=SERVICE_NOTIFICATION;
    int notification_detail_type=NOTIFICATION_SERVICE_CRITICAL;
    int odd=0;
    time_t t;
    host *temp_host;
    service *temp_service;
    int result;

    if(use_lifo==TRUE) {
        result=read_file_into_lifo(log_file_to_use);
        if(result!=LIFO_OK) {
            if(result==LIFO_ERROR_MEMORY) {
                printf("<P><DIV CLASS='warningMessage'>Not enough memory to reverse log file - displaying notifications in natural order...</DIV></P>");
            }
            else if(result==LIFO_ERROR_FILE) {
                printf("<P><DIV CLASS='errorMessage'>Error: Cannot open log file '%s' for reading!</DIV></P>",log_file_to_use);
                return;
            }
            use_lifo=FALSE;
        }
    }

    if(use_lifo==FALSE) {

        if((thefile=mmap_fopen(log_file_to_use))==NULL) {
            printf("<P><DIV CLASS='errorMessage'>Error: Cannot open log file '%s' for reading!</DIV></P>",log_file_to_use);
            return;
        }
    }

    printf("<p>\n");
    printf("<div align='center'>\n");

    printf("<table border=0 CLASS='notifications'>\n");
    printf("<tr>\n");
    printf("<th CLASS='notifications'>Host</th>\n");
    printf("<th CLASS='notifications'>Service</th>\n");
    printf("<th CLASS='notifications'>Type</th>\n");
    printf("<th CLASS='notifications'>Time</th>\n");
    printf("<th CLASS='notifications'>Contact</th>\n");
    printf("<th CLASS='notifications'>Notification Command</th>\n");
    printf("<th CLASS='notifications'>Information</th>\n");
    printf("</tr>\n");

    total_notifications=0;

    while(1) {

        free(input);

        if(use_lifo==TRUE) {
            if((input=pop_lifo())==NULL)
                break;
        }
        else {
            if((input=mmap_fgets(thefile))==NULL)
                break;
        }

        strip(input);

        /* see if this line contains the notification event string */
        if(strstr(input,HOST_NOTIFICATION_STRING)||strstr(input,SERVICE_NOTIFICATION_STRING)) {

            if(strstr(input,HOST_NOTIFICATION_STRING))
                notification_type=HOST_NOTIFICATION;
            else
                notification_type=SERVICE_NOTIFICATION;

            /* get the date/time */
            temp_buffer=(char *)strtok(input,"]");
            t=(time_t)(temp_buffer==NULL)?0L:strtoul(temp_buffer+1,NULL,10);
            get_time_string(&t,date_time,(int)sizeof(date_time),SHORT_DATE_TIME);
            strip(date_time);

            /* get the contact name */
            temp_buffer=(char *)strtok(NULL,":");
            temp_buffer=(char *)strtok(NULL,";");
            snprintf(contact_name,sizeof(contact_name),"%s",(temp_buffer==NULL)?"":temp_buffer+1);
            contact_name[sizeof(contact_name)-1]='\x0';

            /* get the host name */
            temp_buffer=(char *)strtok(NULL,";");
            snprintf(host_name,sizeof(host_name),"%s",(temp_buffer==NULL)?"":temp_buffer);
            host_name[sizeof(host_name)-1]='\x0';

            /* get the service name */
            if(notification_type==SERVICE_NOTIFICATION) {
                temp_buffer=(char *)strtok(NULL,";");
                snprintf(service_name,sizeof(service_name),"%s",(temp_buffer==NULL)?"":temp_buffer);
                service_name[sizeof(service_name)-1]='\x0';
            }

            /* get the alert level */
            temp_buffer=(char *)strtok(NULL,";");
            snprintf(alert_level,sizeof(alert_level),"%s",(temp_buffer==NULL)?"":temp_buffer);
            alert_level[sizeof(alert_level)-1]='\x0';

            if(notification_type==SERVICE_NOTIFICATION) {

                if(!strcmp(alert_level,"CRITICAL")) {
                    notification_detail_type=NOTIFICATION_SERVICE_CRITICAL;
                    strcpy(alert_level_class,"CRITICAL");
                }
                else if(!strcmp(alert_level,"WARNING")) {
                    notification_detail_type=NOTIFICATION_SERVICE_WARNING;
                    strcpy(alert_level_class,"WARNING");
                }
                else if(!strcmp(alert_level,"RECOVERY") || !strcmp(alert_level,"OK")) {
                    strcpy(alert_level,"OK");
                    notification_detail_type=NOTIFICATION_SERVICE_RECOVERY;
                    strcpy(alert_level_class,"OK");
                }
                else if(strstr(alert_level,"CUSTOM (")) {
                    notification_detail_type=NOTIFICATION_SERVICE_CUSTOM;
                    strcpy(alert_level_class,"CUSTOM");
                }
                else if(strstr(alert_level,"ACKNOWLEDGEMENT (")) {
                    notification_detail_type=NOTIFICATION_SERVICE_ACK;
                    strcpy(alert_level_class,"ACKNOWLEDGEMENT");
                }
                else if(strstr(alert_level,"FLAPPINGSTART (")) {
                    strcpy(alert_level,"FLAPPING START");
                    notification_detail_type=NOTIFICATION_SERVICE_FLAP;
                    strcpy(alert_level_class,"UNKNOWN");
                }
                else if(strstr(alert_level,"FLAPPINGSTOP (")) {
                    strcpy(alert_level,"FLAPPING STOP");
                    notification_detail_type=NOTIFICATION_SERVICE_FLAP;
                    strcpy(alert_level_class,"UNKNOWN");
                }
                else {
                    strcpy(alert_level,"UNKNOWN");
                    notification_detail_type=NOTIFICATION_SERVICE_UNKNOWN;
                    strcpy(alert_level_class,"UNKNOWN");
                }
            }

            else {

                if(!strcmp(alert_level,"DOWN")) {
                    strncpy(alert_level,"HOST DOWN",sizeof(alert_level));
                    strcpy(alert_level_class,"HOSTDOWN");
                    notification_detail_type=NOTIFICATION_HOST_DOWN;
                }
                else if(!strcmp(alert_level,"UNREACHABLE")) {
                    strncpy(alert_level,"HOST UNREACHABLE",sizeof(alert_level));
                    strcpy(alert_level_class,"HOSTUNREACHABLE");
                    notification_detail_type=NOTIFICATION_HOST_UNREACHABLE;
                }
                else if(!strcmp(alert_level,"RECOVERY") || !strcmp(alert_level,"UP")) {
                    strncpy(alert_level,"HOST UP",sizeof(alert_level));
                    strcpy(alert_level_class,"HOSTUP");
                    notification_detail_type=NOTIFICATION_HOST_RECOVERY;
                }
                else if(strstr(alert_level,"CUSTOM (")) {
                    strcpy(alert_level_class,"HOSTCUSTOM");
                    notification_detail_type=NOTIFICATION_HOST_CUSTOM;
                }
                else if(strstr(alert_level,"ACKNOWLEDGEMENT (")) {
                    strcpy(alert_level_class,"HOSTACKNOWLEDGEMENT");
                    notification_detail_type=NOTIFICATION_HOST_ACK;
                }
                else if(strstr(alert_level,"FLAPPINGSTART (")) {
                    strcpy(alert_level,"FLAPPING START");
                    strcpy(alert_level_class,"UNKNOWN");
                    notification_detail_type=NOTIFICATION_HOST_FLAP;
                }
                else if(strstr(alert_level,"FLAPPINGSTOP (")) {
                    strcpy(alert_level,"FLAPPING STOP");
                    strcpy(alert_level_class,"UNKNOWN");
                    notification_detail_type=NOTIFICATION_HOST_FLAP;
                }
            }

            /* get the method name */
            temp_buffer=(char *)strtok(NULL,";");
            snprintf(method_name,sizeof(method_name),"%s",(temp_buffer==NULL)?"":temp_buffer);
            method_name[sizeof(method_name)-1]='\x0';

            /* move to the informational message */
            temp_buffer=strtok(NULL,";");

            show_entry=FALSE;

            /* if we're searching by contact, filter out unwanted contact */
            if(query_type==FIND_CONTACT) {
                if(find_all==TRUE)
                    show_entry=TRUE;
                else if(!strcmp(query_contact_name,contact_name))
                    show_entry=TRUE;
            }

            else if(query_type==FIND_HOST) {
                if(find_all==TRUE)
                    show_entry=TRUE;
                else if(!strcmp(query_host_name,host_name))
                    show_entry=TRUE;
            }

            else if(query_type==FIND_SERVICE) {
                if(!strcmp(query_host_name,host_name) && !strcmp(query_svc_description,service_name))
                    show_entry=TRUE;
            }

            if(show_entry==TRUE) {
                if(notification_options==NOTIFICATION_ALL)
                    show_entry=TRUE;
                else if(notification_options==NOTIFICATION_HOST_ALL && notification_type==HOST_NOTIFICATION)
                    show_entry=TRUE;
                else if(notification_options==NOTIFICATION_SERVICE_ALL && notification_type==SERVICE_NOTIFICATION)
                    show_entry=TRUE;
                else if(notification_detail_type & notification_options)
                    show_entry=TRUE;
                else
                    show_entry=FALSE;
            }

            /* make sure user has authorization to view this notification */
            if(notification_type==HOST_NOTIFICATION) {
                temp_host=find_host(host_name);
                if(is_authorized_for_host(temp_host,&current_authdata)==FALSE)
                    show_entry=FALSE;
            }
            else {
                temp_service=find_service(host_name,service_name);
                if(is_authorized_for_service(temp_service,&current_authdata)==FALSE)
                    show_entry=FALSE;
            }

            if(show_entry==TRUE) {

                total_notifications++;

                if(odd) {
                    odd=0;
                    printf("<tr CLASS='notificationsOdd'>\n");
                }
                else {
                    odd=1;
                    printf("<tr CLASS='notificationsEven'>\n");
                }
                printf("<td CLASS='notifications%s'><a href='%s?type=%d&host=%s'>%s</a></td>\n",(odd)?"Even":"Odd",EXTINFO_CGI,DISPLAY_HOST_INFO,url_encode(host_name),host_name);
                if(notification_type==SERVICE_NOTIFICATION) {
                    printf("<td CLASS='notifications%s'><a href='%s?type=%d&host=%s",(odd)?"Even":"Odd",EXTINFO_CGI,DISPLAY_SERVICE_INFO,url_encode(host_name));
                    printf("&service=%s'>%s</a></td>\n",url_encode(service_name),service_name);
                }
                else
                    printf("<td CLASS='notifications%s'>N/A</td>\n",(odd)?"Even":"Odd");
                printf("<td CLASS='notifications%s'>%s</td>\n",alert_level_class,alert_level);
                printf("<td CLASS='notifications%s'>%s</td>\n",(odd)?"Even":"Odd",date_time);
                printf("<td CLASS='notifications%s'><a href='%s?type=contacts#%s'>%s</a></td>\n",(odd)?"Even":"Odd",CONFIG_CGI,url_encode(contact_name),contact_name);
                printf("<td CLASS='notifications%s'><a href='%s?type=commands#%s'>%s</a></td>\n",(odd)?"Even":"Odd",CONFIG_CGI,url_encode(method_name),method_name);
                printf("<td CLASS='notifications%s'>%s</td>\n",(odd)?"Even":"Odd",html_encode(temp_buffer,FALSE));
                printf("</tr>\n");
            }
        }
    }


    printf("</table>\n");

    printf("</div>\n");
    printf("</p>\n");

    if(total_notifications==0) {
        printf("<P><DIV CLASS='errorMessage'>No notifications have been recorded");
        if(find_all==FALSE) {
            if(query_type==FIND_SERVICE)
                printf(" for this service");
            else if(query_type==FIND_CONTACT)
                printf(" for this contact");
            else
                printf(" for this host");
        }
        printf(" in %s log file</DIV></P>",(log_archive==0)?"the current":"this archived");
    }

    free(input);

    if(use_lifo==TRUE)
        free_lifo_memory();
    else
        mmap_fclose(thefile);

    return;
}
Ejemplo n.º 6
0
/** @brief Read's a defined log file and stores the entries into entry list struct
 *  @param [in] log_file the full path of the log file to read
 *  @param [in] search_string a string you are searching for.
 *		Set to NULL to disable search function
 *  @param [in] reverse this bool defines which order the log entries should return
 *  @param [in] ts_start defines the start timestamp for log entries
 *	@arg >=0 means unix timestamp
 *	@arg -1 deactivated
 *  @param [in] ts_end defines the end timestamp for log entries
 *	@arg >=0 means unix timestamp
 *	@arg -1 deactivated
 *  @return
 *	@retval READLOG_OK
 *	@retval READLOG_ERROR
 *	@retval READLOG_ERROR_MEMORY
 *	@retval READLOG_ERROR_NOFILE
 *	@retval READLOG_ERROR_FILTER
 *  @author Ricardo Bartels
 *
 *  This functions reads a  \c log_file and and try's (if set) to filter for a search string.
 *  This search string uses regular expressions. The reverse option defines if you want
 *  have your log entries returned in normal or revers order. Normal order for returning
 *  would be from the newest entry to the oldest. You can also set a time "window". This
 *  defines if you want to exclude entries which are outside of these "window". Then only
 *  entries will be returned which are between start and end. Very useful if user has all
 *  entries in one log file.
**/
int get_log_entries(char *log_file, char *search_string, int reverse, time_t ts_start, time_t ts_end) {
	char *input = NULL;
	char *temp_buffer = NULL;
	char *search_regex = NULL;
	int type = 0;
	int regex_i = 0, i = 0, len = 0;
	short keep_entry = TRUE;
	time_t timestamp;
	mmapfile *thefile = NULL;
	logentry *temp_entry = NULL;
	regex_t preg;
	logfilter *temp_filter;

	if ((thefile = mmap_fopen(log_file)) != NULL) {

		if (search_string != NULL) {
			/* allocate for 3 extra chars, ^, $ and \0 */
			search_regex = malloc(sizeof(char) * (strlen(search_string) * 2 + 3));
			len = strlen(search_string);
			for (i = 0; i < len; i++, regex_i++) {
				if (search_string[i] == '*') {
					search_regex[regex_i++] = '.';
					search_regex[regex_i] = '*';
				} else
					search_regex[regex_i] = search_string[i];
			}
			//search_regex[0]='^';
			//search_regex[regex_i++]='$';

			search_regex[regex_i] = '\0';

			/* check and compile regex */
			if (regcomp(&preg, search_regex, REG_ICASE | REG_NOSUB) != 0) {
				regfree(&preg);
				mmap_fclose(thefile);
				return READLOG_ERROR_FILTER;
			}
		}

		while (1) {

			/* free memory */
			my_free(input);

			if ((input = mmap_fgets(thefile)) == NULL)
				break;

			strip(input);

			if ((int)strlen(input) == 0)
				continue;

			/* get timestamp */
			temp_buffer = strtok(input, "]");
			timestamp = (temp_buffer == NULL) ? 0L : strtoul(temp_buffer + 1, NULL, 10);

			/* skip line if out of time range */
			if ((ts_start >= 0 && timestamp < ts_start) || (ts_end >= 0 && timestamp > ts_end))
				continue;

			/* get log entry text */
			temp_buffer = strtok(NULL, "\n");

			if (search_string != NULL) {
				if (regexec(&preg, temp_buffer, 0, NULL, 0) == REG_NOMATCH)
					continue;
			}


			if (strstr(temp_buffer, " starting..."))
				type = LOGENTRY_STARTUP;
			else if (strstr(temp_buffer, " shutting down..."))
				type = LOGENTRY_SHUTDOWN;
			else if (strstr(temp_buffer, "Bailing out"))
				type = LOGENTRY_BAILOUT;
			else if (strstr(temp_buffer, " restarting..."))
				type = LOGENTRY_RESTART;
			else if (strstr(temp_buffer, "HOST ALERT:") && strstr(temp_buffer, ";DOWN;"))
				type = LOGENTRY_HOST_DOWN;
			else if (strstr(temp_buffer, "HOST ALERT:") && strstr(temp_buffer, ";UNREACHABLE;"))
				type = LOGENTRY_HOST_UNREACHABLE;
			else if (strstr(temp_buffer, "HOST ALERT:") && strstr(temp_buffer, ";RECOVERY;"))
				type = LOGENTRY_HOST_RECOVERY;
			else if (strstr(temp_buffer, "HOST ALERT:") && strstr(temp_buffer, ";UP;"))
				type = LOGENTRY_HOST_UP;
			else if (strstr(temp_buffer, "HOST NOTIFICATION:"))
				type = LOGENTRY_HOST_NOTIFICATION;
			else if (strstr(temp_buffer, "SERVICE ALERT:") && strstr(temp_buffer, ";CRITICAL;"))
				type = LOGENTRY_SERVICE_CRITICAL;
			else if (strstr(temp_buffer, "SERVICE ALERT:") && strstr(temp_buffer, ";WARNING;"))
				type = LOGENTRY_SERVICE_WARNING;
			else if (strstr(temp_buffer, "SERVICE ALERT:") && strstr(temp_buffer, ";UNKNOWN;"))
				type = LOGENTRY_SERVICE_UNKNOWN;
			else if (strstr(temp_buffer, "SERVICE ALERT:") && strstr(temp_buffer, ";RECOVERY;"))
				type = LOGENTRY_SERVICE_RECOVERY;
			else if (strstr(temp_buffer, "SERVICE ALERT:") && strstr(temp_buffer, ";OK;"))
				type = LOGENTRY_SERVICE_OK;
			else if (strstr(temp_buffer, "SERVICE NOTIFICATION:"))
				type = LOGENTRY_SERVICE_NOTIFICATION;
			else if (strstr(temp_buffer, "SERVICE EVENT HANDLER:"))
				type = LOGENTRY_SERVICE_EVENT_HANDLER;
			else if (strstr(temp_buffer, "HOST EVENT HANDLER:"))
				type = LOGENTRY_HOST_EVENT_HANDLER;
			else if (strstr(temp_buffer, "EXTERNAL COMMAND:"))
				type = LOGENTRY_EXTERNAL_COMMAND;
			else if (strstr(temp_buffer, "PASSIVE SERVICE CHECK:"))
				type = LOGENTRY_PASSIVE_SERVICE_CHECK;
			else if (strstr(temp_buffer, "PASSIVE HOST CHECK:"))
				type = LOGENTRY_PASSIVE_HOST_CHECK;
			else if (strstr(temp_buffer, "LOG ROTATION:"))
				type = LOGENTRY_LOG_ROTATION;
			else if (strstr(temp_buffer, "active mode..."))
				type = LOGENTRY_ACTIVE_MODE;
			else if (strstr(temp_buffer, "standby mode..."))
				type = LOGENTRY_STANDBY_MODE;
			else if (strstr(temp_buffer, "SERVICE FLAPPING ALERT:") && strstr(temp_buffer, ";STARTED;"))
				type = LOGENTRY_SERVICE_FLAPPING_STARTED;
			else if (strstr(temp_buffer, "SERVICE FLAPPING ALERT:") && strstr(temp_buffer, ";STOPPED;"))
				type = LOGENTRY_SERVICE_FLAPPING_STOPPED;
			else if (strstr(temp_buffer, "SERVICE FLAPPING ALERT:") && strstr(temp_buffer, ";DISABLED;"))
				type = LOGENTRY_SERVICE_FLAPPING_DISABLED;
			else if (strstr(temp_buffer, "HOST FLAPPING ALERT:") && strstr(temp_buffer, ";STARTED;"))
				type = LOGENTRY_HOST_FLAPPING_STARTED;
			else if (strstr(temp_buffer, "HOST FLAPPING ALERT:") && strstr(temp_buffer, ";STOPPED;"))
				type = LOGENTRY_HOST_FLAPPING_STOPPED;
			else if (strstr(temp_buffer, "HOST FLAPPING ALERT:") && strstr(temp_buffer, ";DISABLED;"))
				type = LOGENTRY_HOST_FLAPPING_DISABLED;
			else if (strstr(temp_buffer, "SERVICE DOWNTIME ALERT:") && strstr(temp_buffer, ";STARTED;"))
				type = LOGENTRY_SERVICE_DOWNTIME_STARTED;
			else if (strstr(temp_buffer, "SERVICE DOWNTIME ALERT:") && strstr(temp_buffer, ";STOPPED;"))
				type = LOGENTRY_SERVICE_DOWNTIME_STOPPED;
			else if (strstr(temp_buffer, "SERVICE DOWNTIME ALERT:") && strstr(temp_buffer, ";CANCELLED;"))
				type = LOGENTRY_SERVICE_DOWNTIME_CANCELLED;
			else if (strstr(temp_buffer, "HOST DOWNTIME ALERT:") && strstr(temp_buffer, ";STARTED;"))
				type = LOGENTRY_HOST_DOWNTIME_STARTED;
			else if (strstr(temp_buffer, "HOST DOWNTIME ALERT:") && strstr(temp_buffer, ";STOPPED;"))
				type = LOGENTRY_HOST_DOWNTIME_STOPPED;
			else if (strstr(temp_buffer, "HOST DOWNTIME ALERT:") && strstr(temp_buffer, ";CANCELLED;"))
				type = LOGENTRY_HOST_DOWNTIME_CANCELLED;
			else if (strstr(temp_buffer, "INITIAL SERVICE STATE:"))
				type = LOGENTRY_SERVICE_INITIAL_STATE;
			else if (strstr(temp_buffer, "INITIAL HOST STATE:"))
				type = LOGENTRY_HOST_INITIAL_STATE;
			else if (strstr(temp_buffer, "CURRENT SERVICE STATE:"))
				type = LOGENTRY_SERVICE_CURRENT_STATE;
			else if (strstr(temp_buffer, "CURRENT HOST STATE:"))
				type = LOGENTRY_HOST_CURRENT_STATE;
			else if (strstr(temp_buffer, "error executing command"))
				type = LOGENTRY_ERROR_COMMAND_EXECUTION;
			else if (strstr(temp_buffer, "idomod:"))
				type = LOGENTRY_IDOMOD;
			else if (strstr(temp_buffer, "npcdmod:"))
				type = LOGENTRY_NPCDMOD;
			else if (strstr(temp_buffer, "Auto-save of"))
				type = LOGENTRY_AUTOSAVE;
			else if (strstr(temp_buffer, "Warning:"))
				type = LOGENTRY_SYSTEM_WARNING;
			else
				type = LOGENTRY_UNDEFINED;

			/* apply filters */
			if (filter_list != NULL) {
				keep_entry = FALSE;
				for (temp_filter = filter_list; temp_filter != NULL; temp_filter = temp_filter->next) {
					if (temp_filter->include != 0) {
						if (temp_filter->include == type) {
							keep_entry = TRUE;
							break;
						}
					} else if (temp_filter->exclude != 0) {
						if (temp_filter->exclude == type) {
							keep_entry = FALSE;
							break;
						} else
							keep_entry = TRUE;
					}
				}
				if (keep_entry == FALSE)
					continue;
			}

			/* initialzie */
			/* allocate memory for a new log entry */
			temp_entry = (logentry *)malloc(sizeof(logentry));
			if (temp_entry == NULL) {
				mmap_fclose(thefile);
				return READLOG_ERROR_MEMORY;
			}

			temp_entry->timestamp = 0L;
			temp_entry->type = 0;
			temp_entry->entry_text = NULL;
			temp_entry->next = NULL;


			temp_entry->timestamp = timestamp;
			temp_entry->type = type;
			temp_entry->entry_text = strdup(temp_buffer);

			if (reverse == TRUE) {
				if (entry_list == NULL) {
					entry_list = temp_entry;
					last_entry = entry_list;
				} else {
					last_entry->next = temp_entry;
					last_entry = temp_entry;
				}
			} else {
				temp_entry->next = entry_list;
				entry_list = temp_entry;
			}
		}

		mmap_fclose(thefile);

		if (search_string != NULL)
			regfree(&preg);
	} else
		return READLOG_ERROR_NOFILE;

	return READLOG_OK;
}
Ejemplo n.º 7
0
/** @brief Read's log data for defined timerange and stores the entries into entry_list struct
 *  @param [out] entry_list returns a filled entry list of requested log data
 *  @param [in] filter_list a list of filters of type logfilter struct
 *  @param [out] error_text returns a error string in case of an error execpt on READLOG_ERROR_MEMORY
 *  @param [in] search_string a string you are searching for
 *		Set to NULL to disable search function
 *  @param [in] reverse this bool defines which order the log entries should return
 *  @param [in] ts_start defines the start timestamp for log entries
 *	@arg >=0 means unix timestamp
 *  @param [in] ts_end defines the end timestamp for log entries
 *	@arg >=0 means unix timestamp
 *  @return
 *	@retval READLOG_OK
 *	@retval READLOG_ERROR_WARNING
 *	@retval READLOG_ERROR_FATAL
 *	@retval READLOG_ERROR_MEMORY
 *	@retval READLOG_ERROR_FILTER
 *  @author Ricardo Bartels
 *
 *  This functions reads a  \c log_file and and try's (if set) to filter for a search string.
 *  This search string uses regular expressions. The reverse option defines if you want
 *  have your log entries returned in normal or revers order. Normal order for returning
 *  would be from the newest entry to the oldest. You can also set a time "window". This
 *  defines if you want to exclude entries which are outside of these "window". Then only
 *  entries will be returned which are between start and end. Very useful if user has all
 *  entries in one log file.
**/
int get_log_entries(logentry **entry_list, logfilter **filter_list, char **error_text, char *search_string, int reverse, time_t ts_start, time_t ts_end) {
	char *input = NULL;
	char *temp_buffer = NULL;
	char *search_regex = NULL;
	char log_file_name[MAX_FILENAME_LENGTH];
	char ts_buffer[16];
	int type = 0;
	int regex_i = 0, i = 0, len = 0;
	int file_num = 1;
	int file = 0;
	int in_range = FALSE;
	int return_val = READLOG_OK;
	int data_found = FALSE;
	int dummy;
	short keep_entry = TRUE;
	time_t timestamp = 0L;
	time_t last_timestamp = 0L;
	mmapfile *thefile = NULL;
	logentry *temp_entry = NULL;
	logentry *last_entry = NULL;
	regex_t preg;
	logfilter *temp_filter;
	DIR *dirp;
	struct dirent *dptr;
	struct file_data files[10000];

	/* empty error_text */
	if (*error_text != NULL)
		my_free(*error_text);

	/* bail out if one timestamp is negative */
	if (ts_start < 0 || ts_end < 0) {
		*error_text = strdup("开始或结束的时间戳是无效的. 请检查提交的日期信息");
		return READLOG_ERROR_FATAL;
	}

	/* check if search_string is set */
	if (search_string != NULL) {

		/* allocate for 3 extra chars, ^, $ and \0 */
		search_regex = malloc(sizeof(char) * (strlen(search_string) * 2 + 3));
		len = strlen(search_string);
		for (i = 0; i < len; i++, regex_i++) {
			if (search_string[i] == '*') {
				search_regex[regex_i++] = '.';
				search_regex[regex_i] = '*';
			} else
				search_regex[regex_i] = search_string[i];
		}

		search_regex[regex_i] = '\0';

		/* check and compile regex, return error on failure */
		if (regcomp(&preg, search_regex, REG_ICASE | REG_NOSUB) != 0) {
			regfree(&preg);
			my_free(search_regex);
			*error_text = strdup("很显然你搜索不符合正则表达式. 请更改您要搜索的字符串.");
			return READLOG_ERROR_FATAL;
		}

		my_free(search_regex);
	}

	/* initialize file data array */
	for (i=0;i<10000;i++)
		files[i].file_name = NULL;

	/* try to open log_archive_path, return if it fails */
	if ((dirp=opendir(log_archive_path)) == NULL){

		if (search_string != NULL)
			regfree(&preg);

		dummy = asprintf(&temp_buffer, "无法打开 \"日志归档路径\" -> \"%s\"!!!", log_archive_path);
		*error_text = strdup(temp_buffer);
		my_free(temp_buffer);

		return READLOG_ERROR_FATAL;

	} else {

		/* read every dir entry */
		while ((dptr=readdir(dirp)) != NULL) {

			/* filter dir for icinga / nagios log files */
			if ((strncmp("icinga-",dptr->d_name,7) == 0 || strncmp("nagios-",dptr->d_name,7) == 0 ) &&
			    strstr(dptr->d_name, ".log") && strlen(dptr->d_name) == 24)
				files[file_num++].file_name = strdup(dptr->d_name);
		}
		closedir(dirp);
	}

	/* sort log files, newest first */
	qsort((void *)files, file_num, sizeof(struct file_data), sort_icinga_logfiles_by_name);

	/* define which log files to use */
	for (i=0; i< file_num; i++) {

		/* first log file is always the current log file */
		if (i == 0) {
			strncpy(log_file_name, log_file, sizeof(log_file_name) -1);
			log_file_name[sizeof(log_file_name)-1] = '\x0';

		/* return full path of logfile and store first timestamp of last file */
		} else {
			snprintf(log_file_name, sizeof(log_file_name) -1, "%s%s",log_archive_path, files[i].file_name);
			log_file_name[sizeof(log_file_name)-1] = '\x0';

			last_timestamp = timestamp;
		}

		/* free file entry and set to NULL. if valid file is found, entry gets refilled */
		my_free(files[i].file_name);

		/* we found data and we are out of range again, file must be older then ts_start. stop checking files */
		if (data_found == TRUE && in_range == FALSE)
			continue;

		/* try to open log file, or throw error and try next log file */
		if((file=open(log_file_name, O_RDONLY)) < -1) {

			if (*error_text == NULL) {
				dummy = asprintf(&temp_buffer, "无法打开日志文件 \"%s\" !!!", log_file_name);
				*error_text = strdup(temp_buffer);
				my_free(temp_buffer);
			}

			return_val = READLOG_ERROR_WARNING;

			continue;
		}

		/* read first 16 bytes to get first timestamp, or throw error if data is not 16 bytes log (empty file) */
		if(read(file,ts_buffer,16) != 16) {

			if (*error_text == NULL) {
				dummy = asprintf(&temp_buffer, "日志文件 \"%s\" 无效! 目前没有在前16个字节内发现时间戳!", log_file_name);
				*error_text = strdup(temp_buffer);
				my_free(temp_buffer);
			}

			return_val = READLOG_ERROR_WARNING;

			close(file);
			continue;
		}

		close(file);

		/* get first timestamp */
		temp_buffer = strtok(ts_buffer, "]");
		timestamp = (temp_buffer == NULL) ? 0L : strtoul(temp_buffer + 1, NULL, 10);


		/* if first (oldest) timestamp in file is newer then ts_end, skip file */
		if (timestamp > ts_end)
			continue;

		in_range = TRUE;

		/* the priviouse file holds range for ts_start */
		if (last_timestamp != 0L && last_timestamp < ts_start)
			in_range = FALSE;

		/* keep file if in range */
		if(in_range == TRUE) {
			files[i].file_name = strdup(log_file_name);
			data_found = TRUE;
		}
	}

	/* read all log files we found earlier in reverse order, starting with the oldest */
	for (i=file_num; i >= 0; i--) {

		/* if file name is empty try next file */
		if (files[i].file_name == NULL)
			continue;

		/* try to open log file */
		if ((thefile = mmap_fopen(files[i].file_name)) == NULL)
			continue;

		while (1) {

			/* free memory */
			my_free(input);

			if ((input = mmap_fgets(thefile)) == NULL)
				break;

			strip(input);

			if ((int)strlen(input) == 0)
				continue;

			/* get timestamp */
			temp_buffer = strtok(input, "]");

			if (temp_buffer == NULL)
				continue;

			timestamp = strtoul(temp_buffer + 1, NULL, 10);

			/* skip line if out of range */
			if ((ts_end >= 0 && timestamp > ts_end) || (ts_start >= 0 && timestamp < ts_start))
				continue;

			/* get log entry text */
			temp_buffer = strtok(NULL, "\n");

			if (temp_buffer == NULL)
				continue;

			/* if we search for something, check if it entry matches search_string */
			if (search_string != NULL) {
				if (regexec(&preg, temp_buffer, 0, NULL, 0) == REG_NOMATCH)
					continue;
			}

			/* categorize log entry */
			if (strstr(temp_buffer, " 开始..."))
				type = LOGENTRY_STARTUP;
			else if (strstr(temp_buffer, " 关闭..."))
				type = LOGENTRY_SHUTDOWN;
			else if (strstr(temp_buffer, "救助"))
				type = LOGENTRY_BAILOUT;
			else if (strstr(temp_buffer, " 重启..."))
				type = LOGENTRY_RESTART;
			else if (strstr(temp_buffer, "主机警告:") && strstr(temp_buffer, ";宕机;"))
				type = LOGENTRY_HOST_DOWN;
			else if (strstr(temp_buffer, "主机警告:") && strstr(temp_buffer, ";不可达;"))
				type = LOGENTRY_HOST_UNREACHABLE;
			else if (strstr(temp_buffer, "主机警告:") && strstr(temp_buffer, ";恢复;"))
				type = LOGENTRY_HOST_RECOVERY;
			else if (strstr(temp_buffer, "主机警告:") && strstr(temp_buffer, ";运行;"))
				type = LOGENTRY_HOST_UP;
			else if (strstr(temp_buffer, "主机通知:"))
				type = LOGENTRY_HOST_NOTIFICATION;
			else if (strstr(temp_buffer, "服务警告:") && strstr(temp_buffer, ";严重;"))
				type = LOGENTRY_SERVICE_CRITICAL;
			else if (strstr(temp_buffer, "服务警告:") && strstr(temp_buffer, ";警报;"))
				type = LOGENTRY_SERVICE_WARNING;
			else if (strstr(temp_buffer, "服务警告:") && strstr(temp_buffer, ";未知;"))
				type = LOGENTRY_SERVICE_UNKNOWN;
			else if (strstr(temp_buffer, "服务警告:") && strstr(temp_buffer, ";恢复;"))
				type = LOGENTRY_SERVICE_RECOVERY;
			else if (strstr(temp_buffer, "服务警告:") && strstr(temp_buffer, ";正常;"))
				type = LOGENTRY_SERVICE_OK;
			else if (strstr(temp_buffer, "服务通知:"))
				type = LOGENTRY_SERVICE_NOTIFICATION;
			else if (strstr(temp_buffer, "服务事件处理:"))
				type = LOGENTRY_SERVICE_EVENT_HANDLER;
			else if (strstr(temp_buffer, "主机事件处理:"))
				type = LOGENTRY_HOST_EVENT_HANDLER;
			else if (strstr(temp_buffer, "额外命令:"))
				type = LOGENTRY_EXTERNAL_COMMAND;
			else if (strstr(temp_buffer, "被动服务检查:"))
				type = LOGENTRY_PASSIVE_SERVICE_CHECK;
			else if (strstr(temp_buffer, "被动主机检查:"))
				type = LOGENTRY_PASSIVE_HOST_CHECK;
			else if (strstr(temp_buffer, "日志回滚:"))
				type = LOGENTRY_LOG_ROTATION;
			else if (strstr(temp_buffer, "主动模式..."))
				type = LOGENTRY_ACTIVE_MODE;
			else if (strstr(temp_buffer, "待机模式..."))
				type = LOGENTRY_STANDBY_MODE;
			else if (strstr(temp_buffer, "服务抖动警告:") && strstr(temp_buffer, ";开始;"))
				type = LOGENTRY_SERVICE_FLAPPING_STARTED;
			else if (strstr(temp_buffer, "服务抖动警告:") && strstr(temp_buffer, ";停止;"))
				type = LOGENTRY_SERVICE_FLAPPING_STOPPED;
			else if (strstr(temp_buffer, "服务抖动警告:") && strstr(temp_buffer, ";禁止;"))
				type = LOGENTRY_SERVICE_FLAPPING_DISABLED;
			else if (strstr(temp_buffer, "主机抖动警告:") && strstr(temp_buffer, ";开始;"))
				type = LOGENTRY_HOST_FLAPPING_STARTED;
			else if (strstr(temp_buffer, "主机抖动警告:") && strstr(temp_buffer, ";停止;"))
				type = LOGENTRY_HOST_FLAPPING_STOPPED;
			else if (strstr(temp_buffer, "主机抖动警告:") && strstr(temp_buffer, ";禁止;"))
				type = LOGENTRY_HOST_FLAPPING_DISABLED;
			else if (strstr(temp_buffer, "服务宕机警告:") && strstr(temp_buffer, ";开始;"))
				type = LOGENTRY_SERVICE_DOWNTIME_STARTED;
			else if (strstr(temp_buffer, "服务宕机警告:") && strstr(temp_buffer, ";停止;"))
				type = LOGENTRY_SERVICE_DOWNTIME_STOPPED;
			else if (strstr(temp_buffer, "服务宕机警告:") && strstr(temp_buffer, ";取消;"))
				type = LOGENTRY_SERVICE_DOWNTIME_CANCELLED;
			else if (strstr(temp_buffer, "主机宕机警告:") && strstr(temp_buffer, ";开始;"))
				type = LOGENTRY_HOST_DOWNTIME_STARTED;
			else if (strstr(temp_buffer, "主机宕机警告:") && strstr(temp_buffer, ";停止;"))
				type = LOGENTRY_HOST_DOWNTIME_STOPPED;
			else if (strstr(temp_buffer, "主机宕机警告:") && strstr(temp_buffer, ";取消;"))
				type = LOGENTRY_HOST_DOWNTIME_CANCELLED;
			else if (strstr(temp_buffer, "初始服务状态:"))
				type = LOGENTRY_SERVICE_INITIAL_STATE;
			else if (strstr(temp_buffer, "初始主机状态:"))
				type = LOGENTRY_HOST_INITIAL_STATE;
			else if (strstr(temp_buffer, "当前服务状态:"))
				type = LOGENTRY_SERVICE_CURRENT_STATE;
			else if (strstr(temp_buffer, "当前主机状态:"))
				type = LOGENTRY_HOST_CURRENT_STATE;
			else if (strstr(temp_buffer, "错误的执行命令"))
				type = LOGENTRY_ERROR_COMMAND_EXECUTION;
			else if (strstr(temp_buffer, "idomod:"))
				type = LOGENTRY_IDOMOD;
			else if (strstr(temp_buffer, "npcdmod:"))
				type = LOGENTRY_NPCDMOD;
			else if (strstr(temp_buffer, "自动保存"))
				type = LOGENTRY_AUTOSAVE;
			else if (strstr(temp_buffer, "警报:"))
				type = LOGENTRY_SYSTEM_WARNING;
			else
				type = LOGENTRY_UNDEFINED;

			/* apply filters */
			if (*filter_list != NULL) {
				keep_entry = FALSE;
				for (temp_filter = *filter_list; temp_filter != NULL; temp_filter = temp_filter->next) {
					if (temp_filter->include != 0) {
						if (temp_filter->include == type) {
							keep_entry = TRUE;
							break;
						}
					} else if (temp_filter->exclude != 0) {
						if (temp_filter->exclude == type) {
							keep_entry = FALSE;
							break;
						} else
							keep_entry = TRUE;
					}
				}
				if (keep_entry == FALSE)
					continue;
			}

			/* initialzie */
			/* allocate memory for a new log entry */
			temp_entry = (logentry *)malloc(sizeof(logentry));
			if (temp_entry == NULL) {

				mmap_fclose(thefile);
				return READLOG_ERROR_MEMORY;
			}

			temp_entry->timestamp = 0L;
			temp_entry->type = 0;
			temp_entry->entry_text = NULL;
			temp_entry->next = NULL;


			temp_entry->timestamp = timestamp;
			temp_entry->type = type;
			temp_entry->entry_text = strdup(temp_buffer);

			if (reverse == TRUE) {
				if (*entry_list == NULL) {
					*entry_list = temp_entry;
					last_entry = *entry_list;
				} else {
					last_entry->next = temp_entry;
					last_entry = temp_entry;
				}
			} else {
				temp_entry->next = *entry_list;
				*entry_list = temp_entry;
			}
		}

		mmap_fclose(thefile);
	}

	for (i=0; i< file_num;i++)
		my_free(files[i].file_name);

	if (search_string != NULL)
		regfree(&preg);

	return return_val;
}