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 "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 /* {{{ */
71 static web_page_t *pages_g;
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);
98 sfree(wp->plugin_name);
104 cmc_web_match_free(wp->matches);
105 cmc_web_page_free(wp->next);
107 } /* }}} void cmc_web_page_free */
109 static int cmc_page_init_memc(web_page_t *wp) /* {{{ */
111 memcached_server_st *server;
113 wp->memc = memcached_create(NULL);
114 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, /* {{{ */
127 oconfig_item_t *ci) {
128 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, /* {{{ */
142 oconfig_item_t *ci) {
145 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
146 WARNING("memcachec plugin: `DSType' needs exactly one string argument.");
150 if (strncasecmp("Gauge", ci->values[0].value.string, strlen("Gauge")) == 0) {
151 dstype = UTILS_MATCH_DS_TYPE_GAUGE;
152 if (strcasecmp("GaugeAverage", ci->values[0].value.string) == 0)
153 dstype |= UTILS_MATCH_CF_GAUGE_AVERAGE;
154 else if (strcasecmp("GaugeMin", ci->values[0].value.string) == 0)
155 dstype |= UTILS_MATCH_CF_GAUGE_MIN;
156 else if (strcasecmp("GaugeMax", ci->values[0].value.string) == 0)
157 dstype |= UTILS_MATCH_CF_GAUGE_MAX;
158 else if (strcasecmp("GaugeLast", ci->values[0].value.string) == 0)
159 dstype |= UTILS_MATCH_CF_GAUGE_LAST;
162 } else if (strncasecmp("Counter", ci->values[0].value.string,
163 strlen("Counter")) == 0) {
164 dstype = UTILS_MATCH_DS_TYPE_COUNTER;
165 if (strcasecmp("CounterSet", ci->values[0].value.string) == 0)
166 dstype |= UTILS_MATCH_CF_COUNTER_SET;
167 else if (strcasecmp("CounterAdd", ci->values[0].value.string) == 0)
168 dstype |= UTILS_MATCH_CF_COUNTER_ADD;
169 else if (strcasecmp("CounterInc", ci->values[0].value.string) == 0)
170 dstype |= UTILS_MATCH_CF_COUNTER_INC;
178 WARNING("memcachec plugin: `%s' is not a valid argument to `DSType'.",
179 ci->values[0].value.string);
183 *dstype_ret = dstype;
185 } /* }}} int cmc_config_add_match_dstype */
187 static int cmc_config_add_match(web_page_t *page, /* {{{ */
188 oconfig_item_t *ci) {
192 if (ci->values_num != 0) {
193 WARNING("memcachec plugin: Ignoring arguments for the `Match' block.");
196 match = calloc(1, sizeof(*match));
198 ERROR("memcachec plugin: calloc failed.");
203 for (int i = 0; i < ci->children_num; i++) {
204 oconfig_item_t *child = ci->children + i;
206 if (strcasecmp("Regex", child->key) == 0)
207 status = cmc_config_add_string("Regex", &match->regex, child);
208 else if (strcasecmp("ExcludeRegex", child->key) == 0)
210 cmc_config_add_string("ExcludeRegex", &match->exclude_regex, child);
211 else if (strcasecmp("DSType", child->key) == 0)
212 status = cmc_config_add_match_dstype(&match->dstype, child);
213 else if (strcasecmp("Type", child->key) == 0)
214 status = cmc_config_add_string("Type", &match->type, child);
215 else if (strcasecmp("Instance", child->key) == 0)
216 status = cmc_config_add_string("Instance", &match->instance, child);
218 WARNING("memcachec plugin: Option `%s' not allowed here.", child->key);
224 } /* for (i = 0; i < ci->children_num; i++) */
226 while (status == 0) {
227 if (match->regex == NULL) {
228 WARNING("memcachec plugin: `Regex' missing in `Match' block.");
232 if (match->type == NULL) {
233 WARNING("memcachec plugin: `Type' missing in `Match' block.");
237 if (match->dstype == 0) {
238 WARNING("memcachec plugin: `DSType' missing in `Match' block.");
243 } /* while (status == 0) */
246 cmc_web_match_free(match);
251 match_create_simple(match->regex, match->exclude_regex, match->dstype);
252 if (match->match == NULL) {
253 ERROR("memcachec plugin: match_create_simple failed.");
254 cmc_web_match_free(match);
259 prev = page->matches;
260 while ((prev != NULL) && (prev->next != NULL))
264 page->matches = match;
270 } /* }}} int cmc_config_add_match */
272 static int cmc_config_add_page(oconfig_item_t *ci) /* {{{ */
277 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
279 "memcachec plugin: `Page' blocks need exactly one string argument.");
283 page = calloc(1, sizeof(*page));
285 ERROR("memcachec plugin: calloc failed.");
291 page->instance = strdup(ci->values[0].value.string);
292 if (page->instance == NULL) {
293 ERROR("memcachec plugin: strdup failed.");
298 /* Process all children */
300 for (int i = 0; i < ci->children_num; i++) {
301 oconfig_item_t *child = ci->children + i;
303 if (strcasecmp("Server", child->key) == 0)
304 status = cmc_config_add_string("Server", &page->server, child);
305 else if (strcasecmp("Key", child->key) == 0)
306 status = cmc_config_add_string("Key", &page->key, child);
307 else if (strcasecmp("Plugin", child->key) == 0)
308 status = cmc_config_add_string("Plugin", &page->plugin_name, child);
309 else if (strcasecmp("Match", child->key) == 0)
310 /* Be liberal with failing matches => don't set `status'. */
311 cmc_config_add_match(page, child);
313 WARNING("memcachec plugin: Option `%s' not allowed here.", child->key);
319 } /* for (i = 0; i < ci->children_num; i++) */
321 /* Additionial sanity checks and libmemcached initialization. */
322 while (status == 0) {
323 if (page->server == NULL) {
324 WARNING("memcachec plugin: `Server' missing in `Page' block.");
328 if (page->key == NULL) {
329 WARNING("memcachec plugin: `Key' missing in `Page' block.");
333 if (page->matches == NULL) {
334 assert(page->instance != NULL);
335 WARNING("memcachec plugin: No (valid) `Match' block "
336 "within `Page' block `%s'.",
342 status = cmc_page_init_memc(page);
345 } /* while (status == 0) */
348 cmc_web_page_free(page);
352 /* Add the new page to the linked list */
359 while (prev->next != NULL)
365 } /* }}} int cmc_config_add_page */
367 static int cmc_config(oconfig_item_t *ci) /* {{{ */
376 for (int i = 0; i < ci->children_num; i++) {
377 oconfig_item_t *child = ci->children + i;
379 if (strcasecmp("Page", child->key) == 0) {
380 status = cmc_config_add_page(child);
386 WARNING("memcachec plugin: Option `%s' not allowed here.", child->key);
391 if ((success == 0) && (errors > 0)) {
392 ERROR("memcachec plugin: All statements failed.");
397 } /* }}} int cmc_config */
399 static int cmc_init(void) /* {{{ */
401 if (pages_g == NULL) {
402 INFO("memcachec plugin: No pages have been defined.");
406 } /* }}} int cmc_init */
408 static void cmc_submit(const web_page_t *wp, const web_match_t *wm, /* {{{ */
410 value_list_t vl = VALUE_LIST_INIT;
414 sstrncpy(vl.plugin, (wp->plugin_name != NULL) ? wp->plugin_name : "memcachec",
416 sstrncpy(vl.plugin_instance, wp->instance, sizeof(vl.plugin_instance));
417 sstrncpy(vl.type, wm->type, sizeof(vl.type));
418 sstrncpy(vl.type_instance, wm->instance, sizeof(vl.type_instance));
420 plugin_dispatch_values(&vl);
421 } /* }}} void cmc_submit */
423 static int cmc_read_page(web_page_t *wp) /* {{{ */
426 size_t string_length;
430 if (wp->memc == NULL)
433 wp->buffer = memcached_get(wp->memc, wp->key, strlen(wp->key), &string_length,
435 if (rc != MEMCACHED_SUCCESS) {
436 ERROR("memcachec plugin: memcached_get failed: %s",
437 memcached_strerror(wp->memc, rc));
441 for (web_match_t *wm = wp->matches; wm != NULL; wm = wm->next) {
442 cu_match_value_t *mv;
444 status = match_apply(wm->match, wp->buffer);
446 WARNING("memcachec plugin: match_apply failed.");
450 mv = match_get_user_data(wm->match);
452 WARNING("memcachec plugin: match_get_user_data returned NULL.");
456 cmc_submit(wp, wm, mv->value);
457 match_value_reset(mv);
458 } /* for (wm = wp->matches; wm != NULL; wm = wm->next) */
463 } /* }}} int cmc_read_page */
465 static int cmc_read(void) /* {{{ */
467 for (web_page_t *wp = pages_g; wp != NULL; wp = wp->next)
471 } /* }}} int cmc_read */
473 static int cmc_shutdown(void) /* {{{ */
475 cmc_web_page_free(pages_g);
479 } /* }}} int cmc_shutdown */
481 void module_register(void) {
482 plugin_register_complex_config("memcachec", cmc_config);
483 plugin_register_init("memcachec", cmc_init);
484 plugin_register_read("memcachec", cmc_read);
485 plugin_register_shutdown("memcachec", cmc_shutdown);
486 } /* void module_register */