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>
32 typedef struct data_source_s data_source_t;
36 data_source_t *data_sources;
37 size_t data_sources_num;
43 typedef struct graph_def_s graph_def_t;
45 static void emulate_graph (int argc, char **argv) /* {{{ */
49 printf ("rrdtool \\\n");
50 for (i = 0; i < argc; i++)
53 printf (" \"%s\" \\\n", argv[i]);
55 printf (" \"%s\"\n", argv[i]);
57 } /* }}} void emulate_graph */
59 static int ag_info_print (rrd_info_t *info) /* {{{ */
61 if (info->type == RD_I_VAL)
62 printf ("[info] %s = %g;\n", info->key, info->value.u_val);
63 else if (info->type == RD_I_CNT)
64 printf ("[info] %s = %lu;\n", info->key, info->value.u_cnt);
65 else if (info->type == RD_I_STR)
66 printf ("[info] %s = %s;\n", info->key, info->value.u_str);
67 else if (info->type == RD_I_INT)
68 printf ("[info] %s = %i;\n", info->key, info->value.u_int);
69 else if (info->type == RD_I_BLO)
70 printf ("[info] %s = [blob, %lu bytes];\n", info->key, info->value.u_blo.size);
72 printf ("[info] %s = [unknown type %#x];\n", info->key, info->type);
75 } /* }}} int ag_info_print */
77 static int output_graph (rrd_info_t *info) /* {{{ */
81 for (img = info; img != NULL; img = img->next)
82 if ((strcmp ("image", img->key) == 0)
83 && (img->type == RD_I_BLO))
89 printf ("Content-Type: image/png\n"
90 "Content-Length: %lu\n"
92 img->value.u_blo.size);
93 fwrite (img->value.u_blo.ptr, img->value.u_blo.size,
94 /* nmemb = */ 1, stdout);
97 } /* }}} int output_graph */
99 #define OUTPUT_ERROR(...) do { \
100 printf ("Content-Type: text/plain\n\n"); \
101 printf (__VA_ARGS__); \
105 int action_graph (void) /* {{{ */
109 graph_instance_t *inst;
113 cfg = graph_get_selected ();
115 OUTPUT_ERROR ("graph_get_selected () failed.\n");
117 inst = inst_get_selected (cfg);
119 OUTPUT_ERROR ("inst_get_selected (%p) failed.\n", (void *) cfg);
121 args = array_create ();
125 array_append (args, "graph");
126 array_append (args, "-");
127 array_append (args, "--imgformat");
128 array_append (args, "PNG");
130 status = gl_instance_get_rrdargs (cfg, inst, args);
133 array_destroy (args);
134 OUTPUT_ERROR ("gl_instance_get_rrdargs failed with status %i.\n", status);
138 info = rrd_graph_v (array_argc (args), array_argv (args));
139 if ((info == NULL) || rrd_test_error ())
141 printf ("Content-Type: text/plain\n\n");
142 printf ("rrd_graph_v failed: %s\n", rrd_get_error ());
143 emulate_graph (array_argc (args), array_argv (args));
149 status = output_graph (info);
154 printf ("Content-Type: text/plain\n\n");
155 printf ("output_graph failed. Maybe the \"image\" info was not found?\n\n");
157 for (ptr = info; ptr != NULL; ptr = ptr->next)
165 rrd_info_free (info);
167 array_destroy (args);
171 } /* }}} int action_graph */
173 /* vim: set sw=2 sts=2 et fdm=marker : */