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
28 typedef struct callback_data_s callback_data_t;
30 static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
31 graph_instance_t *inst,
34 callback_data_t *data = user_data;
41 if (data->graph_index >= data->graph_limit)
47 if (data->cfg != NULL)
48 printf (" </ul></li>\n");
50 memset (desc, 0, sizeof (desc));
51 graph_get_title (cfg, desc, sizeof (desc));
52 html_escape_buffer (desc, sizeof (desc));
54 printf (" <li class=\"graph\">%s\n"
55 " <ul class=\"instance_list\">\n", desc);
58 data->inst_index = -1;
63 if (data->inst_index >= data->inst_limit)
67 printf (" <li class=\"instance more\">More ...</li>\n");
73 memset (params, 0, sizeof (params));
74 inst_get_params (cfg, inst, params, sizeof (params));
75 html_escape_buffer (params, sizeof (params));
77 memset (desc, 0, sizeof (desc));
78 inst_describe (cfg, inst, desc, sizeof (desc));
79 html_escape_buffer (desc, sizeof (desc));
81 printf (" <li class=\"instance\"><a href=\"%s?action=show_graph;%s\">%s</a></li>\n",
82 script_name (), params, desc);
85 } /* }}} int print_graph_inst_html */
89 const char *search_term;
91 typedef struct page_data_s page_data_t;
93 static int print_search_result (void *user_data) /* {{{ */
95 page_data_t *pg_data = user_data;
96 callback_data_t cb_data = { /* cfg = */ NULL,
97 /* graph_index = */ -1, /* graph_limit = */ 20, /* graph_more = */ 0,
98 /* inst_index = */ -1, /* inst_limit = */ 5, /* inst more = */ 0 };
100 if (pg_data->search_term != NULL)
102 char *search_term_html = html_escape (pg_data->search_term);
103 printf (" <h2>Search results for "%s"</h2>\n",
105 free (search_term_html);
108 printf (" <ul id=\"search-output\" class=\"graph_list\">\n");
109 if (pg_data->search_term == NULL)
110 gl_instance_get_all (print_graph_inst_html, /* user_data = */ &cb_data);
113 char *term_lc = strtolower_copy (pg_data->search_term);
115 if (strncmp ("host:", term_lc, strlen ("host:")) == 0)
116 gl_search_field (GIF_HOST, term_lc + strlen ("host:"),
117 print_graph_inst_html, /* user_data = */ &cb_data);
118 else if (strncmp ("plugin:", term_lc, strlen ("plugin:")) == 0)
119 gl_search_field (GIF_PLUGIN, term_lc + strlen ("plugin:"),
120 print_graph_inst_html, /* user_data = */ &cb_data);
121 else if (strncmp ("type:", term_lc, strlen ("type:")) == 0)
122 gl_search_field (GIF_TYPE, term_lc + strlen ("type:"),
123 print_graph_inst_html, /* user_data = */ &cb_data);
126 print_graph_inst_html, /* user_data = */ &cb_data);
131 if (cb_data.cfg != NULL)
132 printf (" </ul></li>\n");
134 if (cb_data.graph_more)
136 printf (" <li class=\"graph more\">More ...</li>\n");
142 } /* }}} int print_search_result */
144 struct print_host_list_data_s
149 typedef struct print_host_list_data_s print_host_list_data_t;
151 static int print_host_list_callback (graph_config_t *cfg, /* {{{ */
152 graph_instance_t *inst, void *user_data)
154 print_host_list_data_t *data = user_data;
155 graph_ident_t *ident;
158 /* make compiler happy */
161 ident = inst_get_selector (inst);
165 host = ident_get_host (ident);
168 ident_destroy (ident);
175 if ((data->last_host != NULL)
176 && (strcmp (data->last_host, host) == 0))
178 ident_destroy (ident);
182 free (data->last_host);
183 data->last_host = strdup (host);
185 array_append (data->array, host);
187 ident_destroy (ident);
189 } /* }}} int print_host_list_callback */
191 static int print_host_list (__attribute__((unused)) void *user_data) /* {{{ */
193 print_host_list_data_t data;
198 data.array = array_create ();
199 data.last_host = NULL;
201 gl_instance_get_all (print_host_list_callback, &data);
203 free (data.last_host);
204 data.last_host = NULL;
206 array_sort (data.array);
208 hosts_argc = array_argc (data.array);
209 hosts_argv = array_argv (data.array);
213 array_destroy (data.array);
217 printf ("<div><h3>List of hosts</h3>\n"
218 "<ul id=\"host-list\">\n");
219 for (i = 0; i < hosts_argc; i++)
221 char *host = hosts_argv[i];
224 if ((data.last_host != NULL) && (strcmp (data.last_host, host) == 0))
226 data.last_host = host;
228 host_html = html_escape (host);
230 printf (" <li><a href=\"%s?action=list_graphs&q=%s\">%s</a></li>\n",
231 script_name (), host_html, host_html);
235 printf ("</ul></div>\n");
237 array_destroy (data.array);
240 } /* }}} int print_host_list */
242 static int list_graphs_html (const char *term) /* {{{ */
245 page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT;
249 snprintf (title, sizeof (title), "c4: Graphs matching \"%s\"", term);
251 strncpy (title, "c4: List of all graphs", sizeof (title));
252 title[sizeof (title) - 1] = 0;
254 memset (&pg_data, 0, sizeof (pg_data));
255 pg_data.search_term = term;
257 pg_callbacks.top_right = html_print_search_box;
258 pg_callbacks.middle_left = print_host_list;
259 pg_callbacks.middle_center = print_search_result;
261 html_print_page (title, &pg_callbacks, &pg_data);
264 } /* }}} int list_graphs_html */
266 int action_list_graphs (void) /* {{{ */
273 search = strtolower_copy (param ("q"));
274 status = list_graphs_html (search);
278 } /* }}} int action_list_graphs */
280 /* vim: set sw=2 sts=2 et fdm=marker : */