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 "utils/common/common.h"
28 #include "utils/match/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_match_dstype(int *dstype_ret, /* {{{ */
127 oconfig_item_t *ci) {
130 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
131 WARNING("memcachec plugin: `DSType' needs exactly one string argument.");
135 if (strncasecmp("Gauge", ci->values[0].value.string, strlen("Gauge")) == 0) {
136 dstype = UTILS_MATCH_DS_TYPE_GAUGE;
137 if (strcasecmp("GaugeAverage", ci->values[0].value.string) == 0)
138 dstype |= UTILS_MATCH_CF_GAUGE_AVERAGE;
139 else if (strcasecmp("GaugeMin", ci->values[0].value.string) == 0)
140 dstype |= UTILS_MATCH_CF_GAUGE_MIN;
141 else if (strcasecmp("GaugeMax", ci->values[0].value.string) == 0)
142 dstype |= UTILS_MATCH_CF_GAUGE_MAX;
143 else if (strcasecmp("GaugeLast", ci->values[0].value.string) == 0)
144 dstype |= UTILS_MATCH_CF_GAUGE_LAST;
147 } else if (strncasecmp("Counter", ci->values[0].value.string,
148 strlen("Counter")) == 0) {
149 dstype = UTILS_MATCH_DS_TYPE_COUNTER;
150 if (strcasecmp("CounterSet", ci->values[0].value.string) == 0)
151 dstype |= UTILS_MATCH_CF_COUNTER_SET;
152 else if (strcasecmp("CounterAdd", ci->values[0].value.string) == 0)
153 dstype |= UTILS_MATCH_CF_COUNTER_ADD;
154 else if (strcasecmp("CounterInc", ci->values[0].value.string) == 0)
155 dstype |= UTILS_MATCH_CF_COUNTER_INC;
163 WARNING("memcachec plugin: `%s' is not a valid argument to `DSType'.",
164 ci->values[0].value.string);
168 *dstype_ret = dstype;
170 } /* }}} int cmc_config_add_match_dstype */
172 static int cmc_config_add_match(web_page_t *page, /* {{{ */
173 oconfig_item_t *ci) {
177 if (ci->values_num != 0) {
178 WARNING("memcachec plugin: Ignoring arguments for the `Match' block.");
181 match = calloc(1, sizeof(*match));
183 ERROR("memcachec plugin: calloc failed.");
188 for (int i = 0; i < ci->children_num; i++) {
189 oconfig_item_t *child = ci->children + i;
191 if (strcasecmp("Regex", child->key) == 0)
192 status = cf_util_get_string(child, &match->regex);
193 else if (strcasecmp("ExcludeRegex", child->key) == 0)
194 status = cf_util_get_string(child, &match->exclude_regex);
195 else if (strcasecmp("DSType", child->key) == 0)
196 status = cmc_config_add_match_dstype(&match->dstype, child);
197 else if (strcasecmp("Type", child->key) == 0)
198 status = cf_util_get_string(child, &match->type);
199 else if (strcasecmp("Instance", child->key) == 0)
200 status = cf_util_get_string(child, &match->instance);
202 WARNING("memcachec plugin: Option `%s' not allowed here.", child->key);
208 } /* for (i = 0; i < ci->children_num; i++) */
210 while (status == 0) {
211 if (match->regex == NULL) {
212 WARNING("memcachec plugin: `Regex' missing in `Match' block.");
216 if (match->type == NULL) {
217 WARNING("memcachec plugin: `Type' missing in `Match' block.");
221 if (match->dstype == 0) {
222 WARNING("memcachec plugin: `DSType' missing in `Match' block.");
227 } /* while (status == 0) */
230 cmc_web_match_free(match);
235 match_create_simple(match->regex, match->exclude_regex, match->dstype);
236 if (match->match == NULL) {
237 ERROR("memcachec plugin: match_create_simple failed.");
238 cmc_web_match_free(match);
243 prev = page->matches;
244 while ((prev != NULL) && (prev->next != NULL))
248 page->matches = match;
254 } /* }}} int cmc_config_add_match */
256 static int cmc_config_add_page(oconfig_item_t *ci) /* {{{ */
261 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
263 "memcachec plugin: `Page' blocks need exactly one string argument.");
267 page = calloc(1, sizeof(*page));
269 ERROR("memcachec plugin: calloc failed.");
275 page->instance = strdup(ci->values[0].value.string);
276 if (page->instance == NULL) {
277 ERROR("memcachec plugin: strdup failed.");
282 /* Process all children */
284 for (int i = 0; i < ci->children_num; i++) {
285 oconfig_item_t *child = ci->children + i;
287 if (strcasecmp("Server", child->key) == 0)
288 status = cf_util_get_string(child, &page->server);
289 else if (strcasecmp("Key", child->key) == 0)
290 status = cf_util_get_string(child, &page->key);
291 else if (strcasecmp("Plugin", child->key) == 0)
292 status = cf_util_get_string(child, &page->plugin_name);
293 else if (strcasecmp("Match", child->key) == 0)
294 /* Be liberal with failing matches => don't set `status'. */
295 cmc_config_add_match(page, child);
297 WARNING("memcachec plugin: Option `%s' not allowed here.", child->key);
303 } /* for (i = 0; i < ci->children_num; i++) */
305 /* Additionial sanity checks and libmemcached initialization. */
306 while (status == 0) {
307 if (page->server == NULL) {
308 WARNING("memcachec plugin: `Server' missing in `Page' block.");
312 if (page->key == NULL) {
313 WARNING("memcachec plugin: `Key' missing in `Page' block.");
317 if (page->matches == NULL) {
318 assert(page->instance != NULL);
319 WARNING("memcachec plugin: No (valid) `Match' block "
320 "within `Page' block `%s'.",
326 status = cmc_page_init_memc(page);
329 } /* while (status == 0) */
332 cmc_web_page_free(page);
336 /* Add the new page to the linked list */
343 while (prev->next != NULL)
349 } /* }}} int cmc_config_add_page */
351 static int cmc_config(oconfig_item_t *ci) /* {{{ */
360 for (int i = 0; i < ci->children_num; i++) {
361 oconfig_item_t *child = ci->children + i;
363 if (strcasecmp("Page", child->key) == 0) {
364 status = cmc_config_add_page(child);
370 WARNING("memcachec plugin: Option `%s' not allowed here.", child->key);
375 if ((success == 0) && (errors > 0)) {
376 ERROR("memcachec plugin: All statements failed.");
381 } /* }}} int cmc_config */
383 static int cmc_init(void) /* {{{ */
385 if (pages_g == NULL) {
386 INFO("memcachec plugin: No pages have been defined.");
390 } /* }}} int cmc_init */
392 static void cmc_submit(const web_page_t *wp, const web_match_t *wm, /* {{{ */
394 value_list_t vl = VALUE_LIST_INIT;
398 sstrncpy(vl.plugin, (wp->plugin_name != NULL) ? wp->plugin_name : "memcachec",
400 sstrncpy(vl.plugin_instance, wp->instance, sizeof(vl.plugin_instance));
401 sstrncpy(vl.type, wm->type, sizeof(vl.type));
402 sstrncpy(vl.type_instance, wm->instance, sizeof(vl.type_instance));
404 plugin_dispatch_values(&vl);
405 } /* }}} void cmc_submit */
407 static int cmc_read_page(web_page_t *wp) /* {{{ */
410 size_t string_length;
414 if (wp->memc == NULL)
417 wp->buffer = memcached_get(wp->memc, wp->key, strlen(wp->key), &string_length,
419 if (rc != MEMCACHED_SUCCESS) {
420 ERROR("memcachec plugin: memcached_get failed: %s",
421 memcached_strerror(wp->memc, rc));
425 for (web_match_t *wm = wp->matches; wm != NULL; wm = wm->next) {
426 cu_match_value_t *mv;
428 status = match_apply(wm->match, wp->buffer);
430 WARNING("memcachec plugin: match_apply failed.");
434 mv = match_get_user_data(wm->match);
436 WARNING("memcachec plugin: match_get_user_data returned NULL.");
440 cmc_submit(wp, wm, mv->value);
441 match_value_reset(mv);
442 } /* for (wm = wp->matches; wm != NULL; wm = wm->next) */
447 } /* }}} int cmc_read_page */
449 static int cmc_read(void) /* {{{ */
451 for (web_page_t *wp = pages_g; wp != NULL; wp = wp->next)
455 } /* }}} int cmc_read */
457 static int cmc_shutdown(void) /* {{{ */
459 cmc_web_page_free(pages_g);
463 } /* }}} int cmc_shutdown */
465 void module_register(void) {
466 plugin_register_complex_config("memcachec", cmc_config);
467 plugin_register_init("memcachec", cmc_init);
468 plugin_register_read("memcachec", cmc_read);
469 plugin_register_shutdown("memcachec", cmc_shutdown);
470 } /* void module_register */