7 #include <dirent.h> /* for PATH_MAX */
14 #include "action_graph.h"
15 #include "graph_list.h"
16 #include "utils_cgi.h"
17 #include "utils_array.h"
20 #include <fcgi_stdio.h>
32 typedef struct graph_data_s graph_data_t;
34 static int get_time_args (graph_data_t *data) /* {{{ */
36 const char *begin_str;
44 begin_str = param ("begin");
45 end_str = param ("end");
47 now = (long) time (NULL);
49 data->begin = now - 86400;
52 if (begin_str != NULL)
56 tmp = strtol (begin_str, &endptr, /* base = */ 0);
57 if ((endptr == begin_str) || (errno != 0))
64 else /* if (begin_str == NULL) */
73 tmp = strtol (end_str, &endptr, /* base = */ 0);
74 if ((endptr == end_str) || (errno != 0))
82 else /* if (end_str == NULL) */
100 array_append (data->args, "-s");
101 array_append_format (data->args, "%li", begin);
102 array_append (data->args, "-e");
103 array_append_format (data->args, "%li", end);
106 } /* }}} int get_time_args */
108 static void emulate_graph (int argc, char **argv) /* {{{ */
112 printf ("rrdtool \\\n");
113 for (i = 0; i < argc; i++)
116 printf (" \"%s\" \\\n", argv[i]);
118 printf (" \"%s\"\n", argv[i]);
120 } /* }}} void emulate_graph */
122 static int ag_info_print (rrd_info_t *info) /* {{{ */
124 if (info->type == RD_I_VAL)
125 printf ("[info] %s = %g;\n", info->key, info->value.u_val);
126 else if (info->type == RD_I_CNT)
127 printf ("[info] %s = %lu;\n", info->key, info->value.u_cnt);
128 else if (info->type == RD_I_STR)
129 printf ("[info] %s = %s;\n", info->key, info->value.u_str);
130 else if (info->type == RD_I_INT)
131 printf ("[info] %s = %i;\n", info->key, info->value.u_int);
132 else if (info->type == RD_I_BLO)
133 printf ("[info] %s = [blob, %lu bytes];\n", info->key, info->value.u_blo.size);
135 printf ("[info] %s = [unknown type %#x];\n", info->key, info->type);
138 } /* }}} int ag_info_print */
140 static int output_graph (graph_data_t *data) /* {{{ */
143 char time_buffer[256];
147 for (img = data->info; img != NULL; img = img->next)
148 if ((strcmp ("image", img->key) == 0)
149 && (img->type == RD_I_BLO))
155 printf ("Content-Type: image/png\n"
156 "Content-Length: %lu\n",
157 img->value.u_blo.size);
162 status = time_to_rfc1123 (data->mtime, time_buffer, sizeof (time_buffer));
164 printf ("Last-Modified: %s\n", time_buffer);
167 /* Print Expires header. */
168 if (data->end >= data->now)
170 /* The end of the timespan can be seen. */
173 /* FIXME: Handle graphs with width != 400. */
174 secs_per_pixel = (data->end - data->begin) / 400;
176 expires = (time_t) (data->now + secs_per_pixel);
178 else /* if (data->end < data->now) */
180 expires = (time_t) (data->now + 86400);
182 status = time_to_rfc1123 (expires, time_buffer, sizeof (time_buffer));
184 printf ("Expires: %s\n", time_buffer);
188 fwrite (img->value.u_blo.ptr, img->value.u_blo.size,
189 /* nmemb = */ 1, stdout);
192 } /* }}} int output_graph */
194 #define OUTPUT_ERROR(...) do { \
195 printf ("Content-Type: text/plain\n\n"); \
196 printf (__VA_ARGS__); \
200 int action_graph (void) /* {{{ */
204 graph_instance_t *inst;
207 cfg = gl_graph_get_selected ();
209 OUTPUT_ERROR ("gl_graph_get_selected () failed.\n");
211 inst = inst_get_selected (cfg);
213 OUTPUT_ERROR ("inst_get_selected (%p) failed.\n", (void *) cfg);
215 data.args = array_create ();
216 if (data.args == NULL)
219 array_append (data.args, "graph");
220 array_append (data.args, "-");
221 array_append (data.args, "--imgformat");
222 array_append (data.args, "PNG");
224 get_time_args (&data);
226 status = inst_get_rrdargs (cfg, inst, data.args);
229 array_destroy (data.args);
230 OUTPUT_ERROR ("inst_get_rrdargs failed with status %i.\n", status);
234 data.info = rrd_graph_v (array_argc (data.args), array_argv (data.args));
235 if ((data.info == NULL) || rrd_test_error ())
237 printf ("Content-Type: text/plain\n\n");
238 printf ("rrd_graph_v failed: %s\n", rrd_get_error ());
239 emulate_graph (array_argc (data.args), array_argv (data.args));
245 data.mtime = inst_get_mtime (inst);
247 status = output_graph (&data);
252 printf ("Content-Type: text/plain\n\n");
253 printf ("output_graph failed. Maybe the \"image\" info was not found?\n\n");
255 for (ptr = data.info; ptr != NULL; ptr = ptr->next)
262 if (data.info != NULL)
263 rrd_info_free (data.info);
265 array_destroy (data.args);
269 } /* }}} int action_graph */
271 /* vim: set sw=2 sts=2 et fdm=marker : */