graph_def.c: Split the config handling into two functions.
[collection4.git] / graph_def.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <errno.h>
4
5 #include "graph_def.h"
6 #include "graph_config.h"
7 #include "common.h"
8 #include "oconfig.h"
9
10 #include <fcgiapp.h>
11 #include <fcgi_stdio.h>
12
13 /*
14  * Data structures
15  */
16 struct graph_def_s
17 {
18   graph_ident_t *select;
19
20   char *ds_name;
21   char *legend;
22   uint32_t color;
23
24   graph_def_t *next;
25 };
26
27 /*
28  * Private functions
29  */
30 #define DEF_CONFIG_FIELD(field) \
31 static int def_config_##field (const oconfig_item_t *ci, graph_ident_t *ident) \
32 {                                                                              \
33   char *tmp = NULL;                                                            \
34   int status = graph_config_get_string (ci, &tmp);                             \
35   if (status != 0)                                                             \
36     return (status);                                                           \
37   ident_set_##field (ident, tmp);                                              \
38   free (tmp);                                                                  \
39   return (0);                                                                  \
40 } /* }}} int def_config_field */
41
42 DEF_CONFIG_FIELD (host);
43 DEF_CONFIG_FIELD (plugin);
44 DEF_CONFIG_FIELD (plugin_instance);
45 DEF_CONFIG_FIELD (type);
46 DEF_CONFIG_FIELD (type_instance);
47
48 #undef DEF_CONFIG_FIELD
49
50 static int def_config_color (const oconfig_item_t *ci, uint32_t *ret_color) /* {{{ */
51 {
52   char *tmp;
53   char *endptr;
54   uint32_t color;
55
56   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
57     return (EINVAL);
58
59   tmp = ci->values[0].value.string;
60
61   endptr = NULL;
62   errno = 0;
63   color = (uint32_t) strtoul (tmp, &endptr, /* base = */ 16);
64   if ((errno != 0) || (endptr == tmp) || (color > 0x00ffffff))
65     return (EINVAL);
66
67   *ret_color = color;
68
69   return (0);
70 } /* }}} int def_config_color */
71
72 static graph_def_t *def_config_get_obj (graph_config_t *cfg, /* {{{ */
73     const oconfig_item_t *ci)
74 {
75   graph_ident_t *ident;
76   char *ds_name = NULL;
77   graph_def_t *def;
78   int i;
79
80   ident = gl_graph_get_selector (cfg);
81   if (ident == NULL)
82   {
83     fprintf (stderr, "def_config_get_obj: gl_graph_get_selector failed");
84     return (NULL);
85   }
86
87   for (i = 0; i < ci->children_num; i++)
88   {
89     oconfig_item_t *child;
90
91 #define HANDLE_FIELD(name,field) \
92     else if (strcasecmp (name, child->key) == 0) \
93       def_config_##field (child, ident)
94
95     child = ci->children + i;
96     if (strcasecmp ("DSName", child->key) == 0)
97       graph_config_get_string (child, &ds_name);
98
99     HANDLE_FIELD ("Host", host);
100     HANDLE_FIELD ("Plugin", plugin);
101     HANDLE_FIELD ("PluginInstance", plugin_instance);
102     HANDLE_FIELD ("Type", type);
103     HANDLE_FIELD ("TypeInstance", type_instance);
104
105 #undef HANDLE_FIELD
106   }
107
108   def = def_create (cfg, ident, ds_name);
109   if (def == NULL)
110   {
111     fprintf (stderr, "def_config_get_obj: def_create failed\n");
112     ident_destroy (ident);
113     free (ds_name);
114     return (NULL);
115   }
116
117   ident_destroy (ident);
118   free (ds_name);
119
120   return (def);
121 } /* }}} graph_def_t *def_config_get_obj */
122
123 /*
124  * Public functions
125  */
126 graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident, /* {{{ */
127     const char *ds_name)
128 {
129   graph_ident_t *selector;
130   graph_def_t *ret;
131
132   if ((cfg == NULL) || (ident == NULL) || (ds_name == NULL))
133     return (NULL);
134
135   selector = gl_graph_get_selector (cfg);
136   if (selector == NULL)
137     return (NULL);
138
139   ret = malloc (sizeof (*ret));
140   if (ret == NULL)
141   {
142     ident_destroy (selector);
143     return (NULL);
144   }
145   memset (ret, 0, sizeof (*ret));
146   ret->legend = NULL;
147
148   ret->ds_name = strdup (ds_name);
149   if (ret->ds_name == NULL)
150   {
151     ident_destroy (selector);
152     free (ret);
153     return (NULL);
154   }
155
156   ret->color = get_random_color ();
157   ret->next = NULL;
158
159   ret->select = ident_copy_with_selector (selector, ident,
160       IDENT_FLAG_REPLACE_ALL);
161   if (ret->select == NULL)
162   {
163     ident_destroy (selector);
164     free (ret->ds_name);
165     free (ret);
166     return (NULL);
167   }
168
169   ident_destroy (selector);
170   return (ret);
171 } /* }}} graph_def_t *def_create */
172
173 void def_destroy (graph_def_t *def) /* {{{ */
174 {
175   graph_def_t *next;
176
177   if (def == NULL)
178     return;
179
180   next = def->next;
181
182   ident_destroy (def->select);
183
184   free (def->ds_name);
185
186   free (def);
187
188   def_destroy (next);
189 } /* }}} void def_destroy */
190
191 int def_config (graph_config_t *cfg, const oconfig_item_t *ci) /* {{{ */
192 {
193   graph_def_t *def;
194   int i;
195
196   def = def_config_get_obj (cfg, ci);
197   if (def == NULL)
198     return (EINVAL);
199
200   for (i = 0; i < ci->children_num; i++)
201   {
202     oconfig_item_t *child;
203
204     child = ci->children + i;
205     if (strcasecmp ("Legend", child->key) == 0)
206       graph_config_get_string (child, &def->legend);
207     else if (strcasecmp ("Color", child->key) == 0)
208       def_config_color (child, &def->color);
209   }
210
211   return (gl_graph_add_def (cfg, def));
212 } /* }}} int def_config */
213
214 int def_append (graph_def_t *head, graph_def_t *def) /* {{{ */
215 {
216   graph_def_t *ptr;
217
218   if ((head == NULL) || (def == NULL))
219     return (EINVAL);
220
221   ptr = head;
222   while (ptr->next != NULL)
223     ptr = ptr->next;
224
225   ptr->next = def;
226
227   return (0);
228 } /* }}} int def_append */
229
230 graph_def_t *def_search (graph_def_t *head, graph_ident_t *ident, /* {{{ */
231     const char *ds_name)
232 {
233   graph_def_t *ptr;
234
235   if ((head == NULL) || (ident == NULL) || (ds_name == NULL))
236     return (NULL);
237
238   for (ptr = head; ptr != NULL; ptr = ptr->next)
239   {
240     if (!ident_matches (ptr->select, ident))
241       continue;
242
243     if (strcmp (ptr->ds_name, ds_name) == 0)
244       return (ptr);
245   }
246
247   return (NULL);
248 } /* }}} graph_def_t *def_search */
249
250 _Bool def_matches (graph_def_t *def, graph_ident_t *ident) /* {{{ */
251 {
252   return (ident_matches (def->select, ident));
253 } /* }}} _Bool def_matches */
254
255 int def_foreach (graph_def_t *def, def_callback_t callback, /* {{{ */
256     void *user_data)
257 {
258   graph_def_t *ptr;
259
260   if ((def == NULL) || (callback == NULL))
261     return (EINVAL);
262
263   for (ptr = def; ptr != NULL; ptr = ptr->next)
264   {
265     int status;
266
267     status = (*callback) (ptr, user_data);
268     if (status != 0)
269       return (status);
270   }
271
272   return (0);
273 } /* }}} int def_foreach */
274
275 int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
276     str_array_t *args)
277 {
278   char *file;
279   int index;
280
281   if ((def == NULL) || (ident == NULL) || (args == NULL))
282     return (EINVAL);
283
284   file = ident_to_file (ident);
285   if (file == NULL)
286   {
287     DEBUG ("gl_ident_get_rrdargs: ident_to_file returned NULL.\n");
288     return (-1);
289   }
290
291   DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
292
293   index = array_argc (args);
294
295   /* CDEFs */
296   array_append_format (args, "DEF:def_%04i_min=%s:%s:MIN",
297       index, file, def->ds_name);
298   array_append_format (args, "DEF:def_%04i_avg=%s:%s:AVERAGE",
299       index, file, def->ds_name);
300   array_append_format (args, "DEF:def_%04i_max=%s:%s:MAX",
301       index, file, def->ds_name);
302   /* VDEFs */
303   array_append_format (args, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM",
304       index, index);
305   array_append_format (args, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE",
306       index, index);
307   array_append_format (args, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM",
308       index, index);
309   array_append_format (args, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST",
310       index, index);
311
312   /* Graph part */
313   array_append_format (args, "LINE1:def_%04i_avg#%06"PRIx32":%s",
314       index, def->color,
315       (def->legend != NULL) ? def->legend : def->ds_name);
316   array_append_format (args, "GPRINT:vdef_%04i_min:%%lg min,", index);
317   array_append_format (args, "GPRINT:vdef_%04i_avg:%%lg avg,", index);
318   array_append_format (args, "GPRINT:vdef_%04i_max:%%lg max,", index);
319   array_append_format (args, "GPRINT:vdef_%04i_lst:%%lg last\\l", index);
320
321   free (file);
322
323   return (0);
324 } /* }}} int def_get_rrdargs */
325
326 /* vim: set sw=2 sts=2 et fdm=marker : */