7782d778fe79de05db643f5dacf75abca9d18ad5
1 /**
2 * collection4 - action_search.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>
29 #include "action_search.h"
30 #include "common.h"
31 #include "graph.h"
32 #include "graph_ident.h"
33 #include "graph_instance.h"
34 #include "graph_list.h"
35 #include "utils_cgi.h"
37 #include <fcgiapp.h>
38 #include <fcgi_stdio.h>
40 #define RESULT_LIMIT 50
42 struct callback_data_s
43 {
44 graph_config_t *cfg;
45 int graph_index;
46 int graph_limit;
47 _Bool graph_more;
48 int inst_index;
49 int inst_limit;
50 _Bool inst_more;
51 const char *search_term;
52 };
53 typedef struct callback_data_s callback_data_t;
55 static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
56 graph_instance_t *inst,
57 void *user_data)
58 {
59 callback_data_t *data = user_data;
60 char params[1024];
61 char desc[1024];
63 if (data->cfg != cfg)
64 {
65 data->graph_index++;
66 if (data->graph_index >= data->graph_limit)
67 {
68 data->graph_more = 1;
69 return (1);
70 }
72 if (data->cfg != NULL)
73 printf (" </ul></li>\n");
75 memset (desc, 0, sizeof (desc));
76 graph_get_title (cfg, desc, sizeof (desc));
77 html_escape_buffer (desc, sizeof (desc));
79 printf (" <li class=\"graph\">%s\n"
80 " <ul class=\"instance_list\">\n", desc);
82 data->cfg = cfg;
83 data->inst_index = -1;
84 data->inst_more = 0;
85 }
87 data->inst_index++;
88 if (data->inst_index >= data->inst_limit)
89 {
90 if (!data->inst_more)
91 {
92 char *search_term_html = html_escape (data->search_term);
93 char param_search_term[1024];
95 memset (params, 0, sizeof (params));
96 graph_get_params (cfg, params, sizeof (params));
97 html_escape_buffer (params, sizeof (params));
99 param_search_term[0] = 0;
100 if (search_term_html != NULL)
101 {
102 snprintf (param_search_term, sizeof (param_search_term), ";q=%s",
103 search_term_html);
104 param_search_term[sizeof (param_search_term) - 1] = 0;
105 }
107 free (search_term_html);
109 printf (" <li class=\"instance more\"><a href=\"%s"
110 "?action=show_graph;%s%s\">More …</a></li>\n",
111 script_name (), params, param_search_term);
113 data->inst_more = 1;
114 }
115 return (0);
116 }
118 memset (params, 0, sizeof (params));
119 inst_get_params (cfg, inst, params, sizeof (params));
120 html_escape_buffer (params, sizeof (params));
122 memset (desc, 0, sizeof (desc));
123 inst_describe (cfg, inst, desc, sizeof (desc));
124 html_escape_buffer (desc, sizeof (desc));
126 printf (" <li class=\"instance\"><a href=\"%s?action=show_instance;%s\">%s</a></li>\n",
127 script_name (), params, desc);
129 return (0);
130 } /* }}} int print_graph_inst_html */
132 static int print_graph_html (graph_config_t *cfg, /* {{{ */
133 __attribute__((unused)) void *user_data)
134 {
135 char params[1024];
136 char title[1024];
138 if (graph_num_instances (cfg) < 1)
139 return (0);
141 memset (title, 0, sizeof (title));
142 graph_get_title (cfg, title, sizeof (title));
143 html_escape_buffer (title, sizeof (title));
145 memset (params, 0, sizeof (params));
146 graph_get_params (cfg, params, sizeof (params));
147 html_escape_buffer (params, sizeof (params));
149 printf (" <li class=\"graph\"><a href=\"%s?action=show_graph;%s\">"
150 "%s</a></li>\n",
151 script_name (), params, title);
153 return (0);
154 } /* }}} int print_graph_html */
156 struct page_data_s
157 {
158 const char *search_term;
159 };
160 typedef struct page_data_s page_data_t;
162 static int print_search_result (void *user_data) /* {{{ */
163 {
164 page_data_t *pg_data = user_data;
165 callback_data_t cb_data = { /* cfg = */ NULL,
166 /* graph_index = */ -1, /* graph_limit = */ 20, /* graph_more = */ 0,
167 /* inst_index = */ -1, /* inst_limit = */ 5, /* inst more = */ 0,
168 /* search_term = */ pg_data->search_term };
170 if (pg_data->search_term != NULL)
171 {
172 char *search_term_html = html_escape (pg_data->search_term);
173 printf (" <h2>Search results for "%s"</h2>\n",
174 search_term_html);
175 free (search_term_html);
176 }
178 printf (" <ul id=\"search-output\" class=\"graph_list\">\n");
179 if (pg_data->search_term == NULL)
180 {
181 gl_graph_get_all (print_graph_html, /* user_data = */ &cb_data);
182 }
183 else
184 {
185 char *term_lc = strtolower_copy (pg_data->search_term);
187 if (strncmp ("host:", term_lc, strlen ("host:")) == 0)
188 gl_search_field (GIF_HOST, term_lc + strlen ("host:"),
189 print_graph_inst_html, /* user_data = */ &cb_data);
190 else if (strncmp ("plugin:", term_lc, strlen ("plugin:")) == 0)
191 gl_search_field (GIF_PLUGIN, term_lc + strlen ("plugin:"),
192 print_graph_inst_html, /* user_data = */ &cb_data);
193 else if (strncmp ("plugin_instance:", term_lc, strlen ("plugin_instance:")) == 0)
194 gl_search_field (GIF_PLUGIN_INSTANCE, term_lc + strlen ("plugin_instance:"),
195 print_graph_inst_html, /* user_data = */ &cb_data);
196 else if (strncmp ("type:", term_lc, strlen ("type:")) == 0)
197 gl_search_field (GIF_TYPE, term_lc + strlen ("type:"),
198 print_graph_inst_html, /* user_data = */ &cb_data);
199 else if (strncmp ("type_instance:", term_lc, strlen ("type_instance:")) == 0)
200 gl_search_field (GIF_TYPE_INSTANCE, term_lc + strlen ("type_instance:"),
201 print_graph_inst_html, /* user_data = */ &cb_data);
202 else
203 gl_search (term_lc,
204 print_graph_inst_html, /* user_data = */ &cb_data);
206 free (term_lc);
207 }
209 if (cb_data.cfg != NULL)
210 printf (" </ul></li>\n");
212 if (cb_data.graph_more)
213 {
214 printf (" <li class=\"graph more\">More ...</li>\n");
215 }
217 printf (" </ul>\n");
219 return (0);
220 } /* }}} int print_search_result */
222 static int print_host_list_callback (const char *host, void *user_data) /* {{{ */
223 {
224 char *host_html;
226 /* Make compiler happy */
227 user_data = NULL;
229 if (host == NULL)
230 return (EINVAL);
232 host_html = html_escape (host);
233 if (host_html == NULL)
234 return (ENOMEM);
236 printf (" <li class=\"host\"><a href=\"%s?action=search;q=host:%s\">"
237 "%s</a></li>\n",
238 script_name (), host_html, host_html);
240 return (0);
241 } /* }}} int print_host_list_callback */
243 static int print_host_list (__attribute__((unused)) void *user_data) /* {{{ */
244 {
245 return (0);
246 printf ("<div><h3>List of hosts</h3>\n"
247 "<ul id=\"host-list\">\n");
248 gl_foreach_host (print_host_list_callback, /* user data = */ NULL);
249 printf ("</ul></div>\n");
251 return (0);
252 } /* }}} int print_host_list */
254 static int search_html (const char *term) /* {{{ */
255 {
256 page_data_t pg_data;
257 page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT;
258 char title[512];
260 if (term != NULL)
261 snprintf (title, sizeof (title), "Graphs matching \"%s\"",
262 term);
263 else
264 strncpy (title, "List of all graphs", sizeof (title));
265 title[sizeof (title) - 1] = 0;
267 memset (&pg_data, 0, sizeof (pg_data));
268 pg_data.search_term = term;
270 pg_callbacks.top_right = html_print_search_box;
271 pg_callbacks.middle_left = print_host_list;
272 pg_callbacks.middle_center = print_search_result;
274 html_print_page (title, &pg_callbacks, &pg_data);
276 return (0);
277 } /* }}} int search_html */
279 int action_search (void) /* {{{ */
280 {
281 char *search;
282 int status;
284 gl_update ();
286 search = strtolower_copy (param ("q"));
287 status = search_html (search);
288 free (search);
290 return (status);
291 } /* }}} int action_search */
293 /* vim: set sw=2 sts=2 et fdm=marker : */