]> git.pld-linux.org Git - packages/nagios.git/blob - nagios-googlemap.patch
5ef45c291f78997eb76c94e63367a699b94d1f55
[packages/nagios.git] / nagios-googlemap.patch
1 --- nagios-4.2.1/cgi/statusmap.c~       2016-09-07 19:23:29.000000000 +0300
2 +++ nagios-4.2.1/cgi/statusmap.c        2016-09-07 19:24:13.969747452 +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 @@ -92,6 +92,7 @@ extern int default_statusmap_layout_meth
15  #define LAYOUT_CIRCULAR                 4
16  #define LAYOUT_CIRCULAR_MARKUP          5
17  #define LAYOUT_CIRCULAR_BALLOON         6
18 +#define LAYOUT_GOOGLEMAP                7
19  
20  
21  struct layer {
22 @@ -99,6 +100,20 @@ struct layer {
23         struct layer *next;
24  };
25  
26 +struct location_t {
27 +       char* name;
28 +       char* note;
29 +       char* latitude;
30 +       char* longitude;
31 +       struct location_t* next;
32 +};
33 +
34 +struct general_location_t {
35 +       char* key;
36 +       char* zoom;
37 +       char* latitude;
38 +       char* longitude;
39 +};
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 @@ -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 @@
70  
71                 /* write JavaScript code for popup window */
72                 write_popup_code();
73 +               if (layout_method == LAYOUT_GOOGLEMAP)
74 +                       write_google_head_code();
75  
76                 printf("</head>\n");
77  
78 +               if (layout_method == LAYOUT_GOOGLEMAP) {
79 +                       printf("<body onload='load()' onunload='GUnload()' CLASS='statusmap' name='mappage' id='mappage'>\n");
80 +               } else 
81                 printf("<body CLASS='statusmap' name='mappage' id='mappage'>\n");
82  
83                 /* include user SSI header */
84 @@ -745,9 +745,9 @@
85                 printf("<td align=right valign=top>\n");
86  
87  #ifdef LEGACY_GRAPHICAL_CGIS
88 -               printf("<form method=\"POST\" action=\"%s\">\n", STATUSMAP_CGI);
89 +               printf("<form name=\"layoutform\" method=\"POST\" action=\"%s\">\n", STATUSMAP_CGI);
90  #else
91 -               printf("<form method=\"POST\" action=\"%s\">\n", LEGACY_STATUSMAP_CGI);
92 +               printf("<form name=\"layoutform\" method=\"POST\" action=\"%s\">\n", LEGACY_STATUSMAP_CGI);
93  #endif
94                 printf("<table border=0 CLASS='optBox'>\n");
95                 printf("<tr><td valign=top>\n");
96 @@ -725,6 +751,7 @@
97                 printf("<option value=%d %s>Circular\n", LAYOUT_CIRCULAR, (layout_method == LAYOUT_CIRCULAR) ? "selected" : "");
98                 printf("<option value=%d %s>Circular (Marked Up)\n", LAYOUT_CIRCULAR_MARKUP, (layout_method == LAYOUT_CIRCULAR_MARKUP) ? "selected" : "");
99                 printf("<option value=%d %s>Circular (Balloon)\n", LAYOUT_CIRCULAR_BALLOON, (layout_method == LAYOUT_CIRCULAR_BALLOON) ? "selected" : "");
100 +               printf("<option value=%d %s>Googlemap\n", LAYOUT_GOOGLEMAP, (layout_method == LAYOUT_GOOGLEMAP) ? "selected" : "");
101                 printf("</select>\n");
102                 printf("</td>\n");
103                 printf("<td CLASS='optBoxItem'>\n");
104 @@ -821,6 +848,11 @@
105         /* display page header */
106         display_page_header();
107  
108 +       if (layout_method==LAYOUT_GOOGLEMAP){
109 +               google_map();
110 +               return;
111 +       }
112 +
113         initialize_graphics();
114         draw_background_image();
115         draw_background_extras();
116 @@ -1563,10 +1602,19 @@
117         return;
118         }
119  
120 +/* create the googlemap */
121 +void google_map(void){
122 +       printf("<!-- Produced by Infoflex Connect AB (http://www.infoflexconnect.se), Fredrik Nilsson and Rujinschi Remus. -->\n");
123 +       printf("<P><DIV ALIGN=center>");
124 +       printf("<div id='map' style='width: 800px; height: 500px'></div>");
125 +       printf("</DIV></P>");
126 +       printf("<!-- Produced by Infoflex Connect AB (http://www.infoflexconnect.se), Fredrik Nilsson and Rujinschi Remus. -->\n");
127  
128 +}
129  
130  /* draws hosts */
131  void draw_hosts(void) {
132 +       
133         host *temp_host;
134         int x1, x2;
135         int y1, y2;
136 @@ -1586,6 +1634,8 @@
137         int translated_x;
138         int translated_y;
139  
140 +       if (layout_method == LAYOUT_GOOGLEMAP)
141 +               return;
142  
143         /* user didn't supply any coordinates for hosts, so display a warning */
144         if(coordinates_were_specified == FALSE) {
145 @@ -2241,6 +2291,197 @@
146  /************************* MISC FUNCTIONS *************************/
147  /******************************************************************/
148  
149 +char* get_status_code(char* name)
150 +{
151 +       char* retVal;
152 +       hoststatus *temp_status;
153 +       temp_status=find_hoststatus(name);
154 +
155 +       /* strip nasty stuff from plugin output */
156 +       sanitize_plugin_output(temp_status->plugin_output);
157 +       
158 +       retVal=malloc(1000);
159 +       retVal[0]=0;
160 +       if(temp_status->status==HOST_DOWN){
161 +               strcat(retVal,"Down");
162 +               if(temp_status->problem_has_been_acknowledged==TRUE)
163 +                       strcat(retVal," (Acknowledged)");
164 +               strcat(retVal, "");
165 +               }
166 +
167 +       else if(temp_status->status==HOST_UNREACHABLE){
168 +               strcat(retVal, "Unreachable");
169 +               if(temp_status->problem_has_been_acknowledged==TRUE)
170 +                       strcat(retVal, " (Acknowledged)");
171 +               strcat(retVal, "");
172 +               }
173 +
174 +       else if(temp_status->status==HOST_UP)
175 +               strcat(retVal, "Up");
176 +
177 +       else if(temp_status->status==HOST_PENDING)
178 +               strcat(retVal, "Pending");
179 +       return retVal;
180 +}
181 +
182 +void write_google_head_code(void)
183 +{
184 +       char ch;
185 +       char* ptr;
186 +       int rc;
187 +       FILE *fp;
188 +       char stateinfo[1024];   
189 +       char line[1024];        
190 +       char key[1024]; 
191 +       char value[1024];       
192 +       char buf[256];
193 +       char* filename;
194 +       int state = 0;
195 +       int counter = 0;
196 +       struct general_location_t gen_loc;
197 +       struct location_t* loc;
198 +       struct location_t* loc_list=NULL;
199 +
200 +
201 +/***************************************
202 + * parse location.cfg
203 + **************************************/
204 +
205 +       filename = "/etc/webapps/nagios/location.cfg";
206 +       if((fp=fopen(filename, "r"))==NULL) {
207 +               char* err_mess = "The file: location.cfg does not exist or could not be read";  
208 +               printf("<p>");
209 +               printf("<font color=\"red\">%s</font>\r\n",err_mess);
210 +               printf("</p>");
211 +               return;
212 +       }
213 +
214 +       while(fgets(buf, sizeof(buf), fp)!=NULL) {
215 +               if ( (state == 1) && (buf[0] == '}')) {
216 +                       // we have reached a definition end
217 +                       state = 0;
218 +                       counter++;
219 +                       if (loc_list) {
220 +                               loc->next = loc_list;
221 +                               loc_list = loc;
222 +                       } else {
223 +                               loc_list = loc;
224 +                       }
225 +                       loc = NULL;
226 +                       continue;
227 +               }       
228 +               if ( (state==2) && (buf[0] == '}')) {
229 +                       state=0;
230 +               }
231 +               key[0] = value[0] = 0;
232 +               if (isspace(buf[0]))
233 +                       rc = sscanf(buf, "%*[ \t]%[^ \t]%*[ \t]%[^\n]", key, value);
234 +               else
235 +                       rc = sscanf(buf, "%[^ \t]%*[ \t]%[^\n]", key, value);
236 +
237 +               if ( (rc == 2)) {
238 +                       if ( (state == 0) &&  (strcmp(key,"define")==0) ) {
239 +                               // we have a start of a new definition
240 +
241 +                               // check if it is the default definition
242 +                               if ( (strncmp(value,"default ",8)==0) ||
243 +                                               (strncmp(value,"default{",8)==0)) {
244 +                                       state=2;
245 +                                       continue;
246 +                               }
247 +                               state = 1;
248 +                               // declare a new location
249 +                               loc = calloc(1,sizeof(*loc));
250 +                               continue;
251 +                       }
252 +                       // state 2 == default definition
253 +                       if ( state == 2 ) {
254 +                               if ( strcmp(key,"key") == 0) {
255 +                                       gen_loc.key=strdup(value);
256 +                               }
257 +                               if ( strcmp(key,"zoom") == 0) {
258 +                                       gen_loc.zoom=strdup(value);
259 +                               }
260 +                               if ( strcmp(key,"lat") == 0) {
261 +                                       gen_loc.latitude=strdup(value);
262 +                               }
263 +                               if ( strcmp(key,"long") == 0) {
264 +                                       gen_loc.longitude=strdup(value);
265 +                               }
266 +                       }
267 +                       // state 1 == location definition
268 +                       if ( state == 1 ) {
269 +                               if ( strcmp(key,"host_name") == 0) {
270 +                                       loc->name=strdup(value);
271 +                               }
272 +                               if ( strcmp(key,"notes") == 0) {
273 +                                       loc->note = strdup(value);
274 +                               }
275 +                               if ( strcmp(key,"lat") == 0) {
276 +                                       loc->latitude = strdup(value);
277 +                               }
278 +                               if ( strcmp(key,"long") == 0) {
279 +                                       loc->longitude = strdup(value);
280 +                               }
281 +                       }
282 +               }
283 +       }
284 +       fclose(fp);
285 +
286 +
287 +       printf("<script type='text/javascript'>\n");
288 +       printf("gstatusmap = {\n");
289 +       printf("lat:%s,\n", gen_loc.latitude);
290 +       printf("lng:%s,\n", gen_loc.longitude);
291 +       if (user_supplied_scaling==TRUE) {
292 +               printf("scale: '%2.1f',\n", user_scaling_factor);
293 +       } else {
294 +               printf("scale: '%2.1f',\n", gen_loc.zoom);
295 +               user_scaling_factor = atof(gen_loc.zoom);
296 +       }
297 +       printf("markers: [\n");
298 +
299 +       loc = loc_list;
300 +       while (loc) {
301 +               // check if allowed to show hostname first
302 +               host* temp_host=find_host(loc->name);
303 +               int in_layer_list=FALSE;
304 +               in_layer_list=is_host_in_layer_list(temp_host);
305 +
306 +               if ( !is_authorized_for_host(temp_host,&current_authdata) && !is_host_in_layer_list(temp_host)) {
307 +                       loc = loc->next;        
308 +                       continue;
309 +               } else {
310 +                       if((in_layer_list==TRUE && exclude_layers==TRUE) || (in_layer_list==FALSE && exclude_layers==FALSE))
311 +                       {
312 +                               loc = loc->next;        
313 +                               continue;
314 +                       }
315 +                       ptr = get_status_code(loc->name);       
316 +                       strcpy(stateinfo,ptr);
317 +                       free(ptr);
318 +
319 +       
320 +                       printf("['%s','",loc->name);
321 +                       ptr = loc->note;
322 +                       while ((ch = *ptr++)) {
323 +                               if (ch == '\'') printf("\\'");
324 +                               else printf("%c", ch);
325 +                       }
326 +                       printf("',%s,%s,'%s'],\n",loc->latitude,loc->longitude,stateinfo);
327 +
328 +                       loc = loc->next;
329 +               }
330 +       }
331 +
332 +       printf("0], \n");
333 +       printf("images_url: '%s'\n", url_images_path);
334 +       printf("};\n");
335 +       printf("</script>\n");
336 +
337 +       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);
338 +       printf("<script src='%sgooglemap.js' type='text/javascript'></script>\n", url_images_path);
339 +}
340  
341  /* write JavaScript code an layer for popup window */
342  void write_popup_code(void){
This page took 0.072736 seconds and 2 git commands to generate.