3abc9b195e5391949f9111b93c9736eea6df12c0
[awesomized/libmemcached] / libmemcached / 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= strtoll(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 size_t length;
150 char *ret;
151
152 *error= MEMCACHED_SUCCESS;
153
154 if (!memcmp("pid", key, strlen("pid")))
155 length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->pid);
156 else if (!memcmp("uptime", key, strlen("uptime")))
157 length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->uptime);
158 else if (!memcmp("time", key, strlen("time")))
159 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->time);
160 else if (!memcmp("version", key, strlen("version")))
161 length= snprintf(buffer, SMALL_STRING_LEN,"%s", stat->version);
162 else if (!memcmp("pointer_size", key, strlen("pointer_size")))
163 length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->pointer_size);
164 else if (!memcmp("rusage_user", key, strlen("rusage_user")))
165 length= snprintf(buffer, SMALL_STRING_LEN,"%u.%u", stat->rusage_user_seconds, stat->rusage_user_microseconds);
166 else if (!memcmp("rusage_system", key, strlen("rusage_system")))
167 length= snprintf(buffer, SMALL_STRING_LEN,"%u.%u", stat->rusage_system_seconds, stat->rusage_system_microseconds);
168 else if (!memcmp("curr_items", key, strlen("curr_items")))
169 length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->curr_items);
170 else if (!memcmp("total_items", key, strlen("total_items")))
171 length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->total_items);
172 else if (!memcmp("bytes", key, strlen("bytes")))
173 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->bytes);
174 else if (!memcmp("curr_connections", key, strlen("curr_connections")))
175 length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->curr_connections);
176 else if (!memcmp("total_connections", key, strlen("total_connections")))
177 length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->total_connections);
178 else if (!memcmp("connection_structures", key, strlen("connection_structures")))
179 length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->connection_structures);
180 else if (!memcmp("cmd_get", key, strlen("cmd_get")))
181 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->cmd_get);
182 else if (!memcmp("cmd_set", key, strlen("cmd_set")))
183 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->cmd_set);
184 else if (!memcmp("get_hits", key, strlen("get_hits")))
185 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->get_hits);
186 else if (!memcmp("get_misses", key, strlen("get_misses")))
187 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->get_misses);
188 else if (!memcmp("evictions", key, strlen("evictions")))
189 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->evictions);
190 else if (!memcmp("bytes_read", key, strlen("bytes_read")))
191 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->bytes_read);
192 else if (!memcmp("bytes_written", key, strlen("bytes_written")))
193 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->bytes_written);
194 else if (!memcmp("limit_maxbytes", key, strlen("limit_maxbytes")))
195 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->limit_maxbytes);
196 else if (!memcmp("threads", key, strlen("threads")))
197 length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->threads);
198 else
199 {
200 *error= MEMCACHED_NOTFOUND;
201 return NULL;
202 }
203
204 if (ptr->call_malloc)
205 ret= ptr->call_malloc(ptr, length + 1);
206 else
207 ret= malloc(length + 1);
208 memcpy(ret, buffer, length);
209 ret[length]= '\0';
210
211 return ret;
212 }
213
214 static memcached_return memcached_stats_fetch(memcached_st *ptr,
215 memcached_stat_st *stat,
216 char *args,
217 unsigned int server_key)
218 {
219 memcached_return rc;
220 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
221 size_t send_length;
222
223 if (args)
224 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
225 "stats %s\r\n", args);
226 else
227 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
228 "stats\r\n");
229
230 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
231 return MEMCACHED_WRITE_FAILURE;
232
233 rc= memcached_do(&ptr->hosts[server_key], buffer, send_length, 1);
234 if (rc != MEMCACHED_SUCCESS)
235 goto error;
236
237 while (1)
238 {
239 rc= memcached_response(&ptr->hosts[server_key], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
240
241 if (rc == MEMCACHED_STAT)
242 {
243 char *string_ptr, *end_ptr;
244 char *key, *value;
245
246 string_ptr= buffer;
247 string_ptr+= 5; /* Move past STAT */
248 for (end_ptr= string_ptr; isgraph(*end_ptr); end_ptr++);
249 key= string_ptr;
250 key[(size_t)(end_ptr-string_ptr)]= 0;
251
252 string_ptr= end_ptr + 1;
253 for (end_ptr= string_ptr; !(isspace(*end_ptr)); end_ptr++);
254 value= string_ptr;
255 value[(size_t)(end_ptr-string_ptr)]= 0;
256 string_ptr= end_ptr + 2;
257 set_data(stat, key, value);
258 }
259 else
260 break;
261 }
262
263 error:
264 if (rc == MEMCACHED_END)
265 return MEMCACHED_SUCCESS;
266 else
267 return rc;
268 }
269
270 memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_return *error)
271 {
272 unsigned int x;
273 memcached_return rc;
274 memcached_stat_st *stats;
275
276 if (ptr->call_malloc)
277 stats= (memcached_stat_st *)ptr->call_malloc(ptr, sizeof(memcached_stat_st)*(ptr->number_of_hosts));
278 else
279 stats= (memcached_stat_st *)malloc(sizeof(memcached_stat_st)*(ptr->number_of_hosts));
280
281 if (!stats)
282 {
283 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
284 if (ptr->call_free)
285 ptr->call_free(ptr, stats);
286 else
287 free(stats);
288
289 return NULL;
290 }
291 memset(stats, 0, sizeof(memcached_stat_st)*(ptr->number_of_hosts));
292
293 rc= MEMCACHED_SUCCESS;
294 for (x= 0; x < ptr->number_of_hosts; x++)
295 {
296 memcached_return temp_return;
297
298 temp_return= memcached_stats_fetch(ptr, stats + x, args, x);
299 if (temp_return != MEMCACHED_SUCCESS)
300 rc= MEMCACHED_SOME_ERRORS;
301 }
302
303 *error= rc;
304 return stats;
305 }
306
307 memcached_return memcached_stat_servername(memcached_stat_st *stat, char *args,
308 char *hostname, unsigned int port)
309 {
310 memcached_return rc;
311 memcached_st memc;
312
313 memcached_create(&memc);
314
315 memcached_server_add(&memc, hostname, port);
316
317 rc= memcached_stats_fetch(&memc, stat, args, 0);
318
319 memcached_free(&memc);
320
321 return rc;
322 }
323
324 /*
325 We make a copy of the keys since at some point in the not so distant future
326 we will add support for "found" keys.
327 */
328 char ** memcached_stat_get_keys(memcached_st *ptr, memcached_stat_st *stat,
329 memcached_return *error)
330 {
331 char **list;
332 size_t length= sizeof(memcached_stat_keys);
333
334 if (ptr->call_malloc)
335 list= (char **)ptr->call_malloc(ptr, length);
336 else
337 list= (char **)malloc(length);
338
339 if (!list)
340 {
341 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
342 return NULL;
343 }
344 memset(list, 0, sizeof(memcached_stat_keys));
345
346 memcpy(list, memcached_stat_keys, sizeof(memcached_stat_keys));
347
348 *error= MEMCACHED_SUCCESS;
349
350 return list;
351 }
352
353 void memcached_stat_free(memcached_st *ptr, memcached_stat_st *stat)
354 {
355 if (stat == NULL)
356 {
357 WATCHPOINT_ASSERT(0); /* Be polite, but when debugging catch this as an error */
358 return;
359 }
360
361 if (ptr && ptr->call_free)
362 ptr->call_free(ptr, stat);
363 else
364 free(stat);
365 }