9a52003c5a7dfd0816b1967cc1f5cf316e078c62
[collection4.git] / graph_ident.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <strings.h>
4 #include <limits.h> /* PATH_MAX */
5
6 #include "graph_ident.h"
7 #include "common.h"
8
9 #define ANY_TOKEN "/any/"
10 #define ALL_TOKEN "/all/"
11
12 #define IS_ANY(str) (((str) != NULL) && (strcasecmp (ANY_TOKEN, (str)) == 0))
13 #define IS_ALL(str) (((str) != NULL) && (strcasecmp (ALL_TOKEN, (str)) == 0))
14
15 /*
16  * Data types
17  */
18 struct graph_ident_s /* {{{ */
19 {
20   char *host;
21   char *plugin;
22   char *plugin_instance;
23   char *type;
24   char *type_instance;
25 }; /* }}} struct graph_ident_s */
26
27 /*
28  * Private functions
29  */
30 static char *part_copy_with_selector (const char *selector, /* {{{ */
31     const char *part, _Bool keep_all_selector)
32 {
33   if ((selector == NULL) || (part == NULL))
34     return (NULL);
35
36   if (IS_ANY (part))
37     return (NULL);
38
39   if (!keep_all_selector && IS_ALL (part))
40     return (NULL);
41
42   /* ANY in the graph selection => concrete value in the instance. */
43   if (IS_ANY (selector))
44     return (strdup (part));
45
46   if (IS_ALL (selector))
47   {
48     if (keep_all_selector)
49       return (strdup (ALL_TOKEN));
50     else
51       return (strdup (part));
52   }
53
54   if (strcmp (selector, part) != 0)
55     return (NULL);
56
57   return (strdup (selector));
58 } /* }}} char *part_copy_with_selector */
59
60 static _Bool part_matches (const char *selector, /* {{{ */
61     const char *part)
62 {
63   if ((selector == NULL) && (part == NULL))
64     return (1);
65
66   if (selector == NULL) /* && (part != NULL) */
67     return (0);
68
69   if (IS_ANY(selector) || IS_ALL(selector))
70     return (1);
71
72   if (part == NULL) /* && (selector != NULL) */
73     return (0);
74
75   if (strcmp (selector, part) == 0)
76     return (1);
77
78   return (0);
79 } /* }}} _Bool part_matches */
80
81 /*
82  * Public functions
83  */
84 graph_ident_t *ident_create (const char *host, /* {{{ */
85     const char *plugin, const char *plugin_instance,
86     const char *type, const char *type_instance)
87 {
88   graph_ident_t *ret;
89
90   if ((host == NULL)
91       || (plugin == NULL) || (plugin_instance == NULL)
92       || (type == NULL) || (type_instance == NULL))
93     return (NULL);
94
95   ret = malloc (sizeof (*ret));
96   if (ret == NULL)
97     return (NULL);
98   memset (ret, 0, sizeof (*ret));
99
100   ret->host = NULL;
101   ret->host = NULL;
102   ret->plugin = NULL;
103   ret->plugin_instance = NULL;
104   ret->type = NULL;
105   ret->type_instance = NULL;
106
107 #define COPY_PART(p) do {        \
108   ret->p = strdup (p);           \
109   if (ret->p == NULL)            \
110   {                              \
111     free (ret->host);            \
112     free (ret->plugin);          \
113     free (ret->plugin_instance); \
114     free (ret->type);            \
115     free (ret->type_instance);   \
116     free (ret);                  \
117     return (NULL);               \
118   }                              \
119 } while (0)
120
121   COPY_PART(host);
122   COPY_PART(plugin);
123   COPY_PART(plugin_instance);
124   COPY_PART(type);
125   COPY_PART(type_instance);
126
127 #undef COPY_PART
128
129   return (ret);
130 } /* }}} graph_ident_t *ident_create */
131
132 graph_ident_t *ident_clone (const graph_ident_t *ident)
133 {
134   return (ident_create (ident->host,
135         ident->plugin, ident->plugin_instance,
136         ident->type, ident->type_instance));
137 } /* }}} graph_ident_t *ident_clone */
138
139 graph_ident_t *ident_copy_with_selector (const graph_ident_t *selector, /* {{{ */
140     const graph_ident_t *ident, _Bool keep_all_selector)
141 {
142   graph_ident_t *ret;
143
144   if ((selector == NULL) || (ident == NULL))
145     return (NULL);
146
147   ret = malloc (sizeof (*ret));
148   if (ret == NULL)
149     return (NULL);
150   memset (ret, 0, sizeof (*ret));
151   ret->host = NULL;
152   ret->plugin = NULL;
153   ret->plugin_instance = NULL;
154   ret->type = NULL;
155   ret->type_instance = NULL;
156
157 #define COPY_PART(p) do {                                  \
158   ret->p = part_copy_with_selector (selector->p, ident->p, \
159       keep_all_selector);                                  \
160   if (ret->p == NULL)                                      \
161   {                                                        \
162     free (ret->host);                                      \
163     free (ret->plugin);                                    \
164     free (ret->plugin_instance);                           \
165     free (ret->type);                                      \
166     free (ret->type_instance);                             \
167     return (NULL);                                         \
168   }                                                        \
169 } while (0)
170
171   COPY_PART (host);
172   COPY_PART (plugin);
173   COPY_PART (plugin_instance);
174   COPY_PART (type);
175   COPY_PART (type_instance);
176
177 #undef COPY_PART
178
179   return (ret);
180 } /* }}} graph_ident_t *ident_copy_with_selector */
181
182 void ident_destroy (graph_ident_t *ident) /* {{{ */
183 {
184   if (ident == NULL)
185     return;
186
187   free (ident->host);
188   free (ident->plugin);
189   free (ident->plugin_instance);
190   free (ident->type);
191   free (ident->type_instance);
192
193   free (ident);
194 } /* }}} void ident_destroy */
195
196 const char *ident_get_host (graph_ident_t *ident) /* {{{ */
197 {
198   if (ident == NULL)
199     return (NULL);
200
201   return (ident->host);
202 } /* }}} char *ident_get_host */
203
204 const char *ident_get_plugin (graph_ident_t *ident) /* {{{ */
205 {
206   if (ident == NULL)
207     return (NULL);
208
209   return (ident->plugin);
210 } /* }}} char *ident_get_plugin */
211
212 const char *ident_get_plugin_instance (graph_ident_t *ident) /* {{{ */
213 {
214   if (ident == NULL)
215     return (NULL);
216
217   return (ident->plugin_instance);
218 } /* }}} char *ident_get_plugin_instance */
219
220 const char *ident_get_type (graph_ident_t *ident) /* {{{ */
221 {
222   if (ident == NULL)
223     return (NULL);
224
225   return (ident->type);
226 } /* }}} char *ident_get_type */
227
228 const char *ident_get_type_instance (graph_ident_t *ident) /* {{{ */
229 {
230   if (ident == NULL)
231     return (NULL);
232
233   return (ident->type_instance);
234 } /* }}} char *ident_get_type_instance */
235
236 int ident_compare (const graph_ident_t *i0, /* {{{ */
237     const graph_ident_t *i1)
238 {
239   int status;
240
241 #define COMPARE_PART(p) do {       \
242   status = strcmp (i0->p, i1->p);  \
243   if (status != 0)                 \
244     return (status);               \
245 } while (0)
246
247   COMPARE_PART (host);
248   COMPARE_PART (plugin);
249   COMPARE_PART (plugin_instance);
250   COMPARE_PART (type);
251   COMPARE_PART (type_instance);
252
253 #undef COMPARE_PART
254
255   return (0);
256 } /* }}} int ident_compare */
257
258 _Bool ident_matches (const graph_ident_t *selector, /* {{{ */
259     const graph_ident_t *ident)
260 {
261   if ((selector == NULL) && (ident == NULL))
262     return (0);
263   else if (selector == NULL)
264     return (-1);
265   else if (ident == NULL)
266     return (1);
267
268   if (!part_matches (selector->host, ident->host))
269     return (0);
270
271   if (!part_matches (selector->plugin, ident->plugin))
272     return (0);
273
274   if (!part_matches (selector->plugin_instance, ident->plugin_instance))
275     return (0);
276
277   if (!part_matches (selector->type, ident->type))
278     return (0);
279
280   if (!part_matches (selector->type_instance, ident->type_instance))
281     return (0);
282
283   return (1);
284 } /* }}} _Bool ident_matches */
285
286 char *ident_to_string (const graph_ident_t *ident) /* {{{ */
287 {
288   char buffer[PATH_MAX];
289
290   buffer[0] = 0;
291
292   strlcat (buffer, ident->host, sizeof (buffer));
293   strlcat (buffer, "/", sizeof (buffer));
294   strlcat (buffer, ident->plugin, sizeof (buffer));
295   if (ident->plugin_instance[0] != 0)
296   {
297     strlcat (buffer, "-", sizeof (buffer));
298     strlcat (buffer, ident->plugin_instance, sizeof (buffer));
299   }
300   strlcat (buffer, "/", sizeof (buffer));
301   strlcat (buffer, ident->type, sizeof (buffer));
302   if (ident->type_instance[0] != 0)
303   {
304     strlcat (buffer, "-", sizeof (buffer));
305     strlcat (buffer, ident->type_instance, sizeof (buffer));
306   }
307
308   return (strdup (buffer));
309 } /* }}} char *ident_to_string */
310
311 char *ident_to_file (const graph_ident_t *ident) /* {{{ */
312 {
313   char buffer[PATH_MAX];
314
315   buffer[0] = 0;
316
317   strlcat (buffer, DATA_DIR, sizeof (buffer));
318   strlcat (buffer, "/", sizeof (buffer));
319
320   strlcat (buffer, ident->host, sizeof (buffer));
321   strlcat (buffer, "/", sizeof (buffer));
322   strlcat (buffer, ident->plugin, sizeof (buffer));
323   if (ident->plugin_instance[0] != 0)
324   {
325     strlcat (buffer, "-", sizeof (buffer));
326     strlcat (buffer, ident->plugin_instance, sizeof (buffer));
327   }
328   strlcat (buffer, "/", sizeof (buffer));
329   strlcat (buffer, ident->type, sizeof (buffer));
330   if (ident->type_instance[0] != 0)
331   {
332     strlcat (buffer, "-", sizeof (buffer));
333     strlcat (buffer, ident->type_instance, sizeof (buffer));
334   }
335
336   strlcat (buffer, ".rrd", sizeof (buffer));
337
338   return (strdup (buffer));
339 } /* }}} char *ident_to_file */
340
341 char *ident_to_json (const graph_ident_t *ident) /* {{{ */
342 {
343   char buffer[4096];
344
345   buffer[0] = 0;
346
347   strlcat (buffer, "{\"host\":\"", sizeof (buffer));
348   strlcat (buffer, ident->host, sizeof (buffer));
349   strlcat (buffer, "\",\"plugin\":\"", sizeof (buffer));
350   strlcat (buffer, ident->plugin, sizeof (buffer));
351   strlcat (buffer, "\",\"plugin_instance\":\"", sizeof (buffer));
352   strlcat (buffer, ident->plugin_instance, sizeof (buffer));
353   strlcat (buffer, "\",\"type\":\"", sizeof (buffer));
354   strlcat (buffer, ident->type, sizeof (buffer));
355   strlcat (buffer, "\",\"type_instance\":\"", sizeof (buffer));
356   strlcat (buffer, ident->type_instance, sizeof (buffer));
357   strlcat (buffer, "\"}", sizeof (buffer));
358
359   return (strdup (buffer));
360 } /* }}} char *ident_to_json */
361
362 /* vim: set sw=2 sts=2 et fdm=marker : */
363