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 (const graph_ident_t *ident) /* {{{ */
211 return (ident->host);
212 } /* }}} char *ident_get_host */
214 const char *ident_get_plugin (const graph_ident_t *ident) /* {{{ */
219 return (ident->plugin);
220 } /* }}} char *ident_get_plugin */
222 const char *ident_get_plugin_instance (const graph_ident_t *ident) /* {{{ */
227 return (ident->plugin_instance);
228 } /* }}} char *ident_get_plugin_instance */
230 const char *ident_get_type (const graph_ident_t *ident) /* {{{ */
235 return (ident->type);
236 } /* }}} char *ident_get_type */
238 const char *ident_get_type_instance (const graph_ident_t *ident) /* {{{ */
243 return (ident->type_instance);
244 } /* }}} char *ident_get_type_instance */
246 const char *ident_get_field (const graph_ident_t *ident, /* {{{ */
247 graph_ident_field_t field)
249 if ((ident == NULL) || (field >= _GIF_LAST))
252 if (field == GIF_HOST)
253 return (ident->host);
254 else if (field == GIF_PLUGIN)
255 return (ident->plugin);
256 else if (field == GIF_PLUGIN_INSTANCE)
257 return (ident->plugin_instance);
258 else if (field == GIF_TYPE)
259 return (ident->type);
260 else if (field == GIF_TYPE_INSTANCE)
261 return (ident->type_instance);
263 return (NULL); /* never reached */
264 } /* }}} const char *ident_get_field */
265 /* }}} ident_get_* methods */
267 /* ident_set_* methods {{{ */
268 int ident_set_host (graph_ident_t *ident, const char *host) /* {{{ */
272 if ((ident == NULL) || (host == NULL))
283 } /* }}} int ident_set_host */
285 int ident_set_plugin (graph_ident_t *ident, const char *plugin) /* {{{ */
289 if ((ident == NULL) || (plugin == NULL))
292 tmp = strdup (plugin);
296 free (ident->plugin);
300 } /* }}} int ident_set_plugin */
302 int ident_set_plugin_instance (graph_ident_t *ident, const char *plugin_instance) /* {{{ */
306 if ((ident == NULL) || (plugin_instance == NULL))
309 tmp = strdup (plugin_instance);
313 free (ident->plugin_instance);
314 ident->plugin_instance = tmp;
317 } /* }}} int ident_set_plugin_instance */
319 int ident_set_type (graph_ident_t *ident, const char *type) /* {{{ */
323 if ((ident == NULL) || (type == NULL))
334 } /* }}} int ident_set_type */
336 int ident_set_type_instance (graph_ident_t *ident, const char *type_instance) /* {{{ */
340 if ((ident == NULL) || (type_instance == NULL))
343 tmp = strdup (type_instance);
347 free (ident->type_instance);
348 ident->type_instance = tmp;
351 } /* }}} int ident_set_type_instance */
353 /* }}} ident_set_* methods */
355 int ident_compare (const graph_ident_t *i0, /* {{{ */
356 const graph_ident_t *i1)
360 #define COMPARE_PART(p) do { \
361 status = strcmp (i0->p, i1->p); \
367 COMPARE_PART (plugin);
368 COMPARE_PART (plugin_instance);
370 COMPARE_PART (type_instance);
375 } /* }}} int ident_compare */
377 _Bool ident_matches (const graph_ident_t *selector, /* {{{ */
378 const graph_ident_t *ident)
380 if ((selector == NULL) && (ident == NULL))
382 else if (selector == NULL)
384 else if (ident == NULL)
387 if (!part_matches (selector->host, ident->host))
390 if (!part_matches (selector->plugin, ident->plugin))
393 if (!part_matches (selector->plugin_instance, ident->plugin_instance))
396 if (!part_matches (selector->type, ident->type))
399 if (!part_matches (selector->type_instance, ident->type_instance))
403 } /* }}} _Bool ident_matches */
405 char *ident_to_string (const graph_ident_t *ident) /* {{{ */
407 char buffer[PATH_MAX];
411 strlcat (buffer, ident->host, sizeof (buffer));
412 strlcat (buffer, "/", sizeof (buffer));
413 strlcat (buffer, ident->plugin, sizeof (buffer));
414 if (ident->plugin_instance[0] != 0)
416 strlcat (buffer, "-", sizeof (buffer));
417 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
419 strlcat (buffer, "/", sizeof (buffer));
420 strlcat (buffer, ident->type, sizeof (buffer));
421 if (ident->type_instance[0] != 0)
423 strlcat (buffer, "-", sizeof (buffer));
424 strlcat (buffer, ident->type_instance, sizeof (buffer));
427 return (strdup (buffer));
428 } /* }}} char *ident_to_string */
430 char *ident_to_file (const graph_ident_t *ident) /* {{{ */
432 char buffer[PATH_MAX];
436 strlcat (buffer, DATA_DIR, sizeof (buffer));
437 strlcat (buffer, "/", sizeof (buffer));
439 strlcat (buffer, ident->host, sizeof (buffer));
440 strlcat (buffer, "/", sizeof (buffer));
441 strlcat (buffer, ident->plugin, sizeof (buffer));
442 if (ident->plugin_instance[0] != 0)
444 strlcat (buffer, "-", sizeof (buffer));
445 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
447 strlcat (buffer, "/", sizeof (buffer));
448 strlcat (buffer, ident->type, sizeof (buffer));
449 if (ident->type_instance[0] != 0)
451 strlcat (buffer, "-", sizeof (buffer));
452 strlcat (buffer, ident->type_instance, sizeof (buffer));
455 strlcat (buffer, ".rrd", sizeof (buffer));
457 return (strdup (buffer));
458 } /* }}} char *ident_to_file */
460 char *ident_to_json (const graph_ident_t *ident) /* {{{ */
466 strlcat (buffer, "{\"host\":\"", sizeof (buffer));
467 strlcat (buffer, ident->host, sizeof (buffer));
468 strlcat (buffer, "\",\"plugin\":\"", sizeof (buffer));
469 strlcat (buffer, ident->plugin, sizeof (buffer));
470 strlcat (buffer, "\",\"plugin_instance\":\"", sizeof (buffer));
471 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
472 strlcat (buffer, "\",\"type\":\"", sizeof (buffer));
473 strlcat (buffer, ident->type, sizeof (buffer));
474 strlcat (buffer, "\",\"type_instance\":\"", sizeof (buffer));
475 strlcat (buffer, ident->type_instance, sizeof (buffer));
476 strlcat (buffer, "\"}", sizeof (buffer));
478 return (strdup (buffer));
479 } /* }}} char *ident_to_json */
481 time_t ident_get_mtime (const graph_ident_t *ident) /* {{{ */
490 file = ident_to_file (ident);
494 memset (&statbuf, 0, sizeof (statbuf));
495 status = stat (file, &statbuf);
498 fprintf (stderr, "ident_get_mtime: stat'ing file \"%s\" failed: %s\n",
499 file, strerror (errno));
504 return (statbuf.st_mtime);
505 } /* }}} time_t ident_get_mtime */
507 /* vim: set sw=2 sts=2 et fdm=marker : */