4 #include <limits.h> /* PATH_MAX */
6 #include "graph_ident.h"
9 #define ANY_TOKEN "/any/"
10 #define ALL_TOKEN "/all/"
12 #define IS_ANY(str) (((str) != NULL) && (strcasecmp (ANY_TOKEN, (str)) == 0))
13 #define IS_ALL(str) (((str) != NULL) && (strcasecmp (ALL_TOKEN, (str)) == 0))
18 struct graph_ident_s /* {{{ */
22 char *plugin_instance;
25 }; /* }}} struct graph_ident_s */
30 static char *part_copy_with_selector (const char *selector, /* {{{ */
31 const char *part, unsigned int flags)
33 if ((selector == NULL) || (part == NULL))
36 if ((flags & IDENT_FLAG_REPLACE_ANY) && IS_ANY (part))
39 if ((flags & IDENT_FLAG_REPLACE_ALL) && IS_ALL (part))
42 /* Replace the ANY and ALL flags if requested and if the selecter actually
44 if ((flags & IDENT_FLAG_REPLACE_ANY) && IS_ANY (selector))
45 return (strdup (part));
47 if ((flags & IDENT_FLAG_REPLACE_ALL) && IS_ALL (selector))
48 return (strdup (part));
50 if (strcmp (selector, part) != 0)
53 /* Otherwise (no replacement), return a copy of the selector. */
54 return (strdup (selector));
55 } /* }}} char *part_copy_with_selector */
57 static _Bool part_matches (const char *selector, /* {{{ */
60 if ((selector == NULL) && (part == NULL))
63 if (selector == NULL) /* && (part != NULL) */
66 if (IS_ANY(selector) || IS_ALL(selector))
69 if (part == NULL) /* && (selector != NULL) */
72 if (strcmp (selector, part) == 0)
76 } /* }}} _Bool part_matches */
81 graph_ident_t *ident_create (const char *host, /* {{{ */
82 const char *plugin, const char *plugin_instance,
83 const char *type, const char *type_instance)
88 || (plugin == NULL) || (plugin_instance == NULL)
89 || (type == NULL) || (type_instance == NULL))
92 ret = malloc (sizeof (*ret));
95 memset (ret, 0, sizeof (*ret));
100 ret->plugin_instance = NULL;
102 ret->type_instance = NULL;
104 #define COPY_PART(p) do { \
105 ret->p = strdup (p); \
106 if (ret->p == NULL) \
109 free (ret->plugin); \
110 free (ret->plugin_instance); \
112 free (ret->type_instance); \
120 COPY_PART(plugin_instance);
122 COPY_PART(type_instance);
127 } /* }}} graph_ident_t *ident_create */
129 graph_ident_t *ident_clone (const graph_ident_t *ident) /* {{{ */
131 return (ident_create (ident->host,
132 ident->plugin, ident->plugin_instance,
133 ident->type, ident->type_instance));
134 } /* }}} graph_ident_t *ident_clone */
136 graph_ident_t *ident_copy_with_selector (const graph_ident_t *selector, /* {{{ */
137 const graph_ident_t *ident, unsigned int flags)
141 if ((selector == NULL) || (ident == NULL))
144 ret = malloc (sizeof (*ret));
147 memset (ret, 0, sizeof (*ret));
150 ret->plugin_instance = NULL;
152 ret->type_instance = NULL;
154 #define COPY_PART(p) do { \
155 ret->p = part_copy_with_selector (selector->p, ident->p, flags); \
156 if (ret->p == NULL) \
159 free (ret->plugin); \
160 free (ret->plugin_instance); \
162 free (ret->type_instance); \
169 COPY_PART (plugin_instance);
171 COPY_PART (type_instance);
176 } /* }}} graph_ident_t *ident_copy_with_selector */
178 void ident_destroy (graph_ident_t *ident) /* {{{ */
184 free (ident->plugin);
185 free (ident->plugin_instance);
187 free (ident->type_instance);
190 } /* }}} void ident_destroy */
192 const char *ident_get_host (graph_ident_t *ident) /* {{{ */
197 return (ident->host);
198 } /* }}} char *ident_get_host */
200 const char *ident_get_plugin (graph_ident_t *ident) /* {{{ */
205 return (ident->plugin);
206 } /* }}} char *ident_get_plugin */
208 const char *ident_get_plugin_instance (graph_ident_t *ident) /* {{{ */
213 return (ident->plugin_instance);
214 } /* }}} char *ident_get_plugin_instance */
216 const char *ident_get_type (graph_ident_t *ident) /* {{{ */
221 return (ident->type);
222 } /* }}} char *ident_get_type */
224 const char *ident_get_type_instance (graph_ident_t *ident) /* {{{ */
229 return (ident->type_instance);
230 } /* }}} char *ident_get_type_instance */
232 int ident_compare (const graph_ident_t *i0, /* {{{ */
233 const graph_ident_t *i1)
237 #define COMPARE_PART(p) do { \
238 status = strcmp (i0->p, i1->p); \
244 COMPARE_PART (plugin);
245 COMPARE_PART (plugin_instance);
247 COMPARE_PART (type_instance);
252 } /* }}} int ident_compare */
254 _Bool ident_matches (const graph_ident_t *selector, /* {{{ */
255 const graph_ident_t *ident)
257 if ((selector == NULL) && (ident == NULL))
259 else if (selector == NULL)
261 else if (ident == NULL)
264 if (!part_matches (selector->host, ident->host))
267 if (!part_matches (selector->plugin, ident->plugin))
270 if (!part_matches (selector->plugin_instance, ident->plugin_instance))
273 if (!part_matches (selector->type, ident->type))
276 if (!part_matches (selector->type_instance, ident->type_instance))
280 } /* }}} _Bool ident_matches */
282 char *ident_to_string (const graph_ident_t *ident) /* {{{ */
284 char buffer[PATH_MAX];
288 strlcat (buffer, ident->host, sizeof (buffer));
289 strlcat (buffer, "/", sizeof (buffer));
290 strlcat (buffer, ident->plugin, sizeof (buffer));
291 if (ident->plugin_instance[0] != 0)
293 strlcat (buffer, "-", sizeof (buffer));
294 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
296 strlcat (buffer, "/", sizeof (buffer));
297 strlcat (buffer, ident->type, sizeof (buffer));
298 if (ident->type_instance[0] != 0)
300 strlcat (buffer, "-", sizeof (buffer));
301 strlcat (buffer, ident->type_instance, sizeof (buffer));
304 return (strdup (buffer));
305 } /* }}} char *ident_to_string */
307 char *ident_to_file (const graph_ident_t *ident) /* {{{ */
309 char buffer[PATH_MAX];
313 strlcat (buffer, DATA_DIR, sizeof (buffer));
314 strlcat (buffer, "/", sizeof (buffer));
316 strlcat (buffer, ident->host, sizeof (buffer));
317 strlcat (buffer, "/", sizeof (buffer));
318 strlcat (buffer, ident->plugin, sizeof (buffer));
319 if (ident->plugin_instance[0] != 0)
321 strlcat (buffer, "-", sizeof (buffer));
322 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
324 strlcat (buffer, "/", sizeof (buffer));
325 strlcat (buffer, ident->type, sizeof (buffer));
326 if (ident->type_instance[0] != 0)
328 strlcat (buffer, "-", sizeof (buffer));
329 strlcat (buffer, ident->type_instance, sizeof (buffer));
332 strlcat (buffer, ".rrd", sizeof (buffer));
334 return (strdup (buffer));
335 } /* }}} char *ident_to_file */
337 char *ident_to_json (const graph_ident_t *ident) /* {{{ */
343 strlcat (buffer, "{\"host\":\"", sizeof (buffer));
344 strlcat (buffer, ident->host, sizeof (buffer));
345 strlcat (buffer, "\",\"plugin\":\"", sizeof (buffer));
346 strlcat (buffer, ident->plugin, sizeof (buffer));
347 strlcat (buffer, "\",\"plugin_instance\":\"", sizeof (buffer));
348 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
349 strlcat (buffer, "\",\"type\":\"", sizeof (buffer));
350 strlcat (buffer, ident->type, sizeof (buffer));
351 strlcat (buffer, "\",\"type_instance\":\"", sizeof (buffer));
352 strlcat (buffer, ident->type_instance, sizeof (buffer));
353 strlcat (buffer, "\"}", sizeof (buffer));
355 return (strdup (buffer));
356 } /* }}} char *ident_to_json */
358 /* vim: set sw=2 sts=2 et fdm=marker : */