Added/restructured all additional hostname information
[m6w6/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 <getopt.h>
8
9 #include <memcached.h>
10
11 #include "client_options.h"
12 #include "utilities.h"
13
14 /* Prototypes */
15 void options_parse(int argc, char *argv[]);
16
17 static int opt_verbose= 0;
18 static int opt_displayflag= 0;
19 static char *opt_servers= NULL;
20
21 int main(int argc, char *argv[])
22 {
23 memcached_st *memc;
24
25 memc= memcached_init(NULL);
26 options_parse(argc, argv);
27
28 if (opt_servers)
29 {
30 unsigned int x;
31 memcached_return rc;
32 memcached_stat_st *stat;
33 memcached_server_st *server_list;
34
35 parse_opt_servers(memc, opt_servers);
36 stat= memcached_stat(memc, NULL, &rc);
37
38 if (rc != MEMCACHED_SUCCESS || rc != MEMCACHED_SOME_ERRORS);
39 {
40 printf("Failure to communicate with servers (%s)\n",
41 memcached_strerror(memc, rc));
42 exit(1);
43 }
44
45 server_list= memcached_server_list(memc);
46
47 printf("Listing %u Server\n\n", memcached_server_count(memc));
48 for (x= 0; x < memcached_server_count(memc); x++)
49 {
50 char **list;
51 char **ptr;
52
53 list= memcached_stat_get_keys(memc, &stat[x], &rc);
54 assert(list);
55 assert(rc == MEMCACHED_SUCCESS);
56
57 printf("Server: %s (%u)\n", memcached_server_name(memc, server_list[x]),
58 memcached_server_port(memc, server_list[x]));
59 for (ptr= list; *ptr; ptr++)
60 {
61 memcached_return rc;
62 char *value= memcached_stat_get_value(memc, &stat[x], *ptr, &rc);
63
64 printf("\t %s: %s\n", *ptr, value);
65 free(value);
66 }
67
68 free(list);
69 printf("\n");
70 }
71
72 free(stat);
73 free(opt_servers);
74 }
75
76 memcached_deinit(memc);
77
78 return 0;
79 }
80
81 void options_parse(int argc, char *argv[])
82 {
83 static struct option long_options[]=
84 {
85 {"version", no_argument, NULL, OPT_VERSION},
86 {"help", no_argument, NULL, OPT_HELP},
87 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
88 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
89 {"servers", required_argument, NULL, OPT_SERVERS},
90 {"flag", no_argument, &opt_displayflag, OPT_FLAG},
91 {0, 0, 0, 0},
92 };
93
94 int option_index= 0;
95 int option_rv;
96
97 while (1)
98 {
99 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
100 if (option_rv == -1) break;
101 switch (option_rv)
102 {
103 case 0:
104 break;
105 case OPT_VERBOSE: /* --verbose or -v */
106 opt_verbose = OPT_VERBOSE;
107 break;
108 case OPT_DEBUG: /* --debug or -d */
109 opt_verbose = OPT_DEBUG;
110 break;
111 case OPT_VERSION: /* --version or -V */
112 printf("memcache tools, memcat, v1.0\n");
113 exit(0);
114 break;
115 case OPT_HELP: /* --help or -h */
116 printf("useful help messages go here\n");
117 exit(0);
118 break;
119 case OPT_SERVERS: /* --servers or -s */
120 opt_servers= strdup(optarg);
121 break;
122 case '?':
123 /* getopt_long already printed an error message. */
124 exit(1);
125 default:
126 abort();
127 }
128 }
129 }