Index: cgi/avail.c
===================================================================
RCS file: /cvsroot/nagios/nagios/cgi/avail.c,v
retrieving revision 1.31
diff -u -r1.31 avail.c
--- cgi/avail.c	17 Jan 2005 17:35:33 -0000	1.31
+++ cgi/avail.c	29 May 2005 12:30:13 -0000
@@ -252,6 +252,8 @@
 void document_footer(void);
 int process_cgivars(void);
 
+void printPageHeader();
+
 
 
 int backtrack_archives=2;
@@ -796,7 +798,8 @@
 		printf("<input type='text' name='backtrack' size='2' maxlength='2' value='%d'>\n",backtrack_archives);
 		printf("</td></tr>\n");
 
-		if((display_type==DISPLAY_HOST_AVAIL && show_all_hosts==TRUE) || (display_type==DISPLAY_SERVICE_AVAIL && show_all_services==TRUE)){
+		if((display_type==DISPLAY_HOST_AVAIL && show_all_hosts==TRUE) || (display_type==DISPLAY_SERVICE_AVAIL && show_all_services==TRUE) ||
+			(display_type==DISPLAY_HOSTGROUP_AVAIL) || (display_type==DISPLAY_SERVICEGROUP_AVAIL)){
 			printf("<tr>");
 			printf("<td valign=top class='reportSelectSubTitle'>Output in CSV Format:</td>\n");
 			printf("<td valign=top class='reportSelectItem'>");
@@ -3414,49 +3417,407 @@
 void display_hostgroup_availability(void){
 	hostgroup *temp_hostgroup;
 
+	printPageHeader();
+
 	/* display data for a specific hostgroup */
 	if(show_all_hostgroups==FALSE){
 		temp_hostgroup=find_hostgroup(hostgroup_name);
 		display_specific_hostgroup_availability(temp_hostgroup);
-	        }
+	}
 
 	/* display data for all hostgroups */
 	else{
 		for(temp_hostgroup=hostgroup_list;temp_hostgroup!=NULL;temp_hostgroup=temp_hostgroup->next)
 			display_specific_hostgroup_availability(temp_hostgroup);
-	        }
+	}
 
 	return;
-        }
-
+}
 
 
-/* display availability for a specific hostgroup */
-void display_specific_hostgroup_availability(hostgroup *hg){
-	unsigned long total_time;
-	unsigned long time_determinate;
-	unsigned long time_indeterminate;
-	avail_subject *temp_subject;
+void printHostlist(int hasHostName, hostgroup *hg, servicegroup *sg, avail_subject *subject, unsigned long total_time, int lineCounter) {
 	double percent_time_up=0.0;
 	double percent_time_down=0.0;
 	double percent_time_unreachable=0.0;
+	double percent_time_indeterminate=0.0;
 	double percent_time_up_known=0.0;
 	double percent_time_down_known=0.0;
 	double percent_time_unreachable_known=0.0;
-	double percent_time_indeterminate=0.0;
+	double percent_scheduled_time_up=0.0;
+	double percent_scheduled_time_up_known=0.0;
+	double percent_unscheduled_time_up=0.0;
+	double percent_unscheduled_time_up_known=0.0;
+	double percent_scheduled_time_down=0.0;
+	double percent_scheduled_time_down_known=0.0;
+	double percent_unscheduled_time_down=0.0;
+	double percent_unscheduled_time_down_known=0.0;
+	double percent_scheduled_time_unreachable=0.0;
+	double percent_scheduled_time_unreachable_known=0.0;
+	double percent_unscheduled_time_unreachable=0.0;
+	double percent_unscheduled_time_unreachable_known=0.0;
+	char *bgclass="";
 
-	double average_percent_time_up=0.0;
-	double average_percent_time_up_known=0.0;
-	double average_percent_time_down=0.0;
-	double average_percent_time_down_known=0.0;
-	double average_percent_time_unreachable=0.0;
-	double average_percent_time_unreachable_known=0.0;
-	double average_percent_time_indeterminate=0.0;
+	unsigned long time_determinate=subject->time_up+subject->time_down+subject->time_unreachable;
+	unsigned long time_indeterminate=total_time-time_determinate;
+
+	if(total_time>0){
+		percent_time_up=(double)(((double)subject->time_up*100.0)/(double)total_time);
+		percent_time_down=(double)(((double)subject->time_down*100.0)/(double)total_time);
+		percent_time_unreachable=(double)(((double)subject->time_unreachable*100.0)/(double)total_time);
+		percent_time_indeterminate=(double)(((double)time_indeterminate*100.0)/(double)total_time);
+		if (time_determinate > 0){
+			percent_time_up_known=(double)(((double)subject->time_up*100.0)/(double)time_determinate);
+			percent_time_down_known=(double)(((double)subject->time_down*100.0)/(double)time_determinate);
+			percent_time_unreachable_known=(double)(((double)subject->time_unreachable*100.0)/(double)time_determinate);
+			percent_scheduled_time_up_known=(double)(((double)subject->scheduled_time_up*100.0)/(double)time_determinate);
+			percent_unscheduled_time_up_known=(double)(((double)(subject->time_up-subject->scheduled_time_up)*100.0)/(double)time_determinate);
+			percent_scheduled_time_down_known=(double)(((double)subject->scheduled_time_down*100.0)/(double)time_determinate);
+			percent_unscheduled_time_down_known=(double)(((double)(subject->time_down-subject->scheduled_time_down)*100.0)/(double)time_determinate);
+			percent_scheduled_time_unreachable_known=(double)(((double)subject->scheduled_time_unreachable*100.0)/(double)time_determinate);
+			percent_unscheduled_time_unreachable_known=(double)(((double)(subject->time_unreachable-subject->scheduled_time_unreachable)*100.0)/(double)time_determinate);
+		}
+		percent_scheduled_time_up=(double)(((double)subject->scheduled_time_up*100.0)/(double)total_time);
+		percent_unscheduled_time_up=(double)(((double)(subject->time_up-subject->scheduled_time_up)*100.0)/(double)total_time);
+		percent_scheduled_time_down=(double)(((double)subject->scheduled_time_down*100.0)/(double)total_time);
+		percent_unscheduled_time_down=(double)(((double)(subject->time_down-subject->scheduled_time_down)*100.0)/(double)total_time);
+		percent_scheduled_time_unreachable=(double)(((double)subject->scheduled_time_unreachable*100.0)/(double)total_time);
+		percent_unscheduled_time_unreachable=(double)(((double)(subject->time_unreachable-subject->scheduled_time_unreachable)*100.0)/(double)total_time);
+	}
+
+	if(output_format==HTML_OUTPUT){
+		if(lineCounter%2==1){
+			bgclass="Odd";
+		} else {
+			bgclass="Even";
+		}
+		printf("<tr CLASS='data%s'><td CLASS='data%s'>",bgclass,bgclass);
+		if (hasHostName)
+			host_report_url(subject->host_name,subject->host_name);
+		else 
+			printf("%s", subject->host_name);
+		printf("</td>");
+		// print service name if we have a service...
+		printf("<td CLASS='hostUP'>%2.3f%% (%2.3f%%)</td>"
+			"<td CLASS='hostDOWN'>%2.3f%% (%2.3f%%)</td>"
+			"<td CLASS='hostUNREACHABLE'>%2.3f%% (%2.3f%%)</td>"
+			"<td class='data%s'>%2.3f%%</td></tr>\n",
+			percent_time_up,percent_time_up_known,
+			percent_time_down,percent_time_down_known,
+			percent_time_unreachable,percent_time_unreachable_known,
+			bgclass,percent_time_indeterminate);
+	} else if (output_format == CSV_OUTPUT) {
+		if (hg) {
+			printf("%s, ", hg->group_name);
+		} else {
+			printf(", ");
+		}
+		if (sg) {
+			printf("%s, ", sg->group_name);
+		} else {
+			printf(", ");
+		}
+		printf("%s,,%lu, ", subject->host_name, total_time);
+		printf("%lu, %2.3f%%, %2.3f%%, "/* scheduled_time_up */
+			"%lu, %2.3f%%, %2.3f%%, "	/* unscheduled_time_up */
+			"%lu, %2.3f%%, %2.3f%%, "	/* time_up */
+			"%lu, %2.3f%%, %2.3f%%, "	/* scheduled_time_down */
+			"%lu, %2.3f%%, %2.3f%%, "	/* unscheduled_time_down */
+			"%lu, %2.3f%%, %2.3f%%, "	/* time_down */
+			"%lu, %2.3f%%, %2.3f%%, "	/* time_unreachable */
+			"%lu, %2.3f%%, %2.3f%%, "	/* scheduled_time_unreachable */
+			"%lu, %2.3f%%, %2.3f%%, "	/* unscheduled_time_unreachable */
+			",,,,,,,,,"					/* time_ok */
+			",,,,,,,,,"					/* time_warning */
+			",,,,,,,,,"					/* time_unknown */
+			",,,,,,,,,"					/* time_critical */
+			",,,,"						/* not_running / no_data */
+			"%lu, %2.3f%%\n",			/* indeterminate */
+
+			subject->scheduled_time_up, percent_scheduled_time_up, percent_scheduled_time_up_known,
+			(subject->time_up-subject->scheduled_time_up), percent_unscheduled_time_up, percent_unscheduled_time_up_known,
+			subject->time_up, percent_time_up, percent_time_up_known,
+
+			subject->scheduled_time_down, percent_scheduled_time_down, percent_scheduled_time_down_known,
+			(subject->time_down-subject->scheduled_time_down), percent_unscheduled_time_down, percent_unscheduled_time_down_known,
+			subject->time_down, percent_time_down, percent_time_down_known,
+
+			subject->time_unreachable, percent_time_unreachable, percent_time_unreachable_known,
+			(subject->time_unreachable-subject->scheduled_time_unreachable), percent_unscheduled_time_unreachable, percent_unscheduled_time_unreachable_known,
+			subject->time_unreachable, percent_time_unreachable, percent_time_unreachable_known,
+
+			time_indeterminate, percent_time_indeterminate);
+	}
+}
+void printServicelist(int hasHostName, int hasServiceName, hostgroup *hg, servicegroup *sg, avail_subject *subject, unsigned long total_time, int lineCounter) {
+	double percent_time_ok=0.0;
+	double percent_time_ok_known=0.0;
+	double percent_scheduled_time_ok=0.0;
+	double percent_scheduled_time_ok_known=0.0;
+	double percent_unscheduled_time_ok=0.0;
+	double percent_unscheduled_time_ok_known=0.0;
+	double percent_time_unknown=0.0;
+	double percent_time_unknown_known=0.0;
+	double percent_scheduled_time_unknown=0.0;
+	double percent_scheduled_time_unknown_known=0.0;
+	double percent_unscheduled_time_unknown=0.0;
+	double percent_unscheduled_time_unknown_known=0.0;
+	double percent_time_warning=0.0;
+	double percent_time_warning_known=0.0;
+	double percent_scheduled_time_warning=0.0;
+	double percent_scheduled_time_warning_known=0.0;
+	double percent_unscheduled_time_warning=0.0;
+	double percent_unscheduled_time_warning_known=0.0;
+	double percent_time_critical=0.0;
+	double percent_time_critical_known=0.0;
+	double percent_scheduled_time_critical=0.0;
+	double percent_scheduled_time_critical_known=0.0;
+	double percent_unscheduled_time_critical=0.0;
+	double percent_unscheduled_time_critical_known=0.0;
+	double percent_time_indeterminate=0.0;
+	double percent_time_indeterminate_scheduled=0.0;
+	double percent_time_indeterminate_unscheduled=0.0;
+	double percent_time_indeterminate_notrunning=0.0;
+	double percent_time_indeterminate_nodata=0.0;
 
-	int current_subject=0;
 
 	char *bgclass="";
-	int odd=1;
+
+	unsigned long time_determinate=subject->time_ok+subject->time_warning+subject->time_unknown+subject->time_critical;
+	unsigned long time_indeterminate=total_time-time_determinate;
+
+	if(total_time>0){
+		percent_time_ok=(double)(((double)subject->time_ok*100.0)/(double)total_time);
+		percent_scheduled_time_ok=(double)(((double)subject->scheduled_time_ok*100.0)/(double)total_time);
+		percent_unscheduled_time_ok=(double)(((double)(subject->time_ok-subject->scheduled_time_ok)*100.0)/(double)total_time);
+		percent_time_warning=(double)(((double)subject->time_warning*100.0)/(double)total_time);
+		percent_scheduled_time_warning=(double)(((double)subject->scheduled_time_warning*100.0)/(double)total_time);
+		percent_unscheduled_time_warning=(double)(((double)(subject->time_warning-subject->scheduled_time_warning)*100.0)/(double)total_time);
+		percent_time_unknown=(double)(((double)subject->time_unknown*100.0)/(double)total_time);
+		percent_scheduled_time_unknown=(double)(((double)subject->scheduled_time_unknown*100.0)/(double)total_time);
+		percent_unscheduled_time_unknown=(double)(((double)(subject->time_unknown-subject->scheduled_time_unknown)*100.0)/(double)total_time);
+		percent_time_critical=(double)(((double)subject->time_critical*100.0)/(double)total_time);
+		percent_scheduled_time_critical=(double)(((double)subject->scheduled_time_critical*100.0)/(double)total_time);
+		percent_unscheduled_time_critical=(double)(((double)(subject->time_critical-subject->scheduled_time_critical)*100.0)/(double)total_time);
+		
+		percent_time_indeterminate=(double)(((double)time_indeterminate*100.0)/(double)total_time);
+		percent_time_indeterminate_scheduled=(double)(((double)subject->scheduled_time_indeterminate*100.0)/(double)total_time);
+		percent_time_indeterminate_unscheduled=percent_time_indeterminate-percent_time_indeterminate_scheduled;
+		percent_time_indeterminate_notrunning=(double)(((double)subject->time_indeterminate_notrunning*100.0)/(double)total_time);
+		percent_time_indeterminate_nodata=(double)(((double)subject->time_indeterminate_nodata*100.0)/(double)total_time);
+
+
+		if(time_determinate>0){
+			percent_time_ok_known=(double)(((double)subject->time_ok*100.0)/(double)time_determinate);
+			percent_scheduled_time_ok_known=(double)(((double)subject->scheduled_time_ok*100.0)/(double)time_determinate);
+			percent_unscheduled_time_ok_known=(double)(((double)(subject->time_ok-subject->scheduled_time_ok)*100.0)/(double)time_determinate);
+			percent_time_warning_known=(double)(((double)subject->time_warning*100.0)/(double)time_determinate);
+			percent_scheduled_time_warning_known=(double)(((double)subject->scheduled_time_warning*100.0)/(double)time_determinate);
+			percent_unscheduled_time_warning_known=(double)(((double)(subject->time_warning-subject->scheduled_time_warning)*100.0)/(double)time_determinate);
+			percent_time_unknown_known=(double)(((double)subject->time_unknown*100.0)/(double)time_determinate);
+			percent_scheduled_time_unknown_known=(double)(((double)subject->scheduled_time_unknown*100.0)/(double)time_determinate);
+			percent_unscheduled_time_unknown_known=(double)(((double)(subject->time_unknown-subject->scheduled_time_unknown)*100.0)/(double)time_determinate);
+			percent_time_critical_known=(double)(((double)subject->time_critical*100.0)/(double)time_determinate);
+			percent_scheduled_time_critical_known=(double)(((double)subject->scheduled_time_critical*100.0)/(double)time_determinate);
+			percent_unscheduled_time_critical_known=(double)(((double)(subject->time_critical-subject->scheduled_time_critical)*100.0)/(double)time_determinate);
+		}
+	}
+
+	if(output_format==HTML_OUTPUT){
+		if(lineCounter%2==1){
+			bgclass="Odd";
+		} else {
+			bgclass="Even";
+		}
+		printf("<tr CLASS='data%s'>",bgclass);
+		if ((hasHostName)&&(hasServiceName)) {
+			printf("<td CLASS='data%s'>",bgclass);
+			host_report_url(subject->host_name,subject->host_name);
+			printf("</td><td CLASS='data%s'>",bgclass);
+			service_report_url(subject->host_name,subject->service_description,subject->service_description);
+			printf("</td>");
+		} else if (hasHostName) {
+			printf("<td CLASS='data%s' colspan=2>",bgclass);
+			host_report_url(subject->host_name,subject->host_name);
+			printf("</td>");
+		} else if (hasServiceName) {
+			printf("<td CLASS='data%s'>&nbsp;</td><td CLASS='data%s'>",bgclass, bgclass);
+			service_report_url(subject->host_name,subject->service_description,subject->service_description);
+			printf("</td>");
+		} else {
+			printf("<td CLASS='data%s' colspan=2>&nbsp;</td>",bgclass);
+		}
+		printf("<td CLASS='serviceOK'>%2.3f%% (%2.3f%%)</td>"
+			"<td CLASS='serviceWARNING'>%2.3f%% (%2.3f%%)</td>"
+			"<td CLASS='serviceUNKNOWN'>%2.3f%% (%2.3f%%)</td>"
+			"<td class='serviceCRITICAL'>%2.3f%% (%2.3f%%)</td>"
+			"<td class='data%s'>%2.3f%%</td></tr>\n",
+			percent_time_ok, percent_time_ok_known,
+			percent_time_warning, percent_time_warning_known,
+			percent_time_unknown, percent_time_unknown_known,
+			percent_time_critical, percent_time_critical_known,
+			bgclass, percent_time_indeterminate);
+	} else if (output_format == CSV_OUTPUT) {
+		if (hg) {
+			printf("%s, ", hg->group_name);
+		} else {
+			printf(", ");
+		}
+		if (sg) {
+			printf("%s, ", sg->group_name);
+		} else {
+			printf(", ");
+		}
+		printf("%s, %s, %lu, ", subject->host_name, subject->service_description, total_time);
+		
+		printf(
+			",,,,,,,,,"				 /* time_up */
+			",,,,,,,,,"				 /* time_down */
+			",,,,,,,,,"				 /* time_unreachable */
+			"%lu, %2.3f%%, %2.3f%%, " /* scheduled_time_ok */
+			"%lu, %2.3f%%, %2.3f%%, " /* unscheduled_time_ok */
+			"%lu, %2.3f%%, %2.3f%%, " /* time_ok */
+
+			"%lu, %2.3f%%, %2.3f%%, " /* scheduled_time_warning */
+			"%lu, %2.3f%%, %2.3f%%, " /* unscheduled_time_warning */
+			"%lu, %2.3f%%, %2.3f%%, " /* time_warning */
+
+			"%lu, %2.3f%%, %2.3f%%, " /* scheduled_time_unknown */
+			"%lu, %2.3f%%, %2.3f%%, " /* unscheduled_time_unknown */
+			"%lu, %2.3f%%, %2.3f%%, " /* time_unknown */
+
+			"%lu, %2.3f%%, %2.3f%%, " /* scheduled_time_critical */
+			"%lu, %2.3f%%, %2.3f%%, " /* unscheduled_time_critical */
+			"%lu, %2.3f%%, %2.3f%%, " /* time_critical */
+			"%lu, %2.3f%%, "			 /* not_running */
+			"%lu, %2.3f%%, "			 /* no_data */
+			"%lu, %2.3f%%\n",			 /* interminate */
+
+			subject->scheduled_time_ok, percent_scheduled_time_ok, percent_scheduled_time_ok_known,
+			(subject->time_ok-subject->scheduled_time_ok), percent_unscheduled_time_ok, percent_unscheduled_time_ok_known,
+			subject->time_ok, percent_time_ok, percent_time_ok_known,
+
+			subject->scheduled_time_warning, percent_scheduled_time_warning, percent_scheduled_time_warning_known,
+			(subject->time_warning-subject->scheduled_time_warning), percent_unscheduled_time_warning, percent_unscheduled_time_warning_known,
+			subject->time_warning, percent_time_warning, percent_time_warning_known,
+
+			subject->scheduled_time_unknown, percent_scheduled_time_unknown, percent_scheduled_time_unknown_known,
+			(subject->time_unknown-subject->scheduled_time_unknown), percent_unscheduled_time_unknown, percent_unscheduled_time_unknown_known,
+			subject->time_unknown, percent_time_unknown, percent_time_unknown_known,
+
+			subject->scheduled_time_critical, percent_scheduled_time_critical, percent_scheduled_time_critical_known,
+			(subject->time_critical-subject->scheduled_time_critical), percent_unscheduled_time_critical, percent_unscheduled_time_critical_known,
+			subject->time_critical, percent_time_critical, percent_time_critical_known,
+
+			subject->time_indeterminate_notrunning, percent_time_indeterminate_notrunning,
+			subject->time_indeterminate_nodata,percent_time_indeterminate_nodata,
+			time_indeterminate, percent_time_indeterminate);
+	}
+}
+
+void printPageHeader() {
+	if(output_format==HTML_OUTPUT){
+	} else 	if(output_format==CSV_OUTPUT) {
+		printf("HOST_GROUP_NAME, SERVICE_GROUP_NAME, HOST_NAME, SERVICE_NAME, TOTAL_TIME, ");
+		printf(" TIME_UP_SCHEDULED, PERCENT_TIME_UP_SCHEDULED, PERCENT_KNOWN_TIME_UP_SCHEDULED, TIME_UP_UNSCHEDULED, PERCENT_TIME_UP_UNSCHEDULED, PERCENT_KNOWN_TIME_UP_UNSCHEDULED, TOTAL_TIME_UP, PERCENT_TOTAL_TIME_UP, PERCENT_KNOWN_TIME_UP,");
+		printf(" TIME_DOWN_SCHEDULED, PERCENT_TIME_DOWN_SCHEDULED, PERCENT_KNOWN_TIME_DOWN_SCHEDULED, TIME_DOWN_UNSCHEDULED, PERCENT_TIME_DOWN_UNSCHEDULED, PERCENT_KNOWN_TIME_DOWN_UNSCHEDULED, TOTAL_TIME_DOWN, PERCENT_TOTAL_TIME_DOWN, PERCENT_KNOWN_TIME_DOWN,");
+		printf(" TIME_UNREACHABLE_SCHEDULED, PERCENT_TIME_UNREACHABLE_SCHEDULED, PERCENT_KNOWN_TIME_UNREACHABLE_SCHEDULED, TIME_UNREACHABLE_UNSCHEDULED, PERCENT_TIME_UNREACHABLE_UNSCHEDULED, PERCENT_KNOWN_TIME_UNREACHABLE_UNSCHEDULED, TOTAL_TIME_UNREACHABLE, PERCENT_TOTAL_TIME_UNREACHABLE, PERCENT_KNOWN_TIME_UNREACHABLE,");
+
+		printf(" TIME_OK_SCHEDULED, PERCENT_TIME_OK_SCHEDULED, PERCENT_KNOWN_TIME_OK_SCHEDULED, TIME_OK_UNSCHEDULED, PERCENT_TIME_OK_UNSCHEDULED, PERCENT_KNOWN_TIME_OK_UNSCHEDULED, TOTAL_TIME_OK, PERCENT_TOTAL_TIME_OK, PERCENT_KNOWN_TIME_OK,");
+		printf(" TIME_WARNING_SCHEDULED, PERCENT_TIME_WARNING_SCHEDULED, PERCENT_KNOWN_TIME_WARNING_SCHEDULED, TIME_WARNING_UNSCHEDULED, PERCENT_TIME_WARNING_UNSCHEDULED, PERCENT_KNOWN_TIME_WARNING_UNSCHEDULED, TOTAL_TIME_WARNING, PERCENT_TOTAL_TIME_WARNING, PERCENT_KNOWN_TIME_WARNING,");
+		printf(" TIME_UNKNOWN_SCHEDULED, PERCENT_TIME_UNKNOWN_SCHEDULED, PERCENT_KNOWN_TIME_UNKNOWN_SCHEDULED, TIME_UNKNOWN_UNSCHEDULED, PERCENT_TIME_UNKNOWN_UNSCHEDULED, PERCENT_KNOWN_TIME_UNKNOWN_UNSCHEDULED, TOTAL_TIME_UNKNOWN, PERCENT_TOTAL_TIME_UNKNOWN, PERCENT_KNOWN_TIME_UNKNOWN,");
+		printf(" TIME_CRITICAL_SCHEDULED, PERCENT_TIME_CRITICAL_SCHEDULED, PERCENT_KNOWN_TIME_CRITICAL_SCHEDULED, TIME_CRITICAL_UNSCHEDULED, PERCENT_TIME_CRITICAL_UNSCHEDULED, PERCENT_KNOWN_TIME_CRITICAL_UNSCHEDULED, TOTAL_TIME_CRITICAL, PERCENT_TOTAL_TIME_CRITICAL, PERCENT_KNOWN_TIME_CRITICAL,");
+
+		printf(" TIME_UNDETERMINED_NOT_RUNNING, PERCENT_TIME_UNDETERMINED_NOT_RUNNING, TIME_UNDETERMINED_NO_DATA, PERCENT_TIME_UNDETERMINED_NO_DATA, TOTAL_TIME_UNDETERMINED, PERCENT_TOTAL_TIME_UNDETERMINED\n");
+	}
+}
+
+void printHostlistHeadder(hostgroup *hg, servicegroup *sg) {
+	if(output_format==HTML_OUTPUT){
+		printf("<br/><br/>\n");
+		if (hg)
+			printf("<div align=center class='dataTitle'>Hostgroup '%s' Host State Breakdowns:</div>\n",hg->group_name);
+		else if (sg)
+			printf("<div align=center class='dataTitle'>Servicegroup '%s' Host State Breakdowns:</div>\n",sg->group_name);
+		printf("<div align=center >\n");
+		printf("<table border=0 class='data'>\n");
+		printf("<tr><th class='data'>Host</th><th class='data'>%% Time Up</th><th class='data'>%% Time Down</th><th class='data'>%% Time Unreachable</th><th class='data'>%% Time Undetermined</th></tr>\n");
+	} else 	if(output_format==CSV_OUTPUT) {}
+}
+void printServicelistHeadder(hostgroup *hg, servicegroup *sg) {
+	if(output_format==HTML_OUTPUT){
+		printf("<br/><br/>\n");
+		if (hg)
+			printf("<div align=center class='dataTitle'>Hostgroup '%s' Service State Breakdowns:</div>\n",hg->group_name);
+		else if (sg)
+			printf("<div align=center class='dataTitle'>Servicegroup '%s' Service State Breakdowns:</div>\n",sg->group_name);
+		else
+			printf("<div align=center class='dataTitle'>Unknowngroup '' Service State Breakdowns:</div>\n");
+		printf("<div align=center >\n");
+		printf("<table border=0 class='data'>\n");
+		printf("<tr><th class='data'>Host</th><th class='data'>Service</th>"
+			"<th class='data'>%% Time OK</th><th class='data'>%% Time Warning</th>"
+			"<th class='data'>%% Time Unknown</th><th class='data'>%% Time Critical</th>"
+			"<th class='data'>%% Time Undetermined</th></tr>\n");
+	} else if(output_format==CSV_OUTPUT) {}
+}
+void printHostlistFooter(hostgroup *hg, servicegroup *sg) {
+	if(output_format==HTML_OUTPUT){
+		printf("</table></div>\n");
+	} else 	if(output_format==CSV_OUTPUT) {}
+}
+void printServicelistFooter(hostgroup *hg, servicegroup *sg) {
+	if(output_format==HTML_OUTPUT){
+		printf("</table></div>\n");
+	} else 	if(output_format==CSV_OUTPUT) {}
+}
+
+void normalizeAverages(avail_subject *subject, int subjects) {
+	/* normalize average values */
+	/* @bug This has rounding bugs that I don't know if they matter perhaps a double is better then int here ?) */
+	subject->time_up /= subjects;
+	subject->time_down /= subjects;
+	subject->time_unreachable /= subjects;
+	subject->scheduled_time_up /= subjects;
+	subject->scheduled_time_down /= subjects;
+	subject->scheduled_time_unreachable /= subjects;
+	subject->time_ok /= subjects;
+	subject->time_warning /= subjects;
+	subject->time_unknown /= subjects;
+	subject->time_critical /= subjects;
+	subject->time_indeterminate_nodata /= subjects;
+	subject->time_indeterminate_notrunning /= subjects;
+	subject->scheduled_time_ok /= subjects;
+	subject->scheduled_time_warning /= subjects;
+	subject->scheduled_time_unknown /= subjects;
+	subject->scheduled_time_critical /= subjects;
+	subject->scheduled_time_indeterminate /= subjects;
+}
+void resetAverages(avail_subject *subject) {
+	subject->time_up = 0;
+	subject->time_down = 0;
+	subject->time_unreachable = 0;
+	subject->scheduled_time_up = 0;
+	subject->scheduled_time_down = 0;
+	subject->scheduled_time_unreachable = 0;
+	subject->time_ok = 0;
+	subject->time_warning = 0;
+	subject->time_unknown = 0;
+	subject->time_critical = 0;
+	subject->time_indeterminate_nodata = 0;
+	subject->time_indeterminate_notrunning = 0;
+	subject->scheduled_time_ok = 0;
+	subject->scheduled_time_warning = 0;
+	subject->scheduled_time_unknown = 0;
+	subject->scheduled_time_critical = 0;
+	subject->scheduled_time_indeterminate = 0;
+}
+/* display availability for a specific hostgroup */
+void display_specific_hostgroup_availability(hostgroup *hg){
+	avail_subject average;
+
+	unsigned long total_time;
+	avail_subject *temp_subject;
+	int current_subject=0;
 	host *temp_host;
 
 	if(hg==NULL)
@@ -3466,16 +3827,17 @@
 	if(is_authorized_for_hostgroup(hg,&current_authdata)==FALSE)
 		return;
 
+	/* reset average element */
+	resetAverages(&average);
+	average.host_name = "average";
+
 	/* calculate total time during period based on timeperiod used for reporting */
 	total_time=calculate_total_time(t1,t2);
 
-	printf("<BR><BR>\n");
-	printf("<DIV ALIGN=CENTER CLASS='dataTitle'>Hostgroup '%s' Host State Breakdowns:</DIV>\n",hg->group_name);
-
-	printf("<DIV ALIGN=CENTER>\n");
-	printf("<TABLE BORDER=0 CLASS='data'>\n");
-	printf("<TR><TH CLASS='data'>Host</TH><TH CLASS='data'>%% Time Up</TH><TH CLASS='data'>%% Time Down</TH><TH CLASS='data'>%% Time Unreachable</TH><TH CLASS='data'>%% Time Undetermined</TH></TR>\n");
+	/* print a nice header */
+	printHostlistHeadder(hg, NULL);
 
+	/* iterate over all hosts in the set */
 	for(temp_subject=subject_list;temp_subject!=NULL;temp_subject=temp_subject->next){
 
 		if(temp_subject->type!=HOST_SUBJECT)
@@ -3490,88 +3852,46 @@
 
 		current_subject++;
 
-		/* reset variables */
-		percent_time_up=0.0;
-		percent_time_down=0.0;
-		percent_time_unreachable=0.0;
-		percent_time_indeterminate=0.0;
-		percent_time_up_known=0.0;
-		percent_time_down_known=0.0;
-		percent_time_unreachable_known=0.0;
-
-		time_determinate=temp_subject->time_up+temp_subject->time_down+temp_subject->time_unreachable;
-		time_indeterminate=total_time-time_determinate;
-	
-		if(total_time>0){
-			percent_time_up=(double)(((double)temp_subject->time_up*100.0)/(double)total_time);
-			percent_time_down=(double)(((double)temp_subject->time_down*100.0)/(double)total_time);
-			percent_time_unreachable=(double)(((double)temp_subject->time_unreachable*100.0)/(double)total_time);
-			percent_time_indeterminate=(double)(((double)time_indeterminate*100.0)/(double)total_time);
-			if(time_determinate>0){
-				percent_time_up_known=(double)(((double)temp_subject->time_up*100.0)/(double)time_determinate);
-				percent_time_down_known=(double)(((double)temp_subject->time_down*100.0)/(double)time_determinate);
-				percent_time_unreachable_known=(double)(((double)temp_subject->time_unreachable*100.0)/(double)time_determinate);
-	                        }
-		        }
-
-		if(odd){
-			odd=0;
-			bgclass="Odd";
-	                }
-		else{
-			odd=1;
-			bgclass="Even";
-	                }
-
-		printf("<tr CLASS='data%s'><td CLASS='data%s'>",bgclass,bgclass);
-		host_report_url(temp_subject->host_name,temp_subject->host_name);
-		printf("</td><td CLASS='hostUP'>%2.3f%% (%2.3f%%)</td><td CLASS='hostDOWN'>%2.3f%% (%2.3f%%)</td><td CLASS='hostUNREACHABLE'>%2.3f%% (%2.3f%%)</td><td class='data%s'>%2.3f%%</td></tr>\n",percent_time_up,percent_time_up_known,percent_time_down,percent_time_down_known,percent_time_unreachable,percent_time_unreachable_known,bgclass,percent_time_indeterminate);
-
-		get_running_average(&average_percent_time_up,percent_time_up,current_subject);
-		get_running_average(&average_percent_time_up_known,percent_time_up_known,current_subject);
-		get_running_average(&average_percent_time_down,percent_time_down,current_subject);
-		get_running_average(&average_percent_time_down_known,percent_time_down_known,current_subject);
-		get_running_average(&average_percent_time_unreachable,percent_time_unreachable,current_subject);
-		get_running_average(&average_percent_time_unreachable_known,percent_time_unreachable_known,current_subject);
-		get_running_average(&average_percent_time_indeterminate,percent_time_indeterminate,current_subject);
-                }
-
-	/* average statistics */
-	if(odd){
-		odd=0;
-		bgclass="Odd";
-	        }
-	else{
-		odd=1;
-		bgclass="Even";
-      	        }
-	printf("<tr CLASS='data%s'><td CLASS='data%s'>Average</td><td CLASS='hostUP'>%2.3f%% (%2.3f%%)</td><td CLASS='hostDOWN'>%2.3f%% (%2.3f%%)</td><td CLASS='hostUNREACHABLE'>%2.3f%% (%2.3f%%)</td><td class='data%s'>%2.3f%%</td></tr>",bgclass,bgclass,average_percent_time_up,average_percent_time_up_known,average_percent_time_down,average_percent_time_down_known,average_percent_time_unreachable,average_percent_time_unreachable_known,bgclass,average_percent_time_indeterminate);
-
-	printf("</table>\n");
-	printf("</DIV>\n");
+		printHostlist(1, hg, NULL, temp_subject, total_time, current_subject);
 
+		/* calculate average values */
+		average.time_up += temp_subject->time_up;
+		average.time_down += temp_subject->time_down;
+		average.time_unreachable += temp_subject->time_unreachable;
+		average.scheduled_time_up += temp_subject->scheduled_time_up;
+		average.scheduled_time_down += temp_subject->scheduled_time_down;
+		average.scheduled_time_unreachable += temp_subject->scheduled_time_unreachable;
+	}
+
+	if (current_subject > 0) {
+		normalizeAverages(&average, current_subject);
+		printHostlist(0, hg, NULL, &average, total_time, current_subject+1);
+	}
+	printHostlistFooter(hg, NULL);
 	return;
-        }
+}
 
 
 /* display servicegroup availability */
 void display_servicegroup_availability(void){
 	servicegroup *temp_servicegroup;
 
+	printPageHeader();
+
 	/* display data for a specific servicegroup */
 	if(show_all_servicegroups==FALSE){
 		temp_servicegroup=find_servicegroup(servicegroup_name);
 		display_specific_servicegroup_availability(temp_servicegroup);
-	        }
+	}
 
 	/* display data for all servicegroups */
 	else{
 		for(temp_servicegroup=servicegroup_list;temp_servicegroup!=NULL;temp_servicegroup=temp_servicegroup->next)
 			display_specific_servicegroup_availability(temp_servicegroup);
-	        }
+	}
 
 	return;
-        }
+}
 
 
 
@@ -3581,42 +3901,8 @@
 	unsigned long time_determinate;
 	unsigned long time_indeterminate;
 	avail_subject *temp_subject;
-	double percent_time_up=0.0;
-	double percent_time_down=0.0;
-	double percent_time_unreachable=0.0;
-	double percent_time_up_known=0.0;
-	double percent_time_down_known=0.0;
-	double percent_time_unreachable_known=0.0;
-	double percent_time_indeterminate=0.0;
-	double percent_time_ok=0.0;
-	double percent_time_warning=0.0;
-	double percent_time_unknown=0.0;
-	double percent_time_critical=0.0;
-	double percent_time_ok_known=0.0;
-	double percent_time_warning_known=0.0;
-	double percent_time_unknown_known=0.0;
-	double percent_time_critical_known=0.0;
-
-	double average_percent_time_up=0.0;
-	double average_percent_time_up_known=0.0;
-	double average_percent_time_down=0.0;
-	double average_percent_time_down_known=0.0;
-	double average_percent_time_unreachable=0.0;
-	double average_percent_time_unreachable_known=0.0;
-	double average_percent_time_ok=0.0;
-	double average_percent_time_ok_known=0.0;
-	double average_percent_time_unknown=0.0;
-	double average_percent_time_unknown_known=0.0;
-	double average_percent_time_warning=0.0;
-	double average_percent_time_warning_known=0.0;
-	double average_percent_time_critical=0.0;
-	double average_percent_time_critical_known=0.0;
-	double average_percent_time_indeterminate=0.0;
-
+	avail_subject average;
 	int current_subject=0;
-
-	char *bgclass="";
-	int odd=1;
 	host *temp_host;
 	service *temp_service;
 	char last_host[MAX_INPUT_BUFFER];
@@ -3628,15 +3914,15 @@
 	if(is_authorized_for_servicegroup(sg,&current_authdata)==FALSE)
 		return;
 
+	/* reset average element */
+	average.host_name = "average";
+	average.service_description = "average";
+	resetAverages(&average);
+
 	/* calculate total time during period based on timeperiod used for reporting */
 	total_time=calculate_total_time(t1,t2);
 
-	printf("<BR><BR>\n");
-	printf("<DIV ALIGN=CENTER CLASS='dataTitle'>Servicegroup '%s' Host State Breakdowns:</DIV>\n",sg->group_name);
-
-	printf("<DIV ALIGN=CENTER>\n");
-	printf("<TABLE BORDER=0 CLASS='data'>\n");
-	printf("<TR><TH CLASS='data'>Host</TH><TH CLASS='data'>%% Time Up</TH><TH CLASS='data'>%% Time Down</TH><TH CLASS='data'>%% Time Unreachable</TH><TH CLASS='data'>%% Time Undetermined</TH></TR>\n");
+	printHostlistHeadder(NULL, sg);
 
 	for(temp_subject=subject_list;temp_subject!=NULL;temp_subject=temp_subject->next){
 
@@ -3651,77 +3937,30 @@
 			continue;
 
 		current_subject++;
+		printHostlist(1, NULL, sg, temp_subject, total_time, current_subject);
 
-		/* reset variables */
-		percent_time_up=0.0;
-		percent_time_down=0.0;
-		percent_time_unreachable=0.0;
-		percent_time_indeterminate=0.0;
-		percent_time_up_known=0.0;
-		percent_time_down_known=0.0;
-		percent_time_unreachable_known=0.0;
-
-		time_determinate=temp_subject->time_up+temp_subject->time_down+temp_subject->time_unreachable;
-		time_indeterminate=total_time-time_determinate;
-	
-		if(total_time>0){
-			percent_time_up=(double)(((double)temp_subject->time_up*100.0)/(double)total_time);
-			percent_time_down=(double)(((double)temp_subject->time_down*100.0)/(double)total_time);
-			percent_time_unreachable=(double)(((double)temp_subject->time_unreachable*100.0)/(double)total_time);
-			percent_time_indeterminate=(double)(((double)time_indeterminate*100.0)/(double)total_time);
-			if(time_determinate>0){
-				percent_time_up_known=(double)(((double)temp_subject->time_up*100.0)/(double)time_determinate);
-				percent_time_down_known=(double)(((double)temp_subject->time_down*100.0)/(double)time_determinate);
-				percent_time_unreachable_known=(double)(((double)temp_subject->time_unreachable*100.0)/(double)time_determinate);
-	                        }
-		        }
+		/* calculate average values */
+		average.time_up += temp_subject->time_up;
+		average.time_down += temp_subject->time_down;
+		average.time_unreachable += temp_subject->time_unreachable;
+		average.scheduled_time_up += temp_subject->scheduled_time_up;
+		average.scheduled_time_down += temp_subject->scheduled_time_down;
+		average.scheduled_time_unreachable += temp_subject->scheduled_time_unreachable;
+	}
+
+	if (current_subject > 0) {
+		normalizeAverages(&average, current_subject);
+		printHostlist(0, NULL, sg, &average, total_time, current_subject+1);
+	}
+	printHostlistFooter(NULL, sg);
 
-		if(odd){
-			odd=0;
-			bgclass="Odd";
-	                }
-		else{
-			odd=1;
-			bgclass="Even";
-	                }
+	/* reset averages */
+	resetAverages(&average);
 
-		printf("<tr CLASS='data%s'><td CLASS='data%s'>",bgclass,bgclass);
-		host_report_url(temp_subject->host_name,temp_subject->host_name);
-		printf("</td><td CLASS='hostUP'>%2.3f%% (%2.3f%%)</td><td CLASS='hostDOWN'>%2.3f%% (%2.3f%%)</td><td CLASS='hostUNREACHABLE'>%2.3f%% (%2.3f%%)</td><td class='data%s'>%2.3f%%</td></tr>\n",percent_time_up,percent_time_up_known,percent_time_down,percent_time_down_known,percent_time_unreachable,percent_time_unreachable_known,bgclass,percent_time_indeterminate);
-
-		get_running_average(&average_percent_time_up,percent_time_up,current_subject);
-		get_running_average(&average_percent_time_up_known,percent_time_up_known,current_subject);
-		get_running_average(&average_percent_time_down,percent_time_down,current_subject);
-		get_running_average(&average_percent_time_down_known,percent_time_down_known,current_subject);
-		get_running_average(&average_percent_time_unreachable,percent_time_unreachable,current_subject);
-		get_running_average(&average_percent_time_unreachable_known,percent_time_unreachable_known,current_subject);
-		get_running_average(&average_percent_time_indeterminate,percent_time_indeterminate,current_subject);
-                }
-
-	/* average statistics */
-	if(odd){
-		odd=0;
-		bgclass="Odd";
-	        }
-	else{
-		odd=1;
-		bgclass="Even";
-      	        }
-	printf("<tr CLASS='data%s'><td CLASS='data%s'>Average</td><td CLASS='hostUP'>%2.3f%% (%2.3f%%)</td><td CLASS='hostDOWN'>%2.3f%% (%2.3f%%)</td><td CLASS='hostUNREACHABLE'>%2.3f%% (%2.3f%%)</td><td class='data%s'>%2.3f%%</td></tr>",bgclass,bgclass,average_percent_time_up,average_percent_time_up_known,average_percent_time_down,average_percent_time_down_known,average_percent_time_unreachable,average_percent_time_unreachable_known,bgclass,average_percent_time_indeterminate);
-
-	printf("</table>\n");
-	printf("</DIV>\n");
-
-	printf("<BR>\n");
-	printf("<DIV ALIGN=CENTER CLASS='dataTitle'>Servicegroup '%s' Service State Breakdowns:</DIV>\n",sg->group_name);
-
-	printf("<DIV ALIGN=CENTER>\n");
-	printf("<TABLE BORDER=0 CLASS='data'>\n");
-	printf("<TR><TH CLASS='data'>Host</TH><TH CLASS='data'>Service</TH><TH CLASS='data'>%% Time OK</TH><TH CLASS='data'>%% Time Warning</TH><TH CLASS='data'>%% Time Unknown</TH><TH CLASS='data'>%% Time Critical</TH><TH CLASS='data'>%% Time Undetermined</TH></TR>\n");
+	/* print the service list */
+	printServicelistHeadder(NULL, sg);
 
 	current_subject=0;
-	average_percent_time_indeterminate=0.0;
-
 	for(temp_subject=subject_list;temp_subject!=NULL;temp_subject=temp_subject->next){
 
 		if(temp_subject->type!=SERVICE_SUBJECT)
@@ -3736,82 +3975,35 @@
 
 		current_subject++;
 
-		time_determinate=temp_subject->time_ok+temp_subject->time_warning+temp_subject->time_unknown+temp_subject->time_critical;
-		time_indeterminate=total_time-time_determinate;
-
-		/* adjust indeterminate time due to insufficient data (not all was caught) */
-		temp_subject->time_indeterminate_nodata=time_indeterminate-temp_subject->time_indeterminate_notrunning;
-
-		/* initialize values */
-		percent_time_ok=0.0;
-		percent_time_warning=0.0;
-		percent_time_unknown=0.0;
-		percent_time_critical=0.0;
-		percent_time_ok_known=0.0;
-		percent_time_warning_known=0.0;
-		percent_time_unknown_known=0.0;
-		percent_time_critical_known=0.0;
-
-		if(total_time>0){
-			percent_time_ok=(double)(((double)temp_subject->time_ok*100.0)/(double)total_time);
-			percent_time_warning=(double)(((double)temp_subject->time_warning*100.0)/(double)total_time);
-			percent_time_unknown=(double)(((double)temp_subject->time_unknown*100.0)/(double)total_time);
-			percent_time_critical=(double)(((double)temp_subject->time_critical*100.0)/(double)total_time);
-			if(time_determinate>0){
-				percent_time_ok_known=(double)(((double)temp_subject->time_ok*100.0)/(double)time_determinate);
-				percent_time_warning_known=(double)(((double)temp_subject->time_warning*100.0)/(double)time_determinate);
-				percent_time_unknown_known=(double)(((double)temp_subject->time_unknown*100.0)/(double)time_determinate);
-				percent_time_critical_known=(double)(((double)temp_subject->time_critical*100.0)/(double)time_determinate);
-	                        }
-                        }
-
-		if(odd){
-			odd=0;
-			bgclass="Odd";
-	                }
-		else{
-			odd=1;
-			bgclass="Even";
-	                }
-
-		printf("<tr CLASS='data%s'><td CLASS='data%s'>",bgclass,bgclass);
 		if(strcmp(temp_subject->host_name,last_host))
-			host_report_url(temp_subject->host_name,temp_subject->host_name);
-		printf("</td><td CLASS='data%s'>",bgclass);
-		service_report_url(temp_subject->host_name,temp_subject->service_description,temp_subject->service_description);
-		printf("</td><td CLASS='serviceOK'>%2.3f%% (%2.3f%%)</td><td CLASS='serviceWARNING'>%2.3f%% (%2.3f%%)</td><td CLASS='serviceUNKNOWN'>%2.3f%% (%2.3f%%)</td><td class='serviceCRITICAL'>%2.3f%% (%2.3f%%)</td><td class='data%s'>%2.3f%%</td></tr>\n",percent_time_ok,percent_time_ok_known,percent_time_warning,percent_time_warning_known,percent_time_unknown,percent_time_unknown_known,percent_time_critical,percent_time_critical_known,bgclass,percent_time_indeterminate);
-
+			printServicelist(1, 1, NULL, sg, temp_subject, total_time, current_subject);
+		else
+			printServicelist(0, 1, NULL, sg, temp_subject, total_time, current_subject);
 		strncpy(last_host,temp_subject->host_name,sizeof(last_host)-1);
 		last_host[sizeof(last_host)-1]='\x0';
 
-		get_running_average(&average_percent_time_ok,percent_time_ok,current_subject);
-		get_running_average(&average_percent_time_ok_known,percent_time_ok_known,current_subject);
-		get_running_average(&average_percent_time_unknown,percent_time_unknown,current_subject);
-		get_running_average(&average_percent_time_unknown_known,percent_time_unknown_known,current_subject);
-		get_running_average(&average_percent_time_warning,percent_time_warning,current_subject);
-		get_running_average(&average_percent_time_warning_known,percent_time_warning_known,current_subject);
-		get_running_average(&average_percent_time_critical,percent_time_critical,current_subject);
-		get_running_average(&average_percent_time_critical_known,percent_time_critical_known,current_subject);
-		get_running_average(&average_percent_time_indeterminate,percent_time_indeterminate,current_subject);
-	        }
-
-	/* display average stats */
-	if(odd){
-		odd=0;
-		bgclass="Odd";
-	        }
-	else{
-		odd=1;
-		bgclass="Even";
-	        }
-
-	printf("<tr CLASS='data%s'><td CLASS='data%s' colspan='2'>Average</td><td CLASS='serviceOK'>%2.3f%% (%2.3f%%)</td><td CLASS='serviceWARNING'>%2.3f%% (%2.3f%%)</td><td CLASS='serviceUNKNOWN'>%2.3f%% (%2.3f%%)</td><td class='serviceCRITICAL'>%2.3f%% (%2.3f%%)</td><td class='data%s'>%2.3f%%</td></tr>\n",bgclass,bgclass,average_percent_time_ok,average_percent_time_ok_known,average_percent_time_warning,average_percent_time_warning_known,average_percent_time_unknown,average_percent_time_unknown_known,average_percent_time_critical,average_percent_time_critical_known,bgclass,average_percent_time_indeterminate);
 
-	printf("</table>\n");
-	printf("</DIV>\n");
+		/* calculate average values */
+		average.time_ok += temp_subject->time_ok;
+		average.time_warning += temp_subject->time_warning;
+		average.time_critical += temp_subject->time_critical;
+		average.time_indeterminate_nodata += temp_subject->time_indeterminate_nodata;
+		average.time_indeterminate_notrunning += temp_subject->time_indeterminate_notrunning;
+		average.scheduled_time_ok += temp_subject->scheduled_time_ok;
+		average.scheduled_time_warning += temp_subject->scheduled_time_warning;
+		average.scheduled_time_critical += temp_subject->scheduled_time_critical;
+		average.scheduled_time_unreachable += temp_subject->scheduled_time_unreachable;
+		average.scheduled_time_indeterminate += temp_subject->scheduled_time_indeterminate;
+	}
+
+	if (current_subject > 0) {
+		normalizeAverages(&average, current_subject);
+		printServicelist(0, 0, NULL, sg, &average, total_time, current_subject+1);
+	}
 
+	printServicelistFooter(NULL, sg);
 	return;
-        }
+	}
 
 
 /* display host availability */
@@ -4258,7 +4450,9 @@
 				printf("\"%s\",",temp_subject->host_name);
 				
 				/* up times */
-				printf(" %lu, %2.3f%%, %2.3f%%, %lu, %2.3f%%, %2.3f%%, %lu, %2.3f%%, %2.3f%%,",temp_subject->scheduled_time_up,percent_time_up_scheduled,percent_time_up_scheduled_known,temp_subject->time_up-temp_subject->scheduled_time_up,percent_time_up_unscheduled,percent_time_up_unscheduled_known,temp_subject->time_up,percent_time_up,percent_time_up_known);
+				printf(" %lu, %2.3f%%, %2.3f%%, %lu, %2.3f%%, %2.3f%%, %lu, %2.3f%%, %2.3f%%,",
+					temp_subject->scheduled_time_up,percent_time_up_scheduled,percent_time_up_scheduled_known,
+					temp_subject->time_up-temp_subject->scheduled_time_up,percent_time_up_unscheduled,percent_time_up_unscheduled_known,temp_subject->time_up,percent_time_up,percent_time_up_known);
 
 				/* down times */
 				printf(" %lu, %2.3f%%, %2.3f%%, %lu, %2.3f%%, %2.3f%%, %lu, %2.3f%%, %2.3f%%,",temp_subject->scheduled_time_down,percent_time_down_scheduled,percent_time_down_scheduled_known,temp_subject->time_down-temp_subject->scheduled_time_down,percent_time_down_unscheduled,percent_time_down_unscheduled_known,temp_subject->time_down,percent_time_down,percent_time_down_known);
@@ -4782,11 +4976,9 @@
 
 /* calculates running average */
 void get_running_average(double *running_average, double new_value, int current_item){
-
 	*running_average=(((*running_average*((double)current_item-1.0))+new_value)/(double)current_item);
-
 	return;
-        }
+}
 
 
 /* used in reports where a timeperiod is selected */
