oconfig.c: Fix compiler warning.
[collection4.git] / graph.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <stdint.h>
4 #include <inttypes.h>
5 #include <string.h>
6 #include <time.h>
7 #include <errno.h>
8 #include <assert.h>
9
10 #include "graph.h"
11 #include "graph_list.h"
12 #include "graph_ident.h"
13 #include "graph_def.h"
14 #include "graph_config.h"
15 #include "common.h"
16 #include "filesystem.h"
17 #include "utils_params.h"
18
19 #include <fcgiapp.h>
20 #include <fcgi_stdio.h>
21
22 /*
23  * Data types
24  */
25 struct graph_config_s /* {{{ */
26 {
27   graph_ident_t *select;
28
29   char *title;
30   char *vertical_label;
31
32   graph_def_t *defs;
33
34   graph_instance_t *instances;
35 }; /* }}} struct graph_config_s */
36
37 /*
38  * Private functions
39  */
40 static graph_instance_t *graph_find_instance (graph_config_t *cfg, /* {{{ */
41     const graph_ident_t *ident)
42 {
43   if ((cfg == NULL) || (ident == NULL))
44     return (NULL);
45
46   return (inst_find_matching (cfg->instances, ident));
47 } /* }}} graph_instance_t *graph_find_instance */
48
49 /*
50  * Config functions
51  */
52 static graph_ident_t *graph_config_get_selector (const oconfig_item_t *ci) /* {{{ */
53 {
54   char *host = NULL;
55   char *plugin = NULL;
56   char *plugin_instance = NULL;
57   char *type = NULL;
58   char *type_instance = NULL;
59   graph_ident_t *ret;
60   int i;
61
62   for (i = 0; i < ci->children_num; i++)
63   {
64     oconfig_item_t *child;
65
66     child = ci->children + i;
67
68     if (strcasecmp ("Host", child->key) == 0)
69       graph_config_get_string (child, &host);
70     else if (strcasecmp ("Plugin", child->key) == 0)
71       graph_config_get_string (child, &plugin);
72     else if (strcasecmp ("PluginInstance", child->key) == 0)
73       graph_config_get_string (child, &plugin_instance);
74     else if (strcasecmp ("Type", child->key) == 0)
75       graph_config_get_string (child, &type);
76     else if (strcasecmp ("TypeInstance", child->key) == 0)
77       graph_config_get_string (child, &type_instance);
78     /* else: ignore all other directives here. */
79   } /* for */
80
81   ret = ident_create (host, plugin, plugin_instance, type, type_instance);
82
83   free (host);
84   free (plugin);
85   free (plugin_instance);
86   free (type);
87   free (type_instance);
88
89   return (ret);
90 } /* }}} int graph_config_get_selector */
91
92 /*
93  * Global functions
94  */
95 graph_config_t *graph_create (const graph_ident_t *selector) /* {{{ */
96 {
97   graph_config_t *cfg;
98
99   cfg = malloc (sizeof (*cfg));
100   if (cfg == NULL)
101     return (NULL);
102   memset (cfg, 0, sizeof (*cfg));
103
104   if (selector != NULL)
105     cfg->select = ident_clone (selector);
106   else
107     cfg->select = NULL;
108
109   cfg->title = NULL;
110   cfg->vertical_label = NULL;
111   cfg->defs = NULL;
112   cfg->instances = NULL;
113
114   return (cfg);
115 } /* }}} int graph_create */
116
117 void graph_destroy (graph_config_t *cfg) /* {{{ */
118 {
119   if (cfg == NULL)
120     return;
121
122   ident_destroy (cfg->select);
123
124   free (cfg->title);
125   free (cfg->vertical_label);
126
127   def_destroy (cfg->defs);
128   inst_destroy (cfg->instances);
129 } /* }}} void graph_destroy */
130
131 int graph_config_add (const oconfig_item_t *ci) /* {{{ */
132 {
133   graph_ident_t *select;
134   graph_config_t *cfg = NULL;
135   int i;
136
137   select = graph_config_get_selector (ci);
138   if (select == NULL)
139     return (EINVAL);
140
141   cfg = graph_create (/* selector = */ NULL);
142   if (cfg == NULL)
143     return (ENOMEM);
144
145   cfg->select = select;
146
147   for (i = 0; i < ci->children_num; i++)
148   {
149     oconfig_item_t *child;
150
151     child = ci->children + i;
152
153     if (strcasecmp ("Title", child->key) == 0)
154       graph_config_get_string (child, &cfg->title);
155     else if (strcasecmp ("VerticalLabel", child->key) == 0)
156       graph_config_get_string (child, &cfg->vertical_label);
157     else if (strcasecmp ("DEF", child->key) == 0)
158       def_config (cfg, child);
159   } /* for */
160
161   gl_add_graph (cfg);
162
163   return (0);
164 } /* }}} graph_config_add */
165
166 int graph_add_file (graph_config_t *cfg, const graph_ident_t *file) /* {{{ */
167 {
168   graph_instance_t *inst;
169
170   inst = graph_find_instance (cfg, file);
171   if (inst == NULL)
172   {
173     inst = inst_create (cfg, file);
174     if (inst == NULL)
175       return (ENOMEM);
176
177     if (cfg->instances == NULL)
178       cfg->instances = inst;
179     else
180       inst_append (cfg->instances, inst);
181   }
182
183   return (inst_add_file (inst, file));
184 } /* }}} int graph_add_file */
185
186 int graph_get_title (graph_config_t *cfg, /* {{{ */
187     char *buffer, size_t buffer_size)
188 {
189   if ((cfg == NULL) || (buffer == NULL) || (buffer_size < 1))
190     return (EINVAL);
191
192   if (cfg->title == NULL)
193     cfg->title = ident_to_string (cfg->select);
194
195   if (cfg->title == NULL)
196     return (ENOMEM);
197
198   strncpy (buffer, cfg->title, buffer_size);
199   buffer[buffer_size - 1] = 0;
200
201   return (0);
202 } /* }}} int graph_get_title */
203
204 graph_ident_t *graph_get_selector (graph_config_t *cfg) /* {{{ */
205 {
206   if (cfg == NULL)
207     return (NULL);
208
209   return (ident_clone (cfg->select));
210 } /* }}} graph_ident_t *graph_get_selector */
211
212 graph_instance_t *graph_get_instances (graph_config_t *cfg) /* {{{ */
213 {
214   if (cfg == NULL)
215     return (NULL);
216
217   return (cfg->instances);
218 } /* }}} graph_instance_t *graph_get_instances */
219
220 graph_def_t *graph_get_defs (graph_config_t *cfg) /* {{{ */
221 {
222   if (cfg == NULL)
223     return (NULL);
224
225   return (cfg->defs);
226 } /* }}} graph_def_t *graph_get_defs */
227
228 int graph_add_def (graph_config_t *cfg, graph_def_t *def) /* {{{ */
229 {
230   if ((cfg == NULL) || (def == NULL))
231     return (EINVAL);
232
233   if (cfg->defs == NULL)
234   {
235     cfg->defs = def;
236     return (0);
237   }
238
239   return (def_append (cfg->defs, def));
240 } /* }}} int graph_add_def */
241
242 _Bool graph_matches (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
243 {
244   if ((cfg == NULL) || (ident == NULL))
245     return (0);
246
247   return (ident_matches (cfg->select, ident));
248 } /* }}} _Bool graph_matches */
249
250 int graph_compare (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
251 {
252   if ((cfg == NULL) || (ident == NULL))
253     return (0);
254
255   return (ident_compare (cfg->select, ident));
256 } /* }}} int graph_compare */
257
258 int graph_clear_instances (graph_config_t *cfg) /* {{{ */
259 {
260   if (cfg == NULL)
261     return (EINVAL);
262
263   inst_destroy (cfg->instances);
264   cfg->instances = NULL;
265
266   return (0);
267 } /* }}} int graph_clear_instances */
268
269 int graph_get_rrdargs (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
270     str_array_t *args)
271 {
272   if ((cfg == NULL) || (inst == NULL) || (args == NULL))
273     return (EINVAL);
274
275   if (cfg->title != NULL)
276   {
277     array_append (args, "-t");
278     array_append (args, cfg->title);
279   }
280
281   if (cfg->vertical_label != NULL)
282   {
283     array_append (args, "-v");
284     array_append (args, cfg->vertical_label);
285   }
286
287   return (0);
288 } /* }}} int graph_get_rrdargs */
289
290 /* vim: set sw=2 sts=2 et fdm=marker : */