7 #include <dirent.h> /* for PATH_MAX */
14 #include "action_graph.h"
16 #include "graph_instance.h"
17 #include "graph_list.h"
18 #include "utils_cgi.h"
19 #include "utils_array.h"
22 #include <fcgi_stdio.h>
34 typedef struct graph_data_s graph_data_t;
36 static int get_time_args (graph_data_t *data) /* {{{ */
38 const char *begin_str;
46 begin_str = param ("begin");
47 end_str = param ("end");
49 now = (long) time (NULL);
51 data->begin = now - 86400;
54 if (begin_str != NULL)
58 tmp = strtol (begin_str, &endptr, /* base = */ 0);
59 if ((endptr == begin_str) || (errno != 0))
66 else /* if (begin_str == NULL) */
75 tmp = strtol (end_str, &endptr, /* base = */ 0);
76 if ((endptr == end_str) || (errno != 0))
84 else /* if (end_str == NULL) */
102 array_append (data->args->options, "-s");
103 array_append_format (data->args->options, "%li", begin);
104 array_append (data->args->options, "-e");
105 array_append_format (data->args->options, "%li", end);
108 } /* }}} int get_time_args */
110 static void emulate_graph (int argc, char **argv) /* {{{ */
114 printf ("rrdtool \\\n");
115 for (i = 0; i < argc; i++)
118 printf (" \"%s\" \\\n", argv[i]);
120 printf (" \"%s\"\n", argv[i]);
122 } /* }}} void emulate_graph */
124 static int ag_info_print (rrd_info_t *info) /* {{{ */
126 if (info->type == RD_I_VAL)
127 printf ("[info] %s = %g;\n", info->key, info->value.u_val);
128 else if (info->type == RD_I_CNT)
129 printf ("[info] %s = %lu;\n", info->key, info->value.u_cnt);
130 else if (info->type == RD_I_STR)
131 printf ("[info] %s = %s;\n", info->key, info->value.u_str);
132 else if (info->type == RD_I_INT)
133 printf ("[info] %s = %i;\n", info->key, info->value.u_int);
134 else if (info->type == RD_I_BLO)
135 printf ("[info] %s = [blob, %lu bytes];\n", info->key, info->value.u_blo.size);
137 printf ("[info] %s = [unknown type %#x];\n", info->key, info->type);
140 } /* }}} int ag_info_print */
142 static int output_graph (graph_data_t *data) /* {{{ */
145 char time_buffer[256];
149 for (img = data->info; img != NULL; img = img->next)
150 if ((strcmp ("image", img->key) == 0)
151 && (img->type == RD_I_BLO))
157 printf ("Content-Type: image/png\n"
158 "Content-Length: %lu\n",
159 img->value.u_blo.size);
164 status = time_to_rfc1123 (data->mtime, time_buffer, sizeof (time_buffer));
166 printf ("Last-Modified: %s\n", time_buffer);
169 /* Print Expires header. */
170 if (data->end >= data->now)
172 /* The end of the timespan can be seen. */
175 /* FIXME: Handle graphs with width != 400. */
176 secs_per_pixel = (data->end - data->begin) / 400;
178 expires = (time_t) (data->now + secs_per_pixel);
180 else /* if (data->end < data->now) */
182 expires = (time_t) (data->now + 86400);
184 status = time_to_rfc1123 (expires, time_buffer, sizeof (time_buffer));
186 printf ("Expires: %s\n", time_buffer);
190 fwrite (img->value.u_blo.ptr, img->value.u_blo.size,
191 /* nmemb = */ 1, stdout);
194 } /* }}} int output_graph */
196 #define OUTPUT_ERROR(...) do { \
197 printf ("Content-Type: text/plain\n\n"); \
198 printf (__VA_ARGS__); \
202 int action_graph (void) /* {{{ */
206 graph_instance_t *inst;
212 cfg = gl_graph_get_selected ();
214 OUTPUT_ERROR ("gl_graph_get_selected () failed.\n");
216 inst = inst_get_selected (cfg);
218 OUTPUT_ERROR ("inst_get_selected (%p) failed.\n", (void *) cfg);
220 data.args = ra_create ();
221 if (data.args == NULL)
224 array_append (data.args->options, "graph");
225 array_append (data.args->options, "-");
226 array_append (data.args->options, "--imgformat");
227 array_append (data.args->options, "PNG");
229 get_time_args (&data);
231 status = inst_get_rrdargs (cfg, inst, data.args);
234 ra_destroy (data.args);
235 OUTPUT_ERROR ("inst_get_rrdargs failed with status %i.\n", status);
238 argc = ra_argc (data.args);
239 argv = ra_argv (data.args);
240 if ((argc < 0) || (argv == NULL))
242 ra_destroy (data.args);
247 data.info = rrd_graph_v (argc, argv);
248 if ((data.info == NULL) || rrd_test_error ())
250 printf ("Content-Type: text/plain\n\n");
251 printf ("rrd_graph_v failed: %s\n", rrd_get_error ());
252 emulate_graph (argc, argv);
258 data.mtime = inst_get_mtime (inst);
260 status = output_graph (&data);
265 printf ("Content-Type: text/plain\n\n");
266 printf ("output_graph failed. Maybe the \"image\" info was not found?\n\n");
268 for (ptr = data.info; ptr != NULL; ptr = ptr->next)
275 if (data.info != NULL)
276 rrd_info_free (data.info);
279 ra_destroy (data.args);
283 } /* }}} int action_graph */
285 /* vim: set sw=2 sts=2 et fdm=marker : */