Merge pull request #1830 from rubenk/move-collectd-header
[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   int i;
204
205   if (ci->values_num != 0)
206   {
207     WARNING ("memcachec plugin: Ignoring arguments for the `Match' block.");
208   }
209
210   match = calloc (1, sizeof (*match));
211   if (match == NULL)
212   {
213     ERROR ("memcachec plugin: calloc failed.");
214     return (-1);
215   }
216
217   status = 0;
218   for (i = 0; i < ci->children_num; i++)
219   {
220     oconfig_item_t *child = ci->children + i;
221
222     if (strcasecmp ("Regex", child->key) == 0)
223       status = cmc_config_add_string ("Regex", &match->regex, child);
224     else if (strcasecmp ("ExcludeRegex", child->key) == 0)
225       status = cmc_config_add_string ("ExcludeRegex", &match->exclude_regex, child);
226     else if (strcasecmp ("DSType", child->key) == 0)
227       status = cmc_config_add_match_dstype (&match->dstype, child);
228     else if (strcasecmp ("Type", child->key) == 0)
229       status = cmc_config_add_string ("Type", &match->type, child);
230     else if (strcasecmp ("Instance", child->key) == 0)
231       status = cmc_config_add_string ("Instance", &match->instance, child);
232     else
233     {
234       WARNING ("memcachec plugin: Option `%s' not allowed here.", child->key);
235       status = -1;
236     }
237
238     if (status != 0)
239       break;
240   } /* for (i = 0; i < ci->children_num; i++) */
241
242   while (status == 0)
243   {
244     if (match->regex == NULL)
245     {
246       WARNING ("memcachec plugin: `Regex' missing in `Match' block.");
247       status = -1;
248     }
249
250     if (match->type == NULL)
251     {
252       WARNING ("memcachec plugin: `Type' missing in `Match' block.");
253       status = -1;
254     }
255
256     if (match->dstype == 0)
257     {
258       WARNING ("memcachec plugin: `DSType' missing in `Match' block.");
259       status = -1;
260     }
261
262     break;
263   } /* while (status == 0) */
264
265   if (status != 0)
266   {
267     cmc_web_match_free (match);
268     return (status);
269   }
270
271   match->match = match_create_simple (match->regex, match->exclude_regex,
272       match->dstype);
273   if (match->match == NULL)
274   {
275     ERROR ("memcachec plugin: match_create_simple failed.");
276     cmc_web_match_free (match);
277     return (-1);
278   }
279   else
280   {
281     web_match_t *prev;
282
283     prev = page->matches;
284     while ((prev != NULL) && (prev->next != NULL))
285       prev = prev->next;
286
287     if (prev == NULL)
288       page->matches = match;
289     else
290       prev->next = match;
291   }
292
293   return (0);
294 } /* }}} int cmc_config_add_match */
295
296 static int cmc_config_add_page (oconfig_item_t *ci) /* {{{ */
297 {
298   web_page_t *page;
299   int status;
300   int i;
301
302   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
303   {
304     WARNING ("memcachec plugin: `Page' blocks need exactly one string argument.");
305     return (-1);
306   }
307
308   page = calloc (1, sizeof (*page));
309   if (page == NULL)
310   {
311     ERROR ("memcachec plugin: calloc failed.");
312     return (-1);
313   }
314   page->server = NULL;
315   page->key = NULL;
316
317   page->instance = strdup (ci->values[0].value.string);
318   if (page->instance == NULL)
319   {
320     ERROR ("memcachec plugin: strdup failed.");
321     sfree (page);
322     return (-1);
323   }
324
325   /* Process all children */
326   status = 0;
327   for (i = 0; i < ci->children_num; i++)
328   {
329     oconfig_item_t *child = ci->children + i;
330
331     if (strcasecmp ("Server", child->key) == 0)
332       status = cmc_config_add_string ("Server", &page->server, child);
333     else if (strcasecmp ("Key", child->key) == 0)
334       status = cmc_config_add_string ("Key", &page->key, child);
335     else if (strcasecmp ("Match", child->key) == 0)
336       /* Be liberal with failing matches => don't set `status'. */
337       cmc_config_add_match (page, child);
338     else
339     {
340       WARNING ("memcachec plugin: Option `%s' not allowed here.", child->key);
341       status = -1;
342     }
343
344     if (status != 0)
345       break;
346   } /* for (i = 0; i < ci->children_num; i++) */
347
348   /* Additionial sanity checks and libmemcached initialization. */
349   while (status == 0)
350   {
351     if (page->server == NULL)
352     {
353       WARNING ("memcachec plugin: `Server' missing in `Page' block.");
354       status = -1;
355     }
356
357     if (page->key == NULL)
358     {
359       WARNING ("memcachec plugin: `Key' missing in `Page' block.");
360       status = -1;
361     }
362
363     if (page->matches == NULL)
364     {
365       assert (page->instance != NULL);
366       WARNING ("memcachec plugin: No (valid) `Match' block "
367           "within `Page' block `%s'.", page->instance);
368       status = -1;
369     }
370
371     if (status == 0)
372       status = cmc_page_init_memc (page);
373
374     break;
375   } /* while (status == 0) */
376
377   if (status != 0)
378   {
379     cmc_web_page_free (page);
380     return (status);
381   }
382
383   /* Add the new page to the linked list */
384   if (pages_g == NULL)
385     pages_g = page;
386   else
387   {
388     web_page_t *prev;
389
390     prev = pages_g;
391     while (prev->next != NULL)
392       prev = prev->next;
393     prev->next = page;
394   }
395
396   return (0);
397 } /* }}} int cmc_config_add_page */
398
399 static int cmc_config (oconfig_item_t *ci) /* {{{ */
400 {
401   int success;
402   int errors;
403   int status;
404   int i;
405
406   success = 0;
407   errors = 0;
408
409   for (i = 0; i < ci->children_num; i++)
410   {
411     oconfig_item_t *child = ci->children + i;
412
413     if (strcasecmp ("Page", child->key) == 0)
414     {
415       status = cmc_config_add_page (child);
416       if (status == 0)
417         success++;
418       else
419         errors++;
420     }
421     else
422     {
423       WARNING ("memcachec plugin: Option `%s' not allowed here.", child->key);
424       errors++;
425     }
426   }
427
428   if ((success == 0) && (errors > 0))
429   {
430     ERROR ("memcachec plugin: All statements failed.");
431     return (-1);
432   }
433
434   return (0);
435 } /* }}} int cmc_config */
436
437 static int cmc_init (void) /* {{{ */
438 {
439   if (pages_g == NULL)
440   {
441     INFO ("memcachec plugin: No pages have been defined.");
442     return (-1);
443   }
444   return (0);
445 } /* }}} int cmc_init */
446
447 static void cmc_submit (const web_page_t *wp, const web_match_t *wm, /* {{{ */
448     const cu_match_value_t *mv)
449 {
450   value_t values[1];
451   value_list_t vl = VALUE_LIST_INIT;
452
453   values[0] = mv->value;
454
455   vl.values = values;
456   vl.values_len = 1;
457   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
458   sstrncpy (vl.plugin, "memcachec", sizeof (vl.plugin));
459   sstrncpy (vl.plugin_instance, wp->instance, sizeof (vl.plugin_instance));
460   sstrncpy (vl.type, wm->type, sizeof (vl.type));
461   sstrncpy (vl.type_instance, wm->instance, sizeof (vl.type_instance));
462
463   plugin_dispatch_values (&vl);
464 } /* }}} void cmc_submit */
465
466 static int cmc_read_page (web_page_t *wp) /* {{{ */
467 {
468   web_match_t *wm;
469   memcached_return rc;
470   size_t string_length;
471   uint32_t flags;
472   int status;
473
474   if (wp->memc == NULL)
475     return (-1);
476
477   wp->buffer = memcached_get (wp->memc, wp->key, strlen (wp->key),
478                               &string_length, &flags, &rc);
479   if (rc != MEMCACHED_SUCCESS)
480   {
481     ERROR ("memcachec plugin: memcached_get failed: %s",
482         memcached_strerror (wp->memc, rc));
483     return (-1);
484   }
485
486   for (wm = wp->matches; wm != NULL; wm = wm->next)
487   {
488     cu_match_value_t *mv;
489
490     status = match_apply (wm->match, wp->buffer);
491     if (status != 0)
492     {
493       WARNING ("memcachec plugin: match_apply failed.");
494       continue;
495     }
496
497     mv = match_get_user_data (wm->match);
498     if (mv == NULL)
499     {
500       WARNING ("memcachec plugin: match_get_user_data returned NULL.");
501       continue;
502     }
503
504     cmc_submit (wp, wm, mv);
505     match_value_reset (mv);
506   } /* for (wm = wp->matches; wm != NULL; wm = wm->next) */
507
508   sfree (wp->buffer);
509
510   return (0);
511 } /* }}} int cmc_read_page */
512
513 static int cmc_read (void) /* {{{ */
514 {
515   web_page_t *wp;
516
517   for (wp = pages_g; wp != NULL; wp = wp->next)
518     cmc_read_page (wp);
519
520   return (0);
521 } /* }}} int cmc_read */
522
523 static int cmc_shutdown (void) /* {{{ */
524 {
525   cmc_web_page_free (pages_g);
526   pages_g = NULL;
527
528   return (0);
529 } /* }}} int cmc_shutdown */
530
531 void module_register (void)
532 {
533   plugin_register_complex_config ("memcachec", cmc_config);
534   plugin_register_init ("memcachec", cmc_init);
535   plugin_register_read ("memcachec", cmc_read);
536   plugin_register_shutdown ("memcachec", cmc_shutdown);
537 } /* void module_register */
538
539 /* vim: set sw=2 sts=2 et fdm=marker : */