Пример #1
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;
	}
Пример #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;
	}
Пример #3
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;
}