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, _Bool keep_all_selector)
33 if ((selector == NULL) || (part == NULL))
39 if (!keep_all_selector && IS_ALL (part))
42 /* ANY in the graph selection => concrete value in the instance. */
43 if (IS_ANY (selector))
44 return (strdup (part));
46 if (IS_ALL (selector))
48 if (keep_all_selector)
49 return (strdup (ALL_TOKEN));
51 return (strdup (part));
54 if (strcmp (selector, part) != 0)
57 return (strdup (selector));
58 } /* }}} char *part_copy_with_selector */
60 static _Bool part_matches (const char *selector, /* {{{ */
63 if ((selector == NULL) && (part == NULL))
66 if (selector == NULL) /* && (part != NULL) */
69 if (IS_ANY(selector) || IS_ALL(selector))
72 if (part == NULL) /* && (selector != NULL) */
75 if (strcmp (selector, part) == 0)
79 } /* }}} _Bool part_matches */
84 graph_ident_t *ident_create (const char *host, /* {{{ */
85 const char *plugin, const char *plugin_instance,
86 const char *type, const char *type_instance)
91 || (plugin == NULL) || (plugin_instance == NULL)
92 || (type == NULL) || (type_instance == NULL))
95 ret = malloc (sizeof (*ret));
98 memset (ret, 0, sizeof (*ret));
103 ret->plugin_instance = NULL;
105 ret->type_instance = NULL;
107 #define COPY_PART(p) do { \
108 ret->p = strdup (p); \
109 if (ret->p == NULL) \
112 free (ret->plugin); \
113 free (ret->plugin_instance); \
115 free (ret->type_instance); \
123 COPY_PART(plugin_instance);
125 COPY_PART(type_instance);
130 } /* }}} graph_ident_t *ident_create */
132 graph_ident_t *ident_clone (const graph_ident_t *ident)
134 return (ident_create (ident->host,
135 ident->plugin, ident->plugin_instance,
136 ident->type, ident->type_instance));
137 } /* }}} graph_ident_t *ident_clone */
139 graph_ident_t *ident_copy_with_selector (const graph_ident_t *selector, /* {{{ */
140 const graph_ident_t *ident, _Bool keep_all_selector)
144 if ((selector == NULL) || (ident == NULL))
147 ret = malloc (sizeof (*ret));
150 memset (ret, 0, sizeof (*ret));
153 ret->plugin_instance = NULL;
155 ret->type_instance = NULL;
157 #define COPY_PART(p) do { \
158 ret->p = part_copy_with_selector (selector->p, ident->p, \
159 keep_all_selector); \
160 if (ret->p == NULL) \
163 free (ret->plugin); \
164 free (ret->plugin_instance); \
166 free (ret->type_instance); \
173 COPY_PART (plugin_instance);
175 COPY_PART (type_instance);
180 } /* }}} graph_ident_t *ident_copy_with_selector */
182 void ident_destroy (graph_ident_t *ident) /* {{{ */
188 free (ident->plugin);
189 free (ident->plugin_instance);
191 free (ident->type_instance);
194 } /* }}} void ident_destroy */
196 const char *ident_get_host (graph_ident_t *ident) /* {{{ */
201 return (ident->host);
202 } /* }}} char *ident_get_host */
204 const char *ident_get_plugin (graph_ident_t *ident) /* {{{ */
209 return (ident->plugin);
210 } /* }}} char *ident_get_plugin */
212 const char *ident_get_plugin_instance (graph_ident_t *ident) /* {{{ */
217 return (ident->plugin_instance);
218 } /* }}} char *ident_get_plugin_instance */
220 const char *ident_get_type (graph_ident_t *ident) /* {{{ */
225 return (ident->type);
226 } /* }}} char *ident_get_type */
228 const char *ident_get_type_instance (graph_ident_t *ident) /* {{{ */
233 return (ident->type_instance);
234 } /* }}} char *ident_get_type_instance */
236 int ident_compare (const graph_ident_t *i0, /* {{{ */
237 const graph_ident_t *i1)
241 #define COMPARE_PART(p) do { \
242 status = strcmp (i0->p, i1->p); \
248 COMPARE_PART (plugin);
249 COMPARE_PART (plugin_instance);
251 COMPARE_PART (type_instance);
256 } /* }}} int ident_compare */
258 _Bool ident_matches (const graph_ident_t *selector, /* {{{ */
259 const graph_ident_t *ident)
261 if ((selector == NULL) && (ident == NULL))
263 else if (selector == NULL)
265 else if (ident == NULL)
268 if (!part_matches (selector->host, ident->host))
271 if (!part_matches (selector->plugin, ident->plugin))
274 if (!part_matches (selector->plugin_instance, ident->plugin_instance))
277 if (!part_matches (selector->type, ident->type))
280 if (!part_matches (selector->type_instance, ident->type_instance))
284 } /* }}} _Bool ident_matches */
286 char *ident_to_string (const graph_ident_t *ident) /* {{{ */
288 char buffer[PATH_MAX];
292 strlcat (buffer, ident->host, sizeof (buffer));
293 strlcat (buffer, "/", sizeof (buffer));
294 strlcat (buffer, ident->plugin, sizeof (buffer));
295 if (ident->plugin_instance[0] != 0)
297 strlcat (buffer, "-", sizeof (buffer));
298 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
300 strlcat (buffer, "/", sizeof (buffer));
301 strlcat (buffer, ident->type, sizeof (buffer));
302 if (ident->type_instance[0] != 0)
304 strlcat (buffer, "-", sizeof (buffer));
305 strlcat (buffer, ident->type_instance, sizeof (buffer));
308 return (strdup (buffer));
309 } /* }}} char *ident_to_string */
311 char *ident_to_file (const graph_ident_t *ident) /* {{{ */
313 char buffer[PATH_MAX];
317 strlcat (buffer, DATA_DIR, sizeof (buffer));
318 strlcat (buffer, "/", sizeof (buffer));
320 strlcat (buffer, ident->host, sizeof (buffer));
321 strlcat (buffer, "/", sizeof (buffer));
322 strlcat (buffer, ident->plugin, sizeof (buffer));
323 if (ident->plugin_instance[0] != 0)
325 strlcat (buffer, "-", sizeof (buffer));
326 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
328 strlcat (buffer, "/", sizeof (buffer));
329 strlcat (buffer, ident->type, sizeof (buffer));
330 if (ident->type_instance[0] != 0)
332 strlcat (buffer, "-", sizeof (buffer));
333 strlcat (buffer, ident->type_instance, sizeof (buffer));
336 strlcat (buffer, ".rrd", sizeof (buffer));
338 return (strdup (buffer));
339 } /* }}} char *ident_to_file */
341 char *ident_to_json (const graph_ident_t *ident) /* {{{ */
347 strlcat (buffer, "{\"host\":\"", sizeof (buffer));
348 strlcat (buffer, ident->host, sizeof (buffer));
349 strlcat (buffer, "\",\"plugin\":\"", sizeof (buffer));
350 strlcat (buffer, ident->plugin, sizeof (buffer));
351 strlcat (buffer, "\",\"plugin_instance\":\"", sizeof (buffer));
352 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
353 strlcat (buffer, "\",\"type\":\"", sizeof (buffer));
354 strlcat (buffer, ident->type, sizeof (buffer));
355 strlcat (buffer, "\",\"type_instance\":\"", sizeof (buffer));
356 strlcat (buffer, ident->type_instance, sizeof (buffer));
357 strlcat (buffer, "\"}", sizeof (buffer));
359 return (strdup (buffer));
360 } /* }}} char *ident_to_json */
362 /* vim: set sw=2 sts=2 et fdm=marker : */