6 static void set_data(memcached_stat_st
*stat
, char *key
, char *value
)
8 if (!memcmp("pid", key
, strlen("pid")))
10 stat
->pid
= strtol(value
, (char **)NULL
, 10);
12 else if (!memcmp("uptime", key
, strlen("uptime")))
14 stat
->uptime
= strtol(value
, (char **)NULL
, 10);
16 else if (!memcmp("time", key
, strlen("time")))
18 stat
->time
= strtoll(value
, (char **)NULL
, 10);
20 else if (!memcmp("version", key
, strlen("version")))
22 memcpy(stat
->version
, value
, 8);
24 else if (!memcmp("pointer_size", key
, strlen("pointer_size")))
26 stat
->pointer_size
= strtoll(value
, (char **)NULL
, 10);
28 else if (!memcmp("rusage_user", key
, strlen("rusage_user")))
30 stat
->rusage_user
= strtoll(value
, (char **)NULL
, 10);
32 else if (!memcmp("rusage_system", key
, strlen("rusage_system")))
34 stat
->rusage_system
= strtoll(value
, (char **)NULL
, 10);
36 else if (!memcmp("rusage_user_seconds", key
, strlen("rusage_user_seconds")))
38 stat
->rusage_user_seconds
= strtoll(value
, (char **)NULL
, 10);
40 else if (!memcmp("rusage_user_microseconds", key
, strlen("rusage_user_microseconds")))
42 stat
->rusage_user_microseconds
= strtoll(value
, (char **)NULL
, 10);
44 else if (!memcmp("rusage_system_seconds", key
, strlen("rusage_system_seconds")))
46 stat
->rusage_system_seconds
= strtoll(value
, (char **)NULL
, 10);
48 else if (!memcmp("rusage_system_microseconds", key
, strlen("rusage_system_microseconds")))
50 stat
->rusage_system_microseconds
= strtoll(value
, (char **)NULL
, 10);
52 else if (!memcmp("curr_items", key
, strlen("curr_items")))
54 stat
->curr_items
= strtoll(value
, (char **)NULL
, 10);
56 else if (!memcmp("total_items", key
, strlen("total_items")))
58 stat
->total_items
= strtoll(value
, (char **)NULL
, 10);
60 else if (!memcmp("bytes", key
, strlen("bytes")))
62 stat
->bytes
= strtoll(value
, (char **)NULL
, 10);
64 else if (!memcmp("curr_connections", key
, strlen("curr_connections")))
66 stat
->curr_connections
= strtoll(value
, (char **)NULL
, 10);
68 else if (!memcmp("total_connections", key
, strlen("total_connections")))
70 stat
->total_connections
= strtoll(value
, (char **)NULL
, 10);
72 else if (!memcmp("connection_structures", key
, strlen("connection_structures")))
74 stat
->connection_structures
= strtoll(value
, (char **)NULL
, 10);
76 else if (!memcmp("cmd_get", key
, strlen("cmd_get")))
78 stat
->cmd_get
= strtoll(value
, (char **)NULL
, 10);
80 else if (!memcmp("cmd_set", key
, strlen("cmd_set")))
82 stat
->cmd_set
= strtoll(value
, (char **)NULL
, 10);
84 else if (!memcmp("get_hits", key
, strlen("get_hits")))
86 stat
->get_hits
= strtoll(value
, (char **)NULL
, 10);
88 else if (!memcmp("get_misses", key
, strlen("get_misses")))
90 stat
->get_misses
= strtoll(value
, (char **)NULL
, 10);
92 else if (!memcmp("evictions", key
, strlen("evictions")))
94 stat
->evictions
= strtoll(value
, (char **)NULL
, 10);
96 else if (!memcmp("bytes_read", key
, strlen("bytes_read")))
98 stat
->bytes_read
= strtoll(value
, (char **)NULL
, 10);
100 else if (!memcmp("bytes_written", key
, strlen("bytes_written")))
102 stat
->bytes_written
= strtoll(value
, (char **)NULL
, 10);
104 else if (!memcmp("limit_maxbytes", key
, strlen("limit_maxbytes")))
106 stat
->limit_maxbytes
= strtoll(value
, (char **)NULL
, 10);
108 else if (!memcmp("threads", key
, strlen("threads")))
110 stat
->threads
= strtol(key
, (char **)NULL
, 10);
114 fprintf(stderr
, "Unknown key %s\n", key
);
118 static memcached_return
memcached_stats_fetch(memcached_st
*ptr
,
119 memcached_stat_st
*stat
,
121 unsigned int server_key
)
124 char buffer
[HUGE_STRING_LEN
];
127 rc
= memcached_connect(ptr
);
129 if (rc
!= MEMCACHED_SUCCESS
)
133 send_length
= snprintf(buffer
, HUGE_STRING_LEN
,
134 "stats %s\r\n", args
);
136 send_length
= snprintf(buffer
, HUGE_STRING_LEN
,
139 if ((send(ptr
->hosts
[server_key
].fd
, buffer
, send_length
, 0) == -1))
141 fprintf(stderr
, "failed on stats\n");
143 return MEMCACHED_WRITE_FAILURE
;
146 rc
= memcached_response(ptr
, buffer
, HUGE_STRING_LEN
, 0);
148 if (rc
== MEMCACHED_SUCCESS
)
150 char *string_ptr
, *end_ptr
;
156 if (memcmp(string_ptr
, "STAT ", 5))
159 for (end_ptr
= string_ptr
; *end_ptr
!= ' '; end_ptr
++);
161 key
[(size_t)(end_ptr
-string_ptr
)]= 0;
163 string_ptr
= end_ptr
+ 1;
164 for (end_ptr
= string_ptr
; *end_ptr
!= '\r'; end_ptr
++);
166 value
[(size_t)(end_ptr
-string_ptr
)]= 0;
167 string_ptr
= end_ptr
+ 2;
168 set_data(stat
, key
, value
);
175 memcached_stat_st
*memcached_stat(memcached_st
*ptr
, char *args
, memcached_return
*error
)
179 memcached_stat_st
*stats
;
180 rc
= memcached_connect(ptr
);
182 if (rc
!= MEMCACHED_SUCCESS
)
188 stats
= (memcached_stat_st
*)malloc(sizeof(memcached_st
)*(ptr
->number_of_hosts
+1));
191 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
194 memset(stats
, 0, sizeof(memcached_st
)*(ptr
->number_of_hosts
+1));
196 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
198 rc
= memcached_stats_fetch(ptr
, stats
+x
, args
, x
);
199 if (rc
!= MEMCACHED_SUCCESS
)
200 rc
= MEMCACHED_SOME_ERRORS
;
203 *error
= x
== 0 ? MEMCACHED_SUCCESS
: rc
;
207 memcached_return
memcached_stat_hostname(memcached_stat_st
*stat
, char *args
,
208 char *hostname
, unsigned int port
)
212 char buffer
[HUGE_STRING_LEN
];
215 memcached_init(&memc
);
217 memcached_server_add(&memc
, hostname
, port
);
219 rc
= memcached_connect(&memc
);
221 if (rc
!= MEMCACHED_SUCCESS
)
224 rc
= memcached_stats_fetch(&memc
, stat
, args
, 0);
226 memcached_deinit(&memc
);