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 = (value_t *) calloc (r->values_num, sizeof (value_t));
208 if (vl.values == NULL)
209 {
210 ERROR ("db query utils: malloc 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 return (-1);
225 }
226 }
228 if (q_area->interval > 0)
229 vl.interval = q_area->interval;
231 sstrncpy (vl.host, q_area->host, sizeof (vl.host));
232 sstrncpy (vl.plugin, q_area->plugin, sizeof (vl.plugin));
233 sstrncpy (vl.plugin_instance, q_area->db_name, sizeof (vl.plugin_instance));
234 sstrncpy (vl.type, r->type, sizeof (vl.type));
236 /* Set vl.type_instance {{{ */
237 if (r->instances_num <= 0)
238 {
239 if (r->instance_prefix == NULL)
240 vl.type_instance[0] = 0;
241 else
242 sstrncpy (vl.type_instance, r->instance_prefix,
243 sizeof (vl.type_instance));
244 }
245 else /* if ((r->instances_num > 0) */
246 {
247 if (r->instance_prefix == NULL)
248 {
249 strjoin (vl.type_instance, sizeof (vl.type_instance),
250 r_area->instances_buffer, r->instances_num, "-");
251 }
252 else
253 {
254 char tmp[DATA_MAX_NAME_LEN];
256 strjoin (tmp, sizeof (tmp), r_area->instances_buffer,
257 r->instances_num, "-");
258 tmp[sizeof (tmp) - 1] = 0;
260 snprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
261 r->instance_prefix, tmp);
262 }
263 }
264 vl.type_instance[sizeof (vl.type_instance) - 1] = 0;
265 /* }}} */
267 /* Annotate meta data. {{{ */
268 if (r->metadata_num > 0)
269 {
270 vl.meta = meta_data_create ();
271 if (vl.meta == NULL)
272 {
273 ERROR ("db query utils:: meta_data_create failed.");
274 return (-ENOMEM);
275 }
277 for (i = 0; i < r->metadata_num; i++)
278 {
279 status = meta_data_add_string (vl.meta, r->metadata[i],
280 r_area->metadata_buffer[i]);
281 if (status != 0)
282 {
283 ERROR ("db query utils:: meta_data_add_string failed.");
284 meta_data_destroy (vl.meta);
285 vl.meta = NULL;
286 return (status);
287 }
288 }
289 }
290 /* }}} */
292 plugin_dispatch_values (&vl);
294 if (r->metadata_num > 0)
295 {
296 meta_data_destroy (vl.meta);
297 vl.meta = NULL;
298 }
299 sfree (vl.values);
300 return (0);
301 } /* }}} void udb_result_submit */
303 static void udb_result_finish_result (udb_result_t const *r, /* {{{ */
304 udb_result_preparation_area_t *prep_area)
305 {
306 if ((r == NULL) || (prep_area == NULL))
307 return;
309 prep_area->ds = NULL;
310 sfree (prep_area->instances_pos);
311 sfree (prep_area->values_pos);
312 sfree (prep_area->metadata_pos);
313 sfree (prep_area->instances_buffer);
314 sfree (prep_area->values_buffer);
315 sfree (prep_area->metadata_buffer);
316 } /* }}} void udb_result_finish_result */
318 static int udb_result_handle_result (udb_result_t *r, /* {{{ */
319 udb_query_preparation_area_t *q_area,
320 udb_result_preparation_area_t *r_area,
321 udb_query_t const *q, char **column_values)
322 {
323 size_t i;
325 assert (r && q_area && r_area);
327 for (i = 0; i < r->instances_num; i++)
328 r_area->instances_buffer[i] = column_values[r_area->instances_pos[i]];
330 for (i = 0; i < r->values_num; i++)
331 r_area->values_buffer[i] = column_values[r_area->values_pos[i]];
333 for (i = 0; i < r->metadata_num; i++)
334 r_area->metadata_buffer[i] = column_values[r_area->metadata_pos[i]];
336 return udb_result_submit (r, r_area, q, q_area);
337 } /* }}} int udb_result_handle_result */
339 static int udb_result_prepare_result (udb_result_t const *r, /* {{{ */
340 udb_result_preparation_area_t *prep_area,
341 char **column_names, size_t column_num)
342 {
343 size_t i;
345 if ((r == NULL) || (prep_area == NULL))
346 return (-EINVAL);
348 #define BAIL_OUT(status) \
349 prep_area->ds = NULL; \
350 sfree (prep_area->instances_pos); \
351 sfree (prep_area->values_pos); \
352 sfree (prep_area->metadata_pos); \
353 sfree (prep_area->instances_buffer); \
354 sfree (prep_area->values_buffer); \
355 sfree (prep_area->metadata_buffer); \
356 return (status)
358 /* Make sure previous preparations are cleaned up. */
359 udb_result_finish_result (r, prep_area);
360 prep_area->instances_pos = NULL;
361 prep_area->values_pos = NULL;
362 prep_area->metadata_pos = NULL;
364 /* Read `ds' and check number of values {{{ */
365 prep_area->ds = plugin_get_ds (r->type);
366 if (prep_area->ds == NULL)
367 {
368 ERROR ("db query utils: udb_result_prepare_result: Type `%s' is not "
369 "known by the daemon. See types.db(5) for details.",
370 r->type);
371 BAIL_OUT (-1);
372 }
374 if (prep_area->ds->ds_num != r->values_num)
375 {
376 ERROR ("db query utils: udb_result_prepare_result: The type `%s' "
377 "requires exactly %zu value%s, but the configuration specifies %zu.",
378 r->type,
379 prep_area->ds->ds_num, (prep_area->ds->ds_num == 1) ? "" : "s",
380 r->values_num);
381 BAIL_OUT (-1);
382 }
383 /* }}} */
385 /* Allocate r->instances_pos, r->values_pos, r->metadata_post,
386 * r->instances_buffer, r->values_buffer, and r->metadata_buffer {{{ */
387 if (r->instances_num > 0)
388 {
389 prep_area->instances_pos
390 = (size_t *) calloc (r->instances_num, sizeof (size_t));
391 if (prep_area->instances_pos == NULL)
392 {
393 ERROR ("db query utils: udb_result_prepare_result: malloc failed.");
394 BAIL_OUT (-ENOMEM);
395 }
397 prep_area->instances_buffer
398 = (char **) calloc (r->instances_num, sizeof (char *));
399 if (prep_area->instances_buffer == NULL)
400 {
401 ERROR ("db query utils: udb_result_prepare_result: malloc failed.");
402 BAIL_OUT (-ENOMEM);
403 }
404 } /* if (r->instances_num > 0) */
406 prep_area->values_pos
407 = (size_t *) calloc (r->values_num, sizeof (size_t));
408 if (prep_area->values_pos == NULL)
409 {
410 ERROR ("db query utils: udb_result_prepare_result: malloc failed.");
411 BAIL_OUT (-ENOMEM);
412 }
414 prep_area->values_buffer
415 = (char **) calloc (r->values_num, sizeof (char *));
416 if (prep_area->values_buffer == NULL)
417 {
418 ERROR ("db query utils: udb_result_prepare_result: malloc failed.");
419 BAIL_OUT (-ENOMEM);
420 }
422 prep_area->metadata_pos
423 = (size_t *) calloc (r->metadata_num, sizeof (size_t));
424 if (prep_area->metadata_pos == NULL)
425 {
426 ERROR ("db query utils: udb_result_prepare_result: malloc failed.");
427 BAIL_OUT (-ENOMEM);
428 }
430 prep_area->metadata_buffer
431 = (char **) calloc (r->metadata_num, sizeof (char *));
432 if (prep_area->metadata_buffer == NULL)
433 {
434 ERROR ("db query utils: udb_result_prepare_result: malloc failed.");
435 BAIL_OUT (-ENOMEM);
436 }
438 /* }}} */
440 /* Determine the position of the instance columns {{{ */
441 for (i = 0; i < r->instances_num; i++)
442 {
443 size_t j;
445 for (j = 0; j < column_num; j++)
446 {
447 if (strcasecmp (r->instances[i], column_names[j]) == 0)
448 {
449 prep_area->instances_pos[i] = j;
450 break;
451 }
452 }
454 if (j >= column_num)
455 {
456 ERROR ("db query utils: udb_result_prepare_result: "
457 "Column `%s' could not be found.",
458 r->instances[i]);
459 BAIL_OUT (-ENOENT);
460 }
461 } /* }}} for (i = 0; i < r->instances_num; i++) */
463 /* Determine the position of the value columns {{{ */
464 for (i = 0; i < r->values_num; i++)
465 {
466 size_t j;
468 for (j = 0; j < column_num; j++)
469 {
470 if (strcasecmp (r->values[i], column_names[j]) == 0)
471 {
472 prep_area->values_pos[i] = j;
473 break;
474 }
475 }
477 if (j >= column_num)
478 {
479 ERROR ("db query utils: udb_result_prepare_result: "
480 "Column `%s' could not be found.",
481 r->values[i]);
482 BAIL_OUT (-ENOENT);
483 }
484 } /* }}} for (i = 0; i < r->values_num; i++) */
486 /* Determine the position of the metadata columns {{{ */
487 for (i = 0; i < r->metadata_num; i++)
488 {
489 size_t j;
491 for (j = 0; j < column_num; j++)
492 {
493 if (strcasecmp (r->metadata[i], column_names[j]) == 0)
494 {
495 prep_area->metadata_pos[i] = j;
496 break;
497 }
498 }
500 if (j >= column_num)
501 {
502 ERROR ("db query utils: udb_result_prepare_result: "
503 "Metadata column `%s' could not be found.",
504 r->values[i]);
505 BAIL_OUT (-ENOENT);
506 }
507 } /* }}} for (i = 0; i < r->metadata_num; i++) */
509 #undef BAIL_OUT
510 return (0);
511 } /* }}} int udb_result_prepare_result */
513 static void udb_result_free (udb_result_t *r) /* {{{ */
514 {
515 size_t i;
517 if (r == NULL)
518 return;
520 sfree (r->type);
522 for (i = 0; i < r->instances_num; i++)
523 sfree (r->instances[i]);
524 sfree (r->instances);
526 for (i = 0; i < r->values_num; i++)
527 sfree (r->values[i]);
528 sfree (r->values);
530 for (i = 0; i < r->metadata_num; i++)
531 sfree (r->metadata[i]);
532 sfree (r->metadata);
534 udb_result_free (r->next);
536 sfree (r);
537 } /* }}} void udb_result_free */
539 static int udb_result_create (const char *query_name, /* {{{ */
540 udb_result_t **r_head, oconfig_item_t *ci)
541 {
542 udb_result_t *r;
543 int status;
544 int i;
546 if (ci->values_num != 0)
547 {
548 WARNING ("db query utils: The `Result' block doesn't accept "
549 "any arguments. Ignoring %i argument%s.",
550 ci->values_num, (ci->values_num == 1) ? "" : "s");
551 }
553 r = (udb_result_t *) malloc (sizeof (*r));
554 if (r == NULL)
555 {
556 ERROR ("db query utils: malloc failed.");
557 return (-1);
558 }
559 memset (r, 0, sizeof (*r));
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 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 = (udb_query_t *) malloc (sizeof (*q));
682 if (q == NULL)
683 {
684 ERROR ("db query utils: malloc failed.");
685 return (-1);
686 }
687 memset (q, 0, sizeof (*q));
688 q->min_version = 0;
689 q->max_version = UINT_MAX;
691 status = udb_config_set_string (&q->name, ci);
692 if (status != 0)
693 {
694 sfree (q);
695 return (status);
696 }
698 /* Fill the `udb_query_t' structure.. */
699 for (i = 0; i < ci->children_num; i++)
700 {
701 oconfig_item_t *child = ci->children + i;
703 if (strcasecmp ("Statement", child->key) == 0)
704 status = udb_config_set_string (&q->statement, child);
705 else if (strcasecmp ("Result", child->key) == 0)
706 status = udb_result_create (q->name, &q->results, child);
707 else if (strcasecmp ("MinVersion", child->key) == 0)
708 status = udb_config_set_uint (&q->min_version, child);
709 else if (strcasecmp ("MaxVersion", child->key) == 0)
710 status = udb_config_set_uint (&q->max_version, child);
712 /* Call custom callbacks */
713 else if (cb != NULL)
714 {
715 status = (*cb) (q, child);
716 if (status != 0)
717 {
718 WARNING ("db query utils: The configuration callback failed "
719 "to handle `%s'.", child->key);
720 }
721 }
722 else
723 {
724 WARNING ("db query utils: Query `%s': Option `%s' not allowed here.",
725 q->name, child->key);
726 status = -1;
727 }
729 if (status != 0)
730 break;
731 }
733 /* Check that all necessary options have been given. */
734 if (status == 0)
735 {
736 if (q->statement == NULL)
737 {
738 WARNING ("db query utils: Query `%s': No `Statement' given.", q->name);
739 status = -1;
740 }
741 if (q->results == NULL)
742 {
743 WARNING ("db query utils: Query `%s': No (valid) `Result' block given.",
744 q->name);
745 status = -1;
746 }
747 } /* if (status == 0) */
749 /* If all went well, add this query to the list of queries within the
750 * database structure. */
751 if (status == 0)
752 {
753 udb_query_t **temp;
755 temp = (udb_query_t **) realloc (query_list,
756 sizeof (*query_list) * (query_list_len + 1));
757 if (temp == NULL)
758 {
759 ERROR ("db query utils: realloc failed");
760 status = -1;
761 }
762 else
763 {
764 query_list = temp;
765 query_list[query_list_len] = q;
766 query_list_len++;
767 }
768 }
770 if (status != 0)
771 {
772 udb_query_free_one (q);
773 return (-1);
774 }
776 *ret_query_list = query_list;
777 *ret_query_list_len = query_list_len;
779 return (0);
780 } /* }}} int udb_query_create */
782 void udb_query_free (udb_query_t **query_list, size_t query_list_len) /* {{{ */
783 {
784 size_t i;
786 if (query_list == NULL)
787 return;
789 for (i = 0; i < query_list_len; i++)
790 udb_query_free_one (query_list[i]);
792 sfree (query_list);
793 } /* }}} void udb_query_free */
795 int udb_query_pick_from_list_by_name (const char *name, /* {{{ */
796 udb_query_t **src_list, size_t src_list_len,
797 udb_query_t ***dst_list, size_t *dst_list_len)
798 {
799 size_t i;
800 int num_added;
802 if ((name == NULL) || (src_list == NULL) || (dst_list == NULL)
803 || (dst_list_len == NULL))
804 {
805 ERROR ("db query utils: udb_query_pick_from_list_by_name: "
806 "Invalid argument.");
807 return (-EINVAL);
808 }
810 num_added = 0;
811 for (i = 0; i < src_list_len; i++)
812 {
813 udb_query_t **tmp_list;
814 size_t tmp_list_len;
816 if (strcasecmp (name, src_list[i]->name) != 0)
817 continue;
819 tmp_list_len = *dst_list_len;
820 tmp_list = (udb_query_t **) realloc (*dst_list, (tmp_list_len + 1)
821 * sizeof (udb_query_t *));
822 if (tmp_list == NULL)
823 {
824 ERROR ("db query utils: realloc failed.");
825 return (-ENOMEM);
826 }
828 tmp_list[tmp_list_len] = src_list[i];
829 tmp_list_len++;
831 *dst_list = tmp_list;
832 *dst_list_len = tmp_list_len;
834 num_added++;
835 } /* for (i = 0; i < src_list_len; i++) */
837 if (num_added <= 0)
838 {
839 ERROR ("db query utils: Cannot find query `%s'. Make sure the <Query> "
840 "block is above the database definition!",
841 name);
842 return (-ENOENT);
843 }
844 else
845 {
846 DEBUG ("db query utils: Added %i versions of query `%s'.",
847 num_added, name);
848 }
850 return (0);
851 } /* }}} int udb_query_pick_from_list_by_name */
853 int udb_query_pick_from_list (oconfig_item_t *ci, /* {{{ */
854 udb_query_t **src_list, size_t src_list_len,
855 udb_query_t ***dst_list, size_t *dst_list_len)
856 {
857 const char *name;
859 if ((ci == NULL) || (src_list == NULL) || (dst_list == NULL)
860 || (dst_list_len == NULL))
861 {
862 ERROR ("db query utils: udb_query_pick_from_list: "
863 "Invalid argument.");
864 return (-EINVAL);
865 }
867 if ((ci->values_num != 1)
868 || (ci->values[0].type != OCONFIG_TYPE_STRING))
869 {
870 ERROR ("db query utils: The `%s' config option "
871 "needs exactly one string argument.", ci->key);
872 return (-1);
873 }
874 name = ci->values[0].value.string;
876 return (udb_query_pick_from_list_by_name (name,
877 src_list, src_list_len,
878 dst_list, dst_list_len));
879 } /* }}} int udb_query_pick_from_list */
881 const char *udb_query_get_name (udb_query_t *q) /* {{{ */
882 {
883 if (q == NULL)
884 return (NULL);
886 return (q->name);
887 } /* }}} const char *udb_query_get_name */
889 const char *udb_query_get_statement (udb_query_t *q) /* {{{ */
890 {
891 if (q == NULL)
892 return (NULL);
894 return (q->statement);
895 } /* }}} const char *udb_query_get_statement */
897 void udb_query_set_user_data (udb_query_t *q, void *user_data) /* {{{ */
898 {
899 if (q == NULL)
900 return;
902 q->user_data = user_data;
903 } /* }}} void udb_query_set_user_data */
905 void *udb_query_get_user_data (udb_query_t *q) /* {{{ */
906 {
907 if (q == NULL)
908 return (NULL);
910 return (q->user_data);
911 } /* }}} void *udb_query_get_user_data */
913 int udb_query_check_version (udb_query_t *q, unsigned int version) /* {{{ */
914 {
915 if (q == NULL)
916 return (-EINVAL);
918 if ((version < q->min_version) || (version > q->max_version))
919 return (0);
921 return (1);
922 } /* }}} int udb_query_check_version */
924 void udb_query_finish_result (udb_query_t const *q, /* {{{ */
925 udb_query_preparation_area_t *prep_area)
926 {
927 udb_result_preparation_area_t *r_area;
928 udb_result_t *r;
930 if ((q == NULL) || (prep_area == NULL))
931 return;
933 prep_area->column_num = 0;
934 sfree (prep_area->host);
935 sfree (prep_area->plugin);
936 sfree (prep_area->db_name);
938 prep_area->interval = 0;
940 for (r = q->results, r_area = prep_area->result_prep_areas;
941 r != NULL; r = r->next, r_area = r_area->next)
942 {
943 /* this may happen during error conditions of the caller */
944 if (r_area == NULL)
945 break;
946 udb_result_finish_result (r, r_area);
947 }
948 } /* }}} void udb_query_finish_result */
950 int udb_query_handle_result (udb_query_t const *q, /* {{{ */
951 udb_query_preparation_area_t *prep_area, char **column_values)
952 {
953 udb_result_preparation_area_t *r_area;
954 udb_result_t *r;
955 int success;
956 int status;
958 if ((q == NULL) || (prep_area == NULL))
959 return (-EINVAL);
961 if ((prep_area->column_num < 1) || (prep_area->host == NULL)
962 || (prep_area->plugin == NULL) || (prep_area->db_name == NULL))
963 {
964 ERROR ("db query utils: Query `%s': Query is not prepared; "
965 "can't handle result.", q->name);
966 return (-EINVAL);
967 }
969 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG /* {{{ */
970 do
971 {
972 size_t i;
974 for (i = 0; i < prep_area->column_num; i++)
975 {
976 DEBUG ("db query utils: udb_query_handle_result (%s, %s): "
977 "column[%zu] = %s;",
978 prep_area->db_name, q->name, i, column_values[i]);
979 }
980 } while (0);
981 #endif /* }}} */
983 success = 0;
984 for (r = q->results, r_area = prep_area->result_prep_areas;
985 r != NULL; r = r->next, r_area = r_area->next)
986 {
987 status = udb_result_handle_result (r, prep_area, r_area,
988 q, column_values);
989 if (status == 0)
990 success++;
991 }
993 if (success == 0)
994 {
995 ERROR ("db query utils: udb_query_handle_result (%s, %s): "
996 "All results failed.", prep_area->db_name, q->name);
997 return (-1);
998 }
1000 return (0);
1001 } /* }}} int udb_query_handle_result */
1003 int udb_query_prepare_result (udb_query_t const *q, /* {{{ */
1004 udb_query_preparation_area_t *prep_area,
1005 const char *host, const char *plugin, const char *db_name,
1006 char **column_names, size_t column_num, cdtime_t interval)
1007 {
1008 udb_result_preparation_area_t *r_area;
1009 udb_result_t *r;
1010 int status;
1012 if ((q == NULL) || (prep_area == NULL))
1013 return (-EINVAL);
1015 udb_query_finish_result (q, prep_area);
1017 prep_area->column_num = column_num;
1018 prep_area->host = strdup (host);
1019 prep_area->plugin = strdup (plugin);
1020 prep_area->db_name = strdup (db_name);
1022 prep_area->interval = interval;
1024 if ((prep_area->host == NULL) || (prep_area->plugin == NULL)
1025 || (prep_area->db_name == NULL))
1026 {
1027 ERROR ("db query utils: Query `%s': Prepare failed: Out of memory.", q->name);
1028 udb_query_finish_result (q, prep_area);
1029 return (-ENOMEM);
1030 }
1032 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG
1033 do
1034 {
1035 size_t i;
1037 for (i = 0; i < column_num; i++)
1038 {
1039 DEBUG ("db query utils: udb_query_prepare_result: "
1040 "query = %s; column[%zu] = %s;",
1041 q->name, i, column_names[i]);
1042 }
1043 } while (0);
1044 #endif
1046 for (r = q->results, r_area = prep_area->result_prep_areas;
1047 r != NULL; r = r->next, r_area = r_area->next)
1048 {
1049 if (! r_area)
1050 {
1051 ERROR ("db query utils: Query `%s': Invalid number of result "
1052 "preparation areas.", q->name);
1053 udb_query_finish_result (q, prep_area);
1054 return (-EINVAL);
1055 }
1057 status = udb_result_prepare_result (r, r_area, column_names, column_num);
1058 if (status != 0)
1059 {
1060 udb_query_finish_result (q, prep_area);
1061 return (status);
1062 }
1063 }
1065 return (0);
1066 } /* }}} int udb_query_prepare_result */
1068 udb_query_preparation_area_t *
1069 udb_query_allocate_preparation_area (udb_query_t *q) /* {{{ */
1070 {
1071 udb_query_preparation_area_t *q_area;
1072 udb_result_preparation_area_t **next_r_area;
1073 udb_result_t *r;
1075 q_area = malloc (sizeof (*q_area));
1076 if (q_area == NULL)
1077 return NULL;
1078 memset (q_area, 0, sizeof (*q_area));
1080 next_r_area = &q_area->result_prep_areas;
1081 for (r = q->results; r != NULL; r = r->next)
1082 {
1083 udb_result_preparation_area_t *r_area;
1085 r_area = malloc (sizeof (*r_area));
1086 if (r_area == NULL)
1087 {
1088 udb_result_preparation_area_t *a = q_area->result_prep_areas;
1090 while (a != NULL)
1091 {
1092 udb_result_preparation_area_t *next = a->next;
1093 sfree (a);
1094 a = next;
1095 }
1097 free (q_area);
1098 return NULL;
1099 }
1101 memset (r_area, 0, sizeof (*r_area));
1103 *next_r_area = r_area;
1104 next_r_area = &r_area->next;
1105 }
1107 return (q_area);
1108 } /* }}} udb_query_preparation_area_t *udb_query_allocate_preparation_area */
1110 void
1111 udb_query_delete_preparation_area (udb_query_preparation_area_t *q_area) /* {{{ */
1112 {
1113 udb_result_preparation_area_t *r_area;
1115 if (q_area == NULL)
1116 return;
1118 r_area = q_area->result_prep_areas;
1119 while (r_area != NULL)
1120 {
1121 udb_result_preparation_area_t *area = r_area;
1123 r_area = r_area->next;
1125 sfree (area->instances_pos);
1126 sfree (area->values_pos);
1127 sfree (area->instances_buffer);
1128 sfree (area->values_buffer);
1129 free (area);
1130 }
1132 sfree (q_area->host);
1133 sfree (q_area->plugin);
1134 sfree (q_area->db_name);
1136 free (q_area);
1137 } /* }}} void udb_query_delete_preparation_area */
1139 /* vim: set sw=2 sts=2 et fdm=marker : */