[icinga-checkins] icinga.org: icinga-core/master: adding json output to notifications. cgi #1217

git at icinga.org git at icinga.org
Sat Apr 30 21:33:36 CEST 2011


Module: icinga-core
Branch: master
Commit: 400a21dbc028ab303d468a16e1389cee775e2d93
URL:    https://git.icinga.org/?p=icinga-core.git;a=commit;h=400a21dbc028ab303d468a16e1389cee775e2d93

Author: Ricardo Bartels <ricardo at bitchbrothers.com>
Date:   Tue Apr 12 23:21:19 2011 +0200

adding json output to notifications.cgi #1217

refs #1217

---

 cgi/cgiutils.c      |   18 +++++++++++++++---
 cgi/notifications.c |   34 ++++++++++++++++++++++++++++++----
 include/cgiutils.h  |    1 +
 3 files changed, 46 insertions(+), 7 deletions(-)

diff --git a/cgi/cgiutils.c b/cgi/cgiutils.c
index a8802f6..56fe5cc 100644
--- a/cgi/cgiutils.c
+++ b/cgi/cgiutils.c
@@ -907,6 +907,13 @@ void document_header(int cgi_id, int use_stylesheet){
 		return;
 	}
 
+	if(content_type==JSON_CONTENT) {
+		printf("Content-type: text/json; charset=\"%s\"\r\n\r\n", http_charset);
+		printf("{\n");
+		return;
+	}
+
+
 	if(cgi_id!=ERROR_CGI_ID){
 		// send HTML CONTENT
 		printf("Content-type: text/html; charset=\"%s\"\r\n\r\n", http_charset);
@@ -1069,9 +1076,6 @@ void document_footer(int cgi_id){
                         break;
 	}
 
-	if(embedded || content_type!=HTML_CONTENT)
-		return;
-
 	if(content_type==WML_CONTENT){
 		/* used by cmd.cgi */
 		printf("</card>\n");
@@ -1079,6 +1083,14 @@ void document_footer(int cgi_id){
 		return;
 	}
 
+	if(content_type==JSON_CONTENT){
+		printf("}\n");
+		return;
+	}
+
+	if(embedded || content_type!=HTML_CONTENT)
+		return;
+
 	if(cgi_id==STATUSWML_CGI_ID) {
 		printf("</wml>\n");
 		return;
diff --git a/cgi/notifications.c b/cgi/notifications.c
index 456c04d..1cb96be 100644
--- a/cgi/notifications.c
+++ b/cgi/notifications.c
@@ -382,6 +382,12 @@ int process_cgivars(void){
 			content_type=CSV_CONTENT;
 		}
 
+		/* we found the JSON output option */
+		else if(!strcmp(variables[x],"jsonoutput")){
+			display_header=FALSE;
+			content_type=JSON_CONTENT;
+		}
+
 		/* we found the order argument */
 		else if(!strcmp(variables[x],"oldestfirst"))
 			reverse=TRUE;
@@ -435,6 +441,7 @@ void display_notifications(void){
 	int total_notifications=0;
 	int notification_detail_type=NOTIFICATION_SERVICE_CRITICAL;
 	int odd=0;
+	int json_start=TRUE;
 	host *temp_host=NULL;
 	service *temp_service=NULL;
 	logentry *temp_entry=NULL;
@@ -460,7 +467,9 @@ void display_notifications(void){
 	}
 
 	if (status==READLOG_OK) {
-		if(content_type==CSV_CONTENT) {
+		if(content_type==JSON_CONTENT)
+			printf("\"notifications\": [\n");
+		else if(content_type==CSV_CONTENT) {
 			printf("%sHOST%s%s",csv_data_enclosure,csv_data_enclosure,csv_delimiter);
 			printf("%sSERVICE%s%s",csv_data_enclosure,csv_data_enclosure,csv_delimiter);
 			printf("%sTYPE%s%s",csv_data_enclosure,csv_data_enclosure,csv_delimiter);
@@ -661,8 +670,21 @@ void display_notifications(void){
 					odd=0;
 				else
 					odd=1;
-			
-				if(content_type==CSV_CONTENT) {
+
+				if(content_type==JSON_CONTENT) {
+					if (json_start==FALSE)
+						printf(",\n");
+					printf("{\"host\": \"%s\", ",(temp_host->display_name!=NULL)?temp_host->display_name:temp_host->name);
+					if(temp_entry->type==LOGENTRY_SERVICE_NOTIFICATION)
+						printf("\"service\": \"%s\", ",(temp_service->display_name!=NULL)?temp_service->display_name:temp_service->description);
+					else
+						printf("\"service\": \"N/A\", ");
+					printf("\"type\": \"%s\", ",alert_level);
+					printf("\"time\": \"%s\", ",date_time);
+					printf("\"contact\": \"%s\", ",contact_name);
+					printf("\"notification_command\": \"%s\", ",method_name);
+					printf("\"information\": \"%s\"}",escape_newlines(temp_buffer));
+				} else if(content_type==CSV_CONTENT) {
 					printf("%s%s%s%s",csv_data_enclosure,(temp_host->display_name!=NULL)?temp_host->display_name:temp_host->name,csv_data_enclosure,csv_delimiter);
 					if(temp_entry->type==LOGENTRY_SERVICE_NOTIFICATION)
 						printf("%s%s%s%s",csv_data_enclosure,(temp_service->display_name!=NULL)?temp_service->display_name:temp_service->description,csv_data_enclosure,csv_delimiter);
@@ -688,13 +710,15 @@ void display_notifications(void){
 					printf("<td CLASS='notifications%s'>%s</td>\n",(odd)?"Even":"Odd",html_encode(temp_buffer,FALSE));
 					printf("</tr>\n");
 				}
+				if (json_start==TRUE)
+					json_start=FALSE;
 			}
 		}
 	}
 
 	free_log_entries();
 
-	if(content_type!=CSV_CONTENT){
+	if(content_type!=CSV_CONTENT && content_type!=JSON_CONTENT){
 		printf("</table>\n");
 
 		printf("</div>\n");
@@ -713,6 +737,8 @@ void display_notifications(void){
 			printf(" in %s log file</DIV></P>",(log_archive==0)?"the current":"this archived");
 		} else
 			printf("<DIV align=center>%d Notification%s</DIV>",total_notifications,(total_notifications==1)?"":"s");
+	}else if (content_type==JSON_CONTENT){
+		printf("\n]\n");
 	}
 
 	return;
diff --git a/include/cgiutils.h b/include/cgiutils.h
index 3ece28d..ea3876e 100644
--- a/include/cgiutils.h
+++ b/include/cgiutils.h
@@ -274,6 +274,7 @@ extern "C" {
 #define WML_CONTENT		1
 #define IMAGE_CONTENT		2
 #define CSV_CONTENT		3
+#define JSON_CONTENT		4
 
 
 /************************ CSV OUTPUT CHARACTERS ****************************/





More information about the icinga-checkins mailing list