Merge branch 'collectd-4.6' into collectd-4.7
[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 verplant.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   int dstype;
41   char *type;
42   char *instance;
43
44   cu_match_t *match;
45
46   web_match_t *next;
47 }; /* }}} */
48
49 struct web_page_s;
50 typedef struct web_page_s web_page_t;
51 struct web_page_s /* {{{ */
52 {
53   char *instance;
54
55   char *server;
56   char *key;
57
58   memcached_st *memc;
59   char *buffer;
60
61   web_match_t *matches;
62
63   web_page_t *next;
64 }; /* }}} */
65
66 /*
67  * Global variables;
68  */
69 static web_page_t *pages_g = NULL;
70
71 /*
72  * Private functions
73  */
74 static void cmc_web_match_free (web_match_t *wm) /* {{{ */
75 {
76   if (wm == NULL)
77     return;
78
79   sfree (wm->regex);
80   sfree (wm->type);
81   sfree (wm->instance);
82   match_destroy (wm->match);
83   cmc_web_match_free (wm->next);
84   sfree (wm);
85 } /* }}} void cmc_web_match_free */
86
87 static void cmc_web_page_free (web_page_t *wp) /* {{{ */
88 {
89   if (wp == NULL)
90     return;
91
92   if (wp->memc != NULL)
93     memcached_free(wp->memc);
94   wp->memc = NULL;
95
96   sfree (wp->instance);
97   sfree (wp->server);
98   sfree (wp->key);
99   sfree (wp->buffer);
100
101   cmc_web_match_free (wp->matches);
102   cmc_web_page_free (wp->next);
103   sfree (wp);
104 } /* }}} void cmc_web_page_free */
105
106 static int cmc_page_init_memc (web_page_t *wp) /* {{{ */
107 {
108   memcached_server_st *server;
109
110   wp->memc = memcached_create(NULL);
111   if (wp->memc == NULL)
112   {
113     ERROR ("memcachec plugin: memcached_create failed.");
114     return (-1);
115   }
116
117   server = memcached_servers_parse (wp->server);
118   memcached_server_push (wp->memc, server);
119   memcached_server_list_free (server);
120
121   return (0);
122 } /* }}} int cmc_page_init_memc */
123
124 static int cmc_config_add_string (const char *name, char **dest, /* {{{ */
125     oconfig_item_t *ci)
126 {
127   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
128   {
129     WARNING ("memcachec plugin: `%s' needs exactly one string argument.", name);
130     return (-1);
131   }
132
133   sfree (*dest);
134   *dest = strdup (ci->values[0].value.string);
135   if (*dest == NULL)
136     return (-1);
137
138   return (0);
139 } /* }}} int cmc_config_add_string */
140
141 static int cmc_config_add_match_dstype (int *dstype_ret, /* {{{ */
142     oconfig_item_t *ci)
143 {
144   int dstype;
145
146   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
147   {
148     WARNING ("memcachec plugin: `DSType' needs exactly one string argument.");
149     return (-1);
150   }
151
152   if (strncasecmp ("Gauge", ci->values[0].value.string,
153         strlen ("Gauge")) == 0)
154   {
155     dstype = UTILS_MATCH_DS_TYPE_GAUGE;
156     if (strcasecmp ("GaugeAverage", ci->values[0].value.string) == 0)
157       dstype |= UTILS_MATCH_CF_GAUGE_AVERAGE;
158     else if (strcasecmp ("GaugeMin", ci->values[0].value.string) == 0)
159       dstype |= UTILS_MATCH_CF_GAUGE_MIN;
160     else if (strcasecmp ("GaugeMax", ci->values[0].value.string) == 0)
161       dstype |= UTILS_MATCH_CF_GAUGE_MAX;
162     else if (strcasecmp ("GaugeLast", ci->values[0].value.string) == 0)
163       dstype |= UTILS_MATCH_CF_GAUGE_LAST;
164     else
165       dstype = 0;
166   }
167   else if (strncasecmp ("Counter", ci->values[0].value.string,
168         strlen ("Counter")) == 0)
169   {
170     dstype = UTILS_MATCH_DS_TYPE_COUNTER;
171     if (strcasecmp ("CounterSet", ci->values[0].value.string) == 0)
172       dstype |= UTILS_MATCH_CF_COUNTER_SET;
173     else if (strcasecmp ("CounterAdd", ci->values[0].value.string) == 0)
174       dstype |= UTILS_MATCH_CF_COUNTER_ADD;
175     else if (strcasecmp ("CounterInc", ci->values[0].value.string) == 0)
176       dstype |= UTILS_MATCH_CF_COUNTER_INC;
177     else
178       dstype = 0;
179   }
180   else
181   {
182     dstype = 0;
183   }
184
185   if (dstype == 0)
186   {
187     WARNING ("memcachec plugin: `%s' is not a valid argument to `DSType'.",
188         ci->values[0].value.string);
189     return (-1);
190   }
191
192   *dstype_ret = dstype;
193   return (0);
194 } /* }}} int cmc_config_add_match_dstype */
195
196 static int cmc_config_add_match (web_page_t *page, /* {{{ */
197     oconfig_item_t *ci)
198 {
199   web_match_t *match;
200   int status;
201   int i;
202
203   if (ci->values_num != 0)
204   {
205     WARNING ("memcachec plugin: Ignoring arguments for the `Match' block.");
206   }
207
208   match = (web_match_t *) malloc (sizeof (*match));
209   if (match == NULL)
210   {
211     ERROR ("memcachec plugin: malloc failed.");
212     return (-1);
213   }
214   memset (match, 0, sizeof (*match));
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 ("DSType", child->key) == 0)
224       status = cmc_config_add_match_dstype (&match->dstype, child);
225     else if (strcasecmp ("Type", child->key) == 0)
226       status = cmc_config_add_string ("Type", &match->type, child);
227     else if (strcasecmp ("Instance", child->key) == 0)
228       status = cmc_config_add_string ("Instance", &match->instance, child);
229     else
230     {
231       WARNING ("memcachec plugin: Option `%s' not allowed here.", child->key);
232       status = -1;
233     }
234
235     if (status != 0)
236       break;
237   } /* for (i = 0; i < ci->children_num; i++) */
238
239   while (status == 0)
240   {
241     if (match->regex == NULL)
242     {
243       WARNING ("memcachec plugin: `Regex' missing in `Match' block.");
244       status = -1;
245     }
246
247     if (match->type == NULL)
248     {
249       WARNING ("memcachec plugin: `Type' missing in `Match' block.");
250       status = -1;
251     }
252
253     if (match->dstype == 0)
254     {
255       WARNING ("memcachec plugin: `DSType' missing in `Match' block.");
256       status = -1;
257     }
258
259     break;
260   } /* while (status == 0) */
261
262   if (status != 0)
263     return (status);
264
265   match->match = match_create_simple (match->regex, match->dstype);
266   if (match->match == NULL)
267   {
268     ERROR ("memcachec plugin: tail_match_add_match_simple failed.");
269     cmc_web_match_free (match);
270     return (-1);
271   }
272   else
273   {
274     web_match_t *prev;
275
276     prev = page->matches;
277     while ((prev != NULL) && (prev->next != NULL))
278       prev = prev->next;
279
280     if (prev == NULL)
281       page->matches = match;
282     else
283       prev->next = match;
284   }
285
286   return (0);
287 } /* }}} int cmc_config_add_match */
288
289 static int cmc_config_add_page (oconfig_item_t *ci) /* {{{ */
290 {
291   web_page_t *page;
292   int status;
293   int i;
294
295   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
296   {
297     WARNING ("memcachec plugin: `Page' blocks need exactly one string argument.");
298     return (-1);
299   }
300
301   page = (web_page_t *) malloc (sizeof (*page));
302   if (page == NULL)
303   {
304     ERROR ("memcachec plugin: malloc failed.");
305     return (-1);
306   }
307   memset (page, 0, sizeof (*page));
308   page->server = NULL;
309   page->key = NULL;
310
311   page->instance = strdup (ci->values[0].value.string);
312   if (page->instance == NULL)
313   {
314     ERROR ("memcachec plugin: strdup failed.");
315     sfree (page);
316     return (-1);
317   }
318
319   /* Process all children */
320   status = 0;
321   for (i = 0; i < ci->children_num; i++)
322   {
323     oconfig_item_t *child = ci->children + i;
324
325     if (strcasecmp ("Server", child->key) == 0)
326       status = cmc_config_add_string ("Server", &page->server, child);
327     if (strcasecmp ("Key", child->key) == 0)
328       status = cmc_config_add_string ("Key", &page->key, child);
329     else if (strcasecmp ("Match", child->key) == 0)
330       /* Be liberal with failing matches => don't set `status'. */
331       cmc_config_add_match (page, child);
332     else
333     {
334       WARNING ("memcachec plugin: Option `%s' not allowed here.", child->key);
335       status = -1;
336     }
337
338     if (status != 0)
339       break;
340   } /* for (i = 0; i < ci->children_num; i++) */
341
342   /* Additionial sanity checks and libmemcached initialization. */
343   while (status == 0)
344   {
345     if (page->server == NULL)
346     {
347       WARNING ("memcachec plugin: `Server' missing in `Page' block.");
348       status = -1;
349     }
350
351     if (page->key == NULL)
352     {
353       WARNING ("memcachec plugin: `Key' missing in `Page' block.");
354       status = -1;
355     }
356
357     if (page->matches == NULL)
358     {
359       assert (page->instance != NULL);
360       WARNING ("memcachec plugin: No (valid) `Match' block "
361           "within `Page' block `%s'.", page->instance);
362       status = -1;
363     }
364
365     if (status == 0)
366       status = cmc_page_init_memc (page);
367
368     break;
369   } /* while (status == 0) */
370
371   if (status != 0)
372   {
373     cmc_web_page_free (page);
374     return (status);
375   }
376
377   /* Add the new page to the linked list */
378   if (pages_g == NULL)
379     pages_g = page;
380   else
381   {
382     web_page_t *prev;
383
384     prev = pages_g;
385     while ((prev != NULL) && (prev->next != NULL))
386       prev = prev->next;
387     prev->next = page;
388   }
389
390   return (0);
391 } /* }}} int cmc_config_add_page */
392
393 static int cmc_config (oconfig_item_t *ci) /* {{{ */
394 {
395   int success;
396   int errors;
397   int status;
398   int i;
399
400   success = 0;
401   errors = 0;
402
403   for (i = 0; i < ci->children_num; i++)
404   {
405     oconfig_item_t *child = ci->children + i;
406
407     if (strcasecmp ("Page", child->key) == 0)
408     {
409       status = cmc_config_add_page (child);
410       if (status == 0)
411         success++;
412       else
413         errors++;
414     }
415     else
416     {
417       WARNING ("memcachec plugin: Option `%s' not allowed here.", child->key);
418       errors++;
419     }
420   }
421
422   if ((success == 0) && (errors > 0))
423   {
424     ERROR ("memcachec plugin: All statements failed.");
425     return (-1);
426   }
427
428   return (0);
429 } /* }}} int cmc_config */
430
431 static int cmc_init (void) /* {{{ */
432 {
433   if (pages_g == NULL)
434   {
435     INFO ("memcachec plugin: No pages have been defined.");
436     return (-1);
437   }
438   return (0);
439 } /* }}} int cmc_init */
440
441 static void cmc_submit (const web_page_t *wp, const web_match_t *wm, /* {{{ */
442     const cu_match_value_t *mv)
443 {
444   value_t values[1];
445   value_list_t vl = VALUE_LIST_INIT;
446
447   values[0] = mv->value;
448
449   vl.values = values;
450   vl.values_len = 1;
451   vl.time = time (NULL);
452   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
453   sstrncpy (vl.plugin, "memcachec", sizeof (vl.plugin));
454   sstrncpy (vl.plugin_instance, wp->instance, sizeof (vl.plugin_instance));
455   sstrncpy (vl.type, wm->type, sizeof (vl.type));
456   sstrncpy (vl.type_instance, wm->instance, sizeof (vl.type_instance));
457
458   plugin_dispatch_values (&vl);
459 } /* }}} void cmc_submit */
460
461 static int cmc_read_page (web_page_t *wp) /* {{{ */
462 {
463   web_match_t *wm;
464   memcached_return rc;
465   size_t string_length;
466   uint32_t flags;
467   int status;
468
469   if (wp->memc == NULL)
470     return (-1);
471
472   wp->buffer = memcached_get (wp->memc, wp->key, strlen (wp->key),
473                               &string_length, &flags, &rc);
474   if (rc != MEMCACHED_SUCCESS)
475   {
476     ERROR ("memcachec plugin: memcached_get failed: %s",
477         memcached_strerror (wp->memc, rc));
478     return (-1);
479   }
480
481   for (wm = wp->matches; wm != NULL; wm = wm->next)
482   {
483     cu_match_value_t *mv;
484
485     status = match_apply (wm->match, wp->buffer);
486     if (status != 0)
487     {
488       WARNING ("memcachec plugin: match_apply failed.");
489       continue;
490     }
491
492     mv = match_get_user_data (wm->match);
493     if (mv == NULL)
494     {
495       WARNING ("memcachec plugin: match_get_user_data returned NULL.");
496       continue;
497     }
498
499     cmc_submit (wp, wm, mv);
500   } /* for (wm = wp->matches; wm != NULL; wm = wm->next) */
501
502   sfree (wp->buffer);
503
504   return (0);
505 } /* }}} int cmc_read_page */
506
507 static int cmc_read (void) /* {{{ */
508 {
509   web_page_t *wp;
510
511   for (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 : */