/***********************************************************************
*
* SHOWLOG.C - Nagios Log File CGI
*
*
* This CGI program will display the contents of the Nagios
* log file.
*
* License:
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
***********************************************************************/
#include "../include/config.h"
#include "../include/common.h"
#include "../include/objects.h"
#include "../include/getcgi.h"
#include "../include/cgiutils.h"
#include "../include/cgiauth.h"
extern char main_config_file[MAX_FILENAME_LENGTH];
extern char url_html_path[MAX_FILENAME_LENGTH];
extern char url_images_path[MAX_FILENAME_LENGTH];
extern char url_stylesheets_path[MAX_FILENAME_LENGTH];
extern int enable_splunk_integration;
void document_header(int);
void document_footer(void);
int process_cgivars(void);
authdata current_authdata;
int display_log(void);
char log_file_to_use[MAX_FILENAME_LENGTH] = "";
int log_archive = 0;
int use_lifo = TRUE;
int embedded = FALSE;
int display_header = TRUE;
int display_frills = TRUE;
int display_timebreaks = TRUE;
int main(void) {
char temp_buffer[MAX_INPUT_BUFFER];
/* get the CGI variables passed in the URL */
process_cgivars();
/* reset internal variables */
reset_cgi_vars();
cgi_init(document_header, document_footer, READ_ALL_OBJECT_DATA, 0);
document_header(TRUE);
/* get authentication information */
get_authentication_information(¤t_authdata);
/* determine what log file we should be using */
get_log_archive_to_use(log_archive, log_file_to_use, (int)sizeof(log_file_to_use));
if(display_header == TRUE) {
/* begin top table */
printf("
It appears as though you do not have permission to view the log file...
If you believe this is an error, check the HTTP server authentication requirements for accessing this CGI
and check the authorization options in your CGI configuration file.
\n");
printf("Not enough memory to reverse log file - displaying log in natural order...
");
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("\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("
\n");
printf("
\n");
printf("
");
printf("
| ");
printf("%s | ", current_message_date);
printf("
| ");
printf("
\n");
printf("
\n");
printf("
\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("
", 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(" ");
display_splunk_generic_url(temp_buffer, 2);
}
printf("
\n");
}
printf("
\n");
printf("
\n");
free(input);
if(use_lifo == FALSE)
mmap_fclose(thefile);
}
if(use_lifo == TRUE)
free_lifo_memory();
return OK;
}