2 * collection4 - action_show_graph.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_show_graph.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 OUTPUT_ERROR(...) do { \
44 printf ("Content-Type: text/plain\n\n"); \
45 printf (__VA_ARGS__); \
49 #define MAX_SHOW_GRAPHS 10
51 struct show_graph_data_s
54 const char *search_term;
56 typedef struct show_graph_data_s show_graph_data_t;
58 static int show_time_selector (__attribute__((unused)) void *user_data) /* {{{ */
62 pl = param_create (/* query string = */ NULL);
63 param_set (pl, "begin", NULL);
64 param_set (pl, "end", NULL);
65 param_set (pl, "button", NULL);
67 printf ("<form action=\"%s\" method=\"get\">\n", script_name ());
69 param_print_hidden (pl);
71 printf (" <select name=\"begin\">\n"
72 " <option value=\"-3600\">Hour</option>\n"
73 " <option value=\"-86400\">Day</option>\n"
74 " <option value=\"-604800\">Week</option>\n"
75 " <option value=\"-2678400\">Month</option>\n"
76 " <option value=\"-31622400\">Year</option>\n"
78 " <input type=\"submit\" name=\"button\" value=\"Go\" />\n");
85 } /* }}} int show_time_selector */
87 static int left_menu (void *user_data) /* {{{ */
89 show_graph_data_t *data = user_data;
91 printf ("\n<ul class=\"menu left\">\n");
93 if ((data->search_term != NULL) && (data->search_term[0] != 0))
97 memset (params, 0, sizeof (params));
98 graph_get_params (data->cfg, params, sizeof (params));
99 html_escape_buffer (params, sizeof (params));
101 printf (" <li><a href=\"%s?action=show_graph;%s\">"
102 "All instances</a></li>\n",
103 script_name (), params);
106 printf (" <li><a href=\"%s?action=list_graphs\">All graphs</a></li>\n"
111 } /* }}} int left_menu */
113 static int show_instance_cb (graph_instance_t *inst, /* {{{ */
116 show_graph_data_t *data = user_data;
120 if ((data->search_term != NULL) && (data->search_term[0] != 0))
123 char *term_lc = strtolower_copy (data->search_term);
125 if (strncmp ("host:", term_lc, strlen ("host:")) == 0)
127 if (inst_matches_field (inst, GIF_HOST, term_lc + strlen ("host:")))
130 else if (strncmp ("plugin:", term_lc, strlen ("plugin:")) == 0)
132 if (inst_matches_field (inst, GIF_PLUGIN, term_lc + strlen ("plugin:")))
135 else if (strncmp ("plugin_instance:", term_lc,
136 strlen ("plugin_instance:")) == 0)
138 if (inst_matches_field (inst, GIF_PLUGIN_INSTANCE,
139 term_lc + strlen ("plugin_instance:")))
142 else if (strncmp ("type:", term_lc, strlen ("type:")) == 0)
144 if (inst_matches_field (inst, GIF_TYPE, term_lc + strlen ("type:")))
147 else if (strncmp ("type_instance:", term_lc,
148 strlen ("type_instance:")) == 0)
150 if (inst_matches_field (inst, GIF_TYPE_INSTANCE,
151 term_lc + strlen ("type_instance:")))
154 else if (inst_matches_string (data->cfg, inst, term_lc))
163 } /* if (data->search_term) */
165 memset (descr, 0, sizeof (descr));
166 inst_describe (data->cfg, inst, descr, sizeof (descr));
167 html_escape_buffer (descr, sizeof (descr));
169 memset (params, 0, sizeof (params));
170 inst_get_params (data->cfg, inst, params, sizeof (params));
171 html_escape_buffer (params, sizeof (params));
173 printf (" <li class=\"instance\"><a href=\"%s?action=show_instance;%s\">"
175 script_name (), params, descr);
178 } /* }}} int show_instance_cb */
180 static int show_graph (void *user_data) /* {{{ */
182 show_graph_data_t *data = user_data;
184 if ((data->search_term == NULL) || (data->search_term[0] == 0))
185 printf ("<h2>All instances</h2>\n");
188 char *search_term_html = html_escape (data->search_term);
189 printf ("<h2>Instances matching "%s"</h2>\n",
191 free (search_term_html);
194 printf ("<ul class=\"instance_list\">\n");
195 graph_inst_foreach (data->cfg, show_instance_cb, data);
199 } /* }}} int show_graph */
201 int action_show_graph (void) /* {{{ */
203 page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT;
204 show_graph_data_t pg_data;
209 memset (&pg_data, 0, sizeof (pg_data));
210 pg_data.cfg = gl_graph_get_selected ();
211 if (pg_data.cfg == NULL)
212 OUTPUT_ERROR ("gl_graph_get_selected () failed.\n");
214 pg_data.search_term = param ("q");
216 memset (tmp, 0, sizeof (tmp));
217 graph_get_title (pg_data.cfg, tmp, sizeof (tmp));
218 snprintf (title, sizeof (title), "Graph \"%s\"", tmp);
219 title[sizeof (title) - 1] = 0;
221 pg_callbacks.top_right = html_print_search_box;
222 pg_callbacks.middle_left = left_menu;
223 pg_callbacks.middle_center = show_graph;
224 pg_callbacks.middle_right = show_time_selector;
226 html_print_page (title, &pg_callbacks, &pg_data);
229 } /* }}} int action_show_graph */
231 /* vim: set sw=2 sts=2 et fdm=marker : */