7 #include <dirent.h> /* for PATH_MAX */
14 #include "action_graph.h"
15 #include "graph_list.h"
16 #include "utils_params.h"
17 #include "utils_array.h"
20 #include <fcgi_stdio.h>
22 static int get_time_args (str_array_t *args) /* {{{ */
24 const char *begin_str;
32 begin_str = param ("begin");
33 end_str = param ("end");
35 now = (long) time (NULL);
37 if (begin_str != NULL)
41 tmp = strtol (begin_str, &endptr, /* base = */ 0);
42 if ((endptr == begin_str) || (errno != 0))
49 else /* if (begin_str == NULL) */
58 tmp = strtol (end_str, &endptr, /* base = */ 0);
59 if ((endptr == end_str) || (errno != 0))
67 else /* if (end_str == NULL) */
82 array_append (args, "-s");
83 array_append_format (args, "%li", begin);
84 array_append (args, "-e");
85 array_append_format (args, "%li", end);
88 } /* }}} int get_time_args */
90 static void emulate_graph (int argc, char **argv) /* {{{ */
94 printf ("rrdtool \\\n");
95 for (i = 0; i < argc; i++)
98 printf (" \"%s\" \\\n", argv[i]);
100 printf (" \"%s\"\n", argv[i]);
102 } /* }}} void emulate_graph */
104 static int ag_info_print (rrd_info_t *info) /* {{{ */
106 if (info->type == RD_I_VAL)
107 printf ("[info] %s = %g;\n", info->key, info->value.u_val);
108 else if (info->type == RD_I_CNT)
109 printf ("[info] %s = %lu;\n", info->key, info->value.u_cnt);
110 else if (info->type == RD_I_STR)
111 printf ("[info] %s = %s;\n", info->key, info->value.u_str);
112 else if (info->type == RD_I_INT)
113 printf ("[info] %s = %i;\n", info->key, info->value.u_int);
114 else if (info->type == RD_I_BLO)
115 printf ("[info] %s = [blob, %lu bytes];\n", info->key, info->value.u_blo.size);
117 printf ("[info] %s = [unknown type %#x];\n", info->key, info->type);
120 } /* }}} int ag_info_print */
122 static int output_graph (rrd_info_t *info, /* {{{ */
127 for (img = info; img != NULL; img = img->next)
128 if ((strcmp ("image", img->key) == 0)
129 && (img->type == RD_I_BLO))
135 printf ("Content-Type: image/png\n"
136 "Content-Length: %lu\n",
137 img->value.u_blo.size);
143 status = time_to_rfc1123 (mtime, buffer, sizeof (buffer));
145 printf ("Last-Modified: %s\n", buffer);
149 fwrite (img->value.u_blo.ptr, img->value.u_blo.size,
150 /* nmemb = */ 1, stdout);
153 } /* }}} int output_graph */
155 #define OUTPUT_ERROR(...) do { \
156 printf ("Content-Type: text/plain\n\n"); \
157 printf (__VA_ARGS__); \
161 int action_graph (void) /* {{{ */
165 graph_instance_t *inst;
169 cfg = gl_graph_get_selected ();
171 OUTPUT_ERROR ("gl_graph_get_selected () failed.\n");
173 inst = inst_get_selected (cfg);
175 OUTPUT_ERROR ("inst_get_selected (%p) failed.\n", (void *) cfg);
177 args = array_create ();
181 array_append (args, "graph");
182 array_append (args, "-");
183 array_append (args, "--imgformat");
184 array_append (args, "PNG");
186 get_time_args (args);
188 status = inst_get_rrdargs (cfg, inst, args);
191 array_destroy (args);
192 OUTPUT_ERROR ("inst_get_rrdargs failed with status %i.\n", status);
196 info = rrd_graph_v (array_argc (args), array_argv (args));
197 if ((info == NULL) || rrd_test_error ())
199 printf ("Content-Type: text/plain\n\n");
200 printf ("rrd_graph_v failed: %s\n", rrd_get_error ());
201 emulate_graph (array_argc (args), array_argv (args));
207 status = output_graph (info, inst_get_mtime (inst));
212 printf ("Content-Type: text/plain\n\n");
213 printf ("output_graph failed. Maybe the \"image\" info was not found?\n\n");
215 for (ptr = info; ptr != NULL; ptr = ptr->next)
223 rrd_info_free (info);
225 array_destroy (args);
229 } /* }}} int action_graph */
231 /* vim: set sw=2 sts=2 et fdm=marker : */