testing: more memcp options
[awesomized/libmemcached] / src / bin / memping.cc
1 /*
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 +--------------------------------------------------------------------+
14 */
15
16 #include "mem_config.h"
17
18 #include <cerrno>
19 #include <cstdio>
20 #include <cstring>
21 #include <getopt.h>
22 #include <unistd.h>
23
24 #include "libmemcached-1.0/memcached.h"
25 #include "libmemcachedutil-1.0/util.h"
26 #include "client_options.h"
27 #include "utilities.h"
28
29 #include <iostream>
30
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;
37
38 #define PROGRAM_NAME "memping"
39 #define PROGRAM_DESCRIPTION "Ping a server to see if it is alive"
40
41 /* Prototypes */
42 void options_parse(int argc, char *argv[]);
43
44 int main(int argc, char *argv[]) {
45 options_parse(argc, argv);
46
47 if (opt_servers == NULL) {
48 char *temp;
49
50 if ((temp = getenv("MEMCACHED_SERVERS"))) {
51 opt_servers = strdup(temp);
52 }
53
54 if (opt_servers == NULL) {
55 std::cerr << "No Servers provided" << std::endl;
56 exit(EXIT_FAILURE);
57 }
58 }
59
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;
65 } else {
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;
70
71 if (opt_verbose) {
72 std::cout << "Trying to ping " << hostname << ":" << port << std::endl;
73 }
74
75 if (libmemcached_util_ping2(hostname, port, opt_username, opt_passwd, &instance_rc) == false)
76 {
77 std::cerr << "Failed to ping " << hostname << ":" << port << " "
78 << memcached_strerror(NULL, instance_rc) << std::endl;
79 exit_code = EXIT_FAILURE;
80 }
81 }
82 }
83 memcached_server_list_free(servers);
84
85 free(opt_servers);
86
87 return exit_code;
88 }
89
90 void options_parse(int argc, char *argv[]) {
91 memcached_programs_help_st help_options[] = {
92 {0},
93 };
94
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},
106 {0, 0, 0, 0},
107 };
108
109 bool opt_version = false;
110 bool opt_help = false;
111 int option_index = 0;
112 while (1) {
113 int option_rv = getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
114
115 if (option_rv == -1)
116 break;
117
118 switch (option_rv) {
119 case 0:
120 break;
121
122 case OPT_BINARY:
123 opt_binary = true;
124 break;
125
126 case OPT_VERBOSE: /* --verbose or -v */
127 opt_verbose = OPT_VERBOSE;
128 break;
129
130 case OPT_DEBUG: /* --debug or -d */
131 opt_verbose = OPT_DEBUG;
132 break;
133
134 case OPT_VERSION: /* --version or -V */
135 version_command(PROGRAM_NAME);
136 break;
137
138 case OPT_HELP: /* --help or -h */
139 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
140 break;
141
142 case OPT_SERVERS: /* --servers or -s */
143 opt_servers = strdup(optarg);
144 break;
145
146 case OPT_EXPIRE: /* --expire */
147 errno = 0;
148 opt_expire = time_t(strtoll(optarg, (char **) NULL, 10));
149 if (errno) {
150 std::cerr << "Incorrect value passed to --expire: `" << optarg << "`" << std::endl;
151 exit(EXIT_FAILURE);
152 }
153 break;
154
155 case OPT_USERNAME:
156 opt_username = optarg;
157 opt_binary = true;
158 break;
159
160 case OPT_PASSWD:
161 opt_passwd = optarg;
162 break;
163
164 case OPT_QUIET:
165 close_stdio();
166 break;
167
168 case '?':
169 /* getopt_long already printed an error message. */
170 exit(1);
171 default:
172 abort();
173 }
174 }
175
176 if (opt_version) {
177 version_command(PROGRAM_NAME);
178 exit(EXIT_SUCCESS);
179 }
180
181 if (opt_help) {
182 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
183 exit(EXIT_SUCCESS);
184 }
185 }