6 static char *memcached_stat_keys
[] = {
19 "connection_structures",
33 static void set_data(memcached_stat_st
*stat
, char *key
, char *value
)
38 fprintf(stderr
, "Invalid key %s\n", key
);
40 else if (!strcmp("pid", key
))
42 stat
->pid
= strtol(value
, (char **)NULL
, 10);
44 else if (!strcmp("uptime", key
))
46 stat
->uptime
= strtol(value
, (char **)NULL
, 10);
48 else if (!strcmp("time", key
))
50 stat
->time
= strtol(value
, (char **)NULL
, 10);
52 else if (!strcmp("version", key
))
54 memcpy(stat
->version
, value
, strlen(value
));
55 stat
->version
[strlen(value
)]= 0;
57 else if (!strcmp("pointer_size", key
))
59 stat
->pointer_size
= strtol(value
, (char **)NULL
, 10);
61 else if (!strcmp("rusage_user", key
))
64 for (walk_ptr
= value
; (!ispunct(*walk_ptr
)); walk_ptr
++);
67 stat
->rusage_user_seconds
= strtol(value
, (char **)NULL
, 10);
68 stat
->rusage_user_microseconds
= strtol(walk_ptr
, (char **)NULL
, 10);
70 else if (!strcmp("rusage_system", key
))
73 for (walk_ptr
= value
; (!ispunct(*walk_ptr
)); walk_ptr
++);
76 stat
->rusage_system_seconds
= strtol(value
, (char **)NULL
, 10);
77 stat
->rusage_system_microseconds
= strtol(walk_ptr
, (char **)NULL
, 10);
79 else if (!strcmp("curr_items", key
))
81 stat
->curr_items
= strtol(value
, (char **)NULL
, 10);
83 else if (!strcmp("total_items", key
))
85 stat
->total_items
= strtol(value
, (char **)NULL
, 10);
87 else if (!strcmp("bytes_read", key
))
89 stat
->bytes_read
= strtoll(value
, (char **)NULL
, 10);
91 else if (!strcmp("bytes_written", key
))
93 stat
->bytes_written
= strtoll(value
, (char **)NULL
, 10);
95 else if (!strcmp("bytes", key
))
97 stat
->bytes
= strtoll(value
, (char **)NULL
, 10);
99 else if (!strcmp("curr_connections", key
))
101 stat
->curr_connections
= strtoll(value
, (char **)NULL
, 10);
103 else if (!strcmp("total_connections", key
))
105 stat
->total_connections
= strtoll(value
, (char **)NULL
, 10);
107 else if (!strcmp("connection_structures", key
))
109 stat
->connection_structures
= strtol(value
, (char **)NULL
, 10);
111 else if (!strcmp("cmd_get", key
))
113 stat
->cmd_get
= strtoll(value
, (char **)NULL
, 10);
115 else if (!strcmp("cmd_set", key
))
117 stat
->cmd_set
= strtoll(value
, (char **)NULL
, 10);
119 else if (!strcmp("get_hits", key
))
121 stat
->get_hits
= strtoll(value
, (char **)NULL
, 10);
123 else if (!strcmp("get_misses", key
))
125 stat
->get_misses
= (uint64_t)strtoll(value
, (char **)NULL
, 10);
127 else if (!strcmp("evictions", key
))
129 stat
->evictions
= (uint64_t)strtoll(value
, (char **)NULL
, 10);
131 else if (!strcmp("limit_maxbytes", key
))
133 stat
->limit_maxbytes
= strtoll(value
, (char **)NULL
, 10);
135 else if (!strcmp("threads", key
))
137 stat
->threads
= strtol(value
, (char **)NULL
, 10);
139 else if (!(strcmp("delete_misses", key
) == 0 ||/* New stats in the 1.3 beta */
140 strcmp("delete_hits", key
) == 0 ||/* Just swallow them for now.. */
141 strcmp("incr_misses", key
) == 0 ||
142 strcmp("incr_hits", key
) == 0 ||
143 strcmp("decr_misses", key
) == 0 ||
144 strcmp("decr_hits", key
) == 0 ||
145 strcmp("cas_misses", key
) == 0 ||
146 strcmp("cas_hits", key
) == 0 ||
147 strcmp("cas_badval", key
) == 0 ||
148 strcmp("cmd_flush", key
) == 0 ||
149 strcmp("accepting_conns", key
) == 0 ||
150 strcmp("listen_disabled_num", key
) == 0))
152 fprintf(stderr
, "Unknown key %s\n", key
);
156 char *memcached_stat_get_value(memcached_st
*ptr
, memcached_stat_st
*stat
,
157 const char *key
, memcached_return
*error
)
159 char buffer
[SMALL_STRING_LEN
];
163 *error
= MEMCACHED_SUCCESS
;
165 if (!memcmp("pid", key
, strlen("pid")))
166 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->pid
);
167 else if (!memcmp("uptime", key
, strlen("uptime")))
168 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->uptime
);
169 else if (!memcmp("time", key
, strlen("time")))
170 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->time
);
171 else if (!memcmp("version", key
, strlen("version")))
172 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%s", stat
->version
);
173 else if (!memcmp("pointer_size", key
, strlen("pointer_size")))
174 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->pointer_size
);
175 else if (!memcmp("rusage_user", key
, strlen("rusage_user")))
176 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u.%u", stat
->rusage_user_seconds
, stat
->rusage_user_microseconds
);
177 else if (!memcmp("rusage_system", key
, strlen("rusage_system")))
178 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u.%u", stat
->rusage_system_seconds
, stat
->rusage_system_microseconds
);
179 else if (!memcmp("curr_items", key
, strlen("curr_items")))
180 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->curr_items
);
181 else if (!memcmp("total_items", key
, strlen("total_items")))
182 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->total_items
);
183 else if (!memcmp("bytes", key
, strlen("bytes")))
184 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->bytes
);
185 else if (!memcmp("curr_connections", key
, strlen("curr_connections")))
186 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->curr_connections
);
187 else if (!memcmp("total_connections", key
, strlen("total_connections")))
188 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->total_connections
);
189 else if (!memcmp("connection_structures", key
, strlen("connection_structures")))
190 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->connection_structures
);
191 else if (!memcmp("cmd_get", key
, strlen("cmd_get")))
192 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->cmd_get
);
193 else if (!memcmp("cmd_set", key
, strlen("cmd_set")))
194 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->cmd_set
);
195 else if (!memcmp("get_hits", key
, strlen("get_hits")))
196 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->get_hits
);
197 else if (!memcmp("get_misses", key
, strlen("get_misses")))
198 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->get_misses
);
199 else if (!memcmp("evictions", key
, strlen("evictions")))
200 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->evictions
);
201 else if (!memcmp("bytes_read", key
, strlen("bytes_read")))
202 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->bytes_read
);
203 else if (!memcmp("bytes_written", key
, strlen("bytes_written")))
204 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->bytes_written
);
205 else if (!memcmp("limit_maxbytes", key
, strlen("limit_maxbytes")))
206 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->limit_maxbytes
);
207 else if (!memcmp("threads", key
, strlen("threads")))
208 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->threads
);
211 *error
= MEMCACHED_NOTFOUND
;
215 if (ptr
->call_malloc
)
216 ret
= ptr
->call_malloc(ptr
, length
+ 1);
218 ret
= malloc(length
+ 1);
219 memcpy(ret
, buffer
, length
);
225 static memcached_return
binary_stats_fetch(memcached_st
*ptr
,
226 memcached_stat_st
*stat
,
228 unsigned int server_key
)
232 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
233 protocol_binary_request_stats request
= {.bytes
= {0}};
234 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
235 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_STAT
;
236 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
240 int len
= strlen(args
);
242 rc
= memcached_validate_key_length(len
, true);
243 unlikely (rc
!= MEMCACHED_SUCCESS
)
246 request
.message
.header
.request
.keylen
= htons((uint16_t)len
);
247 request
.message
.header
.request
.bodylen
= htonl(len
);
249 if ((memcached_do(&ptr
->hosts
[server_key
], request
.bytes
,
250 sizeof(request
.bytes
), 0) != MEMCACHED_SUCCESS
) ||
251 (memcached_io_write(&ptr
->hosts
[server_key
], args
, len
, 1) == -1))
253 memcached_io_reset(&ptr
->hosts
[server_key
]);
254 return MEMCACHED_WRITE_FAILURE
;
259 if (memcached_do(&ptr
->hosts
[server_key
], request
.bytes
,
260 sizeof(request
.bytes
), 1) != MEMCACHED_SUCCESS
)
262 memcached_io_reset(&ptr
->hosts
[server_key
]);
263 return MEMCACHED_WRITE_FAILURE
;
267 memcached_server_response_decrement(&ptr
->hosts
[server_key
]);
270 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
,
271 sizeof(buffer
), NULL
);
272 if (rc
== MEMCACHED_END
)
275 unlikely (rc
!= MEMCACHED_SUCCESS
)
277 memcached_io_reset(&ptr
->hosts
[server_key
]);
281 set_data(stat
, buffer
, buffer
+ strlen(buffer
) + 1);
284 /* shit... memcached_response will decrement the counter, so I need to
285 ** reset it.. todo: look at this and try to find a better solution.
287 ptr
->hosts
[server_key
].cursor_active
= 0;
289 return MEMCACHED_SUCCESS
;
292 static memcached_return
ascii_stats_fetch(memcached_st
*ptr
,
293 memcached_stat_st
*stat
,
295 unsigned int server_key
)
298 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
302 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
303 "stats %s\r\n", args
);
305 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
308 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
309 return MEMCACHED_WRITE_FAILURE
;
311 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, send_length
, 1);
312 if (rc
!= MEMCACHED_SUCCESS
)
317 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
319 if (rc
== MEMCACHED_STAT
)
321 char *string_ptr
, *end_ptr
;
325 string_ptr
+= 5; /* Move past STAT */
326 for (end_ptr
= string_ptr
; isgraph(*end_ptr
); end_ptr
++);
328 key
[(size_t)(end_ptr
-string_ptr
)]= 0;
330 string_ptr
= end_ptr
+ 1;
331 for (end_ptr
= string_ptr
; !(isspace(*end_ptr
)); end_ptr
++);
333 value
[(size_t)(end_ptr
-string_ptr
)]= 0;
334 string_ptr
= end_ptr
+ 2;
335 set_data(stat
, key
, value
);
342 if (rc
== MEMCACHED_END
)
343 return MEMCACHED_SUCCESS
;
348 memcached_stat_st
*memcached_stat(memcached_st
*ptr
, char *args
, memcached_return
*error
)
352 memcached_stat_st
*stats
;
354 if (ptr
->flags
& MEM_USE_UDP
)
356 *error
= MEMCACHED_NOT_SUPPORTED
;
360 if (ptr
->call_malloc
)
361 stats
= (memcached_stat_st
*)ptr
->call_malloc(ptr
, sizeof(memcached_stat_st
)*(ptr
->number_of_hosts
));
363 stats
= (memcached_stat_st
*)malloc(sizeof(memcached_stat_st
)*(ptr
->number_of_hosts
));
367 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
370 memset(stats
, 0, sizeof(memcached_stat_st
)*(ptr
->number_of_hosts
));
372 rc
= MEMCACHED_SUCCESS
;
373 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
375 memcached_return temp_return
;
377 if (ptr
->flags
& MEM_BINARY_PROTOCOL
)
378 temp_return
= binary_stats_fetch(ptr
, stats
+ x
, args
, x
);
380 temp_return
= ascii_stats_fetch(ptr
, stats
+ x
, args
, x
);
382 if (temp_return
!= MEMCACHED_SUCCESS
)
383 rc
= MEMCACHED_SOME_ERRORS
;
390 memcached_return
memcached_stat_servername(memcached_stat_st
*stat
, char *args
,
391 char *hostname
, unsigned int port
)
396 memcached_create(&memc
);
398 memcached_server_add(&memc
, hostname
, port
);
400 if (memc
.flags
& MEM_BINARY_PROTOCOL
)
401 rc
= binary_stats_fetch(&memc
, stat
, args
, 0);
403 rc
= ascii_stats_fetch(&memc
, stat
, args
, 0);
405 memcached_free(&memc
);
411 We make a copy of the keys since at some point in the not so distant future
412 we will add support for "found" keys.
414 char ** memcached_stat_get_keys(memcached_st
*ptr
, memcached_stat_st
*stat
__attribute__((unused
)),
415 memcached_return
*error
)
418 size_t length
= sizeof(memcached_stat_keys
);
420 if (ptr
->call_malloc
)
421 list
= (char **)ptr
->call_malloc(ptr
, length
);
423 list
= (char **)malloc(length
);
427 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
430 memset(list
, 0, sizeof(memcached_stat_keys
));
432 memcpy(list
, memcached_stat_keys
, sizeof(memcached_stat_keys
));
434 *error
= MEMCACHED_SUCCESS
;
439 void memcached_stat_free(memcached_st
*ptr
, memcached_stat_st
*stat
)
443 WATCHPOINT_ASSERT(0); /* Be polite, but when debugging catch this as an error */
447 if (ptr
&& ptr
->call_free
)
448 ptr
->call_free(ptr
, stat
);