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>
38 #include "graph_ident.h"
40 #include "data_provider.h"
41 #include "filesystem.h"
42 #include "utils_cgi.h"
45 #include <fcgi_stdio.h>
50 struct graph_ident_s /* {{{ */
54 char *plugin_instance;
57 }; /* }}} struct graph_ident_s */
62 static char *part_copy_with_selector (const char *selector, /* {{{ */
63 const char *part, unsigned int flags)
65 if ((selector == NULL) || (part == NULL))
68 if ((flags & IDENT_FLAG_REPLACE_ANY) && IS_ANY (part))
71 if ((flags & IDENT_FLAG_REPLACE_ALL) && IS_ALL (part))
74 /* Replace the ANY and ALL flags if requested and if the selecter actually
76 if (IS_ANY (selector))
78 if (flags & IDENT_FLAG_REPLACE_ANY)
79 return (strdup (part));
81 return (strdup (selector));
84 if (IS_ALL (selector))
86 if (flags & IDENT_FLAG_REPLACE_ALL)
87 return (strdup (part));
89 return (strdup (selector));
92 if (strcmp (selector, part) != 0)
95 /* Otherwise (no replacement), return a copy of the selector. */
96 return (strdup (selector));
97 } /* }}} char *part_copy_with_selector */
99 static _Bool part_matches (const char *selector, /* {{{ */
103 if ((selector == NULL) && (part == NULL))
107 if (selector == NULL) /* && (part != NULL) */
110 if (IS_ANY(selector) || IS_ALL(selector))
113 if (part == NULL) /* && (selector != NULL) */
116 if (strcmp (selector, part) == 0)
120 } /* }}} _Bool part_matches */
125 graph_ident_t *ident_create (const char *host, /* {{{ */
126 const char *plugin, const char *plugin_instance,
127 const char *type, const char *type_instance)
132 || (plugin == NULL) || (plugin_instance == NULL)
133 || (type == NULL) || (type_instance == NULL))
136 ret = malloc (sizeof (*ret));
139 memset (ret, 0, sizeof (*ret));
144 ret->plugin_instance = NULL;
146 ret->type_instance = NULL;
148 #define COPY_PART(p) do { \
149 ret->p = strdup (p); \
150 if (ret->p == NULL) \
153 free (ret->plugin); \
154 free (ret->plugin_instance); \
156 free (ret->type_instance); \
164 COPY_PART(plugin_instance);
166 COPY_PART(type_instance);
171 } /* }}} graph_ident_t *ident_create */
173 graph_ident_t *ident_clone (const graph_ident_t *ident) /* {{{ */
175 return (ident_create (ident->host,
176 ident->plugin, ident->plugin_instance,
177 ident->type, ident->type_instance));
178 } /* }}} graph_ident_t *ident_clone */
180 graph_ident_t *ident_copy_with_selector (const graph_ident_t *selector, /* {{{ */
181 const graph_ident_t *ident, unsigned int flags)
185 if ((selector == NULL) || (ident == NULL))
188 ret = malloc (sizeof (*ret));
191 memset (ret, 0, sizeof (*ret));
194 ret->plugin_instance = NULL;
196 ret->type_instance = NULL;
198 #define COPY_PART(p) do { \
199 ret->p = part_copy_with_selector (selector->p, ident->p, flags); \
200 if (ret->p == NULL) \
203 free (ret->plugin); \
204 free (ret->plugin_instance); \
206 free (ret->type_instance); \
213 COPY_PART (plugin_instance);
215 COPY_PART (type_instance);
220 } /* }}} graph_ident_t *ident_copy_with_selector */
222 void ident_destroy (graph_ident_t *ident) /* {{{ */
228 free (ident->plugin);
229 free (ident->plugin_instance);
231 free (ident->type_instance);
234 } /* }}} void ident_destroy */
236 /* ident_get_* methods {{{ */
237 const char *ident_get_host (const graph_ident_t *ident) /* {{{ */
242 return (ident->host);
243 } /* }}} char *ident_get_host */
245 const char *ident_get_plugin (const graph_ident_t *ident) /* {{{ */
250 return (ident->plugin);
251 } /* }}} char *ident_get_plugin */
253 const char *ident_get_plugin_instance (const graph_ident_t *ident) /* {{{ */
258 return (ident->plugin_instance);
259 } /* }}} char *ident_get_plugin_instance */
261 const char *ident_get_type (const graph_ident_t *ident) /* {{{ */
266 return (ident->type);
267 } /* }}} char *ident_get_type */
269 const char *ident_get_type_instance (const graph_ident_t *ident) /* {{{ */
274 return (ident->type_instance);
275 } /* }}} char *ident_get_type_instance */
277 const char *ident_get_field (const graph_ident_t *ident, /* {{{ */
278 graph_ident_field_t field)
280 if ((ident == NULL) || (field >= _GIF_LAST))
283 if (field == GIF_HOST)
284 return (ident->host);
285 else if (field == GIF_PLUGIN)
286 return (ident->plugin);
287 else if (field == GIF_PLUGIN_INSTANCE)
288 return (ident->plugin_instance);
289 else if (field == GIF_TYPE)
290 return (ident->type);
291 else if (field == GIF_TYPE_INSTANCE)
292 return (ident->type_instance);
294 return (NULL); /* never reached */
295 } /* }}} const char *ident_get_field */
296 /* }}} ident_get_* methods */
298 /* ident_set_* methods {{{ */
299 int ident_set_host (graph_ident_t *ident, const char *host) /* {{{ */
303 if ((ident == NULL) || (host == NULL))
314 } /* }}} int ident_set_host */
316 int ident_set_plugin (graph_ident_t *ident, const char *plugin) /* {{{ */
320 if ((ident == NULL) || (plugin == NULL))
323 tmp = strdup (plugin);
327 free (ident->plugin);
331 } /* }}} int ident_set_plugin */
333 int ident_set_plugin_instance (graph_ident_t *ident, const char *plugin_instance) /* {{{ */
337 if ((ident == NULL) || (plugin_instance == NULL))
340 tmp = strdup (plugin_instance);
344 free (ident->plugin_instance);
345 ident->plugin_instance = tmp;
348 } /* }}} int ident_set_plugin_instance */
350 int ident_set_type (graph_ident_t *ident, const char *type) /* {{{ */
354 if ((ident == NULL) || (type == NULL))
365 } /* }}} int ident_set_type */
367 int ident_set_type_instance (graph_ident_t *ident, const char *type_instance) /* {{{ */
371 if ((ident == NULL) || (type_instance == NULL))
374 tmp = strdup (type_instance);
378 free (ident->type_instance);
379 ident->type_instance = tmp;
382 } /* }}} int ident_set_type_instance */
384 /* }}} ident_set_* methods */
386 int ident_compare (const graph_ident_t *i0, /* {{{ */
387 const graph_ident_t *i1)
391 #define COMPARE_PART(p) do { \
392 status = strcmp (i0->p, i1->p); \
398 COMPARE_PART (plugin);
399 COMPARE_PART (plugin_instance);
401 COMPARE_PART (type_instance);
406 } /* }}} int ident_compare */
408 _Bool ident_matches (const graph_ident_t *selector, /* {{{ */
409 const graph_ident_t *ident)
412 if ((selector == NULL) || (ident == NULL))
416 if (!part_matches (selector->host, ident->host))
419 if (!part_matches (selector->plugin, ident->plugin))
422 if (!part_matches (selector->plugin_instance, ident->plugin_instance))
425 if (!part_matches (selector->type, ident->type))
428 if (!part_matches (selector->type_instance, ident->type_instance))
432 } /* }}} _Bool ident_matches */
434 _Bool ident_intersect (const graph_ident_t *s0, /* {{{ */
435 const graph_ident_t *s1)
437 #define INTERSECT_PART(p) do { \
438 if (!IS_ANY (s0->p) && !IS_ALL (s0->p) \
439 && !IS_ANY (s1->p) && !IS_ALL (s1->p) \
440 && (strcmp (s0->p, s1->p) != 0)) \
444 INTERSECT_PART (host);
445 INTERSECT_PART (plugin);
446 INTERSECT_PART (plugin_instance);
447 INTERSECT_PART (type);
448 INTERSECT_PART (type_instance);
450 #undef INTERSECT_PART
453 } /* }}} _Bool ident_intersect */
455 char *ident_to_string (const graph_ident_t *ident) /* {{{ */
457 char buffer[PATH_MAX];
464 strlcat (buffer, ident->host, sizeof (buffer));
465 strlcat (buffer, "/", sizeof (buffer));
466 strlcat (buffer, ident->plugin, sizeof (buffer));
467 if (ident->plugin_instance[0] != 0)
469 strlcat (buffer, "-", sizeof (buffer));
470 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
472 strlcat (buffer, "/", sizeof (buffer));
473 strlcat (buffer, ident->type, sizeof (buffer));
474 if (ident->type_instance[0] != 0)
476 strlcat (buffer, "-", sizeof (buffer));
477 strlcat (buffer, ident->type_instance, sizeof (buffer));
480 return (strdup (buffer));
481 } /* }}} char *ident_to_string */
483 char *ident_to_file (const graph_ident_t *ident) /* {{{ */
485 char buffer[PATH_MAX];
489 strlcat (buffer, DATA_DIR, sizeof (buffer));
490 strlcat (buffer, "/", sizeof (buffer));
492 strlcat (buffer, ident->host, sizeof (buffer));
493 strlcat (buffer, "/", sizeof (buffer));
494 strlcat (buffer, ident->plugin, sizeof (buffer));
495 if (ident->plugin_instance[0] != 0)
497 strlcat (buffer, "-", sizeof (buffer));
498 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
500 strlcat (buffer, "/", sizeof (buffer));
501 strlcat (buffer, ident->type, sizeof (buffer));
502 if (ident->type_instance[0] != 0)
504 strlcat (buffer, "-", sizeof (buffer));
505 strlcat (buffer, ident->type_instance, sizeof (buffer));
508 strlcat (buffer, ".rrd", sizeof (buffer));
510 return (strdup (buffer));
511 } /* }}} char *ident_to_file */
513 int ident_to_json (const graph_ident_t *ident, /* {{{ */
516 yajl_gen_status status;
518 if ((ident == NULL) || (handler == NULL))
521 #define ADD_STRING(str) do { \
522 status = yajl_gen_string (handler, \
523 (unsigned char *) (str), \
524 (unsigned int) strlen (str)); \
525 if (status != yajl_gen_status_ok) \
526 return ((int) status); \
529 yajl_gen_map_open (handler);
531 ADD_STRING (ident->host);
532 ADD_STRING ("plugin");
533 ADD_STRING (ident->plugin);
534 ADD_STRING ("plugin_instance");
535 ADD_STRING (ident->plugin_instance);
537 ADD_STRING (ident->type);
538 ADD_STRING ("type_instance");
539 ADD_STRING (ident->type_instance);
540 yajl_gen_map_close (handler);
545 } /* }}} char *ident_to_json */
547 /* {{{ ident_data_to_json */
548 struct ident_data_to_json__data_s
555 typedef struct ident_data_to_json__data_s ident_data_to_json__data_t;
557 #define yajl_gen_string_cast(h,s,l) \
558 yajl_gen_string (h, (unsigned char *) s, (unsigned int) l)
560 static int ident_data_to_json__get_ident_data (
561 __attribute__((unused)) graph_ident_t *ident, /* {{{ */
562 __attribute__((unused)) const char *ds_name,
563 dp_time_t first_value_time, dp_time_t interval,
564 size_t data_points_num, double *data_points,
567 ident_data_to_json__data_t *data = user_data;
570 double first_value_time_double;
571 double interval_double;
572 double interval_requested;
573 size_t points_consolidate;
575 first_value_time_double = ((double) first_value_time.tv_sec)
576 + (((double) first_value_time.tv_nsec) / 1000000000.0);
577 interval_double = ((double) interval.tv_sec)
578 + (((double) interval.tv_nsec) / 1000000000.0);
579 interval_requested = ((double) data->interval.tv_sec)
580 + (((double) data->interval.tv_nsec) / 1000000000.0);
582 if (interval_requested < (2.0 * interval_double))
583 points_consolidate = 1;
585 points_consolidate = (size_t) (interval_requested / interval_double);
586 assert (points_consolidate >= 1);
588 if (points_consolidate > 1)
590 size_t offset = data_points_num % points_consolidate;
592 first_value_time_double += ((double) offset) * interval_double;
593 interval_double *= ((double) points_consolidate);
596 yajl_gen_map_open (data->handler);
598 yajl_gen_string_cast (data->handler, "first_value_time", strlen ("first_value_time"));
599 yajl_gen_double (data->handler, first_value_time_double);
601 yajl_gen_string_cast (data->handler, "interval", strlen ("interval"));
602 yajl_gen_double (data->handler, interval_double);
604 yajl_gen_string_cast (data->handler, "data", strlen ("data"));
605 yajl_gen_array_open (data->handler);
607 for (i = (data_points_num % points_consolidate);
609 i += points_consolidate)
616 for (j = 0; j < points_consolidate; j++)
618 if (isnan (data_points[i+j]))
621 sum += data_points[i+j];
626 yajl_gen_null (data->handler);
628 yajl_gen_double (data->handler, sum / ((double) num));
631 yajl_gen_array_close (data->handler);
634 } /* }}} int ident_data_to_json__get_ident_data */
636 /* Called for each DS name */
637 static int ident_data_to_json__get_ds_name (graph_ident_t *ident, /* {{{ */
638 const char *ds_name, void *user_data)
640 ident_data_to_json__data_t *data = user_data;
643 yajl_gen_map_open (data->handler);
645 yajl_gen_string_cast (data->handler, "file", strlen ("file"));
646 ident_to_json (ident, data->handler);
648 yajl_gen_string_cast (data->handler, "data_source", strlen ("data_source"));
649 yajl_gen_string_cast (data->handler, ds_name, strlen (ds_name));
651 status = data_provider_get_ident_data (ident, ds_name,
652 data->begin, data->end,
653 ident_data_to_json__get_ident_data,
656 yajl_gen_map_close (data->handler);
659 } /* }}} int ident_data_to_json__get_ds_name */
661 int ident_data_to_json (graph_ident_t *ident, /* {{{ */
662 dp_time_t begin, dp_time_t end, dp_time_t res,
665 ident_data_to_json__data_t data;
671 data.handler = handler;
673 /* Iterate over all DS names */
674 status = data_provider_get_ident_ds_names (ident,
675 ident_data_to_json__get_ds_name, &data);
677 fprintf (stderr, "ident_data_to_json: data_provider_get_ident_ds_names "
678 "failed with status %i\n", status);
681 } /* }}} int ident_data_to_json */
682 /* }}} ident_data_to_json */
684 int ident_describe (const graph_ident_t *ident, /* {{{ */
685 const graph_ident_t *selector,
686 char *buffer, size_t buffer_size)
688 if ((ident == NULL) || (selector == NULL)
689 || (buffer == NULL) || (buffer_size < 2))
694 #define CHECK_FIELD(field) do { \
695 if (strcasecmp (selector->field, ident->field) != 0) \
697 if (buffer[0] != 0) \
698 strlcat (buffer, "/", buffer_size); \
699 strlcat (buffer, ident->field, buffer_size); \
704 CHECK_FIELD (plugin);
705 CHECK_FIELD (plugin_instance);
707 CHECK_FIELD (type_instance);
712 strlcat (buffer, "default", buffer_size);
715 } /* }}} int ident_describe */
717 time_t ident_get_mtime (const graph_ident_t *ident) /* {{{ */
726 file = ident_to_file (ident);
730 memset (&statbuf, 0, sizeof (statbuf));
731 status = stat (file, &statbuf);
734 fprintf (stderr, "ident_get_mtime: stat'ing file \"%s\" failed: %s\n",
735 file, strerror (errno));
740 return (statbuf.st_mtime);
741 } /* }}} time_t ident_get_mtime */
743 /* vim: set sw=2 sts=2 et fdm=marker : */