emit messages to stderr when write fails
[awesomized/libmemcached] / lib / memcached_stats.c
1 /*
2 */
3
4 #include <memcached.h>
5
6 static void set_data(memcached_stat_st *stat, char *key, char *value)
7 {
8 if (!memcmp("pid", key, strlen("pid")))
9 {
10 stat->pid= strtol(value, (char **)NULL, 10);
11 }
12 else if (!memcmp("uptime", key, strlen("uptime")))
13 {
14 stat->uptime= strtol(value, (char **)NULL, 10);
15 }
16 else if (!memcmp("time", key, strlen("time")))
17 {
18 stat->time= strtoll(value, (char **)NULL, 10);
19 }
20 else if (!memcmp("version", key, strlen("version")))
21 {
22 memcpy(stat->version, value, 8);
23 }
24 else if (!memcmp("pointer_size", key, strlen("pointer_size")))
25 {
26 stat->pointer_size= strtoll(value, (char **)NULL, 10);
27 }
28 else if (!memcmp("rusage_user", key, strlen("rusage_user")))
29 {
30 stat->rusage_user= strtoll(value, (char **)NULL, 10);
31 }
32 else if (!memcmp("rusage_system", key, strlen("rusage_system")))
33 {
34 stat->rusage_system= strtoll(value, (char **)NULL, 10);
35 }
36 else if (!memcmp("rusage_user_seconds", key, strlen("rusage_user_seconds")))
37 {
38 stat->rusage_user_seconds= strtoll(value, (char **)NULL, 10);
39 }
40 else if (!memcmp("rusage_user_microseconds", key, strlen("rusage_user_microseconds")))
41 {
42 stat->rusage_user_microseconds= strtoll(value, (char **)NULL, 10);
43 }
44 else if (!memcmp("rusage_system_seconds", key, strlen("rusage_system_seconds")))
45 {
46 stat->rusage_system_seconds= strtoll(value, (char **)NULL, 10);
47 }
48 else if (!memcmp("rusage_system_microseconds", key, strlen("rusage_system_microseconds")))
49 {
50 stat->rusage_system_microseconds= strtoll(value, (char **)NULL, 10);
51 }
52 else if (!memcmp("curr_items", key, strlen("curr_items")))
53 {
54 stat->curr_items= strtoll(value, (char **)NULL, 10);
55 }
56 else if (!memcmp("total_items", key, strlen("total_items")))
57 {
58 stat->total_items= strtoll(value, (char **)NULL, 10);
59 }
60 else if (!memcmp("bytes", key, strlen("bytes")))
61 {
62 stat->bytes= strtoll(value, (char **)NULL, 10);
63 }
64 else if (!memcmp("curr_connections", key, strlen("curr_connections")))
65 {
66 stat->curr_connections= strtoll(value, (char **)NULL, 10);
67 }
68 else if (!memcmp("total_connections", key, strlen("total_connections")))
69 {
70 stat->total_connections= strtoll(value, (char **)NULL, 10);
71 }
72 else if (!memcmp("connection_structures", key, strlen("connection_structures")))
73 {
74 stat->connection_structures= strtoll(value, (char **)NULL, 10);
75 }
76 else if (!memcmp("cmd_get", key, strlen("cmd_get")))
77 {
78 stat->cmd_get= strtoll(value, (char **)NULL, 10);
79 }
80 else if (!memcmp("cmd_set", key, strlen("cmd_set")))
81 {
82 stat->cmd_set= strtoll(value, (char **)NULL, 10);
83 }
84 else if (!memcmp("get_hits", key, strlen("get_hits")))
85 {
86 stat->get_hits= strtoll(value, (char **)NULL, 10);
87 }
88 else if (!memcmp("get_misses", key, strlen("get_misses")))
89 {
90 stat->get_misses= strtoll(value, (char **)NULL, 10);
91 }
92 else if (!memcmp("evictions", key, strlen("evictions")))
93 {
94 stat->evictions= strtoll(value, (char **)NULL, 10);
95 }
96 else if (!memcmp("bytes_read", key, strlen("bytes_read")))
97 {
98 stat->bytes_read= strtoll(value, (char **)NULL, 10);
99 }
100 else if (!memcmp("bytes_written", key, strlen("bytes_written")))
101 {
102 stat->bytes_written= strtoll(value, (char **)NULL, 10);
103 }
104 else if (!memcmp("limit_maxbytes", key, strlen("limit_maxbytes")))
105 {
106 stat->limit_maxbytes= strtoll(value, (char **)NULL, 10);
107 }
108 else if (!memcmp("threads", key, strlen("threads")))
109 {
110 stat->threads= strtol(key, (char **)NULL, 10);
111 }
112 else
113 {
114 fprintf(stderr, "Unknown key %s\n", key);
115 }
116 }
117
118 static memcached_return memcached_stats_fetch(memcached_st *ptr,
119 memcached_stat_st *stat,
120 char *args,
121 unsigned int server_key)
122 {
123 memcached_return rc;
124 char buffer[HUGE_STRING_LEN];
125 size_t send_length, sent_length;
126
127 rc= memcached_connect(ptr);
128
129 if (rc != MEMCACHED_SUCCESS)
130 return rc;
131
132 if (args)
133 send_length= snprintf(buffer, HUGE_STRING_LEN,
134 "stats %s\r\n", args);
135 else
136 send_length= snprintf(buffer, HUGE_STRING_LEN,
137 "stats\r\n");
138
139 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
140 return MEMCACHED_WRITE_FAILURE;
141
142 sent_length= write(ptr->hosts[server_key].fd, buffer, send_length);
143
144 if (sent_length == -1)
145 {
146 fprintf(stderr, "error %s: write: %m\n", __FUNCTION__);
147 return MEMCACHED_WRITE_FAILURE;
148 }
149
150 if (sent_length != send_length)
151 {
152 fprintf(stderr, "error %s: short write %d %d: %m\n",
153 __FUNCTION__, sent_length, send_length);
154 return MEMCACHED_WRITE_FAILURE;
155 }
156
157 rc= memcached_response(ptr, buffer, HUGE_STRING_LEN, 0);
158
159 if (rc == MEMCACHED_SUCCESS)
160 {
161 char *string_ptr, *end_ptr;
162 char *key, *value;
163
164 string_ptr= buffer;
165 while (1)
166 {
167 if (memcmp(string_ptr, "STAT ", 5))
168 break;
169 string_ptr+= 5;
170 for (end_ptr= string_ptr; *end_ptr != ' '; end_ptr++);
171 key= string_ptr;
172 key[(size_t)(end_ptr-string_ptr)]= 0;
173
174 string_ptr= end_ptr + 1;
175 for (end_ptr= string_ptr; *end_ptr != '\r'; end_ptr++);
176 value= string_ptr;
177 value[(size_t)(end_ptr-string_ptr)]= 0;
178 string_ptr= end_ptr + 2;
179 set_data(stat, key, value);
180 }
181 }
182
183 return rc;
184 }
185
186 memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_return *error)
187 {
188 unsigned int x;
189 memcached_return rc;
190 memcached_stat_st *stats;
191 rc= memcached_connect(ptr);
192
193 if (rc != MEMCACHED_SUCCESS)
194 {
195 *error= rc;
196 return NULL;
197 }
198
199 stats= (memcached_stat_st *)malloc(sizeof(memcached_st)*(ptr->number_of_hosts+1));
200 if (stats)
201 {
202 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
203 return NULL;
204 }
205 memset(stats, 0, sizeof(memcached_st)*(ptr->number_of_hosts+1));
206
207 for (x= 0; x < ptr->number_of_hosts; x++)
208 {
209 rc= memcached_stats_fetch(ptr, stats+x, args, x);
210 if (rc != MEMCACHED_SUCCESS)
211 rc= MEMCACHED_SOME_ERRORS;
212 }
213
214 *error= x == 0 ? MEMCACHED_SUCCESS : rc;
215 return stats;
216 }
217
218 memcached_return memcached_stat_hostname(memcached_stat_st *stat, char *args,
219 char *hostname, unsigned int port)
220 {
221 memcached_return rc;
222 memcached_st memc;
223
224 memcached_init(&memc);
225
226 memcached_server_add(&memc, hostname, port);
227
228 rc= memcached_connect(&memc);
229
230 if (rc != MEMCACHED_SUCCESS)
231 return rc;
232
233 rc= memcached_stats_fetch(&memc, stat, args, 0);
234
235 memcached_deinit(&memc);
236
237 return rc;
238 }