X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=lib%2Fmemcached_stats.c;h=0e3c2efe877c91e0ac98a047a911ecc8ec65fe4f;hb=257f0547823cf6b18473ba8bd2580b38235f6d79;hp=8fe5c822fa134252e1e99fe3350018687e9e2af6;hpb=7cbcd28d0e2958df0572d965996a65e22b18e66a;p=awesomized%2Flibmemcached diff --git a/lib/memcached_stats.c b/lib/memcached_stats.c index 8fe5c822..0e3c2efe 100644 --- a/lib/memcached_stats.c +++ b/lib/memcached_stats.c @@ -1,7 +1,38 @@ /* */ -#include +#include "common.h" + +static char *memcached_stat_keys[] = { + "pid", + "uptime", + "time", + "version", + "pointer_size", + "rusage_user", + "rusage_system", + "rusage_user_seconds", + "rusage_user_microseconds", + "rusage_system_seconds", + "rusage_system_microseconds", + "curr_items", + "total_items", + "bytes", + "curr_connections", + "total_connections", + "connection_structures", + "cmd_get", + "cmd_set", + "get_hits", + "get_misses", + "evictions", + "bytes_read", + "bytes_written", + "limit_maxbytes", + "threads", + NULL +}; + static void set_data(memcached_stat_st *stat, char *key, char *value) { @@ -115,22 +146,86 @@ static void set_data(memcached_stat_st *stat, char *key, char *value) } } -memcached_stat_st **memcached_stat(memcached_st *ptr, memcached_return *error) +char *memcached_stat_get_value(memcached_st *ptr, memcached_stat_st *stat, + char *key, memcached_return *error) { - return NULL; + char buffer[SMALL_STRING_LEN]; + *error= MEMCACHED_SUCCESS; + + + memset(buffer, 0, SMALL_STRING_LEN); + + if (!memcmp("pid", key, strlen("pid"))) + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->pid); + else if (!memcmp("uptime", key, strlen("uptime"))) + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->uptime); + else if (!memcmp("time", key, strlen("time"))) + snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->time); + else if (!memcmp("version", key, strlen("version"))) + snprintf(buffer, SMALL_STRING_LEN,"%s", stat->version); + else if (!memcmp("pointer_size", key, strlen("pointer_size"))) + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->pointer_size); + else if (!memcmp("rusage_user", key, strlen("rusage_user"))) + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_user); + else if (!memcmp("rusage_system", key, strlen("rusage_system"))) + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_system); + else if (!memcmp("rusage_user_seconds", key, strlen("rusage_user_seconds"))) + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_user_seconds); + else if (!memcmp("rusage_user_microseconds", key, strlen("rusage_user_microseconds"))) + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_user_microseconds); + else if (!memcmp("rusage_system_seconds", key, strlen("rusage_system_seconds"))) + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_system_seconds); + else if (!memcmp("rusage_system_microseconds", key, strlen("rusage_system_microseconds"))) + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_system_microseconds); + else if (!memcmp("curr_items", key, strlen("curr_items"))) + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->curr_items); + else if (!memcmp("total_items", key, strlen("total_items"))) + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->total_items); + else if (!memcmp("bytes", key, strlen("bytes"))) + snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->bytes); + else if (!memcmp("curr_connections", key, strlen("curr_connections"))) + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->curr_connections); + else if (!memcmp("total_connections", key, strlen("total_connections"))) + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->total_connections); + else if (!memcmp("connection_structures", key, strlen("connection_structures"))) + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->connection_structures); + else if (!memcmp("cmd_get", key, strlen("cmd_get"))) + snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->cmd_get); + else if (!memcmp("cmd_set", key, strlen("cmd_set"))) + snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->cmd_set); + else if (!memcmp("get_hits", key, strlen("get_hits"))) + snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->get_hits); + else if (!memcmp("get_misses", key, strlen("get_misses"))) + snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->get_misses); + else if (!memcmp("evictions", key, strlen("evictions"))) + snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->evictions); + else if (!memcmp("bytes_read", key, strlen("bytes_read"))) + snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->bytes_read); + else if (!memcmp("bytes_written", key, strlen("bytes_written"))) + snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->bytes_written); + else if (!memcmp("limit_maxbytes", key, strlen("limit_maxbytes"))) + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->limit_maxbytes); + else if (!memcmp("threads", key, strlen("threads"))) + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->threads); + else + { + *error= MEMCACHED_NOTFOUND; + return NULL; + } + + return strdup(buffer); } -memcached_return memcached_stat_hostname(memcached_stat_st *stat, char *args, - char *hostname, unsigned int port) +static memcached_return memcached_stats_fetch(memcached_st *ptr, + memcached_stat_st *stat, + char *args, + unsigned int server_key) { - size_t send_length; memcached_return rc; char buffer[HUGE_STRING_LEN]; - memcached_st memc; + size_t send_length, sent_length; - memcached_init(&memc); - - rc= memcached_connect(&memc); + rc= memcached_connect(ptr); if (rc != MEMCACHED_SUCCESS) return rc; @@ -142,15 +237,15 @@ memcached_return memcached_stat_hostname(memcached_stat_st *stat, char *args, send_length= snprintf(buffer, HUGE_STRING_LEN, "stats\r\n"); - if ((send(memc.fd, buffer, send_length, 0) == -1)) - { - fprintf(stderr, "failed on stats\n"); + if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE) + return MEMCACHED_WRITE_FAILURE; - rc= MEMCACHED_WRITE_FAILURE; - goto error; - } + sent_length= send(ptr->hosts[server_key].fd, buffer, send_length, 0); + + if (sent_length == -1 || sent_length != send_length) + return MEMCACHED_WRITE_FAILURE; - rc= memcached_response(&memc, buffer, HUGE_STRING_LEN); + rc= memcached_response(ptr, buffer, HUGE_STRING_LEN, 0); if (rc == MEMCACHED_SUCCESS) { @@ -176,8 +271,84 @@ memcached_return memcached_stat_hostname(memcached_stat_st *stat, char *args, } } -error: - memcached_deinit(&memc); + return rc; +} + +memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_return *error) +{ + unsigned int x; + memcached_return rc; + memcached_stat_st *stats; + + rc= memcached_connect(ptr); + if (rc != MEMCACHED_SUCCESS) + { + *error= rc; + return NULL; + } + + stats= (memcached_stat_st *)malloc(sizeof(memcached_st)*(ptr->number_of_hosts+1)); + if (!stats) + { + *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE; + free(stats); + return NULL; + } + memset(stats, 0, sizeof(memcached_st)*(ptr->number_of_hosts+1)); + + for (x= 0; x < ptr->number_of_hosts; x++) + { + rc= memcached_stats_fetch(ptr, stats+x, args, x); + if (rc != MEMCACHED_SUCCESS) + rc= MEMCACHED_SOME_ERRORS; + } + + *error= rc; + return stats; +} + +memcached_return memcached_stat_servername(memcached_stat_st *stat, char *args, + char *hostname, unsigned int port) +{ + memcached_return rc; + memcached_st memc; + + memcached_create(&memc); + + memcached_server_add(&memc, hostname, port); + + rc= memcached_connect(&memc); + + if (rc != MEMCACHED_SUCCESS) + return rc; + + rc= memcached_stats_fetch(&memc, stat, args, 0); + + memcached_free(&memc); return rc; } + +/* + We make a copy of the keys since at some point in the not so distant future + we will add support for "found" keys. +*/ +char ** memcached_stat_get_keys(memcached_st *ptr, memcached_stat_st *stat, + memcached_return *error) +{ + char **list= (char **)malloc(sizeof(memcached_stat_keys)); + + memset(list, 0, sizeof(memcached_stat_keys)); + + if (!list) + { + *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE; + return NULL; + } + + memcpy(list, memcached_stat_keys, sizeof(memcached_stat_keys)); + + *error= MEMCACHED_SUCCESS; + + return list; +}