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 **/
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>
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"
40 #include <fcgiapp.h>
41 #include <fcgi_stdio.h>
43 #define OUTPUT_ERROR(...) do { \
44 printf ("Content-Type: text/plain\n\n"); \
45 printf (__VA_ARGS__); \
46 return (0); \
47 } while (0)
49 #define MAX_SHOW_GRAPHS 10
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;
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);
72 if (field_name != NULL)
73 printf ("<a href=\"%s?action=list_graphs;q=%s:%s\">%s</a>",
74 script_name (), field_name, str_html, str_html);
75 else
76 printf ("<a href=\"%s?action=list_graphs;q=%s\">%s</a>",
77 script_name (), str_html, str_html);
79 free (str_html);
80 }
81 } /* }}} void show_breadcrump_field */
83 static int show_breadcrump (graph_config_t *cfg, /* {{{ */
84 graph_instance_t *inst)
85 {
86 graph_ident_t *ident;
87 char *prefix;
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 }
100 printf ("<div class=\"breadcrump\">%s: "", prefix);
101 show_breadcrump_field (ident_get_host (ident), "host");
102 printf (" / ");
103 show_breadcrump_field (ident_get_plugin (ident), "plugin");
104 printf (" – ");
105 show_breadcrump_field (ident_get_plugin_instance (ident), "plugin_instance");
106 printf (" / ");
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);
113 return (0);
114 } /* }}} int show_breadcrump */
116 static int show_time_selector (__attribute__((unused)) void *user_data) /* {{{ */
117 {
118 param_list_t *pl;
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"
135 " </select>\n"
136 " <input type=\"submit\" name=\"button\" value=\"Go\" />\n");
138 printf ("</form>\n");
140 param_destroy (pl);
142 return (0);
143 } /* }}} int show_time_selector */
145 static int show_instance_list_cb (graph_instance_t *inst, /* {{{ */
146 void *user_data)
147 {
148 show_graph_data_t *data = user_data;
149 char descr[128];
150 char params[1024];
152 memset (descr, 0, sizeof (descr));
153 inst_describe (data->cfg, inst, descr, sizeof (descr));
154 html_escape_buffer (descr, sizeof (descr));
156 if (inst == data->inst)
157 {
158 printf (" <li class=\"instance\"><strong>%s</strong></li>\n", descr);
159 return (0);
160 }
162 memset (params, 0, sizeof (params));
163 inst_get_params (data->cfg, inst, params, sizeof (params));
164 html_escape_buffer (params, sizeof (params));
166 printf (" <li class=\"instance\">"
167 "<a href=\"%s?action=show_instance;%s\">%s</a></li>\n",
168 script_name (), params, descr);
170 return (0);
171 } /* }}} int show_instance_list_cb */
173 static int show_instance_list (void *user_data) /* {{{ */
174 {
175 show_graph_data_t *data = user_data;
176 char title[128];
177 char params[1024];
179 memset (title, 0, sizeof (title));
180 graph_get_title (data->cfg, title, sizeof (title));
181 html_escape_buffer (title, sizeof (title));
183 memset (params, 0, sizeof (params));
184 graph_get_params (data->cfg, params, sizeof (params));
185 html_escape_buffer (params, sizeof (params));
187 printf ("<ul class=\"graph_list\">\n"
188 " <li class=\"graph\"><a href=\"%s?action=show_instance;%s\">%s</a>\n"
189 " <ul class=\"instance_list\">\n",
190 script_name (), params, title);
192 graph_inst_foreach (data->cfg, show_instance_list_cb, user_data);
194 printf (" </ul>\n"
195 "</ul>\n");
197 return (0);
198 } /* }}} int show_instance_list */
200 static int show_instance_cb (graph_config_t *cfg, /* {{{ */
201 graph_instance_t *inst,
202 void *user_data)
203 {
204 show_graph_data_t *data = user_data;
205 char title[128];
206 char descr[128];
207 char params[1024];
209 long begin;
210 long end;
211 char time_params[128];
212 int status;
214 memset (title, 0, sizeof (title));
215 graph_get_title (cfg, title, sizeof (title));
216 html_escape_buffer (title, sizeof (title));
218 memset (descr, 0, sizeof (descr));
219 inst_describe (cfg, inst, descr, sizeof (descr));
220 html_escape_buffer (descr, sizeof (descr));
222 memset (params, 0, sizeof (params));
223 inst_get_params (cfg, inst, params, sizeof (params));
224 html_escape_buffer (params, sizeof (params));
226 time_params[0] = 0;
227 begin = 0;
228 end = 0;
230 status = get_time_args (&begin, &end, /* now = */ NULL);
231 if (status == 0)
232 {
233 snprintf (time_params, sizeof (time_params), ";begin=%li;end=%li",
234 begin, end);
235 time_params[sizeof (time_params) - 1] = 0;
236 }
238 printf ("<h3>Instance "%s"</h3>\n", descr);
240 show_breadcrump (cfg, inst);
242 if (data->graph_count < MAX_SHOW_GRAPHS)
243 printf ("<div class=\"graph-img\"><img src=\"%s?action=graph;%s%s\" "
244 "title=\"%s / %s\" /></div>\n",
245 script_name (), params, time_params, title, descr);
246 else
247 printf ("<a href=\"%s?action=show_instance;%s\">Show graph "
248 ""%s / %s"</a>\n",
249 script_name (), params, title, descr);
251 data->graph_count++;
253 return (0);
254 } /* }}} int show_instance_cb */
256 static int show_instance (void *user_data) /* {{{ */
257 {
258 show_graph_data_t *data = user_data;
259 int status;
261 fprintf (stderr, "show_instance: Calling inst_get_all_selected()\n");
262 status = inst_get_all_selected (data->cfg,
263 /* callback = */ show_instance_cb, /* user data = */ data);
264 if (status != 0)
265 fprintf (stderr, "show_instance: inst_get_all_selected failed "
266 "with status %i\n", status);
268 return (0);
269 } /* }}} int show_instance */
271 int action_show_instance (void) /* {{{ */
272 {
273 page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT;
274 show_graph_data_t pg_data;
276 char tmp[128];
277 char title[128];
279 memset (&pg_data, 0, sizeof (pg_data));
280 pg_data.cfg = gl_graph_get_selected ();
281 if (pg_data.cfg == NULL)
282 OUTPUT_ERROR ("gl_graph_get_selected () failed.\n");
284 memset (tmp, 0, sizeof (tmp));
285 graph_get_title (pg_data.cfg, tmp, sizeof (tmp));
286 snprintf (title, sizeof (title), "Graph \"%s\"", tmp);
287 title[sizeof (title) - 1] = 0;
289 pg_callbacks.top_right = html_print_search_box;
290 pg_callbacks.middle_center = show_instance;
291 pg_callbacks.middle_left = show_instance_list;
292 pg_callbacks.middle_right = show_time_selector;
294 html_print_page (title, &pg_callbacks, &pg_data);
296 return (0);
297 } /* }}} int action_show_instance */
299 /* vim: set sw=2 sts=2 et fdm=marker : */