1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <stdint.h>
6 #include <inttypes.h>
7 #include <dirent.h> /* for PATH_MAX */
8 #include <assert.h>
9 #include <math.h>
11 #include <rrd.h>
13 #include "common.h"
14 #include "action_graph.h"
15 #include "graph_list.h"
16 #include "utils_params.h"
17 #include "utils_array.h"
19 #include <fcgiapp.h>
20 #include <fcgi_stdio.h>
22 static void emulate_graph (int argc, char **argv) /* {{{ */
23 {
24 int i;
26 printf ("rrdtool \\\n");
27 for (i = 0; i < argc; i++)
28 {
29 if (i < (argc - 1))
30 printf (" \"%s\" \\\n", argv[i]);
31 else
32 printf (" \"%s\"\n", argv[i]);
33 }
34 } /* }}} void emulate_graph */
36 static int ag_info_print (rrd_info_t *info) /* {{{ */
37 {
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);
48 else
49 printf ("[info] %s = [unknown type %#x];\n", info->key, info->type);
51 return (0);
52 } /* }}} int ag_info_print */
54 static int output_graph (rrd_info_t *info) /* {{{ */
55 {
56 rrd_info_t *img;
58 for (img = info; img != NULL; img = img->next)
59 if ((strcmp ("image", img->key) == 0)
60 && (img->type == RD_I_BLO))
61 break;
63 if (img == NULL)
64 return (ENOENT);
66 printf ("Content-Type: image/png\n"
67 "Content-Length: %lu\n"
68 "\n",
69 img->value.u_blo.size);
70 fwrite (img->value.u_blo.ptr, img->value.u_blo.size,
71 /* nmemb = */ 1, stdout);
73 return (0);
74 } /* }}} int output_graph */
76 #define OUTPUT_ERROR(...) do { \
77 printf ("Content-Type: text/plain\n\n"); \
78 printf (__VA_ARGS__); \
79 return (0); \
80 } while (0)
82 int action_graph (void) /* {{{ */
83 {
84 str_array_t *args;
85 graph_config_t *cfg;
86 graph_instance_t *inst;
87 rrd_info_t *info;
88 int status;
90 cfg = gl_graph_get_selected ();
91 if (cfg == NULL)
92 OUTPUT_ERROR ("gl_graph_get_selected () failed.\n");
94 inst = inst_get_selected (cfg);
95 if (inst == NULL)
96 OUTPUT_ERROR ("inst_get_selected (%p) failed.\n", (void *) cfg);
98 args = array_create ();
99 if (args == NULL)
100 return (ENOMEM);
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);
108 if (status != 0)
109 {
110 array_destroy (args);
111 OUTPUT_ERROR ("inst_get_rrdargs failed with status %i.\n", status);
112 }
114 rrd_clear_error ();
115 info = rrd_graph_v (array_argc (args), array_argv (args));
116 if ((info == NULL) || rrd_test_error ())
117 {
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));
121 }
122 else
123 {
124 int status;
126 status = output_graph (info);
127 if (status != 0)
128 {
129 rrd_info_t *ptr;
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)
135 {
136 ag_info_print (ptr);
137 }
138 }
139 }
141 if (info != NULL)
142 rrd_info_free (info);
144 array_destroy (args);
145 args = NULL;
147 return (0);
148 } /* }}} int action_graph */
150 /* vim: set sw=2 sts=2 et fdm=marker : */