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) /* {{{ */
126 for (img = info; img != NULL; img = img->next)
127 if ((strcmp ("image", img->key) == 0)
128 && (img->type == RD_I_BLO))
134 printf ("Content-Type: image/png\n"
135 "Content-Length: %lu\n"
137 img->value.u_blo.size);
138 fwrite (img->value.u_blo.ptr, img->value.u_blo.size,
139 /* nmemb = */ 1, stdout);
142 } /* }}} int output_graph */
144 #define OUTPUT_ERROR(...) do { \
145 printf ("Content-Type: text/plain\n\n"); \
146 printf (__VA_ARGS__); \
150 int action_graph (void) /* {{{ */
154 graph_instance_t *inst;
158 cfg = gl_graph_get_selected ();
160 OUTPUT_ERROR ("gl_graph_get_selected () failed.\n");
162 inst = inst_get_selected (cfg);
164 OUTPUT_ERROR ("inst_get_selected (%p) failed.\n", (void *) cfg);
166 args = array_create ();
170 array_append (args, "graph");
171 array_append (args, "-");
172 array_append (args, "--imgformat");
173 array_append (args, "PNG");
175 get_time_args (args);
177 status = inst_get_rrdargs (cfg, inst, args);
180 array_destroy (args);
181 OUTPUT_ERROR ("inst_get_rrdargs failed with status %i.\n", status);
185 info = rrd_graph_v (array_argc (args), array_argv (args));
186 if ((info == NULL) || rrd_test_error ())
188 printf ("Content-Type: text/plain\n\n");
189 printf ("rrd_graph_v failed: %s\n", rrd_get_error ());
190 emulate_graph (array_argc (args), array_argv (args));
196 status = output_graph (info);
201 printf ("Content-Type: text/plain\n\n");
202 printf ("output_graph failed. Maybe the \"image\" info was not found?\n\n");
204 for (ptr = info; ptr != NULL; ptr = ptr->next)
212 rrd_info_free (info);
214 array_destroy (args);
218 } /* }}} int action_graph */
220 /* vim: set sw=2 sts=2 et fdm=marker : */