"show instance" action: Limit displayed graphs to 10 and re-activate the "breadcrump".
[collection4.git] / src / action_show_instance.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <stdint.h>
6 #include <inttypes.h>
7 #include <assert.h>
8
9 #include "action_show_instance.h"
10 #include "common.h"
11 #include "graph.h"
12 #include "graph_ident.h"
13 #include "graph_instance.h"
14 #include "graph_list.h"
15 #include "utils_cgi.h"
16
17 #include <fcgiapp.h>
18 #include <fcgi_stdio.h>
19
20 #define OUTPUT_ERROR(...) do {             \
21   printf ("Content-Type: text/plain\n\n"); \
22   printf (__VA_ARGS__);                    \
23   return (0);                              \
24 } while (0)
25
26 #define MAX_SHOW_GRAPHS 10
27
28 struct show_graph_data_s
29 {
30   graph_config_t *cfg;
31   graph_instance_t *inst;
32   int graph_count;
33 };
34 typedef struct show_graph_data_s show_graph_data_t;
35
36 static void show_breadcrump_field (const char *str, /* {{{ */
37     const char *field_name)
38 {
39   if ((str == NULL) || (str[0] == 0))
40     printf ("<em>none</em>");
41   else if (IS_ANY (str))
42     printf ("<em>any</em>");
43   else if (IS_ALL (str))
44     printf ("<em>all</em>");
45   else
46   {
47     char *str_html = html_escape (str);
48
49     if (field_name != NULL)
50       printf ("<a href=\"%s?action=list_graphs;q=%s:%s\">%s</a>",
51           script_name (), field_name, str_html, str_html);
52     else
53       printf ("<a href=\"%s?action=list_graphs;q=%s\">%s</a>",
54           script_name (), str_html, str_html);
55
56     free (str_html);
57   }
58 } /* }}} void show_breadcrump_field */
59
60 static int show_breadcrump (graph_config_t *cfg, /* {{{ */
61     graph_instance_t *inst)
62 {
63   graph_ident_t *ident;
64   char *prefix;
65
66   if (inst != NULL)
67   {
68     prefix = "Instance";
69     ident = inst_get_selector (inst);
70   }
71   else
72   {
73     prefix = "Graph";
74     ident = graph_get_selector (cfg);
75   }
76
77   printf ("<div class=\"breadcrump\">%s: &quot;", prefix);
78   show_breadcrump_field (ident_get_host (ident), "host");
79   printf ("&nbsp;/ ");
80   show_breadcrump_field (ident_get_plugin (ident), "plugin");
81   printf ("&nbsp;&ndash; ");
82   show_breadcrump_field (ident_get_plugin_instance (ident), "plugin_instance");
83   printf ("&nbsp;/ ");
84   show_breadcrump_field (ident_get_type (ident), "type");
85   printf ("&nbsp;&ndash; ");
86   show_breadcrump_field (ident_get_type_instance (ident), "type_instance");
87   printf ("&quot;</div>\n");
88
89   ident_destroy (ident);
90   return (0);
91 } /* }}} int show_breadcrump */
92
93 static int show_time_selector (__attribute__((unused)) void *user_data) /* {{{ */
94 {
95   param_list_t *pl;
96
97   pl = param_create (/* query string = */ NULL);
98   param_set (pl, "begin", NULL);
99   param_set (pl, "end", NULL);
100   param_set (pl, "button", NULL);
101
102   printf ("<form action=\"%s\" method=\"get\">\n", script_name ());
103
104   param_print_hidden (pl);
105
106   printf ("  <select name=\"begin\">\n"
107       "    <option value=\"-3600\">Hour</option>\n"
108       "    <option value=\"-86400\">Day</option>\n"
109       "    <option value=\"-604800\">Week</option>\n"
110       "    <option value=\"-2678400\">Month</option>\n"
111       "    <option value=\"-31622400\">Year</option>\n"
112       "  </select>\n"
113       "  <input type=\"submit\" name=\"button\" value=\"Go\" />\n");
114
115   printf ("</form>\n");
116
117   param_destroy (pl);
118
119   return (0);
120 } /* }}} int show_time_selector */
121
122 static int show_instance_list_cb (graph_instance_t *inst, /* {{{ */
123     void *user_data)
124 {
125   show_graph_data_t *data = user_data;
126   char descr[128];
127   char params[1024];
128
129   memset (descr, 0, sizeof (descr));
130   inst_describe (data->cfg, inst, descr, sizeof (descr));
131   html_escape_buffer (descr, sizeof (descr));
132
133   if (inst == data->inst)
134   {
135     printf ("    <li class=\"instance\"><strong>%s</strong></li>\n", descr);
136     return (0);
137   }
138
139   memset (params, 0, sizeof (params));
140   inst_get_params (data->cfg, inst, params, sizeof (params));
141   html_escape_buffer (params, sizeof (params));
142
143   printf ("    <li class=\"instance\">"
144       "<a href=\"%s?action=show_instance;%s\">%s</a></li>\n",
145       script_name (), params, descr);
146
147   return (0);
148 } /* }}} int show_instance_list_cb */
149
150 static int show_instance_list (void *user_data) /* {{{ */
151 {
152   show_graph_data_t *data = user_data;
153   char title[128];
154   char params[1024];
155
156   memset (title, 0, sizeof (title));
157   graph_get_title (data->cfg, title, sizeof (title));
158   html_escape_buffer (title, sizeof (title));
159
160   memset (params, 0, sizeof (params));
161   graph_get_params (data->cfg, params, sizeof (params));
162   html_escape_buffer (params, sizeof (params));
163
164   printf ("<ul class=\"graph_list\">\n"
165       "  <li class=\"graph\"><a href=\"%s?action=show_instance;%s\">%s</a>\n"
166       "  <ul class=\"instance_list\">\n",
167       script_name (), params, title);
168
169   graph_inst_foreach (data->cfg, show_instance_list_cb, user_data);
170
171   printf ("  </ul>\n"
172       "</ul>\n");
173
174   return (0);
175 } /* }}} int show_instance_list */
176
177 static int show_instance_cb (graph_config_t *cfg, /* {{{ */
178     graph_instance_t *inst,
179     void *user_data)
180 {
181   show_graph_data_t *data = user_data;
182   char title[128];
183   char descr[128];
184   char params[1024];
185
186   memset (title, 0, sizeof (title));
187   graph_get_title (cfg, title, sizeof (title));
188   html_escape_buffer (title, sizeof (title));
189
190   memset (descr, 0, sizeof (descr));
191   inst_describe (cfg, inst, descr, sizeof (descr));
192   html_escape_buffer (descr, sizeof (descr));
193
194   memset (params, 0, sizeof (params));
195   inst_get_params (cfg, inst, params, sizeof (params));
196   html_escape_buffer (params, sizeof (params));
197
198   printf ("<h3>Instance &quot;%s&quot;</h3>\n", descr);
199
200   show_breadcrump (cfg, inst);
201
202   if (data->graph_count < MAX_SHOW_GRAPHS)
203     printf ("<div class=\"graph-img\"><img src=\"%s?action=graph;%s\" "
204         "title=\"%s / %s\" /></div>\n",
205         script_name (), params, title, descr);
206   else
207     printf ("<a href=\"%s?action=show_instance;%s\">Show graph "
208         "&quot;%s / %s&quot;</a>\n",
209         script_name (), params, title, descr);
210
211   data->graph_count++;
212
213   return (0);
214 } /* }}} int show_instance_cb */
215
216 static int show_instance (void *user_data) /* {{{ */
217 {
218   show_graph_data_t *data = user_data;
219   int status;
220
221   fprintf (stderr, "show_instance: Calling inst_get_all_selected()\n");
222   status = inst_get_all_selected (data->cfg,
223       /* callback = */ show_instance_cb, /* user data = */ data);
224   if (status != 0)
225     fprintf (stderr, "show_instance: inst_get_all_selected failed "
226         "with status %i\n", status);
227
228   return (0);
229 } /* }}} int show_instance */
230
231 int action_show_instance (void) /* {{{ */
232 {
233   page_callbacks_t pg_callbacks = PAGE_CALLBACKS_INIT;
234   show_graph_data_t pg_data;
235
236   char tmp[128];
237   char title[128];
238
239   memset (&pg_data, 0, sizeof (pg_data));
240   pg_data.cfg = gl_graph_get_selected ();
241   if (pg_data.cfg == NULL)
242     OUTPUT_ERROR ("gl_graph_get_selected () failed.\n");
243
244   memset (tmp, 0, sizeof (tmp));
245   graph_get_title (pg_data.cfg, tmp, sizeof (tmp));
246   snprintf (title, sizeof (title), "Graph \"%s\"", tmp);
247   title[sizeof (title) - 1] = 0;
248
249   pg_callbacks.top_right = html_print_search_box;
250   pg_callbacks.middle_center = show_instance;
251   pg_callbacks.middle_left = show_instance_list;
252   pg_callbacks.middle_right = show_time_selector;
253
254   html_print_page (title, &pg_callbacks, &pg_data);
255
256   return (0);
257 } /* }}} int action_show_instance */
258
259 /* vim: set sw=2 sts=2 et fdm=marker : */