197c6b50fb55074185045ec2fb8c158ebe1071e7
1 #include <stdlib.h>
2 #include <string.h>
3 #include <errno.h>
5 #include "graph_def.h"
6 #include "graph.h"
7 #include "graph_config.h"
8 #include "graph_ident.h"
9 #include "common.h"
10 #include "oconfig.h"
12 #include <fcgiapp.h>
13 #include <fcgi_stdio.h>
15 /*
16 * Data structures
17 */
18 struct graph_def_s
19 {
20 graph_ident_t *select;
22 char *ds_name;
23 char *legend;
24 uint32_t color;
25 _Bool stack;
26 _Bool area;
27 char *format;
29 graph_def_t *next;
30 };
32 /*
33 * Private functions
34 */
35 #define DEF_CONFIG_FIELD(field) \
36 static int def_config_##field (const oconfig_item_t *ci, graph_ident_t *ident) \
37 { \
38 char *tmp = NULL; \
39 int status = graph_config_get_string (ci, &tmp); \
40 if (status != 0) \
41 return (status); \
42 ident_set_##field (ident, tmp); \
43 free (tmp); \
44 return (0); \
45 } /* }}} int def_config_field */
47 DEF_CONFIG_FIELD (host);
48 DEF_CONFIG_FIELD (plugin);
49 DEF_CONFIG_FIELD (plugin_instance);
50 DEF_CONFIG_FIELD (type);
51 DEF_CONFIG_FIELD (type_instance);
53 #undef DEF_CONFIG_FIELD
55 static int def_config_color (const oconfig_item_t *ci, uint32_t *ret_color) /* {{{ */
56 {
57 char *tmp;
58 char *endptr;
59 uint32_t color;
61 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
62 return (EINVAL);
64 tmp = ci->values[0].value.string;
66 endptr = NULL;
67 errno = 0;
68 color = (uint32_t) strtoul (tmp, &endptr, /* base = */ 16);
69 if ((errno != 0) || (endptr == tmp) || (color > 0x00ffffff))
70 return (EINVAL);
72 *ret_color = color;
74 return (0);
75 } /* }}} int def_config_color */
77 static graph_def_t *def_config_get_obj (graph_config_t *cfg, /* {{{ */
78 const oconfig_item_t *ci)
79 {
80 graph_ident_t *ident;
81 char *ds_name = NULL;
82 graph_def_t *def;
83 int i;
85 ident = graph_get_selector (cfg);
86 if (ident == NULL)
87 {
88 fprintf (stderr, "def_config_get_obj: graph_get_selector failed");
89 return (NULL);
90 }
92 for (i = 0; i < ci->children_num; i++)
93 {
94 oconfig_item_t *child;
96 #define HANDLE_FIELD(name,field) \
97 else if (strcasecmp (name, child->key) == 0) \
98 def_config_##field (child, ident)
100 child = ci->children + i;
101 if (strcasecmp ("DSName", child->key) == 0)
102 graph_config_get_string (child, &ds_name);
104 HANDLE_FIELD ("Host", host);
105 HANDLE_FIELD ("Plugin", plugin);
106 HANDLE_FIELD ("PluginInstance", plugin_instance);
107 HANDLE_FIELD ("Type", type);
108 HANDLE_FIELD ("TypeInstance", type_instance);
110 #undef HANDLE_FIELD
111 }
113 def = def_create (cfg, ident, ds_name);
114 if (def == NULL)
115 {
116 fprintf (stderr, "def_config_get_obj: def_create failed\n");
117 ident_destroy (ident);
118 free (ds_name);
119 return (NULL);
120 }
122 ident_destroy (ident);
123 free (ds_name);
125 return (def);
126 } /* }}} graph_def_t *def_config_get_obj */
128 /*
129 * Public functions
130 */
131 graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident, /* {{{ */
132 const char *ds_name)
133 {
134 graph_ident_t *selector;
135 graph_def_t *ret;
137 if ((cfg == NULL) || (ident == NULL) || (ds_name == NULL))
138 return (NULL);
140 selector = graph_get_selector (cfg);
141 if (selector == NULL)
142 return (NULL);
144 ret = malloc (sizeof (*ret));
145 if (ret == NULL)
146 {
147 ident_destroy (selector);
148 return (NULL);
149 }
150 memset (ret, 0, sizeof (*ret));
151 ret->legend = NULL;
152 ret->format = NULL;
154 ret->ds_name = strdup (ds_name);
155 if (ret->ds_name == NULL)
156 {
157 ident_destroy (selector);
158 free (ret);
159 return (NULL);
160 }
162 ret->color = get_random_color ();
163 ret->next = NULL;
165 ret->select = ident_copy_with_selector (selector, ident,
166 IDENT_FLAG_REPLACE_ALL);
167 if (ret->select == NULL)
168 {
169 ident_destroy (selector);
170 free (ret->ds_name);
171 free (ret);
172 return (NULL);
173 }
175 ident_destroy (selector);
176 return (ret);
177 } /* }}} graph_def_t *def_create */
179 void def_destroy (graph_def_t *def) /* {{{ */
180 {
181 graph_def_t *next;
183 if (def == NULL)
184 return;
186 next = def->next;
188 ident_destroy (def->select);
190 free (def->ds_name);
191 free (def->legend);
192 free (def->format);
194 free (def);
196 def_destroy (next);
197 } /* }}} void def_destroy */
199 int def_config (graph_config_t *cfg, const oconfig_item_t *ci) /* {{{ */
200 {
201 graph_def_t *def;
202 int i;
204 def = def_config_get_obj (cfg, ci);
205 if (def == NULL)
206 return (EINVAL);
208 for (i = 0; i < ci->children_num; i++)
209 {
210 oconfig_item_t *child;
212 child = ci->children + i;
213 if (strcasecmp ("Legend", child->key) == 0)
214 graph_config_get_string (child, &def->legend);
215 else if (strcasecmp ("Color", child->key) == 0)
216 def_config_color (child, &def->color);
217 else if (strcasecmp ("Stack", child->key) == 0)
218 graph_config_get_bool (child, &def->stack);
219 else if (strcasecmp ("Area", child->key) == 0)
220 graph_config_get_bool (child, &def->area);
221 else if (strcasecmp ("Format", child->key) == 0)
222 graph_config_get_string (child, &def->format);
223 }
225 return (graph_add_def (cfg, def));
226 } /* }}} int def_config */
228 int def_append (graph_def_t *head, graph_def_t *def) /* {{{ */
229 {
230 graph_def_t *ptr;
232 if ((head == NULL) || (def == NULL))
233 return (EINVAL);
235 ptr = head;
236 while (ptr->next != NULL)
237 ptr = ptr->next;
239 ptr->next = def;
241 return (0);
242 } /* }}} int def_append */
244 graph_def_t *def_search (graph_def_t *head, graph_ident_t *ident, /* {{{ */
245 const char *ds_name)
246 {
247 graph_def_t *ptr;
249 if ((head == NULL) || (ident == NULL) || (ds_name == NULL))
250 return (NULL);
252 for (ptr = head; ptr != NULL; ptr = ptr->next)
253 {
254 if (!ident_matches (ptr->select, ident))
255 continue;
257 if (strcmp (ptr->ds_name, ds_name) == 0)
258 return (ptr);
259 }
261 return (NULL);
262 } /* }}} graph_def_t *def_search */
264 _Bool def_matches (graph_def_t *def, graph_ident_t *ident) /* {{{ */
265 {
266 return (ident_matches (def->select, ident));
267 } /* }}} _Bool def_matches */
269 int def_foreach (graph_def_t *def, def_callback_t callback, /* {{{ */
270 void *user_data)
271 {
272 graph_def_t *ptr;
274 if ((def == NULL) || (callback == NULL))
275 return (EINVAL);
277 for (ptr = def; ptr != NULL; ptr = ptr->next)
278 {
279 int status;
281 status = (*callback) (ptr, user_data);
282 if (status != 0)
283 return (status);
284 }
286 return (0);
287 } /* }}} int def_foreach */
289 int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
290 rrd_args_t *args)
291 {
292 char *file;
293 int index;
295 if ((def == NULL) || (ident == NULL) || (args == NULL))
296 return (EINVAL);
298 file = ident_to_file (ident);
299 if (file == NULL)
300 {
301 DEBUG ("gl_ident_get_rrdargs: ident_to_file returned NULL.\n");
302 return (-1);
303 }
305 DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
307 index = args->index;
308 args->index++;
310 /* CDEFs */
311 array_append_format (args->data, "DEF:def_%04i_min=%s:%s:MIN",
312 index, file, def->ds_name);
313 array_append_format (args->data, "DEF:def_%04i_avg=%s:%s:AVERAGE",
314 index, file, def->ds_name);
315 array_append_format (args->data, "DEF:def_%04i_max=%s:%s:MAX",
316 index, file, def->ds_name);
317 /* VDEFs */
318 array_append_format (args->data, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM",
319 index, index);
320 array_append_format (args->data, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE",
321 index, index);
322 array_append_format (args->data, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM",
323 index, index);
324 array_append_format (args->data, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST",
325 index, index);
327 /* Graph part */
328 array_append_format (args->draw, "%s:def_%04i_avg#%06"PRIx32":%s%s",
329 def->area ? "AREA" : "LINE1",
330 index, def->color,
331 (def->legend != NULL) ? def->legend : def->ds_name,
332 def->stack ? ":STACK" : "");
333 array_append_format (args->draw, "GPRINT:vdef_%04i_min:%s min,",
334 index, (def->format != NULL) ? def->format : "%6.2lf");
335 array_append_format (args->draw, "GPRINT:vdef_%04i_avg:%s avg,",
336 index, (def->format != NULL) ? def->format : "%6.2lf");
337 array_append_format (args->draw, "GPRINT:vdef_%04i_max:%s max,",
338 index, (def->format != NULL) ? def->format : "%6.2lf");
339 array_append_format (args->draw, "GPRINT:vdef_%04i_lst:%s last\\l",
340 index, (def->format != NULL) ? def->format : "%6.2lf");
342 free (file);
344 return (0);
345 } /* }}} int def_get_rrdargs */
347 /* vim: set sw=2 sts=2 et fdm=marker : */