5 #include <limits.h> /* PATH_MAX */
7 #include "graph_ident.h"
9 #include "filesystem.h"
11 #define IS_ANY(str) (((str) != NULL) && (strcasecmp (ANY_TOKEN, (str)) == 0))
12 #define IS_ALL(str) (((str) != NULL) && (strcasecmp (ALL_TOKEN, (str)) == 0))
17 struct graph_ident_s /* {{{ */
21 char *plugin_instance;
24 }; /* }}} struct graph_ident_s */
29 static char *part_copy_with_selector (const char *selector, /* {{{ */
30 const char *part, unsigned int flags)
32 if ((selector == NULL) || (part == NULL))
35 if ((flags & IDENT_FLAG_REPLACE_ANY) && IS_ANY (part))
38 if ((flags & IDENT_FLAG_REPLACE_ALL) && IS_ALL (part))
41 /* Replace the ANY and ALL flags if requested and if the selecter actually
43 if (IS_ANY (selector))
45 if (flags & IDENT_FLAG_REPLACE_ANY)
46 return (strdup (part));
48 return (strdup (selector));
51 if (IS_ALL (selector))
53 if (flags & IDENT_FLAG_REPLACE_ALL)
54 return (strdup (part));
56 return (strdup (selector));
59 if (strcmp (selector, part) != 0)
62 /* Otherwise (no replacement), return a copy of the selector. */
63 return (strdup (selector));
64 } /* }}} char *part_copy_with_selector */
66 static _Bool part_matches (const char *selector, /* {{{ */
69 if ((selector == NULL) && (part == NULL))
72 if (selector == NULL) /* && (part != NULL) */
75 if (IS_ANY(selector) || IS_ALL(selector))
78 if (part == NULL) /* && (selector != NULL) */
81 if (strcmp (selector, part) == 0)
85 } /* }}} _Bool part_matches */
90 graph_ident_t *ident_create (const char *host, /* {{{ */
91 const char *plugin, const char *plugin_instance,
92 const char *type, const char *type_instance)
97 || (plugin == NULL) || (plugin_instance == NULL)
98 || (type == NULL) || (type_instance == NULL))
101 ret = malloc (sizeof (*ret));
104 memset (ret, 0, sizeof (*ret));
109 ret->plugin_instance = NULL;
111 ret->type_instance = NULL;
113 #define COPY_PART(p) do { \
114 ret->p = strdup (p); \
115 if (ret->p == NULL) \
118 free (ret->plugin); \
119 free (ret->plugin_instance); \
121 free (ret->type_instance); \
129 COPY_PART(plugin_instance);
131 COPY_PART(type_instance);
136 } /* }}} graph_ident_t *ident_create */
138 graph_ident_t *ident_clone (const graph_ident_t *ident) /* {{{ */
140 return (ident_create (ident->host,
141 ident->plugin, ident->plugin_instance,
142 ident->type, ident->type_instance));
143 } /* }}} graph_ident_t *ident_clone */
145 graph_ident_t *ident_copy_with_selector (const graph_ident_t *selector, /* {{{ */
146 const graph_ident_t *ident, unsigned int flags)
150 if ((selector == NULL) || (ident == NULL))
153 ret = malloc (sizeof (*ret));
156 memset (ret, 0, sizeof (*ret));
159 ret->plugin_instance = NULL;
161 ret->type_instance = NULL;
163 #define COPY_PART(p) do { \
164 ret->p = part_copy_with_selector (selector->p, ident->p, flags); \
165 if (ret->p == NULL) \
168 free (ret->plugin); \
169 free (ret->plugin_instance); \
171 free (ret->type_instance); \
178 COPY_PART (plugin_instance);
180 COPY_PART (type_instance);
185 } /* }}} graph_ident_t *ident_copy_with_selector */
187 void ident_destroy (graph_ident_t *ident) /* {{{ */
193 free (ident->plugin);
194 free (ident->plugin_instance);
196 free (ident->type_instance);
199 } /* }}} void ident_destroy */
201 /* ident_get_* methods {{{ */
202 const char *ident_get_host (graph_ident_t *ident) /* {{{ */
207 return (ident->host);
208 } /* }}} char *ident_get_host */
210 const char *ident_get_plugin (graph_ident_t *ident) /* {{{ */
215 return (ident->plugin);
216 } /* }}} char *ident_get_plugin */
218 const char *ident_get_plugin_instance (graph_ident_t *ident) /* {{{ */
223 return (ident->plugin_instance);
224 } /* }}} char *ident_get_plugin_instance */
226 const char *ident_get_type (graph_ident_t *ident) /* {{{ */
231 return (ident->type);
232 } /* }}} char *ident_get_type */
234 const char *ident_get_type_instance (graph_ident_t *ident) /* {{{ */
239 return (ident->type_instance);
240 } /* }}} char *ident_get_type_instance */
241 /* }}} ident_get_* methods */
243 /* ident_set_* methods {{{ */
244 int ident_set_host (graph_ident_t *ident, const char *host) /* {{{ */
248 if ((ident == NULL) || (host == NULL))
259 } /* }}} int ident_set_host */
261 int ident_set_plugin (graph_ident_t *ident, const char *plugin) /* {{{ */
265 if ((ident == NULL) || (plugin == NULL))
268 tmp = strdup (plugin);
272 free (ident->plugin);
276 } /* }}} int ident_set_plugin */
278 int ident_set_plugin_instance (graph_ident_t *ident, const char *plugin_instance) /* {{{ */
282 if ((ident == NULL) || (plugin_instance == NULL))
285 tmp = strdup (plugin_instance);
289 free (ident->plugin_instance);
290 ident->plugin_instance = tmp;
293 } /* }}} int ident_set_plugin_instance */
295 int ident_set_type (graph_ident_t *ident, const char *type) /* {{{ */
299 if ((ident == NULL) || (type == NULL))
310 } /* }}} int ident_set_type */
312 int ident_set_type_instance (graph_ident_t *ident, const char *type_instance) /* {{{ */
316 if ((ident == NULL) || (type_instance == NULL))
319 tmp = strdup (type_instance);
323 free (ident->type_instance);
324 ident->type_instance = tmp;
327 } /* }}} int ident_set_type_instance */
329 /* }}} ident_set_* methods */
331 int ident_compare (const graph_ident_t *i0, /* {{{ */
332 const graph_ident_t *i1)
336 #define COMPARE_PART(p) do { \
337 status = strcmp (i0->p, i1->p); \
343 COMPARE_PART (plugin);
344 COMPARE_PART (plugin_instance);
346 COMPARE_PART (type_instance);
351 } /* }}} int ident_compare */
353 _Bool ident_matches (const graph_ident_t *selector, /* {{{ */
354 const graph_ident_t *ident)
356 if ((selector == NULL) && (ident == NULL))
358 else if (selector == NULL)
360 else if (ident == NULL)
363 if (!part_matches (selector->host, ident->host))
366 if (!part_matches (selector->plugin, ident->plugin))
369 if (!part_matches (selector->plugin_instance, ident->plugin_instance))
372 if (!part_matches (selector->type, ident->type))
375 if (!part_matches (selector->type_instance, ident->type_instance))
379 } /* }}} _Bool ident_matches */
381 char *ident_to_string (const graph_ident_t *ident) /* {{{ */
383 char buffer[PATH_MAX];
387 strlcat (buffer, ident->host, sizeof (buffer));
388 strlcat (buffer, "/", sizeof (buffer));
389 strlcat (buffer, ident->plugin, sizeof (buffer));
390 if (ident->plugin_instance[0] != 0)
392 strlcat (buffer, "-", sizeof (buffer));
393 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
395 strlcat (buffer, "/", sizeof (buffer));
396 strlcat (buffer, ident->type, sizeof (buffer));
397 if (ident->type_instance[0] != 0)
399 strlcat (buffer, "-", sizeof (buffer));
400 strlcat (buffer, ident->type_instance, sizeof (buffer));
403 return (strdup (buffer));
404 } /* }}} char *ident_to_string */
406 char *ident_to_file (const graph_ident_t *ident) /* {{{ */
408 char buffer[PATH_MAX];
412 strlcat (buffer, DATA_DIR, sizeof (buffer));
413 strlcat (buffer, "/", sizeof (buffer));
415 strlcat (buffer, ident->host, sizeof (buffer));
416 strlcat (buffer, "/", sizeof (buffer));
417 strlcat (buffer, ident->plugin, sizeof (buffer));
418 if (ident->plugin_instance[0] != 0)
420 strlcat (buffer, "-", sizeof (buffer));
421 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
423 strlcat (buffer, "/", sizeof (buffer));
424 strlcat (buffer, ident->type, sizeof (buffer));
425 if (ident->type_instance[0] != 0)
427 strlcat (buffer, "-", sizeof (buffer));
428 strlcat (buffer, ident->type_instance, sizeof (buffer));
431 strlcat (buffer, ".rrd", sizeof (buffer));
433 return (strdup (buffer));
434 } /* }}} char *ident_to_file */
436 char *ident_to_json (const graph_ident_t *ident) /* {{{ */
442 strlcat (buffer, "{\"host\":\"", sizeof (buffer));
443 strlcat (buffer, ident->host, sizeof (buffer));
444 strlcat (buffer, "\",\"plugin\":\"", sizeof (buffer));
445 strlcat (buffer, ident->plugin, sizeof (buffer));
446 strlcat (buffer, "\",\"plugin_instance\":\"", sizeof (buffer));
447 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
448 strlcat (buffer, "\",\"type\":\"", sizeof (buffer));
449 strlcat (buffer, ident->type, sizeof (buffer));
450 strlcat (buffer, "\",\"type_instance\":\"", sizeof (buffer));
451 strlcat (buffer, ident->type_instance, sizeof (buffer));
452 strlcat (buffer, "\"}", sizeof (buffer));
454 return (strdup (buffer));
455 } /* }}} char *ident_to_json */
457 /* vim: set sw=2 sts=2 et fdm=marker : */