]> git.pld-linux.org Git - packages/nagios.git/blob - nagios-googlemap.patch
- cleanups
[packages/nagios.git] / nagios-googlemap.patch
1 --- nagios-3.2.0/cgi/statusmap.c        2009-10-22 17:06:10.117113115 +0300
2 +++ nagios-3.2.0.googlemap/cgi/statusmap.c      2009-10-22 17:05:32.644615709 +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 @@ -93,13 +97,27 @@
15  #define LAYOUT_CIRCULAR                 4
16  #define LAYOUT_CIRCULAR_MARKUP          5
17  #define LAYOUT_CIRCULAR_BALLOON         6
18 -
19 +#define LAYOUT_GOOGLEMAP                               7
20  
21  typedef struct layer_struct{
22         char *layer_name;
23         struct layer_struct *next;
24          }layer;
25  
26 +typedef struct location_t {
27 +       char* name;
28 +       char* note;
29 +       char* latitude;
30 +       char* longitude;
31 +       struct location_t* next;
32 +} location_t;
33 +
34 +typedef struct general_location_t {
35 +       char* key;
36 +       char* zoom;
37 +       char* latitude;
38 +       char* longitude;
39 +} general_location_t;
40  
41  void document_header(int);
42  void document_footer(void);
43 @@ -117,6 +135,10 @@
44  void draw_background_image(void);
45  void draw_background_extras(void);
46  void draw_host_links(void);
47 +
48 +void google_map(void);
49 +void write_google_head_code(void);
50 +
51  void draw_hosts(void);
52  void draw_host_text(char *,int,int);
53  void draw_text(char *,int,int,int);
54 @@ -345,9 +367,14 @@
55  
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 +               if (layout_method == LAYOUT_GOOGLEMAP) {
64 +                       printf("<body onload='load()' onunload='GUnload()' CLASS='statusmap' name='mappage' id='mappage'>\n");
65 +               } else 
66                 printf("<body CLASS='statusmap' name='mappage' id='mappage'>\n");
67  
68                 /* include user SSI header */
69 @@ -703,7 +730,7 @@
70                 /* right hand column of top row */
71                 printf("<td align=right valign=top>\n");
72  
73 -               printf("<form method=\"POST\" action=\"%s\">\n",STATUSMAP_CGI);
74 +               printf("<form name=\"layoutform\" method=\"POST\" action=\"%s\">\n",STATUSMAP_CGI);
75                 printf("<table border=0 CLASS='optBox'>\n");
76                 printf("<tr><td valign=top>\n");
77                 printf("<input type='hidden' name='host' value='%s'>\n",escape_string(host_name));
78 @@ -725,6 +752,7 @@
79                 printf("<option value=%d %s>Circular\n",LAYOUT_CIRCULAR,(layout_method==LAYOUT_CIRCULAR)?"selected":"");
80                 printf("<option value=%d %s>Circular (Marked Up)\n",LAYOUT_CIRCULAR_MARKUP,(layout_method==LAYOUT_CIRCULAR_MARKUP)?"selected":"");
81                 printf("<option value=%d %s>Circular (Balloon)\n",LAYOUT_CIRCULAR_BALLOON,(layout_method==LAYOUT_CIRCULAR_BALLOON)?"selected":"");
82 +               printf("<option value=%d %s>Googlemap\n",LAYOUT_GOOGLEMAP,(layout_method==LAYOUT_GOOGLEMAP)?"selected":"");
83                 printf("</select>\n");
84                 printf("</td>\n");
85                 printf("<td CLASS='optBoxItem'>\n");
86 @@ -821,6 +849,11 @@
87         /* display page header */
88         display_page_header();
89  
90 +       if (layout_method==LAYOUT_GOOGLEMAP){
91 +               google_map();
92 +               return;
93 +       }
94 +
95         initialize_graphics();
96         draw_background_image();
97         draw_background_extras();
98 @@ -876,6 +909,13 @@
99    
100  
101         /******************************/
102 +       /***** GOOGLEMAP LAYOUT MODE *****/
103 +       /******************************/
104 +       if(layout_method==LAYOUT_GOOGLEMAP){
105 +                       
106 +       }
107 +
108 +       /******************************/
109         /***** MANUAL LAYOUT MODE *****/
110         /******************************/
111  
112 @@ -1563,10 +1603,19 @@
113         return;
114          }
115  
116 +/* create the googlemap */
117 +void google_map(void){
118 +       printf("<!-- Produced by Infoflex Connect AB (http://www.infoflexconnect.se), Fredrik Nilsson and Rujinschi Remus. -->\n");
119 +       printf("<P><DIV ALIGN=center>");
120 +       printf("<div id='map' style='width: 800px; height: 500px'></div>");
121 +       printf("</DIV></P>");
122 +       printf("<!-- Produced by Infoflex Connect AB (http://www.infoflexconnect.se), Fredrik Nilsson and Rujinschi Remus. -->\n");
123  
124 +}
125  
126  /* draws hosts */
127  void draw_hosts(void){
128 +       
129         host *temp_host;
130         int x1, x2;
131         int y1, y2;
132 @@ -1586,6 +1635,8 @@
133         int translated_x;
134         int translated_y;
135  
136 +       if (layout_method==LAYOUT_GOOGLEMAP)
137 +               return;
138         
139         /* user didn't supply any coordinates for hosts, so display a warning */
140         if(coordinates_were_specified==FALSE){
141 @@ -2241,6 +2292,287 @@
142  /************************* MISC FUNCTIONS *************************/
143  /******************************************************************/
144  
145 +char* get_status_code(char* name)
146 +{
147 +       char* retVal;
148 +       hoststatus *temp_status;
149 +       temp_status=find_hoststatus(name);
150 +
151 +       /* strip nasty stuff from plugin output */
152 +       sanitize_plugin_output(temp_status->plugin_output);
153 +       
154 +       retVal=malloc(1000);
155 +       retVal[0]=0;
156 +       if(temp_status->status==HOST_DOWN){
157 +               strcat(retVal,"Down");
158 +               if(temp_status->problem_has_been_acknowledged==TRUE)
159 +                       strcat(retVal," (Acknowledged)");
160 +               strcat(retVal, "");
161 +               }
162 +
163 +       else if(temp_status->status==HOST_UNREACHABLE){
164 +               strcat(retVal, "Unreachable");
165 +               if(temp_status->problem_has_been_acknowledged==TRUE)
166 +                       strcat(retVal, " (Acknowledged)");
167 +               strcat(retVal, "");
168 +               }
169 +
170 +       else if(temp_status->status==HOST_UP)
171 +               strcat(retVal, "Up");
172 +
173 +       else if(temp_status->status==HOST_PENDING)
174 +               strcat(retVal, "Pending");
175 +       return retVal;
176 +}
177 +
178 +void write_google_head_code(void)
179 +{
180 +       char ch;
181 +       char* ptr;
182 +       int rc;
183 +       FILE *fp;
184 +       char stateinfo[1024];   
185 +       char line[1024];        
186 +       char key[1024]; 
187 +       char value[1024];       
188 +       char buf[256];
189 +       char* filename;
190 +       int state = 0;
191 +       int counter = 0;
192 +       general_location_t gen_loc;
193 +       location_t* loc;
194 +       location_t* loc_list=NULL;
195 +
196 +
197 +/***************************************
198 + * parse location.cfg
199 + **************************************/
200 +
201 +       filename = "/etc/webapps/nagios/location.cfg";
202 +       if((fp=fopen(filename, "r"))==NULL) {
203 +               char* err_mess = "The file: location.cfg does not exist or could not be read";  
204 +               printf("<p>");
205 +               printf("<font color=\"red\">%s</font>\r\n",err_mess);
206 +               printf("</p>");
207 +               return;
208 +       }
209 +
210 +       while(fgets(buf, sizeof(buf), fp)!=NULL) {
211 +               if ( (state == 1) && (buf[0] == '}')) {
212 +                       // we have reached a definition end
213 +                       state = 0;
214 +                       counter++;
215 +                       if (loc_list) {
216 +                               loc->next = loc_list;
217 +                               loc_list = loc;
218 +                       } else {
219 +                               loc_list = loc;
220 +                       }
221 +                       loc = NULL;
222 +                       continue;
223 +               }       
224 +               if ( (state==2) && (buf[0] == '}')) {
225 +                       state=0;
226 +               }
227 +               key[0] = value[0] = 0;
228 +               if (isspace(buf[0]))
229 +                       rc = sscanf(buf, "%*[ \t]%[^ \t]%*[ \t]%[^\n]", key, value);
230 +               else
231 +                       rc = sscanf(buf, "%[^ \t]%*[ \t]%[^\n]", key, value);
232 +
233 +               if ( (rc == 2)) {
234 +                       if ( (state == 0) &&  (strcmp(key,"define")==0) ) {
235 +                               // we have a start of a new definition
236 +
237 +                               // check if it is the default definition
238 +                               if ( (strncmp(value,"default ",8)==0) ||
239 +                                               (strncmp(value,"default{",8)==0)) {
240 +                                       state=2;
241 +                                       continue;
242 +                               }
243 +                               state = 1;
244 +                               // declare a new location
245 +                               loc = calloc(1,sizeof(*loc));
246 +                               continue;
247 +                       }
248 +                       // state 2 == default definition
249 +                       if ( state == 2 ) {
250 +                               if ( strcmp(key,"key") == 0) {
251 +                                       gen_loc.key=strdup(value);
252 +                               }
253 +                               if ( strcmp(key,"zoom") == 0) {
254 +                                       gen_loc.zoom=strdup(value);
255 +                               }
256 +                               if ( strcmp(key,"lat") == 0) {
257 +                                       gen_loc.latitude=strdup(value);
258 +                               }
259 +                               if ( strcmp(key,"long") == 0) {
260 +                                       gen_loc.longitude=strdup(value);
261 +                               }
262 +                       }
263 +                       // state 1 == location definition
264 +                       if ( state == 1 ) {
265 +                               if ( strcmp(key,"host_name") == 0) {
266 +                                       loc->name=strdup(value);
267 +                               }
268 +                               if ( strcmp(key,"notes") == 0) {
269 +                                       loc->note = strdup(value);
270 +                               }
271 +                               if ( strcmp(key,"lat") == 0) {
272 +                                       loc->latitude = strdup(value);
273 +                               }
274 +                               if ( strcmp(key,"long") == 0) {
275 +                                       loc->longitude = strdup(value);
276 +                               }
277 +                       }
278 +               }
279 +       }
280 +       fclose(fp);
281 +
282 +       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);
283 +
284 +       printf("<script type='text/javascript'>\n"
285 +       "//<![CDATA[ \n");
286 +
287 +       // printf("<script type=javascript src="webbserverfil" />);
288 +
289 +       printf("function setMapCookie(map) {"
290 +               "document.cookie = 'googlemaplng='+map.getCenter().lng();\n"
291 +               "document.cookie = 'googlemaplat='+map.getCenter().lat();\n"
292 +               "var zoom = map.getZoom();\n"
293 +               "document.cookie = 'googlemapzoom='+zoom;\n"
294 +               "document.layoutform.scaling_factor.value=+zoom;\n"
295 +       "}\n"
296 +       
297 +       "function readCookie(name) {\n"
298 +               "var nameEQ = name + \"=\";\n"
299 +               "var ca = document.cookie.split(';');\n"
300 +               "for(var i=0;i < ca.length;i++)\n"
301 +               "{\n"
302 +                       "var c = ca[i];\n"
303 +                       "while (c.charAt(0)==' ') c = c.substring(1,c.length);\n"
304 +                       "if (c.indexOf(nameEQ) == 0)\n"
305 +                       "{\n"
306 +                               "c=c.substring(nameEQ.length,c.length);\n"
307 +                               "while (c.substring(c.length-1, c.length) == ' ')\n"
308 +                                       "c = c.substring(0,c.length-1);\n"
309 +                               "return c;\n"
310 +                       "}\n"
311 +               "}\n"
312 +               "return null;\n"
313 +       "}\n"
314 +
315 +       "function createMarker(hostname, comment, lat, long, state) {\n"
316 +       " var point = new GLatLng(lat,long);\n"
317 +       "var marker;\n"
318 +       "if ( state==\"Up\" ) {\n"
319 +               "var baseIcon = new GIcon();\n"
320 +               "baseIcon.shadow = \"../images/shadow.png\";\n"
321 +               "baseIcon.iconSize = new GSize(20, 34);\n"
322 +               "baseIcon.shadowSize = new GSize(37, 34);\n"
323 +               "baseIcon.iconAnchor = new GPoint(9, 34);\n"
324 +               "baseIcon.infoWindowAnchor = new GPoint(9, 2);\n"
325 +               "baseIcon.infoShadowAnchor = new GPoint(18, 25);\n"
326 +               "var okicon = new GIcon(baseIcon);\n"
327 +               "okicon.image = \"../images/green.png\";\n"
328 +               "marker = new GMarker(point,okicon);\n"
329 +       "} else {\n"
330 +               "marker = new GMarker(point);\n"
331 +       "}\n"
332 +               
333 +       "GEvent.addListener(marker, \"click\", function() { \n"
334 +                                                "marker.openInfoWindowHtml(\"<b>\"+hostname+\"</b><br>\"+comment+\"<br>State: \"+state+\"\");\n"
335 +       "});\n"
336 +       "return marker;\n"
337 +       "}\n"
338 +
339 +       "function load() {\n"
340 +       "if (GBrowserIsCompatible()) {\n"
341 +               "var map = new GMap2(document.getElementById(\"map\"));\n"
342 +               "map.addControl(new GSmallMapControl());\n"
343 +               "map.addControl(new GMapTypeControl());\n"
344 +               
345 +               "GEvent.addListener(map, \"click\", function () {\n"
346 +                       "setMapCookie(map);\n"
347 +               "});\n"
348 +
349 +               "GEvent.addListener(map, \"move\", function () {\n"
350 +                       "setMapCookie(map);\n"
351 +               "});\n"
352 +
353 +               "GEvent.addListener(map, \"zoom\", function () {\n"
354 +                       "setMapCookie(map);\n"
355 +               "});\n"
356 +
357 +               "var googlemaplat = readCookie('googlemaplat');\n"
358 +        "var googlemaplng = readCookie('googlemaplng');\n"
359 +
360 +               "var centerPoint;\n"
361 +               "if(  googlemaplng!=null && googlemaplat!=null ) \n"
362 +                       "centerPoint = new GLatLng(googlemaplat,googlemaplng);\n"
363 +               "else\n");
364 +                       printf("centerPoint = new GLatLng(%s,%s);\n",gen_loc.latitude,gen_loc.longitude);
365 +
366 +               printf("var zoom = map.getZoom();\n"
367 +               "document.layoutform.scaling_factor.value=+zoom;\n");
368 +
369 +
370 +       if (user_supplied_scaling==TRUE)
371 +       {
372 +               printf("map.setCenter(centerPoint, %2.1f);\n",user_scaling_factor);
373 +               printf("document.layoutform.scaling_factor.value=%2.1f;\n",user_scaling_factor);
374 +       } else {
375 +               printf("var googlemapzoom = readCookie('googlemapzoom');\n"
376 +               "if ( googlemapzoom != null ) \n"
377 +               "{\n"
378 +                       "map.setCenter(centerPoint,parseInt(googlemapzoom));\n"
379 +                       "document.layoutform.scaling_factor.value=googlemapzoom;\n"
380 +               "} else\n"
381 +               "{\n");
382 +               printf("map.setCenter(centerPoint, %s);\n",gen_loc.zoom);
383 +                       printf("document.layoutform.scaling_factor.value=%s;\n",gen_loc.zoom);
384 +               printf("}\n");
385 +               user_scaling_factor = atof(gen_loc.zoom);
386 +       }
387 +                       
388 +       loc = loc_list;
389 +       while (loc) {
390 +               // check if allowed to show hostname first
391 +               host* temp_host=find_host(loc->name);
392 +               int in_layer_list=FALSE;
393 +               in_layer_list=is_host_in_layer_list(temp_host);
394 +
395 +               if ( !is_authorized_for_host(temp_host,&current_authdata) && !is_host_in_layer_list(temp_host)) {
396 +                       loc = loc->next;        
397 +                       continue;
398 +               } else {
399 +                       if((in_layer_list==TRUE && exclude_layers==TRUE) || (in_layer_list==FALSE && exclude_layers==FALSE))
400 +                       {
401 +                               loc = loc->next;        
402 +                               continue;
403 +                       }
404 +                       ptr = get_status_code(loc->name);       
405 +                       strcpy(stateinfo,ptr);
406 +                       free(ptr);
407 +
408 +       
409 +                       printf("map.addOverlay(createMarker('%s','",loc->name);
410 +                       ptr = loc->note;
411 +                       while ((ch = *ptr++)) {
412 +                               if (ch == '\'') printf("\\'");
413 +                               else printf("%c", ch);
414 +                       }
415 +                       printf("',%s,%s,'%s'))\n",loc->latitude,loc->longitude,stateinfo);
416 +
417 +                       loc = loc->next;
418 +               }
419 +       }
420 +                       
421 +       printf("}\n"
422 +       "}\n"
423 +       "//]]>\n"
424 +       "</script>\n");
425 +}
426  
427  /* write JavaScript code an layer for popup window */
428  void write_popup_code(void){
This page took 0.118765 seconds and 3 git commands to generate.