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