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 {
114 ERROR ("memcachec plugin: memcached_create failed.");
115 return (-1);
116 }
118 server = memcached_servers_parse (wp->server);
119 memcached_server_push (wp->memc, server);
120 memcached_server_list_free (server);
122 return (0);
123 } /* }}} int cmc_page_init_memc */
125 static int cmc_config_add_string (const char *name, char **dest, /* {{{ */
126 oconfig_item_t *ci)
127 {
128 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
129 {
130 WARNING ("memcachec plugin: `%s' needs exactly one string argument.", name);
131 return (-1);
132 }
134 sfree (*dest);
135 *dest = strdup (ci->values[0].value.string);
136 if (*dest == NULL)
137 return (-1);
139 return (0);
140 } /* }}} int cmc_config_add_string */
142 static int cmc_config_add_match_dstype (int *dstype_ret, /* {{{ */
143 oconfig_item_t *ci)
144 {
145 int dstype;
147 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
148 {
149 WARNING ("memcachec plugin: `DSType' needs exactly one string argument.");
150 return (-1);
151 }
153 if (strncasecmp ("Gauge", ci->values[0].value.string,
154 strlen ("Gauge")) == 0)
155 {
156 dstype = UTILS_MATCH_DS_TYPE_GAUGE;
157 if (strcasecmp ("GaugeAverage", ci->values[0].value.string) == 0)
158 dstype |= UTILS_MATCH_CF_GAUGE_AVERAGE;
159 else if (strcasecmp ("GaugeMin", ci->values[0].value.string) == 0)
160 dstype |= UTILS_MATCH_CF_GAUGE_MIN;
161 else if (strcasecmp ("GaugeMax", ci->values[0].value.string) == 0)
162 dstype |= UTILS_MATCH_CF_GAUGE_MAX;
163 else if (strcasecmp ("GaugeLast", ci->values[0].value.string) == 0)
164 dstype |= UTILS_MATCH_CF_GAUGE_LAST;
165 else
166 dstype = 0;
167 }
168 else if (strncasecmp ("Counter", ci->values[0].value.string,
169 strlen ("Counter")) == 0)
170 {
171 dstype = UTILS_MATCH_DS_TYPE_COUNTER;
172 if (strcasecmp ("CounterSet", ci->values[0].value.string) == 0)
173 dstype |= UTILS_MATCH_CF_COUNTER_SET;
174 else if (strcasecmp ("CounterAdd", ci->values[0].value.string) == 0)
175 dstype |= UTILS_MATCH_CF_COUNTER_ADD;
176 else if (strcasecmp ("CounterInc", ci->values[0].value.string) == 0)
177 dstype |= UTILS_MATCH_CF_COUNTER_INC;
178 else
179 dstype = 0;
180 }
181 else
182 {
183 dstype = 0;
184 }
186 if (dstype == 0)
187 {
188 WARNING ("memcachec plugin: `%s' is not a valid argument to `DSType'.",
189 ci->values[0].value.string);
190 return (-1);
191 }
193 *dstype_ret = dstype;
194 return (0);
195 } /* }}} int cmc_config_add_match_dstype */
197 static int cmc_config_add_match (web_page_t *page, /* {{{ */
198 oconfig_item_t *ci)
199 {
200 web_match_t *match;
201 int status;
203 if (ci->values_num != 0)
204 {
205 WARNING ("memcachec plugin: Ignoring arguments for the `Match' block.");
206 }
208 match = calloc (1, sizeof (*match));
209 if (match == NULL)
210 {
211 ERROR ("memcachec plugin: calloc failed.");
212 return (-1);
213 }
215 status = 0;
216 for (int i = 0; i < ci->children_num; i++)
217 {
218 oconfig_item_t *child = ci->children + i;
220 if (strcasecmp ("Regex", child->key) == 0)
221 status = cmc_config_add_string ("Regex", &match->regex, child);
222 else if (strcasecmp ("ExcludeRegex", child->key) == 0)
223 status = cmc_config_add_string ("ExcludeRegex", &match->exclude_regex, child);
224 else if (strcasecmp ("DSType", child->key) == 0)
225 status = cmc_config_add_match_dstype (&match->dstype, child);
226 else if (strcasecmp ("Type", child->key) == 0)
227 status = cmc_config_add_string ("Type", &match->type, child);
228 else if (strcasecmp ("Instance", child->key) == 0)
229 status = cmc_config_add_string ("Instance", &match->instance, child);
230 else
231 {
232 WARNING ("memcachec plugin: Option `%s' not allowed here.", child->key);
233 status = -1;
234 }
236 if (status != 0)
237 break;
238 } /* for (i = 0; i < ci->children_num; i++) */
240 while (status == 0)
241 {
242 if (match->regex == NULL)
243 {
244 WARNING ("memcachec plugin: `Regex' missing in `Match' block.");
245 status = -1;
246 }
248 if (match->type == NULL)
249 {
250 WARNING ("memcachec plugin: `Type' missing in `Match' block.");
251 status = -1;
252 }
254 if (match->dstype == 0)
255 {
256 WARNING ("memcachec plugin: `DSType' missing in `Match' block.");
257 status = -1;
258 }
260 break;
261 } /* while (status == 0) */
263 if (status != 0)
264 {
265 cmc_web_match_free (match);
266 return (status);
267 }
269 match->match = match_create_simple (match->regex, match->exclude_regex,
270 match->dstype);
271 if (match->match == NULL)
272 {
273 ERROR ("memcachec plugin: match_create_simple failed.");
274 cmc_web_match_free (match);
275 return (-1);
276 }
277 else
278 {
279 web_match_t *prev;
281 prev = page->matches;
282 while ((prev != NULL) && (prev->next != NULL))
283 prev = prev->next;
285 if (prev == NULL)
286 page->matches = match;
287 else
288 prev->next = match;
289 }
291 return (0);
292 } /* }}} int cmc_config_add_match */
294 static int cmc_config_add_page (oconfig_item_t *ci) /* {{{ */
295 {
296 web_page_t *page;
297 int status;
299 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
300 {
301 WARNING ("memcachec plugin: `Page' blocks need exactly one string argument.");
302 return (-1);
303 }
305 page = calloc (1, sizeof (*page));
306 if (page == NULL)
307 {
308 ERROR ("memcachec plugin: calloc failed.");
309 return (-1);
310 }
311 page->server = NULL;
312 page->key = NULL;
314 page->instance = strdup (ci->values[0].value.string);
315 if (page->instance == NULL)
316 {
317 ERROR ("memcachec plugin: strdup failed.");
318 sfree (page);
319 return (-1);
320 }
322 /* Process all children */
323 status = 0;
324 for (int i = 0; i < ci->children_num; i++)
325 {
326 oconfig_item_t *child = ci->children + i;
328 if (strcasecmp ("Server", child->key) == 0)
329 status = cmc_config_add_string ("Server", &page->server, child);
330 else if (strcasecmp ("Key", child->key) == 0)
331 status = cmc_config_add_string ("Key", &page->key, child);
332 else if (strcasecmp ("Match", child->key) == 0)
333 /* Be liberal with failing matches => don't set `status'. */
334 cmc_config_add_match (page, child);
335 else
336 {
337 WARNING ("memcachec plugin: Option `%s' not allowed here.", child->key);
338 status = -1;
339 }
341 if (status != 0)
342 break;
343 } /* for (i = 0; i < ci->children_num; i++) */
345 /* Additionial sanity checks and libmemcached initialization. */
346 while (status == 0)
347 {
348 if (page->server == NULL)
349 {
350 WARNING ("memcachec plugin: `Server' missing in `Page' block.");
351 status = -1;
352 }
354 if (page->key == NULL)
355 {
356 WARNING ("memcachec plugin: `Key' missing in `Page' block.");
357 status = -1;
358 }
360 if (page->matches == NULL)
361 {
362 assert (page->instance != NULL);
363 WARNING ("memcachec plugin: No (valid) `Match' block "
364 "within `Page' block `%s'.", page->instance);
365 status = -1;
366 }
368 if (status == 0)
369 status = cmc_page_init_memc (page);
371 break;
372 } /* while (status == 0) */
374 if (status != 0)
375 {
376 cmc_web_page_free (page);
377 return (status);
378 }
380 /* Add the new page to the linked list */
381 if (pages_g == NULL)
382 pages_g = page;
383 else
384 {
385 web_page_t *prev;
387 prev = pages_g;
388 while (prev->next != NULL)
389 prev = prev->next;
390 prev->next = page;
391 }
393 return (0);
394 } /* }}} int cmc_config_add_page */
396 static int cmc_config (oconfig_item_t *ci) /* {{{ */
397 {
398 int success;
399 int errors;
400 int status;
402 success = 0;
403 errors = 0;
405 for (int i = 0; i < ci->children_num; i++)
406 {
407 oconfig_item_t *child = ci->children + i;
409 if (strcasecmp ("Page", child->key) == 0)
410 {
411 status = cmc_config_add_page (child);
412 if (status == 0)
413 success++;
414 else
415 errors++;
416 }
417 else
418 {
419 WARNING ("memcachec plugin: Option `%s' not allowed here.", child->key);
420 errors++;
421 }
422 }
424 if ((success == 0) && (errors > 0))
425 {
426 ERROR ("memcachec plugin: All statements failed.");
427 return (-1);
428 }
430 return (0);
431 } /* }}} int cmc_config */
433 static int cmc_init (void) /* {{{ */
434 {
435 if (pages_g == NULL)
436 {
437 INFO ("memcachec plugin: No pages have been defined.");
438 return (-1);
439 }
440 return (0);
441 } /* }}} int cmc_init */
443 static void cmc_submit (const web_page_t *wp, const web_match_t *wm, /* {{{ */
444 const cu_match_value_t *mv)
445 {
446 value_t values[1];
447 value_list_t vl = VALUE_LIST_INIT;
449 values[0] = mv->value;
451 vl.values = values;
452 vl.values_len = 1;
453 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
454 sstrncpy (vl.plugin, "memcachec", sizeof (vl.plugin));
455 sstrncpy (vl.plugin_instance, wp->instance, sizeof (vl.plugin_instance));
456 sstrncpy (vl.type, wm->type, sizeof (vl.type));
457 sstrncpy (vl.type_instance, wm->instance, sizeof (vl.type_instance));
459 plugin_dispatch_values (&vl);
460 } /* }}} void cmc_submit */
462 static int cmc_read_page (web_page_t *wp) /* {{{ */
463 {
464 memcached_return rc;
465 size_t string_length;
466 uint32_t flags;
467 int status;
469 if (wp->memc == NULL)
470 return (-1);
472 wp->buffer = memcached_get (wp->memc, wp->key, strlen (wp->key),
473 &string_length, &flags, &rc);
474 if (rc != MEMCACHED_SUCCESS)
475 {
476 ERROR ("memcachec plugin: memcached_get failed: %s",
477 memcached_strerror (wp->memc, rc));
478 return (-1);
479 }
481 for (web_match_t *wm = wp->matches; wm != NULL; wm = wm->next)
482 {
483 cu_match_value_t *mv;
485 status = match_apply (wm->match, wp->buffer);
486 if (status != 0)
487 {
488 WARNING ("memcachec plugin: match_apply failed.");
489 continue;
490 }
492 mv = match_get_user_data (wm->match);
493 if (mv == NULL)
494 {
495 WARNING ("memcachec plugin: match_get_user_data returned NULL.");
496 continue;
497 }
499 cmc_submit (wp, wm, mv);
500 match_value_reset (mv);
501 } /* for (wm = wp->matches; wm != NULL; wm = wm->next) */
503 sfree (wp->buffer);
505 return (0);
506 } /* }}} int cmc_read_page */
508 static int cmc_read (void) /* {{{ */
509 {
510 for (web_page_t *wp = pages_g; wp != NULL; wp = wp->next)
511 cmc_read_page (wp);
513 return (0);
514 } /* }}} int cmc_read */
516 static int cmc_shutdown (void) /* {{{ */
517 {
518 cmc_web_page_free (pages_g);
519 pages_g = NULL;
521 return (0);
522 } /* }}} int cmc_shutdown */
524 void module_register (void)
525 {
526 plugin_register_complex_config ("memcachec", cmc_config);
527 plugin_register_init ("memcachec", cmc_init);
528 plugin_register_read ("memcachec", cmc_read);
529 plugin_register_shutdown ("memcachec", cmc_shutdown);
530 } /* void module_register */
532 /* vim: set sw=2 sts=2 et fdm=marker : */