f9322d858000ae5148132102e606cb6e6521f9ea
[collection4.git] / src / action_graph.c
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  **/
23
24 #include "config.h"
25
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>
35
36 #include <rrd.h>
37
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"
45
46 #include <fcgiapp.h>
47 #include <fcgi_stdio.h>
48
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;
60
61 static int get_time_args (graph_data_t *data) /* {{{ */
62 {
63   const char *begin_str;
64   const char *end_str;
65   long now;
66   long begin;
67   long end;
68   char *endptr;
69   long tmp;
70
71   begin_str = param ("begin");
72   end_str = param ("end");
73
74   now = (long) time (NULL);
75   data->now = now;
76   data->begin = now - 86400;
77   data->end = now;
78
79   if (begin_str != NULL)
80   {
81     endptr = NULL;
82     errno = 0;
83     tmp = strtol (begin_str, &endptr, /* base = */ 0);
84     if ((endptr == begin_str) || (errno != 0))
85       return (-1);
86     if (tmp <= 0)
87       begin = now + tmp;
88     else
89       begin = tmp;
90   }
91   else /* if (begin_str == NULL) */
92   {
93     begin = now - 86400;
94   }
95
96   if (end_str != NULL)
97   {
98     endptr = NULL;
99     errno = 0;
100     tmp = strtol (end_str, &endptr, /* base = */ 0);
101     if ((endptr == end_str) || (errno != 0))
102       return (-1);
103     end = tmp;
104     if (tmp <= 0)
105       end = now + tmp;
106     else
107       end = tmp;
108   }
109   else /* if (end_str == NULL) */
110   {
111     end = now;
112   }
113
114   if (begin == end)
115     return (-1);
116
117   if (begin > end)
118   {
119     tmp = begin;
120     begin = end;
121     end = tmp;
122   }
123
124   data->begin = begin;
125   data->end = end;
126
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);
131
132   return (0);
133 } /* }}} int get_time_args */
134
135 static void emulate_graph (int argc, char **argv) /* {{{ */
136 {
137   int i;
138
139   printf ("rrdtool \\\n");
140   for (i = 0; i < argc; i++)
141   {
142     if (i < (argc - 1))
143       printf ("  \"%s\" \\\n", argv[i]);
144     else
145       printf ("  \"%s\"\n", argv[i]);
146   }
147 } /* }}} void emulate_graph */
148
149 static int ag_info_print (rrd_info_t *info) /* {{{ */
150 {
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);
161   else
162     printf ("[info] %s = [unknown type %#x];\n", info->key, info->type);
163
164   return (0);
165 } /* }}} int ag_info_print */
166
167 static int output_graph (graph_data_t *data) /* {{{ */
168 {
169   rrd_info_t *img;
170   char time_buffer[256];
171   time_t expires;
172   int status;
173
174   for (img = data->info; img != NULL; img = img->next)
175     if ((strcmp ("image", img->key) == 0)
176         && (img->type == RD_I_BLO))
177       break;
178
179   if (img == NULL)
180     return (ENOENT);
181
182   printf ("Content-Type: image/png\n"
183       "Content-Length: %lu\n",
184       img->value.u_blo.size);
185   if (data->mtime > 0)
186   {
187     int status;
188     
189     status = time_to_rfc1123 (data->mtime, time_buffer, sizeof (time_buffer));
190     if (status == 0)
191       printf ("Last-Modified: %s\n", time_buffer);
192   }
193
194   /* Print Expires header. */
195   if (data->end >= data->now)
196   {
197     /* The end of the timespan can be seen. */
198     long secs_per_pixel;
199
200     /* FIXME: Handle graphs with width != 400. */
201     secs_per_pixel = (data->end - data->begin) / 400;
202
203     expires = (time_t) (data->now + secs_per_pixel);
204   }
205   else /* if (data->end < data->now) */
206   {
207     expires = (time_t) (data->now + 86400);
208   }
209   status = time_to_rfc1123 (expires, time_buffer, sizeof (time_buffer));
210   if (status == 0)
211     printf ("Expires: %s\n", time_buffer);
212
213   printf ("X-Generator: "PACKAGE_STRING"\n");
214   printf ("\n");
215
216   fwrite (img->value.u_blo.ptr, img->value.u_blo.size,
217       /* nmemb = */ 1, stdout);
218
219   return (0);
220 } /* }}} int output_graph */
221
222 #define OUTPUT_ERROR(...) do {             \
223   printf ("Content-Type: text/plain\n\n"); \
224   printf (__VA_ARGS__);                    \
225   return (0);                              \
226 } while (0)
227
228 int action_graph (void) /* {{{ */
229 {
230   graph_data_t data;
231   graph_config_t *cfg;
232   graph_instance_t *inst;
233   int status;
234
235   int argc;
236   char **argv;
237
238   cfg = gl_graph_get_selected ();
239   if (cfg == NULL)
240     OUTPUT_ERROR ("gl_graph_get_selected () failed.\n");
241
242   inst = inst_get_selected (cfg);
243   if (inst == NULL)
244     OUTPUT_ERROR ("inst_get_selected (%p) failed.\n", (void *) cfg);
245
246   data.args = ra_create ();
247   if (data.args == NULL)
248     return (ENOMEM);
249
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");
254
255   get_time_args (&data);
256
257   status = inst_get_rrdargs (cfg, inst, data.args);
258   if (status != 0)
259   {
260     ra_destroy (data.args);
261     OUTPUT_ERROR ("inst_get_rrdargs failed with status %i.\n", status);
262   }
263
264   argc = ra_argc (data.args);
265   argv = ra_argv (data.args);
266   if ((argc < 0) || (argv == NULL))
267   {
268     ra_destroy (data.args);
269     return (-1);
270   }
271
272   rrd_clear_error ();
273   data.info = rrd_graph_v (argc, argv);
274   if ((data.info == NULL) || rrd_test_error ())
275   {
276     printf ("Content-Type: text/plain\n\n");
277     printf ("rrd_graph_v failed: %s\n", rrd_get_error ());
278     emulate_graph (argc, argv);
279   }
280   else
281   {
282     int status;
283
284     data.mtime = inst_get_mtime (inst);
285
286     status = output_graph (&data);
287     if (status != 0)
288     {
289       rrd_info_t *ptr;
290
291       printf ("Content-Type: text/plain\n\n");
292       printf ("output_graph failed. Maybe the \"image\" info was not found?\n\n");
293
294       for (ptr = data.info; ptr != NULL; ptr = ptr->next)
295       {
296         ag_info_print (ptr);
297       }
298     }
299   }
300
301   if (data.info != NULL)
302     rrd_info_free (data.info);
303
304   ra_argv_free (argv);
305   ra_destroy (data.args);
306   data.args = NULL;
307
308   return (0);
309 } /* }}} int action_graph */
310
311 /* vim: set sw=2 sts=2 et fdm=marker : */