2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
16 #include "mem_config.h"
24 #include "libmemcached-1.0/memcached.h"
25 #include "libmemcachedutil-1.0/util.h"
26 #include "client_options.h"
27 #include "utilities.h"
31 static bool opt_binary
= false;
32 static int opt_verbose
= 0;
33 static time_t opt_expire
= 0;
34 static char *opt_servers
= NULL
;
35 static char *opt_username
;
36 static char *opt_passwd
;
38 #define PROGRAM_NAME "memping"
39 #define PROGRAM_DESCRIPTION "Ping a server to see if it is alive"
42 void options_parse(int argc
, char *argv
[]);
44 int main(int argc
, char *argv
[]) {
45 options_parse(argc
, argv
);
47 if (opt_servers
== NULL
) {
50 if ((temp
= getenv("MEMCACHED_SERVERS"))) {
51 opt_servers
= strdup(temp
);
54 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) {
63 std::cerr
<< "Invalid server list provided:" << opt_servers
<< std::endl
;
64 exit_code
= EXIT_FAILURE
;
66 for (uint32_t x
= 0; x
< memcached_server_list_count(servers
); x
++) {
67 memcached_return_t instance_rc
;
68 const char *hostname
= servers
[x
].hostname
;
69 in_port_t port
= servers
[x
].port
;
72 std::cout
<< "Trying to ping " << hostname
<< ":" << port
<< std::endl
;
75 if (libmemcached_util_ping2(hostname
, port
, opt_username
, opt_passwd
, &instance_rc
) == false)
77 std::cerr
<< "Failed to ping " << hostname
<< ":" << port
<< " "
78 << memcached_strerror(NULL
, instance_rc
) << std::endl
;
79 exit_code
= EXIT_FAILURE
;
83 memcached_server_list_free(servers
);
90 void options_parse(int argc
, char *argv
[]) {
91 memcached_programs_help_st help_options
[] = {
95 static struct option long_options
[] = {
96 {(OPTIONSTRING
) "version", no_argument
, NULL
, OPT_VERSION
},
97 {(OPTIONSTRING
) "help", no_argument
, NULL
, OPT_HELP
},
98 {(OPTIONSTRING
) "quiet", no_argument
, NULL
, OPT_QUIET
},
99 {(OPTIONSTRING
) "verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
100 {(OPTIONSTRING
) "debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
101 {(OPTIONSTRING
) "servers", required_argument
, NULL
, OPT_SERVERS
},
102 {(OPTIONSTRING
) "expire", required_argument
, NULL
, OPT_EXPIRE
},
103 {(OPTIONSTRING
) "binary", no_argument
, NULL
, OPT_BINARY
},
104 {(OPTIONSTRING
) "username", required_argument
, NULL
, OPT_USERNAME
},
105 {(OPTIONSTRING
) "password", required_argument
, NULL
, OPT_PASSWD
},
109 bool opt_version
= false;
110 bool opt_help
= false;
111 int option_index
= 0;
113 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
126 case OPT_VERBOSE
: /* --verbose or -v */
127 opt_verbose
= OPT_VERBOSE
;
130 case OPT_DEBUG
: /* --debug or -d */
131 opt_verbose
= OPT_DEBUG
;
134 case OPT_VERSION
: /* --version or -V */
135 version_command(PROGRAM_NAME
);
138 case OPT_HELP
: /* --help or -h */
139 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
142 case OPT_SERVERS
: /* --servers or -s */
143 opt_servers
= strdup(optarg
);
146 case OPT_EXPIRE
: /* --expire */
148 opt_expire
= time_t(strtoll(optarg
, (char **) NULL
, 10));
150 std::cerr
<< "Incorrect value passed to --expire: `" << optarg
<< "`" << std::endl
;
156 opt_username
= optarg
;
169 /* getopt_long already printed an error message. */
177 version_command(PROGRAM_NAME
);
182 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);