5 #include <limits.h> /* PATH_MAX */
7 #include "graph_ident.h"
9 #include "filesystem.h"
14 struct graph_ident_s /* {{{ */
18 char *plugin_instance;
21 }; /* }}} struct graph_ident_s */
26 static char *part_copy_with_selector (const char *selector, /* {{{ */
27 const char *part, unsigned int flags)
29 if ((selector == NULL) || (part == NULL))
32 if ((flags & IDENT_FLAG_REPLACE_ANY) && IS_ANY (part))
35 if ((flags & IDENT_FLAG_REPLACE_ALL) && IS_ALL (part))
38 /* Replace the ANY and ALL flags if requested and if the selecter actually
40 if (IS_ANY (selector))
42 if (flags & IDENT_FLAG_REPLACE_ANY)
43 return (strdup (part));
45 return (strdup (selector));
48 if (IS_ALL (selector))
50 if (flags & IDENT_FLAG_REPLACE_ALL)
51 return (strdup (part));
53 return (strdup (selector));
56 if (strcmp (selector, part) != 0)
59 /* Otherwise (no replacement), return a copy of the selector. */
60 return (strdup (selector));
61 } /* }}} char *part_copy_with_selector */
63 static _Bool part_matches (const char *selector, /* {{{ */
66 if ((selector == NULL) && (part == NULL))
69 if (selector == NULL) /* && (part != NULL) */
72 if (IS_ANY(selector) || IS_ALL(selector))
75 if (part == NULL) /* && (selector != NULL) */
78 if (strcmp (selector, part) == 0)
82 } /* }}} _Bool part_matches */
87 graph_ident_t *ident_create (const char *host, /* {{{ */
88 const char *plugin, const char *plugin_instance,
89 const char *type, const char *type_instance)
94 || (plugin == NULL) || (plugin_instance == NULL)
95 || (type == NULL) || (type_instance == NULL))
98 ret = malloc (sizeof (*ret));
101 memset (ret, 0, sizeof (*ret));
106 ret->plugin_instance = NULL;
108 ret->type_instance = NULL;
110 #define COPY_PART(p) do { \
111 ret->p = strdup (p); \
112 if (ret->p == NULL) \
115 free (ret->plugin); \
116 free (ret->plugin_instance); \
118 free (ret->type_instance); \
126 COPY_PART(plugin_instance);
128 COPY_PART(type_instance);
133 } /* }}} graph_ident_t *ident_create */
135 graph_ident_t *ident_clone (const graph_ident_t *ident) /* {{{ */
137 return (ident_create (ident->host,
138 ident->plugin, ident->plugin_instance,
139 ident->type, ident->type_instance));
140 } /* }}} graph_ident_t *ident_clone */
142 graph_ident_t *ident_copy_with_selector (const graph_ident_t *selector, /* {{{ */
143 const graph_ident_t *ident, unsigned int flags)
147 if ((selector == NULL) || (ident == NULL))
150 ret = malloc (sizeof (*ret));
153 memset (ret, 0, sizeof (*ret));
156 ret->plugin_instance = NULL;
158 ret->type_instance = NULL;
160 #define COPY_PART(p) do { \
161 ret->p = part_copy_with_selector (selector->p, ident->p, flags); \
162 if (ret->p == NULL) \
165 free (ret->plugin); \
166 free (ret->plugin_instance); \
168 free (ret->type_instance); \
175 COPY_PART (plugin_instance);
177 COPY_PART (type_instance);
182 } /* }}} graph_ident_t *ident_copy_with_selector */
184 void ident_destroy (graph_ident_t *ident) /* {{{ */
190 free (ident->plugin);
191 free (ident->plugin_instance);
193 free (ident->type_instance);
196 } /* }}} void ident_destroy */
198 /* ident_get_* methods {{{ */
199 const char *ident_get_host (graph_ident_t *ident) /* {{{ */
204 return (ident->host);
205 } /* }}} char *ident_get_host */
207 const char *ident_get_plugin (graph_ident_t *ident) /* {{{ */
212 return (ident->plugin);
213 } /* }}} char *ident_get_plugin */
215 const char *ident_get_plugin_instance (graph_ident_t *ident) /* {{{ */
220 return (ident->plugin_instance);
221 } /* }}} char *ident_get_plugin_instance */
223 const char *ident_get_type (graph_ident_t *ident) /* {{{ */
228 return (ident->type);
229 } /* }}} char *ident_get_type */
231 const char *ident_get_type_instance (graph_ident_t *ident) /* {{{ */
236 return (ident->type_instance);
237 } /* }}} char *ident_get_type_instance */
238 /* }}} ident_get_* methods */
240 /* ident_set_* methods {{{ */
241 int ident_set_host (graph_ident_t *ident, const char *host) /* {{{ */
245 if ((ident == NULL) || (host == NULL))
256 } /* }}} int ident_set_host */
258 int ident_set_plugin (graph_ident_t *ident, const char *plugin) /* {{{ */
262 if ((ident == NULL) || (plugin == NULL))
265 tmp = strdup (plugin);
269 free (ident->plugin);
273 } /* }}} int ident_set_plugin */
275 int ident_set_plugin_instance (graph_ident_t *ident, const char *plugin_instance) /* {{{ */
279 if ((ident == NULL) || (plugin_instance == NULL))
282 tmp = strdup (plugin_instance);
286 free (ident->plugin_instance);
287 ident->plugin_instance = tmp;
290 } /* }}} int ident_set_plugin_instance */
292 int ident_set_type (graph_ident_t *ident, const char *type) /* {{{ */
296 if ((ident == NULL) || (type == NULL))
307 } /* }}} int ident_set_type */
309 int ident_set_type_instance (graph_ident_t *ident, const char *type_instance) /* {{{ */
313 if ((ident == NULL) || (type_instance == NULL))
316 tmp = strdup (type_instance);
320 free (ident->type_instance);
321 ident->type_instance = tmp;
324 } /* }}} int ident_set_type_instance */
326 /* }}} ident_set_* methods */
328 int ident_compare (const graph_ident_t *i0, /* {{{ */
329 const graph_ident_t *i1)
333 #define COMPARE_PART(p) do { \
334 status = strcmp (i0->p, i1->p); \
340 COMPARE_PART (plugin);
341 COMPARE_PART (plugin_instance);
343 COMPARE_PART (type_instance);
348 } /* }}} int ident_compare */
350 _Bool ident_matches (const graph_ident_t *selector, /* {{{ */
351 const graph_ident_t *ident)
353 if ((selector == NULL) && (ident == NULL))
355 else if (selector == NULL)
357 else if (ident == NULL)
360 if (!part_matches (selector->host, ident->host))
363 if (!part_matches (selector->plugin, ident->plugin))
366 if (!part_matches (selector->plugin_instance, ident->plugin_instance))
369 if (!part_matches (selector->type, ident->type))
372 if (!part_matches (selector->type_instance, ident->type_instance))
376 } /* }}} _Bool ident_matches */
378 char *ident_to_string (const graph_ident_t *ident) /* {{{ */
380 char buffer[PATH_MAX];
384 strlcat (buffer, ident->host, sizeof (buffer));
385 strlcat (buffer, "/", sizeof (buffer));
386 strlcat (buffer, ident->plugin, sizeof (buffer));
387 if (ident->plugin_instance[0] != 0)
389 strlcat (buffer, "-", sizeof (buffer));
390 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
392 strlcat (buffer, "/", sizeof (buffer));
393 strlcat (buffer, ident->type, sizeof (buffer));
394 if (ident->type_instance[0] != 0)
396 strlcat (buffer, "-", sizeof (buffer));
397 strlcat (buffer, ident->type_instance, sizeof (buffer));
400 return (strdup (buffer));
401 } /* }}} char *ident_to_string */
403 char *ident_to_file (const graph_ident_t *ident) /* {{{ */
405 char buffer[PATH_MAX];
409 strlcat (buffer, DATA_DIR, sizeof (buffer));
410 strlcat (buffer, "/", sizeof (buffer));
412 strlcat (buffer, ident->host, sizeof (buffer));
413 strlcat (buffer, "/", sizeof (buffer));
414 strlcat (buffer, ident->plugin, sizeof (buffer));
415 if (ident->plugin_instance[0] != 0)
417 strlcat (buffer, "-", sizeof (buffer));
418 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
420 strlcat (buffer, "/", sizeof (buffer));
421 strlcat (buffer, ident->type, sizeof (buffer));
422 if (ident->type_instance[0] != 0)
424 strlcat (buffer, "-", sizeof (buffer));
425 strlcat (buffer, ident->type_instance, sizeof (buffer));
428 strlcat (buffer, ".rrd", sizeof (buffer));
430 return (strdup (buffer));
431 } /* }}} char *ident_to_file */
433 char *ident_to_json (const graph_ident_t *ident) /* {{{ */
439 strlcat (buffer, "{\"host\":\"", sizeof (buffer));
440 strlcat (buffer, ident->host, sizeof (buffer));
441 strlcat (buffer, "\",\"plugin\":\"", sizeof (buffer));
442 strlcat (buffer, ident->plugin, sizeof (buffer));
443 strlcat (buffer, "\",\"plugin_instance\":\"", sizeof (buffer));
444 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
445 strlcat (buffer, "\",\"type\":\"", sizeof (buffer));
446 strlcat (buffer, ident->type, sizeof (buffer));
447 strlcat (buffer, "\",\"type_instance\":\"", sizeof (buffer));
448 strlcat (buffer, ident->type_instance, sizeof (buffer));
449 strlcat (buffer, "\"}", sizeof (buffer));
451 return (strdup (buffer));
452 } /* }}} char *ident_to_json */
454 /* vim: set sw=2 sts=2 et fdm=marker : */