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>
32 #include <dirent.h> /* for PATH_MAX */
39 #include "action_graph.h"
41 #include "graph_instance.h"
42 #include "graph_list.h"
43 #include "utils_cgi.h"
44 #include "utils_array.h"
47 #include <fcgi_stdio.h>
59 typedef struct graph_data_s graph_data_t;
61 static int get_time_args (graph_data_t *data) /* {{{ */
63 const char *begin_str;
71 begin_str = param ("begin");
72 end_str = param ("end");
74 now = (long) time (NULL);
76 data->begin = now - 86400;
79 if (begin_str != NULL)
83 tmp = strtol (begin_str, &endptr, /* base = */ 0);
84 if ((endptr == begin_str) || (errno != 0))
91 else /* if (begin_str == NULL) */
100 tmp = strtol (end_str, &endptr, /* base = */ 0);
101 if ((endptr == end_str) || (errno != 0))
109 else /* if (end_str == NULL) */
127 array_append (data->args->options, "-s");
128 array_append_format (data->args->options, "%li", begin);
129 array_append (data->args->options, "-e");
130 array_append_format (data->args->options, "%li", end);
133 } /* }}} int get_time_args */
135 static void emulate_graph (int argc, char **argv) /* {{{ */
139 printf ("rrdtool \\\n");
140 for (i = 0; i < argc; i++)
143 printf (" \"%s\" \\\n", argv[i]);
145 printf (" \"%s\"\n", argv[i]);
147 } /* }}} void emulate_graph */
149 static int ag_info_print (rrd_info_t *info) /* {{{ */
151 if (info->type == RD_I_VAL)
152 printf ("[info] %s = %g;\n", info->key, info->value.u_val);
153 else if (info->type == RD_I_CNT)
154 printf ("[info] %s = %lu;\n", info->key, info->value.u_cnt);
155 else if (info->type == RD_I_STR)
156 printf ("[info] %s = %s;\n", info->key, info->value.u_str);
157 else if (info->type == RD_I_INT)
158 printf ("[info] %s = %i;\n", info->key, info->value.u_int);
159 else if (info->type == RD_I_BLO)
160 printf ("[info] %s = [blob, %lu bytes];\n", info->key, info->value.u_blo.size);
162 printf ("[info] %s = [unknown type %#x];\n", info->key, info->type);
165 } /* }}} int ag_info_print */
167 static int output_graph (graph_data_t *data) /* {{{ */
170 char time_buffer[256];
174 for (img = data->info; img != NULL; img = img->next)
175 if ((strcmp ("image", img->key) == 0)
176 && (img->type == RD_I_BLO))
182 printf ("Content-Type: image/png\n"
183 "Content-Length: %lu\n",
184 img->value.u_blo.size);
189 status = time_to_rfc1123 (data->mtime, time_buffer, sizeof (time_buffer));
191 printf ("Last-Modified: %s\n", time_buffer);
194 /* Print Expires header. */
195 if (data->end >= data->now)
197 /* The end of the timespan can be seen. */
200 /* FIXME: Handle graphs with width != 400. */
201 secs_per_pixel = (data->end - data->begin) / 400;
203 expires = (time_t) (data->now + secs_per_pixel);
205 else /* if (data->end < data->now) */
207 expires = (time_t) (data->now + 86400);
209 status = time_to_rfc1123 (expires, time_buffer, sizeof (time_buffer));
211 printf ("Expires: %s\n", time_buffer);
213 printf ("X-Generator: "PACKAGE_STRING"\n");
216 fwrite (img->value.u_blo.ptr, img->value.u_blo.size,
217 /* nmemb = */ 1, stdout);
220 } /* }}} int output_graph */
222 #define OUTPUT_ERROR(...) do { \
223 printf ("Content-Type: text/plain\n\n"); \
224 printf (__VA_ARGS__); \
228 int action_graph (void) /* {{{ */
232 graph_instance_t *inst;
238 cfg = gl_graph_get_selected ();
240 OUTPUT_ERROR ("gl_graph_get_selected () failed.\n");
242 inst = inst_get_selected (cfg);
244 OUTPUT_ERROR ("inst_get_selected (%p) failed.\n", (void *) cfg);
246 data.args = ra_create ();
247 if (data.args == NULL)
250 array_append (data.args->options, "graph");
251 array_append (data.args->options, "-");
252 array_append (data.args->options, "--imgformat");
253 array_append (data.args->options, "PNG");
255 get_time_args (&data);
257 status = inst_get_rrdargs (cfg, inst, data.args);
260 ra_destroy (data.args);
261 OUTPUT_ERROR ("inst_get_rrdargs failed with status %i.\n", status);
264 argc = ra_argc (data.args);
265 argv = ra_argv (data.args);
266 if ((argc < 0) || (argv == NULL))
268 ra_destroy (data.args);
273 data.info = rrd_graph_v (argc, argv);
274 if ((data.info == NULL) || rrd_test_error ())
276 printf ("Content-Type: text/plain\n\n");
277 printf ("rrd_graph_v failed: %s\n", rrd_get_error ());
278 emulate_graph (argc, argv);
284 data.mtime = inst_get_mtime (inst);
286 status = output_graph (&data);
291 printf ("Content-Type: text/plain\n\n");
292 printf ("output_graph failed. Maybe the \"image\" info was not found?\n\n");
294 for (ptr = data.info; ptr != NULL; ptr = ptr->next)
301 if (data.info != NULL)
302 rrd_info_free (data.info);
305 ra_destroy (data.args);
309 } /* }}} int action_graph */
311 /* vim: set sw=2 sts=2 et fdm=marker : */