2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
17 #include <libmemcached/memcached.h>
18 #include <libmemcached/util.h>
19 #include "client_options.h"
20 #include "utilities.h"
24 static bool opt_binary
= false;
25 static int opt_verbose
= 0;
26 static time_t opt_expire
= 0;
27 static char *opt_servers
= NULL
;
28 static char *opt_username
;
29 static char *opt_passwd
;
31 #define PROGRAM_NAME "memping"
32 #define PROGRAM_DESCRIPTION "Ping a server to see if it is alive"
35 void options_parse(int argc
, char *argv
[]);
37 int main(int argc
, char *argv
[])
39 options_parse(argc
, argv
);
41 if (opt_servers
== NULL
)
45 if ((temp
= getenv("MEMCACHED_SERVERS")))
47 opt_servers
= strdup(temp
);
51 std::cerr
<< "No Servers provided" << std::endl
;
56 int exit_code
= EXIT_SUCCESS
;
57 memcached_server_st
*servers
= memcached_servers_parse(opt_servers
);
59 for (uint32_t x
= 0; x
< memcached_server_list_count(servers
); x
++)
61 memcached_return_t instance_rc
;
62 const char *hostname
= servers
[x
].hostname
;
63 in_port_t port
= servers
[x
].port
;
65 if (libmemcached_util_ping2(hostname
, port
, opt_username
, opt_passwd
, &instance_rc
) == false)
67 std::cerr
<< "Failed to ping " << hostname
<< ":" << port
<< " " << memcached_strerror(NULL
, instance_rc
) << std::endl
;
68 exit_code
= EXIT_FAILURE
;
72 memcached_server_list_free(servers
);
80 void options_parse(int argc
, char *argv
[])
82 memcached_programs_help_st help_options
[]=
87 static struct option long_options
[]=
89 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
90 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
91 {(OPTIONSTRING
)"quiet", no_argument
, NULL
, OPT_QUIET
},
92 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
93 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
94 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
95 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
96 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
97 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
98 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
102 bool opt_version
= false;
103 bool opt_help
= false;
107 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
109 if (option_rv
== -1) break;
120 case OPT_VERBOSE
: /* --verbose or -v */
121 opt_verbose
= OPT_VERBOSE
;
124 case OPT_DEBUG
: /* --debug or -d */
125 opt_verbose
= OPT_DEBUG
;
128 case OPT_VERSION
: /* --version or -V */
129 version_command(PROGRAM_NAME
);
132 case OPT_HELP
: /* --help or -h */
133 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
136 case OPT_SERVERS
: /* --servers or -s */
137 opt_servers
= strdup(optarg
);
140 case OPT_EXPIRE
: /* --expire */
141 opt_expire
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
145 opt_username
= optarg
;
157 /* getopt_long already printed an error message. */
166 version_command(PROGRAM_NAME
);
172 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);