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 verplant.org>
27 #include "configfile.h"
28 #include "utils_match.h"
30 #include <libmemcached/memcached.h>
36 typedef struct web_match_s web_match_t;
37 struct web_match_s /* {{{ */
50 typedef struct web_page_s web_page_t;
51 struct web_page_s /* {{{ */
69 static web_page_t *pages_g = NULL;
74 static void cmc_web_match_free (web_match_t *wm) /* {{{ */
82 match_destroy (wm->match);
83 cmc_web_match_free (wm->next);
85 } /* }}} void cmc_web_match_free */
87 static void cmc_web_page_free (web_page_t *wp) /* {{{ */
93 memcached_free(wp->memc);
101 cmc_web_match_free (wp->matches);
102 cmc_web_page_free (wp->next);
104 } /* }}} void cmc_web_page_free */
106 static int cmc_page_init_memc (web_page_t *wp) /* {{{ */
108 memcached_server_st *server;
110 wp->memc = memcached_create(NULL);
111 if (wp->memc == NULL)
113 ERROR ("memcachec plugin: memcached_create failed.");
117 server = memcached_servers_parse (wp->server);
118 memcached_server_push (wp->memc, server);
119 memcached_server_list_free (server);
122 } /* }}} int cmc_page_init_memc */
124 static int cmc_config_add_string (const char *name, char **dest, /* {{{ */
127 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
129 WARNING ("memcachec plugin: `%s' needs exactly one string argument.", name);
134 *dest = strdup (ci->values[0].value.string);
139 } /* }}} int cmc_config_add_string */
141 static int cmc_config_add_match_dstype (int *dstype_ret, /* {{{ */
146 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
148 WARNING ("memcachec plugin: `DSType' needs exactly one string argument.");
152 if (strncasecmp ("Gauge", ci->values[0].value.string,
153 strlen ("Gauge")) == 0)
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;
167 else if (strncasecmp ("Counter", ci->values[0].value.string,
168 strlen ("Counter")) == 0)
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;
187 WARNING ("memcachec plugin: `%s' is not a valid argument to `DSType'.",
188 ci->values[0].value.string);
192 *dstype_ret = dstype;
194 } /* }}} int cmc_config_add_match_dstype */
196 static int cmc_config_add_match (web_page_t *page, /* {{{ */
203 if (ci->values_num != 0)
205 WARNING ("memcachec plugin: Ignoring arguments for the `Match' block.");
208 match = (web_match_t *) malloc (sizeof (*match));
211 ERROR ("memcachec plugin: malloc failed.");
214 memset (match, 0, sizeof (*match));
217 for (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 ("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);
231 WARNING ("memcachec plugin: Option `%s' not allowed here.", child->key);
237 } /* for (i = 0; i < ci->children_num; i++) */
241 if (match->regex == NULL)
243 WARNING ("memcachec plugin: `Regex' missing in `Match' block.");
247 if (match->type == NULL)
249 WARNING ("memcachec plugin: `Type' missing in `Match' block.");
253 if (match->dstype == 0)
255 WARNING ("memcachec plugin: `DSType' missing in `Match' block.");
260 } /* while (status == 0) */
265 match->match = match_create_simple (match->regex, match->dstype);
266 if (match->match == NULL)
268 ERROR ("memcachec plugin: tail_match_add_match_simple failed.");
269 cmc_web_match_free (match);
276 prev = page->matches;
277 while ((prev != NULL) && (prev->next != NULL))
281 page->matches = match;
287 } /* }}} int cmc_config_add_match */
289 static int cmc_config_add_page (oconfig_item_t *ci) /* {{{ */
295 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
297 WARNING ("memcachec plugin: `Page' blocks need exactly one string argument.");
301 page = (web_page_t *) malloc (sizeof (*page));
304 ERROR ("memcachec plugin: malloc failed.");
307 memset (page, 0, sizeof (*page));
311 page->instance = strdup (ci->values[0].value.string);
312 if (page->instance == NULL)
314 ERROR ("memcachec plugin: strdup failed.");
319 /* Process all children */
321 for (i = 0; i < ci->children_num; i++)
323 oconfig_item_t *child = ci->children + i;
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);
334 WARNING ("memcachec plugin: Option `%s' not allowed here.", child->key);
340 } /* for (i = 0; i < ci->children_num; i++) */
342 /* Additionial sanity checks and libmemcached initialization. */
345 if (page->server == NULL)
347 WARNING ("memcachec plugin: `Server' missing in `Page' block.");
351 if (page->key == NULL)
353 WARNING ("memcachec plugin: `Key' missing in `Page' block.");
357 if (page->matches == NULL)
359 assert (page->instance != NULL);
360 WARNING ("memcachec plugin: No (valid) `Match' block "
361 "within `Page' block `%s'.", page->instance);
366 status = cmc_page_init_memc (page);
369 } /* while (status == 0) */
373 cmc_web_page_free (page);
377 /* Add the new page to the linked list */
385 while ((prev != NULL) && (prev->next != NULL))
391 } /* }}} int cmc_config_add_page */
393 static int cmc_config (oconfig_item_t *ci) /* {{{ */
403 for (i = 0; i < ci->children_num; i++)
405 oconfig_item_t *child = ci->children + i;
407 if (strcasecmp ("Page", child->key) == 0)
409 status = cmc_config_add_page (child);
417 WARNING ("memcachec plugin: Option `%s' not allowed here.", child->key);
422 if ((success == 0) && (errors > 0))
424 ERROR ("memcachec plugin: All statements failed.");
429 } /* }}} int cmc_config */
431 static int cmc_init (void) /* {{{ */
435 INFO ("memcachec plugin: No pages have been defined.");
439 } /* }}} int cmc_init */
441 static void cmc_submit (const web_page_t *wp, const web_match_t *wm, /* {{{ */
442 const cu_match_value_t *mv)
445 value_list_t vl = VALUE_LIST_INIT;
447 values[0] = mv->value;
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));
458 plugin_dispatch_values (&vl);
459 } /* }}} void cmc_submit */
461 static int cmc_read_page (web_page_t *wp) /* {{{ */
465 size_t string_length;
469 if (wp->memc == NULL)
472 wp->buffer = memcached_get (wp->memc, wp->key, strlen (wp->key),
473 &string_length, &flags, &rc);
474 if (rc != MEMCACHED_SUCCESS)
476 ERROR ("memcachec plugin: memcached_get failed: %s",
477 memcached_strerror (wp->memc, rc));
481 for (wm = wp->matches; wm != NULL; wm = wm->next)
483 cu_match_value_t *mv;
485 status = match_apply (wm->match, wp->buffer);
488 WARNING ("memcachec plugin: match_apply failed.");
492 mv = match_get_user_data (wm->match);
495 WARNING ("memcachec plugin: match_get_user_data returned NULL.");
499 cmc_submit (wp, wm, mv);
500 } /* for (wm = wp->matches; wm != NULL; wm = wm->next) */
505 } /* }}} int cmc_read_page */
507 static int cmc_read (void) /* {{{ */
511 for (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 : */