1 /**
2 * collectd - src/utils_db_query.c
3 * Copyright (C) 2008,2009 Florian octo Forster
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 **/
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.h"
31 #include "utils_db_query.h"
33 /*
34 * Data types
35 */
36 struct udb_result_s; /* {{{ */
37 typedef struct udb_result_s udb_result_t;
38 struct udb_result_s
39 {
40 char *type;
41 char *instance_prefix;
42 char **instances;
43 size_t instances_num;
44 char **values;
45 size_t values_num;
46 char **metadata;
47 size_t metadata_num;
49 udb_result_t *next;
50 }; /* }}} */
52 struct udb_query_s /* {{{ */
53 {
54 char *name;
55 char *statement;
56 void *user_data;
58 unsigned int min_version;
59 unsigned int max_version;
61 udb_result_t *results;
62 }; /* }}} */
64 struct udb_result_preparation_area_s /* {{{ */
65 {
66 const data_set_t *ds;
67 size_t *instances_pos;
68 size_t *values_pos;
69 size_t *metadata_pos;
70 char **instances_buffer;
71 char **values_buffer;
72 char **metadata_buffer;
74 struct udb_result_preparation_area_s *next;
75 }; /* }}} */
76 typedef struct udb_result_preparation_area_s udb_result_preparation_area_t;
78 struct udb_query_preparation_area_s /* {{{ */
79 {
80 size_t column_num;
81 char *host;
82 char *plugin;
83 char *db_name;
85 cdtime_t interval;
87 udb_result_preparation_area_t *result_prep_areas;
88 }; /* }}} */
90 /*
91 * Config Private functions
92 */
93 static int udb_config_set_string (char **ret_string, /* {{{ */
94 oconfig_item_t *ci)
95 {
96 char *string;
98 if ((ci->values_num != 1)
99 || (ci->values[0].type != OCONFIG_TYPE_STRING))
100 {
101 WARNING ("db query utils: The `%s' config option "
102 "needs exactly one string argument.", ci->key);
103 return (-1);
104 }
106 string = strdup (ci->values[0].value.string);
107 if (string == NULL)
108 {
109 ERROR ("db query utils: strdup failed.");
110 return (-1);
111 }
113 if (*ret_string != NULL)
114 free (*ret_string);
115 *ret_string = string;
117 return (0);
118 } /* }}} int udb_config_set_string */
120 static int udb_config_add_string (char ***ret_array, /* {{{ */
121 size_t *ret_array_len, oconfig_item_t *ci)
122 {
123 char **array;
124 size_t array_len;
125 int i;
127 if (ci->values_num < 1)
128 {
129 WARNING ("db query utils: The `%s' config option "
130 "needs at least one argument.", ci->key);
131 return (-1);
132 }
134 for (i = 0; i < ci->values_num; i++)
135 {
136 if (ci->values[i].type != OCONFIG_TYPE_STRING)
137 {
138 WARNING ("db query utils: Argument %i to the `%s' option "
139 "is not a string.", i + 1, ci->key);
140 return (-1);
141 }
142 }
144 array_len = *ret_array_len;
145 array = (char **) realloc (*ret_array,
146 sizeof (char *) * (array_len + ci->values_num));
147 if (array == NULL)
148 {
149 ERROR ("db query utils: realloc failed.");
150 return (-1);
151 }
152 *ret_array = array;
154 for (i = 0; i < ci->values_num; i++)
155 {
156 array[array_len] = strdup (ci->values[i].value.string);
157 if (array[array_len] == NULL)
158 {
159 ERROR ("db query utils: strdup failed.");
160 *ret_array_len = array_len;
161 return (-1);
162 }
163 array_len++;
164 }
166 *ret_array_len = array_len;
167 return (0);
168 } /* }}} int udb_config_add_string */
170 static int udb_config_set_uint (unsigned int *ret_value, /* {{{ */
171 oconfig_item_t *ci)
172 {
173 double tmp;
175 if ((ci->values_num != 1)
176 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
177 {
178 WARNING ("db query utils: The `%s' config option "
179 "needs exactly one numeric argument.", ci->key);
180 return (-1);
181 }
183 tmp = ci->values[0].value.number;
184 if ((tmp < 0.0) || (tmp > ((double) UINT_MAX)))
185 return (-ERANGE);
187 *ret_value = (unsigned int) (tmp + .5);
188 return (0);
189 } /* }}} int udb_config_set_uint */
191 /*
192 * Result private functions
193 */
194 static int udb_result_submit (udb_result_t *r, /* {{{ */
195 udb_result_preparation_area_t *r_area,
196 udb_query_t const *q, udb_query_preparation_area_t *q_area)
197 {
198 value_list_t vl = VALUE_LIST_INIT;
199 size_t i;
200 int status;
202 assert (r != NULL);
203 assert (r_area->ds != NULL);
204 assert (((size_t) r_area->ds->ds_num) == r->values_num);
205 assert (r->values_num > 0);
207 vl.values = calloc (r->values_num, sizeof (*vl.values));
208 if (vl.values == NULL)
209 {
210 ERROR ("db query utils: calloc failed.");
211 return (-1);
212 }
213 vl.values_len = r_area->ds->ds_num;
215 for (i = 0; i < r->values_num; i++)
216 {
217 char *value_str = r_area->values_buffer[i];
219 if (0 != parse_value (value_str, &vl.values[i], r_area->ds->ds[i].type))
220 {
221 ERROR ("db query utils: udb_result_submit: Parsing `%s' as %s failed.",
222 value_str, DS_TYPE_TO_STRING (r_area->ds->ds[i].type));
223 errno = EINVAL;
224 free (vl.values);
225 return (-1);
226 }
227 }
229 if (q_area->interval > 0)
230 vl.interval = q_area->interval;
232 sstrncpy (vl.host, q_area->host, sizeof (vl.host));
233 sstrncpy (vl.plugin, q_area->plugin, sizeof (vl.plugin));
234 sstrncpy (vl.plugin_instance, q_area->db_name, sizeof (vl.plugin_instance));
235 sstrncpy (vl.type, r->type, sizeof (vl.type));
237 /* Set vl.type_instance {{{ */
238 if (r->instances_num == 0)
239 {
240 if (r->instance_prefix == NULL)
241 vl.type_instance[0] = 0;
242 else
243 sstrncpy (vl.type_instance, r->instance_prefix,
244 sizeof (vl.type_instance));
245 }
246 else /* if ((r->instances_num > 0) */
247 {
248 if (r->instance_prefix == NULL)
249 {
250 strjoin (vl.type_instance, sizeof (vl.type_instance),
251 r_area->instances_buffer, r->instances_num, "-");
252 }
253 else
254 {
255 char tmp[DATA_MAX_NAME_LEN];
257 strjoin (tmp, sizeof (tmp), r_area->instances_buffer,
258 r->instances_num, "-");
259 tmp[sizeof (tmp) - 1] = 0;
261 snprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
262 r->instance_prefix, tmp);
263 }
264 }
265 vl.type_instance[sizeof (vl.type_instance) - 1] = 0;
266 /* }}} */
268 /* Annotate meta data. {{{ */
269 if (r->metadata_num > 0)
270 {
271 vl.meta = meta_data_create ();
272 if (vl.meta == NULL)
273 {
274 ERROR ("db query utils:: meta_data_create failed.");
275 return (-ENOMEM);
276 }
278 for (i = 0; i < r->metadata_num; i++)
279 {
280 status = meta_data_add_string (vl.meta, r->metadata[i],
281 r_area->metadata_buffer[i]);
282 if (status != 0)
283 {
284 ERROR ("db query utils:: meta_data_add_string failed.");
285 meta_data_destroy (vl.meta);
286 vl.meta = NULL;
287 return (status);
288 }
289 }
290 }
291 /* }}} */
293 plugin_dispatch_values (&vl);
295 if (r->metadata_num > 0)
296 {
297 meta_data_destroy (vl.meta);
298 vl.meta = NULL;
299 }
300 sfree (vl.values);
301 return (0);
302 } /* }}} void udb_result_submit */
304 static void udb_result_finish_result (udb_result_t const *r, /* {{{ */
305 udb_result_preparation_area_t *prep_area)
306 {
307 if ((r == NULL) || (prep_area == NULL))
308 return;
310 prep_area->ds = NULL;
311 sfree (prep_area->instances_pos);
312 sfree (prep_area->values_pos);
313 sfree (prep_area->metadata_pos);
314 sfree (prep_area->instances_buffer);
315 sfree (prep_area->values_buffer);
316 sfree (prep_area->metadata_buffer);
317 } /* }}} void udb_result_finish_result */
319 static int udb_result_handle_result (udb_result_t *r, /* {{{ */
320 udb_query_preparation_area_t *q_area,
321 udb_result_preparation_area_t *r_area,
322 udb_query_t const *q, char **column_values)
323 {
324 size_t i;
326 assert (r && q_area && r_area);
328 for (i = 0; i < r->instances_num; i++)
329 r_area->instances_buffer[i] = column_values[r_area->instances_pos[i]];
331 for (i = 0; i < r->values_num; i++)
332 r_area->values_buffer[i] = column_values[r_area->values_pos[i]];
334 for (i = 0; i < r->metadata_num; i++)
335 r_area->metadata_buffer[i] = column_values[r_area->metadata_pos[i]];
337 return udb_result_submit (r, r_area, q, q_area);
338 } /* }}} int udb_result_handle_result */
340 static int udb_result_prepare_result (udb_result_t const *r, /* {{{ */
341 udb_result_preparation_area_t *prep_area,
342 char **column_names, size_t column_num)
343 {
344 size_t i;
346 if ((r == NULL) || (prep_area == NULL))
347 return (-EINVAL);
349 #define BAIL_OUT(status) \
350 prep_area->ds = NULL; \
351 sfree (prep_area->instances_pos); \
352 sfree (prep_area->values_pos); \
353 sfree (prep_area->metadata_pos); \
354 sfree (prep_area->instances_buffer); \
355 sfree (prep_area->values_buffer); \
356 sfree (prep_area->metadata_buffer); \
357 return (status)
359 /* Make sure previous preparations are cleaned up. */
360 udb_result_finish_result (r, prep_area);
361 prep_area->instances_pos = NULL;
362 prep_area->values_pos = NULL;
363 prep_area->metadata_pos = NULL;
365 /* Read `ds' and check number of values {{{ */
366 prep_area->ds = plugin_get_ds (r->type);
367 if (prep_area->ds == NULL)
368 {
369 ERROR ("db query utils: udb_result_prepare_result: Type `%s' is not "
370 "known by the daemon. See types.db(5) for details.",
371 r->type);
372 BAIL_OUT (-1);
373 }
375 if (prep_area->ds->ds_num != r->values_num)
376 {
377 ERROR ("db query utils: udb_result_prepare_result: The type `%s' "
378 "requires exactly %zu value%s, but the configuration specifies %zu.",
379 r->type,
380 prep_area->ds->ds_num, (prep_area->ds->ds_num == 1) ? "" : "s",
381 r->values_num);
382 BAIL_OUT (-1);
383 }
384 /* }}} */
386 /* Allocate r->instances_pos, r->values_pos, r->metadata_post,
387 * r->instances_buffer, r->values_buffer, and r->metadata_buffer {{{ */
388 if (r->instances_num > 0)
389 {
390 prep_area->instances_pos
391 = (size_t *) calloc (r->instances_num, sizeof (size_t));
392 if (prep_area->instances_pos == NULL)
393 {
394 ERROR ("db query utils: udb_result_prepare_result: calloc failed.");
395 BAIL_OUT (-ENOMEM);
396 }
398 prep_area->instances_buffer
399 = (char **) calloc (r->instances_num, sizeof (char *));
400 if (prep_area->instances_buffer == NULL)
401 {
402 ERROR ("db query utils: udb_result_prepare_result: calloc failed.");
403 BAIL_OUT (-ENOMEM);
404 }
405 } /* if (r->instances_num > 0) */
407 prep_area->values_pos
408 = (size_t *) calloc (r->values_num, sizeof (size_t));
409 if (prep_area->values_pos == NULL)
410 {
411 ERROR ("db query utils: udb_result_prepare_result: calloc failed.");
412 BAIL_OUT (-ENOMEM);
413 }
415 prep_area->values_buffer
416 = (char **) calloc (r->values_num, sizeof (char *));
417 if (prep_area->values_buffer == NULL)
418 {
419 ERROR ("db query utils: udb_result_prepare_result: calloc failed.");
420 BAIL_OUT (-ENOMEM);
421 }
423 prep_area->metadata_pos
424 = (size_t *) calloc (r->metadata_num, sizeof (size_t));
425 if (prep_area->metadata_pos == NULL)
426 {
427 ERROR ("db query utils: udb_result_prepare_result: calloc failed.");
428 BAIL_OUT (-ENOMEM);
429 }
431 prep_area->metadata_buffer
432 = (char **) calloc (r->metadata_num, sizeof (char *));
433 if (prep_area->metadata_buffer == NULL)
434 {
435 ERROR ("db query utils: udb_result_prepare_result: calloc failed.");
436 BAIL_OUT (-ENOMEM);
437 }
439 /* }}} */
441 /* Determine the position of the instance columns {{{ */
442 for (i = 0; i < r->instances_num; i++)
443 {
444 size_t j;
446 for (j = 0; j < column_num; j++)
447 {
448 if (strcasecmp (r->instances[i], column_names[j]) == 0)
449 {
450 prep_area->instances_pos[i] = j;
451 break;
452 }
453 }
455 if (j >= column_num)
456 {
457 ERROR ("db query utils: udb_result_prepare_result: "
458 "Column `%s' could not be found.",
459 r->instances[i]);
460 BAIL_OUT (-ENOENT);
461 }
462 } /* }}} for (i = 0; i < r->instances_num; i++) */
464 /* Determine the position of the value columns {{{ */
465 for (i = 0; i < r->values_num; i++)
466 {
467 size_t j;
469 for (j = 0; j < column_num; j++)
470 {
471 if (strcasecmp (r->values[i], column_names[j]) == 0)
472 {
473 prep_area->values_pos[i] = j;
474 break;
475 }
476 }
478 if (j >= column_num)
479 {
480 ERROR ("db query utils: udb_result_prepare_result: "
481 "Column `%s' could not be found.",
482 r->values[i]);
483 BAIL_OUT (-ENOENT);
484 }
485 } /* }}} for (i = 0; i < r->values_num; i++) */
487 /* Determine the position of the metadata columns {{{ */
488 for (i = 0; i < r->metadata_num; i++)
489 {
490 size_t j;
492 for (j = 0; j < column_num; j++)
493 {
494 if (strcasecmp (r->metadata[i], column_names[j]) == 0)
495 {
496 prep_area->metadata_pos[i] = j;
497 break;
498 }
499 }
501 if (j >= column_num)
502 {
503 ERROR ("db query utils: udb_result_prepare_result: "
504 "Metadata column `%s' could not be found.",
505 r->values[i]);
506 BAIL_OUT (-ENOENT);
507 }
508 } /* }}} for (i = 0; i < r->metadata_num; i++) */
510 #undef BAIL_OUT
511 return (0);
512 } /* }}} int udb_result_prepare_result */
514 static void udb_result_free (udb_result_t *r) /* {{{ */
515 {
516 size_t i;
518 if (r == NULL)
519 return;
521 sfree (r->type);
523 for (i = 0; i < r->instances_num; i++)
524 sfree (r->instances[i]);
525 sfree (r->instances);
527 for (i = 0; i < r->values_num; i++)
528 sfree (r->values[i]);
529 sfree (r->values);
531 for (i = 0; i < r->metadata_num; i++)
532 sfree (r->metadata[i]);
533 sfree (r->metadata);
535 udb_result_free (r->next);
537 sfree (r);
538 } /* }}} void udb_result_free */
540 static int udb_result_create (const char *query_name, /* {{{ */
541 udb_result_t **r_head, oconfig_item_t *ci)
542 {
543 udb_result_t *r;
544 int status;
545 int i;
547 if (ci->values_num != 0)
548 {
549 WARNING ("db query utils: The `Result' block doesn't accept "
550 "any arguments. Ignoring %i argument%s.",
551 ci->values_num, (ci->values_num == 1) ? "" : "s");
552 }
554 r = calloc (1, sizeof (*r));
555 if (r == NULL)
556 {
557 ERROR ("db query utils: calloc failed.");
558 return (-1);
559 }
560 r->type = NULL;
561 r->instance_prefix = NULL;
562 r->instances = NULL;
563 r->values = NULL;
564 r->metadata = NULL;
565 r->next = NULL;
567 /* Fill the `udb_result_t' structure.. */
568 status = 0;
569 for (i = 0; i < ci->children_num; i++)
570 {
571 oconfig_item_t *child = ci->children + i;
573 if (strcasecmp ("Type", child->key) == 0)
574 status = udb_config_set_string (&r->type, child);
575 else if (strcasecmp ("InstancePrefix", child->key) == 0)
576 status = udb_config_set_string (&r->instance_prefix, child);
577 else if (strcasecmp ("InstancesFrom", child->key) == 0)
578 status = udb_config_add_string (&r->instances, &r->instances_num, child);
579 else if (strcasecmp ("ValuesFrom", child->key) == 0)
580 status = udb_config_add_string (&r->values, &r->values_num, child);
581 else if (strcasecmp ("MetadataFrom", child->key) == 0)
582 status = udb_config_add_string (&r->metadata, &r->metadata_num, child);
583 else
584 {
585 WARNING ("db query utils: Query `%s': Option `%s' not allowed here.",
586 query_name, child->key);
587 status = -1;
588 }
590 if (status != 0)
591 break;
592 }
594 /* Check that all necessary options have been given. */
595 while (status == 0)
596 {
597 if (r->type == NULL)
598 {
599 WARNING ("db query utils: `Type' not given for "
600 "result in query `%s'", query_name);
601 status = -1;
602 }
603 if (r->values == NULL)
604 {
605 WARNING ("db query utils: `ValuesFrom' not given for "
606 "result in query `%s'", query_name);
607 status = -1;
608 }
610 break;
611 } /* while (status == 0) */
613 if (status != 0)
614 {
615 udb_result_free (r);
616 return (-1);
617 }
619 /* If all went well, add this result to the list of results. */
620 if (*r_head == NULL)
621 {
622 *r_head = r;
623 }
624 else
625 {
626 udb_result_t *last;
628 last = *r_head;
629 while (last->next != NULL)
630 last = last->next;
632 last->next = r;
633 }
635 return (0);
636 } /* }}} int udb_result_create */
638 /*
639 * Query private functions
640 */
641 static void udb_query_free_one (udb_query_t *q) /* {{{ */
642 {
643 if (q == NULL)
644 return;
646 sfree (q->name);
647 sfree (q->statement);
649 udb_result_free (q->results);
651 sfree (q);
652 } /* }}} void udb_query_free_one */
654 /*
655 * Query public functions
656 */
657 int udb_query_create (udb_query_t ***ret_query_list, /* {{{ */
658 size_t *ret_query_list_len, oconfig_item_t *ci,
659 udb_query_create_callback_t cb)
660 {
661 udb_query_t **query_list;
662 size_t query_list_len;
664 udb_query_t *q;
665 int status;
666 int i;
668 if ((ret_query_list == NULL) || (ret_query_list_len == NULL))
669 return (-EINVAL);
670 query_list = *ret_query_list;
671 query_list_len = *ret_query_list_len;
673 if ((ci->values_num != 1)
674 || (ci->values[0].type != OCONFIG_TYPE_STRING))
675 {
676 WARNING ("db query utils: The `Query' block "
677 "needs exactly one string argument.");
678 return (-1);
679 }
681 q = calloc (1, sizeof (*q));
682 if (q == NULL)
683 {
684 ERROR ("db query utils: calloc failed.");
685 return (-1);
686 }
687 q->min_version = 0;
688 q->max_version = UINT_MAX;
690 status = udb_config_set_string (&q->name, ci);
691 if (status != 0)
692 {
693 sfree (q);
694 return (status);
695 }
697 /* Fill the `udb_query_t' structure.. */
698 for (i = 0; i < ci->children_num; i++)
699 {
700 oconfig_item_t *child = ci->children + i;
702 if (strcasecmp ("Statement", child->key) == 0)
703 status = udb_config_set_string (&q->statement, child);
704 else if (strcasecmp ("Result", child->key) == 0)
705 status = udb_result_create (q->name, &q->results, child);
706 else if (strcasecmp ("MinVersion", child->key) == 0)
707 status = udb_config_set_uint (&q->min_version, child);
708 else if (strcasecmp ("MaxVersion", child->key) == 0)
709 status = udb_config_set_uint (&q->max_version, child);
711 /* Call custom callbacks */
712 else if (cb != NULL)
713 {
714 status = (*cb) (q, child);
715 if (status != 0)
716 {
717 WARNING ("db query utils: The configuration callback failed "
718 "to handle `%s'.", child->key);
719 }
720 }
721 else
722 {
723 WARNING ("db query utils: Query `%s': Option `%s' not allowed here.",
724 q->name, child->key);
725 status = -1;
726 }
728 if (status != 0)
729 break;
730 }
732 /* Check that all necessary options have been given. */
733 if (status == 0)
734 {
735 if (q->statement == NULL)
736 {
737 WARNING ("db query utils: Query `%s': No `Statement' given.", q->name);
738 status = -1;
739 }
740 if (q->results == NULL)
741 {
742 WARNING ("db query utils: Query `%s': No (valid) `Result' block given.",
743 q->name);
744 status = -1;
745 }
746 } /* if (status == 0) */
748 /* If all went well, add this query to the list of queries within the
749 * database structure. */
750 if (status == 0)
751 {
752 udb_query_t **temp;
754 temp = (udb_query_t **) realloc (query_list,
755 sizeof (*query_list) * (query_list_len + 1));
756 if (temp == NULL)
757 {
758 ERROR ("db query utils: realloc failed");
759 status = -1;
760 }
761 else
762 {
763 query_list = temp;
764 query_list[query_list_len] = q;
765 query_list_len++;
766 }
767 }
769 if (status != 0)
770 {
771 udb_query_free_one (q);
772 return (-1);
773 }
775 *ret_query_list = query_list;
776 *ret_query_list_len = query_list_len;
778 return (0);
779 } /* }}} int udb_query_create */
781 void udb_query_free (udb_query_t **query_list, size_t query_list_len) /* {{{ */
782 {
783 size_t i;
785 if (query_list == NULL)
786 return;
788 for (i = 0; i < query_list_len; i++)
789 udb_query_free_one (query_list[i]);
791 sfree (query_list);
792 } /* }}} void udb_query_free */
794 int udb_query_pick_from_list_by_name (const char *name, /* {{{ */
795 udb_query_t **src_list, size_t src_list_len,
796 udb_query_t ***dst_list, size_t *dst_list_len)
797 {
798 size_t i;
799 int num_added;
801 if ((name == NULL) || (src_list == NULL) || (dst_list == NULL)
802 || (dst_list_len == NULL))
803 {
804 ERROR ("db query utils: udb_query_pick_from_list_by_name: "
805 "Invalid argument.");
806 return (-EINVAL);
807 }
809 num_added = 0;
810 for (i = 0; i < src_list_len; i++)
811 {
812 udb_query_t **tmp_list;
813 size_t tmp_list_len;
815 if (strcasecmp (name, src_list[i]->name) != 0)
816 continue;
818 tmp_list_len = *dst_list_len;
819 tmp_list = (udb_query_t **) realloc (*dst_list, (tmp_list_len + 1)
820 * sizeof (udb_query_t *));
821 if (tmp_list == NULL)
822 {
823 ERROR ("db query utils: realloc failed.");
824 return (-ENOMEM);
825 }
827 tmp_list[tmp_list_len] = src_list[i];
828 tmp_list_len++;
830 *dst_list = tmp_list;
831 *dst_list_len = tmp_list_len;
833 num_added++;
834 } /* for (i = 0; i < src_list_len; i++) */
836 if (num_added <= 0)
837 {
838 ERROR ("db query utils: Cannot find query `%s'. Make sure the <Query> "
839 "block is above the database definition!",
840 name);
841 return (-ENOENT);
842 }
843 else
844 {
845 DEBUG ("db query utils: Added %i versions of query `%s'.",
846 num_added, name);
847 }
849 return (0);
850 } /* }}} int udb_query_pick_from_list_by_name */
852 int udb_query_pick_from_list (oconfig_item_t *ci, /* {{{ */
853 udb_query_t **src_list, size_t src_list_len,
854 udb_query_t ***dst_list, size_t *dst_list_len)
855 {
856 const char *name;
858 if ((ci == NULL) || (src_list == NULL) || (dst_list == NULL)
859 || (dst_list_len == NULL))
860 {
861 ERROR ("db query utils: udb_query_pick_from_list: "
862 "Invalid argument.");
863 return (-EINVAL);
864 }
866 if ((ci->values_num != 1)
867 || (ci->values[0].type != OCONFIG_TYPE_STRING))
868 {
869 ERROR ("db query utils: The `%s' config option "
870 "needs exactly one string argument.", ci->key);
871 return (-1);
872 }
873 name = ci->values[0].value.string;
875 return (udb_query_pick_from_list_by_name (name,
876 src_list, src_list_len,
877 dst_list, dst_list_len));
878 } /* }}} int udb_query_pick_from_list */
880 const char *udb_query_get_name (udb_query_t *q) /* {{{ */
881 {
882 if (q == NULL)
883 return (NULL);
885 return (q->name);
886 } /* }}} const char *udb_query_get_name */
888 const char *udb_query_get_statement (udb_query_t *q) /* {{{ */
889 {
890 if (q == NULL)
891 return (NULL);
893 return (q->statement);
894 } /* }}} const char *udb_query_get_statement */
896 void udb_query_set_user_data (udb_query_t *q, void *user_data) /* {{{ */
897 {
898 if (q == NULL)
899 return;
901 q->user_data = user_data;
902 } /* }}} void udb_query_set_user_data */
904 void *udb_query_get_user_data (udb_query_t *q) /* {{{ */
905 {
906 if (q == NULL)
907 return (NULL);
909 return (q->user_data);
910 } /* }}} void *udb_query_get_user_data */
912 int udb_query_check_version (udb_query_t *q, unsigned int version) /* {{{ */
913 {
914 if (q == NULL)
915 return (-EINVAL);
917 if ((version < q->min_version) || (version > q->max_version))
918 return (0);
920 return (1);
921 } /* }}} int udb_query_check_version */
923 void udb_query_finish_result (udb_query_t const *q, /* {{{ */
924 udb_query_preparation_area_t *prep_area)
925 {
926 udb_result_preparation_area_t *r_area;
927 udb_result_t *r;
929 if ((q == NULL) || (prep_area == NULL))
930 return;
932 prep_area->column_num = 0;
933 sfree (prep_area->host);
934 sfree (prep_area->plugin);
935 sfree (prep_area->db_name);
937 prep_area->interval = 0;
939 for (r = q->results, r_area = prep_area->result_prep_areas;
940 r != NULL; r = r->next, r_area = r_area->next)
941 {
942 /* this may happen during error conditions of the caller */
943 if (r_area == NULL)
944 break;
945 udb_result_finish_result (r, r_area);
946 }
947 } /* }}} void udb_query_finish_result */
949 int udb_query_handle_result (udb_query_t const *q, /* {{{ */
950 udb_query_preparation_area_t *prep_area, char **column_values)
951 {
952 udb_result_preparation_area_t *r_area;
953 udb_result_t *r;
954 int success;
955 int status;
957 if ((q == NULL) || (prep_area == NULL))
958 return (-EINVAL);
960 if ((prep_area->column_num < 1) || (prep_area->host == NULL)
961 || (prep_area->plugin == NULL) || (prep_area->db_name == NULL))
962 {
963 ERROR ("db query utils: Query `%s': Query is not prepared; "
964 "can't handle result.", q->name);
965 return (-EINVAL);
966 }
968 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG /* {{{ */
969 do
970 {
971 size_t i;
973 for (i = 0; i < prep_area->column_num; i++)
974 {
975 DEBUG ("db query utils: udb_query_handle_result (%s, %s): "
976 "column[%zu] = %s;",
977 prep_area->db_name, q->name, i, column_values[i]);
978 }
979 } while (0);
980 #endif /* }}} */
982 success = 0;
983 for (r = q->results, r_area = prep_area->result_prep_areas;
984 r != NULL; r = r->next, r_area = r_area->next)
985 {
986 status = udb_result_handle_result (r, prep_area, r_area,
987 q, column_values);
988 if (status == 0)
989 success++;
990 }
992 if (success == 0)
993 {
994 ERROR ("db query utils: udb_query_handle_result (%s, %s): "
995 "All results failed.", prep_area->db_name, q->name);
996 return (-1);
997 }
999 return (0);
1000 } /* }}} int udb_query_handle_result */
1002 int udb_query_prepare_result (udb_query_t const *q, /* {{{ */
1003 udb_query_preparation_area_t *prep_area,
1004 const char *host, const char *plugin, const char *db_name,
1005 char **column_names, size_t column_num, cdtime_t interval)
1006 {
1007 udb_result_preparation_area_t *r_area;
1008 udb_result_t *r;
1009 int status;
1011 if ((q == NULL) || (prep_area == NULL))
1012 return (-EINVAL);
1014 udb_query_finish_result (q, prep_area);
1016 prep_area->column_num = column_num;
1017 prep_area->host = strdup (host);
1018 prep_area->plugin = strdup (plugin);
1019 prep_area->db_name = strdup (db_name);
1021 prep_area->interval = interval;
1023 if ((prep_area->host == NULL) || (prep_area->plugin == NULL)
1024 || (prep_area->db_name == NULL))
1025 {
1026 ERROR ("db query utils: Query `%s': Prepare failed: Out of memory.", q->name);
1027 udb_query_finish_result (q, prep_area);
1028 return (-ENOMEM);
1029 }
1031 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG
1032 do
1033 {
1034 size_t i;
1036 for (i = 0; i < column_num; i++)
1037 {
1038 DEBUG ("db query utils: udb_query_prepare_result: "
1039 "query = %s; column[%zu] = %s;",
1040 q->name, i, column_names[i]);
1041 }
1042 } while (0);
1043 #endif
1045 for (r = q->results, r_area = prep_area->result_prep_areas;
1046 r != NULL; r = r->next, r_area = r_area->next)
1047 {
1048 if (! r_area)
1049 {
1050 ERROR ("db query utils: Query `%s': Invalid number of result "
1051 "preparation areas.", q->name);
1052 udb_query_finish_result (q, prep_area);
1053 return (-EINVAL);
1054 }
1056 status = udb_result_prepare_result (r, r_area, column_names, column_num);
1057 if (status != 0)
1058 {
1059 udb_query_finish_result (q, prep_area);
1060 return (status);
1061 }
1062 }
1064 return (0);
1065 } /* }}} int udb_query_prepare_result */
1067 udb_query_preparation_area_t *
1068 udb_query_allocate_preparation_area (udb_query_t *q) /* {{{ */
1069 {
1070 udb_query_preparation_area_t *q_area;
1071 udb_result_preparation_area_t **next_r_area;
1072 udb_result_t *r;
1074 q_area = calloc (1, sizeof (*q_area));
1075 if (q_area == NULL)
1076 return NULL;
1078 next_r_area = &q_area->result_prep_areas;
1079 for (r = q->results; r != NULL; r = r->next)
1080 {
1081 udb_result_preparation_area_t *r_area;
1083 r_area = calloc (1, sizeof (*r_area));
1084 if (r_area == NULL)
1085 {
1086 udb_result_preparation_area_t *a = q_area->result_prep_areas;
1088 while (a != NULL)
1089 {
1090 udb_result_preparation_area_t *next = a->next;
1091 sfree (a);
1092 a = next;
1093 }
1095 free (q_area);
1096 return NULL;
1097 }
1099 *next_r_area = r_area;
1100 next_r_area = &r_area->next;
1101 }
1103 return (q_area);
1104 } /* }}} udb_query_preparation_area_t *udb_query_allocate_preparation_area */
1106 void
1107 udb_query_delete_preparation_area (udb_query_preparation_area_t *q_area) /* {{{ */
1108 {
1109 udb_result_preparation_area_t *r_area;
1111 if (q_area == NULL)
1112 return;
1114 r_area = q_area->result_prep_areas;
1115 while (r_area != NULL)
1116 {
1117 udb_result_preparation_area_t *area = r_area;
1119 r_area = r_area->next;
1121 sfree (area->instances_pos);
1122 sfree (area->values_pos);
1123 sfree (area->instances_buffer);
1124 sfree (area->values_buffer);
1125 free (area);
1126 }
1128 sfree (q_area->host);
1129 sfree (q_area->plugin);
1130 sfree (q_area->db_name);
1132 free (q_area);
1133 } /* }}} void udb_query_delete_preparation_area */
1135 /* vim: set sw=2 sts=2 et fdm=marker : */