6 static char *memcached_stat_keys
[] = {
14 "rusage_user_seconds",
15 "rusage_user_microseconds",
16 "rusage_system_seconds",
17 "rusage_system_microseconds",
23 "connection_structures",
37 static void set_data(memcached_stat_st
*stat
, char *key
, char *value
)
39 if (!memcmp("pid", key
, strlen("pid")))
41 stat
->pid
= strtol(value
, (char **)NULL
, 10);
43 else if (!memcmp("uptime", key
, strlen("uptime")))
45 stat
->uptime
= strtol(value
, (char **)NULL
, 10);
47 else if (!memcmp("time", key
, strlen("time")))
49 stat
->time
= strtoll(value
, (char **)NULL
, 10);
51 else if (!memcmp("version", key
, strlen("version")))
53 memcpy(stat
->version
, value
, 8);
55 else if (!memcmp("pointer_size", key
, strlen("pointer_size")))
57 stat
->pointer_size
= strtoll(value
, (char **)NULL
, 10);
59 else if (!memcmp("rusage_user", key
, strlen("rusage_user")))
61 stat
->rusage_user
= strtoll(value
, (char **)NULL
, 10);
63 else if (!memcmp("rusage_system", key
, strlen("rusage_system")))
65 stat
->rusage_system
= strtoll(value
, (char **)NULL
, 10);
67 else if (!memcmp("rusage_user_seconds", key
, strlen("rusage_user_seconds")))
69 stat
->rusage_user_seconds
= strtoll(value
, (char **)NULL
, 10);
71 else if (!memcmp("rusage_user_microseconds", key
, strlen("rusage_user_microseconds")))
73 stat
->rusage_user_microseconds
= strtoll(value
, (char **)NULL
, 10);
75 else if (!memcmp("rusage_system_seconds", key
, strlen("rusage_system_seconds")))
77 stat
->rusage_system_seconds
= strtoll(value
, (char **)NULL
, 10);
79 else if (!memcmp("rusage_system_microseconds", key
, strlen("rusage_system_microseconds")))
81 stat
->rusage_system_microseconds
= strtoll(value
, (char **)NULL
, 10);
83 else if (!memcmp("curr_items", key
, strlen("curr_items")))
85 stat
->curr_items
= strtoll(value
, (char **)NULL
, 10);
87 else if (!memcmp("total_items", key
, strlen("total_items")))
89 stat
->total_items
= strtoll(value
, (char **)NULL
, 10);
91 else if (!memcmp("bytes", key
, strlen("bytes")))
93 stat
->bytes
= strtoll(value
, (char **)NULL
, 10);
95 else if (!memcmp("curr_connections", key
, strlen("curr_connections")))
97 stat
->curr_connections
= strtoll(value
, (char **)NULL
, 10);
99 else if (!memcmp("total_connections", key
, strlen("total_connections")))
101 stat
->total_connections
= strtoll(value
, (char **)NULL
, 10);
103 else if (!memcmp("connection_structures", key
, strlen("connection_structures")))
105 stat
->connection_structures
= strtoll(value
, (char **)NULL
, 10);
107 else if (!memcmp("cmd_get", key
, strlen("cmd_get")))
109 stat
->cmd_get
= strtoll(value
, (char **)NULL
, 10);
111 else if (!memcmp("cmd_set", key
, strlen("cmd_set")))
113 stat
->cmd_set
= strtoll(value
, (char **)NULL
, 10);
115 else if (!memcmp("get_hits", key
, strlen("get_hits")))
117 stat
->get_hits
= strtoll(value
, (char **)NULL
, 10);
119 else if (!memcmp("get_misses", key
, strlen("get_misses")))
121 stat
->get_misses
= strtoll(value
, (char **)NULL
, 10);
123 else if (!memcmp("evictions", key
, strlen("evictions")))
125 stat
->evictions
= strtoll(value
, (char **)NULL
, 10);
127 else if (!memcmp("bytes_read", key
, strlen("bytes_read")))
129 stat
->bytes_read
= strtoll(value
, (char **)NULL
, 10);
131 else if (!memcmp("bytes_written", key
, strlen("bytes_written")))
133 stat
->bytes_written
= strtoll(value
, (char **)NULL
, 10);
135 else if (!memcmp("limit_maxbytes", key
, strlen("limit_maxbytes")))
137 stat
->limit_maxbytes
= strtoll(value
, (char **)NULL
, 10);
139 else if (!memcmp("threads", key
, strlen("threads")))
141 stat
->threads
= strtol(key
, (char **)NULL
, 10);
145 fprintf(stderr
, "Unknown key %s\n", key
);
149 memcached_return
memcached_stat_get_value(memcached_stat_st
*stat
, char *key
,
150 char *value
, size_t value_length
)
154 rc
= MEMCACHED_SUCCESS
;
156 if (!memcmp("pid", key
, strlen("pid")))
158 snprintf(value
, value_length
,"%u", stat
->pid
);
160 else if (!memcmp("uptime", key
, strlen("uptime")))
162 snprintf(value
, value_length
,"%u", stat
->uptime
);
164 else if (!memcmp("time", key
, strlen("time")))
166 snprintf(value
, value_length
,"%llu", (unsigned long long)stat
->time
);
168 else if (!memcmp("version", key
, strlen("version")))
170 snprintf(value
, value_length
,"%s", stat
->version
);
172 else if (!memcmp("pointer_size", key
, strlen("pointer_size")))
174 snprintf(value
, value_length
,"%u", stat
->pointer_size
);
176 else if (!memcmp("rusage_user", key
, strlen("rusage_user")))
178 snprintf(value
, value_length
,"%u", stat
->rusage_user
);
180 else if (!memcmp("rusage_system", key
, strlen("rusage_system")))
182 snprintf(value
, value_length
,"%u", stat
->rusage_system
);
184 else if (!memcmp("rusage_user_seconds", key
, strlen("rusage_user_seconds")))
186 snprintf(value
, value_length
,"%u", stat
->rusage_user_seconds
);
188 else if (!memcmp("rusage_user_microseconds", key
, strlen("rusage_user_microseconds")))
190 snprintf(value
, value_length
,"%u", stat
->rusage_user_microseconds
);
192 else if (!memcmp("rusage_system_seconds", key
, strlen("rusage_system_seconds")))
194 snprintf(value
, value_length
,"%u", stat
->rusage_system_seconds
);
196 else if (!memcmp("rusage_system_microseconds", key
, strlen("rusage_system_microseconds")))
198 snprintf(value
, value_length
,"%u", stat
->rusage_system_microseconds
);
200 else if (!memcmp("curr_items", key
, strlen("curr_items")))
202 snprintf(value
, value_length
,"%u", stat
->curr_items
);
204 else if (!memcmp("total_items", key
, strlen("total_items")))
206 snprintf(value
, value_length
,"%u", stat
->total_items
);
208 else if (!memcmp("bytes", key
, strlen("bytes")))
210 snprintf(value
, value_length
,"%llu", stat
->bytes
);
212 else if (!memcmp("curr_connections", key
, strlen("curr_connections")))
214 snprintf(value
, value_length
,"%u", stat
->curr_connections
);
216 else if (!memcmp("total_connections", key
, strlen("total_connections")))
218 snprintf(value
, value_length
,"%u", stat
->total_connections
);
220 else if (!memcmp("connection_structures", key
, strlen("connection_structures")))
222 snprintf(value
, value_length
,"%u", stat
->connection_structures
);
224 else if (!memcmp("cmd_get", key
, strlen("cmd_get")))
226 snprintf(value
, value_length
,"%llu", stat
->cmd_get
);
228 else if (!memcmp("cmd_set", key
, strlen("cmd_set")))
230 snprintf(value
, value_length
,"%llu", stat
->cmd_set
);
232 else if (!memcmp("get_hits", key
, strlen("get_hits")))
234 snprintf(value
, value_length
,"%llu", stat
->get_hits
);
236 else if (!memcmp("get_misses", key
, strlen("get_misses")))
238 snprintf(value
, value_length
,"%llu", stat
->get_misses
);
240 else if (!memcmp("evictions", key
, strlen("evictions")))
242 snprintf(value
, value_length
,"%llu", stat
->evictions
);
244 else if (!memcmp("bytes_read", key
, strlen("bytes_read")))
246 snprintf(value
, value_length
,"%llu", stat
->bytes_read
);
248 else if (!memcmp("bytes_written", key
, strlen("bytes_written")))
250 snprintf(value
, value_length
,"%llu", stat
->bytes_written
);
252 else if (!memcmp("limit_maxbytes", key
, strlen("limit_maxbytes")))
254 snprintf(value
, value_length
,"%u", stat
->limit_maxbytes
);
256 else if (!memcmp("threads", key
, strlen("threads")))
258 snprintf(value
, value_length
,"%u", stat
->threads
);
262 rc
= MEMCACHED_NOTFOUND
;
268 static memcached_return
memcached_stats_fetch(memcached_st
*ptr
,
269 memcached_stat_st
*stat
,
271 unsigned int server_key
)
274 char buffer
[HUGE_STRING_LEN
];
275 size_t send_length
, sent_length
;
277 rc
= memcached_connect(ptr
);
279 if (rc
!= MEMCACHED_SUCCESS
)
283 send_length
= snprintf(buffer
, HUGE_STRING_LEN
,
284 "stats %s\r\n", args
);
286 send_length
= snprintf(buffer
, HUGE_STRING_LEN
,
289 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
290 return MEMCACHED_WRITE_FAILURE
;
292 sent_length
= write(ptr
->hosts
[server_key
].fd
, buffer
, send_length
);
294 if (sent_length
== -1 || sent_length
!= send_length
)
295 return MEMCACHED_WRITE_FAILURE
;
297 rc
= memcached_response(ptr
, buffer
, HUGE_STRING_LEN
, 0);
299 if (rc
== MEMCACHED_SUCCESS
)
301 char *string_ptr
, *end_ptr
;
307 if (memcmp(string_ptr
, "STAT ", 5))
310 for (end_ptr
= string_ptr
; *end_ptr
!= ' '; end_ptr
++);
312 key
[(size_t)(end_ptr
-string_ptr
)]= 0;
314 string_ptr
= end_ptr
+ 1;
315 for (end_ptr
= string_ptr
; *end_ptr
!= '\r'; end_ptr
++);
317 value
[(size_t)(end_ptr
-string_ptr
)]= 0;
318 string_ptr
= end_ptr
+ 2;
319 set_data(stat
, key
, value
);
326 memcached_stat_st
*memcached_stat(memcached_st
*ptr
, char *args
, memcached_return
*error
)
330 memcached_stat_st
*stats
;
331 rc
= memcached_connect(ptr
);
333 if (rc
!= MEMCACHED_SUCCESS
)
339 stats
= (memcached_stat_st
*)malloc(sizeof(memcached_st
)*(ptr
->number_of_hosts
+1));
342 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
345 memset(stats
, 0, sizeof(memcached_st
)*(ptr
->number_of_hosts
+1));
347 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
349 rc
= memcached_stats_fetch(ptr
, stats
+x
, args
, x
);
350 if (rc
!= MEMCACHED_SUCCESS
)
351 rc
= MEMCACHED_SOME_ERRORS
;
354 *error
= x
== 0 ? MEMCACHED_SUCCESS
: rc
;
358 memcached_return
memcached_stat_hostname(memcached_stat_st
*stat
, char *args
,
359 char *hostname
, unsigned int port
)
364 memcached_init(&memc
);
366 memcached_server_add(&memc
, hostname
, port
);
368 rc
= memcached_connect(&memc
);
370 if (rc
!= MEMCACHED_SUCCESS
)
373 rc
= memcached_stats_fetch(&memc
, stat
, args
, 0);
375 memcached_deinit(&memc
);
381 We make a copy of the keys since at some point in the not so distant future
382 we will add support for "found" keys.
384 char ** memcached_stat_get_keys(memcached_stat_st
*stat
, memcached_return
*error
)
386 char **list
= (char **)malloc(sizeof(memcached_stat_keys
));
388 memcpy(list
, memcached_stat_keys
, sizeof(memcached_stat_keys
));