07404243a9430c2edceebe1caf4105c306ce1b57
[collection4.git] / src / action_list_graphs.c
1 /**
2  * collection4 - action_list_graphs.c
3  * Copyright (C) 2010  Florian octo Forster
4  * 
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.
9  * 
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.
14  * 
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
19  *
20  * Authors:
21  *   Florian octo Forster <ff at octo.it>
22  **/
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <errno.h>
28
29 #include "action_list_graphs.h"
30 #include "common.h"
31 #include "graph.h"
32 #include "graph_ident.h"
33 #include "graph_instance.h"
34 #include "graph_list.h"
35 #include "utils_cgi.h"
36
37 #include <fcgiapp.h>
38 #include <fcgi_stdio.h>
39
40 #define RESULT_LIMIT 50
41
42 struct callback_data_s
43 {
44   graph_config_t *cfg;
45   int graph_index;
46   int graph_limit;
47   _Bool graph_more;
48   int inst_index;
49   int inst_limit;
50   _Bool inst_more;
51 };
52 typedef struct callback_data_s callback_data_t;
53
54 static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
55     graph_instance_t *inst,
56     void *user_data)
57 {
58   callback_data_t *data = user_data;
59   char params[1024];
60   char desc[1024];
61
62   if (data->cfg != cfg)
63   {
64     data->graph_index++;
65     if (data->graph_index >= data->graph_limit)
66     {
67       data->graph_more = 1;
68       return (1);
69     }
70
71     if (data->cfg != NULL)
72       printf ("  </ul></li>\n");
73
74     memset (desc, 0, sizeof (desc));
75     graph_get_title (cfg, desc, sizeof (desc));
76     html_escape_buffer (desc, sizeof (desc));
77
78     printf ("  <li class=\"graph\">%s\n"
79         "  <ul class=\"instance_list\">\n", desc);
80
81     data->cfg = cfg;
82     data->inst_index = -1;
83     data->inst_more = 0;
84   }
85
86   data->inst_index++;
87   if (data->inst_index >= data->inst_limit)
88   {
89     if (!data->inst_more)
90     {
91       memset (params, 0, sizeof (params));
92       graph_get_params (cfg, params, sizeof (params));
93       html_escape_buffer (params, sizeof (params));
94
95       printf ("    <li class=\"instance more\"><a href=\"%s"
96           "?action=show_graph;%s\">More &#x2026;</a></li>\n",
97           script_name (), params);
98
99       data->inst_more = 1;
100     }
101     return (0);
102   }
103
104   memset (params, 0, sizeof (params));
105   inst_get_params (cfg, inst, params, sizeof (params));
106   html_escape_buffer (params, sizeof (params));
107
108   memset (desc, 0, sizeof (desc));
109   inst_describe (cfg, inst, desc, sizeof (desc));
110   html_escape_buffer (desc, sizeof (desc));
111
112   printf ("    <li class=\"instance\"><a href=\"%s?action=show_instance;%s\">%s</a></li>\n",
113       script_name (), params, desc);
114
115   return (0);
116 } /* }}} int print_graph_inst_html */
117
118 struct page_data_s
119 {
120   const char *search_term;
121 };
122 typedef struct page_data_s page_data_t;
123
124 static int print_search_result (void *user_data) /* {{{ */
125 {
126   page_data_t *pg_data = user_data;
127   callback_data_t cb_data = { /* cfg = */ NULL,
128     /* graph_index = */ -1, /* graph_limit = */ 20, /* graph_more = */ 0,
129     /* inst_index = */  -1, /* inst_limit = */   5, /* inst more = */  0 };
130
131   if (pg_data->search_term != NULL)
132   {
133     char *search_term_html = html_escape (pg_data->search_term);
134     printf ("    <h2>Search results for &quot;%s&quot;</h2>\n",
135         search_term_html);
136     free (search_term_html);
137   }
138
139   printf ("    <ul id=\"search-output\" class=\"graph_list\">\n");
140   if (pg_data->search_term == NULL)
141     gl_instance_get_all (print_graph_inst_html, /* user_data = */ &cb_data);
142   else
143   {
144     char *term_lc = strtolower_copy (pg_data->search_term);
145
146     if (strncmp ("host:", term_lc, strlen ("host:")) == 0)
147       gl_search_field (GIF_HOST, term_lc + strlen ("host:"),
148           print_graph_inst_html, /* user_data = */ &cb_data);
149     else if (strncmp ("plugin:", term_lc, strlen ("plugin:")) == 0)
150       gl_search_field (GIF_PLUGIN, term_lc + strlen ("plugin:"),
151           print_graph_inst_html, /* user_data = */ &cb_data);
152     else if (strncmp ("plugin_instance:", term_lc, strlen ("plugin_instance:")) == 0)
153       gl_search_field (GIF_PLUGIN_INSTANCE, term_lc + strlen ("plugin_instance:"),
154           print_graph_inst_html, /* user_data = */ &cb_data);
155     else if (strncmp ("type:", term_lc, strlen ("type:")) == 0)
156       gl_search_field (GIF_TYPE, term_lc + strlen ("type:"),
157           print_graph_inst_html, /* user_data = */ &cb_data);
158     else if (strncmp ("type_instance:", term_lc, strlen ("type_instance:")) == 0)
159       gl_search_field (GIF_TYPE_INSTANCE, term_lc + strlen ("type_instance:"),
160           print_graph_inst_html, /* user_data = */ &cb_data);
161     else
162       gl_search (term_lc,
163           print_graph_inst_html, /* user_data = */ &cb_data);
164
165     free (term_lc);
166   }
167
168   if (cb_data.cfg != NULL)
169     printf ("      </ul></li>\n");
170
171   if (cb_data.graph_more)
172   {
173     printf ("    <li class=\"graph more\">More ...</li>\n");
174   }
175
176   printf ("    </ul>\n");
177
178   return (0);
179 } /* }}} int print_search_result */
180
181 static int print_host_list_callback (const char *host, void *user_data) /* {{{ */
182 {
183   char *host_html;
184
185   /* Make compiler happy */
186   user_data = NULL;
187
188   if (host == NULL)
189     return (EINVAL);
190   
191   host_html = html_escape (host);
192   if (host_html == NULL)
193     return (ENOMEM);
194
195   printf ("  <li class=\"host\"><a href=\"%s?action=list_graphs;q=host:%s\">"
196       "%s</a></li>\n",
197       script_name (), host_html, host_html);
198
199   return (0);
200 } /* }}} int print_host_list_callback */
201
202 static int print_host_list (__attribute__((unused)) void *user_data) /* {{{ */
203 {
204   printf ("<div><h3>List of hosts</h3>\n"
205       "<ul id=\"host-list\">\n");
206   gl_foreach_host (print_host_list_callback, /* user data = */ NULL);
207   printf ("</ul></div>\n");
208
209   return (0);
210 } /* }}} int print_host_list */
211
212 static int list_graphs_html (const char *term) /* {{{ */
213 {
214   page_data_t pg_data;
215   page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT;
216   char title[512];
217
218   if (term != NULL)
219     snprintf (title, sizeof (title), "c4: Graphs matching \"%s\"", term);
220   else
221     strncpy (title, "c4: List of all graphs", sizeof (title));
222   title[sizeof (title) - 1] = 0;
223
224   memset (&pg_data, 0, sizeof (pg_data));
225   pg_data.search_term = term;
226
227   pg_callbacks.top_right = html_print_search_box;
228   pg_callbacks.middle_left = print_host_list;
229   pg_callbacks.middle_center = print_search_result;
230
231   html_print_page (title, &pg_callbacks, &pg_data);
232
233   return (0);
234 } /* }}} int list_graphs_html */
235
236 int action_list_graphs (void) /* {{{ */
237 {
238   char *search;
239   int status;
240
241   gl_update ();
242
243   search = strtolower_copy (param ("q"));
244   status = list_graphs_html (search);
245   free (search);
246
247   return (status);
248 } /* }}} int action_list_graphs */
249
250 /* vim: set sw=2 sts=2 et fdm=marker : */