]> git.pld-linux.org Git - packages/nagios.git/blob - nagios-googlemap.patch
- created as patch from http://exchange.nagios.org/directory/Addons/Maps-and-Diagrams...
[packages/nagios.git] / nagios-googlemap.patch
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
3 @@ -27,6 +27,10 @@
4   *
5   *****************************************************************************/
6  
7 +#include <stdio.h>
8 +#include <stdlib.h>
9 +#include <string.h>
10 +
11  #include "../include/config.h"
12  #include "../include/common.h"
13  #include "../include/objects.h"
14 @@ -88,13 +92,29 @@
15  #define LAYOUT_CIRCULAR                 4
16  #define LAYOUT_CIRCULAR_MARKUP          5
17  #define LAYOUT_CIRCULAR_BALLOON         6
18 +#define LAYOUT_GOOGLEMAP                               7
19  
20 +/*#define DEBUG 1*/
21  
22  typedef struct layer_struct{
23         char *layer_name;
24         struct layer_struct *next;
25          }layer;
26  
27 +typedef struct location_t {
28 +       char* name;
29 +       char* note;
30 +       char* latitude;
31 +       char* longitude;
32 +       struct location_t* next;
33 +} location_t;
34 +
35 +typedef struct general_location_t {
36 +       char* key;
37 +       char* zoom;
38 +       char* latitude;
39 +       char* longitude;
40 +} general_location_t;
41  
42  void document_header(int);
43  void document_footer(void);
44 @@ -112,6 +132,10 @@
45  void draw_background_image(void);
46  void draw_background_extras(void);
47  void draw_host_links(void);
48 +
49 +void google_map(void);
50 +void write_google_head_code(void);
51 +
52  void draw_hosts(void);
53  void draw_host_text(char *,int,int);
54  void draw_text(char *,int,int,int);
55 @@ -333,9 +357,14 @@
56                 /* write JavaScript code for popup window */
57                 write_popup_code();
58 +               if ( layout_method == LAYOUT_GOOGLEMAP ) 
59 +                       write_google_head_code();
60  
61                 printf("</head>\n");
62                 
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");
66 +               } else 
67 +                       printf("<body CLASS='statusmap' name='mappage' id='mappage'>\n");
68  
69                 /* include user SSI header */
70                 include_ssi_files(STATUSMAP_CGI,SSI_HEADER);
71 @@ -703,7 +732,7 @@
72                 /* right hand column of top row */
73                 printf("<td align=right valign=top>\n");
74  
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));
80 @@ -717,6 +746,7 @@
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");
86                 printf("</td>\n");
87                 printf("<td CLASS='optBoxItem'>\n");
88 @@ -794,6 +824,11 @@
89         /* display page header */
90         display_page_header();
91  
92 +       if (layout_method==LAYOUT_GOOGLEMAP){
93 +               google_map();
94 +               return;
95 +       }
96 +
97         initialize_graphics();
98         draw_background_image();
99         draw_background_extras();
100 @@ -848,6 +883,13 @@
101    
102  
103         /******************************/
104 +       /***** GOOGLEMAP LAYOUT MODE *****/
105 +       /******************************/
106 +       if(layout_method==LAYOUT_GOOGLEMAP){
107 +                       
108 +       }
109 +
110 +       /******************************/
111         /***** MANUAL LAYOUT MODE *****/
112         /******************************/
113  
114 @@ -1535,10 +1577,19 @@
115         return;
116          }
117  
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");
125  
126 +}
127  
128  /* draws hosts */
129  void draw_hosts(void){
130 +       
131         host *temp_host;
132         int x1, x2;
133         int y1, y2;
134 @@ -1558,6 +1609,8 @@
135         int translated_x;
136         int translated_y;
137  
138 +       if (layout_method==LAYOUT_GOOGLEMAP)
139 +               return;
140         
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  /******************************************************************/
146  
147 +int check_auth_location_host(char* name)
148 +{
149 +       int retVal=FALSE;
150 +       host *this_host;
151 +
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",&current_authdata);
156 +       retVal=is_authorized_for_host(this_host,&current_authdata);
157 +       // printf("is_auth=%d\n",retVal);
158 +       return retVal;
159 +}      
160 +
161 +char* get_status_code(char* name)
162 +{
163 +       char* retVal;
164 +       hoststatus *temp_status;
165 +       temp_status=find_hoststatus(name);
166 +
167 +       /* strip nasty stuff from plugin output */
168 +       sanitize_plugin_output(temp_status->plugin_output);
169 +       
170 +       retVal=malloc(1000);
171 +       retVal[0]=0;
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, "");
177 +               }
178 +
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, "");
184 +               }
185 +
186 +       else if(temp_status->status==HOST_UP)
187 +               strcat(retVal, "Up");
188 +
189 +       else if(temp_status->status==HOST_PENDING)
190 +               strcat(retVal, "Pending");
191 +       return retVal;
192 +}
193 +
194 +void write_google_head_code(void)
195 +{
196 +       char ch;
197 +       char* ptr;
198 +       int rc;
199 +       FILE *fp;
200 +       char stateinfo[1024];   
201 +       char line[1024];        
202 +       char key[1024]; 
203 +       char value[1024];       
204 +       char buf[256]; 
205 +       char* filename;
206 +       int state = 0;
207 +       int counter = 0;
208 +       general_location_t gen_loc;
209 +       location_t* loc;
210 +       location_t* loc_list=NULL;
211 +
212 +
213 +/***************************************
214 + * parse location.cfg
215 + **************************************/
216 +
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";  
220 +               printf("<p>");
221 +               printf("<font color=\"red\">%s</font>\r\n",err_mess);
222 +               printf("</p>");
223 +               return;
224 +       }
225 +
226 +       while(fgets(buf, sizeof(buf), fp)!=NULL) {
227 +               if ( (state == 1) && (buf[0] == '}')) {
228 +                       // we have reached a definition end
229 +                       state = 0;
230 +                       counter++;
231 +                       if (loc_list) {
232 +                               loc->next = loc_list;
233 +                               loc_list = loc;
234 +                       } else {
235 +                               loc_list = loc;
236 +                       }
237 +                       loc = NULL;
238 +                       continue;
239 +               }       
240 +               if ( (state==2) && (buf[0] == '}')) {
241 +                       state=0;
242 +               }
243 +               key[0] = value[0] = 0;
244 +               if (isspace(buf[0]))
245 +                       rc = sscanf(buf, "%*[ \t]%[^ \t]%*[ \t]%[^\n]", key, value);
246 +               else
247 +                       rc = sscanf(buf, "%[^ \t]%*[ \t]%[^\n]", key, value);
248 +
249 +               if ( (rc == 2)) {
250 +                       if ( (state == 0) &&  (strcmp(key,"define")==0) ) {
251 +                               // we have a start of a new definition
252 +
253 +                               // check if it is the default definition
254 +                               if ( (strncmp(value,"default ",8)==0) ||
255 +                                               (strncmp(value,"default{",8)==0)) {
256 +                                       state=2;
257 +                                       continue;
258 +                               }
259 +                               state = 1;
260 +                               // declare a new location
261 +                               loc = calloc(1,sizeof(*loc));
262 +                               continue;
263 +                       }
264 +                       // state 2 == default definition
265 +                       if ( state == 2 ) {
266 +                               if ( strcmp(key,"key") == 0) {
267 +                                       gen_loc.key=strdup(value);
268 +                               }
269 +                               if ( strcmp(key,"zoom") == 0) {
270 +                                       gen_loc.zoom=strdup(value);
271 +                               }
272 +                               if ( strcmp(key,"lat") == 0) {
273 +                                       gen_loc.latitude=strdup(value);
274 +                               }
275 +                               if ( strcmp(key,"long") == 0) {
276 +                                       gen_loc.longitude=strdup(value);
277 +                               }
278 +                       }
279 +                       // state 1 == location definition
280 +                       if ( state == 1 ) {
281 +                               if ( strcmp(key,"host_name") == 0) {
282 +                                       loc->name=strdup(value);
283 +                               } 
284 +                               if ( strcmp(key,"notes") == 0) {
285 +                                       loc->note = strdup(value);
286 +                               }
287 +                               if ( strcmp(key,"lat") == 0) {
288 +                                       loc->latitude = strdup(value);
289 +                               }
290 +                               if ( strcmp(key,"long") == 0) {
291 +                                       loc->longitude = strdup(value);
292 +                               }
293 +                       }
294 +               }
295 +       }
296 +       fclose(fp);
297 +
298 +       printf("<script src='http://maps.google.com/maps?file=api&amp;v=2&key=%s&amp;sensor=false' type='text/javascript'></script>\n",gen_loc.key);
299 +
300 +       printf("<script type='text/javascript'>\n"
301 +       "//<![CDATA[ \n");
302 +
303 +       // printf("<script type=javascript src="webbserverfil" />);
304 +
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"
311 +       "}\n"
312 +       
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"
317 +               "{\n"
318 +                       "var c = ca[i];\n"
319 +                       "while (c.charAt(0)==' ') c = c.substring(1,c.length);\n"
320 +                       "if (c.indexOf(nameEQ) == 0)\n"
321 +                       "{\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"
325 +                               "return c;\n"
326 +                       "}\n"
327 +               "}\n"
328 +               "return null;\n"
329 +       "}\n"
330 +
331 +       "function createMarker(hostname, comment, lat, long, state) {\n"
332 +       " var point = new GLatLng(lat,long);\n"
333 +       "var marker;\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"
345 +       "} else {\n"
346 +               "marker = new GMarker(point);\n"
347 +       "}\n"
348 +               
349 +       "GEvent.addListener(marker, \"click\", function() { \n"
350 +                                                "marker.openInfoWindowHtml(\"<b>\"+hostname+\"</b><br>\"+comment+\"<br>State: \"+state+\"\");\n"
351 +       "});\n"
352 +       "return marker;\n"
353 +       "}\n"
354 +
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"
360 +               
361 +               "GEvent.addListener(map, \"click\", function () {\n"
362 +                       "setMapCookie(map);\n"
363 +               "});\n"
364 +
365 +               "GEvent.addListener(map, \"move\", function () {\n"
366 +                       "setMapCookie(map);\n"
367 +               "});\n"
368 +
369 +               "GEvent.addListener(map, \"zoom\", function () {\n"
370 +                       "setMapCookie(map);\n"
371 +               "});\n"
372 +
373 +               "var googlemaplat = readCookie('googlemaplat');\n"
374 +        "var googlemaplng = readCookie('googlemaplng');\n"
375 +
376 +               "var centerPoint;\n"
377 +               "if(  googlemaplng!=null && googlemaplat!=null ) \n"
378 +                       "centerPoint = new GLatLng(googlemaplat,googlemaplng);\n"
379 +               "else\n");
380 +                       printf("centerPoint = new GLatLng(%s,%s);\n",gen_loc.latitude,gen_loc.longitude);
381 +
382 +               printf("var zoom = map.getZoom();\n"
383 +               "document.layoutform.scaling_factor.value=+zoom;\n");
384 +
385 +
386 +       if ( user_supplied_scaling==TRUE ) 
387 +       {
388 +               printf("map.setCenter(centerPoint, %2.1f);\n",user_scaling_factor);
389 +               printf("document.layoutform.scaling_factor.value=%2.1f;\n",user_scaling_factor);
390 +       } else {
391 +               printf("var googlemapzoom = readCookie('googlemapzoom');\n"
392 +               "if ( googlemapzoom != null ) \n"
393 +               "{\n"
394 +                       "map.setCenter(centerPoint,parseInt(googlemapzoom));\n"
395 +                       "document.layoutform.scaling_factor.value=googlemapzoom;\n"
396 +               "} else\n"
397 +               "{\n");
398 +               printf("map.setCenter(centerPoint, %s);\n",gen_loc.zoom);
399 +                       printf("document.layoutform.scaling_factor.value=%s;\n",gen_loc.zoom);
400 +               printf("}\n");
401 +               user_scaling_factor = atof(gen_loc.zoom);
402 +       }
403 +                       
404 +       loc = loc_list;
405 +       while (loc) {
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);
410 +
411 +               if ( !is_authorized_for_host(temp_host,&current_authdata) && !is_host_in_layer_list(temp_host)) {
412 +                       loc = loc->next;        
413 +                       continue;
414 +               } else {
415 +                       if((in_layer_list==TRUE && exclude_layers==TRUE) || (in_layer_list==FALSE && exclude_layers==FALSE))
416 +                       {
417 +                               loc = loc->next;        
418 +                               continue;
419 +                       }
420 +                       ptr = get_status_code(loc->name);       
421 +                       strcpy(stateinfo,ptr);
422 +                       free(ptr);
423 +
424 +       
425 +                       printf("map.addOverlay(createMarker('%s','",loc->name);
426 +                       ptr = loc->note;
427 +                       while ((ch = *ptr++)) {
428 +                               if (ch == '\'') printf("\\'");
429 +                               else printf("%c", ch);
430 +                       }
431 +                       printf("',%s,%s,'%s'))\n",loc->latitude,loc->longitude,stateinfo);
432 +
433 +                       loc = loc->next;
434 +               }
435 +       }
436 +                       
437 +       printf("}\n"
438 +       "}\n"
439 +       "//]]>\n"
440 +       "</script>\n");
441 +}
442  
443  /* write JavaScript code an layer for popup window */
444  void write_popup_code(void){
This page took 0.118862 seconds and 4 git commands to generate.