1 #include <stdlib.h>
2 #include <string.h>
3 #include <errno.h>
4 #include <time.h>
5 #include <assert.h>
7 #include "graph_instance.h"
8 #include "graph.h"
9 #include "graph_def.h"
10 #include "graph_ident.h"
11 #include "graph_list.h"
12 #include "common.h"
13 #include "utils_cgi.h"
15 #include <fcgiapp.h>
16 #include <fcgi_stdio.h>
18 struct graph_instance_s /* {{{ */
19 {
20 graph_ident_t *select;
22 graph_ident_t **files;
23 size_t files_num;
24 }; /* }}} struct graph_instance_s */
26 struct def_callback_data_s
27 {
28 graph_instance_t *inst;
29 str_array_t *args;
30 };
31 typedef struct def_callback_data_s def_callback_data_t;
33 /*
34 * Private functions
35 */
36 /* Create one DEF for each data source in the file. Called by
37 * "inst_get_default_defs" for each file. */
38 static graph_def_t *ident_get_default_defs (graph_config_t *cfg, /* {{{ */
39 graph_ident_t *ident, graph_def_t *def_head)
40 {
41 graph_def_t *defs = NULL;
42 char *file;
43 char **dses = NULL;
44 size_t dses_num = 0;
45 int status;
46 size_t i;
48 if ((cfg == NULL) || (ident == NULL))
49 return (def_head);
51 file = ident_to_file (ident);
52 if (file == NULL)
53 {
54 fprintf (stderr, "ident_get_default_defs: ident_to_file failed\n");
55 return (def_head);
56 }
58 status = ds_list_from_rrd_file (file, &dses_num, &dses);
59 if (status != 0)
60 {
61 free (file);
62 return (def_head);
63 }
65 for (i = 0; i < dses_num; i++)
66 {
67 graph_def_t *def;
69 def = def_search (def_head, ident, dses[i]);
70 if (def != NULL)
71 continue;
73 def = def_create (cfg, ident, dses[i]);
74 if (def == NULL)
75 continue;
77 if (defs == NULL)
78 defs = def;
79 else
80 def_append (defs, def);
82 free (dses[i]);
83 }
85 free (dses);
86 free (file);
88 return (defs);
89 } /* }}} int ident_get_default_defs */
91 /* Create one or more DEFs for each file in the graph instance. The number
92 * depends on the number of data sources in each of the files. Called from
93 * "inst_get_rrdargs" if no DEFs are available from the configuration.
94 * */
95 static graph_def_t *inst_get_default_defs (graph_config_t *cfg, /* {{{ */
96 graph_instance_t *inst)
97 {
98 graph_def_t *defs = NULL;
99 size_t i;
101 if ((cfg == NULL) || (inst == NULL))
102 return (NULL);
104 for (i = 0; i < inst->files_num; i++)
105 {
106 graph_def_t *def;
108 def = ident_get_default_defs (cfg, inst->files[i], defs);
109 if (def == NULL)
110 continue;
112 if (defs == NULL)
113 defs = def;
114 else
115 def_append (defs, def);
116 }
118 return (defs);
119 } /* }}} graph_def_t *inst_get_default_defs */
121 /* Called with each DEF in turn. Calls "def_get_rrdargs" with every appropriate
122 * file / DEF pair. */
123 static int gl_instance_get_rrdargs_cb (graph_def_t *def, void *user_data) /* {{{ */
124 {
125 def_callback_data_t *data = user_data;
126 graph_instance_t *inst = data->inst;
127 str_array_t *args = data->args;
129 size_t i;
131 for (i = 0; i < inst->files_num; i++)
132 {
133 if (!def_matches (def, inst->files[i]))
134 continue;
136 def_get_rrdargs (def, inst->files[i], args);
137 }
139 return (0);
140 } /* }}} int gl_instance_get_rrdargs_cb */
142 static const char *get_part_from_param (const char *prim_key, /* {{{ */
143 const char *sec_key)
144 {
145 const char *val;
147 val = param (prim_key);
148 if (val != NULL)
149 return (val);
151 return (param (sec_key));
152 } /* }}} const char *get_part_from_param */
154 static graph_ident_t *inst_get_selector_from_params (void) /* {{{ */
155 {
156 const char *host = get_part_from_param ("inst_host", "host");
157 const char *plugin = get_part_from_param ("inst_plugin", "plugin");
158 const char *plugin_instance = get_part_from_param ("inst_plugin_instance",
159 "plugin_instance");
160 const char *type = get_part_from_param ("inst_type", "type");
161 const char *type_instance = get_part_from_param ("inst_type_instance",
162 "type_instance");
164 graph_ident_t *ident;
166 if ((host == NULL)
167 || (plugin == NULL) || (plugin_instance == NULL)
168 || (type == NULL) || (type_instance == NULL))
169 {
170 fprintf (stderr, "inst_get_selected: A parameter is NULL\n");
171 return (NULL);
172 }
174 ident = ident_create (host, plugin, plugin_instance, type, type_instance);
175 if (ident == NULL)
176 {
177 fprintf (stderr, "inst_get_selected: ident_create failed\n");
178 return (NULL);
179 }
181 return (ident);
182 } /* }}} graph_ident_t *inst_get_selector_from_params */
184 /*
185 * Public functions
186 */
187 graph_instance_t *inst_create (graph_config_t *cfg, /* {{{ */
188 const graph_ident_t *ident)
189 {
190 graph_instance_t *i;
191 graph_ident_t *selector;
193 if ((cfg == NULL) || (ident == NULL))
194 return (NULL);
196 i = malloc (sizeof (*i));
197 if (i == NULL)
198 return (NULL);
199 memset (i, 0, sizeof (*i));
201 selector = graph_get_selector (cfg);
202 if (selector == NULL)
203 {
204 fprintf (stderr, "inst_create: graph_get_selector failed\n");
205 free (i);
206 return (NULL);
207 }
209 i->select = ident_copy_with_selector (selector, ident,
210 IDENT_FLAG_REPLACE_ANY);
211 if (i->select == NULL)
212 {
213 fprintf (stderr, "inst_create: ident_copy_with_selector failed\n");
214 ident_destroy (selector);
215 free (i);
216 return (NULL);
217 }
219 ident_destroy (selector);
221 i->files = NULL;
222 i->files_num = 0;
224 return (i);
225 } /* }}} graph_instance_t *inst_create */
227 void inst_destroy (graph_instance_t *inst) /* {{{ */
228 {
229 size_t i;
231 if (inst == NULL)
232 return;
234 ident_destroy (inst->select);
236 for (i = 0; i < inst->files_num; i++)
237 ident_destroy (inst->files[i]);
238 free (inst->files);
240 free (inst);
241 } /* }}} void inst_destroy */
243 int inst_add_file (graph_instance_t *inst, /* {{{ */
244 const graph_ident_t *file)
245 {
246 graph_ident_t **tmp;
248 tmp = realloc (inst->files, sizeof (*inst->files) * (inst->files_num + 1));
249 if (tmp == NULL)
250 return (ENOMEM);
251 inst->files = tmp;
253 inst->files[inst->files_num] = ident_clone (file);
254 if (inst->files[inst->files_num] == NULL)
255 return (ENOMEM);
257 inst->files_num++;
259 return (0);
260 } /* }}} int inst_add_file */
262 graph_instance_t *inst_get_selected (graph_config_t *cfg) /* {{{ */
263 {
264 graph_ident_t *ident;
265 graph_instance_t *inst;
267 if (cfg == NULL)
268 cfg = gl_graph_get_selected ();
270 if (cfg == NULL)
271 {
272 DEBUG ("inst_get_selected: cfg == NULL;\n");
273 return (NULL);
274 }
276 ident = inst_get_selector_from_params ();
277 if (ident == NULL)
278 {
279 fprintf (stderr, "inst_get_selected: ident_create failed\n");
280 return (NULL);
281 }
283 inst = graph_inst_find_exact (cfg, ident);
285 ident_destroy (ident);
286 return (inst);
287 } /* }}} graph_instance_t *inst_get_selected */
289 int inst_get_rrdargs (graph_config_t *cfg, /* {{{ */
290 graph_instance_t *inst,
291 str_array_t *args)
292 {
293 def_callback_data_t data = { inst, args };
294 graph_def_t *defs;
295 int status;
297 if ((cfg == NULL) || (inst == NULL) || (args == NULL))
298 return (EINVAL);
300 status = graph_get_rrdargs (cfg, inst, args);
301 if (status != 0)
302 return (status);
304 defs = graph_get_defs (cfg);
305 if (defs == NULL)
306 {
307 defs = inst_get_default_defs (cfg, inst);
309 if (defs == NULL)
310 return (-1);
312 status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
314 def_destroy (defs);
315 }
316 else
317 {
318 status = def_foreach (defs, gl_instance_get_rrdargs_cb, &data);
319 }
321 return (status);
322 } /* }}} int inst_get_rrdargs */
324 graph_ident_t *inst_get_selector (graph_instance_t *inst) /* {{{ */
325 {
326 if (inst == NULL)
327 return (NULL);
329 return (ident_clone (inst->select));
330 } /* }}} graph_ident_t *inst_get_selector */
332 int inst_get_params (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
333 char *buffer, size_t buffer_size)
334 {
335 graph_ident_t *cfg_select;
337 if ((cfg == NULL) || (inst == NULL)
338 || (buffer == NULL) || (buffer_size < 1))
339 return (EINVAL);
341 cfg_select = graph_get_selector (cfg);
342 if (cfg_select == NULL)
343 {
344 fprintf (stderr, "inst_get_params: graph_get_selector failed");
345 return (-1);
346 }
348 buffer[0] = 0;
350 #define COPY_FIELD(field) do { \
351 const char *cfg_f = ident_get_##field (cfg_select); \
352 const char *inst_f = ident_get_##field (inst->select); \
353 if (strcmp (cfg_f, inst_f) == 0) \
354 { \
355 strlcat (buffer, #field, buffer_size); \
356 strlcat (buffer, "=", buffer_size); \
357 strlcat (buffer, cfg_f, buffer_size); \
358 } \
359 else \
360 { \
361 strlcat (buffer, "graph_", buffer_size); \
362 strlcat (buffer, #field, buffer_size); \
363 strlcat (buffer, "=", buffer_size); \
364 strlcat (buffer, cfg_f, buffer_size); \
365 strlcat (buffer, ";", buffer_size); \
366 strlcat (buffer, "inst_", buffer_size); \
367 strlcat (buffer, #field, buffer_size); \
368 strlcat (buffer, "=", buffer_size); \
369 strlcat (buffer, inst_f, buffer_size); \
370 } \
371 } while (0)
373 COPY_FIELD(host);
374 strlcat (buffer, ";", buffer_size);
375 COPY_FIELD(plugin);
376 strlcat (buffer, ";", buffer_size);
377 COPY_FIELD(plugin_instance);
378 strlcat (buffer, ";", buffer_size);
379 COPY_FIELD(type);
380 strlcat (buffer, ";", buffer_size);
381 COPY_FIELD(type_instance);
383 #undef COPY_FIELD
385 ident_destroy (cfg_select);
387 return (0);
388 } /* }}} int inst_get_params */
390 int inst_compare_ident (graph_instance_t *inst, /* {{{ */
391 const graph_ident_t *ident)
392 {
393 if ((inst == NULL) || (ident == NULL))
394 return (0);
396 return (ident_compare (inst->select, ident));
397 } /* }}} int inst_compare_ident */
399 _Bool inst_matches_ident (graph_instance_t *inst, /* {{{ */
400 const graph_ident_t *ident)
401 {
402 if ((inst == NULL) || (ident == NULL))
403 return (0);
405 return (ident_matches (inst->select, ident));
406 } /* }}} _Bool inst_matches_ident */
408 _Bool inst_matches_string (graph_config_t *cfg, /* {{{ */
409 graph_instance_t *inst,
410 const char *term)
411 {
412 char buffer[1024];
413 int status;
415 if ((cfg == NULL) || (inst == NULL) || (term == NULL))
416 return (0);
418 status = inst_describe (cfg, inst, buffer, sizeof (buffer));
419 if (status != 0)
420 {
421 fprintf (stderr, "inst_matches_string: inst_describe failed\n");
422 return (status);
423 }
425 strtolower (buffer);
427 /* no match */
428 if (strstr (buffer, term) == NULL)
429 return (0);
431 return (1);
432 } /* }}} _Bool inst_matches_string */
434 _Bool inst_matches_field (graph_instance_t *inst, /* {{{ */
435 graph_ident_field_t field, const char *field_value)
436 {
437 const char *selector_field;
438 size_t i;
440 if ((inst == NULL) || (field_value == NULL))
441 return (0);
443 selector_field = ident_get_field (inst->select, field);
444 if (selector_field == NULL)
445 return (0);
447 assert (!IS_ANY (selector_field));
448 if (!IS_ALL (selector_field))
449 {
450 if (strcasecmp (selector_field, field_value) == 0)
451 return (1);
452 else
453 return (0);
454 }
456 /* The selector field is an ALL selector
457 * => we need to check the files to see if the instance matches. */
458 for (i = 0; i < inst->files_num; i++)
459 {
460 selector_field = ident_get_field (inst->files[i], field);
461 if (selector_field == NULL)
462 continue;
464 assert (!IS_ANY (selector_field));
465 assert (!IS_ALL (selector_field));
467 if (strcasecmp (selector_field, field_value) == 0)
468 return (1);
469 } /* for files */
471 return (0);
472 } /* }}} _Bool inst_matches_field */
474 int inst_describe (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
475 char *buffer, size_t buffer_size)
476 {
477 graph_ident_t *cfg_select;
479 if ((cfg == NULL) || (inst == NULL)
480 || (buffer == NULL) || (buffer_size < 2))
481 return (EINVAL);
483 cfg_select = graph_get_selector (cfg);
484 if (cfg_select == NULL)
485 {
486 fprintf (stderr, "inst_describe: graph_get_selector failed\n");
487 return (-1);
488 }
490 buffer[0] = 0;
492 #define CHECK_FIELD(field) do { \
493 if (IS_ANY (ident_get_##field (cfg_select))) \
494 { \
495 if (buffer[0] != 0) \
496 strlcat (buffer, "/", buffer_size); \
497 strlcat (buffer, ident_get_##field (inst->select), buffer_size); \
498 } \
499 } while (0)
501 CHECK_FIELD (host);
502 CHECK_FIELD (plugin);
503 CHECK_FIELD (plugin_instance);
504 CHECK_FIELD (type);
505 CHECK_FIELD (type_instance);
507 #undef CHECK_FIELD
509 if (buffer[0] == 0)
510 strlcat (buffer, "default", buffer_size);
512 ident_destroy (cfg_select);
514 return (0);
515 } /* }}} int inst_describe */
517 time_t inst_get_mtime (graph_instance_t *inst) /* {{{ */
518 {
519 size_t i;
520 time_t mtime;
522 if (inst == NULL)
523 return (0);
525 mtime = 0;
526 for (i = 0; i < inst->files_num; i++)
527 {
528 time_t tmp;
530 tmp = ident_get_mtime (inst->files[i]);
531 if (mtime < tmp)
532 mtime = tmp;
533 }
535 return (mtime);
536 } /* }}} time_t inst_get_mtime */
538 /* vim: set sw=2 sts=2 et fdm=marker : */