Code

oconfig.c: Fix compiler warning.
[collection4.git] / graph_ident.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <strings.h>
4 #include <errno.h>
5 #include <limits.h> /* PATH_MAX */
7 #include "graph_ident.h"
8 #include "common.h"
9 #include "filesystem.h"
11 /*
12  * Data types
13  */
14 struct graph_ident_s /* {{{ */
15 {
16   char *host;
17   char *plugin;
18   char *plugin_instance;
19   char *type;
20   char *type_instance;
21 }; /* }}} struct graph_ident_s */
23 /*
24  * Private functions
25  */
26 static char *part_copy_with_selector (const char *selector, /* {{{ */
27     const char *part, unsigned int flags)
28 {
29   if ((selector == NULL) || (part == NULL))
30     return (NULL);
32   if ((flags & IDENT_FLAG_REPLACE_ANY) && IS_ANY (part))
33     return (NULL);
35   if ((flags & IDENT_FLAG_REPLACE_ALL) && IS_ALL (part))
36     return (NULL);
38   /* Replace the ANY and ALL flags if requested and if the selecter actually
39    * *is* that flag. */
40   if (IS_ANY (selector))
41   {
42     if (flags & IDENT_FLAG_REPLACE_ANY)
43       return (strdup (part));
44     else
45       return (strdup (selector));
46   }
48   if (IS_ALL (selector))
49   {
50     if (flags & IDENT_FLAG_REPLACE_ALL)
51       return (strdup (part));
52     else
53       return (strdup (selector));
54   }
56   if (strcmp (selector, part) != 0)
57     return (NULL);
59   /* Otherwise (no replacement), return a copy of the selector. */
60   return (strdup (selector));
61 } /* }}} char *part_copy_with_selector */
63 static _Bool part_matches (const char *selector, /* {{{ */
64     const char *part)
65 {
66   if ((selector == NULL) && (part == NULL))
67     return (1);
69   if (selector == NULL) /* && (part != NULL) */
70     return (0);
72   if (IS_ANY(selector) || IS_ALL(selector))
73     return (1);
75   if (part == NULL) /* && (selector != NULL) */
76     return (0);
78   if (strcmp (selector, part) == 0)
79     return (1);
81   return (0);
82 } /* }}} _Bool part_matches */
84 /*
85  * Public functions
86  */
87 graph_ident_t *ident_create (const char *host, /* {{{ */
88     const char *plugin, const char *plugin_instance,
89     const char *type, const char *type_instance)
90 {
91   graph_ident_t *ret;
93   if ((host == NULL)
94       || (plugin == NULL) || (plugin_instance == NULL)
95       || (type == NULL) || (type_instance == NULL))
96     return (NULL);
98   ret = malloc (sizeof (*ret));
99   if (ret == NULL)
100     return (NULL);
101   memset (ret, 0, sizeof (*ret));
103   ret->host = NULL;
104   ret->host = NULL;
105   ret->plugin = NULL;
106   ret->plugin_instance = NULL;
107   ret->type = NULL;
108   ret->type_instance = NULL;
110 #define COPY_PART(p) do {        \
111   ret->p = strdup (p);           \
112   if (ret->p == NULL)            \
113   {                              \
114     free (ret->host);            \
115     free (ret->plugin);          \
116     free (ret->plugin_instance); \
117     free (ret->type);            \
118     free (ret->type_instance);   \
119     free (ret);                  \
120     return (NULL);               \
121   }                              \
122 } while (0)
124   COPY_PART(host);
125   COPY_PART(plugin);
126   COPY_PART(plugin_instance);
127   COPY_PART(type);
128   COPY_PART(type_instance);
130 #undef COPY_PART
132   return (ret);
133 } /* }}} graph_ident_t *ident_create */
135 graph_ident_t *ident_clone (const graph_ident_t *ident) /* {{{ */
137   return (ident_create (ident->host,
138         ident->plugin, ident->plugin_instance,
139         ident->type, ident->type_instance));
140 } /* }}} graph_ident_t *ident_clone */
142 graph_ident_t *ident_copy_with_selector (const graph_ident_t *selector, /* {{{ */
143     const graph_ident_t *ident, unsigned int flags)
145   graph_ident_t *ret;
147   if ((selector == NULL) || (ident == NULL))
148     return (NULL);
150   ret = malloc (sizeof (*ret));
151   if (ret == NULL)
152     return (NULL);
153   memset (ret, 0, sizeof (*ret));
154   ret->host = NULL;
155   ret->plugin = NULL;
156   ret->plugin_instance = NULL;
157   ret->type = NULL;
158   ret->type_instance = NULL;
160 #define COPY_PART(p) do {                                  \
161   ret->p = part_copy_with_selector (selector->p, ident->p, flags); \
162   if (ret->p == NULL)                                      \
163   {                                                        \
164     free (ret->host);                                      \
165     free (ret->plugin);                                    \
166     free (ret->plugin_instance);                           \
167     free (ret->type);                                      \
168     free (ret->type_instance);                             \
169     return (NULL);                                         \
170   }                                                        \
171 } while (0)
173   COPY_PART (host);
174   COPY_PART (plugin);
175   COPY_PART (plugin_instance);
176   COPY_PART (type);
177   COPY_PART (type_instance);
179 #undef COPY_PART
181   return (ret);
182 } /* }}} graph_ident_t *ident_copy_with_selector */
184 void ident_destroy (graph_ident_t *ident) /* {{{ */
186   if (ident == NULL)
187     return;
189   free (ident->host);
190   free (ident->plugin);
191   free (ident->plugin_instance);
192   free (ident->type);
193   free (ident->type_instance);
195   free (ident);
196 } /* }}} void ident_destroy */
198 /* ident_get_* methods {{{ */
199 const char *ident_get_host (graph_ident_t *ident) /* {{{ */
201   if (ident == NULL)
202     return (NULL);
204   return (ident->host);
205 } /* }}} char *ident_get_host */
207 const char *ident_get_plugin (graph_ident_t *ident) /* {{{ */
209   if (ident == NULL)
210     return (NULL);
212   return (ident->plugin);
213 } /* }}} char *ident_get_plugin */
215 const char *ident_get_plugin_instance (graph_ident_t *ident) /* {{{ */
217   if (ident == NULL)
218     return (NULL);
220   return (ident->plugin_instance);
221 } /* }}} char *ident_get_plugin_instance */
223 const char *ident_get_type (graph_ident_t *ident) /* {{{ */
225   if (ident == NULL)
226     return (NULL);
228   return (ident->type);
229 } /* }}} char *ident_get_type */
231 const char *ident_get_type_instance (graph_ident_t *ident) /* {{{ */
233   if (ident == NULL)
234     return (NULL);
236   return (ident->type_instance);
237 } /* }}} char *ident_get_type_instance */
238 /* }}} ident_get_* methods */
240 /* ident_set_* methods {{{ */
241 int ident_set_host (graph_ident_t *ident, const char *host) /* {{{ */
243   char *tmp;
245   if ((ident == NULL) || (host == NULL))
246     return (EINVAL);
248   tmp = strdup (host);
249   if (tmp == NULL)
250     return (ENOMEM);
252   free (ident->host);
253   ident->host = tmp;
255   return (0);
256 } /* }}} int ident_set_host */
258 int ident_set_plugin (graph_ident_t *ident, const char *plugin) /* {{{ */
260   char *tmp;
262   if ((ident == NULL) || (plugin == NULL))
263     return (EINVAL);
265   tmp = strdup (plugin);
266   if (tmp == NULL)
267     return (ENOMEM);
269   free (ident->plugin);
270   ident->plugin = tmp;
272   return (0);
273 } /* }}} int ident_set_plugin */
275 int ident_set_plugin_instance (graph_ident_t *ident, const char *plugin_instance) /* {{{ */
277   char *tmp;
279   if ((ident == NULL) || (plugin_instance == NULL))
280     return (EINVAL);
282   tmp = strdup (plugin_instance);
283   if (tmp == NULL)
284     return (ENOMEM);
286   free (ident->plugin_instance);
287   ident->plugin_instance = tmp;
289   return (0);
290 } /* }}} int ident_set_plugin_instance */
292 int ident_set_type (graph_ident_t *ident, const char *type) /* {{{ */
294   char *tmp;
296   if ((ident == NULL) || (type == NULL))
297     return (EINVAL);
299   tmp = strdup (type);
300   if (tmp == NULL)
301     return (ENOMEM);
303   free (ident->type);
304   ident->type = tmp;
306   return (0);
307 } /* }}} int ident_set_type */
309 int ident_set_type_instance (graph_ident_t *ident, const char *type_instance) /* {{{ */
311   char *tmp;
313   if ((ident == NULL) || (type_instance == NULL))
314     return (EINVAL);
316   tmp = strdup (type_instance);
317   if (tmp == NULL)
318     return (ENOMEM);
320   free (ident->type_instance);
321   ident->type_instance = tmp;
323   return (0);
324 } /* }}} int ident_set_type_instance */
326 /* }}} ident_set_* methods */
328 int ident_compare (const graph_ident_t *i0, /* {{{ */
329     const graph_ident_t *i1)
331   int status;
333 #define COMPARE_PART(p) do {       \
334   status = strcmp (i0->p, i1->p);  \
335   if (status != 0)                 \
336     return (status);               \
337 } while (0)
339   COMPARE_PART (host);
340   COMPARE_PART (plugin);
341   COMPARE_PART (plugin_instance);
342   COMPARE_PART (type);
343   COMPARE_PART (type_instance);
345 #undef COMPARE_PART
347   return (0);
348 } /* }}} int ident_compare */
350 _Bool ident_matches (const graph_ident_t *selector, /* {{{ */
351     const graph_ident_t *ident)
353   if ((selector == NULL) && (ident == NULL))
354     return (0);
355   else if (selector == NULL)
356     return (-1);
357   else if (ident == NULL)
358     return (1);
360   if (!part_matches (selector->host, ident->host))
361     return (0);
363   if (!part_matches (selector->plugin, ident->plugin))
364     return (0);
366   if (!part_matches (selector->plugin_instance, ident->plugin_instance))
367     return (0);
369   if (!part_matches (selector->type, ident->type))
370     return (0);
372   if (!part_matches (selector->type_instance, ident->type_instance))
373     return (0);
375   return (1);
376 } /* }}} _Bool ident_matches */
378 char *ident_to_string (const graph_ident_t *ident) /* {{{ */
380   char buffer[PATH_MAX];
382   buffer[0] = 0;
384   strlcat (buffer, ident->host, sizeof (buffer));
385   strlcat (buffer, "/", sizeof (buffer));
386   strlcat (buffer, ident->plugin, sizeof (buffer));
387   if (ident->plugin_instance[0] != 0)
388   {
389     strlcat (buffer, "-", sizeof (buffer));
390     strlcat (buffer, ident->plugin_instance, sizeof (buffer));
391   }
392   strlcat (buffer, "/", sizeof (buffer));
393   strlcat (buffer, ident->type, sizeof (buffer));
394   if (ident->type_instance[0] != 0)
395   {
396     strlcat (buffer, "-", sizeof (buffer));
397     strlcat (buffer, ident->type_instance, sizeof (buffer));
398   }
400   return (strdup (buffer));
401 } /* }}} char *ident_to_string */
403 char *ident_to_file (const graph_ident_t *ident) /* {{{ */
405   char buffer[PATH_MAX];
407   buffer[0] = 0;
409   strlcat (buffer, DATA_DIR, sizeof (buffer));
410   strlcat (buffer, "/", sizeof (buffer));
412   strlcat (buffer, ident->host, sizeof (buffer));
413   strlcat (buffer, "/", sizeof (buffer));
414   strlcat (buffer, ident->plugin, sizeof (buffer));
415   if (ident->plugin_instance[0] != 0)
416   {
417     strlcat (buffer, "-", sizeof (buffer));
418     strlcat (buffer, ident->plugin_instance, sizeof (buffer));
419   }
420   strlcat (buffer, "/", sizeof (buffer));
421   strlcat (buffer, ident->type, sizeof (buffer));
422   if (ident->type_instance[0] != 0)
423   {
424     strlcat (buffer, "-", sizeof (buffer));
425     strlcat (buffer, ident->type_instance, sizeof (buffer));
426   }
428   strlcat (buffer, ".rrd", sizeof (buffer));
430   return (strdup (buffer));
431 } /* }}} char *ident_to_file */
433 char *ident_to_json (const graph_ident_t *ident) /* {{{ */
435   char buffer[4096];
437   buffer[0] = 0;
439   strlcat (buffer, "{\"host\":\"", sizeof (buffer));
440   strlcat (buffer, ident->host, sizeof (buffer));
441   strlcat (buffer, "\",\"plugin\":\"", sizeof (buffer));
442   strlcat (buffer, ident->plugin, sizeof (buffer));
443   strlcat (buffer, "\",\"plugin_instance\":\"", sizeof (buffer));
444   strlcat (buffer, ident->plugin_instance, sizeof (buffer));
445   strlcat (buffer, "\",\"type\":\"", sizeof (buffer));
446   strlcat (buffer, ident->type, sizeof (buffer));
447   strlcat (buffer, "\",\"type_instance\":\"", sizeof (buffer));
448   strlcat (buffer, ident->type_instance, sizeof (buffer));
449   strlcat (buffer, "\"}", sizeof (buffer));
451   return (strdup (buffer));
452 } /* }}} char *ident_to_json */
454 /* vim: set sw=2 sts=2 et fdm=marker : */