Fix for connect() to invalidate socket on failure.
[m6w6/libmemcached] / lib / memcached_stats.c
1 /*
2 */
3
4 #include "common.h"
5
6 static char *memcached_stat_keys[] = {
7 "pid",
8 "uptime",
9 "time",
10 "version",
11 "pointer_size",
12 "rusage_user",
13 "rusage_system",
14 "curr_items",
15 "total_items",
16 "bytes",
17 "curr_connections",
18 "total_connections",
19 "connection_structures",
20 "cmd_get",
21 "cmd_set",
22 "get_hits",
23 "get_misses",
24 "evictions",
25 "bytes_read",
26 "bytes_written",
27 "limit_maxbytes",
28 "threads",
29 NULL
30 };
31
32
33 static void set_data(memcached_stat_st *stat, char *key, char *value)
34 {
35
36 if(strlen(key) < 1)
37 {
38 fprintf(stderr, "Invalid key %s\n", key);
39 }
40 else if (!strcmp("pid", key))
41 {
42 stat->pid= strtol(value, (char **)NULL, 10);
43 }
44 else if (!strcmp("uptime", key))
45 {
46 stat->uptime= strtol(value, (char **)NULL, 10);
47 }
48 else if (!strcmp("time", key))
49 {
50 stat->time= strtol(value, (char **)NULL, 10);
51 }
52 else if (!strcmp("version", key))
53 {
54 memcpy(stat->version, value, strlen(value));
55 stat->version[strlen(value)]= 0;
56 }
57 else if (!strcmp("pointer_size", key))
58 {
59 stat->pointer_size= strtol(value, (char **)NULL, 10);
60 }
61 else if (!strcmp("rusage_user", key))
62 {
63 char *walk_ptr;
64 for (walk_ptr= value; (!ispunct(*walk_ptr)); walk_ptr++);
65 *walk_ptr= 0;
66 walk_ptr++;
67 stat->rusage_user_seconds= strtol(value, (char **)NULL, 10);
68 stat->rusage_user_microseconds= strtol(walk_ptr, (char **)NULL, 10);
69 }
70 else if (!strcmp("rusage_system", key))
71 {
72 char *walk_ptr;
73 for (walk_ptr= value; (!ispunct(*walk_ptr)); walk_ptr++);
74 *walk_ptr= 0;
75 walk_ptr++;
76 stat->rusage_system_seconds= strtol(value, (char **)NULL, 10);
77 stat->rusage_system_microseconds= strtol(walk_ptr, (char **)NULL, 10);
78 }
79 else if (!strcmp("curr_items", key))
80 {
81 stat->curr_items= strtol(value, (char **)NULL, 10);
82 }
83 else if (!strcmp("total_items", key))
84 {
85 stat->total_items= strtol(value, (char **)NULL, 10);
86 }
87 else if (!strcmp("bytes", key))
88 {
89 stat->bytes= strtoll(value, (char **)NULL, 10);
90 }
91 else if (!strcmp("curr_connections", key))
92 {
93 stat->curr_connections= strtoll(value, (char **)NULL, 10);
94 }
95 else if (!strcmp("total_connections", key))
96 {
97 stat->total_connections= strtoll(value, (char **)NULL, 10);
98 }
99 else if (!strcmp("connection_structures", key))
100 {
101 stat->connection_structures= strtol(value, (char **)NULL, 10);
102 }
103 else if (!strcmp("cmd_get", key))
104 {
105 stat->cmd_get= strtoll(value, (char **)NULL, 10);
106 }
107 else if (!strcmp("cmd_set", key))
108 {
109 stat->cmd_set= strtoll(value, (char **)NULL, 10);
110 }
111 else if (!strcmp("get_hits", key))
112 {
113 stat->get_hits= strtoll(value, (char **)NULL, 10);
114 }
115 else if (!strcmp("get_misses", key))
116 {
117 stat->get_misses= (uint64_t)strtoll(value, (char **)NULL, 10);
118 }
119 else if (!strcmp("evictions", key))
120 {
121 stat->evictions= (uint64_t)strtoll(value, (char **)NULL, 10);
122 }
123 else if (!strcmp("bytes_read", key))
124 {
125 stat->bytes_read= strtoll(value, (char **)NULL, 10);
126 }
127 else if (!strcmp("bytes_written", key))
128 {
129 stat->bytes_written= strtoll(value, (char **)NULL, 10);
130 }
131 else if (!strcmp("limit_maxbytes", key))
132 {
133 stat->limit_maxbytes= strtol(value, (char **)NULL, 10);
134 }
135 else if (!strcmp("threads", key))
136 {
137 stat->threads= strtol(key, (char **)NULL, 10);
138 }
139 else
140 {
141 fprintf(stderr, "Unknown key %s\n", key);
142 }
143 }
144
145 char *memcached_stat_get_value(memcached_st *ptr, memcached_stat_st *stat,
146 char *key, memcached_return *error)
147 {
148 char buffer[SMALL_STRING_LEN];
149 *error= MEMCACHED_SUCCESS;
150
151 if (!memcmp("pid", key, strlen("pid")))
152 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->pid);
153 else if (!memcmp("uptime", key, strlen("uptime")))
154 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->uptime);
155 else if (!memcmp("time", key, strlen("time")))
156 snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->time);
157 else if (!memcmp("version", key, strlen("version")))
158 snprintf(buffer, SMALL_STRING_LEN,"%s", stat->version);
159 else if (!memcmp("pointer_size", key, strlen("pointer_size")))
160 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->pointer_size);
161 else if (!memcmp("rusage_user", key, strlen("rusage_user")))
162 snprintf(buffer, SMALL_STRING_LEN,"%u.%u", stat->rusage_user_seconds, stat->rusage_user_microseconds);
163 else if (!memcmp("rusage_system", key, strlen("rusage_system")))
164 snprintf(buffer, SMALL_STRING_LEN,"%u.%u", stat->rusage_system_seconds, stat->rusage_system_microseconds);
165 else if (!memcmp("curr_items", key, strlen("curr_items")))
166 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->curr_items);
167 else if (!memcmp("total_items", key, strlen("total_items")))
168 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->total_items);
169 else if (!memcmp("bytes", key, strlen("bytes")))
170 snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->bytes);
171 else if (!memcmp("curr_connections", key, strlen("curr_connections")))
172 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->curr_connections);
173 else if (!memcmp("total_connections", key, strlen("total_connections")))
174 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->total_connections);
175 else if (!memcmp("connection_structures", key, strlen("connection_structures")))
176 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->connection_structures);
177 else if (!memcmp("cmd_get", key, strlen("cmd_get")))
178 snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->cmd_get);
179 else if (!memcmp("cmd_set", key, strlen("cmd_set")))
180 snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->cmd_set);
181 else if (!memcmp("get_hits", key, strlen("get_hits")))
182 snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->get_hits);
183 else if (!memcmp("get_misses", key, strlen("get_misses")))
184 snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->get_misses);
185 else if (!memcmp("evictions", key, strlen("evictions")))
186 snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->evictions);
187 else if (!memcmp("bytes_read", key, strlen("bytes_read")))
188 snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->bytes_read);
189 else if (!memcmp("bytes_written", key, strlen("bytes_written")))
190 snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->bytes_written);
191 else if (!memcmp("limit_maxbytes", key, strlen("limit_maxbytes")))
192 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->limit_maxbytes);
193 else if (!memcmp("threads", key, strlen("threads")))
194 snprintf(buffer, SMALL_STRING_LEN,"%u", stat->threads);
195 else
196 {
197 *error= MEMCACHED_NOTFOUND;
198 return NULL;
199 }
200
201 return strdup(buffer);
202 }
203
204 static memcached_return memcached_stats_fetch(memcached_st *ptr,
205 memcached_stat_st *stat,
206 char *args,
207 unsigned int server_key)
208 {
209 memcached_return rc;
210 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
211 size_t send_length;
212
213 if (args)
214 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
215 "stats %s\r\n", args);
216 else
217 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
218 "stats\r\n");
219
220 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
221 return MEMCACHED_WRITE_FAILURE;
222
223 rc= memcached_do(ptr, server_key, buffer, send_length, 1);
224 if (rc != MEMCACHED_SUCCESS)
225 goto error;
226
227 while (1)
228 {
229 rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL, server_key);
230
231 if (rc == MEMCACHED_STAT)
232 {
233 char *string_ptr, *end_ptr;
234 char *key, *value;
235
236 string_ptr= buffer;
237 string_ptr+= 5; /* Move past STAT */
238 for (end_ptr= string_ptr; isgraph(*end_ptr); end_ptr++);
239 key= string_ptr;
240 key[(size_t)(end_ptr-string_ptr)]= 0;
241
242 string_ptr= end_ptr + 1;
243 for (end_ptr= string_ptr; !(isspace(*end_ptr)); end_ptr++);
244 value= string_ptr;
245 value[(size_t)(end_ptr-string_ptr)]= 0;
246 string_ptr= end_ptr + 2;
247 set_data(stat, key, value);
248 }
249 else
250 break;
251 }
252
253 error:
254 if (rc == MEMCACHED_END)
255 return MEMCACHED_SUCCESS;
256 else
257 return rc;
258 }
259
260 memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_return *error)
261 {
262 unsigned int x;
263 memcached_return rc;
264 memcached_stat_st *stats;
265
266 stats= (memcached_stat_st *)malloc(sizeof(memcached_stat_st)*(ptr->number_of_hosts));
267 if (!stats)
268 {
269 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
270 free(stats);
271 return NULL;
272 }
273 memset(stats, 0, sizeof(memcached_stat_st)*(ptr->number_of_hosts));
274
275 rc= MEMCACHED_SUCCESS;
276 for (x= 0; x < ptr->number_of_hosts; x++)
277 {
278 memcached_return temp_return;
279
280 temp_return= memcached_stats_fetch(ptr, stats + x, args, x);
281 if (temp_return != MEMCACHED_SUCCESS)
282 rc= MEMCACHED_SOME_ERRORS;
283 }
284
285 *error= rc;
286 return stats;
287 }
288
289 memcached_return memcached_stat_servername(memcached_stat_st *stat, char *args,
290 char *hostname, unsigned int port)
291 {
292 memcached_return rc;
293 memcached_st memc;
294
295 memcached_create(&memc);
296
297 memcached_server_add(&memc, hostname, port);
298
299 rc= memcached_stats_fetch(&memc, stat, args, 0);
300
301 memcached_free(&memc);
302
303 return rc;
304 }
305
306 /*
307 We make a copy of the keys since at some point in the not so distant future
308 we will add support for "found" keys.
309 */
310 char ** memcached_stat_get_keys(memcached_st *ptr, memcached_stat_st *stat,
311 memcached_return *error)
312 {
313 char **list;
314 size_t length= sizeof(memcached_stat_keys);
315 list= (char **)malloc(length);
316
317 if (!list)
318 {
319 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
320 return NULL;
321 }
322 memset(list, 0, sizeof(memcached_stat_keys));
323
324 memcpy(list, memcached_stat_keys, sizeof(memcached_stat_keys));
325
326 *error= MEMCACHED_SUCCESS;
327
328 return list;
329 }
330
331 void memcached_stat_free(memcached_st *ptr, memcached_stat_st *stat)
332 {
333 free(stat);
334 }