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, "-s");
103 array_append_format (data->args, "%li", begin);
104 array_append (data->args, "-e");
105 array_append_format (data->args, "%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;
209 cfg = gl_graph_get_selected ();
211 OUTPUT_ERROR ("gl_graph_get_selected () failed.\n");
213 inst = inst_get_selected (cfg);
215 OUTPUT_ERROR ("inst_get_selected (%p) failed.\n", (void *) cfg);
217 data.args = array_create ();
218 if (data.args == NULL)
221 array_append (data.args, "graph");
222 array_append (data.args, "-");
223 array_append (data.args, "--imgformat");
224 array_append (data.args, "PNG");
226 get_time_args (&data);
228 status = inst_get_rrdargs (cfg, inst, data.args);
231 array_destroy (data.args);
232 OUTPUT_ERROR ("inst_get_rrdargs failed with status %i.\n", status);
236 data.info = rrd_graph_v (array_argc (data.args), array_argv (data.args));
237 if ((data.info == NULL) || rrd_test_error ())
239 printf ("Content-Type: text/plain\n\n");
240 printf ("rrd_graph_v failed: %s\n", rrd_get_error ());
241 emulate_graph (array_argc (data.args), array_argv (data.args));
247 data.mtime = inst_get_mtime (inst);
249 status = output_graph (&data);
254 printf ("Content-Type: text/plain\n\n");
255 printf ("output_graph failed. Maybe the \"image\" info was not found?\n\n");
257 for (ptr = data.info; ptr != NULL; ptr = ptr->next)
264 if (data.info != NULL)
265 rrd_info_free (data.info);
267 array_destroy (data.args);
271 } /* }}} int action_graph */
273 /* vim: set sw=2 sts=2 et fdm=marker : */