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