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>
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"
19 #include <fcgiapp.h>
20 #include <fcgi_stdio.h>
22 /*
23 * Data types
24 */
25 struct graph_config_s /* {{{ */
26 {
27 graph_ident_t *select;
29 char *title;
30 char *vertical_label;
32 graph_def_t *defs;
34 graph_instance_t *instances;
35 }; /* }}} struct graph_config_s */
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);
46 return (inst_find_matching (cfg->instances, ident));
47 } /* }}} graph_instance_t *graph_find_instance */
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;
62 for (i = 0; i < ci->children_num; i++)
63 {
64 oconfig_item_t *child;
66 child = ci->children + i;
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 */
81 ret = ident_create (host, plugin, plugin_instance, type, type_instance);
83 free (host);
84 free (plugin);
85 free (plugin_instance);
86 free (type);
87 free (type_instance);
89 return (ret);
90 } /* }}} int graph_config_get_selector */
92 /*
93 * Global functions
94 */
95 graph_config_t *graph_create (const graph_ident_t *selector) /* {{{ */
96 {
97 graph_config_t *cfg;
99 cfg = malloc (sizeof (*cfg));
100 if (cfg == NULL)
101 return (NULL);
102 memset (cfg, 0, sizeof (*cfg));
104 if (selector != NULL)
105 cfg->select = ident_clone (selector);
106 else
107 cfg->select = NULL;
109 cfg->title = NULL;
110 cfg->vertical_label = NULL;
111 cfg->defs = NULL;
112 cfg->instances = NULL;
114 return (cfg);
115 } /* }}} int graph_create */
117 void graph_destroy (graph_config_t *cfg) /* {{{ */
118 {
119 if (cfg == NULL)
120 return;
122 ident_destroy (cfg->select);
124 free (cfg->title);
125 free (cfg->vertical_label);
127 def_destroy (cfg->defs);
128 inst_destroy (cfg->instances);
129 } /* }}} void graph_destroy */
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;
137 select = graph_config_get_selector (ci);
138 if (select == NULL)
139 return (EINVAL);
141 cfg = graph_create (/* selector = */ NULL);
142 if (cfg == NULL)
143 return (ENOMEM);
145 cfg->select = select;
147 for (i = 0; i < ci->children_num; i++)
148 {
149 oconfig_item_t *child;
151 child = ci->children + i;
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 */
161 gl_add_graph (cfg);
163 return (0);
164 } /* }}} graph_config_add */
166 int graph_add_file (graph_config_t *cfg, const graph_ident_t *file) /* {{{ */
167 {
168 graph_instance_t *inst;
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);
177 if (cfg->instances == NULL)
178 cfg->instances = inst;
179 else
180 inst_append (cfg->instances, inst);
181 }
183 return (inst_add_file (inst, file));
184 } /* }}} int graph_add_file */
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);
192 if (cfg->title == NULL)
193 cfg->title = ident_to_string (cfg->select);
195 if (cfg->title == NULL)
196 return (ENOMEM);
198 strncpy (buffer, cfg->title, buffer_size);
199 buffer[buffer_size - 1] = 0;
201 return (0);
202 } /* }}} int graph_get_title */
204 graph_ident_t *graph_get_selector (graph_config_t *cfg) /* {{{ */
205 {
206 if (cfg == NULL)
207 return (NULL);
209 return (ident_clone (cfg->select));
210 } /* }}} graph_ident_t *graph_get_selector */
212 graph_instance_t *graph_get_instances (graph_config_t *cfg) /* {{{ */
213 {
214 if (cfg == NULL)
215 return (NULL);
217 return (cfg->instances);
218 } /* }}} graph_instance_t *graph_get_instances */
220 graph_def_t *graph_get_defs (graph_config_t *cfg) /* {{{ */
221 {
222 if (cfg == NULL)
223 return (NULL);
225 return (cfg->defs);
226 } /* }}} graph_def_t *graph_get_defs */
228 int graph_add_def (graph_config_t *cfg, graph_def_t *def) /* {{{ */
229 {
230 if ((cfg == NULL) || (def == NULL))
231 return (EINVAL);
233 if (cfg->defs == NULL)
234 {
235 cfg->defs = def;
236 return (0);
237 }
239 return (def_append (cfg->defs, def));
240 } /* }}} int graph_add_def */
242 _Bool graph_matches (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
243 {
244 if ((cfg == NULL) || (ident == NULL))
245 return (0);
247 return (ident_matches (cfg->select, ident));
248 } /* }}} _Bool graph_matches */
250 int graph_compare (graph_config_t *cfg, const graph_ident_t *ident) /* {{{ */
251 {
252 if ((cfg == NULL) || (ident == NULL))
253 return (0);
255 return (ident_compare (cfg->select, ident));
256 } /* }}} int graph_compare */
258 int graph_clear_instances (graph_config_t *cfg) /* {{{ */
259 {
260 if (cfg == NULL)
261 return (EINVAL);
263 inst_destroy (cfg->instances);
264 cfg->instances = NULL;
266 return (0);
267 } /* }}} int graph_clear_instances */
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);
275 if (cfg->title != NULL)
276 {
277 array_append (args, "-t");
278 array_append (args, cfg->title);
279 }
281 if (cfg->vertical_label != NULL)
282 {
283 array_append (args, "-v");
284 array_append (args, cfg->vertical_label);
285 }
287 return (0);
288 } /* }}} int graph_get_rrdargs */
290 /* vim: set sw=2 sts=2 et fdm=marker : */