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, /* {{{ */
205 if (ci->values_num != 0)
207 WARNING ("memcachec plugin: Ignoring arguments for the `Match' block.");
210 match = calloc (1, sizeof (*match));
213 ERROR ("memcachec plugin: calloc failed.");
218 for (i = 0; i < ci->children_num; i++)
220 oconfig_item_t *child = ci->children + i;
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);
234 WARNING ("memcachec plugin: Option `%s' not allowed here.", child->key);
240 } /* for (i = 0; i < ci->children_num; i++) */
244 if (match->regex == NULL)
246 WARNING ("memcachec plugin: `Regex' missing in `Match' block.");
250 if (match->type == NULL)
252 WARNING ("memcachec plugin: `Type' missing in `Match' block.");
256 if (match->dstype == 0)
258 WARNING ("memcachec plugin: `DSType' missing in `Match' block.");
263 } /* while (status == 0) */
267 cmc_web_match_free (match);
271 match->match = match_create_simple (match->regex, match->exclude_regex,
273 if (match->match == NULL)
275 ERROR ("memcachec plugin: match_create_simple failed.");
276 cmc_web_match_free (match);
283 prev = page->matches;
284 while ((prev != NULL) && (prev->next != NULL))
288 page->matches = match;
294 } /* }}} int cmc_config_add_match */
296 static int cmc_config_add_page (oconfig_item_t *ci) /* {{{ */
302 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
304 WARNING ("memcachec plugin: `Page' blocks need exactly one string argument.");
308 page = calloc (1, sizeof (*page));
311 ERROR ("memcachec plugin: calloc failed.");
317 page->instance = strdup (ci->values[0].value.string);
318 if (page->instance == NULL)
320 ERROR ("memcachec plugin: strdup failed.");
325 /* Process all children */
327 for (i = 0; i < ci->children_num; i++)
329 oconfig_item_t *child = ci->children + i;
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);
340 WARNING ("memcachec plugin: Option `%s' not allowed here.", child->key);
346 } /* for (i = 0; i < ci->children_num; i++) */
348 /* Additionial sanity checks and libmemcached initialization. */
351 if (page->server == NULL)
353 WARNING ("memcachec plugin: `Server' missing in `Page' block.");
357 if (page->key == NULL)
359 WARNING ("memcachec plugin: `Key' missing in `Page' block.");
363 if (page->matches == NULL)
365 assert (page->instance != NULL);
366 WARNING ("memcachec plugin: No (valid) `Match' block "
367 "within `Page' block `%s'.", page->instance);
372 status = cmc_page_init_memc (page);
375 } /* while (status == 0) */
379 cmc_web_page_free (page);
383 /* Add the new page to the linked list */
391 while (prev->next != NULL)
397 } /* }}} int cmc_config_add_page */
399 static int cmc_config (oconfig_item_t *ci) /* {{{ */
409 for (i = 0; i < ci->children_num; i++)
411 oconfig_item_t *child = ci->children + i;
413 if (strcasecmp ("Page", child->key) == 0)
415 status = cmc_config_add_page (child);
423 WARNING ("memcachec plugin: Option `%s' not allowed here.", child->key);
428 if ((success == 0) && (errors > 0))
430 ERROR ("memcachec plugin: All statements failed.");
435 } /* }}} int cmc_config */
437 static int cmc_init (void) /* {{{ */
441 INFO ("memcachec plugin: No pages have been defined.");
445 } /* }}} int cmc_init */
447 static void cmc_submit (const web_page_t *wp, const web_match_t *wm, /* {{{ */
448 const cu_match_value_t *mv)
451 value_list_t vl = VALUE_LIST_INIT;
453 values[0] = mv->value;
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));
463 plugin_dispatch_values (&vl);
464 } /* }}} void cmc_submit */
466 static int cmc_read_page (web_page_t *wp) /* {{{ */
470 size_t string_length;
474 if (wp->memc == NULL)
477 wp->buffer = memcached_get (wp->memc, wp->key, strlen (wp->key),
478 &string_length, &flags, &rc);
479 if (rc != MEMCACHED_SUCCESS)
481 ERROR ("memcachec plugin: memcached_get failed: %s",
482 memcached_strerror (wp->memc, rc));
486 for (wm = wp->matches; wm != NULL; wm = wm->next)
488 cu_match_value_t *mv;
490 status = match_apply (wm->match, wp->buffer);
493 WARNING ("memcachec plugin: match_apply failed.");
497 mv = match_get_user_data (wm->match);
500 WARNING ("memcachec plugin: match_get_user_data returned NULL.");
504 cmc_submit (wp, wm, mv);
505 match_value_reset (mv);
506 } /* for (wm = wp->matches; wm != NULL; wm = wm->next) */
511 } /* }}} int cmc_read_page */
513 static int cmc_read (void) /* {{{ */
517 for (wp = pages_g; wp != NULL; wp = wp->next)
521 } /* }}} int cmc_read */
523 static int cmc_shutdown (void) /* {{{ */
525 cmc_web_page_free (pages_g);
529 } /* }}} int cmc_shutdown */
531 void module_register (void)
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 */
539 /* vim: set sw=2 sts=2 et fdm=marker : */