6 #include "action_list_graphs.h"
9 #include "graph_ident.h"
10 #include "graph_instance.h"
11 #include "graph_list.h"
12 #include "utils_cgi.h"
15 #include <fcgi_stdio.h>
17 #define RESULT_LIMIT 50
19 struct callback_data_s
29 typedef struct callback_data_s callback_data_t;
31 static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
32 graph_instance_t *inst,
35 callback_data_t *data = user_data;
42 if (data->graph_index >= data->graph_limit)
48 if (data->cfg != NULL)
49 printf (" </ul></li>\n");
51 memset (desc, 0, sizeof (desc));
52 graph_get_title (cfg, desc, sizeof (desc));
53 html_escape_buffer (desc, sizeof (desc));
55 printf (" <li class=\"graph\">%s\n"
56 " <ul class=\"instance_list\">\n", desc);
59 data->inst_index = -1;
64 if (data->inst_index >= data->inst_limit)
68 printf (" <li class=\"instance more\">More ...</li>\n");
74 memset (params, 0, sizeof (params));
75 inst_get_params (cfg, inst, params, sizeof (params));
76 html_escape_buffer (params, sizeof (params));
78 memset (desc, 0, sizeof (desc));
79 inst_describe (cfg, inst, desc, sizeof (desc));
80 html_escape_buffer (desc, sizeof (desc));
82 printf (" <li class=\"instance\"><a href=\"%s?action=show_graph;%s\">%s</a></li>\n",
83 script_name (), params, desc);
86 } /* }}} int print_graph_inst_html */
90 const char *search_term;
92 typedef struct page_data_s page_data_t;
94 static int print_search_result (void *user_data) /* {{{ */
96 page_data_t *pg_data = user_data;
97 callback_data_t cb_data = { /* cfg = */ NULL,
98 /* graph_index = */ -1, /* graph_limit = */ 20, /* graph_more = */ 0,
99 /* inst_index = */ -1, /* inst_limit = */ 5, /* inst more = */ 0 };
101 if (pg_data->search_term != NULL)
103 char *search_term_html = html_escape (pg_data->search_term);
104 printf (" <h2>Search results for "%s"</h2>\n",
106 free (search_term_html);
109 printf (" <ul id=\"search-output\" class=\"graph_list\">\n");
110 if (pg_data->search_term == NULL)
111 gl_instance_get_all (print_graph_inst_html, /* user_data = */ &cb_data);
114 char *term_lc = strtolower_copy (pg_data->search_term);
116 if (strncmp ("host:", term_lc, strlen ("host:")) == 0)
117 gl_search_field (GIF_HOST, term_lc + strlen ("host:"),
118 print_graph_inst_html, /* user_data = */ &cb_data);
119 else if (strncmp ("plugin:", term_lc, strlen ("plugin:")) == 0)
120 gl_search_field (GIF_PLUGIN, term_lc + strlen ("plugin:"),
121 print_graph_inst_html, /* user_data = */ &cb_data);
122 else if (strncmp ("type:", term_lc, strlen ("type:")) == 0)
123 gl_search_field (GIF_TYPE, term_lc + strlen ("type:"),
124 print_graph_inst_html, /* user_data = */ &cb_data);
127 print_graph_inst_html, /* user_data = */ &cb_data);
132 if (cb_data.cfg != NULL)
133 printf (" </ul></li>\n");
135 if (cb_data.graph_more)
137 printf (" <li class=\"graph more\">More ...</li>\n");
143 } /* }}} int print_search_result */
145 struct print_host_list_data_s
150 typedef struct print_host_list_data_s print_host_list_data_t;
152 static int print_host_list_callback (graph_config_t *cfg, /* {{{ */
153 graph_instance_t *inst, void *user_data)
155 print_host_list_data_t *data = user_data;
156 graph_ident_t *ident;
159 /* make compiler happy */
162 ident = inst_get_selector (inst);
166 host = ident_get_host (ident);
169 ident_destroy (ident);
176 if ((data->last_host != NULL)
177 && (strcmp (data->last_host, host) == 0))
179 ident_destroy (ident);
183 free (data->last_host);
184 data->last_host = strdup (host);
186 array_append (data->array, host);
188 ident_destroy (ident);
190 } /* }}} int print_host_list_callback */
192 static int print_host_list (__attribute__((unused)) void *user_data) /* {{{ */
194 print_host_list_data_t data;
199 data.array = array_create ();
200 data.last_host = NULL;
202 gl_instance_get_all (print_host_list_callback, &data);
204 free (data.last_host);
205 data.last_host = NULL;
207 array_sort (data.array);
209 hosts_argc = array_argc (data.array);
210 hosts_argv = array_argv (data.array);
214 array_destroy (data.array);
218 printf ("<div><h3>List of hosts</h3>\n"
219 "<ul id=\"host-list\">\n");
220 for (i = 0; i < hosts_argc; i++)
222 char *host = hosts_argv[i];
225 if ((data.last_host != NULL) && (strcmp (data.last_host, host) == 0))
227 data.last_host = host;
229 host_html = html_escape (host);
231 printf (" <li><a href=\"%s?action=list_graphs&q=host:%s\">%s</a></li>\n",
232 script_name (), host_html, host_html);
236 printf ("</ul></div>\n");
238 array_destroy (data.array);
241 } /* }}} int print_host_list */
243 static int list_graphs_html (const char *term) /* {{{ */
246 page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT;
250 snprintf (title, sizeof (title), "c4: Graphs matching \"%s\"", term);
252 strncpy (title, "c4: List of all graphs", sizeof (title));
253 title[sizeof (title) - 1] = 0;
255 memset (&pg_data, 0, sizeof (pg_data));
256 pg_data.search_term = term;
258 pg_callbacks.top_right = html_print_search_box;
259 pg_callbacks.middle_left = print_host_list;
260 pg_callbacks.middle_center = print_search_result;
262 html_print_page (title, &pg_callbacks, &pg_data);
265 } /* }}} int list_graphs_html */
267 int action_list_graphs (void) /* {{{ */
274 search = strtolower_copy (param ("q"));
275 status = list_graphs_html (search);
279 } /* }}} int action_list_graphs */
281 /* vim: set sw=2 sts=2 et fdm=marker : */