#include "collectd.h"
-#include "common.h"
#include "plugin.h"
-#include "utils_match.h"
+#include "utils/common/common.h"
+#include "utils/match/match.h"
#include <libmemcached/memcached.h>
typedef struct web_page_s web_page_t;
struct web_page_s /* {{{ */
{
+ char *plugin_name;
char *instance;
char *server;
/*
* Global variables;
*/
-static web_page_t *pages_g = NULL;
+static web_page_t *pages_g;
/*
* Private functions
memcached_free(wp->memc);
wp->memc = NULL;
+ sfree(wp->plugin_name);
sfree(wp->instance);
sfree(wp->server);
sfree(wp->key);
wp->memc = memcached_create(NULL);
if (wp->memc == NULL) {
ERROR("memcachec plugin: memcached_create failed.");
- return (-1);
+ return -1;
}
server = memcached_servers_parse(wp->server);
memcached_server_push(wp->memc, server);
memcached_server_list_free(server);
- return (0);
+ return 0;
} /* }}} int cmc_page_init_memc */
-static int cmc_config_add_string(const char *name, char **dest, /* {{{ */
- oconfig_item_t *ci) {
- if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
- WARNING("memcachec plugin: `%s' needs exactly one string argument.", name);
- return (-1);
- }
-
- sfree(*dest);
- *dest = strdup(ci->values[0].value.string);
- if (*dest == NULL)
- return (-1);
-
- return (0);
-} /* }}} int cmc_config_add_string */
-
static int cmc_config_add_match_dstype(int *dstype_ret, /* {{{ */
oconfig_item_t *ci) {
int dstype;
if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
WARNING("memcachec plugin: `DSType' needs exactly one string argument.");
- return (-1);
+ return -1;
}
if (strncasecmp("Gauge", ci->values[0].value.string, strlen("Gauge")) == 0) {
if (dstype == 0) {
WARNING("memcachec plugin: `%s' is not a valid argument to `DSType'.",
ci->values[0].value.string);
- return (-1);
+ return -1;
}
*dstype_ret = dstype;
- return (0);
+ return 0;
} /* }}} int cmc_config_add_match_dstype */
static int cmc_config_add_match(web_page_t *page, /* {{{ */
match = calloc(1, sizeof(*match));
if (match == NULL) {
ERROR("memcachec plugin: calloc failed.");
- return (-1);
+ return -1;
}
status = 0;
oconfig_item_t *child = ci->children + i;
if (strcasecmp("Regex", child->key) == 0)
- status = cmc_config_add_string("Regex", &match->regex, child);
+ status = cf_util_get_string(child, &match->regex);
else if (strcasecmp("ExcludeRegex", child->key) == 0)
- status =
- cmc_config_add_string("ExcludeRegex", &match->exclude_regex, child);
+ status = cf_util_get_string(child, &match->exclude_regex);
else if (strcasecmp("DSType", child->key) == 0)
status = cmc_config_add_match_dstype(&match->dstype, child);
else if (strcasecmp("Type", child->key) == 0)
- status = cmc_config_add_string("Type", &match->type, child);
+ status = cf_util_get_string(child, &match->type);
else if (strcasecmp("Instance", child->key) == 0)
- status = cmc_config_add_string("Instance", &match->instance, child);
+ status = cf_util_get_string(child, &match->instance);
else {
WARNING("memcachec plugin: Option `%s' not allowed here.", child->key);
status = -1;
if (status != 0) {
cmc_web_match_free(match);
- return (status);
+ return status;
}
match->match =
if (match->match == NULL) {
ERROR("memcachec plugin: match_create_simple failed.");
cmc_web_match_free(match);
- return (-1);
+ return -1;
} else {
web_match_t *prev;
prev->next = match;
}
- return (0);
+ return 0;
} /* }}} int cmc_config_add_match */
static int cmc_config_add_page(oconfig_item_t *ci) /* {{{ */
if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
WARNING(
"memcachec plugin: `Page' blocks need exactly one string argument.");
- return (-1);
+ return -1;
}
page = calloc(1, sizeof(*page));
if (page == NULL) {
ERROR("memcachec plugin: calloc failed.");
- return (-1);
+ return -1;
}
page->server = NULL;
page->key = NULL;
if (page->instance == NULL) {
ERROR("memcachec plugin: strdup failed.");
sfree(page);
- return (-1);
+ return -1;
}
/* Process all children */
oconfig_item_t *child = ci->children + i;
if (strcasecmp("Server", child->key) == 0)
- status = cmc_config_add_string("Server", &page->server, child);
+ status = cf_util_get_string(child, &page->server);
else if (strcasecmp("Key", child->key) == 0)
- status = cmc_config_add_string("Key", &page->key, child);
+ status = cf_util_get_string(child, &page->key);
+ else if (strcasecmp("Plugin", child->key) == 0)
+ status = cf_util_get_string(child, &page->plugin_name);
else if (strcasecmp("Match", child->key) == 0)
/* Be liberal with failing matches => don't set `status'. */
cmc_config_add_match(page, child);
if (status != 0) {
cmc_web_page_free(page);
- return (status);
+ return status;
}
/* Add the new page to the linked list */
prev->next = page;
}
- return (0);
+ return 0;
} /* }}} int cmc_config_add_page */
static int cmc_config(oconfig_item_t *ci) /* {{{ */
if ((success == 0) && (errors > 0)) {
ERROR("memcachec plugin: All statements failed.");
- return (-1);
+ return -1;
}
- return (0);
+ return 0;
} /* }}} int cmc_config */
static int cmc_init(void) /* {{{ */
{
if (pages_g == NULL) {
INFO("memcachec plugin: No pages have been defined.");
- return (-1);
+ return -1;
}
- return (0);
+ return 0;
} /* }}} int cmc_init */
static void cmc_submit(const web_page_t *wp, const web_match_t *wm, /* {{{ */
vl.values = &value;
vl.values_len = 1;
- sstrncpy(vl.plugin, "memcachec", sizeof(vl.plugin));
+ sstrncpy(vl.plugin, (wp->plugin_name != NULL) ? wp->plugin_name : "memcachec",
+ sizeof(vl.plugin));
sstrncpy(vl.plugin_instance, wp->instance, sizeof(vl.plugin_instance));
sstrncpy(vl.type, wm->type, sizeof(vl.type));
sstrncpy(vl.type_instance, wm->instance, sizeof(vl.type_instance));
int status;
if (wp->memc == NULL)
- return (-1);
+ return -1;
wp->buffer = memcached_get(wp->memc, wp->key, strlen(wp->key), &string_length,
&flags, &rc);
if (rc != MEMCACHED_SUCCESS) {
ERROR("memcachec plugin: memcached_get failed: %s",
memcached_strerror(wp->memc, rc));
- return (-1);
+ return -1;
}
for (web_match_t *wm = wp->matches; wm != NULL; wm = wm->next) {
sfree(wp->buffer);
- return (0);
+ return 0;
} /* }}} int cmc_read_page */
static int cmc_read(void) /* {{{ */
for (web_page_t *wp = pages_g; wp != NULL; wp = wp->next)
cmc_read_page(wp);
- return (0);
+ return 0;
} /* }}} int cmc_read */
static int cmc_shutdown(void) /* {{{ */
cmc_web_page_free(pages_g);
pages_g = NULL;
- return (0);
+ return 0;
} /* }}} int cmc_shutdown */
void module_register(void) {
plugin_register_read("memcachec", cmc_read);
plugin_register_shutdown("memcachec", cmc_shutdown);
} /* void module_register */
-
-/* vim: set sw=2 sts=2 et fdm=marker : */