b008a56dac3ae6da05fc3e5d0b2b44b141f826fa
[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 */
6
7 #include "graph_ident.h"
8 #include "common.h"
9 #include "filesystem.h"
10
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 */
22
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);
31
32   if ((flags & IDENT_FLAG_REPLACE_ANY) && IS_ANY (part))
33     return (NULL);
34
35   if ((flags & IDENT_FLAG_REPLACE_ALL) && IS_ALL (part))
36     return (NULL);
37
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   }
47
48   if (IS_ALL (selector))
49   {
50     if (flags & IDENT_FLAG_REPLACE_ALL)
51       return (strdup (part));
52     else
53       return (strdup (selector));
54   }
55
56   if (strcmp (selector, part) != 0)
57     return (NULL);
58
59   /* Otherwise (no replacement), return a copy of the selector. */
60   return (strdup (selector));
61 } /* }}} char *part_copy_with_selector */
62
63 static _Bool part_matches (const char *selector, /* {{{ */
64     const char *part)
65 {
66   if ((selector == NULL) && (part == NULL))
67     return (1);
68
69   if (selector == NULL) /* && (part != NULL) */
70     return (0);
71
72   if (IS_ANY(selector) || IS_ALL(selector))
73     return (1);
74
75   if (part == NULL) /* && (selector != NULL) */
76     return (0);
77
78   if (strcmp (selector, part) == 0)
79     return (1);
80
81   return (0);
82 } /* }}} _Bool part_matches */
83
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;
92
93   if ((host == NULL)
94       || (plugin == NULL) || (plugin_instance == NULL)
95       || (type == NULL) || (type_instance == NULL))
96     return (NULL);
97
98   ret = malloc (sizeof (*ret));
99   if (ret == NULL)
100     return (NULL);
101   memset (ret, 0, sizeof (*ret));
102
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;
109
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)
123
124   COPY_PART(host);
125   COPY_PART(plugin);
126   COPY_PART(plugin_instance);
127   COPY_PART(type);
128   COPY_PART(type_instance);
129
130 #undef COPY_PART
131
132   return (ret);
133 } /* }}} graph_ident_t *ident_create */
134
135 graph_ident_t *ident_clone (const graph_ident_t *ident) /* {{{ */
136 {
137   return (ident_create (ident->host,
138         ident->plugin, ident->plugin_instance,
139         ident->type, ident->type_instance));
140 } /* }}} graph_ident_t *ident_clone */
141
142 graph_ident_t *ident_copy_with_selector (const graph_ident_t *selector, /* {{{ */
143     const graph_ident_t *ident, unsigned int flags)
144 {
145   graph_ident_t *ret;
146
147   if ((selector == NULL) || (ident == NULL))
148     return (NULL);
149
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;
159
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)
172
173   COPY_PART (host);
174   COPY_PART (plugin);
175   COPY_PART (plugin_instance);
176   COPY_PART (type);
177   COPY_PART (type_instance);
178
179 #undef COPY_PART
180
181   return (ret);
182 } /* }}} graph_ident_t *ident_copy_with_selector */
183
184 void ident_destroy (graph_ident_t *ident) /* {{{ */
185 {
186   if (ident == NULL)
187     return;
188
189   free (ident->host);
190   free (ident->plugin);
191   free (ident->plugin_instance);
192   free (ident->type);
193   free (ident->type_instance);
194
195   free (ident);
196 } /* }}} void ident_destroy */
197
198 /* ident_get_* methods {{{ */
199 const char *ident_get_host (graph_ident_t *ident) /* {{{ */
200 {
201   if (ident == NULL)
202     return (NULL);
203
204   return (ident->host);
205 } /* }}} char *ident_get_host */
206
207 const char *ident_get_plugin (graph_ident_t *ident) /* {{{ */
208 {
209   if (ident == NULL)
210     return (NULL);
211
212   return (ident->plugin);
213 } /* }}} char *ident_get_plugin */
214
215 const char *ident_get_plugin_instance (graph_ident_t *ident) /* {{{ */
216 {
217   if (ident == NULL)
218     return (NULL);
219
220   return (ident->plugin_instance);
221 } /* }}} char *ident_get_plugin_instance */
222
223 const char *ident_get_type (graph_ident_t *ident) /* {{{ */
224 {
225   if (ident == NULL)
226     return (NULL);
227
228   return (ident->type);
229 } /* }}} char *ident_get_type */
230
231 const char *ident_get_type_instance (graph_ident_t *ident) /* {{{ */
232 {
233   if (ident == NULL)
234     return (NULL);
235
236   return (ident->type_instance);
237 } /* }}} char *ident_get_type_instance */
238 /* }}} ident_get_* methods */
239
240 /* ident_set_* methods {{{ */
241 int ident_set_host (graph_ident_t *ident, const char *host) /* {{{ */
242 {
243   char *tmp;
244
245   if ((ident == NULL) || (host == NULL))
246     return (EINVAL);
247
248   tmp = strdup (host);
249   if (tmp == NULL)
250     return (ENOMEM);
251
252   free (ident->host);
253   ident->host = tmp;
254
255   return (0);
256 } /* }}} int ident_set_host */
257
258 int ident_set_plugin (graph_ident_t *ident, const char *plugin) /* {{{ */
259 {
260   char *tmp;
261
262   if ((ident == NULL) || (plugin == NULL))
263     return (EINVAL);
264
265   tmp = strdup (plugin);
266   if (tmp == NULL)
267     return (ENOMEM);
268
269   free (ident->plugin);
270   ident->plugin = tmp;
271
272   return (0);
273 } /* }}} int ident_set_plugin */
274
275 int ident_set_plugin_instance (graph_ident_t *ident, const char *plugin_instance) /* {{{ */
276 {
277   char *tmp;
278
279   if ((ident == NULL) || (plugin_instance == NULL))
280     return (EINVAL);
281
282   tmp = strdup (plugin_instance);
283   if (tmp == NULL)
284     return (ENOMEM);
285
286   free (ident->plugin_instance);
287   ident->plugin_instance = tmp;
288
289   return (0);
290 } /* }}} int ident_set_plugin_instance */
291
292 int ident_set_type (graph_ident_t *ident, const char *type) /* {{{ */
293 {
294   char *tmp;
295
296   if ((ident == NULL) || (type == NULL))
297     return (EINVAL);
298
299   tmp = strdup (type);
300   if (tmp == NULL)
301     return (ENOMEM);
302
303   free (ident->type);
304   ident->type = tmp;
305
306   return (0);
307 } /* }}} int ident_set_type */
308
309 int ident_set_type_instance (graph_ident_t *ident, const char *type_instance) /* {{{ */
310 {
311   char *tmp;
312
313   if ((ident == NULL) || (type_instance == NULL))
314     return (EINVAL);
315
316   tmp = strdup (type_instance);
317   if (tmp == NULL)
318     return (ENOMEM);
319
320   free (ident->type_instance);
321   ident->type_instance = tmp;
322
323   return (0);
324 } /* }}} int ident_set_type_instance */
325
326 /* }}} ident_set_* methods */
327
328 int ident_compare (const graph_ident_t *i0, /* {{{ */
329     const graph_ident_t *i1)
330 {
331   int status;
332
333 #define COMPARE_PART(p) do {       \
334   status = strcmp (i0->p, i1->p);  \
335   if (status != 0)                 \
336     return (status);               \
337 } while (0)
338
339   COMPARE_PART (host);
340   COMPARE_PART (plugin);
341   COMPARE_PART (plugin_instance);
342   COMPARE_PART (type);
343   COMPARE_PART (type_instance);
344
345 #undef COMPARE_PART
346
347   return (0);
348 } /* }}} int ident_compare */
349
350 _Bool ident_matches (const graph_ident_t *selector, /* {{{ */
351     const graph_ident_t *ident)
352 {
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);
359
360   if (!part_matches (selector->host, ident->host))
361     return (0);
362
363   if (!part_matches (selector->plugin, ident->plugin))
364     return (0);
365
366   if (!part_matches (selector->plugin_instance, ident->plugin_instance))
367     return (0);
368
369   if (!part_matches (selector->type, ident->type))
370     return (0);
371
372   if (!part_matches (selector->type_instance, ident->type_instance))
373     return (0);
374
375   return (1);
376 } /* }}} _Bool ident_matches */
377
378 char *ident_to_string (const graph_ident_t *ident) /* {{{ */
379 {
380   char buffer[PATH_MAX];
381
382   buffer[0] = 0;
383
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   }
399
400   return (strdup (buffer));
401 } /* }}} char *ident_to_string */
402
403 char *ident_to_file (const graph_ident_t *ident) /* {{{ */
404 {
405   char buffer[PATH_MAX];
406
407   buffer[0] = 0;
408
409   strlcat (buffer, DATA_DIR, sizeof (buffer));
410   strlcat (buffer, "/", sizeof (buffer));
411
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   }
427
428   strlcat (buffer, ".rrd", sizeof (buffer));
429
430   return (strdup (buffer));
431 } /* }}} char *ident_to_file */
432
433 char *ident_to_json (const graph_ident_t *ident) /* {{{ */
434 {
435   char buffer[4096];
436
437   buffer[0] = 0;
438
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));
450
451   return (strdup (buffer));
452 } /* }}} char *ident_to_json */
453
454 /* vim: set sw=2 sts=2 et fdm=marker : */
455