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 (IS_ANY (selector))
46 if (flags & IDENT_FLAG_REPLACE_ANY)
47 return (strdup (part));
49 return (strdup (selector));
52 if (IS_ALL (selector))
54 if (flags & IDENT_FLAG_REPLACE_ALL)
55 return (strdup (part));
57 return (strdup (selector));
60 if (strcmp (selector, part) != 0)
63 /* Otherwise (no replacement), return a copy of the selector. */
64 return (strdup (selector));
65 } /* }}} char *part_copy_with_selector */
67 static _Bool part_matches (const char *selector, /* {{{ */
70 if ((selector == NULL) && (part == NULL))
73 if (selector == NULL) /* && (part != NULL) */
76 if (IS_ANY(selector) || IS_ALL(selector))
79 if (part == NULL) /* && (selector != NULL) */
82 if (strcmp (selector, part) == 0)
86 } /* }}} _Bool part_matches */
91 graph_ident_t *ident_create (const char *host, /* {{{ */
92 const char *plugin, const char *plugin_instance,
93 const char *type, const char *type_instance)
98 || (plugin == NULL) || (plugin_instance == NULL)
99 || (type == NULL) || (type_instance == NULL))
102 ret = malloc (sizeof (*ret));
105 memset (ret, 0, sizeof (*ret));
110 ret->plugin_instance = NULL;
112 ret->type_instance = NULL;
114 #define COPY_PART(p) do { \
115 ret->p = strdup (p); \
116 if (ret->p == NULL) \
119 free (ret->plugin); \
120 free (ret->plugin_instance); \
122 free (ret->type_instance); \
130 COPY_PART(plugin_instance);
132 COPY_PART(type_instance);
137 } /* }}} graph_ident_t *ident_create */
139 graph_ident_t *ident_clone (const graph_ident_t *ident) /* {{{ */
141 return (ident_create (ident->host,
142 ident->plugin, ident->plugin_instance,
143 ident->type, ident->type_instance));
144 } /* }}} graph_ident_t *ident_clone */
146 graph_ident_t *ident_copy_with_selector (const graph_ident_t *selector, /* {{{ */
147 const graph_ident_t *ident, unsigned int flags)
151 if ((selector == NULL) || (ident == NULL))
154 ret = malloc (sizeof (*ret));
157 memset (ret, 0, sizeof (*ret));
160 ret->plugin_instance = NULL;
162 ret->type_instance = NULL;
164 #define COPY_PART(p) do { \
165 ret->p = part_copy_with_selector (selector->p, ident->p, flags); \
166 if (ret->p == NULL) \
169 free (ret->plugin); \
170 free (ret->plugin_instance); \
172 free (ret->type_instance); \
179 COPY_PART (plugin_instance);
181 COPY_PART (type_instance);
186 } /* }}} graph_ident_t *ident_copy_with_selector */
188 void ident_destroy (graph_ident_t *ident) /* {{{ */
194 free (ident->plugin);
195 free (ident->plugin_instance);
197 free (ident->type_instance);
200 } /* }}} void ident_destroy */
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 */
242 int ident_compare (const graph_ident_t *i0, /* {{{ */
243 const graph_ident_t *i1)
247 #define COMPARE_PART(p) do { \
248 status = strcmp (i0->p, i1->p); \
254 COMPARE_PART (plugin);
255 COMPARE_PART (plugin_instance);
257 COMPARE_PART (type_instance);
262 } /* }}} int ident_compare */
264 _Bool ident_matches (const graph_ident_t *selector, /* {{{ */
265 const graph_ident_t *ident)
267 if ((selector == NULL) && (ident == NULL))
269 else if (selector == NULL)
271 else if (ident == NULL)
274 if (!part_matches (selector->host, ident->host))
277 if (!part_matches (selector->plugin, ident->plugin))
280 if (!part_matches (selector->plugin_instance, ident->plugin_instance))
283 if (!part_matches (selector->type, ident->type))
286 if (!part_matches (selector->type_instance, ident->type_instance))
290 } /* }}} _Bool ident_matches */
292 char *ident_to_string (const graph_ident_t *ident) /* {{{ */
294 char buffer[PATH_MAX];
298 strlcat (buffer, ident->host, sizeof (buffer));
299 strlcat (buffer, "/", sizeof (buffer));
300 strlcat (buffer, ident->plugin, sizeof (buffer));
301 if (ident->plugin_instance[0] != 0)
303 strlcat (buffer, "-", sizeof (buffer));
304 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
306 strlcat (buffer, "/", sizeof (buffer));
307 strlcat (buffer, ident->type, sizeof (buffer));
308 if (ident->type_instance[0] != 0)
310 strlcat (buffer, "-", sizeof (buffer));
311 strlcat (buffer, ident->type_instance, sizeof (buffer));
314 return (strdup (buffer));
315 } /* }}} char *ident_to_string */
317 char *ident_to_file (const graph_ident_t *ident) /* {{{ */
319 char buffer[PATH_MAX];
323 strlcat (buffer, DATA_DIR, sizeof (buffer));
324 strlcat (buffer, "/", sizeof (buffer));
326 strlcat (buffer, ident->host, sizeof (buffer));
327 strlcat (buffer, "/", sizeof (buffer));
328 strlcat (buffer, ident->plugin, sizeof (buffer));
329 if (ident->plugin_instance[0] != 0)
331 strlcat (buffer, "-", sizeof (buffer));
332 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
334 strlcat (buffer, "/", sizeof (buffer));
335 strlcat (buffer, ident->type, sizeof (buffer));
336 if (ident->type_instance[0] != 0)
338 strlcat (buffer, "-", sizeof (buffer));
339 strlcat (buffer, ident->type_instance, sizeof (buffer));
342 strlcat (buffer, ".rrd", sizeof (buffer));
344 return (strdup (buffer));
345 } /* }}} char *ident_to_file */
347 char *ident_to_json (const graph_ident_t *ident) /* {{{ */
353 strlcat (buffer, "{\"host\":\"", sizeof (buffer));
354 strlcat (buffer, ident->host, sizeof (buffer));
355 strlcat (buffer, "\",\"plugin\":\"", sizeof (buffer));
356 strlcat (buffer, ident->plugin, sizeof (buffer));
357 strlcat (buffer, "\",\"plugin_instance\":\"", sizeof (buffer));
358 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
359 strlcat (buffer, "\",\"type\":\"", sizeof (buffer));
360 strlcat (buffer, ident->type, sizeof (buffer));
361 strlcat (buffer, "\",\"type_instance\":\"", sizeof (buffer));
362 strlcat (buffer, ident->type_instance, sizeof (buffer));
363 strlcat (buffer, "\"}", sizeof (buffer));
365 return (strdup (buffer));
366 } /* }}} char *ident_to_json */
368 /* vim: set sw=2 sts=2 et fdm=marker : */