1 /**
2 * collectd - src/memcachec.c
3 * Copyright (C) 2009 Doug MacEachern
4 * Copyright (C) 2006-2009 Florian octo Forster
5 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Doug MacEachern <Doug.MacEachern at hyperic.com>
21 * Florian octo Forster <octo at collectd.org>
22 **/
24 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
28 #include "utils_match.h"
30 #include <libmemcached/memcached.h>
32 /*
33 * Data types
34 */
35 struct web_match_s;
36 typedef struct web_match_s web_match_t;
37 struct web_match_s /* {{{ */
38 {
39 char *regex;
40 char *exclude_regex;
41 int dstype;
42 char *type;
43 char *instance;
45 cu_match_t *match;
47 web_match_t *next;
48 }; /* }}} */
50 struct web_page_s;
51 typedef struct web_page_s web_page_t;
52 struct web_page_s /* {{{ */
53 {
54 char *instance;
56 char *server;
57 char *key;
59 memcached_st *memc;
60 char *buffer;
62 web_match_t *matches;
64 web_page_t *next;
65 }; /* }}} */
67 /*
68 * Global variables;
69 */
70 static web_page_t *pages_g = NULL;
72 /*
73 * Private functions
74 */
75 static void cmc_web_match_free(web_match_t *wm) /* {{{ */
76 {
77 if (wm == NULL)
78 return;
80 sfree(wm->regex);
81 sfree(wm->type);
82 sfree(wm->instance);
83 match_destroy(wm->match);
84 cmc_web_match_free(wm->next);
85 sfree(wm);
86 } /* }}} void cmc_web_match_free */
88 static void cmc_web_page_free(web_page_t *wp) /* {{{ */
89 {
90 if (wp == NULL)
91 return;
93 if (wp->memc != NULL)
94 memcached_free(wp->memc);
95 wp->memc = NULL;
97 sfree(wp->instance);
98 sfree(wp->server);
99 sfree(wp->key);
100 sfree(wp->buffer);
102 cmc_web_match_free(wp->matches);
103 cmc_web_page_free(wp->next);
104 sfree(wp);
105 } /* }}} void cmc_web_page_free */
107 static int cmc_page_init_memc(web_page_t *wp) /* {{{ */
108 {
109 memcached_server_st *server;
111 wp->memc = memcached_create(NULL);
112 if (wp->memc == NULL) {
113 ERROR("memcachec plugin: memcached_create failed.");
114 return (-1);
115 }
117 server = memcached_servers_parse(wp->server);
118 memcached_server_push(wp->memc, server);
119 memcached_server_list_free(server);
121 return (0);
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);
128 return (-1);
129 }
131 sfree(*dest);
132 *dest = strdup(ci->values[0].value.string);
133 if (*dest == NULL)
134 return (-1);
136 return (0);
137 } /* }}} int cmc_config_add_string */
139 static int cmc_config_add_match_dstype(int *dstype_ret, /* {{{ */
140 oconfig_item_t *ci) {
141 int dstype;
143 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
144 WARNING("memcachec plugin: `DSType' needs exactly one string argument.");
145 return (-1);
146 }
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;
158 else
159 dstype = 0;
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;
169 else
170 dstype = 0;
171 } else {
172 dstype = 0;
173 }
175 if (dstype == 0) {
176 WARNING("memcachec plugin: `%s' is not a valid argument to `DSType'.",
177 ci->values[0].value.string);
178 return (-1);
179 }
181 *dstype_ret = dstype;
182 return (0);
183 } /* }}} int cmc_config_add_match_dstype */
185 static int cmc_config_add_match(web_page_t *page, /* {{{ */
186 oconfig_item_t *ci) {
187 web_match_t *match;
188 int status;
190 if (ci->values_num != 0) {
191 WARNING("memcachec plugin: Ignoring arguments for the `Match' block.");
192 }
194 match = calloc(1, sizeof(*match));
195 if (match == NULL) {
196 ERROR("memcachec plugin: calloc failed.");
197 return (-1);
198 }
200 status = 0;
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)
207 status =
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);
215 else {
216 WARNING("memcachec plugin: Option `%s' not allowed here.", child->key);
217 status = -1;
218 }
220 if (status != 0)
221 break;
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.");
227 status = -1;
228 }
230 if (match->type == NULL) {
231 WARNING("memcachec plugin: `Type' missing in `Match' block.");
232 status = -1;
233 }
235 if (match->dstype == 0) {
236 WARNING("memcachec plugin: `DSType' missing in `Match' block.");
237 status = -1;
238 }
240 break;
241 } /* while (status == 0) */
243 if (status != 0) {
244 cmc_web_match_free(match);
245 return (status);
246 }
248 match->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);
253 return (-1);
254 } else {
255 web_match_t *prev;
257 prev = page->matches;
258 while ((prev != NULL) && (prev->next != NULL))
259 prev = prev->next;
261 if (prev == NULL)
262 page->matches = match;
263 else
264 prev->next = match;
265 }
267 return (0);
268 } /* }}} int cmc_config_add_match */
270 static int cmc_config_add_page(oconfig_item_t *ci) /* {{{ */
271 {
272 web_page_t *page;
273 int status;
275 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
276 WARNING(
277 "memcachec plugin: `Page' blocks need exactly one string argument.");
278 return (-1);
279 }
281 page = calloc(1, sizeof(*page));
282 if (page == NULL) {
283 ERROR("memcachec plugin: calloc failed.");
284 return (-1);
285 }
286 page->server = NULL;
287 page->key = NULL;
289 page->instance = strdup(ci->values[0].value.string);
290 if (page->instance == NULL) {
291 ERROR("memcachec plugin: strdup failed.");
292 sfree(page);
293 return (-1);
294 }
296 /* Process all children */
297 status = 0;
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);
308 else {
309 WARNING("memcachec plugin: Option `%s' not allowed here.", child->key);
310 status = -1;
311 }
313 if (status != 0)
314 break;
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.");
321 status = -1;
322 }
324 if (page->key == NULL) {
325 WARNING("memcachec plugin: `Key' missing in `Page' block.");
326 status = -1;
327 }
329 if (page->matches == NULL) {
330 assert(page->instance != NULL);
331 WARNING("memcachec plugin: No (valid) `Match' block "
332 "within `Page' block `%s'.",
333 page->instance);
334 status = -1;
335 }
337 if (status == 0)
338 status = cmc_page_init_memc(page);
340 break;
341 } /* while (status == 0) */
343 if (status != 0) {
344 cmc_web_page_free(page);
345 return (status);
346 }
348 /* Add the new page to the linked list */
349 if (pages_g == NULL)
350 pages_g = page;
351 else {
352 web_page_t *prev;
354 prev = pages_g;
355 while (prev->next != NULL)
356 prev = prev->next;
357 prev->next = page;
358 }
360 return (0);
361 } /* }}} int cmc_config_add_page */
363 static int cmc_config(oconfig_item_t *ci) /* {{{ */
364 {
365 int success;
366 int errors;
367 int status;
369 success = 0;
370 errors = 0;
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);
377 if (status == 0)
378 success++;
379 else
380 errors++;
381 } else {
382 WARNING("memcachec plugin: Option `%s' not allowed here.", child->key);
383 errors++;
384 }
385 }
387 if ((success == 0) && (errors > 0)) {
388 ERROR("memcachec plugin: All statements failed.");
389 return (-1);
390 }
392 return (0);
393 } /* }}} int cmc_config */
395 static int cmc_init(void) /* {{{ */
396 {
397 if (pages_g == NULL) {
398 INFO("memcachec plugin: No pages have been defined.");
399 return (-1);
400 }
401 return (0);
402 } /* }}} int cmc_init */
404 static void cmc_submit(const web_page_t *wp, const web_match_t *wm, /* {{{ */
405 value_t value) {
406 value_list_t vl = VALUE_LIST_INIT;
408 vl.values = &value;
409 vl.values_len = 1;
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) /* {{{ */
419 {
420 memcached_return rc;
421 size_t string_length;
422 uint32_t flags;
423 int status;
425 if (wp->memc == NULL)
426 return (-1);
428 wp->buffer = memcached_get(wp->memc, wp->key, strlen(wp->key), &string_length,
429 &flags, &rc);
430 if (rc != MEMCACHED_SUCCESS) {
431 ERROR("memcachec plugin: memcached_get failed: %s",
432 memcached_strerror(wp->memc, rc));
433 return (-1);
434 }
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);
440 if (status != 0) {
441 WARNING("memcachec plugin: match_apply failed.");
442 continue;
443 }
445 mv = match_get_user_data(wm->match);
446 if (mv == NULL) {
447 WARNING("memcachec plugin: match_get_user_data returned NULL.");
448 continue;
449 }
451 cmc_submit(wp, wm, mv->value);
452 match_value_reset(mv);
453 } /* for (wm = wp->matches; wm != NULL; wm = wm->next) */
455 sfree(wp->buffer);
457 return (0);
458 } /* }}} int cmc_read_page */
460 static int cmc_read(void) /* {{{ */
461 {
462 for (web_page_t *wp = pages_g; wp != NULL; wp = wp->next)
463 cmc_read_page(wp);
465 return (0);
466 } /* }}} int cmc_read */
468 static int cmc_shutdown(void) /* {{{ */
469 {
470 cmc_web_page_free(pages_g);
471 pages_g = NULL;
473 return (0);
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 */
483 /* vim: set sw=2 sts=2 et fdm=marker : */