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 int graph_def_add_ds (graph_def_t *gd, /* {{{ */
43 const char *in_ds_name, size_t ds_name_len)
45 char ds_name[ds_name_len + 1];
48 strncpy (ds_name, in_ds_name, sizeof (ds_name));
49 ds_name[sizeof (ds_name) - 1] = 0;
51 ds = realloc (gd->data_sources, sizeof (*ds) * (gd->data_sources_num + 1));
54 gd->data_sources = ds;
56 ds = gd->data_sources + gd->data_sources_num;
57 memset (ds, 0, sizeof (*ds));
59 ds->file = strdup (file);
63 ds->name = strdup (ds_name);
73 gd->data_sources_num++;
76 } /* }}} int graph_def_add_ds */
78 static graph_def_t *graph_def_from_rrd_file (char *file) /* {{{ */
80 char *rrd_argv[] = { "info", file, NULL };
81 int rrd_argc = (sizeof (rrd_argv) / sizeof (rrd_argv[0])) - 1;
86 gd = malloc (sizeof (*gd));
89 memset (gd, 0, sizeof (*gd));
91 gd->data_sources = NULL;
93 info = rrd_info (rrd_argc, rrd_argv);
96 printf ("%s: rrd_info (%s) failed.\n", __func__, file);
101 for (ptr = info; ptr != NULL; ptr = ptr->next)
106 if (strncmp ("ds[", ptr->key, strlen ("ds[")) != 0)
109 keylen = strlen (ptr->key);
110 if (keylen < strlen ("ds[?].index"))
113 dslen = keylen - strlen ("ds[].index");
116 if (strcmp ("].index", ptr->key + (strlen ("ds[") + dslen)) != 0)
119 graph_def_add_ds (gd, file, ptr->key + strlen ("ds["), dslen);
122 rrd_info_free (info);
125 } /* }}} graph_def_t *graph_def_from_rrd_file */
127 static graph_def_t *graph_def_from_gl (const graph_list_t *gl) /* {{{ */
129 char rrd_file[PATH_MAX];
131 if ((gl->plugin_instance == NULL) && (gl->type_instance == NULL))
132 snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s/%s.rrd",
133 DATA_DIR, gl->host, gl->plugin, gl->type);
134 else if (gl->type_instance == NULL)
135 snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s-%s/%s.rrd",
136 DATA_DIR, gl->host, gl->plugin, gl->plugin_instance, gl->type);
137 else if (gl->plugin_instance == NULL)
138 snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s/%s-%s.rrd",
139 DATA_DIR, gl->host, gl->plugin, gl->type, gl->type_instance);
141 snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s-%s/%s-%s.rrd",
142 DATA_DIR, gl->host, gl->plugin, gl->plugin_instance,
143 gl->type, gl->type_instance);
144 rrd_file[sizeof (rrd_file) - 1] = 0;
146 printf ("rrd_file = %s;\n", rrd_file);
148 return (graph_def_from_rrd_file (rrd_file));
149 } /* }}} graph_def_t *graph_def_from_gl */
151 static int init_gl (graph_list_t *gl) /* {{{ */
153 gl->host = param ("host");
154 gl->plugin = param ("plugin");
155 gl->plugin_instance = param ("plugin_instance");
156 gl->type = param ("type");
157 gl->type_instance = param ("type_instance");
159 if ((gl->host == NULL)
160 || (gl->plugin == NULL)
161 || (gl->type == NULL))
164 if ((gl->host[0] == 0) || (gl->host[0] == '.')
165 || (gl->plugin[0] == 0) || (gl->plugin[0] == '.')
166 || (gl->type[0] == 0) || (gl->type[0] == '.'))
169 if ((strchr (gl->plugin, '-') != NULL)
170 || (strchr (gl->type, '-') != NULL))
173 if ((gl->plugin_instance != NULL)
174 && (gl->plugin_instance[0] == 0))
175 gl->plugin_instance = NULL;
177 if ((gl->type_instance != NULL)
178 && (gl->type_instance[0] == 0))
179 gl->type_instance = NULL;
182 } /* }}} int init_gl */
184 int action_graph (void) /* {{{ */
191 memset (&gl, 0, sizeof (gl));
192 status = init_gl (&gl);
195 printf ("Content-Type: text/plain\n\n"
196 "init_gl failed with status %i.\n", status);
200 printf ("Content-Type: text/plain\n\n"
201 "Hello, this is %s\n", __func__);
202 gd = graph_def_from_gl (&gl);
205 printf ("graph_def_from_gl failed.\n");
209 for (i = 0; i < gd->data_sources_num; i++)
211 printf ("data source %lu: %s @ %s\n",
212 (unsigned long) i, gd->data_sources[i].name, gd->data_sources[i].file);
218 } /* }}} int action_graph */
220 /* vim: set sw=2 sts=2 et fdm=marker : */