X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Fbin%2Fmemping.cc;h=d875280ead6af5f4135fcbe80a93675fcca1879c;hb=8734a061ef5bafbbe46523a0729898008d479dbf;hp=902954c7882f988d6daa3e64727dcdc9903b5416;hpb=cb40bfe8923a2b06160a965d95b45ca0ea3421ab;p=awesomized%2Flibmemcached diff --git a/src/bin/memping.cc b/src/bin/memping.cc index 902954c7..d875280e 100644 --- a/src/bin/memping.cc +++ b/src/bin/memping.cc @@ -15,6 +15,82 @@ #include "mem_config.h" +#define PROGRAM_NAME "memping" +#define PROGRAM_DESCRIPTION "Ping a server or a set of servers." +#define PROGRAM_VERSION "1.1" + +#include "common/options.hpp" +#include "common/checks.hpp" + +#include "libmemcached/util.h" + +static memcached_return_t ping(const memcached_st *memc, const memcached_instance_st *s, void *ctx) { + auto opt = static_cast(ctx); + memcached_return_t rc; + bool ok; + + if (opt->isset("debug")) { + std::cerr << "Trying to ping" << s->_hostname << ":" << s->port() << ".\n"; + } + + if (auto username = opt->argof("username")) { + auto password = opt->argof("password"); + ok = libmemcached_util_ping2(s->_hostname, s->port(), username, password, &rc); + } else { + ok = libmemcached_util_ping(s->_hostname, s->port(), &rc); + } + if (!ok && !opt->isset("quiet")) { + std::cerr << "Failed to ping '" << s->_hostname << ":" << s->port() + << ":" << memcached_strerror(memc, rc) << ".\n"; + } + + return rc; +} + +int main(int argc, char *argv[]) { + client_options opt{PROGRAM_NAME, PROGRAM_VERSION, PROGRAM_DESCRIPTION}; + + for (const auto &def : opt.defaults) { + switch (def.opt.val) { + case 'h': // --help + case 'V': // --version + case 'v': // --verbose + case 'd': // --debug + case 'q': // --quiet + case 's': // --servers + case 'u': // --username + case 'p': // --password + opt.add(def); + break; + default: + break; + } + } + + if (!opt.parse(argc, argv, nullptr)) { + exit(EXIT_FAILURE); + } + + memcached_st memc; + if (!check_memcached(opt, memc)) { + exit(EXIT_FAILURE); + } + + if (!opt.apply(&memc)) { + memcached_free(&memc); + exit(EXIT_FAILURE); + } + + auto exit_code = EXIT_SUCCESS; + memcached_server_fn cb[1] = {&ping}; + if (!memcached_success(memcached_server_cursor(&memc, cb, &opt, 1))) { + exit_code = EXIT_FAILURE; + } + + memcached_free(&memc); + exit(exit_code); +} + #include #include #include