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>
25 typedef struct data_source_s data_source_t;
29 data_source_t *data_sources;
30 size_t data_sources_num;
36 typedef struct graph_def_s graph_def_t;
38 static void emulate_graph (int argc, char **argv) /* {{{ */
42 printf ("rrdtool \\\n");
43 for (i = 0; i < argc; i++)
46 printf (" \"%s\" \\\n", argv[i]);
48 printf (" \"%s\"\n", argv[i]);
50 } /* }}} void emulate_graph */
52 static int ag_info_print (rrd_info_t *info) /* {{{ */
54 if (info->type == RD_I_VAL)
55 printf ("[info] %s = %g;\n", info->key, info->value.u_val);
56 else if (info->type == RD_I_CNT)
57 printf ("[info] %s = %lu;\n", info->key, info->value.u_cnt);
58 else if (info->type == RD_I_STR)
59 printf ("[info] %s = %s;\n", info->key, info->value.u_str);
60 else if (info->type == RD_I_INT)
61 printf ("[info] %s = %i;\n", info->key, info->value.u_int);
62 else if (info->type == RD_I_BLO)
63 printf ("[info] %s = [blob, %lu bytes];\n", info->key, info->value.u_blo.size);
65 printf ("[info] %s = [unknown type %#x];\n", info->key, info->type);
68 } /* }}} int ag_info_print */
70 static int output_graph (rrd_info_t *info) /* {{{ */
74 for (img = info; img != NULL; img = img->next)
75 if ((strcmp ("image", img->key) == 0)
76 && (img->type == RD_I_BLO))
82 printf ("Content-Type: image/png\n"
83 "Content-Length: %lu\n"
85 img->value.u_blo.size);
86 fwrite (img->value.u_blo.ptr, img->value.u_blo.size,
87 /* nmemb = */ 1, stdout);
90 } /* }}} int output_graph */
92 #define OUTPUT_ERROR(...) do { \
93 printf ("Content-Type: text/plain\n\n"); \
94 printf (__VA_ARGS__); \
98 int action_graph (void) /* {{{ */
102 graph_instance_t *inst;
106 cfg = graph_get_selected ();
108 OUTPUT_ERROR ("graph_get_selected () failed.\n");
110 inst = inst_get_selected (cfg);
112 OUTPUT_ERROR ("inst_get_selected (%p) failed.\n", (void *) cfg);
114 args = array_create ();
118 array_append (args, "graph");
119 array_append (args, "-");
120 array_append (args, "--imgformat");
121 array_append (args, "PNG");
123 status = gl_instance_get_rrdargs (cfg, inst, args);
126 array_destroy (args);
127 OUTPUT_ERROR ("gl_instance_get_rrdargs failed with status %i.\n", status);
131 info = rrd_graph_v (array_argc (args), array_argv (args));
132 if ((info == NULL) || rrd_test_error ())
134 printf ("Content-Type: text/plain\n\n");
135 printf ("rrd_graph_v failed: %s\n", rrd_get_error ());
136 emulate_graph (array_argc (args), array_argv (args));
142 status = output_graph (info);
147 printf ("Content-Type: text/plain\n\n");
148 printf ("output_graph failed. Maybe the \"image\" info was not found?\n\n");
150 for (ptr = info; ptr != NULL; ptr = ptr->next)
158 rrd_info_free (info);
160 array_destroy (args);
164 } /* }}} int action_graph */
166 /* vim: set sw=2 sts=2 et fdm=marker : */