1 /**
2 * collection4 - action_graph.c
3 * Copyright (C) 2010 Florian octo Forster
4 *
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.
9 *
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.
14 *
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
19 *
20 * Authors:
21 * Florian octo Forster <ff at octo.it>
22 **/
24 #include "config.h"
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <stdint.h>
31 #include <inttypes.h>
32 #include <dirent.h> /* for PATH_MAX */
33 #include <assert.h>
34 #include <math.h>
36 #include <rrd.h>
38 #include "common.h"
39 #include "action_graph.h"
40 #include "graph.h"
41 #include "graph_instance.h"
42 #include "graph_list.h"
43 #include "utils_cgi.h"
44 #include "utils_array.h"
46 #include <fcgiapp.h>
47 #include <fcgi_stdio.h>
49 struct graph_data_s
50 {
51 rrd_args_t *args;
52 rrd_info_t *info;
53 time_t mtime;
54 time_t expires;
55 long now;
56 long begin;
57 long end;
58 };
59 typedef struct graph_data_s graph_data_t;
61 static void emulate_graph (int argc, char **argv) /* {{{ */
62 {
63 int i;
65 printf ("rrdtool \\\n");
66 for (i = 0; i < argc; i++)
67 {
68 if (i < (argc - 1))
69 printf (" \"%s\" \\\n", argv[i]);
70 else
71 printf (" \"%s\"\n", argv[i]);
72 }
73 } /* }}} void emulate_graph */
75 static int ag_info_print (rrd_info_t *info) /* {{{ */
76 {
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);
87 else
88 printf ("[info] %s = [unknown type %#x];\n", info->key, info->type);
90 return (0);
91 } /* }}} int ag_info_print */
93 static int output_graph (graph_data_t *data) /* {{{ */
94 {
95 rrd_info_t *img;
96 char time_buffer[256];
97 time_t expires;
98 int status;
100 for (img = data->info; img != NULL; img = img->next)
101 if ((strcmp ("image", img->key) == 0)
102 && (img->type == RD_I_BLO))
103 break;
105 if (img == NULL)
106 return (ENOENT);
108 printf ("Content-Type: image/png\n"
109 "Content-Length: %lu\n",
110 img->value.u_blo.size);
111 if (data->mtime > 0)
112 {
113 int status;
115 status = time_to_rfc1123 (data->mtime, time_buffer, sizeof (time_buffer));
116 if (status == 0)
117 printf ("Last-Modified: %s\n", time_buffer);
118 }
120 /* Print Expires header. */
121 if (data->end >= data->now)
122 {
123 /* The end of the timespan can be seen. */
124 long secs_per_pixel;
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);
130 }
131 else /* if (data->end < data->now) */
132 {
133 expires = (time_t) (data->now + 86400);
134 }
135 status = time_to_rfc1123 (expires, time_buffer, sizeof (time_buffer));
136 if (status == 0)
137 printf ("Expires: %s\n", time_buffer);
139 printf ("X-Generator: "PACKAGE_STRING"\n");
140 printf ("\n");
142 fwrite (img->value.u_blo.ptr, img->value.u_blo.size,
143 /* nmemb = */ 1, stdout);
145 return (0);
146 } /* }}} int output_graph */
148 #define OUTPUT_ERROR(...) do { \
149 printf ("Content-Type: text/plain\n\n"); \
150 printf (__VA_ARGS__); \
151 return (0); \
152 } while (0)
154 int action_graph (void) /* {{{ */
155 {
156 graph_data_t data;
157 graph_config_t *cfg;
158 graph_instance_t *inst;
159 int status;
161 int argc;
162 char **argv;
164 cfg = gl_graph_get_selected ();
165 if (cfg == NULL)
166 OUTPUT_ERROR ("gl_graph_get_selected () failed.\n");
168 inst = inst_get_selected (cfg);
169 if (inst == NULL)
170 OUTPUT_ERROR ("inst_get_selected (%p) failed.\n", (void *) cfg);
172 data.args = ra_create ();
173 if (data.args == NULL)
174 return (ENOMEM);
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);
182 if (status == 0)
183 {
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);
188 }
190 status = inst_get_rrdargs (cfg, inst, data.args);
191 if (status != 0)
192 {
193 ra_destroy (data.args);
194 OUTPUT_ERROR ("inst_get_rrdargs failed with status %i.\n", status);
195 }
197 argc = ra_argc (data.args);
198 argv = ra_argv (data.args);
199 if ((argc < 0) || (argv == NULL))
200 {
201 ra_destroy (data.args);
202 return (-1);
203 }
205 rrd_clear_error ();
206 data.info = rrd_graph_v (argc, argv);
207 if ((data.info == NULL) || rrd_test_error ())
208 {
209 printf ("Content-Type: text/plain\n\n");
210 printf ("rrd_graph_v failed: %s\n", rrd_get_error ());
211 emulate_graph (argc, argv);
212 }
213 else
214 {
215 int status;
217 data.mtime = inst_get_mtime (inst);
219 status = output_graph (&data);
220 if (status != 0)
221 {
222 rrd_info_t *ptr;
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)
228 {
229 ag_info_print (ptr);
230 }
231 }
232 }
234 if (data.info != NULL)
235 rrd_info_free (data.info);
237 ra_argv_free (argv);
238 ra_destroy (data.args);
239 data.args = NULL;
241 return (0);
242 } /* }}} int action_graph */
244 /* vim: set sw=2 sts=2 et fdm=marker : */