X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fmemcached.c;h=bd83b46eeb26782ea8983cb485e2f233ddba42a9;hb=745ba17db2b2c1868667734d1317e1ee1d0b68ae;hp=46155e7e455eea81c5d9e8d69f4ccbb69655bace;hpb=5afde1077e3c8fc119b79caa2572cbf84dbfe5ca;p=collectd.git diff --git a/src/memcached.c b/src/memcached.c index 46155e7e..bd83b46e 100644 --- a/src/memcached.c +++ b/src/memcached.c @@ -29,9 +29,9 @@ **/ #include "collectd.h" + #include "common.h" #include "plugin.h" -#include "configfile.h" #include #include @@ -101,7 +101,7 @@ static int memcached_connect_inet (memcached_t *st) const char *host; const char *port; - struct addrinfo *ai_list, *ai_ptr; + struct addrinfo *ai_list; int status; int fd = -1; @@ -127,7 +127,7 @@ static int memcached_connect_inet (memcached_t *st) return (-1); } - for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) + for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) { /* create our socket descriptor */ fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol); @@ -239,33 +239,32 @@ static int memcached_query_daemon (char *buffer, size_t buffer_size, memcached_t static void memcached_init_vl (value_list_t *vl, memcached_t const *st) { + char const *host = st->host; + + /* Keep default hostname, if: + * - Legacy mode is used. + * - "Socket" option is given (doc: "Host option is ignored"). + * - "Host" option is not provided. + * - "Host" option is set to "localhost" or "127.0.0.1". */ + if ((strcmp (st->name, "__legacy__") != 0) + && (st->socket == NULL) + && (st->host != NULL) + && (strcmp ("127.0.0.1", st->host) != 0) + && (strcmp ("localhost", st->host) != 0)) + sstrncpy (vl->host, host, sizeof (vl->host)); + sstrncpy (vl->plugin, "memcached", sizeof (vl->plugin)); - if (strcmp (st->name, "__legacy__") == 0) /* legacy mode */ - { - sstrncpy (vl->host, hostname_g, sizeof (vl->host)); - } - else - { - if (st->socket != NULL) - sstrncpy (vl->host, hostname_g, sizeof (vl->host)); - else - sstrncpy (vl->host, - (st->host != NULL) ? st->host : MEMCACHED_DEF_HOST, - sizeof (vl->host)); + if (strcmp (st->name, "__legacy__") != 0) sstrncpy (vl->plugin_instance, st->name, sizeof (vl->plugin_instance)); - } } static void submit_derive (const char *type, const char *type_inst, derive_t value, memcached_t *st) { - value_t values[1]; value_list_t vl = VALUE_LIST_INIT; - memcached_init_vl (&vl, st); - - values[0].derive = value; - vl.values = values; + memcached_init_vl (&vl, st); + vl.values = &(value_t) { .derive = value }; vl.values_len = 1; sstrncpy (vl.type, type, sizeof (vl.type)); if (type_inst != NULL) @@ -277,15 +276,15 @@ static void submit_derive (const char *type, const char *type_inst, static void submit_derive2 (const char *type, const char *type_inst, derive_t value0, derive_t value1, memcached_t *st) { - value_t values[2]; value_list_t vl = VALUE_LIST_INIT; - memcached_init_vl (&vl, st); - - values[0].derive = value0; - values[1].derive = value1; + value_t values[] = { + { .derive = value0 }, + { .derive = value1 }, + }; + memcached_init_vl (&vl, st); vl.values = values; - vl.values_len = 2; + vl.values_len = STATIC_ARRAY_SIZE (values); sstrncpy (vl.type, type, sizeof (vl.type)); if (type_inst != NULL) sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance)); @@ -296,13 +295,10 @@ static void submit_derive2 (const char *type, const char *type_inst, static void submit_gauge (const char *type, const char *type_inst, gauge_t value, memcached_t *st) { - value_t values[1]; value_list_t vl = VALUE_LIST_INIT; - memcached_init_vl (&vl, st); - values[0].gauge = value; - - vl.values = values; + memcached_init_vl (&vl, st); + vl.values = &(value_t) { .gauge = value }; vl.values_len = 1; sstrncpy (vl.type, type, sizeof (vl.type)); if (type_inst != NULL) @@ -314,15 +310,15 @@ static void submit_gauge (const char *type, const char *type_inst, static void submit_gauge2 (const char *type, const char *type_inst, gauge_t value0, gauge_t value1, memcached_t *st) { - value_t values[2]; value_list_t vl = VALUE_LIST_INIT; - memcached_init_vl (&vl, st); - - values[0].gauge = value0; - values[1].gauge = value1; + value_t values[] = { + { .gauge = value0 }, + { .gauge = value1 }, + }; + memcached_init_vl (&vl, st); vl.values = values; - vl.values_len = 2; + vl.values_len = STATIC_ARRAY_SIZE (values); sstrncpy (vl.type, type, sizeof (vl.type)); if (type_inst != NULL) sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance)); @@ -545,13 +541,9 @@ static int memcached_read (user_data_t *user_data) static int memcached_add_read_callback (memcached_t *st) { - user_data_t ud = { 0 }; char callback_name[3*DATA_MAX_NAME_LEN]; int status; - ud.data = st; - ud.free_func = memcached_free; - assert (st->name != NULL); ssnprintf (callback_name, sizeof (callback_name), "memcached/%s", st->name); @@ -559,7 +551,11 @@ static int memcached_add_read_callback (memcached_t *st) /* name = */ callback_name, /* callback = */ memcached_read, /* interval = */ 0, - /* user_data = */ &ud); + &(user_data_t) { + .data = st, + .free_func = memcached_free, + }); + return (status); } /* int memcached_add_read_callback */ @@ -574,7 +570,6 @@ static int memcached_add_read_callback (memcached_t *st) static int config_add_instance(oconfig_item_t *ci) { memcached_t *st; - int i; int status = 0; /* Disable automatic generation of default instance in the init callback. */ @@ -603,7 +598,7 @@ static int config_add_instance(oconfig_item_t *ci) } assert (st->name != NULL); - for (i = 0; i < ci->children_num; i++) + for (int i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; @@ -640,9 +635,8 @@ static int memcached_config (oconfig_item_t *ci) { int status = 0; _Bool have_instance_block = 0; - int i; - for (i = 0; i < ci->children_num; i++) + for (int i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i;