Merge in sasl update
[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 int opt_binary= 0;
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 shutdown_sasl();
77
78 return exit_code;
79 }
80
81
82 void options_parse(int argc, char *argv[])
83 {
84 memcached_programs_help_st help_options[]=
85 {
86 {0},
87 };
88
89 static struct option long_options[]=
90 {
91 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
92 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
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 int option_index= 0;
103 int option_rv;
104
105 while (1)
106 {
107 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
108 if (option_rv == -1) break;
109 switch (option_rv)
110 {
111 case 0:
112 break;
113 case OPT_BINARY:
114 opt_binary = 1;
115 break;
116 case OPT_VERBOSE: /* --verbose or -v */
117 opt_verbose = OPT_VERBOSE;
118 break;
119 case OPT_DEBUG: /* --debug or -d */
120 opt_verbose = OPT_DEBUG;
121 break;
122 case OPT_VERSION: /* --version or -V */
123 version_command(PROGRAM_NAME);
124 break;
125 case OPT_HELP: /* --help or -h */
126 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
127 break;
128 case OPT_SERVERS: /* --servers or -s */
129 opt_servers= strdup(optarg);
130 break;
131 case OPT_EXPIRE: /* --expire */
132 opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
133 break;
134 case OPT_USERNAME:
135 opt_username= optarg;
136 break;
137 case OPT_PASSWD:
138 opt_passwd= optarg;
139 break;
140 case '?':
141 /* getopt_long already printed an error message. */
142 exit(1);
143 default:
144 abort();
145 }
146 }
147 }