6 static char *memcached_stat_keys
[] = {
19 "connection_structures",
33 static void set_data(memcached_stat_st
*stat
, char *key
, char *value
)
35 if (!memcmp("pid", key
, strlen("pid")))
37 stat
->pid
= strtol(value
, (char **)NULL
, 10);
39 else if (!memcmp("uptime", key
, strlen("uptime")))
41 stat
->uptime
= strtol(value
, (char **)NULL
, 10);
43 else if (!memcmp("time", key
, strlen("time")))
45 stat
->time
= strtol(value
, (char **)NULL
, 10);
47 else if (!memcmp("version", key
, strlen("version")))
49 memcpy(stat
->version
, value
, strlen(value
));
50 stat
->version
[strlen(value
)]= 0;
52 else if (!memcmp("pointer_size", key
, strlen("pointer_size")))
54 stat
->pointer_size
= strtol(value
, (char **)NULL
, 10);
56 else if (!memcmp("rusage_user", key
, strlen("rusage_user")))
59 for (walk_ptr
= value
; (!ispunct(*walk_ptr
)); walk_ptr
++);
62 stat
->rusage_user_seconds
= strtol(value
, (char **)NULL
, 10);
63 stat
->rusage_user_microseconds
= strtol(walk_ptr
, (char **)NULL
, 10);
65 else if (!memcmp("rusage_system", key
, strlen("rusage_system")))
68 for (walk_ptr
= value
; (!ispunct(*walk_ptr
)); walk_ptr
++);
71 stat
->rusage_system_seconds
= strtol(value
, (char **)NULL
, 10);
72 stat
->rusage_system_microseconds
= strtol(walk_ptr
, (char **)NULL
, 10);
74 else if (!memcmp("curr_items", key
, strlen("curr_items")))
76 stat
->curr_items
= strtol(value
, (char **)NULL
, 10);
78 else if (!memcmp("total_items", key
, strlen("total_items")))
80 stat
->total_items
= strtol(value
, (char **)NULL
, 10);
82 else if (!memcmp("bytes", key
, strlen("bytes")))
84 //stat->bytes= strtoll(value, (char **)NULL, 10);
86 else if (!memcmp("curr_connections", key
, strlen("curr_connections")))
88 //stat->curr_connections= strtoll(value, (char **)NULL, 10);
90 else if (!memcmp("total_connections", key
, strlen("total_connections")))
92 //stat->total_connections= strtoll(value, (char **)NULL, 10);
94 else if (!memcmp("connection_structures", key
, strlen("connection_structures")))
96 stat
->connection_structures
= strtol(value
, (char **)NULL
, 10);
98 else if (!memcmp("cmd_get", key
, strlen("cmd_get")))
100 stat
->cmd_get
= strtoll(value
, (char **)NULL
, 10);
102 else if (!memcmp("cmd_set", key
, strlen("cmd_set")))
104 stat
->cmd_set
= strtoll(value
, (char **)NULL
, 10);
106 else if (!memcmp("get_hits", key
, strlen("get_hits")))
108 stat
->get_hits
= strtoll(value
, (char **)NULL
, 10);
110 else if (!memcmp("get_misses", key
, strlen("get_misses")))
112 stat
->get_misses
= (uint64_t)strtoll(value
, (char **)NULL
, 10);
114 else if (!memcmp("evictions", key
, strlen("evictions")))
116 stat
->evictions
= (uint64_t)strtoll(value
, (char **)NULL
, 10);
118 else if (!memcmp("bytes_read", key
, strlen("bytes_read")))
120 stat
->bytes_read
= strtoll(value
, (char **)NULL
, 10);
122 else if (!memcmp("bytes_written", key
, strlen("bytes_written")))
124 stat
->bytes_written
= strtoll(value
, (char **)NULL
, 10);
126 else if (!memcmp("limit_maxbytes", key
, strlen("limit_maxbytes")))
128 stat
->limit_maxbytes
= strtol(value
, (char **)NULL
, 10);
130 else if (!memcmp("threads", key
, strlen("threads")))
132 stat
->threads
= strtol(key
, (char **)NULL
, 10);
136 fprintf(stderr
, "Unknown key %s\n", key
);
140 char *memcached_stat_get_value(memcached_st
*ptr
, memcached_stat_st
*stat
,
141 char *key
, memcached_return
*error
)
143 char buffer
[SMALL_STRING_LEN
];
144 *error
= MEMCACHED_SUCCESS
;
146 if (!memcmp("pid", key
, strlen("pid")))
147 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->pid
);
148 else if (!memcmp("uptime", key
, strlen("uptime")))
149 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->uptime
);
150 else if (!memcmp("time", key
, strlen("time")))
151 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->time
);
152 else if (!memcmp("version", key
, strlen("version")))
153 snprintf(buffer
, SMALL_STRING_LEN
,"%s", stat
->version
);
154 else if (!memcmp("pointer_size", key
, strlen("pointer_size")))
155 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->pointer_size
);
156 else if (!memcmp("rusage_user", key
, strlen("rusage_user")))
157 snprintf(buffer
, SMALL_STRING_LEN
,"%u.%u", stat
->rusage_user_seconds
, stat
->rusage_user_microseconds
);
158 else if (!memcmp("rusage_system", key
, strlen("rusage_system")))
159 snprintf(buffer
, SMALL_STRING_LEN
,"%u.%u", stat
->rusage_system_seconds
, stat
->rusage_system_microseconds
);
160 else if (!memcmp("curr_items", key
, strlen("curr_items")))
161 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->curr_items
);
162 else if (!memcmp("total_items", key
, strlen("total_items")))
163 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->total_items
);
164 else if (!memcmp("bytes", key
, strlen("bytes")))
165 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->bytes
);
166 else if (!memcmp("curr_connections", key
, strlen("curr_connections")))
167 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->curr_connections
);
168 else if (!memcmp("total_connections", key
, strlen("total_connections")))
169 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->total_connections
);
170 else if (!memcmp("connection_structures", key
, strlen("connection_structures")))
171 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->connection_structures
);
172 else if (!memcmp("cmd_get", key
, strlen("cmd_get")))
173 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->cmd_get
);
174 else if (!memcmp("cmd_set", key
, strlen("cmd_set")))
175 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->cmd_set
);
176 else if (!memcmp("get_hits", key
, strlen("get_hits")))
177 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->get_hits
);
178 else if (!memcmp("get_misses", key
, strlen("get_misses")))
179 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->get_misses
);
180 else if (!memcmp("evictions", key
, strlen("evictions")))
181 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->evictions
);
182 else if (!memcmp("bytes_read", key
, strlen("bytes_read")))
183 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->bytes_read
);
184 else if (!memcmp("bytes_written", key
, strlen("bytes_written")))
185 snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->bytes_written
);
186 else if (!memcmp("limit_maxbytes", key
, strlen("limit_maxbytes")))
187 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->limit_maxbytes
);
188 else if (!memcmp("threads", key
, strlen("threads")))
189 snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->threads
);
192 *error
= MEMCACHED_NOTFOUND
;
196 return strdup(buffer
);
199 static memcached_return
memcached_stats_fetch(memcached_st
*ptr
,
200 memcached_stat_st
*stat
,
202 unsigned int server_key
)
205 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
209 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
210 "stats %s\r\n", args
);
212 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
215 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
216 return MEMCACHED_WRITE_FAILURE
;
218 rc
= memcached_do(ptr
, server_key
, buffer
, send_length
, 1);
219 if (rc
!= MEMCACHED_SUCCESS
)
224 rc
= memcached_response(ptr
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
, server_key
);
226 if (rc
== MEMCACHED_STAT
)
228 char *string_ptr
, *end_ptr
;
232 string_ptr
+= 5; /* Move past STAT */
233 for (end_ptr
= string_ptr
; isgraph(*end_ptr
); end_ptr
++);
235 key
[(size_t)(end_ptr
-string_ptr
)]= 0;
237 string_ptr
= end_ptr
+ 1;
238 for (end_ptr
= string_ptr
; !(isspace(*end_ptr
)); end_ptr
++);
240 value
[(size_t)(end_ptr
-string_ptr
)]= 0;
241 string_ptr
= end_ptr
+ 2;
242 set_data(stat
, key
, value
);
249 if (rc
== MEMCACHED_END
)
250 return MEMCACHED_SUCCESS
;
255 memcached_stat_st
*memcached_stat(memcached_st
*ptr
, char *args
, memcached_return
*error
)
259 memcached_stat_st
*stats
;
261 stats
= (memcached_stat_st
*)malloc(sizeof(memcached_stat_st
)*(ptr
->number_of_hosts
));
264 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
268 memset(stats
, 0, sizeof(memcached_stat_st
)*(ptr
->number_of_hosts
));
270 rc
= MEMCACHED_SUCCESS
;
271 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
273 memcached_return temp_return
;
275 temp_return
= memcached_stats_fetch(ptr
, stats
+ x
, args
, x
);
276 if (temp_return
!= MEMCACHED_SUCCESS
)
277 rc
= MEMCACHED_SOME_ERRORS
;
284 memcached_return
memcached_stat_servername(memcached_stat_st
*stat
, char *args
,
285 char *hostname
, unsigned int port
)
290 memcached_create(&memc
);
292 memcached_server_add(&memc
, hostname
, port
);
294 rc
= memcached_stats_fetch(&memc
, stat
, args
, 0);
296 memcached_free(&memc
);
302 We make a copy of the keys since at some point in the not so distant future
303 we will add support for "found" keys.
305 char ** memcached_stat_get_keys(memcached_st
*ptr
, memcached_stat_st
*stat
,
306 memcached_return
*error
)
309 size_t length
= sizeof(memcached_stat_keys
);
310 list
= (char **)malloc(length
);
314 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
317 memset(list
, 0, sizeof(memcached_stat_keys
));
319 memcpy(list
, memcached_stat_keys
, sizeof(memcached_stat_keys
));
321 *error
= MEMCACHED_SUCCESS
;
326 void memcached_stat_free(memcached_st
*ptr
, memcached_stat_st
*stat
)