2 * collection4 - action_list_graphs.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>
29 #include "action_list_graphs.h"
32 #include "graph_ident.h"
33 #include "graph_instance.h"
34 #include "graph_list.h"
35 #include "utils_cgi.h"
38 #include <fcgi_stdio.h>
40 #define RESULT_LIMIT 50
42 struct callback_data_s
52 typedef struct callback_data_s callback_data_t;
54 static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
55 graph_instance_t *inst,
58 callback_data_t *data = user_data;
65 if (data->graph_index >= data->graph_limit)
71 if (data->cfg != NULL)
72 printf (" </ul></li>\n");
74 memset (desc, 0, sizeof (desc));
75 graph_get_title (cfg, desc, sizeof (desc));
76 html_escape_buffer (desc, sizeof (desc));
78 printf (" <li class=\"graph\">%s\n"
79 " <ul class=\"instance_list\">\n", desc);
82 data->inst_index = -1;
87 if (data->inst_index >= data->inst_limit)
91 memset (params, 0, sizeof (params));
92 graph_get_params (cfg, params, sizeof (params));
93 html_escape_buffer (params, sizeof (params));
95 printf (" <li class=\"instance more\"><a href=\"%s"
96 "?action=show_graph;%s\">More …</a></li>\n",
97 script_name (), params);
104 memset (params, 0, sizeof (params));
105 inst_get_params (cfg, inst, params, sizeof (params));
106 html_escape_buffer (params, sizeof (params));
108 memset (desc, 0, sizeof (desc));
109 inst_describe (cfg, inst, desc, sizeof (desc));
110 html_escape_buffer (desc, sizeof (desc));
112 printf (" <li class=\"instance\"><a href=\"%s?action=show_instance;%s\">%s</a></li>\n",
113 script_name (), params, desc);
116 } /* }}} int print_graph_inst_html */
118 static int print_graph_html (graph_config_t *cfg, /* {{{ */
119 __attribute__((unused)) void *user_data)
124 memset (title, 0, sizeof (title));
125 graph_get_title (cfg, title, sizeof (title));
126 html_escape_buffer (title, sizeof (title));
128 memset (params, 0, sizeof (params));
129 graph_get_params (cfg, params, sizeof (params));
130 html_escape_buffer (params, sizeof (params));
132 printf (" <li class=\"graph\"><a href=\"%s?action=show_graph;%s\">"
134 script_name (), params, title);
137 } /* }}} int print_graph_html */
141 const char *search_term;
143 typedef struct page_data_s page_data_t;
145 static int print_search_result (void *user_data) /* {{{ */
147 page_data_t *pg_data = user_data;
148 callback_data_t cb_data = { /* cfg = */ NULL,
149 /* graph_index = */ -1, /* graph_limit = */ 20, /* graph_more = */ 0,
150 /* inst_index = */ -1, /* inst_limit = */ 5, /* inst more = */ 0 };
152 if (pg_data->search_term != NULL)
154 char *search_term_html = html_escape (pg_data->search_term);
155 printf (" <h2>Search results for "%s"</h2>\n",
157 free (search_term_html);
160 printf (" <ul id=\"search-output\" class=\"graph_list\">\n");
161 if (pg_data->search_term == NULL)
163 gl_graph_get_all (print_graph_html, /* user_data = */ &cb_data);
167 char *term_lc = strtolower_copy (pg_data->search_term);
169 if (strncmp ("host:", term_lc, strlen ("host:")) == 0)
170 gl_search_field (GIF_HOST, term_lc + strlen ("host:"),
171 print_graph_inst_html, /* user_data = */ &cb_data);
172 else if (strncmp ("plugin:", term_lc, strlen ("plugin:")) == 0)
173 gl_search_field (GIF_PLUGIN, term_lc + strlen ("plugin:"),
174 print_graph_inst_html, /* user_data = */ &cb_data);
175 else if (strncmp ("plugin_instance:", term_lc, strlen ("plugin_instance:")) == 0)
176 gl_search_field (GIF_PLUGIN_INSTANCE, term_lc + strlen ("plugin_instance:"),
177 print_graph_inst_html, /* user_data = */ &cb_data);
178 else if (strncmp ("type:", term_lc, strlen ("type:")) == 0)
179 gl_search_field (GIF_TYPE, term_lc + strlen ("type:"),
180 print_graph_inst_html, /* user_data = */ &cb_data);
181 else if (strncmp ("type_instance:", term_lc, strlen ("type_instance:")) == 0)
182 gl_search_field (GIF_TYPE_INSTANCE, term_lc + strlen ("type_instance:"),
183 print_graph_inst_html, /* user_data = */ &cb_data);
186 print_graph_inst_html, /* user_data = */ &cb_data);
191 if (cb_data.cfg != NULL)
192 printf (" </ul></li>\n");
194 if (cb_data.graph_more)
196 printf (" <li class=\"graph more\">More ...</li>\n");
202 } /* }}} int print_search_result */
204 static int print_host_list_callback (const char *host, void *user_data) /* {{{ */
208 /* Make compiler happy */
214 host_html = html_escape (host);
215 if (host_html == NULL)
218 printf (" <li class=\"host\"><a href=\"%s?action=list_graphs;q=host:%s\">"
220 script_name (), host_html, host_html);
223 } /* }}} int print_host_list_callback */
225 static int print_host_list (__attribute__((unused)) void *user_data) /* {{{ */
227 printf ("<div><h3>List of hosts</h3>\n"
228 "<ul id=\"host-list\">\n");
229 gl_foreach_host (print_host_list_callback, /* user data = */ NULL);
230 printf ("</ul></div>\n");
233 } /* }}} int print_host_list */
235 static int list_graphs_html (const char *term) /* {{{ */
238 page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT;
242 snprintf (title, sizeof (title), "c4: Graphs matching \"%s\"", term);
244 strncpy (title, "c4: List of all graphs", sizeof (title));
245 title[sizeof (title) - 1] = 0;
247 memset (&pg_data, 0, sizeof (pg_data));
248 pg_data.search_term = term;
250 pg_callbacks.top_right = html_print_search_box;
251 pg_callbacks.middle_left = print_host_list;
252 pg_callbacks.middle_center = print_search_result;
254 html_print_page (title, &pg_callbacks, &pg_data);
257 } /* }}} int list_graphs_html */
259 int action_list_graphs (void) /* {{{ */
266 search = strtolower_copy (param ("q"));
267 status = list_graphs_html (search);
271 } /* }}} int action_list_graphs */
273 /* vim: set sw=2 sts=2 et fdm=marker : */