2 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
3 * Copyright (C) 2006-2009 Brian Aker
6 * Use and distribution licensed under the BSD license. See
7 * the COPYING file in the parent directory for full text.
12 #include "mem_config.h"
20 #include <libmemcached-1.0/memcached.h>
21 #include <libmemcachedutil-1.0/util.h>
22 #include "client_options.h"
23 #include "utilities.h"
27 static bool opt_binary
= false;
28 static int opt_verbose
= 0;
29 static time_t opt_expire
= 0;
30 static char *opt_servers
= NULL
;
31 static char *opt_username
;
32 static char *opt_passwd
;
34 #define PROGRAM_NAME "memping"
35 #define PROGRAM_DESCRIPTION "Ping a server to see if it is alive"
38 void options_parse(int argc
, char *argv
[]);
40 int main(int argc
, char *argv
[])
42 options_parse(argc
, argv
);
44 if (opt_servers
== NULL
)
48 if ((temp
= getenv("MEMCACHED_SERVERS")))
50 opt_servers
= strdup(temp
);
53 if (opt_servers
== NULL
)
55 std::cerr
<< "No Servers provided" << std::endl
;
60 int exit_code
= EXIT_SUCCESS
;
61 memcached_server_st
*servers
= memcached_servers_parse(opt_servers
);
62 if (servers
== NULL
or memcached_server_list_count(servers
) == 0)
64 std::cerr
<< "Invalid server list provided:" << opt_servers
<< std::endl
;
65 exit_code
= EXIT_FAILURE
;
69 for (uint32_t x
= 0; x
< memcached_server_list_count(servers
); x
++)
71 memcached_return_t instance_rc
;
72 const char *hostname
= servers
[x
].hostname
;
73 in_port_t port
= servers
[x
].port
;
77 std::cout
<< "Trying to ping " << hostname
<< ":" << port
<< std::endl
;
80 if (libmemcached_util_ping2(hostname
, port
, opt_username
, opt_passwd
, &instance_rc
) == false)
82 std::cerr
<< "Failed to ping " << hostname
<< ":" << port
<< " " << memcached_strerror(NULL
, instance_rc
) << std::endl
;
83 exit_code
= EXIT_FAILURE
;
87 memcached_server_list_free(servers
);
95 void options_parse(int argc
, char *argv
[])
97 memcached_programs_help_st help_options
[]=
102 static struct option long_options
[]=
104 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
105 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
106 {(OPTIONSTRING
)"quiet", no_argument
, NULL
, OPT_QUIET
},
107 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
108 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
109 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
110 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
111 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
112 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
113 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
117 bool opt_version
= false;
118 bool opt_help
= false;
122 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
124 if (option_rv
== -1) break;
135 case OPT_VERBOSE
: /* --verbose or -v */
136 opt_verbose
= OPT_VERBOSE
;
139 case OPT_DEBUG
: /* --debug or -d */
140 opt_verbose
= OPT_DEBUG
;
143 case OPT_VERSION
: /* --version or -V */
144 version_command(PROGRAM_NAME
);
147 case OPT_HELP
: /* --help or -h */
148 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
151 case OPT_SERVERS
: /* --servers or -s */
152 opt_servers
= strdup(optarg
);
155 case OPT_EXPIRE
: /* --expire */
157 opt_expire
= time_t(strtoll(optarg
, (char **)NULL
, 10));
160 std::cerr
<< "Incorrect value passed to --expire: `" << optarg
<< "`" << std::endl
;
166 opt_username
= optarg
;
179 /* getopt_long already printed an error message. */
188 version_command(PROGRAM_NAME
);
194 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);