Icinga-core 1.4.0
next gen monitoring
cgi/histogram.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002  *
00003  * HISTOGRAM.C -  Icinga Alert Histogram CGI
00004  *
00005  * Copyright (c) 1999-2009 Ethan Galstad (egalstad@nagios.org)
00006  * Copyright (c) 2009-2011 Icinga Development Team (http://www.icinga.org)
00007  *
00008  * License:
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License version 2 as
00012  * published by the Free Software Foundation.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00022  *
00023  *****************************************************************************/
00024 
00025 #include "../include/config.h"
00026 #include "../include/common.h"
00027 #include "../include/objects.h"
00028 #include "../include/statusdata.h"
00029 #include "../include/readlogs.h"
00030 
00031 #include "../include/cgiutils.h"
00032 #include "../include/getcgi.h"
00033 #include "../include/cgiauth.h"
00034 
00035 #include <gd.h>                 /* Boutell's GD library function */
00036 #include <gdfonts.h>            /* GD library small font definition */
00037 
00038 
00039 /*#define DEBUG                 1*/
00040 
00041 
00042 #define HISTOGRAM_IMAGE         "histogram.png"
00043 
00044 /* archived state types */
00045 #define AS_NO_DATA              0
00046 #define AS_PROGRAM_START        1
00047 #define AS_PROGRAM_END          2
00048 #define AS_HOST_UP              3
00049 #define AS_HOST_DOWN            4
00050 #define AS_HOST_UNREACHABLE     5
00051 #define AS_SVC_OK               6
00052 #define AS_SVC_UNKNOWN          7
00053 #define AS_SVC_WARNING          8
00054 #define AS_SVC_CRITICAL         9
00055 
00056 
00057 /* display types */
00058 #define DISPLAY_HOST_HISTOGRAM          0
00059 #define DISPLAY_SERVICE_HISTOGRAM       1
00060 #define DISPLAY_NO_HISTOGRAM            2
00061 
00062 /* input types */
00063 #define GET_INPUT_NONE                  0
00064 #define GET_INPUT_TARGET_TYPE           1
00065 #define GET_INPUT_HOST_TARGET           2
00066 #define GET_INPUT_SERVICE_TARGET        3
00067 #define GET_INPUT_OPTIONS               4
00068 
00069 /* breakdown types */
00070 #define BREAKDOWN_MONTHLY       0
00071 #define BREAKDOWN_DAY_OF_MONTH  1
00072 #define BREAKDOWN_DAY_OF_WEEK   2
00073 #define BREAKDOWN_HOURLY        3
00074 
00075 
00076 #define MAX_ARCHIVE_SPREAD      65
00077 #define MAX_ARCHIVE             65
00078 #define MAX_ARCHIVE_BACKTRACKS  60
00079 
00080 #define DRAWING_WIDTH           550
00081 #define DRAWING_HEIGHT          195
00082 #define DRAWING_X_OFFSET        60
00083 #define DRAWING_Y_OFFSET        235
00084 
00085 #define GRAPH_HOST_UP                   1
00086 #define GRAPH_HOST_DOWN                 2
00087 #define GRAPH_HOST_UNREACHABLE          4
00088 #define GRAPH_SERVICE_OK                8
00089 #define GRAPH_SERVICE_WARNING           16
00090 #define GRAPH_SERVICE_UNKNOWN           32
00091 #define GRAPH_SERVICE_CRITICAL          64
00092 
00093 #define GRAPH_HOST_PROBLEMS             6
00094 #define GRAPH_HOST_ALL                  7
00095 
00096 #define GRAPH_SERVICE_PROBLEMS          112
00097 #define GRAPH_SERVICE_ALL               120
00098 
00099 #define GRAPH_EVERYTHING                255
00100 
00101 
00102 #define GRAPH_SOFT_STATETYPES           1
00103 #define GRAPH_HARD_STATETYPES           2
00104 #define GRAPH_ALL_STATETYPES            3
00105 
00106 
00107 
00108 
00109 extern char main_config_file[MAX_FILENAME_LENGTH];
00110 extern char url_html_path[MAX_FILENAME_LENGTH];
00111 extern char url_images_path[MAX_FILENAME_LENGTH];
00112 extern char url_stylesheets_path[MAX_FILENAME_LENGTH];
00113 extern char url_js_path[MAX_FILENAME_LENGTH];
00114 extern char physical_images_path[MAX_FILENAME_LENGTH];
00115 
00116 extern int     log_rotation_method;
00117 
00118 extern host *host_list;
00119 extern service *service_list;
00120 extern logentry *entry_list;
00121 
00122 
00123 authdata current_authdata;
00124 
00125 
00126 typedef struct timeslice_data_struct{
00127         unsigned long    service_ok;
00128         unsigned long    host_up;
00129         unsigned long    service_critical;
00130         unsigned long    host_down;
00131         unsigned long    service_unknown;
00132         unsigned long    host_unreachable;
00133         unsigned long    service_warning;
00134         }timeslice_data;
00135 
00136 
00137 timeslice_data *tsdata;
00138 
00139 void compute_report_times(void);
00140 void graph_all_histogram_data(void);
00141 void add_archived_state(int,time_t);
00142 void read_archived_state_data(void);
00143 void scan_log_file_for_archived_state_data(char *);
00144 void draw_line(int,int,int,int,int);
00145 void draw_dashed_line(int,int,int,int,int);
00146 
00147 int process_cgivars(void);
00148 
00149 
00150 time_t t1;
00151 time_t t2;
00152 
00153 int start_second=0;
00154 int start_minute=0;
00155 int start_hour=0;
00156 int start_day=1;
00157 int start_month=1;
00158 int start_year=2000;
00159 int end_second=0;
00160 int end_minute=0;
00161 int end_hour=24;
00162 int end_day=1;
00163 int end_month=1;
00164 int end_year=2000;
00165 
00166 extern int content_type;
00167 int input_type=GET_INPUT_NONE;
00168 int timeperiod_type=TIMEPERIOD_LAST24HOURS;
00169 int breakdown_type=BREAKDOWN_HOURLY;
00170 int compute_time_from_parts=FALSE;
00171 
00172 int initial_states_logged=FALSE;
00173 int assume_state_retention=TRUE;
00174 int new_states_only=FALSE;
00175 
00176 int last_state=AS_NO_DATA;
00177 int program_restart_has_occurred=FALSE;
00178 
00179 int graph_events=GRAPH_EVERYTHING;
00180 int graph_statetypes=GRAPH_HARD_STATETYPES;
00181 
00182 extern int embedded;
00183 extern int display_header;
00184 extern int daemon_check;
00185 
00186 gdImagePtr histogram_image=0;
00187 int color_white=0;
00188 int color_black=0;
00189 int color_red=0;
00190 int color_darkred=0;
00191 int color_green=0;
00192 int color_yellow=0;
00193 int color_orange=0;
00194 int color_lightgray=0;
00195 FILE *image_file=NULL;
00196 
00197 int backtrack_archives=0;
00198 int earliest_archive=0;
00199 time_t earliest_time;
00200 time_t latest_time;
00201 
00202 int image_width=900;
00203 int image_height=320;
00204 
00205 int total_buckets=96;
00206 
00207 int display_type=DISPLAY_NO_HISTOGRAM;
00208 int show_all_hosts=TRUE;
00209 int show_all_hostgroups=TRUE;
00210 int show_all_servicegroups=TRUE;
00211 
00212 char *host_name="";
00213 char *host_filter=NULL;
00214 char *hostgroup_name=NULL;
00215 char *servicegroup_name=NULL;
00216 char *service_desc="";
00217 char *service_filter=NULL;
00218 
00219 int CGI_ID=HISTOGRAM_CGI_ID;
00220 
00221 int main(int argc, char **argv){
00222         int result=OK;
00223         char temp_buffer[MAX_INPUT_BUFFER];
00224         char image_template[MAX_INPUT_BUFFER];
00225         char start_timestring[MAX_INPUT_BUFFER];
00226         char end_timestring[MAX_INPUT_BUFFER];
00227         host *temp_host=NULL;
00228         service *temp_service=NULL;
00229         int is_authorized=TRUE;
00230         int found=FALSE;
00231         int days,hours,minutes,seconds;
00232         char *first_service=NULL;
00233         int x;
00234         time_t t3;
00235         time_t current_time;
00236         struct tm *t;
00237 
00238         /* initialize time period to last 24 hours */
00239         time(&t2);
00240         t1=(time_t)(t2-(60*60*24));
00241 
00242         /* get the arguments passed in the URL */
00243         process_cgivars();
00244 
00245         /* reset internal CGI variables */
00246         reset_cgi_vars();
00247         
00248         /* read the CGI configuration file */
00249         result=read_cgi_config_file(get_cgi_config_location());
00250         if(result==ERROR){
00251                 if(content_type==HTML_CONTENT){
00252                         document_header(CGI_ID,FALSE);
00253                         print_error(get_cgi_config_location(), ERROR_CGI_CFG_FILE);
00254                         document_footer(CGI_ID);
00255                         }
00256                 return ERROR;
00257                 }
00258 
00259         /* read the main configuration file */
00260         result=read_main_config_file(main_config_file);
00261         if(result==ERROR){
00262                 if(content_type==HTML_CONTENT){
00263                         document_header(CGI_ID,FALSE);
00264                         print_error(main_config_file, ERROR_CGI_MAIN_CFG);
00265                         document_footer(CGI_ID);
00266                         }
00267                 return ERROR;
00268                 }
00269 
00270         /* read all object configuration data */
00271         result=read_all_object_configuration_data(main_config_file,READ_ALL_OBJECT_DATA);
00272         if(result==ERROR){
00273                 if(content_type==HTML_CONTENT){
00274                         document_header(CGI_ID,FALSE);
00275                         print_error(NULL, ERROR_CGI_OBJECT_DATA);
00276                         document_footer(CGI_ID);
00277                         }
00278                 return ERROR;
00279                 }
00280 
00281         /* read all status data */
00282         result=read_all_status_data(get_cgi_config_location(),READ_ALL_STATUS_DATA);
00283         if(result==ERROR && daemon_check==TRUE){
00284                 if(content_type==HTML_CONTENT){
00285                         document_header(CGI_ID,FALSE);
00286                         print_error(NULL, ERROR_CGI_STATUS_DATA);
00287                         document_footer(CGI_ID);
00288                         }
00289                 free_memory();
00290                 return ERROR;
00291                 }
00292 
00293         document_header(CGI_ID,TRUE);
00294 
00295         /* get authentication information */
00296         get_authentication_information(&current_authdata);
00297 
00298         if(compute_time_from_parts==TRUE)
00299                 compute_report_times();
00300 
00301         /* make sure times are sane, otherwise swap them */
00302         if(t2<t1){
00303                 t3=t2;
00304                 t2=t1;
00305                 t1=t3;
00306                 }
00307                         
00308 
00309         if(content_type==HTML_CONTENT && display_header==TRUE){
00310 
00311                 /* begin top table */
00312                 printf("<table border=0 width=100%% cellspacing=0 cellpadding=0>\n");
00313                 printf("<tr>\n");
00314 
00315                 /* left column of the first row */
00316                 printf("<td align=left valign=top width=33%%>\n");
00317 
00318                 if(display_type==DISPLAY_HOST_HISTOGRAM)
00319                         snprintf(temp_buffer,sizeof(temp_buffer)-1,"Host Alert Histogram");
00320                 else if(display_type==DISPLAY_SERVICE_HISTOGRAM)
00321                         snprintf(temp_buffer,sizeof(temp_buffer)-1,"Service Alert Histogram");
00322                 else
00323                         snprintf(temp_buffer,sizeof(temp_buffer)-1,"Host and Service Alert Histogram");
00324                 temp_buffer[sizeof(temp_buffer)-1]='\x0';
00325                 display_info_table(temp_buffer,FALSE,&current_authdata, daemon_check);
00326 
00327                 if(display_type!=DISPLAY_NO_HISTOGRAM && input_type==GET_INPUT_NONE){
00328 
00329                         printf("<TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0 CLASS='linkBox'>\n");
00330                         printf("<TR><TD CLASS='linkBox'>\n");
00331 
00332                         if(display_type==DISPLAY_HOST_HISTOGRAM){
00333 #ifdef USE_TRENDS
00334                                 printf("<a href='%s?host=%s&t1=%lu&t2=%lu&assumestateretention=%s'>View Trends For This Host</a><BR>\n",TRENDS_CGI,url_encode(host_name),t1,t2,(assume_state_retention==TRUE)?"yes":"no");
00335 #endif
00336                                 printf("<a href='%s?host=%s&t1=%lu&t2=%lu&assumestateretention=%s&show_log_entries'>View Availability Report For This Host</a><BR>\n",AVAIL_CGI,url_encode(host_name),t1,t2,(assume_state_retention==TRUE)?"yes":"no");
00337                                 printf("<a href='%s?host=%s'>View Status Detail For This Host</a><BR>\n",STATUS_CGI,url_encode(host_name));
00338                                 printf("<a href='%s?host=%s'>View History For This Host</a><BR>\n",HISTORY_CGI,url_encode(host_name));
00339                                 printf("<a href='%s?host=%s'>View Notifications For This Host</a><BR>\n",NOTIFICATIONS_CGI,url_encode(host_name));
00340                                 }
00341                         else{
00342 #ifdef USE_TRENDS
00343                                 printf("<a href='%s?host=%s",TRENDS_CGI,url_encode(host_name));
00344 #endif
00345                                 printf("&service=%s&t1=%lu&t2=%lu&assumestateretention=%s'>View Trends For This Service</a><BR>\n",url_encode(service_desc),t1,t2,(assume_state_retention==TRUE)?"yes":"no");
00346                                 printf("<a href='%s?host=%s",AVAIL_CGI,url_encode(host_name));
00347                                 printf("&service=%s&t1=%lu&t2=%lu&assumestateretention=%s&show_log_entries'>View Availability Report For This Service</a><BR>\n",url_encode(service_desc),t1,t2,(assume_state_retention==TRUE)?"yes":"no");
00348                                 printf("<A HREF='%s?host=%s&",HISTORY_CGI,url_encode(host_name));
00349                                 printf("service=%s'>View History For This Service</A><BR>\n",url_encode(service_desc));
00350                                 printf("<A HREF='%s?host=%s&",NOTIFICATIONS_CGI,url_encode(host_name));
00351                                 printf("service=%s'>View Notifications For This Service</A><BR>\n",url_encode(service_desc));
00352                                 }
00353 
00354                         printf("</TD></TR>\n");
00355                         printf("</TABLE>\n");
00356                         }
00357 
00358                 printf("</td>\n");
00359 
00360                 /* center column of top row */
00361                 printf("<td align=center valign=top width=33%%>\n");
00362 
00363                 if(display_type!=DISPLAY_NO_HISTOGRAM && input_type==GET_INPUT_NONE){
00364 
00365                         /* find the host */
00366                         temp_host=find_host(host_name);
00367 
00368                         /* find the service */
00369                         temp_service=find_service(host_name,service_desc);
00370 
00371                         printf("<DIV ALIGN=CENTER CLASS='dataTitle'>\n");
00372                         if(display_type==DISPLAY_HOST_HISTOGRAM)
00373                                 printf("Host '%s'",(temp_host->display_name!=NULL)?temp_host->display_name:temp_host->name);
00374                         else if(display_type==DISPLAY_SERVICE_HISTOGRAM)
00375                                 printf("Service '%s' On Host '%s'",(temp_service->display_name!=NULL)?temp_service->display_name:temp_service->description,(temp_host->display_name!=NULL)?temp_host->display_name:temp_host->name);
00376                         printf("</DIV>\n");
00377 
00378                         printf("<BR>\n");
00379 
00380                         printf("<IMG SRC='%s%s' BORDER=0 ALT='%s Event Histogram' TITLE='%s Event Histogram'>\n",url_images_path,TRENDS_ICON,(display_type==DISPLAY_HOST_HISTOGRAM)?"Host":"Service",(display_type==DISPLAY_HOST_HISTOGRAM)?"Host":"Service");
00381 
00382                         printf("<BR CLEAR=ALL>\n");
00383 
00384                         get_time_string(&t1,start_timestring,sizeof(start_timestring)-1,SHORT_DATE_TIME);
00385                         get_time_string(&t2,end_timestring,sizeof(end_timestring)-1,SHORT_DATE_TIME);
00386                         printf("<div align=center class='reportRange'>%s to %s</div>\n",start_timestring,end_timestring);
00387 
00388                         get_time_breakdown((time_t)(t2-t1),&days,&hours,&minutes,&seconds);
00389                         printf("<div align=center class='reportDuration'>Duration: %dd %dh %dm %ds</div>\n",days,hours,minutes,seconds);
00390                         }
00391 
00392                 printf("</td>\n");
00393 
00394                 /* right hand column of top row */
00395                 printf("<td align=right valign=bottom width=33%%>\n");
00396 
00397                 printf("<form method=\"GET\" action=\"%s\">\n",HISTOGRAM_CGI);
00398                 printf("<table border=0 CLASS='optBox'>\n");
00399 
00400                 if(display_type!=DISPLAY_NO_HISTOGRAM && input_type==GET_INPUT_NONE){
00401 
00402                         printf("<tr><td CLASS='optBoxItem' valign=top align=left>Breakdown type:</td><td CLASS='optBoxItem' valign=top align=left>Initial states logged:</td></tr>\n");
00403                         printf("<tr><td CLASS='optBoxItem' valign=top align=left>\n");
00404 
00405                         printf("<input type='hidden' name='t1' value='%lu'>\n",(unsigned long)t1);
00406                         printf("<input type='hidden' name='t2' value='%lu'>\n",(unsigned long)t2);
00407                         printf("<input type='hidden' name='host' value='%s'>\n",escape_string(host_name));
00408                         if(display_type==DISPLAY_SERVICE_HISTOGRAM)
00409                                 printf("<input type='hidden' name='service' value='%s'>\n",escape_string(service_desc));
00410 
00411 
00412                         printf("<tr><td CLASS='optBoxItem' valign=top align=left>Report period:</td><td CLASS='optBoxItem' valign=top align=left>Assume state retention:</td></tr>\n");
00413                         printf("<tr><td CLASS='optBoxItem' valign=top align=left>\n");
00414                         printf("<select name='timeperiod'>\n");
00415                         printf("<option value=custom>[ Current time range ]\n");
00416                         printf("<option value=today %s>Today\n",(timeperiod_type==TIMEPERIOD_TODAY)?"SELECTED":"");
00417                         printf("<option value=last24hours %s>Last 24 Hours\n",(timeperiod_type==TIMEPERIOD_LAST24HOURS)?"SELECTED":"");
00418                         printf("<option value=yesterday %s>Yesterday\n",(timeperiod_type==TIMEPERIOD_YESTERDAY)?"SELECTED":"");
00419                         printf("<option value=thisweek %s>This Week\n",(timeperiod_type==TIMEPERIOD_THISWEEK)?"SELECTED":"");
00420                         printf("<option value=last7days %s>Last 7 Days\n",(timeperiod_type==TIMEPERIOD_LAST7DAYS)?"SELECTED":"");
00421                         printf("<option value=lastweek %s>Last Week\n",(timeperiod_type==TIMEPERIOD_LASTWEEK)?"SELECTED":"");
00422                         printf("<option value=thismonth %s>This Month\n",(timeperiod_type==TIMEPERIOD_THISMONTH)?"SELECTED":"");
00423                         printf("<option value=last31days %s>Last 31 Days\n",(timeperiod_type==TIMEPERIOD_LAST31DAYS)?"SELECTED":"");
00424                         printf("<option value=lastmonth %s>Last Month\n",(timeperiod_type==TIMEPERIOD_LASTMONTH)?"SELECTED":"");
00425                         printf("<option value=thisyear %s>This Year\n",(timeperiod_type==TIMEPERIOD_THISYEAR)?"SELECTED":"");
00426                         printf("<option value=lastyear %s>Last Year\n",(timeperiod_type==TIMEPERIOD_LASTYEAR)?"SELECTED":"");
00427                         printf("</select>\n");
00428                         printf("</td><td CLASS='optBoxItem' valign=top align=left>\n");
00429                         printf("<select name='assumestateretention'>\n");
00430                         printf("<option value=yes %s>yes\n",(assume_state_retention==TRUE)?"SELECTED":"");
00431                         printf("<option value=no %s>no\n",(assume_state_retention==TRUE)?"":"SELECTED");
00432                         printf("</select>\n");
00433                         printf("</td></tr>\n");
00434 
00435                         printf("<select name='breakdown'>\n");
00436                         printf("<option value=monthly %s>Month\n",(breakdown_type==BREAKDOWN_MONTHLY)?"SELECTED":"");
00437                         printf("<option value=dayofmonth %s>Day of the Month\n",(breakdown_type==BREAKDOWN_DAY_OF_MONTH)?"SELECTED":"");
00438                         printf("<option value=dayofweek %s>Day of the Week\n",(breakdown_type==BREAKDOWN_DAY_OF_WEEK)?"SELECTED":"");
00439                         printf("<option value=hourly %s>Hour of the Day\n",(breakdown_type==BREAKDOWN_HOURLY)?"SELECTED":"");
00440                         printf("</select>\n");
00441                         printf("</td><td CLASS='optBoxItem' valign=top align=left>\n");
00442                         printf("<select name='initialstateslogged'>\n");
00443                         printf("<option value=yes %s>yes\n",(initial_states_logged==TRUE)?"SELECTED":"");
00444                         printf("<option value=no %s>no\n",(initial_states_logged==TRUE)?"":"SELECTED");
00445                         printf("</select>\n");
00446                         printf("</td></tr>\n");
00447 
00448                         printf("<tr><td CLASS='optBoxItem' valign=top align=left>Events to graph:</td><td CLASS='optBoxItem' valign=top align=left>Ignore repeated states:</td></tr>\n");
00449                         printf("<tr><td CLASS='optBoxItem' valign=top align=left>\n");
00450                         printf("<select name='graphevents'>\n");
00451                         if(display_type==DISPLAY_HOST_HISTOGRAM){
00452                                 printf("<option value=%d %s>All host events\n",GRAPH_HOST_ALL,(graph_events==GRAPH_HOST_ALL)?"SELECTED":"");
00453                                 printf("<option value=%d %s>Host problem events\n",GRAPH_HOST_PROBLEMS,(graph_events==GRAPH_HOST_PROBLEMS)?"SELECTED":"");
00454                                 printf("<option value=%d %s>Host up events\n",GRAPH_HOST_UP,(graph_events==GRAPH_HOST_UP)?"SELECTED":"");
00455                                 printf("<option value=%d %s>Host down events\n",GRAPH_HOST_DOWN,(graph_events==GRAPH_HOST_DOWN)?"SELECTED":"");
00456                                 printf("<option value=%d %s>Host unreachable events\n",GRAPH_HOST_UNREACHABLE,(graph_events==GRAPH_HOST_UNREACHABLE)?"SELECTED":"");
00457                                 }
00458                         else{
00459                                 printf("<option value=%d %s>All service events\n",GRAPH_SERVICE_ALL,(graph_events==GRAPH_SERVICE_ALL)?"SELECTED":"");
00460                                 printf("<option value=%d %s>Service problem events\n",GRAPH_SERVICE_PROBLEMS,(graph_events==GRAPH_SERVICE_PROBLEMS)?"SELECTED":"");
00461                                 printf("<option value=%d %s>Service ok events\n",GRAPH_SERVICE_OK,(graph_events==GRAPH_SERVICE_OK)?"SELECTED":"");
00462                                 printf("<option value=%d %s>Service warning events\n",GRAPH_SERVICE_WARNING,(graph_events==GRAPH_SERVICE_WARNING)?"SELECTED":"");
00463                                 printf("<option value=%d %s>Service unknown events\n",GRAPH_SERVICE_UNKNOWN,(graph_events==GRAPH_SERVICE_UNKNOWN)?"SELECTED":"");
00464                                 printf("<option value=%d %s>Service critical events\n",GRAPH_SERVICE_CRITICAL,(graph_events==GRAPH_SERVICE_CRITICAL)?"SELECTED":"");
00465                                 }
00466                         printf("</select>\n");
00467                         printf("</td><td CLASS='optBoxItem' valign=top align=left>\n");
00468                         printf("<select name='newstatesonly'>\n");
00469                         printf("<option value=yes %s>yes\n",(new_states_only==TRUE)?"SELECTED":"");
00470                         printf("<option value=no %s>no\n",(new_states_only==TRUE)?"":"SELECTED");
00471                         printf("</select>\n");
00472                         printf("</td></tr>\n");
00473 
00474                         printf("<tr><td CLASS='optBoxItem' valign=top align=left>State types to graph:</td><td CLASS='optBoxItem' valign=top align=left></td></tr>\n");
00475                         printf("<tr><td CLASS='optBoxItem' valign=top align=left>\n");
00476                         printf("<select name='graphstatetypes'>\n");
00477                         printf("<option value=%d %s>Hard states\n",GRAPH_HARD_STATETYPES,(graph_statetypes==GRAPH_HARD_STATETYPES)?"SELECTED":"");
00478                         printf("<option value=%d %s>Soft states\n",GRAPH_SOFT_STATETYPES,(graph_statetypes==GRAPH_SOFT_STATETYPES)?"SELECTED":"");
00479                         printf("<option value=%d %s>Hard and soft states\n",GRAPH_ALL_STATETYPES,(graph_statetypes==GRAPH_ALL_STATETYPES)?"SELECTED":"");
00480                         printf("</select>\n");
00481                         printf("</td><td CLASS='optBoxItem' valign=top align=left>\n");
00482                         printf("<input type='submit' value='Update'>\n");
00483                         printf("</td></tr>\n");
00484                         }
00485 
00486                 /* display context-sensitive help */
00487                 printf("<tr><td></td><td align=right valign=bottom>\n");
00488                 if(display_type!=DISPLAY_NO_HISTOGRAM && input_type==GET_INPUT_NONE){
00489                         if(display_type==DISPLAY_HOST_HISTOGRAM)
00490                                 display_context_help(CONTEXTHELP_HISTOGRAM_HOST);
00491                         else
00492                                 display_context_help(CONTEXTHELP_HISTOGRAM_SERVICE);
00493                         }
00494                 else if(display_type==DISPLAY_NO_HISTOGRAM || input_type!=GET_INPUT_NONE){
00495                         if(input_type==GET_INPUT_NONE)
00496                                 display_context_help(CONTEXTHELP_HISTOGRAM_MENU1);
00497                         else if(input_type==GET_INPUT_TARGET_TYPE)
00498                                 display_context_help(CONTEXTHELP_HISTOGRAM_MENU1);
00499                         else if(input_type==GET_INPUT_HOST_TARGET)
00500                                 display_context_help(CONTEXTHELP_HISTOGRAM_MENU2);
00501                         else if(input_type==GET_INPUT_SERVICE_TARGET)
00502                                 display_context_help(CONTEXTHELP_HISTOGRAM_MENU3);
00503                         else if(input_type==GET_INPUT_OPTIONS)
00504                                 display_context_help(CONTEXTHELP_HISTOGRAM_MENU4);
00505                         }
00506                 printf("</td></tr>\n");
00507 
00508                 printf("</table>\n");
00509                 printf("</form>\n");
00510 
00511                 printf("</td>\n");
00512 
00513                 /* end of top table */
00514                 printf("</tr>\n");
00515                 printf("</table>\n");
00516                 }
00517 
00518         /* check authorization... */
00519         if(display_type==DISPLAY_HOST_HISTOGRAM){
00520                 temp_host=find_host(host_name);
00521                 if(temp_host==NULL || is_authorized_for_host(temp_host,&current_authdata)==FALSE)
00522                         is_authorized=FALSE;
00523                 }
00524         else if(display_type==DISPLAY_SERVICE_HISTOGRAM){
00525                 temp_service=find_service(host_name,service_desc);
00526                 if(temp_service==NULL || is_authorized_for_service(temp_service,&current_authdata)==FALSE)
00527                         is_authorized=FALSE;
00528                 }
00529         if(is_authorized==FALSE){
00530 
00531                 if(content_type==HTML_CONTENT) {
00532                         if (display_type==DISPLAY_HOST_HISTOGRAM)
00533                                 print_generic_error_message("It appears as though you are not authorized to view information for the specified host...",NULL,0);
00534                         else
00535                                 print_generic_error_message("It appears as though you are not authorized to view information for the specified service...",NULL,0);
00536                 }
00537 
00538                 document_footer(CGI_ID);
00539                 free_memory();
00540                 return ERROR;
00541         }
00542 
00543         if(display_type!=DISPLAY_NO_HISTOGRAM && input_type==GET_INPUT_NONE){
00544 
00545                 /* print URL to image */
00546                 if(content_type==HTML_CONTENT){
00547 
00548                         printf("<BR><BR>\n");
00549                         printf("<DIV ALIGN=CENTER>\n");
00550                         printf("<IMG SRC='%s?createimage&t1=%lu&t2=%lu",HISTOGRAM_CGI,(unsigned long)t1,(unsigned long)t2);
00551                         printf("&host=%s",url_encode(host_name));
00552                         if(display_type==DISPLAY_SERVICE_HISTOGRAM)
00553                                 printf("&service=%s",url_encode(service_desc));
00554                         printf("&breakdown=");
00555                         if(breakdown_type==BREAKDOWN_MONTHLY)
00556                                 printf("monthly");
00557                         else if(breakdown_type==BREAKDOWN_DAY_OF_MONTH)
00558                                 printf("dayofmonth");
00559                         else if(breakdown_type==BREAKDOWN_DAY_OF_WEEK)
00560                                 printf("dayofweek");
00561                         else
00562                                 printf("hourly");
00563                         printf("&assumestateretention=%s",(assume_state_retention==TRUE)?"yes":"no");
00564                         printf("&initialstateslogged=%s",(initial_states_logged==TRUE)?"yes":"no");
00565                         printf("&newstatesonly=%s",(new_states_only==TRUE)?"yes":"no");
00566                         printf("&graphevents=%d",graph_events);
00567                         printf("&graphstatetypes=%d",graph_statetypes);
00568                         printf("' BORDER=0 name='histogramimage'>\n");
00569                         printf("</DIV>\n");
00570                         }
00571 
00572                 /* read and process state data */
00573                 else{
00574 
00575                         /* allocate memory */
00576                         tsdata=NULL;
00577                         if(breakdown_type==BREAKDOWN_MONTHLY)
00578                                 total_buckets=12;
00579                         else if(breakdown_type==BREAKDOWN_DAY_OF_MONTH)
00580                                 total_buckets=31;
00581                         else if(breakdown_type==BREAKDOWN_DAY_OF_WEEK)
00582                                 total_buckets=7;
00583                         else
00584                                 total_buckets=96;
00585 
00586                         tsdata=(timeslice_data *)malloc(sizeof(timeslice_data)*total_buckets);
00587                         if(tsdata==NULL)
00588                                 return ERROR;
00589 
00590                         for(x=0;x<total_buckets;x++){
00591                                 tsdata[x].service_ok=0L;
00592                                 tsdata[x].service_unknown=0L;
00593                                 tsdata[x].service_warning=0L;
00594                                 tsdata[x].service_critical=0L;
00595                                 tsdata[x].host_up=0L;
00596                                 tsdata[x].host_down=0L;
00597                                 tsdata[x].host_unreachable=0L;
00598                                 }
00599 
00600                         /* read in all necessary archived state data */
00601                         read_archived_state_data();
00602 
00603 #ifdef DEBUG
00604                         printf("Done reading archived state data.\n");
00605 #endif
00606 
00607                         /* location of image template */
00608                         snprintf(image_template,sizeof(image_template)-1,"%s/%s",physical_images_path,HISTOGRAM_IMAGE);
00609                         image_template[sizeof(image_template)-1]='\x0';
00610 
00611                         /* allocate buffer for storing image */
00612                         image_file=fopen(image_template,"r");
00613                         if(image_file!=NULL){
00614                                 histogram_image=gdImageCreateFromPng(image_file);
00615                                 fclose(image_file);
00616                                 }
00617                         if(histogram_image==NULL)
00618                                 histogram_image=gdImageCreate(image_width,image_height);
00619                         if(histogram_image==NULL){
00620 #ifdef DEBUG
00621                                 printf("Error: Could not allocate memory for image\n");
00622 #endif
00623                                 return ERROR;
00624                                 }
00625 
00626                         /* allocate colors used for drawing */
00627                         color_white=gdImageColorAllocate(histogram_image,255,255,255);
00628                         color_black=gdImageColorAllocate(histogram_image,0,0,0);
00629                         color_red=gdImageColorAllocate(histogram_image,255,0,0);
00630                         color_darkred=gdImageColorAllocate(histogram_image,128,0,0);
00631                         color_green=gdImageColorAllocate(histogram_image,0,128,0);
00632                         color_yellow=gdImageColorAllocate(histogram_image,176,178,20);
00633                         color_orange=gdImageColorAllocate(histogram_image,255,100,25);
00634                         color_lightgray=gdImageColorAllocate(histogram_image,192,192,192);
00635 
00636                         /* set transparency index */
00637                         gdImageColorTransparent(histogram_image,color_white);
00638 
00639                         /* make sure the graphic is interlaced */
00640                         gdImageInterlace(histogram_image,1);
00641 
00642 #ifdef DEBUG
00643                         printf("Starting to graph data...\n");
00644 #endif
00645 
00646                         /* graph archived state histogram data */
00647                         graph_all_histogram_data();
00648 
00649 #ifdef DEBUG
00650                         printf("Done graphing data.\n");
00651 #endif
00652 
00653                         /* use STDOUT for writing the image data... */
00654                         image_file=stdout;
00655 
00656 #ifdef DEBUG
00657                         image_file=fopen("/tmp/histogram.png","w");
00658 #endif
00659 
00660                         /* write the image to to STDOUT */
00661                         gdImagePng(histogram_image,image_file);
00662         
00663 #ifdef DEBUG            
00664                         fclose(image_file);
00665 #endif
00666 
00667                         /* free memory allocated to image */
00668                         gdImageDestroy(histogram_image);
00669 
00670                         /* free memory allocated for data */
00671                         free(tsdata);
00672                         }
00673                 }
00674 
00675 
00676         /* show user a selection of hosts and services to choose from... */
00677         if(display_type==DISPLAY_NO_HISTOGRAM || input_type!=GET_INPUT_NONE){
00678 
00679                 /* ask the user for what host they want a report for */
00680                 if(input_type==GET_INPUT_HOST_TARGET){
00681 
00682                         printf("<P><DIV ALIGN=CENTER>\n");
00683                         printf("<DIV CLASS='reportSelectTitle'>Step 2: Select Host</DIV>\n");
00684                         printf("</DIV></P>\n");
00685 
00686                         printf("<P><DIV ALIGN=CENTER>\n");
00687 
00688                         printf("<form method=\"GET\" action=\"%s\">\n",HISTOGRAM_CGI);
00689                         printf("<input type='hidden' name='input' value='getoptions'>\n");
00690 
00691                         printf("<TABLE BORDER=0 cellspacing=0 cellpadding=10>\n");
00692                         printf("<tr><td class='reportSelectSubTitle' valign=center>Host:</td>\n");
00693                         printf("<td class='reportSelectItem' valign=center>\n");
00694                         printf("<select name='host'>\n");
00695 
00696                         for(temp_host=host_list;temp_host!=NULL;temp_host=temp_host->next){
00697                                 if(is_authorized_for_host(temp_host,&current_authdata)==TRUE)
00698                                         printf("<option value='%s'>%s\n",escape_string(temp_host->name),(temp_host->display_name!=NULL)?temp_host->display_name:temp_host->name);
00699                                 }
00700 
00701                         printf("</select>\n");
00702                         printf("</td></tr>\n");
00703 
00704                         printf("<tr><td></td><td class='reportSelectItem'>\n");
00705                         printf("<input type='submit' value='Continue to Step 3'>\n");
00706                         printf("</td></tr>\n");
00707 
00708                         printf("</TABLE>\n");
00709                         printf("</form>\n");
00710 
00711                         printf("</DIV></P>\n");
00712                         }
00713 
00714                 /* ask the user for what service they want a report for */
00715                 else if(input_type==GET_INPUT_SERVICE_TARGET){
00716 
00717                         printf("<SCRIPT LANGUAGE='JavaScript'>\n");
00718                         printf("function gethostname(hostindex){\n");
00719                         printf("hostnames=[");
00720 
00721                         for(temp_service=service_list;temp_service!=NULL;temp_service=temp_service->next){
00722                                 if(is_authorized_for_service(temp_service,&current_authdata)==TRUE){
00723                                         if(found==TRUE)
00724                                                 printf(",");
00725                                         else
00726                                                 first_service=temp_service->host_name;
00727                                         printf(" \"%s\"",temp_service->host_name);
00728                                         found=TRUE;
00729                                         }
00730                                 }
00731                 
00732                         printf(" ]\n");
00733                         printf("return hostnames[hostindex];\n");
00734                         printf("}\n");
00735                         printf("</SCRIPT>\n");
00736 
00737 
00738                         printf("<P><DIV ALIGN=CENTER>\n");
00739                         printf("<DIV CLASS='reportSelectTitle'>Step 2: Select Service</DIV>\n");
00740                         printf("</DIV></P>\n");
00741 
00742                         printf("<P><DIV ALIGN=CENTER>\n");
00743                         
00744                         printf("<form method=\"GET\" action=\"%s\" name=\"serviceform\">\n",HISTOGRAM_CGI);
00745                         printf("<input type='hidden' name='input' value='getoptions'>\n");
00746                         printf("<input type='hidden' name='host' value='%s'>\n",(first_service==NULL)?"unknown":(char *)escape_string(first_service));
00747 
00748                         printf("<TABLE BORDER=0 cellpadding=5>\n");
00749                         printf("<tr><td class='reportSelectSubTitle'>Service:</td>\n");
00750                         printf("<td class='reportSelectItem'>\n");
00751                         printf("<select name='service' onFocus='document.serviceform.host.value=gethostname(this.selectedIndex);' onChange='document.serviceform.host.value=gethostname(this.selectedIndex);'>\n");
00752 
00753                         for(temp_service=service_list;temp_service!=NULL;temp_service=temp_service->next){
00754                                 if(is_authorized_for_service(temp_service,&current_authdata)==TRUE)
00755                                         temp_host=find_host(temp_service->host_name);
00756                                         printf("<option value='%s'>%s;%s\n",escape_string(temp_service->description),(temp_host->display_name!=NULL)?temp_host->display_name:temp_host->name,(temp_service->display_name!=NULL)?temp_service->display_name:temp_service->description);
00757                                 }
00758 
00759                         printf("</select>\n");
00760                         printf("</td></tr>\n");
00761 
00762                         printf("<tr><td></td><td class='reportSelectItem'>\n");
00763                         printf("<input type='submit' value='Continue to Step 3'>\n");
00764                         printf("</td></tr>\n");
00765 
00766                         printf("</TABLE>\n");
00767                         printf("</form>\n");
00768 
00769                         printf("</DIV></P>\n");
00770                         }
00771 
00772                 /* ask the user for report range and options */
00773                 else if(input_type==GET_INPUT_OPTIONS){
00774 
00775                         time(&current_time);
00776                         t=localtime(&current_time);
00777 
00778                         start_day=1;
00779                         start_year=t->tm_year+1900;
00780                         end_day=t->tm_mday;
00781                         end_year=t->tm_year+1900;
00782 
00783                         printf("<P><DIV ALIGN=CENTER>\n");
00784                         printf("<DIV CLASS='reportSelectTitle'>Step 3: Select Report Options</DIV>\n");
00785                         printf("</DIV></P>\n");
00786 
00787                         printf("<P><DIV ALIGN=CENTER>\n");
00788 
00789                         printf("<form method=\"GET\" action=\"%s\">\n",HISTOGRAM_CGI);
00790                         printf("<input type='hidden' name='host' value='%s'>\n",escape_string(host_name));
00791                         if(display_type==DISPLAY_SERVICE_HISTOGRAM)
00792                                 printf("<input type='hidden' name='service' value='%s'>\n",escape_string(service_desc));
00793 
00794                         printf("<TABLE BORDER=0 cellpadding=5>\n");
00795                         printf("<tr><td class='reportSelectSubTitle' align=right>Report Period:</td>\n");
00796                         printf("<td class='reportSelectItem'>\n");
00797                         printf("<select name='timeperiod'>\n");
00798                         printf("<option value=today>Today\n");
00799                         printf("<option value=last24hours>Last 24 Hours\n");
00800                         printf("<option value=yesterday>Yesterday\n");
00801                         printf("<option value=thisweek>This Week\n");
00802                         printf("<option value=last7days SELECTED>Last 7 Days\n");
00803                         printf("<option value=lastweek>Last Week\n");
00804                         printf("<option value=thismonth>This Month\n");
00805                         printf("<option value=last31days>Last 31 Days\n");
00806                         printf("<option value=lastmonth>Last Month\n");
00807                         printf("<option value=thisyear>This Year\n");
00808                         printf("<option value=lastyear>Last Year\n");
00809                         printf("<option value=custom>* CUSTOM REPORT PERIOD *\n");
00810                         printf("</select>\n");
00811                         printf("</td></tr>\n");
00812 
00813                         printf("<tr><td valign=top calss='reportSelectSubTitle'>If Custom Report Period...</td></tr>\n");
00814 
00815                         printf("<tr>");
00816                         printf("<td valign=top class='reportSelectSubTitle'>Start Date (Inclusive):</td>\n");
00817                         printf("<td align=left valign=top class='reportSelectItem'>");
00818                         printf("<select name='smon'>\n");
00819                         printf("<option value='1' %s>January\n",(t->tm_mon==0)?"SELECTED":"");
00820                         printf("<option value='2' %s>February\n",(t->tm_mon==1)?"SELECTED":"");
00821                         printf("<option value='3' %s>March\n",(t->tm_mon==2)?"SELECTED":"");
00822                         printf("<option value='4' %s>April\n",(t->tm_mon==3)?"SELECTED":"");
00823                         printf("<option value='5' %s>May\n",(t->tm_mon==4)?"SELECTED":"");
00824                         printf("<option value='6' %s>June\n",(t->tm_mon==5)?"SELECTED":"");
00825                         printf("<option value='7' %s>July\n",(t->tm_mon==6)?"SELECTED":"");
00826                         printf("<option value='8' %s>August\n",(t->tm_mon==7)?"SELECTED":"");
00827                         printf("<option value='9' %s>September\n",(t->tm_mon==8)?"SELECTED":"");
00828                         printf("<option value='10' %s>October\n",(t->tm_mon==9)?"SELECTED":"");
00829                         printf("<option value='11' %s>November\n",(t->tm_mon==10)?"SELECTED":"");
00830                         printf("<option value='12' %s>December\n",(t->tm_mon==11)?"SELECTED":"");
00831                         printf("</select>\n ");
00832                         printf("<input type='text' size='2' maxlength='2' name='sday' value='%d'> ",start_day);
00833                         printf("<input type='text' size='4' maxlength='4' name='syear' value='%d'>",start_year);
00834                         printf("<input type='hidden' name='shour' value='0'>\n");
00835                         printf("<input type='hidden' name='smin' value='0'>\n");
00836                         printf("<input type='hidden' name='ssec' value='0'>\n");
00837                         printf("</td>\n");
00838                         printf("</tr>\n");
00839 
00840                         printf("<tr>");
00841                         printf("<td valign=top class='reportSelectSubTitle'>End Date (Inclusive):</td>\n");
00842                         printf("<td align=left valign=top class='reportSelectItem'>");
00843                         printf("<select name='emon'>\n");
00844                         printf("<option value='1' %s>January\n",(t->tm_mon==0)?"SELECTED":"");
00845                         printf("<option value='2' %s>February\n",(t->tm_mon==1)?"SELECTED":"");
00846                         printf("<option value='3' %s>March\n",(t->tm_mon==2)?"SELECTED":"");
00847                         printf("<option value='4' %s>April\n",(t->tm_mon==3)?"SELECTED":"");
00848                         printf("<option value='5' %s>May\n",(t->tm_mon==4)?"SELECTED":"");
00849                         printf("<option value='6' %s>June\n",(t->tm_mon==5)?"SELECTED":"");
00850                         printf("<option value='7' %s>July\n",(t->tm_mon==6)?"SELECTED":"");
00851                         printf("<option value='8' %s>August\n",(t->tm_mon==7)?"SELECTED":"");
00852                         printf("<option value='9' %s>September\n",(t->tm_mon==8)?"SELECTED":"");
00853                         printf("<option value='10' %s>October\n",(t->tm_mon==9)?"SELECTED":"");
00854                         printf("<option value='11' %s>November\n",(t->tm_mon==10)?"SELECTED":"");
00855                         printf("<option value='12' %s>December\n",(t->tm_mon==11)?"SELECTED":"");
00856                         printf("</select>\n ");
00857                         printf("<input type='text' size='2' maxlength='2' name='eday' value='%d'> ",end_day);
00858                         printf("<input type='text' size='4' maxlength='4' name='eyear' value='%d'>",end_year);
00859                         printf("<input type='hidden' name='ehour' value='24'>\n");
00860                         printf("<input type='hidden' name='emin' value='0'>\n");
00861                         printf("<input type='hidden' name='esec' value='0'>\n");
00862                         printf("</td>\n");
00863                         printf("</tr>\n");
00864 
00865                         printf("<tr><td colspan=2><br></td></tr>\n");
00866 
00867                         printf("<tr><td class='reportSelectSubTitle' align=right>Statistics Breakdown:</td>\n");
00868                         printf("<td class='reportSelectItem'>\n");
00869                         printf("<select name='breakdown'>\n");
00870                         printf("<option value=monthly>Month\n");
00871                         printf("<option value=dayofmonth SELECTED>Day of the Month\n");
00872                         printf("<option value=dayofweek>Day of the Week\n");
00873                         printf("<option value=hourly>Hour of the Day\n");
00874                         printf("</select>\n");
00875                         printf("</td></tr>\n");
00876 
00877                         printf("<tr><td class='reportSelectSubTitle' align=right>Events To Graph:</td>\n");
00878                         printf("<td class='reportSelectItem'>\n");
00879                         printf("<select name='graphevents'>\n");
00880                         if(display_type==DISPLAY_HOST_HISTOGRAM){
00881                                 printf("<option value=%d %s>All host events\n",GRAPH_HOST_ALL,(graph_events==GRAPH_HOST_ALL)?"SELECTED":"");
00882                                 printf("<option value=%d %s>Host problem events\n",GRAPH_HOST_PROBLEMS,(graph_events==GRAPH_HOST_PROBLEMS)?"SELECTED":"");
00883                                 printf("<option value=%d %s>Host up events\n",GRAPH_HOST_UP,(graph_events==GRAPH_HOST_UP)?"SELECTED":"");
00884                                 printf("<option value=%d %s>Host down events\n",GRAPH_HOST_DOWN,(graph_events==GRAPH_HOST_DOWN)?"SELECTED":"");
00885                                 printf("<option value=%d %s>Host unreachable events\n",GRAPH_HOST_UNREACHABLE,(graph_events==GRAPH_HOST_UNREACHABLE)?"SELECTED":"");
00886                                 }
00887                         else{
00888                                 printf("<option value=%d %s>All service events\n",GRAPH_SERVICE_ALL,(graph_events==GRAPH_SERVICE_ALL)?"SELECTED":"");
00889                                 printf("<option value=%d %s>Service problem events\n",GRAPH_SERVICE_PROBLEMS,(graph_events==GRAPH_SERVICE_PROBLEMS)?"SELECTED":"");
00890                                 printf("<option value=%d %s>Service ok events\n",GRAPH_SERVICE_OK,(graph_events==GRAPH_SERVICE_OK)?"SELECTED":"");
00891                                 printf("<option value=%d %s>Service warning events\n",GRAPH_SERVICE_WARNING,(graph_events==GRAPH_SERVICE_WARNING)?"SELECTED":"");
00892                                 printf("<option value=%d %s>Service unknown events\n",GRAPH_SERVICE_UNKNOWN,(graph_events==GRAPH_SERVICE_UNKNOWN)?"SELECTED":"");
00893                                 printf("<option value=%d %s>Service critical events\n",GRAPH_SERVICE_CRITICAL,(graph_events==GRAPH_SERVICE_CRITICAL)?"SELECTED":"");
00894                                 }
00895                         printf("</select>\n");
00896                         printf("</td></tr>\n");
00897 
00898                         printf("<tr><td class='reportSelectSubTitle' align=right>State Types To Graph:</td>\n");
00899                         printf("<td class='reportSelectItem'>\n");
00900                         printf("<select name='graphstatetypes'>\n");
00901                         printf("<option value=%d>Hard states\n",GRAPH_HARD_STATETYPES);
00902                         printf("<option value=%d>Soft states\n",GRAPH_SOFT_STATETYPES);
00903                         printf("<option value=%d SELECTED>Hard and soft states\n",GRAPH_ALL_STATETYPES);
00904                         printf("</select>\n");
00905                         printf("</td></tr>\n");
00906 
00907                         printf("<tr><td class='reportSelectSubTitle' align=right>Assume State Retention:</td>\n");
00908                         printf("<td class='reportSelectItem'>\n");
00909                         printf("<select name='assumestateretention'>\n");
00910                         printf("<option value='yes'>Yes\n");
00911                         printf("<option value='no'>No\n");
00912                         printf("</select>\n");
00913                         printf("</td></tr>\n");
00914 
00915                         printf("<tr><td class='reportSelectSubTitle' align=right>Initial States Logged:</td>\n");
00916                         printf("<td class='reportSelectItem'>\n");
00917                         printf("<select name='initialstateslogged'>\n");
00918                         printf("<option value='yes'>Yes\n");
00919                         printf("<option value='no' SELECTED>No\n");
00920                         printf("</select>\n");
00921                         printf("</td></tr>\n");
00922 
00923                         printf("<tr><td class='reportSelectSubTitle' align=right>Ignore Repeated States:</td>\n");
00924                         printf("<td class='reportSelectItem'>\n");
00925                         printf("<select name='newstatesonly'>\n");
00926                         printf("<option value='yes'>Yes\n");
00927                         printf("<option value='no' SELECTED>No\n");
00928                         printf("</select>\n");
00929                         printf("</td></tr>\n");
00930 
00931                         printf("<tr><td></td><td class='reportSelectItem'><input type='submit' value='Create Report'></td></tr>\n");
00932 
00933                         printf("</TABLE>\n");
00934                         printf("</form>\n");
00935 
00936                         printf("</DIV></P>\n");
00937                         }
00938 
00939                 /* as the user whether they want a graph for a host or service */
00940                 else{
00941                         printf("<P><DIV ALIGN=CENTER>\n");
00942                         printf("<DIV CLASS='reportSelectTitle'>Step 1: Select Report Type</DIV>\n");
00943                         printf("</DIV></P>\n");
00944 
00945                         printf("<P><DIV ALIGN=CENTER>\n");
00946 
00947                         printf("<form method=\"GET\" action=\"%s\">\n",HISTOGRAM_CGI);
00948 
00949                         printf("<TABLE BORDER=0 cellpadding=5>\n");
00950                         printf("<tr><td class='reportSelectSubTitle' align=right>Type:</td>\n");
00951                         printf("<td class='reportSelectItem'>\n");
00952                         printf("<select name='input'>\n");
00953                         printf("<option value=gethost>Host\n");
00954                         printf("<option value=getservice>Service\n");
00955                         printf("</select>\n");
00956                         printf("</td></tr>\n");
00957 
00958                         printf("<tr><td></td><td class='reportSelectItem'>\n");
00959                         printf("<input type='submit' value='Continue to Step 2'>\n");
00960                         printf("</td></tr>\n");
00961 
00962                         printf("</TABLE>\n");
00963                         printf("</form>\n");
00964 
00965                         printf("</DIV></P>\n");
00966                         }
00967                 
00968                 }
00969 
00970         document_footer(CGI_ID);
00971 
00972         /* free all other allocated memory */
00973         free_memory();
00974 
00975         return OK;
00976 }
00977 
00978 int process_cgivars(void){
00979         char **variables;
00980         int error=FALSE;
00981         int x;
00982 
00983         variables=getcgivars();
00984 
00985         for(x=0;variables[x]!=NULL;x++){
00986 
00987                 /* do some basic length checking on the variable identifier to prevent buffer overflows */
00988                 if(strlen(variables[x])>=MAX_INPUT_BUFFER-1){
00989                         x++;
00990                         continue;
00991                         }
00992 
00993                 /* we found the host argument */
00994                 else if(!strcmp(variables[x],"host")){
00995                         x++;
00996                         if(variables[x]==NULL){
00997                                 error=TRUE;
00998                                 break;
00999                                 }
01000 
01001                         if((host_name=(char *)strdup(variables[x]))==NULL)
01002                                 host_name="";
01003                         strip_html_brackets(host_name);
01004 
01005                         display_type=DISPLAY_HOST_HISTOGRAM;
01006                         }
01007 
01008                 /* we found the node width argument */
01009                 else if(!strcmp(variables[x],"service")){
01010                         x++;
01011                         if(variables[x]==NULL){
01012                                 error=TRUE;
01013                                 break;
01014                                 }
01015 
01016                         if((service_desc=(char *)strdup(variables[x]))==NULL)
01017                                 service_desc="";
01018                         strip_html_brackets(service_desc);
01019 
01020                         display_type=DISPLAY_SERVICE_HISTOGRAM;
01021                         }
01022 
01023                 /* we found first time argument */
01024                 else if(!strcmp(variables[x],"t1")){
01025                         x++;
01026                         if(variables[x]==NULL){
01027                                 error=TRUE;
01028                                 break;
01029                                 }
01030 
01031                         t1=(time_t)strtoul(variables[x],NULL,10);
01032                         timeperiod_type=TIMEPERIOD_CUSTOM;
01033                         }
01034 
01035                 /* we found first time argument */
01036                 else if(!strcmp(variables[x],"t2")){
01037                         x++;
01038                         if(variables[x]==NULL){
01039                                 error=TRUE;
01040                                 break;
01041                                 }
01042 
01043                         t2=(time_t)strtoul(variables[x],NULL,10);
01044                         timeperiod_type=TIMEPERIOD_CUSTOM;
01045                         }
01046 
01047                 /* we found the image creation option */
01048                 else if(!strcmp(variables[x],"createimage")){
01049                         content_type=IMAGE_CONTENT;
01050                         }
01051 
01052                 /* we found the backtrack archives argument */
01053                 else if(!strcmp(variables[x],"backtrack")){
01054                         x++;
01055                         if(variables[x]==NULL){
01056                                 error=TRUE;
01057                                 break;
01058                                 }
01059 
01060                         backtrack_archives=atoi(variables[x]);
01061                         if(backtrack_archives<0)
01062                                 backtrack_archives=0;
01063                         if(backtrack_archives>MAX_ARCHIVE_BACKTRACKS)
01064                                 backtrack_archives=MAX_ARCHIVE_BACKTRACKS;
01065                         }
01066 
01067                 /* we found the standard timeperiod argument */
01068                 else if(!strcmp(variables[x],"timeperiod")){
01069                         x++;
01070                         if(variables[x]==NULL){
01071                                 error=TRUE;
01072                                 break;
01073                                 }
01074 
01075                         if(!strcmp(variables[x],"today"))
01076                                 timeperiod_type=TIMEPERIOD_TODAY;
01077                         else if(!strcmp(variables[x],"yesterday"))
01078                                 timeperiod_type=TIMEPERIOD_YESTERDAY;
01079                         else if(!strcmp(variables[x],"thisweek"))
01080                                 timeperiod_type=TIMEPERIOD_THISWEEK;
01081                         else if(!strcmp(variables[x],"lastweek"))
01082                                 timeperiod_type=TIMEPERIOD_LASTWEEK;
01083                         else if(!strcmp(variables[x],"thismonth"))
01084                                 timeperiod_type=TIMEPERIOD_THISMONTH;
01085                         else if(!strcmp(variables[x],"lastmonth"))
01086                                 timeperiod_type=TIMEPERIOD_LASTMONTH;
01087                         else if(!strcmp(variables[x],"thisquarter"))
01088                                 timeperiod_type=TIMEPERIOD_THISQUARTER;
01089                         else if(!strcmp(variables[x],"lastquarter"))
01090                                 timeperiod_type=TIMEPERIOD_LASTQUARTER;
01091                         else if(!strcmp(variables[x],"thisyear"))
01092                                 timeperiod_type=TIMEPERIOD_THISYEAR;
01093                         else if(!strcmp(variables[x],"lastyear"))
01094                                 timeperiod_type=TIMEPERIOD_LASTYEAR;
01095                         else if(!strcmp(variables[x],"last24hours"))
01096                                 timeperiod_type=TIMEPERIOD_LAST24HOURS;
01097                         else if(!strcmp(variables[x],"last7days"))
01098                                 timeperiod_type=TIMEPERIOD_LAST7DAYS;
01099                         else if(!strcmp(variables[x],"last31days"))
01100                                 timeperiod_type=TIMEPERIOD_LAST31DAYS;
01101                         else if(!strcmp(variables[x],"custom"))
01102                                 timeperiod_type=TIMEPERIOD_CUSTOM;
01103                         else
01104                                 timeperiod_type=TIMEPERIOD_TODAY;
01105 
01106         
01107                         if(timeperiod_type!=TIMEPERIOD_CUSTOM)
01108                                 convert_timeperiod_to_times(timeperiod_type,&t1,&t2);
01109                         }
01110 
01111                 /* we found time argument */
01112                 else if(!strcmp(variables[x],"smon")){
01113                         x++;
01114                         if(variables[x]==NULL){
01115                                 error=TRUE;
01116                                 break;
01117                                 }
01118 
01119                         if(timeperiod_type!=TIMEPERIOD_CUSTOM)
01120                                 continue;
01121 
01122                         start_month=atoi(variables[x]);
01123                         timeperiod_type=TIMEPERIOD_CUSTOM;
01124                         compute_time_from_parts=TRUE;
01125                         }
01126 
01127                 /* we found time argument */
01128                 else if(!strcmp(variables[x],"sday")){
01129                         x++;
01130                         if(variables[x]==NULL){
01131                                 error=TRUE;
01132                                 break;
01133                                 }
01134 
01135                         if(timeperiod_type!=TIMEPERIOD_CUSTOM)
01136                                 continue;
01137 
01138                         start_day=atoi(variables[x]);
01139                         timeperiod_type=TIMEPERIOD_CUSTOM;
01140                         compute_time_from_parts=TRUE;
01141                         }
01142 
01143                 /* we found time argument */
01144                 else if(!strcmp(variables[x],"syear")){
01145                         x++;
01146                         if(variables[x]==NULL){
01147                                 error=TRUE;
01148                                 break;
01149                                 }
01150 
01151                         if(timeperiod_type!=TIMEPERIOD_CUSTOM)
01152                                 continue;
01153 
01154                         start_year=atoi(variables[x]);
01155                         timeperiod_type=TIMEPERIOD_CUSTOM;
01156                         compute_time_from_parts=TRUE;
01157                         }
01158 
01159                 /* we found time argument */
01160                 else if(!strcmp(variables[x],"smin")){
01161                         x++;
01162                         if(variables[x]==NULL){
01163                                 error=TRUE;
01164                                 break;
01165                                 }
01166 
01167                         if(timeperiod_type!=TIMEPERIOD_CUSTOM)
01168                                 continue;
01169 
01170                         start_minute=atoi(variables[x]);
01171                         timeperiod_type=TIMEPERIOD_CUSTOM;
01172                         compute_time_from_parts=TRUE;
01173                         }
01174 
01175                 /* we found time argument */
01176                 else if(!strcmp(variables[x],"ssec")){
01177                         x++;
01178                         if(variables[x]==NULL){
01179                                 error=TRUE;
01180                                 break;
01181                                 }
01182 
01183                         if(timeperiod_type!=TIMEPERIOD_CUSTOM)
01184                                 continue;
01185 
01186                         start_second=atoi(variables[x]);
01187                         timeperiod_type=TIMEPERIOD_CUSTOM;
01188                         compute_time_from_parts=TRUE;
01189                         }
01190 
01191                 /* we found time argument */
01192                 else if(!strcmp(variables[x],"shour")){
01193                         x++;
01194                         if(variables[x]==NULL){
01195                                 error=TRUE;
01196                                 break;
01197                                 }
01198 
01199                         if(timeperiod_type!=TIMEPERIOD_CUSTOM)
01200                                 continue;
01201 
01202                         start_hour=atoi(variables[x]);
01203                         timeperiod_type=TIMEPERIOD_CUSTOM;
01204                         compute_time_from_parts=TRUE;
01205                         }
01206 
01207 
01208                 /* we found time argument */
01209                 else if(!strcmp(variables[x],"emon")){
01210                         x++;
01211                         if(variables[x]==NULL){
01212                                 error=TRUE;
01213                                 break;
01214                                 }
01215 
01216                         if(timeperiod_type!=TIMEPERIOD_CUSTOM)
01217                                 continue;
01218 
01219                         end_month=atoi(variables[x]);
01220                         timeperiod_type=TIMEPERIOD_CUSTOM;
01221                         compute_time_from_parts=TRUE;
01222                         }
01223 
01224                 /* we found time argument */
01225                 else if(!strcmp(variables[x],"eday")){
01226                         x++;
01227                         if(variables[x]==NULL){
01228                                 error=TRUE;
01229                                 break;
01230                                 }
01231 
01232                         if(timeperiod_type!=TIMEPERIOD_CUSTOM)
01233                                 continue;
01234 
01235                         end_day=atoi(variables[x]);
01236                         timeperiod_type=TIMEPERIOD_CUSTOM;
01237                         compute_time_from_parts=TRUE;
01238                         }
01239 
01240                 /* we found time argument */
01241                 else if(!strcmp(variables[x],"eyear")){
01242                         x++;
01243                         if(variables[x]==NULL){
01244                                 error=TRUE;
01245                                 break;
01246                                 }
01247 
01248                         if(timeperiod_type!=TIMEPERIOD_CUSTOM)
01249                                 continue;
01250 
01251                         end_year=atoi(variables[x]);
01252                         timeperiod_type=TIMEPERIOD_CUSTOM;
01253                         compute_time_from_parts=TRUE;
01254                         }
01255 
01256                 /* we found time argument */
01257                 else if(!strcmp(variables[x],"emin")){
01258                         x++;
01259                         if(variables[x]==NULL){
01260                                 error=TRUE;
01261                                 break;
01262                                 }
01263 
01264                         if(timeperiod_type!=TIMEPERIOD_CUSTOM)
01265                                 continue;
01266 
01267                         end_minute=atoi(variables[x]);
01268                         timeperiod_type=TIMEPERIOD_CUSTOM;
01269                         compute_time_from_parts=TRUE;
01270                         }
01271 
01272                 /* we found time argument */
01273                 else if(!strcmp(variables[x],"esec")){
01274                         x++;
01275                         if(variables[x]==NULL){
01276                                 error=TRUE;
01277                                 break;
01278                                 }
01279 
01280                         if(timeperiod_type!=TIMEPERIOD_CUSTOM)
01281                                 continue;
01282 
01283                         end_second=atoi(variables[x]);
01284                         timeperiod_type=TIMEPERIOD_CUSTOM;
01285                         compute_time_from_parts=TRUE;
01286                         }
01287 
01288                 /* we found time argument */
01289                 else if(!strcmp(variables[x],"ehour")){
01290                         x++;
01291                         if(variables[x]==NULL){
01292                                 error=TRUE;
01293                                 break;
01294                                 }
01295 
01296                         if(timeperiod_type!=TIMEPERIOD_CUSTOM)
01297                                 continue;
01298 
01299                         end_hour=atoi(variables[x]);
01300                         timeperiod_type=TIMEPERIOD_CUSTOM;
01301                         compute_time_from_parts=TRUE;
01302                         }
01303 
01304                 /* we found the embed option */
01305                 else if(!strcmp(variables[x],"embedded"))
01306                         embedded=TRUE;
01307 
01308                 /* we found the noheader option */
01309                 else if(!strcmp(variables[x],"noheader"))
01310                         display_header=FALSE;
01311 
01312                 /* we found the nodaemoncheck option */
01313                 else if(!strcmp(variables[x],"nodaemoncheck"))
01314                         daemon_check=FALSE;
01315 
01316                 /* we found the input option */
01317                 else if(!strcmp(variables[x],"input")){
01318                         x++;
01319                         if(variables[x]==NULL){
01320                                 error=TRUE;
01321                                 break;
01322                                 }
01323 
01324                         if(!strcmp(variables[x],"gethost"))
01325                                 input_type=GET_INPUT_HOST_TARGET;
01326                         else if(!strcmp(variables[x],"getservice"))
01327                                 input_type=GET_INPUT_SERVICE_TARGET;
01328                         else if(!strcmp(variables[x],"getoptions"))
01329                                 input_type=GET_INPUT_OPTIONS;
01330                         else
01331                                 input_type=GET_INPUT_TARGET_TYPE;
01332                         }
01333 
01334                 /* we found the graph states option */
01335                 else if(!strcmp(variables[x],"graphevents")){
01336                         x++;
01337                         if(variables[x]==NULL){
01338                                 error=TRUE;
01339                                 break;
01340                                 }
01341 
01342                         graph_events=atoi(variables[x]);
01343                         }
01344 
01345                 /* we found the graph state types option */
01346                 else if(!strcmp(variables[x],"graphstatetypes")){
01347                         x++;
01348                         if(variables[x]==NULL){
01349                                 error=TRUE;
01350                                 break;
01351                                 }
01352 
01353                         graph_statetypes=atoi(variables[x]);
01354                         }
01355 
01356                 /* we found the breakdown option */
01357                 else if(!strcmp(variables[x],"breakdown")){
01358                         x++;
01359                         if(variables[x]==NULL){
01360                                 error=TRUE;
01361                                 break;
01362                                 }
01363 
01364                         if(!strcmp(variables[x],"monthly"))
01365                                 breakdown_type=BREAKDOWN_MONTHLY;
01366                         else if(!strcmp(variables[x],"dayofmonth"))
01367                                 breakdown_type=BREAKDOWN_DAY_OF_MONTH;
01368                         else if(!strcmp(variables[x],"dayofweek"))
01369                                 breakdown_type=BREAKDOWN_DAY_OF_WEEK;
01370                         else
01371                                 breakdown_type=BREAKDOWN_HOURLY;
01372                         }
01373 
01374                 /* we found the assume state retention option */
01375                 else if(!strcmp(variables[x],"assumestateretention")){
01376                         x++;
01377                         if(variables[x]==NULL){
01378                                 error=TRUE;
01379                                 break;
01380                                 }
01381 
01382                         if(!strcmp(variables[x],"yes"))
01383                                 assume_state_retention=TRUE;
01384                         else
01385                                 assume_state_retention=FALSE;
01386                         }
01387 
01388                 /* we found the initial states logged option */
01389                 else if(!strcmp(variables[x],"initialstateslogged")){
01390                         x++;
01391                         if(variables[x]==NULL){
01392                                 error=TRUE;
01393                                 break;
01394                                 }
01395 
01396                         if(!strcmp(variables[x],"yes"))
01397                                 initial_states_logged=TRUE;
01398                         else
01399                                 initial_states_logged=FALSE;
01400 
01401                         }
01402 
01403                 /* we found the new states only option */
01404                 else if(!strcmp(variables[x],"newstatesonly")){
01405                         x++;
01406                         if(variables[x]==NULL){
01407                                 error=TRUE;
01408                                 break;
01409                                 }
01410 
01411                         if(!strcmp(variables[x],"yes"))
01412                                 new_states_only=TRUE;
01413                         else
01414                                 new_states_only=FALSE;
01415 
01416                         }
01417                 }
01418 
01419         /* free memory allocated to the CGI variables */
01420         free_cgivars(variables);
01421 
01422         return error;
01423 }
01424 
01425 
01426 
01427 /* graphs histogram data */
01428 void graph_all_histogram_data(void){
01429         int pixel_x;
01430         int pixel_y;
01431         int last_pixel_y;
01432         int current_bucket;
01433         int actual_bucket;
01434         unsigned long max_value;
01435         double x_scaling_factor;
01436         double y_scaling_factor;
01437         double x_units;
01438         double y_units;
01439         int current_unit;
01440         int actual_unit;
01441         char temp_buffer[MAX_INPUT_BUFFER];
01442         int string_width;
01443         int string_height;
01444         char *days[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
01445         char *months[12]={"January","February","March","April","May","June","July","August","September","October","November","December"};
01446         char start_time[MAX_INPUT_BUFFER];
01447         char end_time[MAX_INPUT_BUFFER];
01448 
01449         unsigned long state1_max=0L;
01450         unsigned long state1_min=0L;
01451         int have_state1_min=FALSE;
01452         unsigned long state1_sum=0L;
01453         double state1_avg=0.0;
01454         unsigned long state2_max=0L;
01455         unsigned long state2_min=0L;
01456         int have_state2_min=FALSE;
01457         unsigned long state2_sum=0L;
01458         double state2_avg=0.0;
01459         unsigned long state3_max=0L;
01460         unsigned long state3_min=0L;
01461         int have_state3_min=FALSE;
01462         unsigned long state3_sum=0L;
01463         double state3_avg=0.0;
01464         unsigned long state4_max=0L;
01465         unsigned long state4_min=0L;
01466         int have_state4_min=FALSE;
01467         unsigned long state4_sum=0L;
01468         double state4_avg=0.0;
01469 
01470         host *temp_host=NULL;
01471         service *temp_service=NULL;
01472 
01473         /* find the host */
01474         temp_host=find_host(host_name);
01475 
01476         /* find the service */
01477         temp_service=find_service(host_name,service_desc);
01478 
01479 
01480 #ifdef DEBUG
01481         printf("Total Buckets: %d\n",total_buckets);
01482 #endif
01483 
01484         /* determine max value in the buckets (for scaling) */
01485         max_value=0L;
01486         for(current_bucket=0;current_bucket<total_buckets;current_bucket++){
01487                 if(tsdata[current_bucket].service_ok>max_value)
01488                         max_value=tsdata[current_bucket].service_ok;
01489                 if(tsdata[current_bucket].service_warning>max_value)
01490                         max_value=tsdata[current_bucket].service_warning;
01491                 if(tsdata[current_bucket].service_unknown>max_value)
01492                         max_value=tsdata[current_bucket].service_unknown;
01493                 if(tsdata[current_bucket].service_critical>max_value)
01494                         max_value=tsdata[current_bucket].service_critical;
01495                 if(tsdata[current_bucket].host_up>max_value)
01496                         max_value=tsdata[current_bucket].host_up;
01497                 if(tsdata[current_bucket].host_down>max_value)
01498                         max_value=tsdata[current_bucket].host_down;
01499                 if(tsdata[current_bucket].host_unreachable>max_value)
01500                         max_value=tsdata[current_bucket].host_unreachable;
01501                 }
01502 
01503 #ifdef DEBUG
01504         printf("Done determining max bucket values\n");
01505         printf("MAX_VALUE=%lu\n",max_value);
01506         printf("DRAWING_HEIGHT=%lu\n",DRAWING_HEIGHT);
01507 #endif
01508 
01509         /* min number of values to graph */
01510         if(max_value<10)
01511                 max_value=10;
01512 #ifdef DEBUG
01513         printf("ADJUSTED MAX_VALUE=%lu\n",max_value);
01514 #endif
01515 
01516         /* determine y scaling factor */
01517         /*y_scaling_factor=floor((double)DRAWING_HEIGHT/(double)max_value);*/
01518         y_scaling_factor=(double)((double)DRAWING_HEIGHT/(double)max_value);
01519 
01520         /* determine x scaling factor */
01521         x_scaling_factor=(double)((double)DRAWING_WIDTH/(double)total_buckets);
01522 
01523         /* determine y units resolution - we want a max of about 10 y grid lines */
01524         /*
01525         y_units=(double)((double)DRAWING_HEIGHT/19.0);
01526         y_units=ceil(y_units/y_scaling_factor)*y_scaling_factor;
01527         */
01528         y_units=ceil(19.0/y_scaling_factor);
01529 
01530         /* determine x units resolution */
01531         if(breakdown_type==BREAKDOWN_HOURLY)
01532                 x_units=(double)((double)DRAWING_WIDTH/(double)(total_buckets/4.0));
01533         else
01534                 x_units=x_scaling_factor;
01535 
01536 #ifdef DEBUG
01537         printf("DRAWING_WIDTH: %d\n",DRAWING_WIDTH);
01538         printf("DRAWING_HEIGHT: %d\n",DRAWING_HEIGHT);
01539         printf("max_value: %lu\n",max_value);
01540         printf("x_scaling_factor: %.3f\n",x_scaling_factor);
01541         printf("y_scaling_factor: %.3f\n",y_scaling_factor);
01542         printf("x_units: %.3f\n",x_units);
01543         printf("y_units: %.3f\n",y_units);
01544         printf("y units to draw: %.3f\n",((double)max_value/y_units));
01545 #endif
01546 
01547         string_height=gdFontSmall->h;
01548 
01549 #ifdef DEBUG
01550         printf("Starting to draw Y grid lines...\n");
01551 #endif
01552 
01553         /* draw y grid lines */
01554         if(max_value>0){
01555                 for(current_unit=1;(current_unit*y_units*y_scaling_factor)<=DRAWING_HEIGHT;current_unit++){
01556                         draw_dashed_line(DRAWING_X_OFFSET,DRAWING_Y_OFFSET-(current_unit*y_units*y_scaling_factor),DRAWING_X_OFFSET+DRAWING_WIDTH,DRAWING_Y_OFFSET-(current_unit*y_units*y_scaling_factor),color_lightgray);
01557 #ifdef DEBUG
01558                         printf("  Drawing Y unit #%d @ %d\n",current_unit,(int)(current_unit*y_units*y_scaling_factor));
01559 #endif
01560                         }
01561                 }
01562 
01563 #ifdef DEBUG
01564         printf("Starting to draw X grid lines...\n");
01565 #endif
01566 
01567         /* draw x grid lines */
01568         for(current_unit=1;(int)(current_unit*x_units)<=DRAWING_WIDTH;current_unit++)
01569                 draw_dashed_line(DRAWING_X_OFFSET+(int)(current_unit*x_units),DRAWING_Y_OFFSET,DRAWING_X_OFFSET+(int)(current_unit*x_units),DRAWING_Y_OFFSET-DRAWING_HEIGHT,color_lightgray);
01570 
01571 #ifdef DEBUG
01572         printf("Starting to draw grid units...\n");
01573 #endif
01574 
01575         /* draw y units */
01576         if(max_value>0){
01577                 for(current_unit=0;(current_unit*y_units*y_scaling_factor)<=DRAWING_HEIGHT;current_unit++){
01578                         snprintf(temp_buffer,sizeof(temp_buffer)-1,"%d",(int)(current_unit*y_units));
01579                         temp_buffer[sizeof(temp_buffer)-1]='\x0';
01580                         string_width=gdFontSmall->w*strlen(temp_buffer);
01581                         gdImageString(histogram_image,gdFontSmall,DRAWING_X_OFFSET-string_width-5,DRAWING_Y_OFFSET-(current_unit*y_units*y_scaling_factor)-(string_height/2),(unsigned char *)temp_buffer,color_black);
01582                         }
01583                 }
01584 
01585         /* draw x units */
01586         for(current_unit=0,actual_unit=0;(int)(current_unit*x_units)<=DRAWING_WIDTH;current_unit++,actual_unit++){
01587 
01588                 if(actual_unit>=total_buckets)
01589                         actual_unit=0;
01590 
01591                 if(breakdown_type==BREAKDOWN_MONTHLY)
01592                         snprintf(temp_buffer,sizeof(temp_buffer)-1,"%s",months[actual_unit]);
01593                 else if(breakdown_type==BREAKDOWN_DAY_OF_MONTH)
01594                         snprintf(temp_buffer,sizeof(temp_buffer)-1,"%d",actual_unit+1);
01595                 else if(breakdown_type==BREAKDOWN_DAY_OF_WEEK)
01596                         snprintf(temp_buffer,sizeof(temp_buffer)-1,"%s",days[actual_unit]);
01597                 else
01598                         snprintf(temp_buffer,sizeof(temp_buffer)-1,"%02d:00",(actual_unit==24)?0:actual_unit);
01599 
01600                 temp_buffer[sizeof(temp_buffer)-1]='\x0';
01601                 string_width=gdFontSmall->w*strlen(temp_buffer);
01602 
01603                 gdImageStringUp(histogram_image,gdFontSmall,DRAWING_X_OFFSET+(current_unit*x_units)-(string_height/2),DRAWING_Y_OFFSET+5+string_width,(unsigned char *)temp_buffer,color_black);
01604                 }
01605 
01606         /* draw y unit measure */
01607         snprintf(temp_buffer,sizeof(temp_buffer)-1,"Number of Events");
01608         temp_buffer[sizeof(temp_buffer)-1]='\x0';
01609         string_width=gdFontSmall->w*strlen(temp_buffer);
01610         gdImageStringUp(histogram_image,gdFontSmall,0,DRAWING_Y_OFFSET-(DRAWING_HEIGHT/2)+(string_width/2),(unsigned char *)temp_buffer,color_black);
01611 
01612         /* draw x unit measure */
01613         if(breakdown_type==BREAKDOWN_MONTHLY)
01614                 snprintf(temp_buffer,sizeof(temp_buffer)-1,"Month");
01615         else if(breakdown_type==BREAKDOWN_DAY_OF_MONTH)
01616                 snprintf(temp_buffer,sizeof(temp_buffer)-1,"Day of the Month");
01617         else if(breakdown_type==BREAKDOWN_DAY_OF_WEEK)
01618                 snprintf(temp_buffer,sizeof(temp_buffer)-1,"Day of the Week");
01619         else
01620                 snprintf(temp_buffer,sizeof(temp_buffer)-1,"Hour of the Day (15 minute increments)");
01621         temp_buffer[sizeof(temp_buffer)-1]='\x0';
01622         string_width=gdFontSmall->w*strlen(temp_buffer);
01623         gdImageString(histogram_image,gdFontSmall,DRAWING_X_OFFSET+(DRAWING_WIDTH/2)-(string_width/2),DRAWING_Y_OFFSET+70,(unsigned char *)temp_buffer,color_black);
01624 
01625         /* draw title */
01626         snprintf(start_time,sizeof(start_time)-1,"%s",ctime(&t1));
01627         start_time[sizeof(start_time)-1]='\x0';
01628         start_time[strlen(start_time)-1]='\x0';
01629         snprintf(end_time,sizeof(end_time)-1,"%s",ctime(&t2));
01630         end_time[sizeof(end_time)-1]='\x0';
01631         end_time[strlen(end_time)-1]='\x0';
01632 
01633         if(display_type==DISPLAY_HOST_HISTOGRAM)
01634                 snprintf(temp_buffer,sizeof(temp_buffer)-1,"Event History For Host '%s'",(temp_host->display_name!=NULL)?temp_host->display_name:temp_host->name);
01635         else
01636                 snprintf(temp_buffer,sizeof(temp_buffer)-1,"Event History For Service '%s' On Host '%s'",(temp_service->display_name!=NULL)?temp_service->display_name:temp_service->description,(temp_host->display_name!=NULL)?temp_host->display_name:temp_host->name);
01637         temp_buffer[sizeof(temp_buffer)-1]='\x0';
01638         string_width=gdFontSmall->w*strlen(temp_buffer);
01639         gdImageString(histogram_image,gdFontSmall,DRAWING_X_OFFSET+(DRAWING_WIDTH/2)-(string_width/2),0,(unsigned char *)temp_buffer,color_black);
01640 
01641         snprintf(temp_buffer,sizeof(temp_buffer)-1,"%s to %s",start_time,end_time);
01642         temp_buffer[sizeof(temp_buffer)-1]='\x0';
01643         string_width=gdFontSmall->w*strlen(temp_buffer);
01644         gdImageString(histogram_image,gdFontSmall,DRAWING_X_OFFSET+(DRAWING_WIDTH/2)-(string_width/2),string_height+5,(unsigned char *)temp_buffer,color_black);
01645 
01646 
01647 #ifdef DEBUG
01648         printf("About to starting graphing (total_buckets=%d)...\n",total_buckets);
01649 #endif
01650         
01651 
01652         /* graph service states */
01653         if(display_type==DISPLAY_HOST_HISTOGRAM){
01654 
01655                 /* graph host recoveries */
01656                 if(graph_events & GRAPH_HOST_UP){
01657 
01658                         last_pixel_y=0;
01659                         for(current_bucket=0,actual_bucket=0;current_bucket<=total_buckets;current_bucket++,actual_bucket++){
01660 
01661                                 if(actual_bucket>=total_buckets)
01662                                         actual_bucket=0;
01663 
01664                                 pixel_x=(int)(current_bucket*x_scaling_factor);
01665                         
01666                                 pixel_y=(int)(tsdata[actual_bucket].host_up*y_scaling_factor);
01667 
01668                                 if(current_bucket>0 && !(last_pixel_y==0 && pixel_y==0))
01669                                         draw_line(DRAWING_X_OFFSET+pixel_x-(int)x_scaling_factor,DRAWING_Y_OFFSET-last_pixel_y,DRAWING_X_OFFSET+pixel_x,DRAWING_Y_OFFSET-pixel_y,color_green);
01670 
01671                                 last_pixel_y=pixel_y;
01672 
01673                                 if(current_bucket<total_buckets){
01674                                         if(have_state1_min==FALSE || tsdata[actual_bucket].host_up<state1_min){
01675                                                 state1_min=tsdata[actual_bucket].host_up;
01676                                                 have_state1_min=TRUE;
01677                                                 }
01678                                         if(state1_max==0 || tsdata[actual_bucket].host_up>state1_max)
01679                                                 state1_max=tsdata[actual_bucket].host_up;
01680                                         state1_sum+=tsdata[actual_bucket].host_up;
01681                                         }
01682                                 }
01683                         }
01684 
01685 #ifdef DEBUG
01686                 printf("Done graphing HOST UP states...\n");
01687 #endif
01688 
01689                 /* graph host down states */
01690                 if(graph_events & GRAPH_HOST_DOWN){
01691 
01692                         last_pixel_y=0;
01693                         for(current_bucket=0,actual_bucket=0;current_bucket<=total_buckets;current_bucket++,actual_bucket++){
01694 
01695                                 if(actual_bucket>=total_buckets)
01696                                         actual_bucket=0;
01697 
01698                                 pixel_x=(int)(current_bucket*x_scaling_factor);
01699                         
01700                                 pixel_y=(int)(tsdata[actual_bucket].host_down*y_scaling_factor);
01701 
01702                                 if(current_bucket>0 && !(last_pixel_y==0 && pixel_y==0))
01703                                         draw_line(DRAWING_X_OFFSET+pixel_x-(int)x_scaling_factor,DRAWING_Y_OFFSET-last_pixel_y,DRAWING_X_OFFSET+pixel_x,DRAWING_Y_OFFSET-pixel_y,color_red);
01704 
01705                                 last_pixel_y=pixel_y;
01706 
01707                                 if(current_bucket<total_buckets){
01708                                         if(have_state2_min==FALSE || tsdata[actual_bucket].host_down<state2_min){
01709                                                 state2_min=tsdata[actual_bucket].host_down;
01710                                                 have_state2_min=TRUE;
01711                                                 }
01712                                         if(state2_max==0 || tsdata[actual_bucket].host_down>state2_max)
01713                                                 state2_max=tsdata[actual_bucket].host_down;
01714                                         state2_sum+=tsdata[actual_bucket].host_down;
01715                                         }
01716                                 }
01717                         }
01718 
01719 #ifdef DEBUG
01720                 printf("Done graphing HOST DOWN states...\n");
01721 #endif
01722 
01723                 /* graph host unreachable states */
01724                 if(graph_events & GRAPH_HOST_UNREACHABLE){
01725 
01726                         last_pixel_y=0;
01727                         for(current_bucket=0,actual_bucket=0;current_bucket<=total_buckets;current_bucket++,actual_bucket++){
01728 
01729                                 if(actual_bucket>=total_buckets)
01730                                         actual_bucket=0;
01731 
01732                                 pixel_x=(int)(current_bucket*x_scaling_factor);
01733                         
01734                                 pixel_y=(int)(tsdata[actual_bucket].host_unreachable*y_scaling_factor);
01735 
01736                                 if(current_bucket>0 && !(last_pixel_y==0 && pixel_y==0))
01737                                         draw_line(DRAWING_X_OFFSET+pixel_x-(int)x_scaling_factor,DRAWING_Y_OFFSET-last_pixel_y,DRAWING_X_OFFSET+pixel_x,DRAWING_Y_OFFSET-pixel_y,color_darkred);
01738 
01739                                 last_pixel_y=pixel_y;
01740 
01741                                 if(current_bucket<total_buckets){
01742                                         if(have_state3_min==FALSE || tsdata[actual_bucket].host_unreachable<state3_min){
01743                                                 state3_min=tsdata[actual_bucket].host_unreachable;
01744                                                 have_state3_min=TRUE;
01745                                                 }
01746                                         if(state3_max==0 || tsdata[actual_bucket].host_unreachable>state3_max)
01747                                                 state3_max=tsdata[actual_bucket].host_unreachable;
01748                                         state3_sum+=tsdata[actual_bucket].host_unreachable;
01749                                         }
01750                                 }
01751                         }
01752 
01753 #ifdef DEBUG
01754                 printf("Done graphing HOST UNREACHABLE states...\n");
01755 #endif
01756 
01757                 }
01758 
01759         /* graph service states */
01760         else{
01761 
01762                 /* graph service recoveries */
01763                 if(graph_events & GRAPH_SERVICE_OK){
01764 
01765                         last_pixel_y=0;
01766                         for(current_bucket=0,actual_bucket=0;current_bucket<=total_buckets;current_bucket++,actual_bucket++){
01767 
01768                                 if(actual_bucket>=total_buckets)
01769                                         actual_bucket=0;
01770 
01771                                 pixel_x=(int)(current_bucket*x_scaling_factor);
01772                         
01773                                 pixel_y=(int)(tsdata[actual_bucket].service_ok*y_scaling_factor);
01774 
01775                                 if(current_bucket>0 && !(last_pixel_y==0 && pixel_y==0))
01776                                         draw_line(DRAWING_X_OFFSET+pixel_x-(int)x_scaling_factor,DRAWING_Y_OFFSET-last_pixel_y,DRAWING_X_OFFSET+pixel_x,DRAWING_Y_OFFSET-pixel_y,color_green);
01777 
01778                                 last_pixel_y=pixel_y;
01779 
01780                                 if(current_bucket<total_buckets){
01781                                         if(have_state1_min==FALSE || tsdata[actual_bucket].service_ok<state1_min){
01782                                                 state1_min=tsdata[actual_bucket].service_ok;
01783                                                 have_state1_min=TRUE;
01784                                                 }
01785                                         if(state1_max==0 || tsdata[actual_bucket].service_ok>state1_max)
01786                                                 state1_max=tsdata[actual_bucket].service_ok;
01787                                         state1_sum+=tsdata[actual_bucket].service_ok;
01788                                         }
01789                                 }
01790                         }
01791 
01792                 /* graph service warning states */
01793                 if(graph_events & GRAPH_SERVICE_WARNING){
01794 
01795                         last_pixel_y=0;
01796                         for(current_bucket=0,actual_bucket=0;current_bucket<=total_buckets;current_bucket++,actual_bucket++){
01797 
01798                                 if(actual_bucket>=total_buckets)
01799                                         actual_bucket=0;
01800 
01801                                 pixel_x=(int)(current_bucket*x_scaling_factor);
01802                                 
01803                                 pixel_y=(int)(tsdata[actual_bucket].service_warning*y_scaling_factor);
01804 
01805                                 if(current_bucket>0 && !(last_pixel_y==0 && pixel_y==0))
01806                                         draw_line(DRAWING_X_OFFSET+pixel_x-(int)x_scaling_factor,DRAWING_Y_OFFSET-last_pixel_y,DRAWING_X_OFFSET+pixel_x,DRAWING_Y_OFFSET-pixel_y,color_yellow);
01807 
01808                                 last_pixel_y=pixel_y;
01809 
01810                                 if(current_bucket<total_buckets){
01811                                         if(have_state2_min==FALSE || tsdata[actual_bucket].service_warning<state2_min){
01812                                                 state2_min=tsdata[actual_bucket].service_warning;
01813                                                 have_state2_min=TRUE;
01814                                                 }
01815                                         if(state2_max==0 || tsdata[actual_bucket].service_warning>state2_max)
01816                                                 state2_max=tsdata[actual_bucket].service_warning;
01817                                         state2_sum+=tsdata[actual_bucket].service_warning;
01818                                         }
01819                                 }
01820                         }
01821 
01822                 /* graph service unknown states */
01823                 if(graph_events & GRAPH_SERVICE_UNKNOWN){
01824 
01825                         last_pixel_y=0;
01826                         for(current_bucket=0,actual_bucket=0;current_bucket<=total_buckets;current_bucket++,actual_bucket++){
01827                                 
01828                                 if(actual_bucket>=total_buckets)
01829                                         actual_bucket=0;
01830 
01831                                 pixel_x=(int)(current_bucket*x_scaling_factor);
01832                         
01833                                 pixel_y=(int)(tsdata[actual_bucket].service_unknown*y_scaling_factor);
01834 
01835                                 if(current_bucket>0 && !(last_pixel_y==0 && pixel_y==0))
01836                                         draw_line(DRAWING_X_OFFSET+pixel_x-(int)x_scaling_factor,DRAWING_Y_OFFSET-last_pixel_y,DRAWING_X_OFFSET+pixel_x,DRAWING_Y_OFFSET-pixel_y,color_orange);
01837 
01838                                 last_pixel_y=pixel_y;
01839 
01840                                 if(current_bucket<total_buckets){
01841                                         if(have_state3_min==FALSE || tsdata[actual_bucket].service_unknown<state3_min){
01842                                                 state3_min=tsdata[actual_bucket].service_unknown;
01843                                                 have_state3_min=TRUE;
01844                                                 }
01845                                         if(state3_max==0 || tsdata[actual_bucket].service_unknown>state3_max)
01846                                                 state3_max=tsdata[actual_bucket].service_unknown;
01847                                         state3_sum+=tsdata[actual_bucket].service_unknown;
01848                                         }
01849                                 }
01850                         }
01851 
01852                 /* graph service critical states */
01853                 if(graph_events & GRAPH_SERVICE_CRITICAL){
01854 
01855                         last_pixel_y=0;
01856                         for(current_bucket=0,actual_bucket=0;current_bucket<=total_buckets;current_bucket++,actual_bucket++){
01857 
01858                                 if(actual_bucket>=total_buckets)
01859                                         actual_bucket=0;
01860 
01861                                 pixel_x=(int)(current_bucket*x_scaling_factor);
01862                         
01863                                 pixel_y=(int)(tsdata[actual_bucket].service_critical*y_scaling_factor);
01864 
01865                                 if(current_bucket>0 && !(last_pixel_y==0 && pixel_y==0))
01866                                         draw_line(DRAWING_X_OFFSET+pixel_x-(int)x_scaling_factor,DRAWING_Y_OFFSET-last_pixel_y,DRAWING_X_OFFSET+pixel_x,DRAWING_Y_OFFSET-pixel_y,color_red);
01867 
01868                                 last_pixel_y=pixel_y;
01869 
01870                                 if(current_bucket<total_buckets){
01871                                         if(have_state4_min==FALSE || tsdata[actual_bucket].service_critical<state4_min){
01872                                                 state4_min=tsdata[actual_bucket].service_critical;
01873                                                 have_state4_min=TRUE;
01874                                                 }
01875                                         if(state4_max==0 || tsdata[actual_bucket].service_critical>state4_max)
01876                                                 state4_max=tsdata[actual_bucket].service_critical;
01877                                         state4_sum+=tsdata[actual_bucket].service_critical;
01878                                         }
01879                                 }
01880                         }
01881                 }
01882 
01883 #ifdef DEBUG
01884         printf("Done graphing states...\n");
01885 #endif
01886 
01887         /* draw graph boundaries */
01888         draw_line(DRAWING_X_OFFSET,DRAWING_Y_OFFSET,DRAWING_X_OFFSET,DRAWING_Y_OFFSET-DRAWING_HEIGHT,color_black);
01889         draw_line(DRAWING_X_OFFSET+DRAWING_WIDTH,DRAWING_Y_OFFSET,DRAWING_X_OFFSET+DRAWING_WIDTH,DRAWING_Y_OFFSET-DRAWING_HEIGHT,color_black);
01890         draw_line(DRAWING_X_OFFSET,DRAWING_Y_OFFSET,DRAWING_X_OFFSET+DRAWING_WIDTH,DRAWING_Y_OFFSET,color_black);
01891 
01892 
01893         /* graph stats */
01894         snprintf(temp_buffer,sizeof(temp_buffer)-1,"EVENT TYPE");
01895         temp_buffer[sizeof(temp_buffer)-1]='\x0';
01896         string_width=gdFontSmall->w*strlen(temp_buffer);
01897         gdImageString(histogram_image,gdFontSmall,DRAWING_X_OFFSET+DRAWING_WIDTH+15,DRAWING_Y_OFFSET-DRAWING_HEIGHT,(unsigned char *)temp_buffer,color_black);
01898 
01899         snprintf(temp_buffer,sizeof(temp_buffer)-1,"  MIN   MAX   SUM   AVG");
01900         temp_buffer[sizeof(temp_buffer)-1]='\x0';
01901         string_width=gdFontSmall->w*strlen(temp_buffer);
01902         gdImageString(histogram_image,gdFontSmall,DRAWING_X_OFFSET+DRAWING_WIDTH+115,DRAWING_Y_OFFSET-DRAWING_HEIGHT,(unsigned char *)temp_buffer,color_black);
01903 
01904         draw_line(DRAWING_X_OFFSET+DRAWING_WIDTH+15,DRAWING_Y_OFFSET-DRAWING_HEIGHT+string_height+2,DRAWING_X_OFFSET+DRAWING_WIDTH+275,DRAWING_Y_OFFSET-DRAWING_HEIGHT+string_height+2,color_black);
01905 
01906         snprintf(temp_buffer,sizeof(temp_buffer)-1,"Recovery (%s):",(display_type==DISPLAY_SERVICE_HISTOGRAM)?"Ok":"Up");
01907         temp_buffer[sizeof(temp_buffer)-1]='\x0';
01908         string_width=gdFontSmall->w*strlen(temp_buffer);
01909         gdImageString(histogram_image,gdFontSmall,DRAWING_X_OFFSET+DRAWING_WIDTH+15,DRAWING_Y_OFFSET-DRAWING_HEIGHT+((string_height+5)*1),(unsigned char *)temp_buffer,color_green);
01910 
01911         state1_avg=(double)((double)state1_sum/(double)total_buckets);
01912         snprintf(temp_buffer,sizeof(temp_buffer)-1,"%5lu %5lu %5lu   %.2f",state1_min,state1_max,state1_sum,state1_avg);
01913         temp_buffer[sizeof(temp_buffer)-1]='\x0';
01914         string_width=gdFontSmall->w*strlen(temp_buffer);
01915         gdImageString(histogram_image,gdFontSmall,DRAWING_X_OFFSET+DRAWING_WIDTH+115,DRAWING_Y_OFFSET-DRAWING_HEIGHT+((string_height+5)*1),(unsigned char *)temp_buffer,color_black);
01916 
01917         snprintf(temp_buffer,sizeof(temp_buffer)-1,"%s:",(display_type==DISPLAY_SERVICE_HISTOGRAM)?"Warning":"Down");
01918         temp_buffer[sizeof(temp_buffer)-1]='\x0';
01919         string_width=gdFontSmall->w*strlen(temp_buffer);
01920         gdImageString(histogram_image,gdFontSmall,DRAWING_X_OFFSET+DRAWING_WIDTH+15,DRAWING_Y_OFFSET-DRAWING_HEIGHT+((string_height+5)*2),(unsigned char *)temp_buffer,(display_type==DISPLAY_SERVICE_HISTOGRAM)?color_yellow:color_red);
01921 
01922         state2_avg=(double)((double)state2_sum/(double)total_buckets);
01923         snprintf(temp_buffer,sizeof(temp_buffer)-1,"%5lu %5lu %5lu   %.2f",state2_min,state2_max,state2_sum,state2_avg);
01924         temp_buffer[sizeof(temp_buffer)-1]='\x0';
01925         string_width=gdFontSmall->w*strlen(temp_buffer);
01926         gdImageString(histogram_image,gdFontSmall,DRAWING_X_OFFSET+DRAWING_WIDTH+115,DRAWING_Y_OFFSET-DRAWING_HEIGHT+((string_height+5)*2),(unsigned char *)temp_buffer,color_black);
01927 
01928         snprintf(temp_buffer,sizeof(temp_buffer)-1,"%s:",(display_type==DISPLAY_SERVICE_HISTOGRAM)?"Unknown":"Unreachable");
01929         temp_buffer[sizeof(temp_buffer)-1]='\x0';
01930         string_width=gdFontSmall->w*strlen(temp_buffer);
01931         gdImageString(histogram_image,gdFontSmall,DRAWING_X_OFFSET+DRAWING_WIDTH+15,DRAWING_Y_OFFSET-DRAWING_HEIGHT+((string_height+5)*3),(unsigned char *)temp_buffer,(display_type==DISPLAY_SERVICE_HISTOGRAM)?color_orange:color_darkred);
01932 
01933         state3_avg=(double)((double)state3_sum/(double)total_buckets);
01934         snprintf(temp_buffer,sizeof(temp_buffer)-1,"%5lu %5lu %5lu   %.2f",state3_min,state3_max,state3_sum,state3_avg);
01935         temp_buffer[sizeof(temp_buffer)-1]='\x0';
01936         string_width=gdFontSmall->w*strlen(temp_buffer);
01937         gdImageString(histogram_image,gdFontSmall,DRAWING_X_OFFSET+DRAWING_WIDTH+115,DRAWING_Y_OFFSET-DRAWING_HEIGHT+((string_height+5)*3),(unsigned char *)temp_buffer,color_black);
01938 
01939         if(display_type==DISPLAY_SERVICE_HISTOGRAM){
01940 
01941                 snprintf(temp_buffer,sizeof(temp_buffer)-1,"Critical:");
01942                 temp_buffer[sizeof(temp_buffer)-1]='\x0';
01943                 string_width=gdFontSmall->w*strlen(temp_buffer);
01944                 gdImageString(histogram_image,gdFontSmall,DRAWING_X_OFFSET+DRAWING_WIDTH+15,DRAWING_Y_OFFSET-DRAWING_HEIGHT+((string_height+5)*4),(unsigned char *)temp_buffer,color_red);
01945 
01946                 state4_avg=(double)((double)state4_sum/(double)total_buckets);
01947                 snprintf(temp_buffer,sizeof(temp_buffer)-1,"%5lu %5lu %5lu   %.2f",state4_min,state4_max,state4_sum,state4_avg);
01948                 temp_buffer[sizeof(temp_buffer)-1]='\x0';
01949                 string_width=gdFontSmall->w*strlen(temp_buffer);
01950                 gdImageString(histogram_image,gdFontSmall,DRAWING_X_OFFSET+DRAWING_WIDTH+115,DRAWING_Y_OFFSET-DRAWING_HEIGHT+((string_height+5)*4),(unsigned char *)temp_buffer,color_black);
01951                 }
01952 
01953         return;
01954 }
01955 
01956 
01957 /* adds an archived state entry */
01958 void add_archived_state(int state_type, time_t time_stamp){
01959         struct tm *our_time;
01960         int bucket;
01961         int skip_state=FALSE;
01962 
01963 #ifdef DEBUG2
01964         printf("NEW ENTRY: last=%d this=%d\n",last_state,state_type);
01965 #endif
01966 
01967         /* don't record program starts/stops, just make a note that one occurred */
01968         if(state_type==AS_PROGRAM_START || state_type==AS_PROGRAM_END){
01969 #ifdef DEBUG2
01970                 printf("Recording a program start: %d\n",state_type);
01971 #endif
01972                 program_restart_has_occurred=TRUE;
01973                 return;
01974                 }
01975 
01976         /* see if we should even take into account this event */
01977         if(program_restart_has_occurred==TRUE){
01978 
01979 #ifdef DEBUG2
01980                 printf("program_restart_has_occurred: last=%d this=%d\n",last_state,state_type);
01981 #endif
01982                 
01983                 if(initial_states_logged==TRUE){
01984                         if(state_type==AS_SVC_OK && last_state==AS_SVC_OK)
01985                                 skip_state=TRUE;
01986                         if(state_type==AS_HOST_UP && last_state==AS_HOST_UP)
01987                                 skip_state=TRUE;
01988                         }
01989                         
01990                 if(assume_state_retention==TRUE && initial_states_logged==TRUE){
01991                         if(state_type==AS_SVC_WARNING && last_state==AS_SVC_WARNING)
01992                                 skip_state=TRUE;
01993                         if(state_type==AS_SVC_UNKNOWN && last_state==AS_SVC_UNKNOWN)
01994                                 skip_state=TRUE;
01995                         if(state_type==AS_SVC_CRITICAL && last_state==AS_SVC_CRITICAL)
01996                                 skip_state=TRUE;
01997                         if(state_type==AS_HOST_DOWN && last_state==AS_HOST_DOWN)
01998                                 skip_state=TRUE;
01999                         if(state_type==AS_HOST_UNREACHABLE && last_state==AS_HOST_UNREACHABLE)
02000                                 skip_state=TRUE;
02001                         }
02002 
02003                 if(skip_state==TRUE){
02004                         program_restart_has_occurred=FALSE;
02005 #ifdef DEBUG2
02006                         printf("Skipping state...\n");
02007 #endif
02008                         return;
02009                         }
02010                 }
02011 
02012         /* reset program restart variable */
02013         program_restart_has_occurred=FALSE;
02014 
02015         /* are we only processing new states */
02016         if(new_states_only==TRUE && state_type==last_state){
02017 #ifdef DEBUG2
02018                 printf("Skipping state (not a new state)...\n");
02019 #endif
02020                 return;
02021                 }
02022 
02023 #ifdef DEBUG2
02024         printf("GOODSTATE: %d @ %lu\n",state_type,(unsigned long)time_stamp);
02025 #endif
02026                 
02027 
02028 
02029         our_time=localtime(&time_stamp);
02030 
02031         /* calculate the correct bucket to dump the data into */
02032         if(breakdown_type==BREAKDOWN_MONTHLY)
02033                 bucket=our_time->tm_mon;
02034 
02035         else if(breakdown_type==BREAKDOWN_DAY_OF_MONTH)
02036                 bucket=our_time->tm_mday-1;
02037 
02038         else if(breakdown_type==BREAKDOWN_DAY_OF_WEEK)
02039                 bucket=our_time->tm_wday;
02040 
02041         else
02042                 bucket=(our_time->tm_hour*4)+(our_time->tm_min/15);
02043 
02044 #ifdef DEBUG2
02045         printf("\tBucket=%d\n",bucket);
02046 #endif  
02047 
02048         /* save the data in the correct bucket */
02049         if(state_type==AS_SVC_OK)
02050                 tsdata[bucket].service_ok++;
02051         else if(state_type==AS_SVC_UNKNOWN)
02052                 tsdata[bucket].service_unknown++;
02053         else if(state_type==AS_SVC_WARNING)
02054                 tsdata[bucket].service_warning++;
02055         else if(state_type==AS_SVC_CRITICAL)
02056                 tsdata[bucket].service_critical++;
02057         else if(state_type==AS_HOST_UP)
02058                 tsdata[bucket].host_up++;
02059         else if(state_type==AS_HOST_DOWN)
02060                 tsdata[bucket].host_down++;
02061         else if(state_type==AS_HOST_UNREACHABLE)
02062                 tsdata[bucket].host_unreachable++;
02063 
02064         /* record last state type */
02065         last_state=state_type;
02066 
02067         return;
02068 }
02069 
02070 
02071 
02072 /* reads log files for archived state data */
02073 void read_archived_state_data(void){
02074         char filename[MAX_FILENAME_LENGTH];
02075         int newest_archive=0;
02076         int oldest_archive=0;
02077         int current_archive;
02078 
02079 #ifdef DEBUG2
02080         printf("Determining archives to use...\n");
02081 #endif
02082 
02083         /* determine earliest archive to use */
02084         oldest_archive=determine_archive_to_use_from_time(t1);
02085         if(log_rotation_method!=LOG_ROTATION_NONE)
02086                 oldest_archive+=backtrack_archives;
02087 
02088         /* determine most recent archive to use */
02089         newest_archive=determine_archive_to_use_from_time(t2);
02090 
02091         if(oldest_archive<newest_archive)
02092                 oldest_archive=newest_archive;
02093 
02094 #ifdef DEBUG2
02095         printf("Oldest archive: %d\n",oldest_archive);
02096         printf("Newest archive: %d\n",newest_archive);
02097 #endif
02098 
02099         /* Service filter */
02100         add_log_filter(LOGENTRY_SERVICE_OK,LOGFILTER_INCLUDE);
02101         add_log_filter(LOGENTRY_SERVICE_WARNING,LOGFILTER_INCLUDE);
02102         add_log_filter(LOGENTRY_SERVICE_CRITICAL,LOGFILTER_INCLUDE);
02103         add_log_filter(LOGENTRY_SERVICE_UNKNOWN,LOGFILTER_INCLUDE);
02104         add_log_filter(LOGENTRY_SERVICE_RECOVERY,LOGFILTER_INCLUDE);
02105 
02106         /* Host filter */
02107         add_log_filter(LOGENTRY_HOST_UP,LOGFILTER_INCLUDE);
02108         add_log_filter(LOGENTRY_HOST_DOWN,LOGFILTER_INCLUDE);
02109         add_log_filter(LOGENTRY_HOST_UNREACHABLE,LOGFILTER_INCLUDE);
02110         add_log_filter(LOGENTRY_HOST_RECOVERY,LOGFILTER_INCLUDE);
02111 
02112         /* system message */
02113         add_log_filter(LOGENTRY_STARTUP,LOGFILTER_INCLUDE);
02114         add_log_filter(LOGENTRY_RESTART,LOGFILTER_INCLUDE);
02115         add_log_filter(LOGENTRY_SHUTDOWN,LOGFILTER_INCLUDE);
02116         add_log_filter(LOGENTRY_BAILOUT,LOGFILTER_INCLUDE);
02117 
02118         /* read in all the necessary archived logs */
02119         for(current_archive=newest_archive;current_archive<=oldest_archive;current_archive++){
02120 
02121                 /* get the name of the log file that contains this archive */
02122                 get_log_archive_to_use(current_archive,filename,sizeof(filename)-1);
02123 
02124 #ifdef DEBUG2
02125                 printf("\tCurrent archive: %d (%s)\n",current_archive,filename);
02126 #endif
02127 
02128                 /* scan the log file for archived state data */
02129                 scan_log_file_for_archived_state_data(filename);
02130         }
02131 
02132         free_log_filters();
02133 
02134         return;
02135 }
02136 
02137 
02138 
02139 /* grabs archives state data from a log file */
02140 void scan_log_file_for_archived_state_data(char *filename){
02141         char entry_host_name[MAX_INPUT_BUFFER];
02142         char entry_service_desc[MAX_INPUT_BUFFER];
02143         char *temp_buffer;
02144         logentry *temp_entry=NULL;
02145         int status;
02146 
02147         /* print something so browser doesn't time out */
02148         if(content_type==HTML_CONTENT){
02149                 printf(" ");
02150                 fflush(NULL);
02151         }
02152 
02153         status = get_log_entries(filename,NULL,FALSE,t1-(60*60*24*backtrack_archives),t2);
02154         
02155         if (status!=READLOG_OK) {
02156 #ifdef DEBUG2
02157                 printf("Could not open file '%s' for reading.\n",filename);
02158 #endif
02159                 /* free memory */
02160                 free_log_entries();
02161                 return;
02162         }else{
02163 
02164 #ifdef DEBUG2
02165                 printf("Scanning log file '%s' for archived state data...\n",filename);
02166 #endif
02167 
02168                 for(temp_entry=entry_list;temp_entry!=NULL;temp_entry=temp_entry->next) {
02169 
02170                         /* program starts/restarts */
02171                         if(temp_entry->type==LOGENTRY_STARTUP)
02172                                 add_archived_state(AS_PROGRAM_START,temp_entry->timestamp);
02173                         if(temp_entry->type==LOGENTRY_RESTART)
02174                                 add_archived_state(AS_PROGRAM_START,temp_entry->timestamp);
02175 
02176                         /* program stops */
02177                         if(temp_entry->type==LOGENTRY_SHUTDOWN)
02178                                 add_archived_state(AS_PROGRAM_END,temp_entry->timestamp);
02179                         if(temp_entry->type==LOGENTRY_BAILOUT)
02180                                 add_archived_state(AS_PROGRAM_END,temp_entry->timestamp);
02181 
02182                         if(display_type==DISPLAY_HOST_HISTOGRAM){
02183                                 switch(temp_entry->type){
02184 
02185                                         /* normal host alerts and initial/current states */
02186                                         case LOGENTRY_HOST_DOWN:
02187                                         case LOGENTRY_HOST_UNREACHABLE:
02188                                         case LOGENTRY_HOST_RECOVERY:
02189                                         case LOGENTRY_HOST_UP:
02190 
02191                                                 /* get host name */
02192                                                 temp_buffer=my_strtok(temp_entry->entry_text,":");
02193                                                 temp_buffer=my_strtok(NULL,";");
02194                                                 strncpy(entry_host_name,(temp_buffer==NULL)?"":temp_buffer+1,sizeof(entry_host_name));
02195                                                 entry_host_name[sizeof(entry_host_name)-1]='\x0';
02196 
02197                                                 if(strcmp(host_name,entry_host_name))
02198                                                         continue;
02199 
02200                                                 /* skip soft states if necessary */
02201                                                 if(!(graph_statetypes & GRAPH_SOFT_STATETYPES) && strstr(temp_entry->entry_text,";SOFT;"))
02202                                                         continue;
02203 
02204                                                 /* skip hard states if necessary */
02205                                                 if(!(graph_statetypes & GRAPH_HARD_STATETYPES) && strstr(temp_entry->entry_text,";HARD;"))
02206                                                         continue;
02207 
02208                                                 if(temp_entry->type==LOGENTRY_HOST_DOWN)
02209                                                         add_archived_state(AS_HOST_DOWN,temp_entry->timestamp);
02210                                                 else if(temp_entry->type==LOGENTRY_HOST_UNREACHABLE)
02211                                                         add_archived_state(AS_HOST_UNREACHABLE,temp_entry->timestamp);
02212                                                 else if(temp_entry->type==LOGENTRY_HOST_RECOVERY || temp_entry->type==LOGENTRY_HOST_UP)
02213                                                         add_archived_state(AS_HOST_UP,temp_entry->timestamp);
02214 
02215                                                 break;
02216                                 }
02217                         }
02218 
02219                         else if(display_type==DISPLAY_SERVICE_HISTOGRAM){
02220                                 switch(temp_entry->type){
02221 
02222                                         /* normal service alerts and initial/current states */
02223                                         case LOGENTRY_SERVICE_CRITICAL:
02224                                         case LOGENTRY_SERVICE_WARNING:
02225                                         case LOGENTRY_SERVICE_UNKNOWN:
02226                                         case LOGENTRY_SERVICE_RECOVERY:
02227                                         case LOGENTRY_SERVICE_OK:
02228 
02229                                                 /* get host name */
02230                                                 temp_buffer=my_strtok(temp_entry->entry_text,":");
02231                                                 temp_buffer=my_strtok(NULL,";");
02232                                                 strncpy(entry_host_name,(temp_buffer==NULL)?"":temp_buffer+1,sizeof(entry_host_name));
02233                                                 entry_host_name[sizeof(entry_host_name)-1]='\x0';
02234 
02235                                                 if(strcmp(host_name,entry_host_name))
02236                                                         continue;
02237                                 
02238                                                 /* get service description */
02239                                                 temp_buffer=my_strtok(NULL,";");
02240                                                 strncpy(entry_service_desc,(temp_buffer==NULL)?"":temp_buffer,sizeof(entry_service_desc));
02241                                                 entry_service_desc[sizeof(entry_service_desc)-1]='\x0';
02242 
02243                                                 if(strcmp(service_desc,entry_service_desc))
02244                                                         continue;
02245 
02246                                                 /* skip soft states if necessary */
02247                                                 if(!(graph_statetypes & GRAPH_SOFT_STATETYPES) && strstr(temp_entry->entry_text,";SOFT;"))
02248                                                         continue;
02249 
02250                                                 /* skip hard states if necessary */
02251                                                 if(!(graph_statetypes & GRAPH_HARD_STATETYPES) && strstr(temp_entry->entry_text,";HARD;"))
02252                                                         continue;
02253 
02254                                                 if(temp_entry->type==LOGENTRY_SERVICE_CRITICAL)
02255                                                         add_archived_state(AS_SVC_CRITICAL,temp_entry->timestamp);
02256                                                 else if(temp_entry->type==LOGENTRY_SERVICE_WARNING)
02257                                                         add_archived_state(AS_SVC_WARNING,temp_entry->timestamp);
02258                                                 else if(temp_entry->type==LOGENTRY_SERVICE_UNKNOWN)
02259                                                         add_archived_state(AS_SVC_UNKNOWN,temp_entry->timestamp);
02260                                                 else if(temp_entry->type==LOGENTRY_SERVICE_RECOVERY || temp_entry->type==LOGENTRY_SERVICE_OK)
02261                                                         add_archived_state(AS_SVC_OK,temp_entry->timestamp);
02262                                         break;
02263                                 }
02264                         }
02265                 }
02266         }       
02267 
02268         /* free memory */
02269         free_log_entries();
02270 
02271         return;
02272 }
02273 
02274 
02275 void compute_report_times(void){
02276         time_t current_time;
02277         struct tm *st;
02278         struct tm *et;
02279 
02280         /* get the current time */
02281         time(&current_time);
02282 
02283         st=localtime(&current_time);
02284 
02285         st->tm_sec=start_second;
02286         st->tm_min=start_minute;
02287         st->tm_hour=start_hour;
02288         st->tm_mday=start_day;
02289         st->tm_mon=start_month-1;
02290         st->tm_year=start_year-1900;
02291         st->tm_isdst=-1;
02292 
02293         t1=mktime(st);
02294 
02295         et=localtime(&current_time);
02296 
02297         et->tm_sec=end_second;
02298         et->tm_min=end_minute;
02299         et->tm_hour=end_hour;
02300         et->tm_mday=end_day;
02301         et->tm_mon=end_month-1;
02302         et->tm_year=end_year-1900;
02303         et->tm_isdst=-1;
02304 
02305         t2=mktime(et);
02306 }
02307 
02308 
02309 
02310 /* draws a solid line */
02311 void draw_line(int x1,int y1,int x2,int y2,int color){
02312         int styleSolid[1];
02313 
02314         styleSolid[0]=color;
02315 
02316         /* sets current style to a solid line */
02317         gdImageSetStyle(histogram_image,styleSolid,1);
02318 
02319         /* draws a line (dashed) */
02320         gdImageLine(histogram_image,x1,y1,x2,y2,gdStyled);
02321 
02322         return;
02323 }
02324 
02325 
02326 /* draws a dashed line */
02327 void draw_dashed_line(int x1,int y1,int x2,int y2,int color){
02328         int styleDashed[6];
02329 
02330         styleDashed[0]=color;
02331         styleDashed[1]=color;
02332         styleDashed[2]=gdTransparent;
02333         styleDashed[3]=gdTransparent;
02334         styleDashed[4]=gdTransparent;
02335         styleDashed[5]=gdTransparent;
02336 
02337         /* sets current style to a solid line */
02338         gdImageSetStyle(histogram_image,styleDashed,6);
02339 
02340         /* draws a line (dashed) */
02341         gdImageLine(histogram_image,x1,y1,x2,y2,gdStyled);
02342 
02343         return;
02344 }
02345 
 All Data Structures Files Functions Variables Typedefs Defines