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"
41 #include <fcgi_stdio.h>
43 #define RESULT_LIMIT 50
45 struct callback_data_s
54 const char *search_term;
56 typedef struct callback_data_s callback_data_t;
58 static int left_menu (__attribute__((unused)) void *user_data) /* {{{ */
60 printf ("\n<ul class=\"menu left\">\n"
61 " <li><a href=\"%s?action=list_graphs\">All graphs</a></li>\n"
62 " <li><a href=\"%s?action=list_hosts\">All hosts</a></li>\n"
64 script_name (), script_name ());
67 } /* }}} int left_menu */
69 static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
70 graph_instance_t *inst,
73 callback_data_t *data = user_data;
80 if (data->graph_index >= data->graph_limit)
86 if (data->cfg != NULL)
87 printf (" </ul></li>\n");
89 memset (desc, 0, sizeof (desc));
90 graph_get_title (cfg, desc, sizeof (desc));
91 html_escape_buffer (desc, sizeof (desc));
93 printf (" <li class=\"graph\">%s\n"
94 " <ul class=\"instance_list\">\n", desc);
97 data->inst_index = -1;
102 if (data->inst_index >= data->inst_limit)
104 if (!data->inst_more)
106 char *search_term_html = html_escape (data->search_term);
107 char param_search_term[1024];
109 memset (params, 0, sizeof (params));
110 graph_get_params (cfg, params, sizeof (params));
111 html_escape_buffer (params, sizeof (params));
113 param_search_term[0] = 0;
114 if (search_term_html != NULL)
116 snprintf (param_search_term, sizeof (param_search_term), ";q=%s",
118 param_search_term[sizeof (param_search_term) - 1] = 0;
121 free (search_term_html);
123 printf (" <li class=\"instance more\"><a href=\"%s"
124 "?action=show_graph;%s%s\">More …</a></li>\n",
125 script_name (), params, param_search_term);
132 memset (params, 0, sizeof (params));
133 inst_get_params (cfg, inst, params, sizeof (params));
134 html_escape_buffer (params, sizeof (params));
136 memset (desc, 0, sizeof (desc));
137 inst_describe (cfg, inst, desc, sizeof (desc));
138 html_escape_buffer (desc, sizeof (desc));
140 printf (" <li class=\"instance\"><a href=\"%s?action=show_instance;%s\">%s</a></li>\n",
141 script_name (), params, desc);
144 } /* }}} int print_graph_inst_html */
148 const char *search_term;
150 typedef struct page_data_s page_data_t;
152 static int print_search_result (void *user_data) /* {{{ */
154 page_data_t *pg_data = user_data;
155 callback_data_t cb_data = { /* cfg = */ NULL,
156 /* graph_index = */ -1, /* graph_limit = */ 20, /* graph_more = */ 0,
157 /* inst_index = */ -1, /* inst_limit = */ 5, /* inst more = */ 0,
158 /* search_term = */ pg_data->search_term };
160 char *search_term_html;
162 assert (pg_data->search_term != NULL);
164 search_term_html = html_escape (pg_data->search_term);
165 printf (" <h2>Search results for "%s"</h2>\n",
167 free (search_term_html);
169 printf (" <ul id=\"search-output\" class=\"graph_list\">\n");
171 term_lc = strtolower_copy (pg_data->search_term);
173 if (strncmp ("host:", term_lc, strlen ("host:")) == 0)
174 gl_search_field (GIF_HOST, term_lc + strlen ("host:"),
175 print_graph_inst_html, /* user_data = */ &cb_data);
176 else if (strncmp ("plugin:", term_lc, strlen ("plugin:")) == 0)
177 gl_search_field (GIF_PLUGIN, term_lc + strlen ("plugin:"),
178 print_graph_inst_html, /* user_data = */ &cb_data);
179 else if (strncmp ("plugin_instance:", term_lc, strlen ("plugin_instance:")) == 0)
180 gl_search_field (GIF_PLUGIN_INSTANCE, term_lc + strlen ("plugin_instance:"),
181 print_graph_inst_html, /* user_data = */ &cb_data);
182 else if (strncmp ("type:", term_lc, strlen ("type:")) == 0)
183 gl_search_field (GIF_TYPE, term_lc + strlen ("type:"),
184 print_graph_inst_html, /* user_data = */ &cb_data);
185 else if (strncmp ("type_instance:", term_lc, strlen ("type_instance:")) == 0)
186 gl_search_field (GIF_TYPE_INSTANCE, term_lc + strlen ("type_instance:"),
187 print_graph_inst_html, /* user_data = */ &cb_data);
190 print_graph_inst_html, /* user_data = */ &cb_data);
194 if (cb_data.cfg != NULL)
195 printf (" </ul></li>\n");
197 if (cb_data.graph_more)
199 printf (" <li class=\"graph more\">More ...</li>\n");
205 } /* }}} int print_search_result */
207 static int print_search_form (void *user_data) /* {{{ */
209 page_data_t *pg_data = user_data;
210 char search_term_html[1024];
212 if (pg_data->search_term != NULL)
214 html_escape_copy (search_term_html, pg_data->search_term,
215 sizeof (search_term_html));
219 search_term_html[0] = 0;
222 printf ("<form action=\"%s\" method=\"get\">\n"
223 " <input type=\"hidden\" name=\"action\" value=\"search\" />\n"
225 " <legend>Advanced search</legend>\n"
227 " <span class=\"nbr\">\n"
228 " <input type=\"checkbox\" name=\"sfh\" value=\"1\" checked=\"checked\" id=\"search_for_host\" />\n"
229 " <label for=\"search_for_host\">Host</label>\n"
231 " <span class=\"nbr\">\n"
232 " <input type=\"checkbox\" name=\"sfp\" value=\"1\" checked=\"checked\" id=\"search_for_plugin\" />\n"
233 " <label for=\"search_for_plugin\">Plugin</label>\n"
235 " <span class=\"nbr\">\n"
236 " <input type=\"checkbox\" name=\"sfpi\" value=\"1\" checked=\"checked\" id=\"search_for_plugin_instance\" />\n"
237 " <label for=\"search_for_plugin_instance\">Plugin instance</label>\n"
239 " <span class=\"nbr\">\n"
240 " <input type=\"checkbox\" name=\"sft\" value=\"1\" checked=\"checked\" id=\"search_for_type\" />\n"
241 " <label for=\"search_for_type\">Type</label>\n"
243 " <span class=\"nbr\">\n"
244 " <input type=\"checkbox\" name=\"sfti\" value=\"1\" checked=\"checked\" id=\"search_for_type_instance\" />\n"
245 " <label for=\"search_for_type_instance\">Type instance</label>\n"
248 " <div>Search for <input type=\"text\" name=\"q\" value=\"%s\" size=\"50\" /></div>\n"
249 " <input type=\"submit\" name=\"button\" value=\"Advanced search\" />"
252 script_name (), search_term_html);
255 } /* }}} int print_search_form */
257 static int search_html (const char *term) /* {{{ */
260 page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT;
264 snprintf (title, sizeof (title), "Graphs matching \"%s\"",
267 strncpy (title, "Search", sizeof (title));
268 title[sizeof (title) - 1] = 0;
270 memset (&pg_data, 0, sizeof (pg_data));
271 pg_data.search_term = term;
273 pg_callbacks.top_right = html_print_search_box;
274 pg_callbacks.middle_left = left_menu;
276 pg_callbacks.middle_center = print_search_result;
278 pg_callbacks.middle_center = print_search_form;
280 html_print_page (title, &pg_callbacks, &pg_data);
283 } /* }}} int search_html */
285 int action_search (void) /* {{{ */
292 search = strtolower_copy (param ("q"));
293 if ((search != NULL) && (search[0] == 0))
298 status = search_html (search);
302 } /* }}} int action_search */
304 /* vim: set sw=2 sts=2 et fdm=marker : */