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 struct show_graph_data_s
54 graph_instance_t *inst;
57 typedef struct show_graph_data_s show_graph_data_t;
59 static void show_breadcrump_field (const char *str, /* {{{ */
60 const char *field_name)
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>");
70 char *str_html = html_escape (str);
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);
76 printf ("<a href=\"%s?action=search;q=%s\">%s</a>",
77 script_name (), str_html, str_html);
81 } /* }}} void show_breadcrump_field */
83 static int show_breadcrump (graph_config_t *cfg, /* {{{ */
84 graph_instance_t *inst)
92 ident = inst_get_selector (inst);
97 ident = graph_get_selector (cfg);
100 printf ("<div class=\"breadcrump\">%s: "", prefix);
101 show_breadcrump_field (ident_get_host (ident), "host");
103 show_breadcrump_field (ident_get_plugin (ident), "plugin");
104 printf (" – ");
105 show_breadcrump_field (ident_get_plugin_instance (ident), "plugin_instance");
107 show_breadcrump_field (ident_get_type (ident), "type");
108 printf (" – ");
109 show_breadcrump_field (ident_get_type_instance (ident), "type_instance");
110 printf (""</div>\n");
112 ident_destroy (ident);
114 } /* }}} int show_breadcrump */
116 static int show_time_selector (__attribute__((unused)) void *user_data) /* {{{ */
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);
125 printf ("<form action=\"%s\" method=\"get\">\n", script_name ());
127 param_print_hidden (pl);
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"
136 " <input type=\"submit\" name=\"button\" value=\"Go\" />\n");
138 printf ("</form>\n");
143 } /* }}} int show_time_selector */
145 static int left_menu (void *user_data) /* {{{ */
147 show_graph_data_t *data = user_data;
149 graph_instance_t *inst;
150 graph_ident_t *ident;
153 memset (params, 0, sizeof (params));
154 graph_get_params (data->cfg, params, sizeof (params));
155 html_escape_buffer (params, sizeof (params));
157 inst = inst_get_selected (data->cfg);
158 ident = inst_get_selector (inst);
159 host = ident_get_host (ident);
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,
170 char host_html[1024];
173 html_escape_copy (host_html, host, sizeof (host_html));
174 uri_escape_copy (host_uri, host, sizeof (host_uri));
176 printf (" <li><a href=\"%s?action=search;q=host:%s\">Host "%s"</a></li>\n",
177 script_name (), host_uri, host_html);
182 ident_destroy (ident);
185 } /* }}} int left_menu */
187 static int show_instance_cb (graph_config_t *cfg, /* {{{ */
188 graph_instance_t *inst,
191 show_graph_data_t *data = user_data;
198 char time_params[128];
201 memset (title, 0, sizeof (title));
202 graph_get_title (cfg, title, sizeof (title));
203 html_escape_buffer (title, sizeof (title));
205 memset (descr, 0, sizeof (descr));
206 inst_describe (cfg, inst, descr, sizeof (descr));
207 html_escape_buffer (descr, sizeof (descr));
209 memset (params, 0, sizeof (params));
210 inst_get_params (cfg, inst, params, sizeof (params));
211 html_escape_buffer (params, sizeof (params));
217 status = get_time_args (&begin, &end, /* now = */ NULL);
220 snprintf (time_params, sizeof (time_params), ";begin=%li;end=%li",
222 time_params[sizeof (time_params) - 1] = 0;
225 printf ("<h2>Instance "%s"</h2>\n", descr);
227 show_breadcrump (cfg, inst);
229 if (data->graph_count < MAX_SHOW_GRAPHS)
230 printf ("<div class=\"graph-img\"><img src=\"%s?action=graph;%s%s\" "
231 "title=\"%s / %s\" /></div>\n",
232 script_name (), params, time_params, title, descr);
234 printf ("<a href=\"%s?action=show_instance;%s\">Show graph "
235 ""%s / %s"</a>\n",
236 script_name (), params, title, descr);
238 printf ("<div id=\"c4-graph%i\" class=\"graph-json\"></div>\n", data->graph_count);
239 printf ("<script type=\"text/javascript\">c4.graphs[%i] = { \"params\": \"%s\", \"begin\": %li, \"end\": %li };</script>\n",
240 data->graph_count, params, (long) begin, (long) end);
242 printf ("<div style=\"clear: both;\"><a href=\"%s?action=graph_data_json;%s%s\">"
243 "Get graph data as JSON</a></div>\n",
244 script_name (), params, time_params);
249 } /* }}} int show_instance_cb */
251 static int show_instance (void *user_data) /* {{{ */
253 show_graph_data_t *data = user_data;
257 status = inst_get_all_selected (data->cfg,
258 /* callback = */ show_instance_cb, /* user data = */ data);
260 fprintf (stderr, "show_instance: inst_get_all_selected failed "
261 "with status %i\n", status);
263 memset (params, 0, sizeof (params));
264 graph_get_params (data->cfg, params, sizeof (params));
265 html_escape_buffer (params, sizeof (params));
267 printf ("<div style=\"clear: both;\"><a href=\"%s?action=graph_def_json;%s\">"
268 "Get graph definition as JSON</a></div>\n",
269 script_name (), params);
272 } /* }}} int show_instance */
274 int action_show_instance (void) /* {{{ */
276 page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT;
277 show_graph_data_t pg_data;
282 memset (&pg_data, 0, sizeof (pg_data));
283 pg_data.cfg = gl_graph_get_selected ();
284 if (pg_data.cfg == NULL)
285 OUTPUT_ERROR ("gl_graph_get_selected () failed.\n");
287 memset (tmp, 0, sizeof (tmp));
288 graph_get_title (pg_data.cfg, tmp, sizeof (tmp));
289 snprintf (title, sizeof (title), "Graph \"%s\"", tmp);
290 title[sizeof (title) - 1] = 0;
292 pg_callbacks.top_right = html_print_search_box;
293 pg_callbacks.middle_center = show_instance;
294 pg_callbacks.middle_left = left_menu;
295 pg_callbacks.middle_right = show_time_selector;
297 html_print_page (title, &pg_callbacks, &pg_data);
300 } /* }}} int action_show_instance */
302 /* vim: set sw=2 sts=2 et fdm=marker : */