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 void emulate_graph (int argc, char **argv) /* {{{ */
26 printf ("rrdtool \\\n");
27 for (i = 0; i < argc; i++)
30 printf (" \"%s\" \\\n", argv[i]);
32 printf (" \"%s\"\n", argv[i]);
34 } /* }}} void emulate_graph */
36 static int ag_info_print (rrd_info_t *info) /* {{{ */
38 if (info->type == RD_I_VAL)
39 printf ("[info] %s = %g;\n", info->key, info->value.u_val);
40 else if (info->type == RD_I_CNT)
41 printf ("[info] %s = %lu;\n", info->key, info->value.u_cnt);
42 else if (info->type == RD_I_STR)
43 printf ("[info] %s = %s;\n", info->key, info->value.u_str);
44 else if (info->type == RD_I_INT)
45 printf ("[info] %s = %i;\n", info->key, info->value.u_int);
46 else if (info->type == RD_I_BLO)
47 printf ("[info] %s = [blob, %lu bytes];\n", info->key, info->value.u_blo.size);
49 printf ("[info] %s = [unknown type %#x];\n", info->key, info->type);
52 } /* }}} int ag_info_print */
54 static int output_graph (rrd_info_t *info) /* {{{ */
58 for (img = info; img != NULL; img = img->next)
59 if ((strcmp ("image", img->key) == 0)
60 && (img->type == RD_I_BLO))
66 printf ("Content-Type: image/png\n"
67 "Content-Length: %lu\n"
69 img->value.u_blo.size);
70 fwrite (img->value.u_blo.ptr, img->value.u_blo.size,
71 /* nmemb = */ 1, stdout);
74 } /* }}} int output_graph */
76 #define OUTPUT_ERROR(...) do { \
77 printf ("Content-Type: text/plain\n\n"); \
78 printf (__VA_ARGS__); \
82 int action_graph (void) /* {{{ */
86 graph_instance_t *inst;
90 cfg = gl_graph_get_selected ();
92 OUTPUT_ERROR ("gl_graph_get_selected () failed.\n");
94 inst = inst_get_selected (cfg);
96 OUTPUT_ERROR ("inst_get_selected (%p) failed.\n", (void *) cfg);
98 args = array_create ();
102 array_append (args, "graph");
103 array_append (args, "-");
104 array_append (args, "--imgformat");
105 array_append (args, "PNG");
107 status = inst_get_rrdargs (cfg, inst, args);
110 array_destroy (args);
111 OUTPUT_ERROR ("inst_get_rrdargs failed with status %i.\n", status);
115 info = rrd_graph_v (array_argc (args), array_argv (args));
116 if ((info == NULL) || rrd_test_error ())
118 printf ("Content-Type: text/plain\n\n");
119 printf ("rrd_graph_v failed: %s\n", rrd_get_error ());
120 emulate_graph (array_argc (args), array_argv (args));
126 status = output_graph (info);
131 printf ("Content-Type: text/plain\n\n");
132 printf ("output_graph failed. Maybe the \"image\" info was not found?\n\n");
134 for (ptr = info; ptr != NULL; ptr = ptr->next)
142 rrd_info_free (info);
144 array_destroy (args);
148 } /* }}} int action_graph */
150 /* vim: set sw=2 sts=2 et fdm=marker : */