2 * collection4 - action_search.c
3 * Copyright (C) 2010 Florian octo Forster
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.
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.
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
21 * Florian octo Forster <ff at octo.it>
32 #include "action_search.h"
35 #include "graph_ident.h"
36 #include "graph_instance.h"
37 #include "graph_list.h"
38 #include "utils_cgi.h"
39 #include "utils_search.h"
42 #include <fcgi_stdio.h>
44 #define RESULT_LIMIT 50
46 struct callback_data_s
55 const char *search_term;
57 typedef struct callback_data_s callback_data_t;
59 static int left_menu (__attribute__((unused)) void *user_data) /* {{{ */
61 printf ("\n<ul class=\"menu left\">\n"
62 " <li><a href=\"%s?action=list_graphs\">All graphs</a></li>\n"
63 " <li><a href=\"%s?action=list_hosts\">All hosts</a></li>\n"
65 script_name (), script_name ());
68 } /* }}} int left_menu */
70 static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
71 graph_instance_t *inst,
74 callback_data_t *data = user_data;
81 if (data->graph_index >= data->graph_limit)
87 if (data->cfg != NULL)
88 printf (" </ul></li>\n");
90 memset (desc, 0, sizeof (desc));
91 graph_get_title (cfg, desc, sizeof (desc));
92 html_escape_buffer (desc, sizeof (desc));
94 printf (" <li class=\"graph\">%s\n"
95 " <ul class=\"instance_list\">\n", desc);
98 data->inst_index = -1;
103 if (data->inst_index >= data->inst_limit)
105 if (!data->inst_more)
107 char *search_term_html = html_escape (data->search_term);
108 char param_search_term[1024];
110 memset (params, 0, sizeof (params));
111 graph_get_params (cfg, params, sizeof (params));
112 html_escape_buffer (params, sizeof (params));
114 param_search_term[0] = 0;
115 if (search_term_html != NULL)
117 snprintf (param_search_term, sizeof (param_search_term), ";q=%s",
119 param_search_term[sizeof (param_search_term) - 1] = 0;
122 free (search_term_html);
124 printf (" <li class=\"instance more\"><a href=\"%s"
125 "?action=show_graph;%s%s\">More …</a></li>\n",
126 script_name (), params, param_search_term);
133 memset (params, 0, sizeof (params));
134 inst_get_params (cfg, inst, params, sizeof (params));
135 html_escape_buffer (params, sizeof (params));
137 memset (desc, 0, sizeof (desc));
138 inst_describe (cfg, inst, desc, sizeof (desc));
139 html_escape_buffer (desc, sizeof (desc));
141 printf (" <li class=\"instance\"><a href=\"%s?action=show_instance;%s\">%s</a></li>\n",
142 script_name (), params, desc);
145 } /* }}} int print_graph_inst_html */
150 search_info_t *search_info;
152 typedef struct page_data_s page_data_t;
154 static int print_search_result (void *user_data) /* {{{ */
156 page_data_t *pg_data = user_data;
157 callback_data_t cb_data = { /* cfg = */ NULL,
158 /* graph_index = */ -1, /* graph_limit = */ 20, /* graph_more = */ 0,
159 /* inst_index = */ -1, /* inst_limit = */ 5, /* inst more = */ 0,
160 /* search_term = */ pg_data->search_term };
161 char *search_term_html;
163 assert (pg_data->search_term != NULL);
165 search_term_html = html_escape (pg_data->search_term);
166 printf (" <h2>Search results for "%s"</h2>\n",
168 free (search_term_html);
170 printf (" <ul id=\"search-output\" class=\"graph_list\">\n");
172 gl_search (pg_data->search_info, print_graph_inst_html,
173 /* user_data = */ &cb_data);
176 if (cb_data.cfg != NULL)
177 printf (" </ul></li>\n");
179 if (cb_data.graph_more)
181 printf (" <li class=\"graph more\">More ...</li>\n");
187 } /* }}} int print_search_result */
189 static int print_search_form (void *user_data) /* {{{ */
191 page_data_t *pg_data = user_data;
192 char search_term_html[1024];
194 if (pg_data->search_term != NULL)
196 html_escape_copy (search_term_html, pg_data->search_term,
197 sizeof (search_term_html));
201 search_term_html[0] = 0;
204 printf ("<form action=\"%s\" method=\"get\">\n"
205 " <input type=\"hidden\" name=\"action\" value=\"search\" />\n"
207 " <legend>Advanced search</legend>\n"
209 " <span class=\"nbr\">\n"
210 " <input type=\"checkbox\" name=\"sfh\" value=\"1\" checked=\"checked\" id=\"search_for_host\" />\n"
211 " <label for=\"search_for_host\">Host</label>\n"
213 " <span class=\"nbr\">\n"
214 " <input type=\"checkbox\" name=\"sfp\" value=\"1\" checked=\"checked\" id=\"search_for_plugin\" />\n"
215 " <label for=\"search_for_plugin\">Plugin</label>\n"
217 " <span class=\"nbr\">\n"
218 " <input type=\"checkbox\" name=\"sfpi\" value=\"1\" checked=\"checked\" id=\"search_for_plugin_instance\" />\n"
219 " <label for=\"search_for_plugin_instance\">Plugin instance</label>\n"
221 " <span class=\"nbr\">\n"
222 " <input type=\"checkbox\" name=\"sft\" value=\"1\" checked=\"checked\" id=\"search_for_type\" />\n"
223 " <label for=\"search_for_type\">Type</label>\n"
225 " <span class=\"nbr\">\n"
226 " <input type=\"checkbox\" name=\"sfti\" value=\"1\" checked=\"checked\" id=\"search_for_type_instance\" />\n"
227 " <label for=\"search_for_type_instance\">Type instance</label>\n"
230 " <div>Search for <input type=\"text\" name=\"q\" value=\"%s\" size=\"50\" /></div>\n"
231 " <input type=\"submit\" name=\"button\" value=\"Advanced search\" />"
234 script_name (), search_term_html);
237 } /* }}} int print_search_form */
239 static int search_html (page_data_t *pg_data) /* {{{ */
241 page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT;
244 if (pg_data->search_term != NULL)
245 snprintf (title, sizeof (title), "Graphs matching \"%s\"",
246 pg_data->search_term);
248 strncpy (title, "Search", sizeof (title));
249 title[sizeof (title) - 1] = 0;
251 pg_callbacks.top_right = html_print_search_box;
252 pg_callbacks.middle_left = left_menu;
253 if (pg_data->search_term != NULL)
254 pg_callbacks.middle_center = print_search_result;
256 pg_callbacks.middle_center = print_search_form;
258 html_print_page (title, &pg_callbacks, pg_data);
261 } /* }}} int search_html */
263 int action_search (void) /* {{{ */
270 pg_data.search_term = strtolower_copy (param ("q"));
271 if ((pg_data.search_term != NULL) && (pg_data.search_term[0] == 0))
273 free (pg_data.search_term);
274 pg_data.search_term = NULL;
275 pg_data.search_info = NULL;
279 pg_data.search_info = search_parse (pg_data.search_term);
282 status = search_html (&pg_data);
284 free (pg_data.search_term);
285 search_destroy (pg_data.search_info);
288 } /* }}} int action_search */
290 /* vim: set sw=2 sts=2 et fdm=marker : */