2 * collection4 - action_graph.c
3 * Copyright (C) 2010 Florian octo Forster
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.
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.
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
21 * Florian octo Forster <ff at octo.it>
30 #include <dirent.h> /* for PATH_MAX */
37 #include "action_graph.h"
39 #include "graph_instance.h"
40 #include "graph_list.h"
41 #include "utils_cgi.h"
42 #include "utils_array.h"
45 #include <fcgi_stdio.h>
57 typedef struct graph_data_s graph_data_t;
59 static int get_time_args (graph_data_t *data) /* {{{ */
61 const char *begin_str;
69 begin_str = param ("begin");
70 end_str = param ("end");
72 now = (long) time (NULL);
74 data->begin = now - 86400;
77 if (begin_str != NULL)
81 tmp = strtol (begin_str, &endptr, /* base = */ 0);
82 if ((endptr == begin_str) || (errno != 0))
89 else /* if (begin_str == NULL) */
98 tmp = strtol (end_str, &endptr, /* base = */ 0);
99 if ((endptr == end_str) || (errno != 0))
107 else /* if (end_str == NULL) */
125 array_append (data->args->options, "-s");
126 array_append_format (data->args->options, "%li", begin);
127 array_append (data->args->options, "-e");
128 array_append_format (data->args->options, "%li", end);
131 } /* }}} int get_time_args */
133 static void emulate_graph (int argc, char **argv) /* {{{ */
137 printf ("rrdtool \\\n");
138 for (i = 0; i < argc; i++)
141 printf (" \"%s\" \\\n", argv[i]);
143 printf (" \"%s\"\n", argv[i]);
145 } /* }}} void emulate_graph */
147 static int ag_info_print (rrd_info_t *info) /* {{{ */
149 if (info->type == RD_I_VAL)
150 printf ("[info] %s = %g;\n", info->key, info->value.u_val);
151 else if (info->type == RD_I_CNT)
152 printf ("[info] %s = %lu;\n", info->key, info->value.u_cnt);
153 else if (info->type == RD_I_STR)
154 printf ("[info] %s = %s;\n", info->key, info->value.u_str);
155 else if (info->type == RD_I_INT)
156 printf ("[info] %s = %i;\n", info->key, info->value.u_int);
157 else if (info->type == RD_I_BLO)
158 printf ("[info] %s = [blob, %lu bytes];\n", info->key, info->value.u_blo.size);
160 printf ("[info] %s = [unknown type %#x];\n", info->key, info->type);
163 } /* }}} int ag_info_print */
165 static int output_graph (graph_data_t *data) /* {{{ */
168 char time_buffer[256];
172 for (img = data->info; img != NULL; img = img->next)
173 if ((strcmp ("image", img->key) == 0)
174 && (img->type == RD_I_BLO))
180 printf ("Content-Type: image/png\n"
181 "Content-Length: %lu\n",
182 img->value.u_blo.size);
187 status = time_to_rfc1123 (data->mtime, time_buffer, sizeof (time_buffer));
189 printf ("Last-Modified: %s\n", time_buffer);
192 /* Print Expires header. */
193 if (data->end >= data->now)
195 /* The end of the timespan can be seen. */
198 /* FIXME: Handle graphs with width != 400. */
199 secs_per_pixel = (data->end - data->begin) / 400;
201 expires = (time_t) (data->now + secs_per_pixel);
203 else /* if (data->end < data->now) */
205 expires = (time_t) (data->now + 86400);
207 status = time_to_rfc1123 (expires, time_buffer, sizeof (time_buffer));
209 printf ("Expires: %s\n", time_buffer);
213 fwrite (img->value.u_blo.ptr, img->value.u_blo.size,
214 /* nmemb = */ 1, stdout);
217 } /* }}} int output_graph */
219 #define OUTPUT_ERROR(...) do { \
220 printf ("Content-Type: text/plain\n\n"); \
221 printf (__VA_ARGS__); \
225 int action_graph (void) /* {{{ */
229 graph_instance_t *inst;
235 cfg = gl_graph_get_selected ();
237 OUTPUT_ERROR ("gl_graph_get_selected () failed.\n");
239 inst = inst_get_selected (cfg);
241 OUTPUT_ERROR ("inst_get_selected (%p) failed.\n", (void *) cfg);
243 data.args = ra_create ();
244 if (data.args == NULL)
247 array_append (data.args->options, "graph");
248 array_append (data.args->options, "-");
249 array_append (data.args->options, "--imgformat");
250 array_append (data.args->options, "PNG");
252 get_time_args (&data);
254 status = inst_get_rrdargs (cfg, inst, data.args);
257 ra_destroy (data.args);
258 OUTPUT_ERROR ("inst_get_rrdargs failed with status %i.\n", status);
261 argc = ra_argc (data.args);
262 argv = ra_argv (data.args);
263 if ((argc < 0) || (argv == NULL))
265 ra_destroy (data.args);
270 data.info = rrd_graph_v (argc, argv);
271 if ((data.info == NULL) || rrd_test_error ())
273 printf ("Content-Type: text/plain\n\n");
274 printf ("rrd_graph_v failed: %s\n", rrd_get_error ());
275 emulate_graph (argc, argv);
281 data.mtime = inst_get_mtime (inst);
283 status = output_graph (&data);
288 printf ("Content-Type: text/plain\n\n");
289 printf ("output_graph failed. Maybe the \"image\" info was not found?\n\n");
291 for (ptr = data.info; ptr != NULL; ptr = ptr->next)
298 if (data.info != NULL)
299 rrd_info_free (data.info);
302 ra_destroy (data.args);
306 } /* }}} int action_graph */
308 /* vim: set sw=2 sts=2 et fdm=marker : */