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>
30 #include <limits.h> /* PATH_MAX */
31 #include <sys/types.h>
34 #include "graph_ident.h"
36 #include "filesystem.h"
39 #include <fcgi_stdio.h>
44 struct graph_ident_s /* {{{ */
48 char *plugin_instance;
51 }; /* }}} struct graph_ident_s */
56 static char *part_copy_with_selector (const char *selector, /* {{{ */
57 const char *part, unsigned int flags)
59 if ((selector == NULL) || (part == NULL))
62 if ((flags & IDENT_FLAG_REPLACE_ANY) && IS_ANY (part))
65 if ((flags & IDENT_FLAG_REPLACE_ALL) && IS_ALL (part))
68 /* Replace the ANY and ALL flags if requested and if the selecter actually
70 if (IS_ANY (selector))
72 if (flags & IDENT_FLAG_REPLACE_ANY)
73 return (strdup (part));
75 return (strdup (selector));
78 if (IS_ALL (selector))
80 if (flags & IDENT_FLAG_REPLACE_ALL)
81 return (strdup (part));
83 return (strdup (selector));
86 if (strcmp (selector, part) != 0)
89 /* Otherwise (no replacement), return a copy of the selector. */
90 return (strdup (selector));
91 } /* }}} char *part_copy_with_selector */
93 static _Bool part_matches (const char *selector, /* {{{ */
96 if ((selector == NULL) && (part == NULL))
99 if (selector == NULL) /* && (part != NULL) */
102 if (IS_ANY(selector) || IS_ALL(selector))
105 if (part == NULL) /* && (selector != NULL) */
108 if (strcmp (selector, part) == 0)
112 } /* }}} _Bool part_matches */
117 graph_ident_t *ident_create (const char *host, /* {{{ */
118 const char *plugin, const char *plugin_instance,
119 const char *type, const char *type_instance)
124 || (plugin == NULL) || (plugin_instance == NULL)
125 || (type == NULL) || (type_instance == NULL))
128 ret = malloc (sizeof (*ret));
131 memset (ret, 0, sizeof (*ret));
136 ret->plugin_instance = NULL;
138 ret->type_instance = NULL;
140 #define COPY_PART(p) do { \
141 ret->p = strdup (p); \
142 if (ret->p == NULL) \
145 free (ret->plugin); \
146 free (ret->plugin_instance); \
148 free (ret->type_instance); \
156 COPY_PART(plugin_instance);
158 COPY_PART(type_instance);
163 } /* }}} graph_ident_t *ident_create */
165 graph_ident_t *ident_clone (const graph_ident_t *ident) /* {{{ */
167 return (ident_create (ident->host,
168 ident->plugin, ident->plugin_instance,
169 ident->type, ident->type_instance));
170 } /* }}} graph_ident_t *ident_clone */
172 graph_ident_t *ident_copy_with_selector (const graph_ident_t *selector, /* {{{ */
173 const graph_ident_t *ident, unsigned int flags)
177 if ((selector == NULL) || (ident == NULL))
180 ret = malloc (sizeof (*ret));
183 memset (ret, 0, sizeof (*ret));
186 ret->plugin_instance = NULL;
188 ret->type_instance = NULL;
190 #define COPY_PART(p) do { \
191 ret->p = part_copy_with_selector (selector->p, ident->p, flags); \
192 if (ret->p == NULL) \
195 free (ret->plugin); \
196 free (ret->plugin_instance); \
198 free (ret->type_instance); \
205 COPY_PART (plugin_instance);
207 COPY_PART (type_instance);
212 } /* }}} graph_ident_t *ident_copy_with_selector */
214 void ident_destroy (graph_ident_t *ident) /* {{{ */
220 free (ident->plugin);
221 free (ident->plugin_instance);
223 free (ident->type_instance);
226 } /* }}} void ident_destroy */
228 /* ident_get_* methods {{{ */
229 const char *ident_get_host (const graph_ident_t *ident) /* {{{ */
234 return (ident->host);
235 } /* }}} char *ident_get_host */
237 const char *ident_get_plugin (const graph_ident_t *ident) /* {{{ */
242 return (ident->plugin);
243 } /* }}} char *ident_get_plugin */
245 const char *ident_get_plugin_instance (const graph_ident_t *ident) /* {{{ */
250 return (ident->plugin_instance);
251 } /* }}} char *ident_get_plugin_instance */
253 const char *ident_get_type (const graph_ident_t *ident) /* {{{ */
258 return (ident->type);
259 } /* }}} char *ident_get_type */
261 const char *ident_get_type_instance (const graph_ident_t *ident) /* {{{ */
266 return (ident->type_instance);
267 } /* }}} char *ident_get_type_instance */
269 const char *ident_get_field (const graph_ident_t *ident, /* {{{ */
270 graph_ident_field_t field)
272 if ((ident == NULL) || (field >= _GIF_LAST))
275 if (field == GIF_HOST)
276 return (ident->host);
277 else if (field == GIF_PLUGIN)
278 return (ident->plugin);
279 else if (field == GIF_PLUGIN_INSTANCE)
280 return (ident->plugin_instance);
281 else if (field == GIF_TYPE)
282 return (ident->type);
283 else if (field == GIF_TYPE_INSTANCE)
284 return (ident->type_instance);
286 return (NULL); /* never reached */
287 } /* }}} const char *ident_get_field */
288 /* }}} ident_get_* methods */
290 /* ident_set_* methods {{{ */
291 int ident_set_host (graph_ident_t *ident, const char *host) /* {{{ */
295 if ((ident == NULL) || (host == NULL))
306 } /* }}} int ident_set_host */
308 int ident_set_plugin (graph_ident_t *ident, const char *plugin) /* {{{ */
312 if ((ident == NULL) || (plugin == NULL))
315 tmp = strdup (plugin);
319 free (ident->plugin);
323 } /* }}} int ident_set_plugin */
325 int ident_set_plugin_instance (graph_ident_t *ident, const char *plugin_instance) /* {{{ */
329 if ((ident == NULL) || (plugin_instance == NULL))
332 tmp = strdup (plugin_instance);
336 free (ident->plugin_instance);
337 ident->plugin_instance = tmp;
340 } /* }}} int ident_set_plugin_instance */
342 int ident_set_type (graph_ident_t *ident, const char *type) /* {{{ */
346 if ((ident == NULL) || (type == NULL))
357 } /* }}} int ident_set_type */
359 int ident_set_type_instance (graph_ident_t *ident, const char *type_instance) /* {{{ */
363 if ((ident == NULL) || (type_instance == NULL))
366 tmp = strdup (type_instance);
370 free (ident->type_instance);
371 ident->type_instance = tmp;
374 } /* }}} int ident_set_type_instance */
376 /* }}} ident_set_* methods */
378 int ident_compare (const graph_ident_t *i0, /* {{{ */
379 const graph_ident_t *i1)
383 #define COMPARE_PART(p) do { \
384 status = strcmp (i0->p, i1->p); \
390 COMPARE_PART (plugin);
391 COMPARE_PART (plugin_instance);
393 COMPARE_PART (type_instance);
398 } /* }}} int ident_compare */
400 _Bool ident_matches (const graph_ident_t *selector, /* {{{ */
401 const graph_ident_t *ident)
403 if ((selector == NULL) || (ident == NULL))
406 if (!part_matches (selector->host, ident->host))
409 if (!part_matches (selector->plugin, ident->plugin))
412 if (!part_matches (selector->plugin_instance, ident->plugin_instance))
415 if (!part_matches (selector->type, ident->type))
418 if (!part_matches (selector->type_instance, ident->type_instance))
422 } /* }}} _Bool ident_matches */
424 char *ident_to_string (const graph_ident_t *ident) /* {{{ */
426 char buffer[PATH_MAX];
430 strlcat (buffer, ident->host, sizeof (buffer));
431 strlcat (buffer, "/", sizeof (buffer));
432 strlcat (buffer, ident->plugin, sizeof (buffer));
433 if (ident->plugin_instance[0] != 0)
435 strlcat (buffer, "-", sizeof (buffer));
436 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
438 strlcat (buffer, "/", sizeof (buffer));
439 strlcat (buffer, ident->type, sizeof (buffer));
440 if (ident->type_instance[0] != 0)
442 strlcat (buffer, "-", sizeof (buffer));
443 strlcat (buffer, ident->type_instance, sizeof (buffer));
446 return (strdup (buffer));
447 } /* }}} char *ident_to_string */
449 char *ident_to_file (const graph_ident_t *ident) /* {{{ */
451 char buffer[PATH_MAX];
455 strlcat (buffer, DATA_DIR, sizeof (buffer));
456 strlcat (buffer, "/", sizeof (buffer));
458 strlcat (buffer, ident->host, sizeof (buffer));
459 strlcat (buffer, "/", sizeof (buffer));
460 strlcat (buffer, ident->plugin, sizeof (buffer));
461 if (ident->plugin_instance[0] != 0)
463 strlcat (buffer, "-", sizeof (buffer));
464 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
466 strlcat (buffer, "/", sizeof (buffer));
467 strlcat (buffer, ident->type, sizeof (buffer));
468 if (ident->type_instance[0] != 0)
470 strlcat (buffer, "-", sizeof (buffer));
471 strlcat (buffer, ident->type_instance, sizeof (buffer));
474 strlcat (buffer, ".rrd", sizeof (buffer));
476 return (strdup (buffer));
477 } /* }}} char *ident_to_file */
479 char *ident_to_json (const graph_ident_t *ident) /* {{{ */
485 strlcat (buffer, "{\"host\":\"", sizeof (buffer));
486 strlcat (buffer, ident->host, sizeof (buffer));
487 strlcat (buffer, "\",\"plugin\":\"", sizeof (buffer));
488 strlcat (buffer, ident->plugin, sizeof (buffer));
489 strlcat (buffer, "\",\"plugin_instance\":\"", sizeof (buffer));
490 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
491 strlcat (buffer, "\",\"type\":\"", sizeof (buffer));
492 strlcat (buffer, ident->type, sizeof (buffer));
493 strlcat (buffer, "\",\"type_instance\":\"", sizeof (buffer));
494 strlcat (buffer, ident->type_instance, sizeof (buffer));
495 strlcat (buffer, "\"}", sizeof (buffer));
497 return (strdup (buffer));
498 } /* }}} char *ident_to_json */
500 time_t ident_get_mtime (const graph_ident_t *ident) /* {{{ */
509 file = ident_to_file (ident);
513 memset (&statbuf, 0, sizeof (statbuf));
514 status = stat (file, &statbuf);
517 fprintf (stderr, "ident_get_mtime: stat'ing file \"%s\" failed: %s\n",
518 file, strerror (errno));
523 return (statbuf.st_mtime);
524 } /* }}} time_t ident_get_mtime */
526 /* vim: set sw=2 sts=2 et fdm=marker : */