X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fmemcached.c;h=11df999d977caeacc5e923e07b3d5d28311dc0e6;hp=39400d9aad53944e9a0f056ac496c423a029e6ad;hb=d486225f89ea52d8ed2b4242eba2ad94c409f837;hpb=e0f43d29ba65148a0a0baf9bffc4345ddc8fe8ef diff --git a/src/memcached.c b/src/memcached.c index 39400d9a..11df999d 100644 --- a/src/memcached.c +++ b/src/memcached.c @@ -69,7 +69,7 @@ struct memcached_s { }; typedef struct memcached_s memcached_t; -static _Bool memcached_have_instances = 0; +static bool memcached_have_instances; static void memcached_free(void *arg) { memcached_t *st = arg; @@ -92,17 +92,15 @@ static void memcached_free(void *arg) { static int memcached_connect_unix(memcached_t *st) { struct sockaddr_un serv_addr = {0}; - int fd; serv_addr.sun_family = AF_UNIX; sstrncpy(serv_addr.sun_path, st->socket, sizeof(serv_addr.sun_path)); /* create our socket descriptor */ - fd = socket(AF_UNIX, SOCK_STREAM, 0); + int fd = socket(AF_UNIX, SOCK_STREAM, 0); if (fd < 0) { - char errbuf[1024]; ERROR("memcached plugin: memcached_connect_unix: socket(2) failed: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); + STRERRNO); return -1; } @@ -127,21 +125,18 @@ static int memcached_connect_unix(memcached_t *st) { static int memcached_connect_inet(memcached_t *st) { struct addrinfo *ai_list; - int status; int fd = -1; struct addrinfo ai_hints = {.ai_family = AF_UNSPEC, .ai_flags = AI_ADDRCONFIG, .ai_socktype = SOCK_STREAM}; - status = getaddrinfo(st->connhost, st->connport, &ai_hints, &ai_list); + int status = getaddrinfo(st->connhost, st->connport, &ai_hints, &ai_list); if (status != 0) { - char errbuf[1024]; ERROR("memcached plugin: memcached_connect_inet: " "getaddrinfo(%s,%s) failed: %s", st->connhost, st->connport, - (status == EAI_SYSTEM) ? sstrerror(errno, errbuf, sizeof(errbuf)) - : gai_strerror(status)); + (status == EAI_SYSTEM) ? STRERRNO : gai_strerror(status)); return -1; } @@ -150,10 +145,9 @@ static int memcached_connect_inet(memcached_t *st) { /* create our socket descriptor */ fd = socket(ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol); if (fd < 0) { - char errbuf[1024]; WARNING("memcached plugin: memcached_connect_inet: " "socket(2) failed: %s", - sstrerror(errno, errbuf, sizeof(errbuf))); + STRERRNO); continue; } @@ -248,9 +242,8 @@ static int memcached_query_daemon(char *buffer, size_t buffer_size, status = (int)swrite(st->fd, "stats\r\n", strlen("stats\r\n")); if (status != 0) { - char errbuf[1024]; ERROR("memcached plugin: Instance \"%s\": write(2) failed: %s", st->name, - sstrerror(errno, errbuf, sizeof(errbuf))); + STRERRNO); shutdown(st->fd, SHUT_RDWR); close(st->fd); st->fd = -1; @@ -282,17 +275,22 @@ static int memcached_query_daemon(char *buffer, size_t buffer_size, char const end_token[5] = {'E', 'N', 'D', '\r', '\n'}; if (status < 0) { - char errbuf[1024]; if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) continue; ERROR("memcached plugin: Instance \"%s\": Error reading from socket: %s", - st->name, sstrerror(errno, errbuf, sizeof(errbuf))); + st->name, STRERRNO); shutdown(st->fd, SHUT_RDWR); close(st->fd); st->fd = -1; return -1; + } else if (status == 0) { + ERROR("memcached plugin: Instance \"%s\": Connection closed by peer", + st->name); + close(st->fd); + st->fd = -1; + return -1; } buffer_fill += (size_t)status; @@ -408,9 +406,12 @@ static gauge_t calculate_ratio_percent(derive_t part, derive_t total, *prev_part = part; *prev_total = total; - if (num == 0 || denom == 0) + if (denom == 0) return NAN; + if (num == 0) + return 0; + return 100.0 * (gauge_t)num / (gauge_t)denom; } @@ -428,19 +429,19 @@ static gauge_t calculate_ratio_percent2(derive_t part1, derive_t part2, *prev1 = part1; *prev2 = part2; - if (num == 0 || denom == 0) + if (denom == 0) return NAN; + if (num == 0) + return 0; + return 100.0 * (gauge_t)num / (gauge_t)denom; } static int memcached_read(user_data_t *user_data) { char buf[4096]; char *fields[3]; - char *ptr; char *line; - char *saveptr; - int fields_num; derive_t bytes_used = 0; derive_t bytes_total = 0; @@ -466,18 +467,15 @@ static int memcached_read(user_data_t *user_data) { #define FIELD_IS(cnst) \ (((sizeof(cnst) - 1) == name_len) && (strcmp(cnst, fields[1]) == 0)) - ptr = buf; - saveptr = NULL; + char *ptr = buf; + char *saveptr = NULL; while ((line = strtok_r(ptr, "\n\r", &saveptr)) != NULL) { - int name_len; - ptr = NULL; - fields_num = strsplit(line, fields, 3); - if (fields_num != 3) + if (strsplit(line, fields, 3) != 3) continue; - name_len = strlen(fields[1]); + int name_len = strlen(fields[1]); if (name_len == 0) continue; @@ -525,7 +523,7 @@ static int memcached_read(user_data_t *user_data) { else if (FIELD_IS("curr_connections")) { submit_gauge("memcached_connections", "current", atof(fields[2]), st); } else if (FIELD_IS("listen_disabled_num")) { - submit_derive("connections", "listen_disabled", atoll(fields[2]), st); + submit_derive("total_events", "listen_disabled", atoll(fields[2]), st); } /* * Total number of connections opened since the server started running @@ -701,13 +699,12 @@ static int memcached_add_read_callback(memcached_t *st) { * */ static int config_add_instance(oconfig_item_t *ci) { - memcached_t *st; int status = 0; /* Disable automatic generation of default instance in the init callback. */ - memcached_have_instances = 1; + memcached_have_instances = true; - st = calloc(1, sizeof(*st)); + memcached_t *st = calloc(1, sizeof(*st)); if (st == NULL) { ERROR("memcached plugin: calloc failed."); return ENOMEM; @@ -758,7 +755,7 @@ static int config_add_instance(oconfig_item_t *ci) { } /* int config_add_instance */ static int memcached_config(oconfig_item_t *ci) { - _Bool have_instance_block = 0; + bool have_instance_block = 0; for (int i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; @@ -782,14 +779,12 @@ static int memcached_config(oconfig_item_t *ci) { } /* int memcached_config */ static int memcached_init(void) { - memcached_t *st; - int status; if (memcached_have_instances) return 0; /* No instances were configured, lets start a default instance. */ - st = calloc(1, sizeof(*st)); + memcached_t *st = calloc(1, sizeof(*st)); if (st == NULL) return ENOMEM; st->name = NULL; @@ -800,9 +795,9 @@ static int memcached_init(void) { st->fd = -1; - status = memcached_add_read_callback(st); + int status = memcached_add_read_callback(st); if (status == 0) - memcached_have_instances = 1; + memcached_have_instances = true; return status; } /* int memcached_init */