X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libmemcached%2Fmemcached.hpp;h=d7086b4ce301ea11a876fdcb3a20f8b0831a009a;hb=a7a011c2ea4a63368b3a96a332da00820ed402cb;hp=46573f4959b2aab0a874da0eae407965fb2360a3;hpb=4388f9a86dfc0ff7f49205e950ee1aab91b620f9;p=m6w6%2Flibmemcached diff --git a/libmemcached/memcached.hpp b/libmemcached/memcached.hpp index 46573f49..d7086b4c 100644 --- a/libmemcached/memcached.hpp +++ b/libmemcached/memcached.hpp @@ -236,7 +236,13 @@ public: ret_val.reserve(value_length); ret_val.assign(value, value + value_length); key.assign(ret_key); + free(value); } + else if (value) + { + free(value); + } + return rc; } @@ -265,6 +271,7 @@ public: { ret_val.reserve(value_length); ret_val.assign(value, value + value_length); + free(value); return true; } return false; @@ -302,6 +309,7 @@ public: { ret_val.reserve(value_length); ret_val.assign(value, value + value_length); + free(value); return true; } return false; @@ -917,6 +925,60 @@ public: return version; } + /** + * Retrieve memcached statistics. Populate a std::map with the retrieved + * stats. Each server will map to another std::map of the key:value stats. + * + * @param[out] stats_map a std::map to be populated with the memcached + * stats + * @return true on success; false otherwise + */ + bool getStats(std::map< std::string, std::map > + &stats_map) + { + memcached_return rc; + memcached_stat_st *stats= memcached_stat(&memc, NULL, &rc); + + if (rc != MEMCACHED_SUCCESS && + rc != MEMCACHED_SOME_ERRORS) + { + return false; + } + + uint32_t server_count= memcached_server_count(&memc); + + /* + * For each memcached server, construct a std::map for its stats and add + * it to the std::map of overall stats. + */ + for (uint32_t x= 0; x < server_count; x++) + { + std::ostringstream strstm; + std::string server_name(memcached_server_name(&memc, servers[x])); + server_name.append(":"); + strstm << memcached_server_port(&memc, servers[x]); + server_name.append(strstm.str()); + + std::map server_stats; + char **list= NULL; + char **ptr= NULL; + + list= memcached_stat_get_keys(&memc, &stats[x], &rc); + for (ptr= list; *ptr; ptr++) + { + char *value= memcached_stat_get_value(&memc, &stats[x], *ptr, &rc); + server_stats[*ptr]= value; + free(value); + } + + stats_map[server_name]= server_stats; + free(list); + } + + memcached_stat_free(&memc, stats); + return true; + } + private: std::string servers_list;