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>
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 /* {{{ */
51 typedef struct web_page_s web_page_t;
52 struct web_page_s /* {{{ */
70 static web_page_t *pages_g = NULL;
75 static void cmc_web_match_free (web_match_t *wm) /* {{{ */
83 match_destroy (wm->match);
84 cmc_web_match_free (wm->next);
86 } /* }}} void cmc_web_match_free */
88 static void cmc_web_page_free (web_page_t *wp) /* {{{ */
94 memcached_free(wp->memc);
102 cmc_web_match_free (wp->matches);
103 cmc_web_page_free (wp->next);
105 } /* }}} void cmc_web_page_free */
107 static int cmc_page_init_memc (web_page_t *wp) /* {{{ */
109 memcached_server_st *server;
111 wp->memc = memcached_create(NULL);
112 if (wp->memc == NULL)
114 ERROR ("memcachec plugin: memcached_create failed.");
118 server = memcached_servers_parse (wp->server);
119 memcached_server_push (wp->memc, server);
120 memcached_server_list_free (server);
123 } /* }}} int cmc_page_init_memc */
125 static int cmc_config_add_string (const char *name, char **dest, /* {{{ */
128 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
130 WARNING ("memcachec plugin: `%s' needs exactly one string argument.", name);
135 *dest = strdup (ci->values[0].value.string);
140 } /* }}} int cmc_config_add_string */
142 static int cmc_config_add_match_dstype (int *dstype_ret, /* {{{ */
147 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
149 WARNING ("memcachec plugin: `DSType' needs exactly one string argument.");
153 if (strncasecmp ("Gauge", ci->values[0].value.string,
154 strlen ("Gauge")) == 0)
156 dstype = UTILS_MATCH_DS_TYPE_GAUGE;
157 if (strcasecmp ("GaugeAverage", ci->values[0].value.string) == 0)
158 dstype |= UTILS_MATCH_CF_GAUGE_AVERAGE;
159 else if (strcasecmp ("GaugeMin", ci->values[0].value.string) == 0)
160 dstype |= UTILS_MATCH_CF_GAUGE_MIN;
161 else if (strcasecmp ("GaugeMax", ci->values[0].value.string) == 0)
162 dstype |= UTILS_MATCH_CF_GAUGE_MAX;
163 else if (strcasecmp ("GaugeLast", ci->values[0].value.string) == 0)
164 dstype |= UTILS_MATCH_CF_GAUGE_LAST;
168 else if (strncasecmp ("Counter", ci->values[0].value.string,
169 strlen ("Counter")) == 0)
171 dstype = UTILS_MATCH_DS_TYPE_COUNTER;
172 if (strcasecmp ("CounterSet", ci->values[0].value.string) == 0)
173 dstype |= UTILS_MATCH_CF_COUNTER_SET;
174 else if (strcasecmp ("CounterAdd", ci->values[0].value.string) == 0)
175 dstype |= UTILS_MATCH_CF_COUNTER_ADD;
176 else if (strcasecmp ("CounterInc", ci->values[0].value.string) == 0)
177 dstype |= UTILS_MATCH_CF_COUNTER_INC;
188 WARNING ("memcachec plugin: `%s' is not a valid argument to `DSType'.",
189 ci->values[0].value.string);
193 *dstype_ret = dstype;
195 } /* }}} int cmc_config_add_match_dstype */
197 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 = (web_match_t *) malloc (sizeof (*match));
212 ERROR ("memcachec plugin: malloc failed.");
215 memset (match, 0, sizeof (*match));
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: tail_match_add_match_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 = (web_page_t *) malloc (sizeof (*page));
311 ERROR ("memcachec plugin: malloc failed.");
314 memset (page, 0, sizeof (*page));
318 page->instance = strdup (ci->values[0].value.string);
319 if (page->instance == NULL)
321 ERROR ("memcachec plugin: strdup failed.");
326 /* Process all children */
328 for (i = 0; i < ci->children_num; i++)
330 oconfig_item_t *child = ci->children + i;
332 if (strcasecmp ("Server", child->key) == 0)
333 status = cmc_config_add_string ("Server", &page->server, child);
334 else if (strcasecmp ("Key", child->key) == 0)
335 status = cmc_config_add_string ("Key", &page->key, child);
336 else if (strcasecmp ("Match", child->key) == 0)
337 /* Be liberal with failing matches => don't set `status'. */
338 cmc_config_add_match (page, child);
341 WARNING ("memcachec plugin: Option `%s' not allowed here.", child->key);
347 } /* for (i = 0; i < ci->children_num; i++) */
349 /* Additionial sanity checks and libmemcached initialization. */
352 if (page->server == NULL)
354 WARNING ("memcachec plugin: `Server' missing in `Page' block.");
358 if (page->key == NULL)
360 WARNING ("memcachec plugin: `Key' missing in `Page' block.");
364 if (page->matches == NULL)
366 assert (page->instance != NULL);
367 WARNING ("memcachec plugin: No (valid) `Match' block "
368 "within `Page' block `%s'.", page->instance);
373 status = cmc_page_init_memc (page);
376 } /* while (status == 0) */
380 cmc_web_page_free (page);
384 /* Add the new page to the linked list */
392 while ((prev != NULL) && (prev->next != NULL))
398 } /* }}} int cmc_config_add_page */
400 static int cmc_config (oconfig_item_t *ci) /* {{{ */
410 for (i = 0; i < ci->children_num; i++)
412 oconfig_item_t *child = ci->children + i;
414 if (strcasecmp ("Page", child->key) == 0)
416 status = cmc_config_add_page (child);
424 WARNING ("memcachec plugin: Option `%s' not allowed here.", child->key);
429 if ((success == 0) && (errors > 0))
431 ERROR ("memcachec plugin: All statements failed.");
436 } /* }}} int cmc_config */
438 static int cmc_init (void) /* {{{ */
442 INFO ("memcachec plugin: No pages have been defined.");
446 } /* }}} int cmc_init */
448 static void cmc_submit (const web_page_t *wp, const web_match_t *wm, /* {{{ */
449 const cu_match_value_t *mv)
452 value_list_t vl = VALUE_LIST_INIT;
454 values[0] = mv->value;
458 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
459 sstrncpy (vl.plugin, "memcachec", sizeof (vl.plugin));
460 sstrncpy (vl.plugin_instance, wp->instance, sizeof (vl.plugin_instance));
461 sstrncpy (vl.type, wm->type, sizeof (vl.type));
462 sstrncpy (vl.type_instance, wm->instance, sizeof (vl.type_instance));
464 plugin_dispatch_values (&vl);
465 } /* }}} void cmc_submit */
467 static int cmc_read_page (web_page_t *wp) /* {{{ */
471 size_t string_length;
475 if (wp->memc == NULL)
478 wp->buffer = memcached_get (wp->memc, wp->key, strlen (wp->key),
479 &string_length, &flags, &rc);
480 if (rc != MEMCACHED_SUCCESS)
482 ERROR ("memcachec plugin: memcached_get failed: %s",
483 memcached_strerror (wp->memc, rc));
487 for (wm = wp->matches; wm != NULL; wm = wm->next)
489 cu_match_value_t *mv;
491 status = match_apply (wm->match, wp->buffer);
494 WARNING ("memcachec plugin: match_apply failed.");
498 mv = match_get_user_data (wm->match);
501 WARNING ("memcachec plugin: match_get_user_data returned NULL.");
505 cmc_submit (wp, wm, mv);
506 match_value_reset (mv);
507 } /* for (wm = wp->matches; wm != NULL; wm = wm->next) */
512 } /* }}} int cmc_read_page */
514 static int cmc_read (void) /* {{{ */
518 for (wp = pages_g; wp != NULL; wp = wp->next)
522 } /* }}} int cmc_read */
524 static int cmc_shutdown (void) /* {{{ */
526 cmc_web_page_free (pages_g);
530 } /* }}} int cmc_shutdown */
532 void module_register (void)
534 plugin_register_complex_config ("memcachec", cmc_config);
535 plugin_register_init ("memcachec", cmc_init);
536 plugin_register_read ("memcachec", cmc_read);
537 plugin_register_shutdown ("memcachec", cmc_shutdown);
538 } /* void module_register */
540 /* vim: set sw=2 sts=2 et fdm=marker : */