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, /* {{{ */
97 if ((selector == NULL) && (part == NULL))
100 if (selector == NULL) /* && (part != NULL) */
103 if (IS_ANY(selector) || IS_ALL(selector))
106 if (part == NULL) /* && (selector != NULL) */
109 if (strcmp (selector, part) == 0)
113 } /* }}} _Bool part_matches */
118 graph_ident_t *ident_create (const char *host, /* {{{ */
119 const char *plugin, const char *plugin_instance,
120 const char *type, const char *type_instance)
125 || (plugin == NULL) || (plugin_instance == NULL)
126 || (type == NULL) || (type_instance == NULL))
129 ret = malloc (sizeof (*ret));
132 memset (ret, 0, sizeof (*ret));
137 ret->plugin_instance = NULL;
139 ret->type_instance = NULL;
141 #define COPY_PART(p) do { \
142 ret->p = strdup (p); \
143 if (ret->p == NULL) \
146 free (ret->plugin); \
147 free (ret->plugin_instance); \
149 free (ret->type_instance); \
157 COPY_PART(plugin_instance);
159 COPY_PART(type_instance);
164 } /* }}} graph_ident_t *ident_create */
166 graph_ident_t *ident_clone (const graph_ident_t *ident) /* {{{ */
168 return (ident_create (ident->host,
169 ident->plugin, ident->plugin_instance,
170 ident->type, ident->type_instance));
171 } /* }}} graph_ident_t *ident_clone */
173 graph_ident_t *ident_copy_with_selector (const graph_ident_t *selector, /* {{{ */
174 const graph_ident_t *ident, unsigned int flags)
178 if ((selector == NULL) || (ident == NULL))
181 ret = malloc (sizeof (*ret));
184 memset (ret, 0, sizeof (*ret));
187 ret->plugin_instance = NULL;
189 ret->type_instance = NULL;
191 #define COPY_PART(p) do { \
192 ret->p = part_copy_with_selector (selector->p, ident->p, flags); \
193 if (ret->p == NULL) \
196 free (ret->plugin); \
197 free (ret->plugin_instance); \
199 free (ret->type_instance); \
206 COPY_PART (plugin_instance);
208 COPY_PART (type_instance);
213 } /* }}} graph_ident_t *ident_copy_with_selector */
215 void ident_destroy (graph_ident_t *ident) /* {{{ */
221 free (ident->plugin);
222 free (ident->plugin_instance);
224 free (ident->type_instance);
227 } /* }}} void ident_destroy */
229 /* ident_get_* methods {{{ */
230 const char *ident_get_host (const graph_ident_t *ident) /* {{{ */
235 return (ident->host);
236 } /* }}} char *ident_get_host */
238 const char *ident_get_plugin (const graph_ident_t *ident) /* {{{ */
243 return (ident->plugin);
244 } /* }}} char *ident_get_plugin */
246 const char *ident_get_plugin_instance (const graph_ident_t *ident) /* {{{ */
251 return (ident->plugin_instance);
252 } /* }}} char *ident_get_plugin_instance */
254 const char *ident_get_type (const graph_ident_t *ident) /* {{{ */
259 return (ident->type);
260 } /* }}} char *ident_get_type */
262 const char *ident_get_type_instance (const graph_ident_t *ident) /* {{{ */
267 return (ident->type_instance);
268 } /* }}} char *ident_get_type_instance */
270 const char *ident_get_field (const graph_ident_t *ident, /* {{{ */
271 graph_ident_field_t field)
273 if ((ident == NULL) || (field >= _GIF_LAST))
276 if (field == GIF_HOST)
277 return (ident->host);
278 else if (field == GIF_PLUGIN)
279 return (ident->plugin);
280 else if (field == GIF_PLUGIN_INSTANCE)
281 return (ident->plugin_instance);
282 else if (field == GIF_TYPE)
283 return (ident->type);
284 else if (field == GIF_TYPE_INSTANCE)
285 return (ident->type_instance);
287 return (NULL); /* never reached */
288 } /* }}} const char *ident_get_field */
289 /* }}} ident_get_* methods */
291 /* ident_set_* methods {{{ */
292 int ident_set_host (graph_ident_t *ident, const char *host) /* {{{ */
296 if ((ident == NULL) || (host == NULL))
307 } /* }}} int ident_set_host */
309 int ident_set_plugin (graph_ident_t *ident, const char *plugin) /* {{{ */
313 if ((ident == NULL) || (plugin == NULL))
316 tmp = strdup (plugin);
320 free (ident->plugin);
324 } /* }}} int ident_set_plugin */
326 int ident_set_plugin_instance (graph_ident_t *ident, const char *plugin_instance) /* {{{ */
330 if ((ident == NULL) || (plugin_instance == NULL))
333 tmp = strdup (plugin_instance);
337 free (ident->plugin_instance);
338 ident->plugin_instance = tmp;
341 } /* }}} int ident_set_plugin_instance */
343 int ident_set_type (graph_ident_t *ident, const char *type) /* {{{ */
347 if ((ident == NULL) || (type == NULL))
358 } /* }}} int ident_set_type */
360 int ident_set_type_instance (graph_ident_t *ident, const char *type_instance) /* {{{ */
364 if ((ident == NULL) || (type_instance == NULL))
367 tmp = strdup (type_instance);
371 free (ident->type_instance);
372 ident->type_instance = tmp;
375 } /* }}} int ident_set_type_instance */
377 /* }}} ident_set_* methods */
379 int ident_compare (const graph_ident_t *i0, /* {{{ */
380 const graph_ident_t *i1)
384 #define COMPARE_PART(p) do { \
385 status = strcmp (i0->p, i1->p); \
391 COMPARE_PART (plugin);
392 COMPARE_PART (plugin_instance);
394 COMPARE_PART (type_instance);
399 } /* }}} int ident_compare */
401 _Bool ident_matches (const graph_ident_t *selector, /* {{{ */
402 const graph_ident_t *ident)
404 if ((selector == NULL) || (ident == NULL))
407 if (!part_matches (selector->host, ident->host))
410 if (!part_matches (selector->plugin, ident->plugin))
413 if (!part_matches (selector->plugin_instance, ident->plugin_instance))
416 if (!part_matches (selector->type, ident->type))
419 if (!part_matches (selector->type_instance, ident->type_instance))
423 } /* }}} _Bool ident_matches */
425 char *ident_to_string (const graph_ident_t *ident) /* {{{ */
427 char buffer[PATH_MAX];
431 strlcat (buffer, ident->host, sizeof (buffer));
432 strlcat (buffer, "/", sizeof (buffer));
433 strlcat (buffer, ident->plugin, sizeof (buffer));
434 if (ident->plugin_instance[0] != 0)
436 strlcat (buffer, "-", sizeof (buffer));
437 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
439 strlcat (buffer, "/", sizeof (buffer));
440 strlcat (buffer, ident->type, sizeof (buffer));
441 if (ident->type_instance[0] != 0)
443 strlcat (buffer, "-", sizeof (buffer));
444 strlcat (buffer, ident->type_instance, sizeof (buffer));
447 return (strdup (buffer));
448 } /* }}} char *ident_to_string */
450 char *ident_to_file (const graph_ident_t *ident) /* {{{ */
452 char buffer[PATH_MAX];
456 strlcat (buffer, DATA_DIR, sizeof (buffer));
457 strlcat (buffer, "/", sizeof (buffer));
459 strlcat (buffer, ident->host, sizeof (buffer));
460 strlcat (buffer, "/", sizeof (buffer));
461 strlcat (buffer, ident->plugin, sizeof (buffer));
462 if (ident->plugin_instance[0] != 0)
464 strlcat (buffer, "-", sizeof (buffer));
465 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
467 strlcat (buffer, "/", sizeof (buffer));
468 strlcat (buffer, ident->type, sizeof (buffer));
469 if (ident->type_instance[0] != 0)
471 strlcat (buffer, "-", sizeof (buffer));
472 strlcat (buffer, ident->type_instance, sizeof (buffer));
475 strlcat (buffer, ".rrd", sizeof (buffer));
477 return (strdup (buffer));
478 } /* }}} char *ident_to_file */
480 #define ADD_FIELD(field) do { \
482 json_escape_copy (json, ident->field, sizeof (json)); \
483 strlcat (buffer, json, sizeof (buffer)); \
486 char *ident_to_json (const graph_ident_t *ident) /* {{{ */
492 strlcat (buffer, "{\"host\":\"", sizeof (buffer));
494 strlcat (buffer, "\",\"plugin\":\"", sizeof (buffer));
496 strlcat (buffer, "\",\"plugin_instance\":\"", sizeof (buffer));
497 ADD_FIELD (plugin_instance);
498 strlcat (buffer, "\",\"type\":\"", sizeof (buffer));
500 strlcat (buffer, "\",\"type_instance\":\"", sizeof (buffer));
501 ADD_FIELD (type_instance);
502 strlcat (buffer, "\"}", sizeof (buffer));
504 return (strdup (buffer));
505 } /* }}} char *ident_to_json */
509 time_t ident_get_mtime (const graph_ident_t *ident) /* {{{ */
518 file = ident_to_file (ident);
522 memset (&statbuf, 0, sizeof (statbuf));
523 status = stat (file, &statbuf);
526 fprintf (stderr, "ident_get_mtime: stat'ing file \"%s\" failed: %s\n",
527 file, strerror (errno));
532 return (statbuf.st_mtime);
533 } /* }}} time_t ident_get_mtime */
535 /* vim: set sw=2 sts=2 et fdm=marker : */