2 * collection4 - graph_ident.c
3 * Copyright (C) 2010 Florian octo Forster
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
21 * Florian octo Forster <ff at octo.it>
32 #include <limits.h> /* PATH_MAX */
33 #include <sys/types.h>
37 #include "graph_ident.h"
39 #include "data_provider.h"
40 #include "filesystem.h"
41 #include "utils_cgi.h"
44 #include <fcgi_stdio.h>
49 struct graph_ident_s /* {{{ */
53 char *plugin_instance;
56 }; /* }}} struct graph_ident_s */
61 static char *part_copy_with_selector (const char *selector, /* {{{ */
62 const char *part, unsigned int flags)
64 if ((selector == NULL) || (part == NULL))
67 if ((flags & IDENT_FLAG_REPLACE_ANY) && IS_ANY (part))
70 if ((flags & IDENT_FLAG_REPLACE_ALL) && IS_ALL (part))
73 /* Replace the ANY and ALL flags if requested and if the selecter actually
75 if (IS_ANY (selector))
77 if (flags & IDENT_FLAG_REPLACE_ANY)
78 return (strdup (part));
80 return (strdup (selector));
83 if (IS_ALL (selector))
85 if (flags & IDENT_FLAG_REPLACE_ALL)
86 return (strdup (part));
88 return (strdup (selector));
91 if (strcmp (selector, part) != 0)
94 /* Otherwise (no replacement), return a copy of the selector. */
95 return (strdup (selector));
96 } /* }}} char *part_copy_with_selector */
98 static _Bool part_matches (const char *selector, /* {{{ */
102 if ((selector == NULL) && (part == NULL))
106 if (selector == NULL) /* && (part != NULL) */
109 if (IS_ANY(selector) || IS_ALL(selector))
112 if (part == NULL) /* && (selector != NULL) */
115 if (strcmp (selector, part) == 0)
119 } /* }}} _Bool part_matches */
124 graph_ident_t *ident_create (const char *host, /* {{{ */
125 const char *plugin, const char *plugin_instance,
126 const char *type, const char *type_instance)
131 || (plugin == NULL) || (plugin_instance == NULL)
132 || (type == NULL) || (type_instance == NULL))
135 ret = malloc (sizeof (*ret));
138 memset (ret, 0, sizeof (*ret));
143 ret->plugin_instance = NULL;
145 ret->type_instance = NULL;
147 #define COPY_PART(p) do { \
148 ret->p = strdup (p); \
149 if (ret->p == NULL) \
152 free (ret->plugin); \
153 free (ret->plugin_instance); \
155 free (ret->type_instance); \
163 COPY_PART(plugin_instance);
165 COPY_PART(type_instance);
170 } /* }}} graph_ident_t *ident_create */
172 graph_ident_t *ident_clone (const graph_ident_t *ident) /* {{{ */
174 return (ident_create (ident->host,
175 ident->plugin, ident->plugin_instance,
176 ident->type, ident->type_instance));
177 } /* }}} graph_ident_t *ident_clone */
179 graph_ident_t *ident_copy_with_selector (const graph_ident_t *selector, /* {{{ */
180 const graph_ident_t *ident, unsigned int flags)
184 if ((selector == NULL) || (ident == NULL))
187 ret = malloc (sizeof (*ret));
190 memset (ret, 0, sizeof (*ret));
193 ret->plugin_instance = NULL;
195 ret->type_instance = NULL;
197 #define COPY_PART(p) do { \
198 ret->p = part_copy_with_selector (selector->p, ident->p, flags); \
199 if (ret->p == NULL) \
202 free (ret->plugin); \
203 free (ret->plugin_instance); \
205 free (ret->type_instance); \
212 COPY_PART (plugin_instance);
214 COPY_PART (type_instance);
219 } /* }}} graph_ident_t *ident_copy_with_selector */
221 void ident_destroy (graph_ident_t *ident) /* {{{ */
227 free (ident->plugin);
228 free (ident->plugin_instance);
230 free (ident->type_instance);
233 } /* }}} void ident_destroy */
235 /* ident_get_* methods {{{ */
236 const char *ident_get_host (const graph_ident_t *ident) /* {{{ */
241 return (ident->host);
242 } /* }}} char *ident_get_host */
244 const char *ident_get_plugin (const graph_ident_t *ident) /* {{{ */
249 return (ident->plugin);
250 } /* }}} char *ident_get_plugin */
252 const char *ident_get_plugin_instance (const graph_ident_t *ident) /* {{{ */
257 return (ident->plugin_instance);
258 } /* }}} char *ident_get_plugin_instance */
260 const char *ident_get_type (const graph_ident_t *ident) /* {{{ */
265 return (ident->type);
266 } /* }}} char *ident_get_type */
268 const char *ident_get_type_instance (const graph_ident_t *ident) /* {{{ */
273 return (ident->type_instance);
274 } /* }}} char *ident_get_type_instance */
276 const char *ident_get_field (const graph_ident_t *ident, /* {{{ */
277 graph_ident_field_t field)
279 if ((ident == NULL) || (field >= _GIF_LAST))
282 if (field == GIF_HOST)
283 return (ident->host);
284 else if (field == GIF_PLUGIN)
285 return (ident->plugin);
286 else if (field == GIF_PLUGIN_INSTANCE)
287 return (ident->plugin_instance);
288 else if (field == GIF_TYPE)
289 return (ident->type);
290 else if (field == GIF_TYPE_INSTANCE)
291 return (ident->type_instance);
293 return (NULL); /* never reached */
294 } /* }}} const char *ident_get_field */
295 /* }}} ident_get_* methods */
297 /* ident_set_* methods {{{ */
298 int ident_set_host (graph_ident_t *ident, const char *host) /* {{{ */
302 if ((ident == NULL) || (host == NULL))
313 } /* }}} int ident_set_host */
315 int ident_set_plugin (graph_ident_t *ident, const char *plugin) /* {{{ */
319 if ((ident == NULL) || (plugin == NULL))
322 tmp = strdup (plugin);
326 free (ident->plugin);
330 } /* }}} int ident_set_plugin */
332 int ident_set_plugin_instance (graph_ident_t *ident, const char *plugin_instance) /* {{{ */
336 if ((ident == NULL) || (plugin_instance == NULL))
339 tmp = strdup (plugin_instance);
343 free (ident->plugin_instance);
344 ident->plugin_instance = tmp;
347 } /* }}} int ident_set_plugin_instance */
349 int ident_set_type (graph_ident_t *ident, const char *type) /* {{{ */
353 if ((ident == NULL) || (type == NULL))
364 } /* }}} int ident_set_type */
366 int ident_set_type_instance (graph_ident_t *ident, const char *type_instance) /* {{{ */
370 if ((ident == NULL) || (type_instance == NULL))
373 tmp = strdup (type_instance);
377 free (ident->type_instance);
378 ident->type_instance = tmp;
381 } /* }}} int ident_set_type_instance */
383 /* }}} ident_set_* methods */
385 int ident_compare (const graph_ident_t *i0, /* {{{ */
386 const graph_ident_t *i1)
390 #define COMPARE_PART(p) do { \
391 status = strcmp (i0->p, i1->p); \
397 COMPARE_PART (plugin);
398 COMPARE_PART (plugin_instance);
400 COMPARE_PART (type_instance);
405 } /* }}} int ident_compare */
407 _Bool ident_matches (const graph_ident_t *selector, /* {{{ */
408 const graph_ident_t *ident)
411 if ((selector == NULL) || (ident == NULL))
415 if (!part_matches (selector->host, ident->host))
418 if (!part_matches (selector->plugin, ident->plugin))
421 if (!part_matches (selector->plugin_instance, ident->plugin_instance))
424 if (!part_matches (selector->type, ident->type))
427 if (!part_matches (selector->type_instance, ident->type_instance))
431 } /* }}} _Bool ident_matches */
433 _Bool ident_intersect (const graph_ident_t *s0, /* {{{ */
434 const graph_ident_t *s1)
436 #define INTERSECT_PART(p) do { \
437 if (!IS_ANY (s0->p) && !IS_ALL (s0->p) \
438 && !IS_ANY (s1->p) && !IS_ALL (s1->p) \
439 && (strcmp (s0->p, s1->p) != 0)) \
443 INTERSECT_PART (host);
444 INTERSECT_PART (plugin);
445 INTERSECT_PART (plugin_instance);
446 INTERSECT_PART (type);
447 INTERSECT_PART (type_instance);
449 #undef INTERSECT_PART
452 } /* }}} _Bool ident_intersect */
454 char *ident_to_string (const graph_ident_t *ident) /* {{{ */
456 char buffer[PATH_MAX];
460 strlcat (buffer, ident->host, sizeof (buffer));
461 strlcat (buffer, "/", sizeof (buffer));
462 strlcat (buffer, ident->plugin, sizeof (buffer));
463 if (ident->plugin_instance[0] != 0)
465 strlcat (buffer, "-", sizeof (buffer));
466 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
468 strlcat (buffer, "/", sizeof (buffer));
469 strlcat (buffer, ident->type, sizeof (buffer));
470 if (ident->type_instance[0] != 0)
472 strlcat (buffer, "-", sizeof (buffer));
473 strlcat (buffer, ident->type_instance, sizeof (buffer));
476 return (strdup (buffer));
477 } /* }}} char *ident_to_string */
479 char *ident_to_file (const graph_ident_t *ident) /* {{{ */
481 char buffer[PATH_MAX];
485 strlcat (buffer, DATA_DIR, sizeof (buffer));
486 strlcat (buffer, "/", sizeof (buffer));
488 strlcat (buffer, ident->host, sizeof (buffer));
489 strlcat (buffer, "/", sizeof (buffer));
490 strlcat (buffer, ident->plugin, sizeof (buffer));
491 if (ident->plugin_instance[0] != 0)
493 strlcat (buffer, "-", sizeof (buffer));
494 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
496 strlcat (buffer, "/", sizeof (buffer));
497 strlcat (buffer, ident->type, sizeof (buffer));
498 if (ident->type_instance[0] != 0)
500 strlcat (buffer, "-", sizeof (buffer));
501 strlcat (buffer, ident->type_instance, sizeof (buffer));
504 strlcat (buffer, ".rrd", sizeof (buffer));
506 return (strdup (buffer));
507 } /* }}} char *ident_to_file */
509 int ident_to_json (const graph_ident_t *ident, /* {{{ */
512 yajl_gen_status status;
514 if ((ident == NULL) || (handler == NULL))
517 #define ADD_STRING(str) do { \
518 status = yajl_gen_string (handler, \
519 (unsigned char *) (str), \
520 (unsigned int) strlen (str)); \
521 if (status != yajl_gen_status_ok) \
522 return ((int) status); \
525 yajl_gen_map_open (handler);
527 ADD_STRING (ident->host);
528 ADD_STRING ("plugin");
529 ADD_STRING (ident->plugin);
530 ADD_STRING ("plugin_instance");
531 ADD_STRING (ident->plugin_instance);
533 ADD_STRING (ident->type);
534 ADD_STRING ("type_instance");
535 ADD_STRING (ident->type_instance);
536 yajl_gen_map_close (handler);
541 } /* }}} char *ident_to_json */
543 /* {{{ ident_data_to_json */
544 struct ident_data_to_json__data_s
550 typedef struct ident_data_to_json__data_s ident_data_to_json__data_t;
552 #define yajl_gen_string_cast(h,s,l) \
553 yajl_gen_string (h, (unsigned char *) s, (unsigned int) l)
555 static int ident_data_to_json__get_ident_data (
556 __attribute__((unused)) graph_ident_t *ident, /* {{{ */
557 __attribute__((unused)) const char *ds_name,
558 dp_time_t first_value_time, dp_time_t interval,
559 size_t data_points_num, double *data_points,
562 ident_data_to_json__data_t *data = user_data;
565 double first_value_time_double;
566 double interval_double;
568 /* TODO: Make points_num_limit configurable. */
569 /* points_num_limit: The number of data-points to send at least. */
570 size_t points_num_limit = 400;
571 size_t points_consolidate;
573 first_value_time_double = ((double) first_value_time.tv_sec)
574 + (((double) first_value_time.tv_nsec) / 1000000000.0);
575 interval_double = ((double) interval.tv_sec)
576 + (((double) interval.tv_nsec) / 1000000000.0);
578 if (data_points_num <= points_num_limit)
579 points_consolidate = 1;
581 points_consolidate = data_points_num / points_num_limit;
583 if (points_consolidate > 1)
585 size_t offset = data_points_num % points_consolidate;
587 first_value_time_double += ((double) offset) * interval_double;
588 interval_double *= ((double) points_consolidate);
591 yajl_gen_map_open (data->handler);
593 yajl_gen_string_cast (data->handler, "first_value_time", strlen ("first_value_time"));
594 yajl_gen_double (data->handler, first_value_time_double);
596 yajl_gen_string_cast (data->handler, "interval", strlen ("interval"));
597 yajl_gen_double (data->handler, interval_double);
599 yajl_gen_string_cast (data->handler, "data", strlen ("data"));
600 yajl_gen_array_open (data->handler);
602 for (i = (data_points_num % points_consolidate);
604 i += points_consolidate)
611 for (j = 0; j < points_consolidate; j++)
613 if (isnan (data_points[i+j]))
616 sum += data_points[i+j];
621 yajl_gen_null (data->handler);
623 yajl_gen_double (data->handler, sum / ((double) num));
626 yajl_gen_array_close (data->handler);
629 } /* }}} int ident_data_to_json__get_ident_data */
631 /* Called for each DS name */
632 static int ident_data_to_json__get_ds_name (graph_ident_t *ident, /* {{{ */
633 const char *ds_name, void *user_data)
635 ident_data_to_json__data_t *data = user_data;
638 yajl_gen_map_open (data->handler);
640 yajl_gen_string_cast (data->handler, "file", strlen ("file"));
641 ident_to_json (ident, data->handler);
643 yajl_gen_string_cast (data->handler, "data_source", strlen ("data_source"));
644 yajl_gen_string_cast (data->handler, ds_name, strlen (ds_name));
646 status = data_provider_get_ident_data (ident, ds_name,
647 data->begin, data->end,
648 ident_data_to_json__get_ident_data,
651 yajl_gen_map_close (data->handler);
654 } /* }}} int ident_data_to_json__get_ds_name */
656 int ident_data_to_json (graph_ident_t *ident, /* {{{ */
657 dp_time_t begin, dp_time_t end,
660 ident_data_to_json__data_t data;
665 data.handler = handler;
667 /* Iterate over all DS names */
668 status = data_provider_get_ident_ds_names (ident,
669 ident_data_to_json__get_ds_name, &data);
671 fprintf (stderr, "ident_data_to_json: data_provider_get_ident_ds_names "
672 "failed with status %i\n", status);
675 } /* }}} int ident_data_to_json */
676 /* }}} ident_data_to_json */
678 int ident_describe (const graph_ident_t *ident, /* {{{ */
679 const graph_ident_t *selector,
680 char *buffer, size_t buffer_size)
682 if ((ident == NULL) || (selector == NULL)
683 || (buffer == NULL) || (buffer_size < 2))
688 #define CHECK_FIELD(field) do { \
689 if (strcasecmp (selector->field, ident->field) != 0) \
691 if (buffer[0] != 0) \
692 strlcat (buffer, "/", buffer_size); \
693 strlcat (buffer, ident->field, buffer_size); \
698 CHECK_FIELD (plugin);
699 CHECK_FIELD (plugin_instance);
701 CHECK_FIELD (type_instance);
706 strlcat (buffer, "default", buffer_size);
709 } /* }}} int ident_describe */
711 time_t ident_get_mtime (const graph_ident_t *ident) /* {{{ */
720 file = ident_to_file (ident);
724 memset (&statbuf, 0, sizeof (statbuf));
725 status = stat (file, &statbuf);
728 fprintf (stderr, "ident_get_mtime: stat'ing file \"%s\" failed: %s\n",
729 file, strerror (errno));
734 return (statbuf.st_mtime);
735 } /* }}} time_t ident_get_mtime */
737 /* vim: set sw=2 sts=2 et fdm=marker : */