collectd.conf(5): Document the gRPC plugin.
[collectd.git] / src / memcachec.c
1 /**
2  * collectd - src/memcachec.c
3  * Copyright (C) 2009       Doug MacEachern
4  * Copyright (C) 2006-2009  Florian octo Forster
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; only version 2 of the License is applicable.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Doug MacEachern <Doug.MacEachern at hyperic.com>
21  *   Florian octo Forster <octo at collectd.org>
22  **/
23
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27 #include "configfile.h"
28 #include "utils_match.h"
29
30 #include <libmemcached/memcached.h>
31
32 /*
33  * Data types
34  */
35 struct web_match_s;
36 typedef struct web_match_s web_match_t;
37 struct web_match_s /* {{{ */
38 {
39   char *regex;
40   char *exclude_regex;
41   int dstype;
42   char *type;
43   char *instance;
44
45   cu_match_t *match;
46
47   web_match_t *next;
48 }; /* }}} */
49
50 struct web_page_s;
51 typedef struct web_page_s web_page_t;
52 struct web_page_s /* {{{ */
53 {
54   char *instance;
55
56   char *server;
57   char *key;
58
59   memcached_st *memc;
60   char *buffer;
61
62   web_match_t *matches;
63
64   web_page_t *next;
65 }; /* }}} */
66
67 /*
68  * Global variables;
69  */
70 static web_page_t *pages_g = NULL;
71
72 /*
73  * Private functions
74  */
75 static void cmc_web_match_free (web_match_t *wm) /* {{{ */
76 {
77   if (wm == NULL)
78     return;
79
80   sfree (wm->regex);
81   sfree (wm->type);
82   sfree (wm->instance);
83   match_destroy (wm->match);
84   cmc_web_match_free (wm->next);
85   sfree (wm);
86 } /* }}} void cmc_web_match_free */
87
88 static void cmc_web_page_free (web_page_t *wp) /* {{{ */
89 {
90   if (wp == NULL)
91     return;
92
93   if (wp->memc != NULL)
94     memcached_free(wp->memc);
95   wp->memc = NULL;
96
97   sfree (wp->instance);
98   sfree (wp->server);
99   sfree (wp->key);
100   sfree (wp->buffer);
101
102   cmc_web_match_free (wp->matches);
103   cmc_web_page_free (wp->next);
104   sfree (wp);
105 } /* }}} void cmc_web_page_free */
106
107 static int cmc_page_init_memc (web_page_t *wp) /* {{{ */
108 {
109   memcached_server_st *server;
110
111   wp->memc = memcached_create(NULL);
112   if (wp->memc == NULL)
113   {
114     ERROR ("memcachec plugin: memcached_create failed.");
115     return (-1);
116   }
117
118   server = memcached_servers_parse (wp->server);
119   memcached_server_push (wp->memc, server);
120   memcached_server_list_free (server);
121
122   return (0);
123 } /* }}} int cmc_page_init_memc */
124
125 static int cmc_config_add_string (const char *name, char **dest, /* {{{ */
126     oconfig_item_t *ci)
127 {
128   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
129   {
130     WARNING ("memcachec plugin: `%s' needs exactly one string argument.", name);
131     return (-1);
132   }
133
134   sfree (*dest);
135   *dest = strdup (ci->values[0].value.string);
136   if (*dest == NULL)
137     return (-1);
138
139   return (0);
140 } /* }}} int cmc_config_add_string */
141
142 static int cmc_config_add_match_dstype (int *dstype_ret, /* {{{ */
143     oconfig_item_t *ci)
144 {
145   int dstype;
146
147   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
148   {
149     WARNING ("memcachec plugin: `DSType' needs exactly one string argument.");
150     return (-1);
151   }
152
153   if (strncasecmp ("Gauge", ci->values[0].value.string,
154         strlen ("Gauge")) == 0)
155   {
156     dstype = UTILS_MATCH_DS_TYPE_GAUGE;
157     if (strcasecmp ("GaugeAverage", ci->values[0].value.string) == 0)
158       dstype |= UTILS_MATCH_CF_GAUGE_AVERAGE;
159     else if (strcasecmp ("GaugeMin", ci->values[0].value.string) == 0)
160       dstype |= UTILS_MATCH_CF_GAUGE_MIN;
161     else if (strcasecmp ("GaugeMax", ci->values[0].value.string) == 0)
162       dstype |= UTILS_MATCH_CF_GAUGE_MAX;
163     else if (strcasecmp ("GaugeLast", ci->values[0].value.string) == 0)
164       dstype |= UTILS_MATCH_CF_GAUGE_LAST;
165     else
166       dstype = 0;
167   }
168   else if (strncasecmp ("Counter", ci->values[0].value.string,
169         strlen ("Counter")) == 0)
170   {
171     dstype = UTILS_MATCH_DS_TYPE_COUNTER;
172     if (strcasecmp ("CounterSet", ci->values[0].value.string) == 0)
173       dstype |= UTILS_MATCH_CF_COUNTER_SET;
174     else if (strcasecmp ("CounterAdd", ci->values[0].value.string) == 0)
175       dstype |= UTILS_MATCH_CF_COUNTER_ADD;
176     else if (strcasecmp ("CounterInc", ci->values[0].value.string) == 0)
177       dstype |= UTILS_MATCH_CF_COUNTER_INC;
178     else
179       dstype = 0;
180   }
181   else
182   {
183     dstype = 0;
184   }
185
186   if (dstype == 0)
187   {
188     WARNING ("memcachec plugin: `%s' is not a valid argument to `DSType'.",
189         ci->values[0].value.string);
190     return (-1);
191   }
192
193   *dstype_ret = dstype;
194   return (0);
195 } /* }}} int cmc_config_add_match_dstype */
196
197 static int cmc_config_add_match (web_page_t *page, /* {{{ */
198     oconfig_item_t *ci)
199 {
200   web_match_t *match;
201   int status;
202   int i;
203
204   if (ci->values_num != 0)
205   {
206     WARNING ("memcachec plugin: Ignoring arguments for the `Match' block.");
207   }
208
209   match = calloc (1, sizeof (*match));
210   if (match == NULL)
211   {
212     ERROR ("memcachec plugin: calloc failed.");
213     return (-1);
214   }
215
216   status = 0;
217   for (i = 0; i < ci->children_num; i++)
218   {
219     oconfig_item_t *child = ci->children + i;
220
221     if (strcasecmp ("Regex", child->key) == 0)
222       status = cmc_config_add_string ("Regex", &match->regex, child);
223     else if (strcasecmp ("ExcludeRegex", child->key) == 0)
224       status = cmc_config_add_string ("ExcludeRegex", &match->exclude_regex, child);
225     else if (strcasecmp ("DSType", child->key) == 0)
226       status = cmc_config_add_match_dstype (&match->dstype, child);
227     else if (strcasecmp ("Type", child->key) == 0)
228       status = cmc_config_add_string ("Type", &match->type, child);
229     else if (strcasecmp ("Instance", child->key) == 0)
230       status = cmc_config_add_string ("Instance", &match->instance, child);
231     else
232     {
233       WARNING ("memcachec plugin: Option `%s' not allowed here.", child->key);
234       status = -1;
235     }
236
237     if (status != 0)
238       break;
239   } /* for (i = 0; i < ci->children_num; i++) */
240
241   while (status == 0)
242   {
243     if (match->regex == NULL)
244     {
245       WARNING ("memcachec plugin: `Regex' missing in `Match' block.");
246       status = -1;
247     }
248
249     if (match->type == NULL)
250     {
251       WARNING ("memcachec plugin: `Type' missing in `Match' block.");
252       status = -1;
253     }
254
255     if (match->dstype == 0)
256     {
257       WARNING ("memcachec plugin: `DSType' missing in `Match' block.");
258       status = -1;
259     }
260
261     break;
262   } /* while (status == 0) */
263
264   if (status != 0)
265   {
266     cmc_web_match_free (match);
267     return (status);
268   }
269
270   match->match = match_create_simple (match->regex, match->exclude_regex,
271       match->dstype);
272   if (match->match == NULL)
273   {
274     ERROR ("memcachec plugin: tail_match_add_match_simple failed.");
275     cmc_web_match_free (match);
276     return (-1);
277   }
278   else
279   {
280     web_match_t *prev;
281
282     prev = page->matches;
283     while ((prev != NULL) && (prev->next != NULL))
284       prev = prev->next;
285
286     if (prev == NULL)
287       page->matches = match;
288     else
289       prev->next = match;
290   }
291
292   return (0);
293 } /* }}} int cmc_config_add_match */
294
295 static int cmc_config_add_page (oconfig_item_t *ci) /* {{{ */
296 {
297   web_page_t *page;
298   int status;
299   int i;
300
301   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
302   {
303     WARNING ("memcachec plugin: `Page' blocks need exactly one string argument.");
304     return (-1);
305   }
306
307   page = calloc (1, sizeof (*page));
308   if (page == NULL)
309   {
310     ERROR ("memcachec plugin: calloc failed.");
311     return (-1);
312   }
313   page->server = NULL;
314   page->key = NULL;
315
316   page->instance = strdup (ci->values[0].value.string);
317   if (page->instance == NULL)
318   {
319     ERROR ("memcachec plugin: strdup failed.");
320     sfree (page);
321     return (-1);
322   }
323
324   /* Process all children */
325   status = 0;
326   for (i = 0; i < ci->children_num; i++)
327   {
328     oconfig_item_t *child = ci->children + i;
329
330     if (strcasecmp ("Server", child->key) == 0)
331       status = cmc_config_add_string ("Server", &page->server, child);
332     else if (strcasecmp ("Key", child->key) == 0)
333       status = cmc_config_add_string ("Key", &page->key, child);
334     else if (strcasecmp ("Match", child->key) == 0)
335       /* Be liberal with failing matches => don't set `status'. */
336       cmc_config_add_match (page, child);
337     else
338     {
339       WARNING ("memcachec plugin: Option `%s' not allowed here.", child->key);
340       status = -1;
341     }
342
343     if (status != 0)
344       break;
345   } /* for (i = 0; i < ci->children_num; i++) */
346
347   /* Additionial sanity checks and libmemcached initialization. */
348   while (status == 0)
349   {
350     if (page->server == NULL)
351     {
352       WARNING ("memcachec plugin: `Server' missing in `Page' block.");
353       status = -1;
354     }
355
356     if (page->key == NULL)
357     {
358       WARNING ("memcachec plugin: `Key' missing in `Page' block.");
359       status = -1;
360     }
361
362     if (page->matches == NULL)
363     {
364       assert (page->instance != NULL);
365       WARNING ("memcachec plugin: No (valid) `Match' block "
366           "within `Page' block `%s'.", page->instance);
367       status = -1;
368     }
369
370     if (status == 0)
371       status = cmc_page_init_memc (page);
372
373     break;
374   } /* while (status == 0) */
375
376   if (status != 0)
377   {
378     cmc_web_page_free (page);
379     return (status);
380   }
381
382   /* Add the new page to the linked list */
383   if (pages_g == NULL)
384     pages_g = page;
385   else
386   {
387     web_page_t *prev;
388
389     prev = pages_g;
390     while (prev->next != NULL)
391       prev = prev->next;
392     prev->next = page;
393   }
394
395   return (0);
396 } /* }}} int cmc_config_add_page */
397
398 static int cmc_config (oconfig_item_t *ci) /* {{{ */
399 {
400   int success;
401   int errors;
402   int status;
403   int i;
404
405   success = 0;
406   errors = 0;
407
408   for (i = 0; i < ci->children_num; i++)
409   {
410     oconfig_item_t *child = ci->children + i;
411
412     if (strcasecmp ("Page", child->key) == 0)
413     {
414       status = cmc_config_add_page (child);
415       if (status == 0)
416         success++;
417       else
418         errors++;
419     }
420     else
421     {
422       WARNING ("memcachec plugin: Option `%s' not allowed here.", child->key);
423       errors++;
424     }
425   }
426
427   if ((success == 0) && (errors > 0))
428   {
429     ERROR ("memcachec plugin: All statements failed.");
430     return (-1);
431   }
432
433   return (0);
434 } /* }}} int cmc_config */
435
436 static int cmc_init (void) /* {{{ */
437 {
438   if (pages_g == NULL)
439   {
440     INFO ("memcachec plugin: No pages have been defined.");
441     return (-1);
442   }
443   return (0);
444 } /* }}} int cmc_init */
445
446 static void cmc_submit (const web_page_t *wp, const web_match_t *wm, /* {{{ */
447     const cu_match_value_t *mv)
448 {
449   value_t values[1];
450   value_list_t vl = VALUE_LIST_INIT;
451
452   values[0] = mv->value;
453
454   vl.values = values;
455   vl.values_len = 1;
456   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
457   sstrncpy (vl.plugin, "memcachec", sizeof (vl.plugin));
458   sstrncpy (vl.plugin_instance, wp->instance, sizeof (vl.plugin_instance));
459   sstrncpy (vl.type, wm->type, sizeof (vl.type));
460   sstrncpy (vl.type_instance, wm->instance, sizeof (vl.type_instance));
461
462   plugin_dispatch_values (&vl);
463 } /* }}} void cmc_submit */
464
465 static int cmc_read_page (web_page_t *wp) /* {{{ */
466 {
467   web_match_t *wm;
468   memcached_return rc;
469   size_t string_length;
470   uint32_t flags;
471   int status;
472
473   if (wp->memc == NULL)
474     return (-1);
475
476   wp->buffer = memcached_get (wp->memc, wp->key, strlen (wp->key),
477                               &string_length, &flags, &rc);
478   if (rc != MEMCACHED_SUCCESS)
479   {
480     ERROR ("memcachec plugin: memcached_get failed: %s",
481         memcached_strerror (wp->memc, rc));
482     return (-1);
483   }
484
485   for (wm = wp->matches; wm != NULL; wm = wm->next)
486   {
487     cu_match_value_t *mv;
488
489     status = match_apply (wm->match, wp->buffer);
490     if (status != 0)
491     {
492       WARNING ("memcachec plugin: match_apply failed.");
493       continue;
494     }
495
496     mv = match_get_user_data (wm->match);
497     if (mv == NULL)
498     {
499       WARNING ("memcachec plugin: match_get_user_data returned NULL.");
500       continue;
501     }
502
503     cmc_submit (wp, wm, mv);
504     match_value_reset (mv);
505   } /* for (wm = wp->matches; wm != NULL; wm = wm->next) */
506
507   sfree (wp->buffer);
508
509   return (0);
510 } /* }}} int cmc_read_page */
511
512 static int cmc_read (void) /* {{{ */
513 {
514   web_page_t *wp;
515
516   for (wp = pages_g; wp != NULL; wp = wp->next)
517     cmc_read_page (wp);
518
519   return (0);
520 } /* }}} int cmc_read */
521
522 static int cmc_shutdown (void) /* {{{ */
523 {
524   cmc_web_page_free (pages_g);
525   pages_g = NULL;
526
527   return (0);
528 } /* }}} int cmc_shutdown */
529
530 void module_register (void)
531 {
532   plugin_register_complex_config ("memcachec", cmc_config);
533   plugin_register_init ("memcachec", cmc_init);
534   plugin_register_read ("memcachec", cmc_read);
535   plugin_register_shutdown ("memcachec", cmc_shutdown);
536 } /* void module_register */
537
538 /* vim: set sw=2 sts=2 et fdm=marker : */