/*********************************************************************** * * HISTORY.C - NetSaint History CGI * * Copyright (c) 1999-2001 Ethan Galstad (netsaint@netsaint.org) * Last Modified: 02-10-2001 * * This CGI program will display the history for the specified host. * If no host is specified, the history for all hosts will be displayed. * * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * 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 "../common/config.h" #include "../common/locations.h" #include "../common/common.h" #include "../common/objects.h" #include "getcgi.h" #include "cgiutils.h" #include "auth.h" #include "lifo.h" #define DISPLAY_HOSTS 0 #define DISPLAY_SERVICES 1 #define SERVICE_HISTORY 0 #define HOST_HISTORY 1 #define SERVICE_FLAPPING_HISTORY 2 #define HOST_FLAPPING_HISTORY 3 #define SERVICE_DOWNTIME_HISTORY 4 #define HOST_DOWNTIME_HISTORY 5 #define STATE_ALL 0 #define STATE_SOFT 1 #define STATE_HARD 2 void get_history(void); void document_header(int); void document_footer(void); int process_cgivars(void); extern char main_config_file[MAX_FILENAME_LENGTH]; extern char url_images_path[MAX_FILENAME_LENGTH]; extern char url_stylesheets_path[MAX_FILENAME_LENGTH]; extern int log_rotation_method; extern time_t last_scheduled_log_rotation; extern time_t this_scheduled_log_rotation; authdata current_authdata; char log_file_to_use[MAX_FILENAME_LENGTH]; int log_archive=0; int show_all_hosts=TRUE; char *host_name="all"; char *svc_description=""; int display_type=DISPLAY_HOSTS; int use_lifo=TRUE; int history_options=HISTORY_ALL; int state_options=STATE_ALL; extern host *host_list; int embedded=FALSE; int display_header=TRUE; int display_frills=TRUE; int display_timebreaks=TRUE; int display_system_messages=TRUE; int display_flapping_alerts=TRUE; int display_downtime_alerts=TRUE; int main(void){ int result=OK; char temp_buffer[MAX_INPUT_BUFFER]; char temp_buffer2[MAX_INPUT_BUFFER]; /* get the variables passed to us */ process_cgivars(); /* reset internal CGI variables */ reset_cgi_vars(); /* read the CGI configuration file */ result=read_cgi_config_file(DEFAULT_CGI_CONFIG_FILE); if(result==ERROR){ document_header(FALSE); printf("

Error: Could not open CGI configuration file '%s' for reading!

\n",DEFAULT_CGI_CONFIG_FILE); document_footer(); return ERROR; } document_header(TRUE); /* read the main configuration file */ result=read_main_config_file(main_config_file); if(result==ERROR){ printf("

Error: Could not open main configuration file '%s' for reading!

\n",main_config_file); document_footer(); return ERROR; } /* read all object configuration data */ result=read_all_object_configuration_data(main_config_file,READ_HOSTGROUPS|READ_CONTACTGROUPS|READ_CONTACTS|READ_HOSTS|READ_SERVICES); if(result==ERROR){ printf("

Error: Could not read some or all object configuration data!

\n"); document_footer(); return ERROR; } /* 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("\n"); printf("\n"); /* left column of the first row */ printf("\n"); /* middle column of top row */ printf("\n"); /* right hand column of top row */ printf("\n"); /* end of top table */ printf("\n"); printf("
\n"); if(display_type==DISPLAY_SERVICES) snprintf(temp_buffer,sizeof(temp_buffer)-1,"%s For Service %s On Host %s",(log_rotation_method==LOG_ROTATION_NONE || log_archive==0)?"Current Alert History":"Archived Alert History",svc_description,host_name); else{ if(show_all_hosts==TRUE) snprintf(temp_buffer,sizeof(temp_buffer)-1,"%s For All Hosts",(log_rotation_method==LOG_ROTATION_NONE || log_archive==0)?"Current Alert History":"Archived Alert History"); else snprintf(temp_buffer,sizeof(temp_buffer)-1,"%s For All Host %s",(log_rotation_method==LOG_ROTATION_NONE || log_archive==0)?"Current Alert History":"Archived Alert History",host_name); } temp_buffer[sizeof(temp_buffer)-1]='\x0'; display_info_table(temp_buffer,FALSE,¤t_authdata); printf("\n"); printf("\n"); printf("\n"); printf("\n"); snprintf(temp_buffer,sizeof(temp_buffer)-1,"%s?%shost=%s&type=%d&statetype=%d&",HISTORY_CGI,(use_lifo==FALSE)?"oldestfirst&":"",url_encode(host_name),history_options,state_options); temp_buffer[sizeof(temp_buffer)-1]='\x0'; if(display_type==DISPLAY_SERVICES){ snprintf(temp_buffer2,sizeof(temp_buffer2)-1,"service=%s&",url_encode(svc_description)); temp_buffer2[sizeof(temp_buffer2)-1]='\x0'; strncat(temp_buffer,temp_buffer2,sizeof(temp_buffer)-strlen(temp_buffer)-1); temp_buffer[sizeof(temp_buffer)-1]='\x0'; } display_nav_table(temp_buffer,log_archive); printf("\n"); printf("\n"); printf("\n",HISTORY_CGI); printf("\n",(show_all_hosts==TRUE)?"all":host_name); if(display_type==DISPLAY_SERVICES) printf("\n",svc_description); printf("\n",log_archive); printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("\n") ; printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("",(display_flapping_alerts==FALSE)?"checked":""); printf("\n"); printf("\n"); printf("",(display_downtime_alerts==FALSE)?"checked":""); printf("\n"); printf("\n"); printf("",(display_system_messages==FALSE)?"checked":""); printf("\n"); printf("\n"); printf("",(use_lifo==FALSE)?"checked":""); printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("
State type options:
History detail level for "); if(display_type==DISPLAY_HOSTS) printf("%s host%s",(show_all_hosts==TRUE)?"all":"this",(show_all_hosts==TRUE)?"s":""); else printf("service"); printf(":
Hide Flapping Alerts
Hide Downtime Alerts
Hide Process Messages
Older Entries First
\n"); printf("
\n"); } /* display history */ get_history(); /* free allocated memory */ free_memory(); return OK; } void document_header(int use_stylesheet){ char date_time[MAX_DATETIME_LENGTH]; time_t current_time; time_t expire_time; printf("Cache-Control: no-store\n"); printf("Pragma: no-cache\n"); time(¤t_time); get_time_string(¤t_time,date_time,sizeof(date_time),HTTP_DATE_TIME); printf("Last-Modified: %s\n",date_time); expire_time=(time_t)0L; get_time_string(&expire_time,date_time,sizeof(date_time),HTTP_DATE_TIME); printf("Expires: %s\n",date_time); printf("Content-type: text/html\n\n"); if(embedded==TRUE) return; printf("\n"); printf("\n"); printf("\n"); printf("NetSaint History\n"); printf("\n"); if(use_stylesheet==TRUE) printf("\n",url_stylesheets_path,HISTORY_CSS); printf("\n"); printf("\n"); return; } void document_footer(void){ if(embedded==TRUE) return; printf("\n"); printf("\n"); return; } int process_cgivars(void){ char **variables; int error=FALSE; int x; variables=getcgivars(); for(x=0;variables[x]!=NULL;x++){ /* do some basic length checking on the variable identifier to prevent buffer overflows */ if(strlen(variables[x])>=MAX_INPUT_BUFFER-1) continue; /* we found the host argument */ else if(!strcmp(variables[x],"host")){ x++; if(variables[x]==NULL){ error=TRUE; break; } host_name=(char *)malloc(strlen(variables[x])+1); if(host_name==NULL) host_name=""; else strcpy(host_name,variables[x]); display_type=DISPLAY_HOSTS; if(!strcmp(host_name,"all")) show_all_hosts=TRUE; else show_all_hosts=FALSE; } /* we found the service argument */ else if(!strcmp(variables[x],"service")){ x++; if(variables[x]==NULL){ error=TRUE; break; } svc_description=(char *)malloc(strlen(variables[x])+1); if(svc_description==NULL) svc_description=""; else strcpy(svc_description,variables[x]); display_type=DISPLAY_SERVICES; } /* we found the history type argument */ else if(!strcmp(variables[x],"type")){ x++; if(variables[x]==NULL){ error=TRUE; break; } history_options=atoi(variables[x]); } /* we found the history state type argument */ else if(!strcmp(variables[x],"statetype")){ x++; if(variables[x]==NULL){ error=TRUE; break; } state_options=atoi(variables[x]); } /* we found the log archive argument */ else if(!strcmp(variables[x],"archive")){ x++; if(variables[x]==NULL){ error=TRUE; break; } log_archive=atoi(variables[x]); if(log_archive<0) log_archive=0; } /* we found the order argument */ else if(!strcmp(variables[x],"oldestfirst")){ use_lifo=FALSE; } /* we found the embed option */ else if(!strcmp(variables[x],"embedded")) embedded=TRUE; /* we found the noheader option */ else if(!strcmp(variables[x],"noheader")) display_header=FALSE; /* we found the nofrills option */ else if(!strcmp(variables[x],"nofrills")) display_frills=FALSE; /* we found the notimebreaks option */ else if(!strcmp(variables[x],"notimebreaks")) display_timebreaks=FALSE; /* we found the no system messages option */ else if(!strcmp(variables[x],"nosystem")) display_system_messages=FALSE; /* we found the no flapping alerts option */ else if(!strcmp(variables[x],"noflapping")) display_flapping_alerts=FALSE; /* we found the no downtime alerts option */ else if(!strcmp(variables[x],"nodowntime")) display_downtime_alerts=FALSE; } return error; } void get_history(void){ FILE *fp=NULL; char image[MAX_INPUT_BUFFER]; char image_alt[MAX_INPUT_BUFFER]; char input_buffer[MAX_INPUT_BUFFER]; char input_buffer2[MAX_INPUT_BUFFER]; 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; int history_type=SERVICE_HISTORY; int history_detail_type=HISTORY_SERVICE_CRITICAL; char entry_host_name[MAX_HOSTNAME_LENGTH]; char entry_service_desc[MAX_SERVICEDESC_LENGTH]; host *temp_host; service *temp_service; int result; char last_message_date[MAX_INPUT_BUFFER]=""; char current_message_date[MAX_INPUT_BUFFER]=""; struct tm *time_ptr; if(use_lifo==TRUE){ result=read_file_into_lifo(log_file_to_use); if(result!=LIFO_OK){ if(result==LIFO_ERROR_MEMORY){ printf("

Not enough memory to reverse log file - displaying history in natural order...

\n"); } else if(result==LIFO_ERROR_FILE){ printf("

Error: Cannot open log file '%s' for reading!


",log_file_to_use); return; } use_lifo=FALSE; } } if(use_lifo==FALSE){ fp=fopen(log_file_to_use,"r"); if(fp==NULL){ printf("

Error: Cannot open log file '%s' for reading!


",log_file_to_use); return; } } printf("

\n"); printf("
\n"); printf(""); printf(""); printf("",current_message_date); printf(""); printf("

%s
\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'; } if(display_frills==TRUE) printf("%s",url_images_path,image,image_alt); printf("[%s] %s
\n",date_time,temp_buffer); found_line=TRUE; } } } printf("

\n"); if(found_line==FALSE){ printf("
\n"); printf("

No history information was found "); if(display_type==DISPLAY_HOSTS) printf("%s",(show_all_hosts==TRUE)?"":"for this host "); else printf("for this this service "); printf("in %s log file

",(log_archive==0)?"the current":"this archived"); } printf("
\n"); if(use_lifo==TRUE) free_lifo_memory(); else fclose(fp); return; }