7 #include <limits.h> /* PATH_MAX */
11 #include "graph_ident.h"
13 #include "filesystem.h"
16 #include <fcgi_stdio.h>
21 struct graph_ident_s /* {{{ */
25 char *plugin_instance;
28 }; /* }}} struct graph_ident_s */
33 static char *part_copy_with_selector (const char *selector, /* {{{ */
34 const char *part, unsigned int flags)
36 if ((selector == NULL) || (part == NULL))
39 if ((flags & IDENT_FLAG_REPLACE_ANY) && IS_ANY (part))
42 if ((flags & IDENT_FLAG_REPLACE_ALL) && IS_ALL (part))
45 /* Replace the ANY and ALL flags if requested and if the selecter actually
47 if (IS_ANY (selector))
49 if (flags & IDENT_FLAG_REPLACE_ANY)
50 return (strdup (part));
52 return (strdup (selector));
55 if (IS_ALL (selector))
57 if (flags & IDENT_FLAG_REPLACE_ALL)
58 return (strdup (part));
60 return (strdup (selector));
63 if (strcmp (selector, part) != 0)
66 /* Otherwise (no replacement), return a copy of the selector. */
67 return (strdup (selector));
68 } /* }}} char *part_copy_with_selector */
70 static _Bool part_matches (const char *selector, /* {{{ */
73 if ((selector == NULL) && (part == NULL))
76 if (selector == NULL) /* && (part != NULL) */
79 if (IS_ANY(selector) || IS_ALL(selector))
82 if (part == NULL) /* && (selector != NULL) */
85 if (strcmp (selector, part) == 0)
89 } /* }}} _Bool part_matches */
94 graph_ident_t *ident_create (const char *host, /* {{{ */
95 const char *plugin, const char *plugin_instance,
96 const char *type, const char *type_instance)
101 || (plugin == NULL) || (plugin_instance == NULL)
102 || (type == NULL) || (type_instance == NULL))
105 ret = malloc (sizeof (*ret));
108 memset (ret, 0, sizeof (*ret));
113 ret->plugin_instance = NULL;
115 ret->type_instance = NULL;
117 #define COPY_PART(p) do { \
118 ret->p = strdup (p); \
119 if (ret->p == NULL) \
122 free (ret->plugin); \
123 free (ret->plugin_instance); \
125 free (ret->type_instance); \
133 COPY_PART(plugin_instance);
135 COPY_PART(type_instance);
140 } /* }}} graph_ident_t *ident_create */
142 graph_ident_t *ident_clone (const graph_ident_t *ident) /* {{{ */
144 return (ident_create (ident->host,
145 ident->plugin, ident->plugin_instance,
146 ident->type, ident->type_instance));
147 } /* }}} graph_ident_t *ident_clone */
149 graph_ident_t *ident_copy_with_selector (const graph_ident_t *selector, /* {{{ */
150 const graph_ident_t *ident, unsigned int flags)
154 if ((selector == NULL) || (ident == NULL))
157 ret = malloc (sizeof (*ret));
160 memset (ret, 0, sizeof (*ret));
163 ret->plugin_instance = NULL;
165 ret->type_instance = NULL;
167 #define COPY_PART(p) do { \
168 ret->p = part_copy_with_selector (selector->p, ident->p, flags); \
169 if (ret->p == NULL) \
172 free (ret->plugin); \
173 free (ret->plugin_instance); \
175 free (ret->type_instance); \
182 COPY_PART (plugin_instance);
184 COPY_PART (type_instance);
189 } /* }}} graph_ident_t *ident_copy_with_selector */
191 void ident_destroy (graph_ident_t *ident) /* {{{ */
197 free (ident->plugin);
198 free (ident->plugin_instance);
200 free (ident->type_instance);
203 } /* }}} void ident_destroy */
205 /* ident_get_* methods {{{ */
206 const char *ident_get_host (graph_ident_t *ident) /* {{{ */
211 return (ident->host);
212 } /* }}} char *ident_get_host */
214 const char *ident_get_plugin (graph_ident_t *ident) /* {{{ */
219 return (ident->plugin);
220 } /* }}} char *ident_get_plugin */
222 const char *ident_get_plugin_instance (graph_ident_t *ident) /* {{{ */
227 return (ident->plugin_instance);
228 } /* }}} char *ident_get_plugin_instance */
230 const char *ident_get_type (graph_ident_t *ident) /* {{{ */
235 return (ident->type);
236 } /* }}} char *ident_get_type */
238 const char *ident_get_type_instance (graph_ident_t *ident) /* {{{ */
243 return (ident->type_instance);
244 } /* }}} char *ident_get_type_instance */
245 /* }}} ident_get_* methods */
247 /* ident_set_* methods {{{ */
248 int ident_set_host (graph_ident_t *ident, const char *host) /* {{{ */
252 if ((ident == NULL) || (host == NULL))
263 } /* }}} int ident_set_host */
265 int ident_set_plugin (graph_ident_t *ident, const char *plugin) /* {{{ */
269 if ((ident == NULL) || (plugin == NULL))
272 tmp = strdup (plugin);
276 free (ident->plugin);
280 } /* }}} int ident_set_plugin */
282 int ident_set_plugin_instance (graph_ident_t *ident, const char *plugin_instance) /* {{{ */
286 if ((ident == NULL) || (plugin_instance == NULL))
289 tmp = strdup (plugin_instance);
293 free (ident->plugin_instance);
294 ident->plugin_instance = tmp;
297 } /* }}} int ident_set_plugin_instance */
299 int ident_set_type (graph_ident_t *ident, const char *type) /* {{{ */
303 if ((ident == NULL) || (type == NULL))
314 } /* }}} int ident_set_type */
316 int ident_set_type_instance (graph_ident_t *ident, const char *type_instance) /* {{{ */
320 if ((ident == NULL) || (type_instance == NULL))
323 tmp = strdup (type_instance);
327 free (ident->type_instance);
328 ident->type_instance = tmp;
331 } /* }}} int ident_set_type_instance */
333 /* }}} ident_set_* methods */
335 int ident_compare (const graph_ident_t *i0, /* {{{ */
336 const graph_ident_t *i1)
340 #define COMPARE_PART(p) do { \
341 status = strcmp (i0->p, i1->p); \
347 COMPARE_PART (plugin);
348 COMPARE_PART (plugin_instance);
350 COMPARE_PART (type_instance);
355 } /* }}} int ident_compare */
357 _Bool ident_matches (const graph_ident_t *selector, /* {{{ */
358 const graph_ident_t *ident)
360 if ((selector == NULL) && (ident == NULL))
362 else if (selector == NULL)
364 else if (ident == NULL)
367 if (!part_matches (selector->host, ident->host))
370 if (!part_matches (selector->plugin, ident->plugin))
373 if (!part_matches (selector->plugin_instance, ident->plugin_instance))
376 if (!part_matches (selector->type, ident->type))
379 if (!part_matches (selector->type_instance, ident->type_instance))
383 } /* }}} _Bool ident_matches */
385 char *ident_to_string (const graph_ident_t *ident) /* {{{ */
387 char buffer[PATH_MAX];
391 strlcat (buffer, ident->host, sizeof (buffer));
392 strlcat (buffer, "/", sizeof (buffer));
393 strlcat (buffer, ident->plugin, sizeof (buffer));
394 if (ident->plugin_instance[0] != 0)
396 strlcat (buffer, "-", sizeof (buffer));
397 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
399 strlcat (buffer, "/", sizeof (buffer));
400 strlcat (buffer, ident->type, sizeof (buffer));
401 if (ident->type_instance[0] != 0)
403 strlcat (buffer, "-", sizeof (buffer));
404 strlcat (buffer, ident->type_instance, sizeof (buffer));
407 return (strdup (buffer));
408 } /* }}} char *ident_to_string */
410 char *ident_to_file (const graph_ident_t *ident) /* {{{ */
412 char buffer[PATH_MAX];
416 strlcat (buffer, DATA_DIR, sizeof (buffer));
417 strlcat (buffer, "/", sizeof (buffer));
419 strlcat (buffer, ident->host, sizeof (buffer));
420 strlcat (buffer, "/", sizeof (buffer));
421 strlcat (buffer, ident->plugin, sizeof (buffer));
422 if (ident->plugin_instance[0] != 0)
424 strlcat (buffer, "-", sizeof (buffer));
425 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
427 strlcat (buffer, "/", sizeof (buffer));
428 strlcat (buffer, ident->type, sizeof (buffer));
429 if (ident->type_instance[0] != 0)
431 strlcat (buffer, "-", sizeof (buffer));
432 strlcat (buffer, ident->type_instance, sizeof (buffer));
435 strlcat (buffer, ".rrd", sizeof (buffer));
437 return (strdup (buffer));
438 } /* }}} char *ident_to_file */
440 char *ident_to_json (const graph_ident_t *ident) /* {{{ */
446 strlcat (buffer, "{\"host\":\"", sizeof (buffer));
447 strlcat (buffer, ident->host, sizeof (buffer));
448 strlcat (buffer, "\",\"plugin\":\"", sizeof (buffer));
449 strlcat (buffer, ident->plugin, sizeof (buffer));
450 strlcat (buffer, "\",\"plugin_instance\":\"", sizeof (buffer));
451 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
452 strlcat (buffer, "\",\"type\":\"", sizeof (buffer));
453 strlcat (buffer, ident->type, sizeof (buffer));
454 strlcat (buffer, "\",\"type_instance\":\"", sizeof (buffer));
455 strlcat (buffer, ident->type_instance, sizeof (buffer));
456 strlcat (buffer, "\"}", sizeof (buffer));
458 return (strdup (buffer));
459 } /* }}} char *ident_to_json */
461 time_t ident_get_mtime (const graph_ident_t *ident) /* {{{ */
470 file = ident_to_file (ident);
474 memset (&statbuf, 0, sizeof (statbuf));
475 status = stat (file, &statbuf);
478 fprintf (stderr, "ident_get_mtime: stat'ing file \"%s\" failed: %s\n",
479 file, strerror (errno));
484 return (statbuf.st_mtime);
485 } /* }}} time_t ident_get_mtime */
487 /* vim: set sw=2 sts=2 et fdm=marker : */