2 * collection4 - graph_ident.c
3 * Copyright (C) 2010 Florian octo Forster
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
21 * Florian octo Forster <ff at octo.it>
30 #include <limits.h> /* PATH_MAX */
31 #include <sys/types.h>
34 #include "graph_ident.h"
36 #include "filesystem.h"
37 #include "utils_cgi.h"
40 #include <fcgi_stdio.h>
45 struct graph_ident_s /* {{{ */
49 char *plugin_instance;
52 }; /* }}} struct graph_ident_s */
57 static char *part_copy_with_selector (const char *selector, /* {{{ */
58 const char *part, unsigned int flags)
60 if ((selector == NULL) || (part == NULL))
63 if ((flags & IDENT_FLAG_REPLACE_ANY) && IS_ANY (part))
66 if ((flags & IDENT_FLAG_REPLACE_ALL) && IS_ALL (part))
69 /* Replace the ANY and ALL flags if requested and if the selecter actually
71 if (IS_ANY (selector))
73 if (flags & IDENT_FLAG_REPLACE_ANY)
74 return (strdup (part));
76 return (strdup (selector));
79 if (IS_ALL (selector))
81 if (flags & IDENT_FLAG_REPLACE_ALL)
82 return (strdup (part));
84 return (strdup (selector));
87 if (strcmp (selector, part) != 0)
90 /* Otherwise (no replacement), return a copy of the selector. */
91 return (strdup (selector));
92 } /* }}} char *part_copy_with_selector */
94 static _Bool part_matches (const char *selector, /* {{{ */
98 if ((selector == NULL) && (part == NULL))
102 if (selector == NULL) /* && (part != NULL) */
105 if (IS_ANY(selector) || IS_ALL(selector))
108 if (part == NULL) /* && (selector != NULL) */
111 if (strcmp (selector, part) == 0)
115 } /* }}} _Bool part_matches */
120 graph_ident_t *ident_create (const char *host, /* {{{ */
121 const char *plugin, const char *plugin_instance,
122 const char *type, const char *type_instance)
127 || (plugin == NULL) || (plugin_instance == NULL)
128 || (type == NULL) || (type_instance == NULL))
131 ret = malloc (sizeof (*ret));
134 memset (ret, 0, sizeof (*ret));
139 ret->plugin_instance = NULL;
141 ret->type_instance = NULL;
143 #define COPY_PART(p) do { \
144 ret->p = strdup (p); \
145 if (ret->p == NULL) \
148 free (ret->plugin); \
149 free (ret->plugin_instance); \
151 free (ret->type_instance); \
159 COPY_PART(plugin_instance);
161 COPY_PART(type_instance);
166 } /* }}} graph_ident_t *ident_create */
168 graph_ident_t *ident_clone (const graph_ident_t *ident) /* {{{ */
170 return (ident_create (ident->host,
171 ident->plugin, ident->plugin_instance,
172 ident->type, ident->type_instance));
173 } /* }}} graph_ident_t *ident_clone */
175 graph_ident_t *ident_copy_with_selector (const graph_ident_t *selector, /* {{{ */
176 const graph_ident_t *ident, unsigned int flags)
180 if ((selector == NULL) || (ident == NULL))
183 ret = malloc (sizeof (*ret));
186 memset (ret, 0, sizeof (*ret));
189 ret->plugin_instance = NULL;
191 ret->type_instance = NULL;
193 #define COPY_PART(p) do { \
194 ret->p = part_copy_with_selector (selector->p, ident->p, flags); \
195 if (ret->p == NULL) \
198 free (ret->plugin); \
199 free (ret->plugin_instance); \
201 free (ret->type_instance); \
208 COPY_PART (plugin_instance);
210 COPY_PART (type_instance);
215 } /* }}} graph_ident_t *ident_copy_with_selector */
217 void ident_destroy (graph_ident_t *ident) /* {{{ */
223 free (ident->plugin);
224 free (ident->plugin_instance);
226 free (ident->type_instance);
229 } /* }}} void ident_destroy */
231 /* ident_get_* methods {{{ */
232 const char *ident_get_host (const graph_ident_t *ident) /* {{{ */
237 return (ident->host);
238 } /* }}} char *ident_get_host */
240 const char *ident_get_plugin (const graph_ident_t *ident) /* {{{ */
245 return (ident->plugin);
246 } /* }}} char *ident_get_plugin */
248 const char *ident_get_plugin_instance (const graph_ident_t *ident) /* {{{ */
253 return (ident->plugin_instance);
254 } /* }}} char *ident_get_plugin_instance */
256 const char *ident_get_type (const graph_ident_t *ident) /* {{{ */
261 return (ident->type);
262 } /* }}} char *ident_get_type */
264 const char *ident_get_type_instance (const graph_ident_t *ident) /* {{{ */
269 return (ident->type_instance);
270 } /* }}} char *ident_get_type_instance */
272 const char *ident_get_field (const graph_ident_t *ident, /* {{{ */
273 graph_ident_field_t field)
275 if ((ident == NULL) || (field >= _GIF_LAST))
278 if (field == GIF_HOST)
279 return (ident->host);
280 else if (field == GIF_PLUGIN)
281 return (ident->plugin);
282 else if (field == GIF_PLUGIN_INSTANCE)
283 return (ident->plugin_instance);
284 else if (field == GIF_TYPE)
285 return (ident->type);
286 else if (field == GIF_TYPE_INSTANCE)
287 return (ident->type_instance);
289 return (NULL); /* never reached */
290 } /* }}} const char *ident_get_field */
291 /* }}} ident_get_* methods */
293 /* ident_set_* methods {{{ */
294 int ident_set_host (graph_ident_t *ident, const char *host) /* {{{ */
298 if ((ident == NULL) || (host == NULL))
309 } /* }}} int ident_set_host */
311 int ident_set_plugin (graph_ident_t *ident, const char *plugin) /* {{{ */
315 if ((ident == NULL) || (plugin == NULL))
318 tmp = strdup (plugin);
322 free (ident->plugin);
326 } /* }}} int ident_set_plugin */
328 int ident_set_plugin_instance (graph_ident_t *ident, const char *plugin_instance) /* {{{ */
332 if ((ident == NULL) || (plugin_instance == NULL))
335 tmp = strdup (plugin_instance);
339 free (ident->plugin_instance);
340 ident->plugin_instance = tmp;
343 } /* }}} int ident_set_plugin_instance */
345 int ident_set_type (graph_ident_t *ident, const char *type) /* {{{ */
349 if ((ident == NULL) || (type == NULL))
360 } /* }}} int ident_set_type */
362 int ident_set_type_instance (graph_ident_t *ident, const char *type_instance) /* {{{ */
366 if ((ident == NULL) || (type_instance == NULL))
369 tmp = strdup (type_instance);
373 free (ident->type_instance);
374 ident->type_instance = tmp;
377 } /* }}} int ident_set_type_instance */
379 /* }}} ident_set_* methods */
381 int ident_compare (const graph_ident_t *i0, /* {{{ */
382 const graph_ident_t *i1)
386 #define COMPARE_PART(p) do { \
387 status = strcmp (i0->p, i1->p); \
393 COMPARE_PART (plugin);
394 COMPARE_PART (plugin_instance);
396 COMPARE_PART (type_instance);
401 } /* }}} int ident_compare */
403 _Bool ident_matches (const graph_ident_t *selector, /* {{{ */
404 const graph_ident_t *ident)
407 if ((selector == NULL) || (ident == NULL))
411 if (!part_matches (selector->host, ident->host))
414 if (!part_matches (selector->plugin, ident->plugin))
417 if (!part_matches (selector->plugin_instance, ident->plugin_instance))
420 if (!part_matches (selector->type, ident->type))
423 if (!part_matches (selector->type_instance, ident->type_instance))
427 } /* }}} _Bool ident_matches */
429 char *ident_to_string (const graph_ident_t *ident) /* {{{ */
431 char buffer[PATH_MAX];
435 strlcat (buffer, ident->host, sizeof (buffer));
436 strlcat (buffer, "/", sizeof (buffer));
437 strlcat (buffer, ident->plugin, sizeof (buffer));
438 if (ident->plugin_instance[0] != 0)
440 strlcat (buffer, "-", sizeof (buffer));
441 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
443 strlcat (buffer, "/", sizeof (buffer));
444 strlcat (buffer, ident->type, sizeof (buffer));
445 if (ident->type_instance[0] != 0)
447 strlcat (buffer, "-", sizeof (buffer));
448 strlcat (buffer, ident->type_instance, sizeof (buffer));
451 return (strdup (buffer));
452 } /* }}} char *ident_to_string */
454 char *ident_to_file (const graph_ident_t *ident) /* {{{ */
456 char buffer[PATH_MAX];
460 strlcat (buffer, DATA_DIR, sizeof (buffer));
461 strlcat (buffer, "/", sizeof (buffer));
463 strlcat (buffer, ident->host, sizeof (buffer));
464 strlcat (buffer, "/", sizeof (buffer));
465 strlcat (buffer, ident->plugin, sizeof (buffer));
466 if (ident->plugin_instance[0] != 0)
468 strlcat (buffer, "-", sizeof (buffer));
469 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
471 strlcat (buffer, "/", sizeof (buffer));
472 strlcat (buffer, ident->type, sizeof (buffer));
473 if (ident->type_instance[0] != 0)
475 strlcat (buffer, "-", sizeof (buffer));
476 strlcat (buffer, ident->type_instance, sizeof (buffer));
479 strlcat (buffer, ".rrd", sizeof (buffer));
481 return (strdup (buffer));
482 } /* }}} char *ident_to_file */
484 #define ADD_FIELD(field) do { \
486 json_escape_copy (json, ident->field, sizeof (json)); \
487 strlcat (buffer, json, sizeof (buffer)); \
490 char *ident_to_json (const graph_ident_t *ident) /* {{{ */
496 strlcat (buffer, "{\"host\":\"", sizeof (buffer));
498 strlcat (buffer, "\",\"plugin\":\"", sizeof (buffer));
500 strlcat (buffer, "\",\"plugin_instance\":\"", sizeof (buffer));
501 ADD_FIELD (plugin_instance);
502 strlcat (buffer, "\",\"type\":\"", sizeof (buffer));
504 strlcat (buffer, "\",\"type_instance\":\"", sizeof (buffer));
505 ADD_FIELD (type_instance);
506 strlcat (buffer, "\"}", sizeof (buffer));
508 return (strdup (buffer));
509 } /* }}} char *ident_to_json */
513 time_t ident_get_mtime (const graph_ident_t *ident) /* {{{ */
522 file = ident_to_file (ident);
526 memset (&statbuf, 0, sizeof (statbuf));
527 status = stat (file, &statbuf);
530 fprintf (stderr, "ident_get_mtime: stat'ing file \"%s\" failed: %s\n",
531 file, strerror (errno));
536 return (statbuf.st_mtime);
537 } /* }}} time_t ident_get_mtime */
539 /* vim: set sw=2 sts=2 et fdm=marker : */