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
;
155 if (!memcmp("pid", key
, strlen("pid")))
156 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->pid
);
157 else if (!memcmp("uptime", key
, strlen("uptime")))
158 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->uptime
);
159 else if (!memcmp("time", key
, strlen("time")))
160 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->time
);
161 else if (!memcmp("version", key
, strlen("version")))
162 snprintf(buffer
, SMALL_STRING_LEN
,"%s", stat
->version
);
163 else if (!memcmp("pointer_size", key
, strlen("pointer_size")))
164 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->pointer_size
);
165 else if (!memcmp("rusage_user", key
, strlen("rusage_user")))
166 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->rusage_user
);
167 else if (!memcmp("rusage_system", key
, strlen("rusage_system")))
168 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->rusage_system
);
169 else if (!memcmp("rusage_user_seconds", key
, strlen("rusage_user_seconds")))
170 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->rusage_user_seconds
);
171 else if (!memcmp("rusage_user_microseconds", key
, strlen("rusage_user_microseconds")))
172 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->rusage_user_microseconds
);
173 else if (!memcmp("rusage_system_seconds", key
, strlen("rusage_system_seconds")))
174 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->rusage_system_seconds
);
175 else if (!memcmp("rusage_system_microseconds", key
, strlen("rusage_system_microseconds")))
176 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->rusage_system_microseconds
);
177 else if (!memcmp("curr_items", key
, strlen("curr_items")))
178 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->curr_items
);
179 else if (!memcmp("total_items", key
, strlen("total_items")))
180 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->total_items
);
181 else if (!memcmp("bytes", key
, strlen("bytes")))
182 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", stat
->bytes
);
183 else if (!memcmp("curr_connections", key
, strlen("curr_connections")))
184 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->curr_connections
);
185 else if (!memcmp("total_connections", key
, strlen("total_connections")))
186 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->total_connections
);
187 else if (!memcmp("connection_structures", key
, strlen("connection_structures")))
188 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->connection_structures
);
189 else if (!memcmp("cmd_get", key
, strlen("cmd_get")))
190 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", stat
->cmd_get
);
191 else if (!memcmp("cmd_set", key
, strlen("cmd_set")))
192 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", stat
->cmd_set
);
193 else if (!memcmp("get_hits", key
, strlen("get_hits")))
194 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", stat
->get_hits
);
195 else if (!memcmp("get_misses", key
, strlen("get_misses")))
196 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", stat
->get_misses
);
197 else if (!memcmp("evictions", key
, strlen("evictions")))
198 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", stat
->evictions
);
199 else if (!memcmp("bytes_read", key
, strlen("bytes_read")))
200 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", stat
->bytes_read
);
201 else if (!memcmp("bytes_written", key
, strlen("bytes_written")))
202 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", stat
->bytes_written
);
203 else if (!memcmp("limit_maxbytes", key
, strlen("limit_maxbytes")))
204 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->limit_maxbytes
);
205 else if (!memcmp("threads", key
, strlen("threads")))
206 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->threads
);
209 *error
= MEMCACHED_NOTFOUND
;
213 return strdup(buffer
);
216 static memcached_return
memcached_stats_fetch(memcached_st
*ptr
,
217 memcached_stat_st
*stat
,
219 unsigned int server_key
)
222 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
225 rc
= memcached_connect(ptr
, server_key
);
227 if (rc
!= MEMCACHED_SUCCESS
)
231 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
232 "stats %s\r\n", args
);
234 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
237 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
238 return MEMCACHED_WRITE_FAILURE
;
240 rc
= memcached_do(ptr
, server_key
, buffer
, send_length
, 1);
241 if (rc
!= MEMCACHED_SUCCESS
)
246 rc
= memcached_response(ptr
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, server_key
);
248 if (rc
== MEMCACHED_STAT
)
250 char *string_ptr
, *end_ptr
;
254 string_ptr
+= 5; /* Move past STAT */
255 for (end_ptr
= string_ptr
; *end_ptr
!= ' '; end_ptr
++);
257 key
[(size_t)(end_ptr
-string_ptr
)]= 0;
259 string_ptr
= end_ptr
+ 1;
260 for (end_ptr
= string_ptr
; *end_ptr
!= '\r'; end_ptr
++);
262 value
[(size_t)(end_ptr
-string_ptr
)]= 0;
263 string_ptr
= end_ptr
+ 2;
264 set_data(stat
, key
, value
);
271 if (rc
== MEMCACHED_END
)
272 return MEMCACHED_SUCCESS
;
277 memcached_stat_st
*memcached_stat(memcached_st
*ptr
, char *args
, memcached_return
*error
)
281 memcached_stat_st
*stats
;
283 stats
= (memcached_stat_st
*)malloc(sizeof(memcached_st
)*(ptr
->number_of_hosts
+1));
286 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
290 memset(stats
, 0, sizeof(memcached_st
)*(ptr
->number_of_hosts
+1));
292 rc
= MEMCACHED_SUCCESS
;
293 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
295 memcached_return temp_return
;
296 temp_return
= memcached_stats_fetch(ptr
, stats
+x
, args
, x
);
297 if (temp_return
!= MEMCACHED_SUCCESS
)
298 rc
= MEMCACHED_SOME_ERRORS
;
305 memcached_return
memcached_stat_servername(memcached_stat_st
*stat
, char *args
,
306 char *hostname
, unsigned int port
)
311 memcached_create(&memc
);
313 memcached_server_add(&memc
, hostname
, port
);
315 rc
= memcached_stats_fetch(&memc
, stat
, args
, 0);
317 memcached_free(&memc
);
323 We make a copy of the keys since at some point in the not so distant future
324 we will add support for "found" keys.
326 char ** memcached_stat_get_keys(memcached_st
*ptr
, memcached_stat_st
*stat
,
327 memcached_return
*error
)
329 char **list
= (char **)malloc(sizeof(memcached_stat_keys
));
331 memset(list
, 0, sizeof(memcached_stat_keys
));
335 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
339 memcpy(list
, memcached_stat_keys
, sizeof(memcached_stat_keys
));
341 *error
= MEMCACHED_SUCCESS
;
346 void memcached_stat_free(memcached_st
*ptr
, memcached_stat_st
*stat
)