2 * collectd - src/memcachec.c
3 * Copyright (C) 2009 Doug MacEachern
4 * Copyright (C) 2006-2009 Florian octo Forster
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.
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.
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
20 * Doug MacEachern <Doug.MacEachern at hyperic.com>
21 * Florian octo Forster <octo at collectd.org>
28 #include "configfile.h"
29 #include "utils_match.h"
31 #include <libmemcached/memcached.h>
37 typedef struct web_match_s web_match_t;
38 struct web_match_s /* {{{ */
52 typedef struct web_page_s web_page_t;
53 struct web_page_s /* {{{ */
71 static web_page_t *pages_g = NULL;
76 static void cmc_web_match_free (web_match_t *wm) /* {{{ */
84 match_destroy (wm->match);
85 cmc_web_match_free (wm->next);
87 } /* }}} void cmc_web_match_free */
89 static void cmc_web_page_free (web_page_t *wp) /* {{{ */
95 memcached_free(wp->memc);
103 cmc_web_match_free (wp->matches);
104 cmc_web_page_free (wp->next);
106 } /* }}} void cmc_web_page_free */
108 static int cmc_page_init_memc (web_page_t *wp) /* {{{ */
110 memcached_server_st *server;
112 wp->memc = memcached_create(NULL);
113 if (wp->memc == NULL)
115 ERROR ("memcachec plugin: memcached_create failed.");
119 server = memcached_servers_parse (wp->server);
120 memcached_server_push (wp->memc, server);
121 memcached_server_list_free (server);
124 } /* }}} int cmc_page_init_memc */
126 static int cmc_config_add_string (const char *name, char **dest, /* {{{ */
129 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
131 WARNING ("memcachec plugin: `%s' needs exactly one string argument.", name);
136 *dest = strdup (ci->values[0].value.string);
141 } /* }}} int cmc_config_add_string */
143 static int cmc_config_add_match_dstype (int *dstype_ret, /* {{{ */
148 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
150 WARNING ("memcachec plugin: `DSType' needs exactly one string argument.");
154 if (strncasecmp ("Gauge", ci->values[0].value.string,
155 strlen ("Gauge")) == 0)
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;
169 else if (strncasecmp ("Counter", ci->values[0].value.string,
170 strlen ("Counter")) == 0)
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;
189 WARNING ("memcachec plugin: `%s' is not a valid argument to `DSType'.",
190 ci->values[0].value.string);
194 *dstype_ret = dstype;
196 } /* }}} int cmc_config_add_match_dstype */
198 static int cmc_config_add_match (web_page_t *page, /* {{{ */
204 if (ci->values_num != 0)
206 WARNING ("memcachec plugin: Ignoring arguments for the `Match' block.");
209 match = calloc (1, sizeof (*match));
212 ERROR ("memcachec plugin: calloc failed.");
217 for (int i = 0; i < ci->children_num; i++)
219 oconfig_item_t *child = ci->children + i;
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);
233 WARNING ("memcachec plugin: Option `%s' not allowed here.", child->key);
239 } /* for (i = 0; i < ci->children_num; i++) */
243 if (match->regex == NULL)
245 WARNING ("memcachec plugin: `Regex' missing in `Match' block.");
249 if (match->type == NULL)
251 WARNING ("memcachec plugin: `Type' missing in `Match' block.");
255 if (match->dstype == 0)
257 WARNING ("memcachec plugin: `DSType' missing in `Match' block.");
262 } /* while (status == 0) */
266 cmc_web_match_free (match);
270 match->match = match_create_simple (match->regex, match->exclude_regex,
272 if (match->match == NULL)
274 ERROR ("memcachec plugin: match_create_simple failed.");
275 cmc_web_match_free (match);
282 prev = page->matches;
283 while ((prev != NULL) && (prev->next != NULL))
287 page->matches = match;
293 } /* }}} int cmc_config_add_match */
295 static int cmc_config_add_page (oconfig_item_t *ci) /* {{{ */
300 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
302 WARNING ("memcachec plugin: `Page' blocks need exactly one string argument.");
306 page = calloc (1, sizeof (*page));
309 ERROR ("memcachec plugin: calloc failed.");
315 page->instance = strdup (ci->values[0].value.string);
316 if (page->instance == NULL)
318 ERROR ("memcachec plugin: strdup failed.");
323 /* Process all children */
325 for (int i = 0; i < ci->children_num; i++)
327 oconfig_item_t *child = ci->children + i;
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);
338 WARNING ("memcachec plugin: Option `%s' not allowed here.", child->key);
344 } /* for (i = 0; i < ci->children_num; i++) */
346 /* Additionial sanity checks and libmemcached initialization. */
349 if (page->server == NULL)
351 WARNING ("memcachec plugin: `Server' missing in `Page' block.");
355 if (page->key == NULL)
357 WARNING ("memcachec plugin: `Key' missing in `Page' block.");
361 if (page->matches == NULL)
363 assert (page->instance != NULL);
364 WARNING ("memcachec plugin: No (valid) `Match' block "
365 "within `Page' block `%s'.", page->instance);
370 status = cmc_page_init_memc (page);
373 } /* while (status == 0) */
377 cmc_web_page_free (page);
381 /* Add the new page to the linked list */
389 while (prev->next != NULL)
395 } /* }}} int cmc_config_add_page */
397 static int cmc_config (oconfig_item_t *ci) /* {{{ */
406 for (int i = 0; i < ci->children_num; i++)
408 oconfig_item_t *child = ci->children + i;
410 if (strcasecmp ("Page", child->key) == 0)
412 status = cmc_config_add_page (child);
420 WARNING ("memcachec plugin: Option `%s' not allowed here.", child->key);
425 if ((success == 0) && (errors > 0))
427 ERROR ("memcachec plugin: All statements failed.");
432 } /* }}} int cmc_config */
434 static int cmc_init (void) /* {{{ */
438 INFO ("memcachec plugin: No pages have been defined.");
442 } /* }}} int cmc_init */
444 static void cmc_submit (const web_page_t *wp, const web_match_t *wm, /* {{{ */
445 const cu_match_value_t *mv)
448 value_list_t vl = VALUE_LIST_INIT;
450 values[0] = mv->value;
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));
460 plugin_dispatch_values (&vl);
461 } /* }}} void cmc_submit */
463 static int cmc_read_page (web_page_t *wp) /* {{{ */
466 size_t string_length;
470 if (wp->memc == NULL)
473 wp->buffer = memcached_get (wp->memc, wp->key, strlen (wp->key),
474 &string_length, &flags, &rc);
475 if (rc != MEMCACHED_SUCCESS)
477 ERROR ("memcachec plugin: memcached_get failed: %s",
478 memcached_strerror (wp->memc, rc));
482 for (web_match_t *wm = wp->matches; wm != NULL; wm = wm->next)
484 cu_match_value_t *mv;
486 status = match_apply (wm->match, wp->buffer);
489 WARNING ("memcachec plugin: match_apply failed.");
493 mv = match_get_user_data (wm->match);
496 WARNING ("memcachec plugin: match_get_user_data returned NULL.");
500 cmc_submit (wp, wm, mv);
501 match_value_reset (mv);
502 } /* for (wm = wp->matches; wm != NULL; wm = wm->next) */
507 } /* }}} int cmc_read_page */
509 static int cmc_read (void) /* {{{ */
511 for (web_page_t *wp = pages_g; wp != NULL; wp = wp->next)
515 } /* }}} int cmc_read */
517 static int cmc_shutdown (void) /* {{{ */
519 cmc_web_page_free (pages_g);
523 } /* }}} int cmc_shutdown */
525 void module_register (void)
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 */
533 /* vim: set sw=2 sts=2 et fdm=marker : */