d6e315078749285fd56bda874cce033c8629d84b
[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 <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 server_list= memcached_server_list(memc);
39
40 printf("Listing %u Server\n\n", memcached_server_count(memc));
41 for (x= 0; x < memcached_server_count(memc); x++)
42 {
43 char **list;
44 char **ptr;
45
46 list= memcached_stat_get_keys(memc, &stat[x], &rc);
47 assert(rc == MEMCACHED_SUCCESS);
48
49 printf("Server: %s (%u)\n", memcached_server_name(memc, server_list[x]),
50 memcached_server_port(memc, server_list[x]));
51 for (ptr= list; *ptr; ptr++)
52 {
53 memcached_return rc;
54 char *value= memcached_stat_get_value(memc, &stat[x], *ptr, &rc);
55
56 printf("\t %s: %s\n", *ptr, value);
57 free(value);
58 }
59
60 free(list);
61 printf("\n");
62 }
63
64 free(stat);
65 free(opt_servers);
66 }
67
68 memcached_deinit(memc);
69
70 return 0;
71 }
72
73 void options_parse(int argc, char *argv[])
74 {
75 static struct option long_options[]=
76 {
77 {"version", no_argument, NULL, OPT_VERSION},
78 {"help", no_argument, NULL, OPT_HELP},
79 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
80 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
81 {"servers", required_argument, NULL, OPT_SERVERS},
82 {"flag", no_argument, &opt_displayflag, OPT_FLAG},
83 {0, 0, 0, 0},
84 };
85
86 int option_index= 0;
87 int option_rv;
88
89 while (1)
90 {
91 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
92 if (option_rv == -1) break;
93 switch (option_rv)
94 {
95 case 0:
96 break;
97 case OPT_VERBOSE: /* --verbose or -v */
98 opt_verbose = OPT_VERBOSE;
99 break;
100 case OPT_DEBUG: /* --debug or -d */
101 opt_verbose = OPT_DEBUG;
102 break;
103 case OPT_VERSION: /* --version or -V */
104 printf("memcache tools, memcat, v1.0\n");
105 exit(0);
106 break;
107 case OPT_HELP: /* --help or -h */
108 printf("useful help messages go here\n");
109 exit(0);
110 break;
111 case OPT_SERVERS: /* --servers or -s */
112 opt_servers= strdup(optarg);
113 break;
114 case '?':
115 /* getopt_long already printed an error message. */
116 exit(1);
117 default:
118 abort();
119 }
120 }
121 }