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 /* {{{ */
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) {
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, /* {{{ */
125 oconfig_item_t *ci) {
126 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
127 WARNING("memcachec plugin: `%s' needs exactly one string argument.", name);
132 *dest = strdup(ci->values[0].value.string);
137 } /* }}} int cmc_config_add_string */
139 static int cmc_config_add_match_dstype(int *dstype_ret, /* {{{ */
140 oconfig_item_t *ci) {
143 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
144 WARNING("memcachec plugin: `DSType' needs exactly one string argument.");
148 if (strncasecmp("Gauge", ci->values[0].value.string, strlen("Gauge")) == 0) {
149 dstype = UTILS_MATCH_DS_TYPE_GAUGE;
150 if (strcasecmp("GaugeAverage", ci->values[0].value.string) == 0)
151 dstype |= UTILS_MATCH_CF_GAUGE_AVERAGE;
152 else if (strcasecmp("GaugeMin", ci->values[0].value.string) == 0)
153 dstype |= UTILS_MATCH_CF_GAUGE_MIN;
154 else if (strcasecmp("GaugeMax", ci->values[0].value.string) == 0)
155 dstype |= UTILS_MATCH_CF_GAUGE_MAX;
156 else if (strcasecmp("GaugeLast", ci->values[0].value.string) == 0)
157 dstype |= UTILS_MATCH_CF_GAUGE_LAST;
160 } else if (strncasecmp("Counter", ci->values[0].value.string,
161 strlen("Counter")) == 0) {
162 dstype = UTILS_MATCH_DS_TYPE_COUNTER;
163 if (strcasecmp("CounterSet", ci->values[0].value.string) == 0)
164 dstype |= UTILS_MATCH_CF_COUNTER_SET;
165 else if (strcasecmp("CounterAdd", ci->values[0].value.string) == 0)
166 dstype |= UTILS_MATCH_CF_COUNTER_ADD;
167 else if (strcasecmp("CounterInc", ci->values[0].value.string) == 0)
168 dstype |= UTILS_MATCH_CF_COUNTER_INC;
176 WARNING("memcachec plugin: `%s' is not a valid argument to `DSType'.",
177 ci->values[0].value.string);
181 *dstype_ret = dstype;
183 } /* }}} int cmc_config_add_match_dstype */
185 static int cmc_config_add_match(web_page_t *page, /* {{{ */
186 oconfig_item_t *ci) {
190 if (ci->values_num != 0) {
191 WARNING("memcachec plugin: Ignoring arguments for the `Match' block.");
194 match = calloc(1, sizeof(*match));
196 ERROR("memcachec plugin: calloc failed.");
201 for (int i = 0; i < ci->children_num; i++) {
202 oconfig_item_t *child = ci->children + i;
204 if (strcasecmp("Regex", child->key) == 0)
205 status = cmc_config_add_string("Regex", &match->regex, child);
206 else if (strcasecmp("ExcludeRegex", child->key) == 0)
208 cmc_config_add_string("ExcludeRegex", &match->exclude_regex, child);
209 else if (strcasecmp("DSType", child->key) == 0)
210 status = cmc_config_add_match_dstype(&match->dstype, child);
211 else if (strcasecmp("Type", child->key) == 0)
212 status = cmc_config_add_string("Type", &match->type, child);
213 else if (strcasecmp("Instance", child->key) == 0)
214 status = cmc_config_add_string("Instance", &match->instance, child);
216 WARNING("memcachec plugin: Option `%s' not allowed here.", child->key);
222 } /* for (i = 0; i < ci->children_num; i++) */
224 while (status == 0) {
225 if (match->regex == NULL) {
226 WARNING("memcachec plugin: `Regex' missing in `Match' block.");
230 if (match->type == NULL) {
231 WARNING("memcachec plugin: `Type' missing in `Match' block.");
235 if (match->dstype == 0) {
236 WARNING("memcachec plugin: `DSType' missing in `Match' block.");
241 } /* while (status == 0) */
244 cmc_web_match_free(match);
249 match_create_simple(match->regex, match->exclude_regex, match->dstype);
250 if (match->match == NULL) {
251 ERROR("memcachec plugin: match_create_simple failed.");
252 cmc_web_match_free(match);
257 prev = page->matches;
258 while ((prev != NULL) && (prev->next != NULL))
262 page->matches = match;
268 } /* }}} int cmc_config_add_match */
270 static int cmc_config_add_page(oconfig_item_t *ci) /* {{{ */
275 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
277 "memcachec plugin: `Page' blocks need exactly one string argument.");
281 page = calloc(1, sizeof(*page));
283 ERROR("memcachec plugin: calloc failed.");
289 page->instance = strdup(ci->values[0].value.string);
290 if (page->instance == NULL) {
291 ERROR("memcachec plugin: strdup failed.");
296 /* Process all children */
298 for (int i = 0; i < ci->children_num; i++) {
299 oconfig_item_t *child = ci->children + i;
301 if (strcasecmp("Server", child->key) == 0)
302 status = cmc_config_add_string("Server", &page->server, child);
303 else if (strcasecmp("Key", child->key) == 0)
304 status = cmc_config_add_string("Key", &page->key, child);
305 else if (strcasecmp("Match", child->key) == 0)
306 /* Be liberal with failing matches => don't set `status'. */
307 cmc_config_add_match(page, child);
309 WARNING("memcachec plugin: Option `%s' not allowed here.", child->key);
315 } /* for (i = 0; i < ci->children_num; i++) */
317 /* Additionial sanity checks and libmemcached initialization. */
318 while (status == 0) {
319 if (page->server == NULL) {
320 WARNING("memcachec plugin: `Server' missing in `Page' block.");
324 if (page->key == NULL) {
325 WARNING("memcachec plugin: `Key' missing in `Page' block.");
329 if (page->matches == NULL) {
330 assert(page->instance != NULL);
331 WARNING("memcachec plugin: No (valid) `Match' block "
332 "within `Page' block `%s'.",
338 status = cmc_page_init_memc(page);
341 } /* while (status == 0) */
344 cmc_web_page_free(page);
348 /* Add the new page to the linked list */
355 while (prev->next != NULL)
361 } /* }}} int cmc_config_add_page */
363 static int cmc_config(oconfig_item_t *ci) /* {{{ */
372 for (int i = 0; i < ci->children_num; i++) {
373 oconfig_item_t *child = ci->children + i;
375 if (strcasecmp("Page", child->key) == 0) {
376 status = cmc_config_add_page(child);
382 WARNING("memcachec plugin: Option `%s' not allowed here.", child->key);
387 if ((success == 0) && (errors > 0)) {
388 ERROR("memcachec plugin: All statements failed.");
393 } /* }}} int cmc_config */
395 static int cmc_init(void) /* {{{ */
397 if (pages_g == NULL) {
398 INFO("memcachec plugin: No pages have been defined.");
402 } /* }}} int cmc_init */
404 static void cmc_submit(const web_page_t *wp, const web_match_t *wm, /* {{{ */
406 value_list_t vl = VALUE_LIST_INIT;
410 sstrncpy(vl.plugin, "memcachec", sizeof(vl.plugin));
411 sstrncpy(vl.plugin_instance, wp->instance, sizeof(vl.plugin_instance));
412 sstrncpy(vl.type, wm->type, sizeof(vl.type));
413 sstrncpy(vl.type_instance, wm->instance, sizeof(vl.type_instance));
415 plugin_dispatch_values(&vl);
416 } /* }}} void cmc_submit */
418 static int cmc_read_page(web_page_t *wp) /* {{{ */
421 size_t string_length;
425 if (wp->memc == NULL)
428 wp->buffer = memcached_get(wp->memc, wp->key, strlen(wp->key), &string_length,
430 if (rc != MEMCACHED_SUCCESS) {
431 ERROR("memcachec plugin: memcached_get failed: %s",
432 memcached_strerror(wp->memc, rc));
436 for (web_match_t *wm = wp->matches; wm != NULL; wm = wm->next) {
437 cu_match_value_t *mv;
439 status = match_apply(wm->match, wp->buffer);
441 WARNING("memcachec plugin: match_apply failed.");
445 mv = match_get_user_data(wm->match);
447 WARNING("memcachec plugin: match_get_user_data returned NULL.");
451 cmc_submit(wp, wm, mv->value);
452 match_value_reset(mv);
453 } /* for (wm = wp->matches; wm != NULL; wm = wm->next) */
458 } /* }}} int cmc_read_page */
460 static int cmc_read(void) /* {{{ */
462 for (web_page_t *wp = pages_g; wp != NULL; wp = wp->next)
466 } /* }}} int cmc_read */
468 static int cmc_shutdown(void) /* {{{ */
470 cmc_web_page_free(pages_g);
474 } /* }}} int cmc_shutdown */
476 void module_register(void) {
477 plugin_register_complex_config("memcachec", cmc_config);
478 plugin_register_init("memcachec", cmc_init);
479 plugin_register_read("memcachec", cmc_read);
480 plugin_register_shutdown("memcachec", cmc_shutdown);
481 } /* void module_register */