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
);
54 std::cerr
<< "No Servers provided" << std::endl
;
59 int exit_code
= EXIT_SUCCESS
;
60 memcached_server_st
*servers
= memcached_servers_parse(opt_servers
);
62 for (uint32_t x
= 0; x
< memcached_server_list_count(servers
); x
++)
64 memcached_return_t instance_rc
;
65 const char *hostname
= servers
[x
].hostname
;
66 in_port_t port
= servers
[x
].port
;
68 if (libmemcached_util_ping2(hostname
, port
, opt_username
, opt_passwd
, &instance_rc
) == false)
70 std::cerr
<< "Failed to ping " << hostname
<< ":" << port
<< " " << memcached_strerror(NULL
, instance_rc
) << std::endl
;
71 exit_code
= EXIT_FAILURE
;
75 memcached_server_list_free(servers
);
83 void options_parse(int argc
, char *argv
[])
85 memcached_programs_help_st help_options
[]=
90 static struct option long_options
[]=
92 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
93 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
94 {(OPTIONSTRING
)"quiet", no_argument
, NULL
, OPT_QUIET
},
95 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
96 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
97 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
98 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
99 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
100 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
101 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
105 bool opt_version
= false;
106 bool opt_help
= false;
110 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
112 if (option_rv
== -1) break;
123 case OPT_VERBOSE
: /* --verbose or -v */
124 opt_verbose
= OPT_VERBOSE
;
127 case OPT_DEBUG
: /* --debug or -d */
128 opt_verbose
= OPT_DEBUG
;
131 case OPT_VERSION
: /* --version or -V */
132 version_command(PROGRAM_NAME
);
135 case OPT_HELP
: /* --help or -h */
136 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
139 case OPT_SERVERS
: /* --servers or -s */
140 opt_servers
= strdup(optarg
);
143 case OPT_EXPIRE
: /* --expire */
145 opt_expire
= time_t(strtoll(optarg
, (char **)NULL
, 10));
148 std::cerr
<< "Incorrect value passed to --expire: `" << optarg
<< "`" << std::cerr
;
154 opt_username
= optarg
;
167 /* getopt_long already printed an error message. */
176 version_command(PROGRAM_NAME
);
182 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);