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 *plugin_name;
55 char *instance;
57 char *server;
58 char *key;
60 memcached_st *memc;
61 char *buffer;
63 web_match_t *matches;
65 web_page_t *next;
66 }; /* }}} */
68 /*
69 * Global variables;
70 */
71 static web_page_t *pages_g = NULL;
73 /*
74 * Private functions
75 */
76 static void cmc_web_match_free(web_match_t *wm) /* {{{ */
77 {
78 if (wm == NULL)
79 return;
81 sfree(wm->regex);
82 sfree(wm->type);
83 sfree(wm->instance);
84 match_destroy(wm->match);
85 cmc_web_match_free(wm->next);
86 sfree(wm);
87 } /* }}} void cmc_web_match_free */
89 static void cmc_web_page_free(web_page_t *wp) /* {{{ */
90 {
91 if (wp == NULL)
92 return;
94 if (wp->memc != NULL)
95 memcached_free(wp->memc);
96 wp->memc = NULL;
98 sfree(wp->plugin_name);
99 sfree(wp->instance);
100 sfree(wp->server);
101 sfree(wp->key);
102 sfree(wp->buffer);
104 cmc_web_match_free(wp->matches);
105 cmc_web_page_free(wp->next);
106 sfree(wp);
107 } /* }}} void cmc_web_page_free */
109 static int cmc_page_init_memc(web_page_t *wp) /* {{{ */
110 {
111 memcached_server_st *server;
113 wp->memc = memcached_create(NULL);
114 if (wp->memc == NULL) {
115 ERROR("memcachec plugin: memcached_create failed.");
116 return -1;
117 }
119 server = memcached_servers_parse(wp->server);
120 memcached_server_push(wp->memc, server);
121 memcached_server_list_free(server);
123 return 0;
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);
130 return -1;
131 }
133 sfree(*dest);
134 *dest = strdup(ci->values[0].value.string);
135 if (*dest == NULL)
136 return -1;
138 return 0;
139 } /* }}} int cmc_config_add_string */
141 static int cmc_config_add_match_dstype(int *dstype_ret, /* {{{ */
142 oconfig_item_t *ci) {
143 int dstype;
145 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
146 WARNING("memcachec plugin: `DSType' needs exactly one string argument.");
147 return -1;
148 }
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;
160 else
161 dstype = 0;
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;
171 else
172 dstype = 0;
173 } else {
174 dstype = 0;
175 }
177 if (dstype == 0) {
178 WARNING("memcachec plugin: `%s' is not a valid argument to `DSType'.",
179 ci->values[0].value.string);
180 return -1;
181 }
183 *dstype_ret = dstype;
184 return 0;
185 } /* }}} int cmc_config_add_match_dstype */
187 static int cmc_config_add_match(web_page_t *page, /* {{{ */
188 oconfig_item_t *ci) {
189 web_match_t *match;
190 int status;
192 if (ci->values_num != 0) {
193 WARNING("memcachec plugin: Ignoring arguments for the `Match' block.");
194 }
196 match = calloc(1, sizeof(*match));
197 if (match == NULL) {
198 ERROR("memcachec plugin: calloc failed.");
199 return -1;
200 }
202 status = 0;
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)
209 status =
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);
217 else {
218 WARNING("memcachec plugin: Option `%s' not allowed here.", child->key);
219 status = -1;
220 }
222 if (status != 0)
223 break;
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.");
229 status = -1;
230 }
232 if (match->type == NULL) {
233 WARNING("memcachec plugin: `Type' missing in `Match' block.");
234 status = -1;
235 }
237 if (match->dstype == 0) {
238 WARNING("memcachec plugin: `DSType' missing in `Match' block.");
239 status = -1;
240 }
242 break;
243 } /* while (status == 0) */
245 if (status != 0) {
246 cmc_web_match_free(match);
247 return status;
248 }
250 match->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);
255 return -1;
256 } else {
257 web_match_t *prev;
259 prev = page->matches;
260 while ((prev != NULL) && (prev->next != NULL))
261 prev = prev->next;
263 if (prev == NULL)
264 page->matches = match;
265 else
266 prev->next = match;
267 }
269 return 0;
270 } /* }}} int cmc_config_add_match */
272 static int cmc_config_add_page(oconfig_item_t *ci) /* {{{ */
273 {
274 web_page_t *page;
275 int status;
277 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
278 WARNING(
279 "memcachec plugin: `Page' blocks need exactly one string argument.");
280 return -1;
281 }
283 page = calloc(1, sizeof(*page));
284 if (page == NULL) {
285 ERROR("memcachec plugin: calloc failed.");
286 return -1;
287 }
288 page->server = NULL;
289 page->key = NULL;
291 page->instance = strdup(ci->values[0].value.string);
292 if (page->instance == NULL) {
293 ERROR("memcachec plugin: strdup failed.");
294 sfree(page);
295 return -1;
296 }
298 /* Process all children */
299 status = 0;
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);
312 else {
313 WARNING("memcachec plugin: Option `%s' not allowed here.", child->key);
314 status = -1;
315 }
317 if (status != 0)
318 break;
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.");
325 status = -1;
326 }
328 if (page->key == NULL) {
329 WARNING("memcachec plugin: `Key' missing in `Page' block.");
330 status = -1;
331 }
333 if (page->matches == NULL) {
334 assert(page->instance != NULL);
335 WARNING("memcachec plugin: No (valid) `Match' block "
336 "within `Page' block `%s'.",
337 page->instance);
338 status = -1;
339 }
341 if (status == 0)
342 status = cmc_page_init_memc(page);
344 break;
345 } /* while (status == 0) */
347 if (status != 0) {
348 cmc_web_page_free(page);
349 return status;
350 }
352 /* Add the new page to the linked list */
353 if (pages_g == NULL)
354 pages_g = page;
355 else {
356 web_page_t *prev;
358 prev = pages_g;
359 while (prev->next != NULL)
360 prev = prev->next;
361 prev->next = page;
362 }
364 return 0;
365 } /* }}} int cmc_config_add_page */
367 static int cmc_config(oconfig_item_t *ci) /* {{{ */
368 {
369 int success;
370 int errors;
371 int status;
373 success = 0;
374 errors = 0;
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);
381 if (status == 0)
382 success++;
383 else
384 errors++;
385 } else {
386 WARNING("memcachec plugin: Option `%s' not allowed here.", child->key);
387 errors++;
388 }
389 }
391 if ((success == 0) && (errors > 0)) {
392 ERROR("memcachec plugin: All statements failed.");
393 return -1;
394 }
396 return 0;
397 } /* }}} int cmc_config */
399 static int cmc_init(void) /* {{{ */
400 {
401 if (pages_g == NULL) {
402 INFO("memcachec plugin: No pages have been defined.");
403 return -1;
404 }
405 return 0;
406 } /* }}} int cmc_init */
408 static void cmc_submit(const web_page_t *wp, const web_match_t *wm, /* {{{ */
409 value_t value) {
410 value_list_t vl = VALUE_LIST_INIT;
412 vl.values = &value;
413 vl.values_len = 1;
414 sstrncpy(vl.plugin, (wp->plugin_name != NULL) ? wp->plugin_name : "memcachec",
415 sizeof (vl.plugin));
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) /* {{{ */
424 {
425 memcached_return rc;
426 size_t string_length;
427 uint32_t flags;
428 int status;
430 if (wp->memc == NULL)
431 return -1;
433 wp->buffer = memcached_get(wp->memc, wp->key, strlen(wp->key), &string_length,
434 &flags, &rc);
435 if (rc != MEMCACHED_SUCCESS) {
436 ERROR("memcachec plugin: memcached_get failed: %s",
437 memcached_strerror(wp->memc, rc));
438 return -1;
439 }
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);
445 if (status != 0) {
446 WARNING("memcachec plugin: match_apply failed.");
447 continue;
448 }
450 mv = match_get_user_data(wm->match);
451 if (mv == NULL) {
452 WARNING("memcachec plugin: match_get_user_data returned NULL.");
453 continue;
454 }
456 cmc_submit(wp, wm, mv->value);
457 match_value_reset(mv);
458 } /* for (wm = wp->matches; wm != NULL; wm = wm->next) */
460 sfree(wp->buffer);
462 return 0;
463 } /* }}} int cmc_read_page */
465 static int cmc_read(void) /* {{{ */
466 {
467 for (web_page_t *wp = pages_g; wp != NULL; wp = wp->next)
468 cmc_read_page(wp);
470 return 0;
471 } /* }}} int cmc_read */
473 static int cmc_shutdown(void) /* {{{ */
474 {
475 cmc_web_page_free(pages_g);
476 pages_g = NULL;
478 return 0;
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 */