5 #include <limits.h> /* PATH_MAX */
7 #include "graph_ident.h"
10 #define IS_ANY(str) (((str) != NULL) && (strcasecmp (ANY_TOKEN, (str)) == 0))
11 #define IS_ALL(str) (((str) != NULL) && (strcasecmp (ALL_TOKEN, (str)) == 0))
16 struct graph_ident_s /* {{{ */
20 char *plugin_instance;
23 }; /* }}} struct graph_ident_s */
28 static char *part_copy_with_selector (const char *selector, /* {{{ */
29 const char *part, unsigned int flags)
31 if ((selector == NULL) || (part == NULL))
34 if ((flags & IDENT_FLAG_REPLACE_ANY) && IS_ANY (part))
37 if ((flags & IDENT_FLAG_REPLACE_ALL) && IS_ALL (part))
40 /* Replace the ANY and ALL flags if requested and if the selecter actually
42 if (IS_ANY (selector))
44 if (flags & IDENT_FLAG_REPLACE_ANY)
45 return (strdup (part));
47 return (strdup (selector));
50 if (IS_ALL (selector))
52 if (flags & IDENT_FLAG_REPLACE_ALL)
53 return (strdup (part));
55 return (strdup (selector));
58 if (strcmp (selector, part) != 0)
61 /* Otherwise (no replacement), return a copy of the selector. */
62 return (strdup (selector));
63 } /* }}} char *part_copy_with_selector */
65 static _Bool part_matches (const char *selector, /* {{{ */
68 if ((selector == NULL) && (part == NULL))
71 if (selector == NULL) /* && (part != NULL) */
74 if (IS_ANY(selector) || IS_ALL(selector))
77 if (part == NULL) /* && (selector != NULL) */
80 if (strcmp (selector, part) == 0)
84 } /* }}} _Bool part_matches */
89 graph_ident_t *ident_create (const char *host, /* {{{ */
90 const char *plugin, const char *plugin_instance,
91 const char *type, const char *type_instance)
96 || (plugin == NULL) || (plugin_instance == NULL)
97 || (type == NULL) || (type_instance == NULL))
100 ret = malloc (sizeof (*ret));
103 memset (ret, 0, sizeof (*ret));
108 ret->plugin_instance = NULL;
110 ret->type_instance = NULL;
112 #define COPY_PART(p) do { \
113 ret->p = strdup (p); \
114 if (ret->p == NULL) \
117 free (ret->plugin); \
118 free (ret->plugin_instance); \
120 free (ret->type_instance); \
128 COPY_PART(plugin_instance);
130 COPY_PART(type_instance);
135 } /* }}} graph_ident_t *ident_create */
137 graph_ident_t *ident_clone (const graph_ident_t *ident) /* {{{ */
139 return (ident_create (ident->host,
140 ident->plugin, ident->plugin_instance,
141 ident->type, ident->type_instance));
142 } /* }}} graph_ident_t *ident_clone */
144 graph_ident_t *ident_copy_with_selector (const graph_ident_t *selector, /* {{{ */
145 const graph_ident_t *ident, unsigned int flags)
149 if ((selector == NULL) || (ident == NULL))
152 ret = malloc (sizeof (*ret));
155 memset (ret, 0, sizeof (*ret));
158 ret->plugin_instance = NULL;
160 ret->type_instance = NULL;
162 #define COPY_PART(p) do { \
163 ret->p = part_copy_with_selector (selector->p, ident->p, flags); \
164 if (ret->p == NULL) \
167 free (ret->plugin); \
168 free (ret->plugin_instance); \
170 free (ret->type_instance); \
177 COPY_PART (plugin_instance);
179 COPY_PART (type_instance);
184 } /* }}} graph_ident_t *ident_copy_with_selector */
186 void ident_destroy (graph_ident_t *ident) /* {{{ */
192 free (ident->plugin);
193 free (ident->plugin_instance);
195 free (ident->type_instance);
198 } /* }}} void ident_destroy */
200 /* ident_get_* methods {{{ */
201 const char *ident_get_host (graph_ident_t *ident) /* {{{ */
206 return (ident->host);
207 } /* }}} char *ident_get_host */
209 const char *ident_get_plugin (graph_ident_t *ident) /* {{{ */
214 return (ident->plugin);
215 } /* }}} char *ident_get_plugin */
217 const char *ident_get_plugin_instance (graph_ident_t *ident) /* {{{ */
222 return (ident->plugin_instance);
223 } /* }}} char *ident_get_plugin_instance */
225 const char *ident_get_type (graph_ident_t *ident) /* {{{ */
230 return (ident->type);
231 } /* }}} char *ident_get_type */
233 const char *ident_get_type_instance (graph_ident_t *ident) /* {{{ */
238 return (ident->type_instance);
239 } /* }}} char *ident_get_type_instance */
240 /* }}} ident_get_* methods */
242 /* ident_set_* methods {{{ */
243 int ident_set_host (graph_ident_t *ident, const char *host) /* {{{ */
247 if ((ident == NULL) || (host == NULL))
258 } /* }}} int ident_set_host */
260 int ident_set_plugin (graph_ident_t *ident, const char *plugin) /* {{{ */
264 if ((ident == NULL) || (plugin == NULL))
267 tmp = strdup (plugin);
271 free (ident->plugin);
275 } /* }}} int ident_set_plugin */
277 int ident_set_plugin_instance (graph_ident_t *ident, const char *plugin_instance) /* {{{ */
281 if ((ident == NULL) || (plugin_instance == NULL))
284 tmp = strdup (plugin_instance);
288 free (ident->plugin_instance);
289 ident->plugin_instance = tmp;
292 } /* }}} int ident_set_plugin_instance */
294 int ident_set_type (graph_ident_t *ident, const char *type) /* {{{ */
298 if ((ident == NULL) || (type == NULL))
309 } /* }}} int ident_set_type */
311 int ident_set_type_instance (graph_ident_t *ident, const char *type_instance) /* {{{ */
315 if ((ident == NULL) || (type_instance == NULL))
318 tmp = strdup (type_instance);
322 free (ident->type_instance);
323 ident->type_instance = tmp;
326 } /* }}} int ident_set_type_instance */
328 /* }}} ident_set_* methods */
330 int ident_compare (const graph_ident_t *i0, /* {{{ */
331 const graph_ident_t *i1)
335 #define COMPARE_PART(p) do { \
336 status = strcmp (i0->p, i1->p); \
342 COMPARE_PART (plugin);
343 COMPARE_PART (plugin_instance);
345 COMPARE_PART (type_instance);
350 } /* }}} int ident_compare */
352 _Bool ident_matches (const graph_ident_t *selector, /* {{{ */
353 const graph_ident_t *ident)
355 if ((selector == NULL) && (ident == NULL))
357 else if (selector == NULL)
359 else if (ident == NULL)
362 if (!part_matches (selector->host, ident->host))
365 if (!part_matches (selector->plugin, ident->plugin))
368 if (!part_matches (selector->plugin_instance, ident->plugin_instance))
371 if (!part_matches (selector->type, ident->type))
374 if (!part_matches (selector->type_instance, ident->type_instance))
378 } /* }}} _Bool ident_matches */
380 char *ident_to_string (const graph_ident_t *ident) /* {{{ */
382 char buffer[PATH_MAX];
386 strlcat (buffer, ident->host, sizeof (buffer));
387 strlcat (buffer, "/", sizeof (buffer));
388 strlcat (buffer, ident->plugin, sizeof (buffer));
389 if (ident->plugin_instance[0] != 0)
391 strlcat (buffer, "-", sizeof (buffer));
392 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
394 strlcat (buffer, "/", sizeof (buffer));
395 strlcat (buffer, ident->type, sizeof (buffer));
396 if (ident->type_instance[0] != 0)
398 strlcat (buffer, "-", sizeof (buffer));
399 strlcat (buffer, ident->type_instance, sizeof (buffer));
402 return (strdup (buffer));
403 } /* }}} char *ident_to_string */
405 char *ident_to_file (const graph_ident_t *ident) /* {{{ */
407 char buffer[PATH_MAX];
411 strlcat (buffer, DATA_DIR, sizeof (buffer));
412 strlcat (buffer, "/", sizeof (buffer));
414 strlcat (buffer, ident->host, sizeof (buffer));
415 strlcat (buffer, "/", sizeof (buffer));
416 strlcat (buffer, ident->plugin, sizeof (buffer));
417 if (ident->plugin_instance[0] != 0)
419 strlcat (buffer, "-", sizeof (buffer));
420 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
422 strlcat (buffer, "/", sizeof (buffer));
423 strlcat (buffer, ident->type, sizeof (buffer));
424 if (ident->type_instance[0] != 0)
426 strlcat (buffer, "-", sizeof (buffer));
427 strlcat (buffer, ident->type_instance, sizeof (buffer));
430 strlcat (buffer, ".rrd", sizeof (buffer));
432 return (strdup (buffer));
433 } /* }}} char *ident_to_file */
435 char *ident_to_json (const graph_ident_t *ident) /* {{{ */
441 strlcat (buffer, "{\"host\":\"", sizeof (buffer));
442 strlcat (buffer, ident->host, sizeof (buffer));
443 strlcat (buffer, "\",\"plugin\":\"", sizeof (buffer));
444 strlcat (buffer, ident->plugin, sizeof (buffer));
445 strlcat (buffer, "\",\"plugin_instance\":\"", sizeof (buffer));
446 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
447 strlcat (buffer, "\",\"type\":\"", sizeof (buffer));
448 strlcat (buffer, ident->type, sizeof (buffer));
449 strlcat (buffer, "\",\"type_instance\":\"", sizeof (buffer));
450 strlcat (buffer, ident->type_instance, sizeof (buffer));
451 strlcat (buffer, "\"}", sizeof (buffer));
453 return (strdup (buffer));
454 } /* }}} char *ident_to_json */
456 /* vim: set sw=2 sts=2 et fdm=marker : */