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 char *memcached_stat_get_value(memcached_st
*ptr
, memcached_stat_st
*stat
,
150 char *key
, memcached_return
*error
)
152 char buffer
[SMALL_STRING_LEN
];
153 *error
= MEMCACHED_SUCCESS
;
156 memset(buffer
, 0, SMALL_STRING_LEN
);
158 if (!memcmp("pid", key
, strlen("pid")))
159 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->pid
);
160 else if (!memcmp("uptime", key
, strlen("uptime")))
161 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->uptime
);
162 else if (!memcmp("time", key
, strlen("time")))
163 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->time
);
164 else if (!memcmp("version", key
, strlen("version")))
165 snprintf(buffer
, SMALL_STRING_LEN
,"%s", stat
->version
);
166 else if (!memcmp("pointer_size", key
, strlen("pointer_size")))
167 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->pointer_size
);
168 else if (!memcmp("rusage_user", key
, strlen("rusage_user")))
169 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->rusage_user
);
170 else if (!memcmp("rusage_system", key
, strlen("rusage_system")))
171 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->rusage_system
);
172 else if (!memcmp("rusage_user_seconds", key
, strlen("rusage_user_seconds")))
173 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->rusage_user_seconds
);
174 else if (!memcmp("rusage_user_microseconds", key
, strlen("rusage_user_microseconds")))
175 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->rusage_user_microseconds
);
176 else if (!memcmp("rusage_system_seconds", key
, strlen("rusage_system_seconds")))
177 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->rusage_system_seconds
);
178 else if (!memcmp("rusage_system_microseconds", key
, strlen("rusage_system_microseconds")))
179 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->rusage_system_microseconds
);
180 else if (!memcmp("curr_items", key
, strlen("curr_items")))
181 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->curr_items
);
182 else if (!memcmp("total_items", key
, strlen("total_items")))
183 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->total_items
);
184 else if (!memcmp("bytes", key
, strlen("bytes")))
185 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", stat
->bytes
);
186 else if (!memcmp("curr_connections", key
, strlen("curr_connections")))
187 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->curr_connections
);
188 else if (!memcmp("total_connections", key
, strlen("total_connections")))
189 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->total_connections
);
190 else if (!memcmp("connection_structures", key
, strlen("connection_structures")))
191 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->connection_structures
);
192 else if (!memcmp("cmd_get", key
, strlen("cmd_get")))
193 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", stat
->cmd_get
);
194 else if (!memcmp("cmd_set", key
, strlen("cmd_set")))
195 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", stat
->cmd_set
);
196 else if (!memcmp("get_hits", key
, strlen("get_hits")))
197 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", stat
->get_hits
);
198 else if (!memcmp("get_misses", key
, strlen("get_misses")))
199 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", stat
->get_misses
);
200 else if (!memcmp("evictions", key
, strlen("evictions")))
201 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", stat
->evictions
);
202 else if (!memcmp("bytes_read", key
, strlen("bytes_read")))
203 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", stat
->bytes_read
);
204 else if (!memcmp("bytes_written", key
, strlen("bytes_written")))
205 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", stat
->bytes_written
);
206 else if (!memcmp("limit_maxbytes", key
, strlen("limit_maxbytes")))
207 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->limit_maxbytes
);
208 else if (!memcmp("threads", key
, strlen("threads")))
209 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->threads
);
212 *error
= MEMCACHED_NOTFOUND
;
216 return strdup(buffer
);
219 static memcached_return
memcached_stats_fetch(memcached_st
*ptr
,
220 memcached_stat_st
*stat
,
222 unsigned int server_key
)
225 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
226 size_t send_length
, sent_length
;
228 rc
= memcached_connect(ptr
, server_key
);
230 if (rc
!= MEMCACHED_SUCCESS
)
234 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
235 "stats %s\r\n", args
);
237 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
240 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
241 return MEMCACHED_WRITE_FAILURE
;
243 sent_length
= memcached_io_write(ptr
, server_key
, buffer
, send_length
, 1);
245 if (sent_length
== -1 || sent_length
!= send_length
)
246 return MEMCACHED_WRITE_FAILURE
;
250 rc
= memcached_response(ptr
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, server_key
);
252 if (rc
== MEMCACHED_STAT
)
254 char *string_ptr
, *end_ptr
;
258 string_ptr
+= 5; /* Move past STAT */
259 for (end_ptr
= string_ptr
; *end_ptr
!= ' '; end_ptr
++);
261 key
[(size_t)(end_ptr
-string_ptr
)]= 0;
263 string_ptr
= end_ptr
+ 1;
264 for (end_ptr
= string_ptr
; *end_ptr
!= '\r'; end_ptr
++);
266 value
[(size_t)(end_ptr
-string_ptr
)]= 0;
267 string_ptr
= end_ptr
+ 2;
268 set_data(stat
, key
, value
);
274 if (rc
== MEMCACHED_END
)
275 return MEMCACHED_SUCCESS
;
280 memcached_stat_st
*memcached_stat(memcached_st
*ptr
, char *args
, memcached_return
*error
)
284 memcached_stat_st
*stats
;
286 stats
= (memcached_stat_st
*)malloc(sizeof(memcached_st
)*(ptr
->number_of_hosts
+1));
289 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
293 memset(stats
, 0, sizeof(memcached_st
)*(ptr
->number_of_hosts
+1));
295 rc
= MEMCACHED_SUCCESS
;
296 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
298 memcached_return temp_return
;
299 temp_return
= memcached_stats_fetch(ptr
, stats
+x
, args
, x
);
300 if (temp_return
!= MEMCACHED_SUCCESS
)
301 rc
= MEMCACHED_SOME_ERRORS
;
308 memcached_return
memcached_stat_servername(memcached_stat_st
*stat
, char *args
,
309 char *hostname
, unsigned int port
)
314 memcached_create(&memc
);
316 memcached_server_add(&memc
, hostname
, port
);
318 rc
= memcached_stats_fetch(&memc
, stat
, args
, 0);
320 memcached_free(&memc
);
326 We make a copy of the keys since at some point in the not so distant future
327 we will add support for "found" keys.
329 char ** memcached_stat_get_keys(memcached_st
*ptr
, memcached_stat_st
*stat
,
330 memcached_return
*error
)
332 char **list
= (char **)malloc(sizeof(memcached_stat_keys
));
334 memset(list
, 0, sizeof(memcached_stat_keys
));
338 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
342 memcpy(list
, memcached_stat_keys
, sizeof(memcached_stat_keys
));
344 *error
= MEMCACHED_SUCCESS
;