1 --- nagios-3.2.0/cgi/statusmap.c 2009-10-22 14:54:22.000000000 +0300
2 +++ nagios-3.2.0/cgi/statusmap.c 2009-10-22 16:11:22.450924994 +0300
5 *****************************************************************************/
11 #include "../include/config.h"
12 #include "../include/common.h"
13 #include "../include/objects.h"
15 #define LAYOUT_CIRCULAR 4
16 #define LAYOUT_CIRCULAR_MARKUP 5
17 #define LAYOUT_CIRCULAR_BALLOON 6
18 +#define LAYOUT_GOOGLEMAP 7
22 typedef struct layer_struct{
24 struct layer_struct *next;
27 +typedef struct location_t {
32 + struct location_t* next;
35 +typedef struct general_location_t {
40 +} general_location_t;
42 void document_header(int);
43 void document_footer(void);
45 void draw_background_image(void);
46 void draw_background_extras(void);
47 void draw_host_links(void);
49 +void google_map(void);
50 +void write_google_head_code(void);
52 void draw_hosts(void);
53 void draw_host_text(char *,int,int);
54 void draw_text(char *,int,int,int);
56 /* write JavaScript code for popup window */
58 + if ( layout_method == LAYOUT_GOOGLEMAP )
59 + write_google_head_code();
63 - printf("<body CLASS='statusmap' name='mappage' id='mappage'>\n");
64 + if ( layout_method == LAYOUT_GOOGLEMAP ) {
65 + printf("<body onload='load()' onunload='GUnload()' CLASS='statusmap' name='mappage' id='mappage'>\n");
67 + printf("<body CLASS='statusmap' name='mappage' id='mappage'>\n");
69 /* include user SSI header */
70 include_ssi_files(STATUSMAP_CGI,SSI_HEADER);
72 /* right hand column of top row */
73 printf("<td align=right valign=top>\n");
75 - printf("<form method=\"POST\" action=\"%s\">\n",STATUSMAP_CGI);
76 + printf("<form name=\"layoutform\" method=\"POST\" action=\"%s\">\n",STATUSMAP_CGI);
77 printf("<table border=0 CLASS='optBox'>\n");
78 printf("<tr><td valign=top>\n");
79 printf("<input type='hidden' name='host' value='%s'>\n",escape_string(host_name));
81 printf("<option value=%d %s>Circular\n",LAYOUT_CIRCULAR,(layout_method==LAYOUT_CIRCULAR)?"selected":"");
82 printf("<option value=%d %s>Circular (Marked Up)\n",LAYOUT_CIRCULAR_MARKUP,(layout_method==LAYOUT_CIRCULAR_MARKUP)?"selected":"");
83 printf("<option value=%d %s>Circular (Balloon)\n",LAYOUT_CIRCULAR_BALLOON,(layout_method==LAYOUT_CIRCULAR_BALLOON)?"selected":"");
84 + printf("<option value=%d %s>Googlemap\n",LAYOUT_GOOGLEMAP,(layout_method==LAYOUT_GOOGLEMAP)?"selected":"");
85 printf("</select>\n");
87 printf("<td CLASS='optBoxItem'>\n");
89 /* display page header */
90 display_page_header();
92 + if (layout_method==LAYOUT_GOOGLEMAP){
97 initialize_graphics();
98 draw_background_image();
99 draw_background_extras();
103 /******************************/
104 + /***** GOOGLEMAP LAYOUT MODE *****/
105 + /******************************/
106 + if(layout_method==LAYOUT_GOOGLEMAP){
110 + /******************************/
111 /***** MANUAL LAYOUT MODE *****/
112 /******************************/
114 @@ -1535,10 +1577,19 @@
118 +/* create the googlemap */
119 +void google_map(void){
120 + printf("<!-- Produced by Infoflex Connect AB (http://www.infoflexconnect.se), Fredrik Nilsson and Rujinschi Remus. -->\n");
121 + printf("<P><DIV ALIGN=center>");
122 + printf("<div id='map' style='width: 800px; height: 500px'></div>");
123 + printf("</DIV></P>");
124 + printf("<!-- Produced by Infoflex Connect AB (http://www.infoflexconnect.se), Fredrik Nilsson and Rujinschi Remus. -->\n");
129 void draw_hosts(void){
134 @@ -1558,6 +1609,8 @@
138 + if (layout_method==LAYOUT_GOOGLEMAP)
141 /* user didn't supply any coordinates for hosts, so display a warning */
142 if(coordinates_were_specified==FALSE){
143 @@ -2213,6 +2266,301 @@
144 /************************* MISC FUNCTIONS *************************/
145 /******************************************************************/
147 +int check_auth_location_host(char* name)
152 + //printf("false=%d,true=%d\n",FALSE,TRUE);
153 + this_host=find_host(name);
154 + // printf("this_host=%s\n",this_host->name);
155 + // printf("current_auth=%s\n",¤t_authdata);
156 + retVal=is_authorized_for_host(this_host,¤t_authdata);
157 + // printf("is_auth=%d\n",retVal);
161 +char* get_status_code(char* name)
164 + hoststatus *temp_status;
165 + temp_status=find_hoststatus(name);
167 + /* strip nasty stuff from plugin output */
168 + sanitize_plugin_output(temp_status->plugin_output);
170 + retVal=malloc(1000);
172 + if(temp_status->status==HOST_DOWN){
173 + strcat(retVal,"Down");
174 + if(temp_status->problem_has_been_acknowledged==TRUE)
175 + strcat(retVal," (Acknowledged)");
176 + strcat(retVal, "");
179 + else if(temp_status->status==HOST_UNREACHABLE){
180 + strcat(retVal, "Unreachable");
181 + if(temp_status->problem_has_been_acknowledged==TRUE)
182 + strcat(retVal, " (Acknowledged)");
183 + strcat(retVal, "");
186 + else if(temp_status->status==HOST_UP)
187 + strcat(retVal, "Up");
189 + else if(temp_status->status==HOST_PENDING)
190 + strcat(retVal, "Pending");
194 +void write_google_head_code(void)
200 + char stateinfo[1024];
208 + general_location_t gen_loc;
210 + location_t* loc_list=NULL;
213 +/***************************************
214 + * parse location.cfg
215 + **************************************/
217 + filename = "/etc/webapps/nagios/location.cfg";
218 + if((fp=fopen(filename, "r"))==NULL) {
219 + char* err_mess = "The file: location.cfg does not exist or could not be read";
221 + printf("<font color=\"red\">%s</font>\r\n",err_mess);
226 + while(fgets(buf, sizeof(buf), fp)!=NULL) {
227 + if ( (state == 1) && (buf[0] == '}')) {
228 + // we have reached a definition end
232 + loc->next = loc_list;
240 + if ( (state==2) && (buf[0] == '}')) {
243 + key[0] = value[0] = 0;
244 + if (isspace(buf[0]))
245 + rc = sscanf(buf, "%*[ \t]%[^ \t]%*[ \t]%[^\n]", key, value);
247 + rc = sscanf(buf, "%[^ \t]%*[ \t]%[^\n]", key, value);
250 + if ( (state == 0) && (strcmp(key,"define")==0) ) {
251 + // we have a start of a new definition
253 + // check if it is the default definition
254 + if ( (strncmp(value,"default ",8)==0) ||
255 + (strncmp(value,"default{",8)==0)) {
260 + // declare a new location
261 + loc = calloc(1,sizeof(*loc));
264 + // state 2 == default definition
265 + if ( state == 2 ) {
266 + if ( strcmp(key,"key") == 0) {
267 + gen_loc.key=strdup(value);
269 + if ( strcmp(key,"zoom") == 0) {
270 + gen_loc.zoom=strdup(value);
272 + if ( strcmp(key,"lat") == 0) {
273 + gen_loc.latitude=strdup(value);
275 + if ( strcmp(key,"long") == 0) {
276 + gen_loc.longitude=strdup(value);
279 + // state 1 == location definition
280 + if ( state == 1 ) {
281 + if ( strcmp(key,"host_name") == 0) {
282 + loc->name=strdup(value);
284 + if ( strcmp(key,"notes") == 0) {
285 + loc->note = strdup(value);
287 + if ( strcmp(key,"lat") == 0) {
288 + loc->latitude = strdup(value);
290 + if ( strcmp(key,"long") == 0) {
291 + loc->longitude = strdup(value);
298 + printf("<script src='http://maps.google.com/maps?file=api&v=2&key=%s&sensor=false' type='text/javascript'></script>\n",gen_loc.key);
300 + printf("<script type='text/javascript'>\n"
303 + // printf("<script type=javascript src="webbserverfil" />);
305 + printf("function setMapCookie(map) {"
306 + "document.cookie = 'googlemaplng='+map.getCenter().lng();\n"
307 + "document.cookie = 'googlemaplat='+map.getCenter().lat();\n"
308 + "var zoom = map.getZoom();\n"
309 + "document.cookie = 'googlemapzoom='+zoom;\n"
310 + "document.layoutform.scaling_factor.value=+zoom;\n"
313 + "function readCookie(name) {\n"
314 + "var nameEQ = name + \"=\";\n"
315 + "var ca = document.cookie.split(';');\n"
316 + "for(var i=0;i < ca.length;i++)\n"
319 + "while (c.charAt(0)==' ') c = c.substring(1,c.length);\n"
320 + "if (c.indexOf(nameEQ) == 0)\n"
322 + "c=c.substring(nameEQ.length,c.length);\n"
323 + "while (c.substring(c.length-1, c.length) == ' ')\n"
324 + "c = c.substring(0,c.length-1);\n"
331 + "function createMarker(hostname, comment, lat, long, state) {\n"
332 + " var point = new GLatLng(lat,long);\n"
334 + "if ( state==\"Up\" ) {\n"
335 + "var baseIcon = new GIcon();\n"
336 + "baseIcon.shadow = \"../images/shadow.png\";\n"
337 + "baseIcon.iconSize = new GSize(20, 34);\n"
338 + "baseIcon.shadowSize = new GSize(37, 34);\n"
339 + "baseIcon.iconAnchor = new GPoint(9, 34);\n"
340 + "baseIcon.infoWindowAnchor = new GPoint(9, 2);\n"
341 + "baseIcon.infoShadowAnchor = new GPoint(18, 25);\n"
342 + "var okicon = new GIcon(baseIcon);\n"
343 + "okicon.image = \"../images/green.png\";\n"
344 + "marker = new GMarker(point,okicon);\n"
346 + "marker = new GMarker(point);\n"
349 + "GEvent.addListener(marker, \"click\", function() { \n"
350 + "marker.openInfoWindowHtml(\"<b>\"+hostname+\"</b><br>\"+comment+\"<br>State: \"+state+\"\");\n"
355 + "function load() {\n"
356 + "if (GBrowserIsCompatible()) {\n"
357 + "var map = new GMap2(document.getElementById(\"map\"));\n"
358 + "map.addControl(new GSmallMapControl());\n"
359 + "map.addControl(new GMapTypeControl());\n"
361 + "GEvent.addListener(map, \"click\", function () {\n"
362 + "setMapCookie(map);\n"
365 + "GEvent.addListener(map, \"move\", function () {\n"
366 + "setMapCookie(map);\n"
369 + "GEvent.addListener(map, \"zoom\", function () {\n"
370 + "setMapCookie(map);\n"
373 + "var googlemaplat = readCookie('googlemaplat');\n"
374 + "var googlemaplng = readCookie('googlemaplng');\n"
376 + "var centerPoint;\n"
377 + "if( googlemaplng!=null && googlemaplat!=null ) \n"
378 + "centerPoint = new GLatLng(googlemaplat,googlemaplng);\n"
380 + printf("centerPoint = new GLatLng(%s,%s);\n",gen_loc.latitude,gen_loc.longitude);
382 + printf("var zoom = map.getZoom();\n"
383 + "document.layoutform.scaling_factor.value=+zoom;\n");
386 + if ( user_supplied_scaling==TRUE )
388 + printf("map.setCenter(centerPoint, %2.1f);\n",user_scaling_factor);
389 + printf("document.layoutform.scaling_factor.value=%2.1f;\n",user_scaling_factor);
391 + printf("var googlemapzoom = readCookie('googlemapzoom');\n"
392 + "if ( googlemapzoom != null ) \n"
394 + "map.setCenter(centerPoint,parseInt(googlemapzoom));\n"
395 + "document.layoutform.scaling_factor.value=googlemapzoom;\n"
398 + printf("map.setCenter(centerPoint, %s);\n",gen_loc.zoom);
399 + printf("document.layoutform.scaling_factor.value=%s;\n",gen_loc.zoom);
401 + user_scaling_factor = atof(gen_loc.zoom);
406 + // check if allowed to show hostname first
407 + host* temp_host=find_host(loc->name);
408 + int in_layer_list=FALSE;
409 + in_layer_list=is_host_in_layer_list(temp_host);
411 + if ( !is_authorized_for_host(temp_host,¤t_authdata) && !is_host_in_layer_list(temp_host)) {
415 + if((in_layer_list==TRUE && exclude_layers==TRUE) || (in_layer_list==FALSE && exclude_layers==FALSE))
420 + ptr = get_status_code(loc->name);
421 + strcpy(stateinfo,ptr);
425 + printf("map.addOverlay(createMarker('%s','",loc->name);
427 + while ((ch = *ptr++)) {
428 + if (ch == '\'') printf("\\'");
429 + else printf("%c", ch);
431 + printf("',%s,%s,'%s'))\n",loc->latitude,loc->longitude,stateinfo);
443 /* write JavaScript code an layer for popup window */
444 void write_popup_code(void){