7 #include <dirent.h> /* for PATH_MAX */
11 #include <fcgi_stdio.h>
16 #include "action_graph.h"
17 #include "graph_list.h"
18 #include "utils_params.h"
30 typedef struct data_source_s data_source_t;
34 data_source_t *data_sources;
35 size_t data_sources_num;
39 typedef struct graph_def_s graph_def_t;
41 static void graph_def_free (graph_def_t *gd)
48 for (i = 0; i < gd->data_sources_num; i++)
50 free (gd->data_sources[i].file);
51 free (gd->data_sources[i].name);
52 free (gd->data_sources[i].legend);
54 free (gd->data_sources);
56 } /* }}} void graph_def_free */
58 static int graph_def_add_ds (graph_def_t *gd, /* {{{ */
60 const char *in_ds_name, size_t ds_name_len)
62 char ds_name[ds_name_len + 1];
65 strncpy (ds_name, in_ds_name, sizeof (ds_name));
66 ds_name[sizeof (ds_name) - 1] = 0;
68 ds = realloc (gd->data_sources, sizeof (*ds) * (gd->data_sources_num + 1));
71 gd->data_sources = ds;
73 ds = gd->data_sources + gd->data_sources_num;
74 memset (ds, 0, sizeof (*ds));
76 ds->file = strdup (file);
80 ds->name = strdup (ds_name);
90 gd->data_sources_num++;
93 } /* }}} int graph_def_add_ds */
95 static graph_def_t *graph_def_from_rrd_file (char *file) /* {{{ */
97 char *rrd_argv[] = { "info", file, NULL };
98 int rrd_argc = (sizeof (rrd_argv) / sizeof (rrd_argv[0])) - 1;
103 gd = malloc (sizeof (*gd));
106 memset (gd, 0, sizeof (*gd));
108 gd->data_sources = NULL;
110 info = rrd_info (rrd_argc, rrd_argv);
113 printf ("%s: rrd_info (%s) failed.\n", __func__, file);
118 for (ptr = info; ptr != NULL; ptr = ptr->next)
123 if (strncmp ("ds[", ptr->key, strlen ("ds[")) != 0)
126 keylen = strlen (ptr->key);
127 if (keylen < strlen ("ds[?].index"))
130 dslen = keylen - strlen ("ds[].index");
133 if (strcmp ("].index", ptr->key + (strlen ("ds[") + dslen)) != 0)
136 graph_def_add_ds (gd, file, ptr->key + strlen ("ds["), dslen);
139 rrd_info_free (info);
142 } /* }}} graph_def_t *graph_def_from_rrd_file */
144 static graph_def_t *graph_def_from_gl (const graph_list_t *gl) /* {{{ */
146 char rrd_file[PATH_MAX];
148 if ((gl->plugin_instance == NULL) && (gl->type_instance == NULL))
149 snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s/%s.rrd",
150 DATA_DIR, gl->host, gl->plugin, gl->type);
151 else if (gl->type_instance == NULL)
152 snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s-%s/%s.rrd",
153 DATA_DIR, gl->host, gl->plugin, gl->plugin_instance, gl->type);
154 else if (gl->plugin_instance == NULL)
155 snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s/%s-%s.rrd",
156 DATA_DIR, gl->host, gl->plugin, gl->type, gl->type_instance);
158 snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s-%s/%s-%s.rrd",
159 DATA_DIR, gl->host, gl->plugin, gl->plugin_instance,
160 gl->type, gl->type_instance);
161 rrd_file[sizeof (rrd_file) - 1] = 0;
163 printf ("rrd_file = %s;\n", rrd_file);
165 return (graph_def_from_rrd_file (rrd_file));
166 } /* }}} graph_def_t *graph_def_from_gl */
168 static int init_gl (graph_list_t *gl) /* {{{ */
170 gl->host = param ("host");
171 gl->plugin = param ("plugin");
172 gl->plugin_instance = param ("plugin_instance");
173 gl->type = param ("type");
174 gl->type_instance = param ("type_instance");
176 if ((gl->host == NULL)
177 || (gl->plugin == NULL)
178 || (gl->type == NULL))
181 if ((gl->host[0] == 0) || (gl->host[0] == '.')
182 || (gl->plugin[0] == 0) || (gl->plugin[0] == '.')
183 || (gl->type[0] == 0) || (gl->type[0] == '.'))
186 if ((strchr (gl->plugin, '-') != NULL)
187 || (strchr (gl->type, '-') != NULL))
190 if ((gl->plugin_instance != NULL)
191 && (gl->plugin_instance[0] == 0))
192 gl->plugin_instance = NULL;
194 if ((gl->type_instance != NULL)
195 && (gl->type_instance[0] == 0))
196 gl->type_instance = NULL;
199 } /* }}} int init_gl */
201 int action_graph (void) /* {{{ */
208 memset (&gl, 0, sizeof (gl));
209 status = init_gl (&gl);
212 printf ("Content-Type: text/plain\n\n"
213 "init_gl failed with status %i.\n", status);
217 printf ("Content-Type: text/plain\n\n"
218 "Hello, this is %s\n", __func__);
219 gd = graph_def_from_gl (&gl);
222 printf ("graph_def_from_gl failed.\n");
226 for (i = 0; i < gd->data_sources_num; i++)
228 printf ("data source %lu: %s @ %s\n",
229 (unsigned long) i, gd->data_sources[i].name, gd->data_sources[i].file);
235 } /* }}} int action_graph */
237 /* vim: set sw=2 sts=2 et fdm=marker : */