Refactor of generate tests.
[awesomized/libmemcached] / src / 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 <assert.h>
9 #include <getopt.h>
10
11 #include <memcached.h>
12
13 #include "client_options.h"
14 #include "utilities.h"
15
16 #define PROGRAM_NAME "memstat"
17 #define PROGRAM_DESCRIPTION "Output the state of a memcached cluster."
18
19 /* Prototypes */
20 void options_parse(int argc, char *argv[]);
21
22 static int opt_verbose= 0;
23 static int opt_displayflag= 0;
24 static char *opt_servers= NULL;
25
26 int main(int argc, char *argv[])
27 {
28 unsigned int x;
29 memcached_return rc;
30 memcached_st *memc;
31 memcached_stat_st *stat;
32 memcached_server_st *servers;
33 memcached_server_st *server_list;
34
35 options_parse(argc, argv);
36
37 if (!opt_servers)
38 {
39 char *temp;
40
41 if ((temp= getenv("MEMCACHED_SERVERS")))
42 opt_servers= strdup(temp);
43 else
44 exit(1);
45 }
46
47 memc= memcached_create(NULL);
48
49 servers= memcached_servers_parse(opt_servers);
50 memcached_server_push(memc, servers);
51 memcached_server_list_free(servers);
52
53 stat= memcached_stat(memc, NULL, &rc);
54
55 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_SOME_ERRORS)
56 {
57 printf("Failure to communicate with servers (%s)\n",
58 memcached_strerror(memc, rc));
59 exit(1);
60 }
61
62 server_list= memcached_server_list(memc);
63
64 printf("Listing %u Server\n\n", memcached_server_count(memc));
65 for (x= 0; x < memcached_server_count(memc); x++)
66 {
67 char **list;
68 char **ptr;
69
70 list= memcached_stat_get_keys(memc, &stat[x], &rc);
71 assert(list);
72 assert(rc == MEMCACHED_SUCCESS);
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 }