Merge branch 'collectd-5.5'
[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
26 #include "common.h"
27 #include "plugin.h"
28 #include "configfile.h"
29 #include "utils_match.h"
30
31 #include <libmemcached/memcached.h>
32
33 /*
34  * Data types
35  */
36 struct web_match_s;
37 typedef struct web_match_s web_match_t;
38 struct web_match_s /* {{{ */
39 {
40   char *regex;
41   char *exclude_regex;
42   int dstype;
43   char *type;
44   char *instance;
45
46   cu_match_t *match;
47
48   web_match_t *next;
49 }; /* }}} */
50
51 struct web_page_s;
52 typedef struct web_page_s web_page_t;
53 struct web_page_s /* {{{ */
54 {
55   char *instance;
56
57   char *server;
58   char *key;
59
60   memcached_st *memc;
61   char *buffer;
62
63   web_match_t *matches;
64
65   web_page_t *next;
66 }; /* }}} */
67
68 /*
69  * Global variables;
70  */
71 static web_page_t *pages_g = NULL;
72
73 /*
74  * Private functions
75  */
76 static void cmc_web_match_free (web_match_t *wm) /* {{{ */
77 {
78   if (wm == NULL)
79     return;
80
81   sfree (wm->regex);
82   sfree (wm->type);
83   sfree (wm->instance);
84   match_destroy (wm->match);
85   cmc_web_match_free (wm->next);
86   sfree (wm);
87 } /* }}} void cmc_web_match_free */
88
89 static void cmc_web_page_free (web_page_t *wp) /* {{{ */
90 {
91   if (wp == NULL)
92     return;
93
94   if (wp->memc != NULL)
95     memcached_free(wp->memc);
96   wp->memc = NULL;
97
98   sfree (wp->instance);
99   sfree (wp->server);
100   sfree (wp->key);
101   sfree (wp->buffer);
102
103   cmc_web_match_free (wp->matches);
104   cmc_web_page_free (wp->next);
105   sfree (wp);
106 } /* }}} void cmc_web_page_free */
107
108 static int cmc_page_init_memc (web_page_t *wp) /* {{{ */
109 {
110   memcached_server_st *server;
111
112   wp->memc = memcached_create(NULL);
113   if (wp->memc == NULL)
114   {
115     ERROR ("memcachec plugin: memcached_create failed.");
116     return (-1);
117   }
118
119   server = memcached_servers_parse (wp->server);
120   memcached_server_push (wp->memc, server);
121   memcached_server_list_free (server);
122
123   return (0);
124 } /* }}} int cmc_page_init_memc */
125
126 static int cmc_config_add_string (const char *name, char **dest, /* {{{ */
127     oconfig_item_t *ci)
128 {
129   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
130   {
131     WARNING ("memcachec plugin: `%s' needs exactly one string argument.", name);
132     return (-1);
133   }
134
135   sfree (*dest);
136   *dest = strdup (ci->values[0].value.string);
137   if (*dest == NULL)
138     return (-1);
139
140   return (0);
141 } /* }}} int cmc_config_add_string */
142
143 static int cmc_config_add_match_dstype (int *dstype_ret, /* {{{ */
144     oconfig_item_t *ci)
145 {
146   int dstype;
147
148   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
149   {
150     WARNING ("memcachec plugin: `DSType' needs exactly one string argument.");
151     return (-1);
152   }
153
154   if (strncasecmp ("Gauge", ci->values[0].value.string,
155         strlen ("Gauge")) == 0)
156   {
157     dstype = UTILS_MATCH_DS_TYPE_GAUGE;
158     if (strcasecmp ("GaugeAverage", ci->values[0].value.string) == 0)
159       dstype |= UTILS_MATCH_CF_GAUGE_AVERAGE;
160     else if (strcasecmp ("GaugeMin", ci->values[0].value.string) == 0)
161       dstype |= UTILS_MATCH_CF_GAUGE_MIN;
162     else if (strcasecmp ("GaugeMax", ci->values[0].value.string) == 0)
163       dstype |= UTILS_MATCH_CF_GAUGE_MAX;
164     else if (strcasecmp ("GaugeLast", ci->values[0].value.string) == 0)
165       dstype |= UTILS_MATCH_CF_GAUGE_LAST;
166     else
167       dstype = 0;
168   }
169   else if (strncasecmp ("Counter", ci->values[0].value.string,
170         strlen ("Counter")) == 0)
171   {
172     dstype = UTILS_MATCH_DS_TYPE_COUNTER;
173     if (strcasecmp ("CounterSet", ci->values[0].value.string) == 0)
174       dstype |= UTILS_MATCH_CF_COUNTER_SET;
175     else if (strcasecmp ("CounterAdd", ci->values[0].value.string) == 0)
176       dstype |= UTILS_MATCH_CF_COUNTER_ADD;
177     else if (strcasecmp ("CounterInc", ci->values[0].value.string) == 0)
178       dstype |= UTILS_MATCH_CF_COUNTER_INC;
179     else
180       dstype = 0;
181   }
182   else
183   {
184     dstype = 0;
185   }
186
187   if (dstype == 0)
188   {
189     WARNING ("memcachec plugin: `%s' is not a valid argument to `DSType'.",
190         ci->values[0].value.string);
191     return (-1);
192   }
193
194   *dstype_ret = dstype;
195   return (0);
196 } /* }}} int cmc_config_add_match_dstype */
197
198 static int cmc_config_add_match (web_page_t *page, /* {{{ */
199     oconfig_item_t *ci)
200 {
201   web_match_t *match;
202   int status;
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 (int 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: match_create_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
300   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
301   {
302     WARNING ("memcachec plugin: `Page' blocks need exactly one string argument.");
303     return (-1);
304   }
305
306   page = calloc (1, sizeof (*page));
307   if (page == NULL)
308   {
309     ERROR ("memcachec plugin: calloc failed.");
310     return (-1);
311   }
312   page->server = NULL;
313   page->key = NULL;
314
315   page->instance = strdup (ci->values[0].value.string);
316   if (page->instance == NULL)
317   {
318     ERROR ("memcachec plugin: strdup failed.");
319     sfree (page);
320     return (-1);
321   }
322
323   /* Process all children */
324   status = 0;
325   for (int i = 0; i < ci->children_num; i++)
326   {
327     oconfig_item_t *child = ci->children + i;
328
329     if (strcasecmp ("Server", child->key) == 0)
330       status = cmc_config_add_string ("Server", &page->server, child);
331     else if (strcasecmp ("Key", child->key) == 0)
332       status = cmc_config_add_string ("Key", &page->key, child);
333     else if (strcasecmp ("Match", child->key) == 0)
334       /* Be liberal with failing matches => don't set `status'. */
335       cmc_config_add_match (page, child);
336     else
337     {
338       WARNING ("memcachec plugin: Option `%s' not allowed here.", child->key);
339       status = -1;
340     }
341
342     if (status != 0)
343       break;
344   } /* for (i = 0; i < ci->children_num; i++) */
345
346   /* Additionial sanity checks and libmemcached initialization. */
347   while (status == 0)
348   {
349     if (page->server == NULL)
350     {
351       WARNING ("memcachec plugin: `Server' missing in `Page' block.");
352       status = -1;
353     }
354
355     if (page->key == NULL)
356     {
357       WARNING ("memcachec plugin: `Key' missing in `Page' block.");
358       status = -1;
359     }
360
361     if (page->matches == NULL)
362     {
363       assert (page->instance != NULL);
364       WARNING ("memcachec plugin: No (valid) `Match' block "
365           "within `Page' block `%s'.", page->instance);
366       status = -1;
367     }
368
369     if (status == 0)
370       status = cmc_page_init_memc (page);
371
372     break;
373   } /* while (status == 0) */
374
375   if (status != 0)
376   {
377     cmc_web_page_free (page);
378     return (status);
379   }
380
381   /* Add the new page to the linked list */
382   if (pages_g == NULL)
383     pages_g = page;
384   else
385   {
386     web_page_t *prev;
387
388     prev = pages_g;
389     while (prev->next != NULL)
390       prev = prev->next;
391     prev->next = page;
392   }
393
394   return (0);
395 } /* }}} int cmc_config_add_page */
396
397 static int cmc_config (oconfig_item_t *ci) /* {{{ */
398 {
399   int success;
400   int errors;
401   int status;
402
403   success = 0;
404   errors = 0;
405
406   for (int i = 0; i < ci->children_num; i++)
407   {
408     oconfig_item_t *child = ci->children + i;
409
410     if (strcasecmp ("Page", child->key) == 0)
411     {
412       status = cmc_config_add_page (child);
413       if (status == 0)
414         success++;
415       else
416         errors++;
417     }
418     else
419     {
420       WARNING ("memcachec plugin: Option `%s' not allowed here.", child->key);
421       errors++;
422     }
423   }
424
425   if ((success == 0) && (errors > 0))
426   {
427     ERROR ("memcachec plugin: All statements failed.");
428     return (-1);
429   }
430
431   return (0);
432 } /* }}} int cmc_config */
433
434 static int cmc_init (void) /* {{{ */
435 {
436   if (pages_g == NULL)
437   {
438     INFO ("memcachec plugin: No pages have been defined.");
439     return (-1);
440   }
441   return (0);
442 } /* }}} int cmc_init */
443
444 static void cmc_submit (const web_page_t *wp, const web_match_t *wm, /* {{{ */
445     const cu_match_value_t *mv)
446 {
447   value_t values[1];
448   value_list_t vl = VALUE_LIST_INIT;
449
450   values[0] = mv->value;
451
452   vl.values = values;
453   vl.values_len = 1;
454   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
455   sstrncpy (vl.plugin, "memcachec", sizeof (vl.plugin));
456   sstrncpy (vl.plugin_instance, wp->instance, sizeof (vl.plugin_instance));
457   sstrncpy (vl.type, wm->type, sizeof (vl.type));
458   sstrncpy (vl.type_instance, wm->instance, sizeof (vl.type_instance));
459
460   plugin_dispatch_values (&vl);
461 } /* }}} void cmc_submit */
462
463 static int cmc_read_page (web_page_t *wp) /* {{{ */
464 {
465   memcached_return rc;
466   size_t string_length;
467   uint32_t flags;
468   int status;
469
470   if (wp->memc == NULL)
471     return (-1);
472
473   wp->buffer = memcached_get (wp->memc, wp->key, strlen (wp->key),
474                               &string_length, &flags, &rc);
475   if (rc != MEMCACHED_SUCCESS)
476   {
477     ERROR ("memcachec plugin: memcached_get failed: %s",
478         memcached_strerror (wp->memc, rc));
479     return (-1);
480   }
481
482   for (web_match_t *wm = wp->matches; wm != NULL; wm = wm->next)
483   {
484     cu_match_value_t *mv;
485
486     status = match_apply (wm->match, wp->buffer);
487     if (status != 0)
488     {
489       WARNING ("memcachec plugin: match_apply failed.");
490       continue;
491     }
492
493     mv = match_get_user_data (wm->match);
494     if (mv == NULL)
495     {
496       WARNING ("memcachec plugin: match_get_user_data returned NULL.");
497       continue;
498     }
499
500     cmc_submit (wp, wm, mv);
501     match_value_reset (mv);
502   } /* for (wm = wp->matches; wm != NULL; wm = wm->next) */
503
504   sfree (wp->buffer);
505
506   return (0);
507 } /* }}} int cmc_read_page */
508
509 static int cmc_read (void) /* {{{ */
510 {
511   for (web_page_t *wp = pages_g; wp != NULL; wp = wp->next)
512     cmc_read_page (wp);
513
514   return (0);
515 } /* }}} int cmc_read */
516
517 static int cmc_shutdown (void) /* {{{ */
518 {
519   cmc_web_page_free (pages_g);
520   pages_g = NULL;
521
522   return (0);
523 } /* }}} int cmc_shutdown */
524
525 void module_register (void)
526 {
527   plugin_register_complex_config ("memcachec", cmc_config);
528   plugin_register_init ("memcachec", cmc_init);
529   plugin_register_read ("memcachec", cmc_read);
530   plugin_register_shutdown ("memcachec", cmc_shutdown);
531 } /* void module_register */
532
533 /* vim: set sw=2 sts=2 et fdm=marker : */