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"
18 #include <libmemcached-1.0/memcached.h>
19 #include <libmemcachedutil-1.0/util.h>
20 #include "client_options.h"
21 #include "utilities.h"
25 static bool opt_binary
= false;
26 static int opt_verbose
= 0;
27 static time_t opt_expire
= 0;
28 static char *opt_servers
= NULL
;
29 static char *opt_username
;
30 static char *opt_passwd
;
32 #define PROGRAM_NAME "memping"
33 #define PROGRAM_DESCRIPTION "Ping a server to see if it is alive"
36 void options_parse(int argc
, char *argv
[]);
38 int main(int argc
, char *argv
[])
40 options_parse(argc
, argv
);
42 if (opt_servers
== NULL
)
46 if ((temp
= getenv("MEMCACHED_SERVERS")))
48 opt_servers
= strdup(temp
);
52 std::cerr
<< "No Servers provided" << std::endl
;
57 int exit_code
= EXIT_SUCCESS
;
58 memcached_server_st
*servers
= memcached_servers_parse(opt_servers
);
60 for (uint32_t x
= 0; x
< memcached_server_list_count(servers
); x
++)
62 memcached_return_t instance_rc
;
63 const char *hostname
= servers
[x
].hostname
;
64 in_port_t port
= servers
[x
].port
;
66 if (libmemcached_util_ping2(hostname
, port
, opt_username
, opt_passwd
, &instance_rc
) == false)
68 std::cerr
<< "Failed to ping " << hostname
<< ":" << port
<< " " << memcached_strerror(NULL
, instance_rc
) << std::endl
;
69 exit_code
= EXIT_FAILURE
;
73 memcached_server_list_free(servers
);
81 void options_parse(int argc
, char *argv
[])
83 memcached_programs_help_st help_options
[]=
88 static struct option long_options
[]=
90 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
91 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
92 {(OPTIONSTRING
)"quiet", no_argument
, NULL
, OPT_QUIET
},
93 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
94 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
95 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
96 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
97 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
98 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
99 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
103 bool opt_version
= false;
104 bool opt_help
= false;
108 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
110 if (option_rv
== -1) break;
121 case OPT_VERBOSE
: /* --verbose or -v */
122 opt_verbose
= OPT_VERBOSE
;
125 case OPT_DEBUG
: /* --debug or -d */
126 opt_verbose
= OPT_DEBUG
;
129 case OPT_VERSION
: /* --version or -V */
130 version_command(PROGRAM_NAME
);
133 case OPT_HELP
: /* --help or -h */
134 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
137 case OPT_SERVERS
: /* --servers or -s */
138 opt_servers
= strdup(optarg
);
141 case OPT_EXPIRE
: /* --expire */
142 opt_expire
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
146 opt_username
= optarg
;
159 /* getopt_long already printed an error message. */
168 version_command(PROGRAM_NAME
);
174 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);