Search for pod2man during configure
[m6w6/libmemcached] / clients / memstat.c
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <sys/types.h>
5 #include <sys/mman.h>
6 #include <fcntl.h>
7 #include <string.h>
8 #include <getopt.h>
9
10 #include <libmemcached/memcached.h>
11
12 #include "client_options.h"
13 #include "utilities.h"
14
15 #define PROGRAM_NAME "memstat"
16 #define PROGRAM_DESCRIPTION "Output the state of a memcached cluster."
17
18 /* Prototypes */
19 void options_parse(int argc, char *argv[]);
20
21 static int opt_verbose= 0;
22 static int opt_displayflag= 0;
23 static char *opt_servers= NULL;
24
25 int main(int argc, char *argv[])
26 {
27 unsigned int x;
28 memcached_return rc;
29 memcached_st *memc;
30 memcached_stat_st *stat;
31 memcached_server_st *servers;
32 memcached_server_st *server_list;
33
34 options_parse(argc, argv);
35
36 if (!opt_servers)
37 {
38 char *temp;
39
40 if ((temp= getenv("MEMCACHED_SERVERS")))
41 opt_servers= strdup(temp);
42 else
43 {
44 fprintf(stderr, "No Servers provided\n");
45 exit(1);
46 }
47 }
48
49 memc= memcached_create(NULL);
50
51 servers= memcached_servers_parse(opt_servers);
52 memcached_server_push(memc, servers);
53 memcached_server_list_free(servers);
54
55 stat= memcached_stat(memc, NULL, &rc);
56
57 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_SOME_ERRORS)
58 {
59 printf("Failure to communicate with servers (%s)\n",
60 memcached_strerror(memc, rc));
61 exit(1);
62 }
63
64 server_list= memcached_server_list(memc);
65
66 printf("Listing %u Server\n\n", memcached_server_count(memc));
67 for (x= 0; x < memcached_server_count(memc); x++)
68 {
69 char **list;
70 char **ptr;
71
72 list= memcached_stat_get_keys(memc, &stat[x], &rc);
73
74 printf("Server: %s (%u)\n", memcached_server_name(memc, server_list[x]),
75 memcached_server_port(memc, server_list[x]));
76 for (ptr= list; *ptr; ptr++)
77 {
78 memcached_return rc;
79 char *value= memcached_stat_get_value(memc, &stat[x], *ptr, &rc);
80
81 printf("\t %s: %s\n", *ptr, value);
82 free(value);
83 }
84
85 free(list);
86 printf("\n");
87 }
88
89 free(stat);
90 free(opt_servers);
91
92 memcached_free(memc);
93
94 return 0;
95 }
96
97 void options_parse(int argc, char *argv[])
98 {
99 memcached_programs_help_st help_options[]=
100 {
101 {0},
102 };
103
104 static struct option long_options[]=
105 {
106 {"version", no_argument, NULL, OPT_VERSION},
107 {"help", no_argument, NULL, OPT_HELP},
108 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
109 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
110 {"servers", required_argument, NULL, OPT_SERVERS},
111 {"flag", no_argument, &opt_displayflag, OPT_FLAG},
112 {0, 0, 0, 0},
113 };
114
115 int option_index= 0;
116 int option_rv;
117
118 while (1)
119 {
120 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
121 if (option_rv == -1) break;
122 switch (option_rv)
123 {
124 case 0:
125 break;
126 case OPT_VERBOSE: /* --verbose or -v */
127 opt_verbose = OPT_VERBOSE;
128 break;
129 case OPT_DEBUG: /* --debug or -d */
130 opt_verbose = OPT_DEBUG;
131 break;
132 case OPT_VERSION: /* --version or -V */
133 version_command(PROGRAM_NAME);
134 break;
135 case OPT_HELP: /* --help or -h */
136 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
137 break;
138 case OPT_SERVERS: /* --servers or -s */
139 opt_servers= strdup(optarg);
140 break;
141 case '?':
142 /* getopt_long already printed an error message. */
143 exit(1);
144 default:
145 abort();
146 }
147 }
148 }