show_instance action: Switch to JavaScript-based drawing by default.
[collection4.git] / src / action_show_instance.c
1 /**
2  * collection4 - action_show_instance.c
3  * Copyright (C) 2010  Florian octo Forster
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  * 
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA  02110-1301  USA
19  *
20  * Authors:
21  *   Florian octo Forster <ff at octo.it>
22  **/
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <stdint.h>
29 #include <inttypes.h>
30 #include <assert.h>
31
32 #include "action_show_instance.h"
33 #include "common.h"
34 #include "graph.h"
35 #include "graph_ident.h"
36 #include "graph_instance.h"
37 #include "graph_list.h"
38 #include "utils_cgi.h"
39
40 #include <fcgiapp.h>
41 #include <fcgi_stdio.h>
42
43 #define OUTPUT_ERROR(...) do {             \
44   printf ("Content-Type: text/plain\n\n"); \
45   printf (__VA_ARGS__);                    \
46   return (0);                              \
47 } while (0)
48
49 #define MAX_SHOW_GRAPHS 10
50
51 #define SGD_FORMAT_JSON 0
52 #define SGD_FORMAT_RRD  1
53
54 struct show_graph_data_s
55 {
56   graph_config_t *cfg;
57   graph_instance_t *inst;
58   int graph_count;
59   int format;
60 };
61 typedef struct show_graph_data_s show_graph_data_t;
62
63 static void show_breadcrump_field (const char *str, /* {{{ */
64     const char *field_name)
65 {
66   if ((str == NULL) || (str[0] == 0))
67     printf ("<em>none</em>");
68   else if (IS_ANY (str))
69     printf ("<em>any</em>");
70   else if (IS_ALL (str))
71     printf ("<em>all</em>");
72   else
73   {
74     char *str_html = html_escape (str);
75
76     if (field_name != NULL)
77       printf ("<a href=\"%s?action=search;q=%s:%s\">%s</a>",
78           script_name (), field_name, str_html, str_html);
79     else
80       printf ("<a href=\"%s?action=search;q=%s\">%s</a>",
81           script_name (), str_html, str_html);
82
83     free (str_html);
84   }
85 } /* }}} void show_breadcrump_field */
86
87 static int show_breadcrump (graph_config_t *cfg, /* {{{ */
88     graph_instance_t *inst)
89 {
90   graph_ident_t *ident;
91   char *prefix;
92
93   if (inst != NULL)
94   {
95     prefix = "Instance";
96     ident = inst_get_selector (inst);
97   }
98   else
99   {
100     prefix = "Graph";
101     ident = graph_get_selector (cfg);
102   }
103
104   printf ("<div class=\"breadcrump\">%s: &quot;", prefix);
105   show_breadcrump_field (ident_get_host (ident), "host");
106   printf ("&nbsp;/ ");
107   show_breadcrump_field (ident_get_plugin (ident), "plugin");
108   printf ("&nbsp;&ndash; ");
109   show_breadcrump_field (ident_get_plugin_instance (ident), "plugin_instance");
110   printf ("&nbsp;/ ");
111   show_breadcrump_field (ident_get_type (ident), "type");
112   printf ("&nbsp;&ndash; ");
113   show_breadcrump_field (ident_get_type_instance (ident), "type_instance");
114   printf ("&quot;</div>\n");
115
116   ident_destroy (ident);
117   return (0);
118 } /* }}} int show_breadcrump */
119
120 static int show_time_selector (__attribute__((unused)) void *user_data) /* {{{ */
121 {
122   param_list_t *pl;
123
124   pl = param_create (/* query string = */ NULL);
125   param_set (pl, "begin", NULL);
126   param_set (pl, "end", NULL);
127   param_set (pl, "button", NULL);
128
129   printf ("<form action=\"%s\" method=\"get\">\n", script_name ());
130
131   param_print_hidden (pl);
132
133   printf ("  <select name=\"begin\">\n"
134       "    <option value=\"-3600\">Hour</option>\n"
135       "    <option value=\"-86400\">Day</option>\n"
136       "    <option value=\"-604800\">Week</option>\n"
137       "    <option value=\"-2678400\">Month</option>\n"
138       "    <option value=\"-31622400\">Year</option>\n"
139       "  </select>\n"
140       "  <input type=\"submit\" name=\"button\" value=\"Go\" />\n");
141
142   printf ("</form>\n");
143
144   param_destroy (pl);
145
146   return (0);
147 } /* }}} int show_time_selector */
148
149 static int left_menu (void *user_data) /* {{{ */
150 {
151   show_graph_data_t *data = user_data;
152   char params[1024];
153   graph_instance_t *inst;
154   graph_ident_t *ident;
155   const char *host;
156
157   memset (params, 0, sizeof (params));
158   graph_get_params (data->cfg, params, sizeof (params));
159   html_escape_buffer (params, sizeof (params));
160
161   inst = inst_get_selected (data->cfg);
162   ident = inst_get_selector (inst);
163   host = ident_get_host (ident);
164   if (IS_ANY (host))
165     host = NULL;
166
167   printf ("\n<ul class=\"menu left\">\n"
168       "  <li><a href=\"%s?action=show_graph;%s\">All instances</a></li>\n"
169       "  <li><a href=\"%s?action=list_graphs\">All graphs</a></li>\n",
170       script_name (), params,
171       script_name ());
172   if (host != NULL)
173   {
174     char host_html[1024];
175     char host_uri[1024];
176
177     html_escape_copy (host_html, host, sizeof (host_html));
178     uri_escape_copy (host_uri, host, sizeof (host_uri));
179
180     printf ("  <li><a href=\"%s?action=search;q=host:%s\">Host &quot;%s&quot;</a></li>\n",
181         script_name (), host_uri, host_html);
182   }
183   printf ("</ul>\n");
184
185   host = NULL;
186   ident_destroy (ident);
187
188   return (0);
189 } /* }}} int left_menu */
190
191 static int show_instance_json (graph_config_t *cfg, /* {{{ */
192     graph_instance_t *inst,
193     long begin, long end, int index)
194 {
195   yajl_gen_config handler_config;
196   yajl_gen handler;
197   const unsigned char *json_buffer;
198   unsigned int json_buffer_length;
199
200   graph_ident_t *graph_selector;
201   graph_ident_t *inst_selector;
202
203   graph_selector = graph_get_selector (cfg);
204   if (graph_selector == NULL)
205     return (ENOMEM);
206
207   inst_selector = inst_get_selector (inst);
208   if (inst_selector == NULL)
209   {
210     ident_destroy (inst_selector);
211     return (ENOMEM);
212   }
213
214   memset (&handler_config, 0, sizeof (handler_config));
215   handler_config.beautify = 1;
216   handler_config.indentString = "  ";
217
218   handler = yajl_gen_alloc2 (/* callback = */ NULL,
219       &handler_config,
220       /* alloc functions = */ NULL,
221       /* context = */ NULL);
222   if (handler == NULL)
223   {
224     ident_destroy (inst_selector);
225     ident_destroy (graph_selector);
226     return (-1);
227   }
228
229   yajl_gen_map_open (handler);
230
231   yajl_gen_string (handler,
232       (unsigned char *) "graph_selector",
233       (unsigned int) strlen ("graph_selector"));
234   ident_to_json (graph_selector, handler);
235   ident_destroy (graph_selector);
236
237   yajl_gen_string (handler,
238       (unsigned char *) "instance_selector",
239       (unsigned int) strlen ("instance_selector"));
240   ident_to_json (inst_selector, handler);
241   ident_destroy (inst_selector);
242
243   yajl_gen_string (handler,
244       (unsigned char *) "begin",
245       (unsigned int) strlen ("begin"));
246   yajl_gen_integer (handler, begin);
247
248   yajl_gen_string (handler,
249       (unsigned char *) "end",
250       (unsigned int) strlen ("end"));
251   yajl_gen_integer (handler, end);
252
253   yajl_gen_map_close (handler);
254
255   json_buffer = NULL;
256   json_buffer_length = 0;
257   yajl_gen_get_buf (handler, &json_buffer, &json_buffer_length);
258
259   if (json_buffer == NULL)
260   {
261     yajl_gen_free (handler);
262     return (EINVAL);
263   }
264
265   printf ("<div id=\"c4-graph%i\" class=\"graph-json\"></div>\n", index);
266   printf ("<script type=\"text/javascript\">c4.instances[%i] = %s;</script>\n",
267       index, (const char *) json_buffer);
268
269   yajl_gen_free (handler);
270   return (0);
271 } /* }}} int show_instance_json */
272
273 static int show_instance_rrdtool (graph_config_t *cfg, /* {{{ */
274     graph_instance_t *inst,
275     long begin, long end, int index)
276 {
277   char title[128];
278   char descr[128];
279   char params[1024];
280   char time_params[128];
281
282   memset (title, 0, sizeof (title));
283   graph_get_title (cfg, title, sizeof (title));
284   html_escape_buffer (title, sizeof (title));
285
286   memset (descr, 0, sizeof (descr));
287   inst_describe (cfg, inst, descr, sizeof (descr));
288   html_escape_buffer (descr, sizeof (descr));
289
290   memset (params, 0, sizeof (params));
291   inst_get_params (cfg, inst, params, sizeof (params));
292   html_escape_buffer (params, sizeof (params));
293
294   snprintf (time_params, sizeof (time_params), ";begin=%li;end=%li",
295       begin, end);
296   time_params[sizeof (time_params) - 1] = 0;
297
298   if (index < MAX_SHOW_GRAPHS)
299     printf ("<div class=\"graph-img\"><img src=\"%s?action=graph;%s%s\" "
300         "title=\"%s / %s\" /></div>\n",
301         script_name (), params, time_params, title, descr);
302   else
303     printf ("<a href=\"%s?action=show_instance;%s\">Show graph "
304         "&quot;%s / %s&quot;</a>\n",
305         script_name (), params, title, descr);
306
307 #if 0
308   printf ("<div><a href=\"%s?action=graph_data_json;%s%s\">"
309       "Get graph data as JSON</a></div>\n",
310       script_name (), params, time_params);
311 #endif
312
313   return (0);
314 } /* }}} int show_instance_rrdtool */
315
316 static int show_instance_cb (graph_config_t *cfg, /* {{{ */
317     graph_instance_t *inst,
318     void *user_data)
319 {
320   show_graph_data_t *data = user_data;
321   char descr[128];
322
323   long begin;
324   long end;
325   int status;
326
327   memset (descr, 0, sizeof (descr));
328   inst_describe (cfg, inst, descr, sizeof (descr));
329   html_escape_buffer (descr, sizeof (descr));
330
331   begin = 0;
332   end = 0;
333
334   status = get_time_args (&begin, &end, /* now = */ NULL);
335   if (status != 0)
336     return (status);
337
338   printf ("<h2>Instance &quot;%s&quot;</h2>\n", descr);
339
340   show_breadcrump (cfg, inst);
341
342   if (data->format == SGD_FORMAT_RRD)
343     show_instance_rrdtool (cfg, inst, begin, end, data->graph_count);
344   else
345     show_instance_json (cfg, inst, begin, end, data->graph_count);
346
347   data->graph_count++;
348
349   return (0);
350 } /* }}} int show_instance_cb */
351
352 static int show_instance (void *user_data) /* {{{ */
353 {
354   show_graph_data_t *data = user_data;
355   /* char params[1024]; */
356   int status;
357
358   status = inst_get_all_selected (data->cfg,
359       /* callback = */ show_instance_cb, /* user data = */ data);
360   if (status != 0)
361     fprintf (stderr, "show_instance: inst_get_all_selected failed "
362         "with status %i\n", status);
363
364 #if 0
365   memset (params, 0, sizeof (params));
366   graph_get_params (data->cfg, params, sizeof (params));
367   html_escape_buffer (params, sizeof (params));
368
369   printf ("<div style=\"clear: both;\"><a href=\"%s?action=graph_def_json;%s\">"
370       "Get graph definition as JSON</a></div>\n",
371       script_name (), params);
372 #endif
373
374   return (0);
375 } /* }}} int show_instance */
376
377 int action_show_instance (void) /* {{{ */
378 {
379   page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT;
380   show_graph_data_t pg_data;
381
382   char tmp[128];
383   char title[128];
384   char *format;
385
386   memset (&pg_data, 0, sizeof (pg_data));
387   pg_data.cfg = gl_graph_get_selected ();
388   if (pg_data.cfg == NULL)
389     OUTPUT_ERROR ("gl_graph_get_selected () failed.\n");
390
391   memset (tmp, 0, sizeof (tmp));
392   graph_get_title (pg_data.cfg, tmp, sizeof (tmp));
393   snprintf (title, sizeof (title), "Graph \"%s\"", tmp);
394   title[sizeof (title) - 1] = 0;
395
396   format = param ("format");
397   if ((format != NULL) && (strcasecmp ("RRD", format) == 0))
398     pg_data.format = SGD_FORMAT_RRD;
399   else
400     pg_data.format = SGD_FORMAT_JSON;
401
402   pg_callbacks.top_right = html_print_search_box;
403   pg_callbacks.middle_center = show_instance;
404   pg_callbacks.middle_left = left_menu;
405   pg_callbacks.middle_right = show_time_selector;
406
407   html_print_page (title, &pg_callbacks, &pg_data);
408
409   return (0);
410 } /* }}} int action_show_instance */
411
412 /* vim: set sw=2 sts=2 et fdm=marker : */