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