memcached_server_add() now works so you can connect to host other then
[m6w6/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 memcached_stat_st **memcached_stat(memcached_st *ptr, memcached_return *error)
119 {
120 return NULL;
121 }
122
123 memcached_return memcached_stat_hostname(memcached_stat_st *stat, char *args,
124 char *hostname, unsigned int port)
125 {
126 size_t send_length;
127 memcached_return rc;
128 char buffer[HUGE_STRING_LEN];
129 memcached_st memc;
130
131 memcached_init(&memc);
132
133 rc= memcached_connect(&memc);
134
135 if (rc != MEMCACHED_SUCCESS)
136 return rc;
137
138 if (args)
139 send_length= snprintf(buffer, HUGE_STRING_LEN,
140 "stats %s\r\n", args);
141 else
142 send_length= snprintf(buffer, HUGE_STRING_LEN,
143 "stats\r\n");
144
145 if ((send(memc.fd, buffer, send_length, 0) == -1))
146 {
147 fprintf(stderr, "failed on stats\n");
148
149 rc= MEMCACHED_WRITE_FAILURE;
150 goto error;
151 }
152
153 rc= memcached_response(&memc, buffer, HUGE_STRING_LEN);
154
155 if (rc == MEMCACHED_SUCCESS)
156 {
157 char *string_ptr, *end_ptr;
158 char *key, *value;
159
160 string_ptr= buffer;
161 while (1)
162 {
163 if (memcmp(string_ptr, "STAT ", 5))
164 break;
165 string_ptr+= 5;
166 for (end_ptr= string_ptr; *end_ptr != ' '; end_ptr++);
167 key= string_ptr;
168 key[(size_t)(end_ptr-string_ptr)]= 0;
169
170 string_ptr= end_ptr + 1;
171 for (end_ptr= string_ptr; *end_ptr != '\r'; end_ptr++);
172 value= string_ptr;
173 value[(size_t)(end_ptr-string_ptr)]= 0;
174 string_ptr= end_ptr + 2;
175 set_data(stat, key, value);
176 }
177 }
178
179 error:
180 memcached_deinit(&memc);
181
182 return rc;
183 }