6 #include "action_list_graphs.h"
9 #include "graph_ident.h"
10 #include "graph_list.h"
11 #include "utils_cgi.h"
14 #include <fcgi_stdio.h>
16 #define RESULT_LIMIT 50
18 struct callback_data_s
24 typedef struct callback_data_s callback_data_t;
26 static int json_begin_graph (graph_config_t *cfg) /* {{{ */
33 graph_get_title (cfg, desc, sizeof (desc));
35 printf ("{\"title\":\"%s\",\"instances\":[", desc);
38 } /* }}} int json_begin_graph */
40 static int json_end_graph (void) /* {{{ */
45 } /* }}} int json_end_graph */
47 static int json_print_instance (graph_config_t *cfg, /* {{{ */
48 graph_instance_t *inst)
53 if ((cfg == NULL) || (inst == NULL))
56 memset (desc, 0, sizeof (desc));
57 inst_describe (cfg, inst, desc, sizeof (desc));
59 memset (params, 0, sizeof (params));
60 inst_get_params (cfg, inst, params, sizeof (params));
62 printf ("{\"description\":\"%s\",\"params\":\"%s\"}",
66 } /* }}} int json_print_instance */
68 static int print_graph_inst_json (graph_config_t *cfg, /* {{{ */
69 graph_instance_t *inst,
72 callback_data_t *data = user_data;
81 json_begin_graph (cfg);
86 else /* if (not first instance) */
91 json_print_instance (cfg, inst);
100 } /* }}} int print_graph_inst_json */
102 static int list_graphs_json (const char *term) /* {{{ */
104 callback_data_t data;
107 char time_buffer[128];
110 printf ("Content-Type: application/json\n");
113 status = time_to_rfc1123 (now + 300, time_buffer, sizeof (time_buffer));
115 printf ("Expires: %s\n"
116 "Cache-Control: public\n",
121 data.limit = RESULT_LIMIT;
126 gl_instance_get_all (print_graph_inst_json, /* user_data = */ &data);
128 gl_search (term, print_graph_inst_json, /* user_data = */ &data);
136 } /* }}} int list_graphs_json */
138 static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
139 graph_instance_t *inst,
142 callback_data_t *data = user_data;
146 if (data->cfg != cfg)
148 if (data->cfg != NULL)
149 printf (" </ul></li>\n");
151 memset (desc, 0, sizeof (desc));
152 graph_get_title (cfg, desc, sizeof (desc));
153 html_escape_buffer (desc, sizeof (desc));
155 printf (" <li class=\"graph\">%s\n"
156 " <ul class=\"instance_list\">\n", desc);
161 memset (params, 0, sizeof (params));
162 inst_get_params (cfg, inst, params, sizeof (params));
163 html_escape_buffer (params, sizeof (params));
165 memset (desc, 0, sizeof (desc));
166 inst_describe (cfg, inst, desc, sizeof (desc));
167 html_escape_buffer (desc, sizeof (desc));
169 printf (" <li class=\"instance\"><a href=\"%s?action=graph;%s\">%s</a></li>\n",
170 script_name (), params, desc);
175 /* Abort scan if limit is reached. */
176 if (data->limit == 0)
180 } /* }}} int print_graph_inst_html */
184 const char *search_term;
186 typedef struct page_data_s page_data_t;
188 static int print_search_box (void *user_data) /* {{{ */
190 page_data_t *data = user_data;
195 fprintf (stderr, "print_search_box: data == NULL\n");
199 term_html = html_escape (data->search_term);
201 printf ("<form action=\"%s\" method=\"get\">\n"
202 " <input type=\"hidden\" name=\"action\" value=\"list_graphs\" />\n"
203 " <input type=\"text\" name=\"search\" value=\"%s\" id=\"search-input\" />\n"
204 " <input type=\"submit\" name=\"button\" value=\"Search\" />\n"
207 (term_html != NULL) ? term_html : "");
212 } /* }}} int print_search_box */
214 static int print_search_result (void *user_data) /* {{{ */
216 page_data_t *pg_data = user_data;
217 callback_data_t cb_data = { NULL, /* limit = */ RESULT_LIMIT, /* first = */ 1 };
219 printf (" <ul id=\"search-output\" class=\"graph_list\">\n");
220 if (pg_data->search_term == NULL)
221 gl_instance_get_all (print_graph_inst_html, /* user_data = */ &cb_data);
224 char *term_lc = strtolower_copy (pg_data->search_term);
225 gl_search (term_lc, print_graph_inst_html, /* user_data = */ &cb_data);
229 if (cb_data.cfg != NULL)
230 printf (" </ul></li>\n");
235 } /* }}} int print_search_result */
237 struct print_host_list_data_s
242 typedef struct print_host_list_data_s print_host_list_data_t;
244 static int print_host_list_callback (graph_config_t *cfg, /* {{{ */
245 graph_instance_t *inst, void *user_data)
247 print_host_list_data_t *data = user_data;
248 graph_ident_t *ident;
251 /* make compiler happy */
254 ident = inst_get_selector (inst);
258 host = ident_get_host (ident);
261 ident_destroy (ident);
268 if ((data->last_host != NULL)
269 && (strcmp (data->last_host, host) == 0))
271 ident_destroy (ident);
275 free (data->last_host);
276 data->last_host = strdup (host);
278 array_append (data->array, host);
280 ident_destroy (ident);
282 } /* }}} int print_host_list_callback */
284 static int print_host_list (__attribute__((unused)) void *user_data) /* {{{ */
286 print_host_list_data_t data;
291 data.array = array_create ();
292 data.last_host = NULL;
294 gl_instance_get_all (print_host_list_callback, &data);
296 free (data.last_host);
297 data.last_host = NULL;
299 array_sort (data.array);
301 hosts_argc = array_argc (data.array);
302 hosts_argv = array_argv (data.array);
306 array_destroy (data.array);
310 printf ("<ul id=\"host-list\">\n");
311 for (i = 0; i < hosts_argc; i++)
313 char *host = hosts_argv[i];
316 if ((data.last_host != NULL) && (strcmp (data.last_host, host) == 0))
318 data.last_host = host;
320 host_html = html_escape (host);
322 printf (" <li><a href=\"%s?action=list_graphs&search=%s\">%s</a></li>\n",
323 script_name (), host_html, host_html);
329 array_destroy (data.array);
332 } /* }}} int print_host_list */
334 static int list_graphs_html (const char *term) /* {{{ */
337 page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT;
341 snprintf (title, sizeof (title), "c4: Graphs matching \"%s\"", term);
343 strncpy (title, "c4: List of all graphs", sizeof (title));
344 title[sizeof (title) - 1] = 0;
346 memset (&pg_data, 0, sizeof (pg_data));
347 pg_data.search_term = term;
349 pg_callbacks.top_right = print_search_box;
350 pg_callbacks.middle_left = print_host_list;
351 pg_callbacks.middle_center = print_search_result;
353 html_print_page (title, &pg_callbacks, &pg_data);
356 } /* }}} int list_graphs_html */
358 int action_list_graphs (void) /* {{{ */
366 search = strtolower_copy (param ("search"));
368 format = param ("format");
372 if (strcmp ("json", format) == 0)
373 status = list_graphs_json (search);
375 status = list_graphs_html (search);
380 } /* }}} int action_list_graphs */
382 /* vim: set sw=2 sts=2 et fdm=marker : */