show_instance action: Export graph and instance data in a more structured fashion.
[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 struct show_graph_data_s
52 {
53   graph_config_t *cfg;
54   graph_instance_t *inst;
55   int graph_count;
56 };
57 typedef struct show_graph_data_s show_graph_data_t;
58
59 static void show_breadcrump_field (const char *str, /* {{{ */
60     const char *field_name)
61 {
62   if ((str == NULL) || (str[0] == 0))
63     printf ("<em>none</em>");
64   else if (IS_ANY (str))
65     printf ("<em>any</em>");
66   else if (IS_ALL (str))
67     printf ("<em>all</em>");
68   else
69   {
70     char *str_html = html_escape (str);
71
72     if (field_name != NULL)
73       printf ("<a href=\"%s?action=search;q=%s:%s\">%s</a>",
74           script_name (), field_name, str_html, str_html);
75     else
76       printf ("<a href=\"%s?action=search;q=%s\">%s</a>",
77           script_name (), str_html, str_html);
78
79     free (str_html);
80   }
81 } /* }}} void show_breadcrump_field */
82
83 static int show_breadcrump (graph_config_t *cfg, /* {{{ */
84     graph_instance_t *inst)
85 {
86   graph_ident_t *ident;
87   char *prefix;
88
89   if (inst != NULL)
90   {
91     prefix = "Instance";
92     ident = inst_get_selector (inst);
93   }
94   else
95   {
96     prefix = "Graph";
97     ident = graph_get_selector (cfg);
98   }
99
100   printf ("<div class=\"breadcrump\">%s: &quot;", prefix);
101   show_breadcrump_field (ident_get_host (ident), "host");
102   printf ("&nbsp;/ ");
103   show_breadcrump_field (ident_get_plugin (ident), "plugin");
104   printf ("&nbsp;&ndash; ");
105   show_breadcrump_field (ident_get_plugin_instance (ident), "plugin_instance");
106   printf ("&nbsp;/ ");
107   show_breadcrump_field (ident_get_type (ident), "type");
108   printf ("&nbsp;&ndash; ");
109   show_breadcrump_field (ident_get_type_instance (ident), "type_instance");
110   printf ("&quot;</div>\n");
111
112   ident_destroy (ident);
113   return (0);
114 } /* }}} int show_breadcrump */
115
116 static int show_time_selector (__attribute__((unused)) void *user_data) /* {{{ */
117 {
118   param_list_t *pl;
119
120   pl = param_create (/* query string = */ NULL);
121   param_set (pl, "begin", NULL);
122   param_set (pl, "end", NULL);
123   param_set (pl, "button", NULL);
124
125   printf ("<form action=\"%s\" method=\"get\">\n", script_name ());
126
127   param_print_hidden (pl);
128
129   printf ("  <select name=\"begin\">\n"
130       "    <option value=\"-3600\">Hour</option>\n"
131       "    <option value=\"-86400\">Day</option>\n"
132       "    <option value=\"-604800\">Week</option>\n"
133       "    <option value=\"-2678400\">Month</option>\n"
134       "    <option value=\"-31622400\">Year</option>\n"
135       "  </select>\n"
136       "  <input type=\"submit\" name=\"button\" value=\"Go\" />\n");
137
138   printf ("</form>\n");
139
140   param_destroy (pl);
141
142   return (0);
143 } /* }}} int show_time_selector */
144
145 static int left_menu (void *user_data) /* {{{ */
146 {
147   show_graph_data_t *data = user_data;
148   char params[1024];
149   graph_instance_t *inst;
150   graph_ident_t *ident;
151   const char *host;
152
153   memset (params, 0, sizeof (params));
154   graph_get_params (data->cfg, params, sizeof (params));
155   html_escape_buffer (params, sizeof (params));
156
157   inst = inst_get_selected (data->cfg);
158   ident = inst_get_selector (inst);
159   host = ident_get_host (ident);
160   if (IS_ANY (host))
161     host = NULL;
162
163   printf ("\n<ul class=\"menu left\">\n"
164       "  <li><a href=\"%s?action=show_graph;%s\">All instances</a></li>\n"
165       "  <li><a href=\"%s?action=list_graphs\">All graphs</a></li>\n",
166       script_name (), params,
167       script_name ());
168   if (host != NULL)
169   {
170     char host_html[1024];
171     char host_uri[1024];
172
173     html_escape_copy (host_html, host, sizeof (host_html));
174     uri_escape_copy (host_uri, host, sizeof (host_uri));
175
176     printf ("  <li><a href=\"%s?action=search;q=host:%s\">Host &quot;%s&quot;</a></li>\n",
177         script_name (), host_uri, host_html);
178   }
179   printf ("</ul>\n");
180
181   host = NULL;
182   ident_destroy (ident);
183
184   return (0);
185 } /* }}} int left_menu */
186
187 static int show_instance_json (graph_config_t *cfg, /* {{{ */
188     graph_instance_t *inst,
189     time_t begin, time_t end, int index)
190 {
191   yajl_gen_config handler_config;
192   yajl_gen handler;
193   const unsigned char *json_buffer;
194   unsigned int json_buffer_length;
195
196   graph_ident_t *graph_selector;
197   graph_ident_t *inst_selector;
198
199   graph_selector = graph_get_selector (cfg);
200   if (graph_selector == NULL)
201     return (ENOMEM);
202
203   inst_selector = inst_get_selector (inst);
204   if (inst_selector == NULL)
205   {
206     ident_destroy (inst_selector);
207     return (ENOMEM);
208   }
209
210   memset (&handler_config, 0, sizeof (handler_config));
211   handler_config.beautify = 1;
212   handler_config.indentString = "  ";
213
214   handler = yajl_gen_alloc2 (/* callback = */ NULL,
215       &handler_config,
216       /* alloc functions = */ NULL,
217       /* context = */ NULL);
218   if (handler == NULL)
219   {
220     ident_destroy (inst_selector);
221     ident_destroy (graph_selector);
222     return (-1);
223   }
224
225   yajl_gen_map_open (handler);
226
227   yajl_gen_string (handler,
228       (unsigned char *) "graph_selector",
229       (unsigned int) strlen ("graph_selector"));
230   ident_to_json (graph_selector, handler);
231   ident_destroy (graph_selector);
232
233   yajl_gen_string (handler,
234       (unsigned char *) "instance_selector",
235       (unsigned int) strlen ("instance_selector"));
236   ident_to_json (inst_selector, handler);
237   ident_destroy (inst_selector);
238
239   yajl_gen_string (handler,
240       (unsigned char *) "begin",
241       (unsigned int) strlen ("begin"));
242   yajl_gen_integer (handler, (long int) begin);
243
244   yajl_gen_string (handler,
245       (unsigned char *) "end",
246       (unsigned int) strlen ("end"));
247   yajl_gen_integer (handler, (long int) end);
248
249   yajl_gen_map_close (handler);
250
251   json_buffer = NULL;
252   json_buffer_length = 0;
253   yajl_gen_get_buf (handler, &json_buffer, &json_buffer_length);
254
255   if (json_buffer == NULL)
256   {
257     yajl_gen_free (handler);
258     return (EINVAL);
259   }
260
261   printf ("<div id=\"c4-graph%i\" class=\"graph-json\"></div>\n", index);
262   printf ("<script type=\"text/javascript\">c4.instances[%i] = %s;</script>\n",
263       index, (const char *) json_buffer);
264
265   yajl_gen_free (handler);
266   return (0);
267 } /* }}} int show_instance_json */
268
269 static int show_instance_cb (graph_config_t *cfg, /* {{{ */
270     graph_instance_t *inst,
271     void *user_data)
272 {
273   show_graph_data_t *data = user_data;
274   char title[128];
275   char descr[128];
276   char params[1024];
277
278   long begin;
279   long end;
280   char time_params[128];
281   int status;
282
283   memset (title, 0, sizeof (title));
284   graph_get_title (cfg, title, sizeof (title));
285   html_escape_buffer (title, sizeof (title));
286
287   memset (descr, 0, sizeof (descr));
288   inst_describe (cfg, inst, descr, sizeof (descr));
289   html_escape_buffer (descr, sizeof (descr));
290
291   memset (params, 0, sizeof (params));
292   inst_get_params (cfg, inst, params, sizeof (params));
293   html_escape_buffer (params, sizeof (params));
294
295   time_params[0] = 0;
296   begin = 0;
297   end = 0;
298
299   status = get_time_args (&begin, &end, /* now = */ NULL);
300   if (status == 0)
301   {
302     snprintf (time_params, sizeof (time_params), ";begin=%li;end=%li",
303         begin, end);
304     time_params[sizeof (time_params) - 1] = 0;
305   }
306
307   printf ("<h2>Instance &quot;%s&quot;</h2>\n", descr);
308
309   show_breadcrump (cfg, inst);
310
311   if (data->graph_count < MAX_SHOW_GRAPHS)
312     printf ("<div class=\"graph-img\"><img src=\"%s?action=graph;%s%s\" "
313         "title=\"%s / %s\" /></div>\n",
314         script_name (), params, time_params, title, descr);
315   else
316     printf ("<a href=\"%s?action=show_instance;%s\">Show graph "
317         "&quot;%s / %s&quot;</a>\n",
318         script_name (), params, title, descr);
319
320   show_instance_json (cfg, inst, begin, end, data->graph_count);
321
322   printf ("<div style=\"clear: both;\"><a href=\"%s?action=graph_data_json;%s%s\">"
323       "Get graph data as JSON</a></div>\n",
324       script_name (), params, time_params);
325
326   data->graph_count++;
327
328   return (0);
329 } /* }}} int show_instance_cb */
330
331 static int show_instance (void *user_data) /* {{{ */
332 {
333   show_graph_data_t *data = user_data;
334   char params[1024];
335   int status;
336
337   status = inst_get_all_selected (data->cfg,
338       /* callback = */ show_instance_cb, /* user data = */ data);
339   if (status != 0)
340     fprintf (stderr, "show_instance: inst_get_all_selected failed "
341         "with status %i\n", status);
342
343   memset (params, 0, sizeof (params));
344   graph_get_params (data->cfg, params, sizeof (params));
345   html_escape_buffer (params, sizeof (params));
346
347   printf ("<div style=\"clear: both;\"><a href=\"%s?action=graph_def_json;%s\">"
348       "Get graph definition as JSON</a></div>\n",
349       script_name (), params);
350
351   return (0);
352 } /* }}} int show_instance */
353
354 int action_show_instance (void) /* {{{ */
355 {
356   page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT;
357   show_graph_data_t pg_data;
358
359   char tmp[128];
360   char title[128];
361
362   memset (&pg_data, 0, sizeof (pg_data));
363   pg_data.cfg = gl_graph_get_selected ();
364   if (pg_data.cfg == NULL)
365     OUTPUT_ERROR ("gl_graph_get_selected () failed.\n");
366
367   memset (tmp, 0, sizeof (tmp));
368   graph_get_title (pg_data.cfg, tmp, sizeof (tmp));
369   snprintf (title, sizeof (title), "Graph \"%s\"", tmp);
370   title[sizeof (title) - 1] = 0;
371
372   pg_callbacks.top_right = html_print_search_box;
373   pg_callbacks.middle_center = show_instance;
374   pg_callbacks.middle_left = left_menu;
375   pg_callbacks.middle_right = show_time_selector;
376
377   html_print_page (title, &pg_callbacks, &pg_data);
378
379   return (0);
380 } /* }}} int action_show_instance */
381
382 /* vim: set sw=2 sts=2 et fdm=marker : */