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 printf (" <li class=\"instance more\">More ...</li>\n");
97 memset (params, 0, sizeof (params));
98 inst_get_params (cfg, inst, params, sizeof (params));
99 html_escape_buffer (params, sizeof (params));
101 memset (desc, 0, sizeof (desc));
102 inst_describe (cfg, inst, desc, sizeof (desc));
103 html_escape_buffer (desc, sizeof (desc));
105 printf (" <li class=\"instance\"><a href=\"%s?action=show_instance;%s\">%s</a></li>\n",
106 script_name (), params, desc);
109 } /* }}} int print_graph_inst_html */
113 const char *search_term;
115 typedef struct page_data_s page_data_t;
117 static int print_search_result (void *user_data) /* {{{ */
119 page_data_t *pg_data = user_data;
120 callback_data_t cb_data = { /* cfg = */ NULL,
121 /* graph_index = */ -1, /* graph_limit = */ 20, /* graph_more = */ 0,
122 /* inst_index = */ -1, /* inst_limit = */ 5, /* inst more = */ 0 };
124 if (pg_data->search_term != NULL)
126 char *search_term_html = html_escape (pg_data->search_term);
127 printf (" <h2>Search results for "%s"</h2>\n",
129 free (search_term_html);
132 printf (" <ul id=\"search-output\" class=\"graph_list\">\n");
133 if (pg_data->search_term == NULL)
134 gl_instance_get_all (print_graph_inst_html, /* user_data = */ &cb_data);
137 char *term_lc = strtolower_copy (pg_data->search_term);
139 if (strncmp ("host:", term_lc, strlen ("host:")) == 0)
140 gl_search_field (GIF_HOST, term_lc + strlen ("host:"),
141 print_graph_inst_html, /* user_data = */ &cb_data);
142 else if (strncmp ("plugin:", term_lc, strlen ("plugin:")) == 0)
143 gl_search_field (GIF_PLUGIN, term_lc + strlen ("plugin:"),
144 print_graph_inst_html, /* user_data = */ &cb_data);
145 else if (strncmp ("plugin_instance:", term_lc, strlen ("plugin_instance:")) == 0)
146 gl_search_field (GIF_PLUGIN_INSTANCE, term_lc + strlen ("plugin_instance:"),
147 print_graph_inst_html, /* user_data = */ &cb_data);
148 else if (strncmp ("type:", term_lc, strlen ("type:")) == 0)
149 gl_search_field (GIF_TYPE, term_lc + strlen ("type:"),
150 print_graph_inst_html, /* user_data = */ &cb_data);
151 else if (strncmp ("type_instance:", term_lc, strlen ("type_instance:")) == 0)
152 gl_search_field (GIF_TYPE_INSTANCE, term_lc + strlen ("type_instance:"),
153 print_graph_inst_html, /* user_data = */ &cb_data);
156 print_graph_inst_html, /* user_data = */ &cb_data);
161 if (cb_data.cfg != NULL)
162 printf (" </ul></li>\n");
164 if (cb_data.graph_more)
166 printf (" <li class=\"graph more\">More ...</li>\n");
172 } /* }}} int print_search_result */
174 static int print_host_list_callback (const char *host, void *user_data) /* {{{ */
178 /* Make compiler happy */
184 host_html = html_escape (host);
185 if (host_html == NULL)
188 printf (" <li class=\"host\"><a href=\"%s?action=list_graphs;q=host:%s\">"
190 script_name (), host_html, host_html);
193 } /* }}} int print_host_list_callback */
195 static int print_host_list (__attribute__((unused)) void *user_data) /* {{{ */
197 printf ("<div><h3>List of hosts</h3>\n"
198 "<ul id=\"host-list\">\n");
199 gl_foreach_host (print_host_list_callback, /* user data = */ NULL);
200 printf ("</ul></div>\n");
203 } /* }}} int print_host_list */
205 static int list_graphs_html (const char *term) /* {{{ */
208 page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT;
212 snprintf (title, sizeof (title), "c4: Graphs matching \"%s\"", term);
214 strncpy (title, "c4: List of all graphs", sizeof (title));
215 title[sizeof (title) - 1] = 0;
217 memset (&pg_data, 0, sizeof (pg_data));
218 pg_data.search_term = term;
220 pg_callbacks.top_right = html_print_search_box;
221 pg_callbacks.middle_left = print_host_list;
222 pg_callbacks.middle_center = print_search_result;
224 html_print_page (title, &pg_callbacks, &pg_data);
227 } /* }}} int list_graphs_html */
229 int action_list_graphs (void) /* {{{ */
236 search = strtolower_copy (param ("q"));
237 status = list_graphs_html (search);
241 } /* }}} int action_list_graphs */
243 /* vim: set sw=2 sts=2 et fdm=marker : */