Merge lp:~tangent-org/libmemcached/1.0-build/ Build: jenkins-Libmemcached-187
[m6w6/libmemcached] / clients / memping.cc
1 /* LibMemcached
2 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
3 * Copyright (C) 2006-2009 Brian Aker
4 * All rights reserved.
5 *
6 * Use and distribution licensed under the BSD license. See
7 * the COPYING file in the parent directory for full text.
8 *
9 * Summary:
10 *
11 */
12 #include "mem_config.h"
13
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <getopt.h>
18 #include <libmemcached-1.0/memcached.h>
19 #include <libmemcachedutil-1.0/util.h>
20 #include "client_options.h"
21 #include "utilities.h"
22
23 #include <iostream>
24
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;
31
32 #define PROGRAM_NAME "memping"
33 #define PROGRAM_DESCRIPTION "Ping a server to see if it is alive"
34
35 /* Prototypes */
36 void options_parse(int argc, char *argv[]);
37
38 int main(int argc, char *argv[])
39 {
40 options_parse(argc, argv);
41
42 if (opt_servers == NULL)
43 {
44 char *temp;
45
46 if ((temp= getenv("MEMCACHED_SERVERS")))
47 {
48 opt_servers= strdup(temp);
49 }
50 else
51 {
52 std::cerr << "No Servers provided" << std::endl;
53 exit(EXIT_FAILURE);
54 }
55 }
56
57 int exit_code= EXIT_SUCCESS;
58 memcached_server_st *servers= memcached_servers_parse(opt_servers);
59 {
60 for (uint32_t x= 0; x < memcached_server_list_count(servers); x++)
61 {
62 memcached_return_t instance_rc;
63 const char *hostname= servers[x].hostname;
64 in_port_t port= servers[x].port;
65
66 if (libmemcached_util_ping2(hostname, port, opt_username, opt_passwd, &instance_rc) == false)
67 {
68 std::cerr << "Failed to ping " << hostname << ":" << port << " " << memcached_strerror(NULL, instance_rc) << std::endl;
69 exit_code= EXIT_FAILURE;
70 }
71 }
72 }
73 memcached_server_list_free(servers);
74
75 free(opt_servers);
76
77 return exit_code;
78 }
79
80
81 void options_parse(int argc, char *argv[])
82 {
83 memcached_programs_help_st help_options[]=
84 {
85 {0},
86 };
87
88 static struct option long_options[]=
89 {
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},
100 {0, 0, 0, 0},
101 };
102
103 bool opt_version= false;
104 bool opt_help= false;
105 int option_index= 0;
106 while (1)
107 {
108 int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
109
110 if (option_rv == -1) break;
111
112 switch (option_rv)
113 {
114 case 0:
115 break;
116
117 case OPT_BINARY:
118 opt_binary= true;
119 break;
120
121 case OPT_VERBOSE: /* --verbose or -v */
122 opt_verbose = OPT_VERBOSE;
123 break;
124
125 case OPT_DEBUG: /* --debug or -d */
126 opt_verbose = OPT_DEBUG;
127 break;
128
129 case OPT_VERSION: /* --version or -V */
130 version_command(PROGRAM_NAME);
131 break;
132
133 case OPT_HELP: /* --help or -h */
134 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
135 break;
136
137 case OPT_SERVERS: /* --servers or -s */
138 opt_servers= strdup(optarg);
139 break;
140
141 case OPT_EXPIRE: /* --expire */
142 opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
143 break;
144
145 case OPT_USERNAME:
146 opt_username= optarg;
147 opt_binary= true;
148 break;
149
150 case OPT_PASSWD:
151 opt_passwd= optarg;
152 break;
153
154 case OPT_QUIET:
155 close_stdio();
156 break;
157
158 case '?':
159 /* getopt_long already printed an error message. */
160 exit(1);
161 default:
162 abort();
163 }
164 }
165
166 if (opt_version)
167 {
168 version_command(PROGRAM_NAME);
169 exit(EXIT_SUCCESS);
170 }
171
172 if (opt_help)
173 {
174 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
175 exit(EXIT_SUCCESS);
176 }
177 }