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 void emulate_graph (int argc, char **argv) /* {{{ */
65 printf ("rrdtool \\\n");
66 for (i = 0; i < argc; i++)
69 printf (" \"%s\" \\\n", argv[i]);
71 printf (" \"%s\"\n", argv[i]);
73 } /* }}} void emulate_graph */
75 static int ag_info_print (rrd_info_t *info) /* {{{ */
77 if (info->type == RD_I_VAL)
78 printf ("[info] %s = %g;\n", info->key, info->value.u_val);
79 else if (info->type == RD_I_CNT)
80 printf ("[info] %s = %lu;\n", info->key, info->value.u_cnt);
81 else if (info->type == RD_I_STR)
82 printf ("[info] %s = %s;\n", info->key, info->value.u_str);
83 else if (info->type == RD_I_INT)
84 printf ("[info] %s = %i;\n", info->key, info->value.u_int);
85 else if (info->type == RD_I_BLO)
86 printf ("[info] %s = [blob, %lu bytes];\n", info->key, info->value.u_blo.size);
88 printf ("[info] %s = [unknown type %#x];\n", info->key, info->type);
91 } /* }}} int ag_info_print */
93 static int output_graph (graph_data_t *data) /* {{{ */
96 char time_buffer[256];
100 for (img = data->info; img != NULL; img = img->next)
101 if ((strcmp ("image", img->key) == 0)
102 && (img->type == RD_I_BLO))
108 printf ("Content-Type: image/png\n"
109 "Content-Length: %lu\n",
110 img->value.u_blo.size);
115 status = time_to_rfc1123 (data->mtime, time_buffer, sizeof (time_buffer));
117 printf ("Last-Modified: %s\n", time_buffer);
120 /* Print Expires header. */
121 if (data->end >= data->now)
123 /* The end of the timespan can be seen. */
126 /* FIXME: Handle graphs with width != 400. */
127 secs_per_pixel = (data->end - data->begin) / 400;
129 expires = (time_t) (data->now + secs_per_pixel);
131 else /* if (data->end < data->now) */
133 expires = (time_t) (data->now + 86400);
135 status = time_to_rfc1123 (expires, time_buffer, sizeof (time_buffer));
137 printf ("Expires: %s\n", time_buffer);
139 printf ("X-Generator: "PACKAGE_STRING"\n");
142 fwrite (img->value.u_blo.ptr, img->value.u_blo.size,
143 /* nmemb = */ 1, stdout);
146 } /* }}} int output_graph */
148 #define OUTPUT_ERROR(...) do { \
149 printf ("Content-Type: text/plain\n\n"); \
150 printf (__VA_ARGS__); \
154 int action_graph (void) /* {{{ */
158 graph_instance_t *inst;
164 cfg = gl_graph_get_selected ();
166 OUTPUT_ERROR ("gl_graph_get_selected () failed.\n");
168 inst = inst_get_selected (cfg);
170 OUTPUT_ERROR ("inst_get_selected (%p) failed.\n", (void *) cfg);
172 data.args = ra_create ();
173 if (data.args == NULL)
176 array_append (data.args->options, "graph");
177 array_append (data.args->options, "-");
178 array_append (data.args->options, "--imgformat");
179 array_append (data.args->options, "PNG");
181 status = get_time_args (&data.begin, &data.end, &data.now);
184 array_append (data.args->options, "-s");
185 array_append_format (data.args->options, "%li", data.begin);
186 array_append (data.args->options, "-e");
187 array_append_format (data.args->options, "%li", data.end);
190 status = inst_get_rrdargs (cfg, inst, data.args);
193 ra_destroy (data.args);
194 OUTPUT_ERROR ("inst_get_rrdargs failed with status %i.\n", status);
197 argc = ra_argc (data.args);
198 argv = ra_argv (data.args);
199 if ((argc < 0) || (argv == NULL))
201 ra_destroy (data.args);
206 data.info = rrd_graph_v (argc, argv);
207 if ((data.info == NULL) || rrd_test_error ())
209 printf ("Content-Type: text/plain\n\n");
210 printf ("rrd_graph_v failed: %s\n", rrd_get_error ());
211 emulate_graph (argc, argv);
217 data.mtime = inst_get_mtime (inst);
219 status = output_graph (&data);
224 printf ("Content-Type: text/plain\n\n");
225 printf ("output_graph failed. Maybe the \"image\" info was not found?\n\n");
227 for (ptr = data.info; ptr != NULL; ptr = ptr->next)
234 if (data.info != NULL)
235 rrd_info_free (data.info);
238 ra_destroy (data.args);
242 } /* }}} int action_graph */
244 /* vim: set sw=2 sts=2 et fdm=marker : */