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