/************************************************************************** * * STATUSJSON.C - Nagios Status CGI for JSON export * * Copyright (c) 2008 Yann Jouanin (yann.jouanin@intelunix.fr) * Last Modified: 06-06-2008 * * License: * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *************************************************************************/ #include "../include/config.h" #include "../include/common.h" #include "../include/objects.h" #include "../include/statusdata.h" #include "../include/cgiutils.h" #include "../include/getcgi.h" #include "../include/cgiauth.h" extern time_t program_start; extern char main_config_file[MAX_FILENAME_LENGTH]; extern host *host_list; extern hostgroup *hostgroup_list; extern service *service_list; extern hoststatus *hoststatus_list; extern servicestatus *servicestatus_list; extern servicegroup *servicegroup_list; extern int enable_notifications; extern int execute_service_checks; extern int nagios_process_state; extern char *ping_syntax; void document_header(void); void document_footer(void); void display_hostgroups(void); void display_hosts (void); void display_servicegroups(void); void display_services(void); void display_map(void); char *host_name=""; char *hostgroup_name=""; char *service_desc=""; char *ping_address=""; char *traceroute_address=""; int show_all_hostgroups=TRUE; authdata current_authdata; int main(void){ int result=OK; document_header(); /* read the CGI configuration file */ result=read_cgi_config_file(get_cgi_config_location()); if(result==ERROR){ printf("

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

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

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

\n",main_config_file); document_footer(); return ERROR; } /* read all object configuration data */ result=read_all_object_configuration_data(main_config_file,READ_ALL_OBJECT_DATA); if(result==ERROR){ printf("

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

\n"); document_footer(); return ERROR; } /* read all status data */ result=read_all_status_data(get_cgi_config_location(),READ_ALL_STATUS_DATA); if(result==ERROR){ printf("

Error: Could not read host and service status information!

\n"); document_footer(); free_memory(); return ERROR; } /* get authentication information */ get_authentication_information(¤t_authdata); display_servicegroups(); printf(","); display_hostgroups(); printf(","); display_hosts(); printf(","); display_services(); printf(","); display_map(); document_footer(); /* free all allocated memory */ free_memory(); return OK; } void document_header(void){ char date_time[MAX_DATETIME_LENGTH]; time_t expire_time; time_t current_time; time(¤t_time); printf("Cache-Control: no-store\r\n"); printf("Pragma: no-cache\r\n"); get_time_string(¤t_time,date_time,(int)sizeof(date_time),HTTP_DATE_TIME); printf("Last-Modified: %s\r\n",date_time); expire_time=(time_t)0L; get_time_string(&expire_time,date_time,(int)sizeof(date_time),HTTP_DATE_TIME); printf("Expires: %s\r\n",date_time); printf("Content-type: text/json\r\n\r\n"); printf("{"); return; } void document_footer(void){ printf("}"); return; } void display_hostgroups () { hostgroup * temp_hostgroup=NULL; hostgroupmember *temp_hostmember=NULL; printf("\"hostgroups\":{"); for(temp_hostgroup=hostgroup_list;temp_hostgroup!=NULL;temp_hostgroup=temp_hostgroup->next){ printf("\"%s\":[",temp_hostgroup->group_name); for (temp_hostmember=temp_hostgroup->members;temp_hostmember!=NULL;temp_hostmember=temp_hostmember->next) { printf("\"%s\"",temp_hostmember->host_name); if (temp_hostmember->next != NULL) printf(","); } printf("]"); if ((temp_hostgroup->next)!= NULL) printf(","); } printf("}"); } void display_hosts() { host *temp_host; hoststatus *stattemp; printf("\"hosts\":{"); for(stattemp=hoststatus_list;stattemp!=NULL;stattemp=stattemp->next) { sanitize_plugin_output(stattemp->plugin_output); temp_host=find_host(stattemp->host_name); printf("\"%s\":{\"alias\":\"%s\",\"plugin_output\":\"%s\",\"perf_data\":\"%s\",\"status\":%d}",stattemp->host_name,temp_host->alias,stattemp->plugin_output,stattemp->perf_data,stattemp->status); if ((stattemp->next)!=NULL) printf(","); } printf("}"); } void display_servicegroups() { servicegroup *temp_svg=NULL; servicegroupmember *temp_svgm=NULL; printf("\"servicegroup\":{"); for(temp_svg=servicegroup_list;temp_svg!=NULL;temp_svg=temp_svg->next) { printf("\"%s\":[",temp_svg->group_name); temp_svgm = temp_svg->members; for(;temp_svgm!=NULL;temp_svgm=temp_svgm->next) { printf("{\"hostname\":\"%s\",\"servicename\":\"%s\"}",temp_svgm->host_name,temp_svgm->service_description); if (temp_svgm->next != NULL) printf(",");} printf("]"); if (temp_svg->next != NULL) printf(","); } printf("}"); } void display_services() { servicestatus *servtemp; char * tempstr; tempstr = malloc(sizeof(char) * 1024); printf("\"services\":{"); strcpy(tempstr,""); for(servtemp=servicestatus_list;servtemp!=NULL;servtemp=servtemp->next) { if (strcmp(tempstr,servtemp->host_name)!=0) printf("\"%s\":{",servtemp->host_name); sanitize_plugin_output(servtemp->plugin_output); printf("\"%s\":{\"status\":%d,\"plugin_output\":\"%s\"}",servtemp->description,servtemp->status,servtemp->plugin_output); if (servtemp->next != NULL && strcmp((servtemp->next)->host_name,servtemp->host_name)==0) printf(","); else if (servtemp->next != NULL) printf("},"); if (servtemp->next == NULL) printf("}"); strcpy(tempstr,servtemp->host_name); } printf("}"); } void display_map() { host *temp_host; hostsmember *temp_hostm; printf("\"map\":{"); for (temp_host=host_list;temp_host!=NULL;temp_host=temp_host->next){ printf("\"%s\":{\"nbofparents\":%d,\"nbofchilds\":%d, \"parents\":[",temp_host->name,number_of_immediate_child_hosts(temp_host),number_of_immediate_parent_hosts(temp_host)); for(temp_hostm=temp_host->parent_hosts;temp_hostm!=NULL;temp_hostm->next) { printf("\"%s\"",temp_hostm->host_name); if (temp_hostm->next != NULL) printf(","); } printf("]}"); if (temp_host->next != NULL) printf(","); } printf("}"); }