2 * collection4 - action_show_instance.c
3 * Copyright (C) 2010 Florian octo Forster
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.
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.
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
21 * Florian octo Forster <ff at octo.it>
32 #include "action_show_instance.h"
35 #include "graph_ident.h"
36 #include "graph_instance.h"
37 #include "graph_list.h"
38 #include "utils_cgi.h"
41 #include <fcgi_stdio.h>
43 #define OUTPUT_ERROR(...) do { \
44 printf ("Content-Type: text/plain\n\n"); \
45 printf (__VA_ARGS__); \
49 #define MAX_SHOW_GRAPHS 10
51 #define SGD_FORMAT_JSON 0
52 #define SGD_FORMAT_RRD 1
54 struct show_graph_data_s
57 graph_instance_t *inst;
61 typedef struct show_graph_data_s show_graph_data_t;
63 static void show_breadcrump_field (const char *str, /* {{{ */
64 const char *field_name)
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>");
74 char *str_html = html_escape (str);
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);
80 printf ("<a href=\"%s?action=search;q=%s\">%s</a>",
81 script_name (), str_html, str_html);
85 } /* }}} void show_breadcrump_field */
87 static int show_breadcrump (graph_config_t *cfg, /* {{{ */
88 graph_instance_t *inst)
96 ident = inst_get_selector (inst);
101 ident = graph_get_selector (cfg);
104 printf ("<div class=\"breadcrump\">%s: "", prefix);
105 show_breadcrump_field (ident_get_host (ident), "host");
107 show_breadcrump_field (ident_get_plugin (ident), "plugin");
108 printf (" – ");
109 show_breadcrump_field (ident_get_plugin_instance (ident), "plugin_instance");
111 show_breadcrump_field (ident_get_type (ident), "type");
112 printf (" – ");
113 show_breadcrump_field (ident_get_type_instance (ident), "type_instance");
114 printf (""</div>\n");
116 ident_destroy (ident);
118 } /* }}} int show_breadcrump */
120 static int show_time_selector (__attribute__((unused)) void *user_data) /* {{{ */
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);
129 printf ("<form action=\"%s\" method=\"get\">\n", script_name ());
131 param_print_hidden (pl);
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><br />\n");
140 printf (" <input id=\"format-json\" type=\"radio\" name=\"format\" value=\"JSON\" checked=\"checked\" />"
141 "<label for=\"format-json\"> JSON (gRaphaël)</label><br />\n"
142 " <input id=\"format-rrd\" type=\"radio\" name=\"format\" value=\"RRD\" />"
143 "<label for=\"format-rrd\"> RRDtool</label>\n<br />");
144 printf (" <input type=\"submit\" name=\"button\" value=\"Go\" />\n");
146 printf ("</form>\n");
151 } /* }}} int show_time_selector */
153 static int left_menu (void *user_data) /* {{{ */
155 show_graph_data_t *data = user_data;
157 graph_instance_t *inst;
158 graph_ident_t *ident;
161 memset (params, 0, sizeof (params));
162 graph_get_params (data->cfg, params, sizeof (params));
163 html_escape_buffer (params, sizeof (params));
165 inst = inst_get_selected (data->cfg);
166 ident = inst_get_selector (inst);
167 host = ident_get_host (ident);
171 printf ("\n<ul class=\"menu left\">\n"
172 " <li><a href=\"%s?action=show_graph;%s\">All instances</a></li>\n"
173 " <li><a href=\"%s?action=list_graphs\">All graphs</a></li>\n",
174 script_name (), params,
178 char host_html[1024];
181 html_escape_copy (host_html, host, sizeof (host_html));
182 uri_escape_copy (host_uri, host, sizeof (host_uri));
184 printf (" <li><a href=\"%s?action=search;q=host:%s\">Host "%s"</a></li>\n",
185 script_name (), host_uri, host_html);
190 ident_destroy (ident);
193 } /* }}} int left_menu */
195 static int show_instance_json (graph_config_t *cfg, /* {{{ */
196 graph_instance_t *inst,
197 long begin, long end, int index)
199 yajl_gen_config handler_config;
201 const unsigned char *json_buffer;
202 unsigned int json_buffer_length;
204 graph_ident_t *graph_selector;
205 graph_ident_t *inst_selector;
207 graph_selector = graph_get_selector (cfg);
208 if (graph_selector == NULL)
211 inst_selector = inst_get_selector (inst);
212 if (inst_selector == NULL)
214 ident_destroy (inst_selector);
218 memset (&handler_config, 0, sizeof (handler_config));
219 handler_config.beautify = 1;
220 handler_config.indentString = " ";
222 handler = yajl_gen_alloc2 (/* callback = */ NULL,
224 /* alloc functions = */ NULL,
225 /* context = */ NULL);
228 ident_destroy (inst_selector);
229 ident_destroy (graph_selector);
233 yajl_gen_map_open (handler);
235 yajl_gen_string (handler,
236 (unsigned char *) "graph_selector",
237 (unsigned int) strlen ("graph_selector"));
238 ident_to_json (graph_selector, handler);
239 ident_destroy (graph_selector);
241 yajl_gen_string (handler,
242 (unsigned char *) "instance_selector",
243 (unsigned int) strlen ("instance_selector"));
244 ident_to_json (inst_selector, handler);
245 ident_destroy (inst_selector);
247 yajl_gen_string (handler,
248 (unsigned char *) "begin",
249 (unsigned int) strlen ("begin"));
250 yajl_gen_integer (handler, begin);
252 yajl_gen_string (handler,
253 (unsigned char *) "end",
254 (unsigned int) strlen ("end"));
255 yajl_gen_integer (handler, end);
257 yajl_gen_map_close (handler);
260 json_buffer_length = 0;
261 yajl_gen_get_buf (handler, &json_buffer, &json_buffer_length);
263 if (json_buffer == NULL)
265 yajl_gen_free (handler);
269 printf ("<div id=\"c4-graph%i\" class=\"graph-json\"></div>\n", index);
270 printf ("<script type=\"text/javascript\">c4.instances[%i] = %s;</script>\n",
271 index, (const char *) json_buffer);
273 yajl_gen_free (handler);
275 } /* }}} int show_instance_json */
277 static int show_instance_rrdtool (graph_config_t *cfg, /* {{{ */
278 graph_instance_t *inst,
279 long begin, long end, int index)
284 char time_params[128];
286 memset (title, 0, sizeof (title));
287 graph_get_title (cfg, title, sizeof (title));
288 html_escape_buffer (title, sizeof (title));
290 memset (descr, 0, sizeof (descr));
291 inst_describe (cfg, inst, descr, sizeof (descr));
292 html_escape_buffer (descr, sizeof (descr));
294 memset (params, 0, sizeof (params));
295 inst_get_params (cfg, inst, params, sizeof (params));
296 html_escape_buffer (params, sizeof (params));
298 snprintf (time_params, sizeof (time_params), ";begin=%li;end=%li",
300 time_params[sizeof (time_params) - 1] = 0;
302 if (index < MAX_SHOW_GRAPHS)
303 printf ("<div class=\"graph-img\"><img src=\"%s?action=graph;%s%s\" "
304 "title=\"%s / %s\" /></div>\n",
305 script_name (), params, time_params, title, descr);
307 printf ("<a href=\"%s?action=show_instance;%s\">Show graph "
308 ""%s / %s"</a>\n",
309 script_name (), params, title, descr);
312 printf ("<div><a href=\"%s?action=instance_data_json;%s%s\">"
313 "Get graph data as JSON</a></div>\n",
314 script_name (), params, time_params);
318 } /* }}} int show_instance_rrdtool */
320 static int show_instance_cb (graph_config_t *cfg, /* {{{ */
321 graph_instance_t *inst,
324 show_graph_data_t *data = user_data;
331 memset (descr, 0, sizeof (descr));
332 inst_describe (cfg, inst, descr, sizeof (descr));
333 html_escape_buffer (descr, sizeof (descr));
338 status = get_time_args (&begin, &end, /* now = */ NULL);
342 printf ("<h2>Instance "%s"</h2>\n", descr);
344 show_breadcrump (cfg, inst);
346 if (data->format == SGD_FORMAT_RRD)
347 show_instance_rrdtool (cfg, inst, begin, end, data->graph_count);
349 show_instance_json (cfg, inst, begin, end, data->graph_count);
354 } /* }}} int show_instance_cb */
356 static int show_instance (void *user_data) /* {{{ */
358 show_graph_data_t *data = user_data;
359 /* char params[1024]; */
362 status = inst_get_all_selected (data->cfg,
363 /* callback = */ show_instance_cb, /* user data = */ data);
365 fprintf (stderr, "show_instance: inst_get_all_selected failed "
366 "with status %i\n", status);
369 memset (params, 0, sizeof (params));
370 graph_get_params (data->cfg, params, sizeof (params));
371 html_escape_buffer (params, sizeof (params));
373 printf ("<div style=\"clear: both;\"><a href=\"%s?action=graph_def_json;%s\">"
374 "Get graph definition as JSON</a></div>\n",
375 script_name (), params);
379 } /* }}} int show_instance */
381 int action_show_instance (void) /* {{{ */
383 page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT;
384 show_graph_data_t pg_data;
390 memset (&pg_data, 0, sizeof (pg_data));
391 pg_data.cfg = gl_graph_get_selected ();
392 if (pg_data.cfg == NULL)
393 OUTPUT_ERROR ("gl_graph_get_selected () failed.\n");
395 memset (tmp, 0, sizeof (tmp));
396 graph_get_title (pg_data.cfg, tmp, sizeof (tmp));
397 snprintf (title, sizeof (title), "Graph \"%s\"", tmp);
398 title[sizeof (title) - 1] = 0;
400 format = param ("format");
401 if ((format != NULL) && (strcasecmp ("RRD", format) == 0))
402 pg_data.format = SGD_FORMAT_RRD;
404 pg_data.format = SGD_FORMAT_JSON;
406 pg_callbacks.top_right = html_print_search_box;
407 pg_callbacks.middle_center = show_instance;
408 pg_callbacks.middle_left = left_menu;
409 pg_callbacks.middle_right = show_time_selector;
411 html_print_page (title, &pg_callbacks, &pg_data);
414 } /* }}} int action_show_instance */
416 /* vim: set sw=2 sts=2 et fdm=marker : */