Broke library up into multiple files.
[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 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 (args)
136 send_length= snprintf(buffer, HUGE_STRING_LEN,
137 "stats %s\r\n", args);
138 else
139 send_length= snprintf(buffer, HUGE_STRING_LEN,
140 "stats\r\n");
141
142 if ((send(memc.fd, buffer, send_length, 0) == -1))
143 {
144 fprintf(stderr, "failed on stats\n");
145
146 rc= MEMCACHED_WRITE_FAILURE;
147 goto error;
148 }
149
150 rc= memcached_response(&memc, buffer, HUGE_STRING_LEN);
151
152 if (rc == MEMCACHED_SUCCESS)
153 {
154 char *string_ptr, *end_ptr;
155 char *key, *value;
156
157 string_ptr= buffer;
158 while (1)
159 {
160 if (memcmp(string_ptr, "STAT ", 5))
161 break;
162 string_ptr+= 5;
163 for (end_ptr= string_ptr; *end_ptr != ' '; end_ptr++);
164 key= string_ptr;
165 key[(size_t)(end_ptr-string_ptr)]= 0;
166 printf("Key %s\n", key);
167
168 string_ptr= end_ptr + 1;
169 for (end_ptr= string_ptr; *end_ptr != '\r'; end_ptr++);
170 value= string_ptr;
171 value[(size_t)(end_ptr-string_ptr)]= 0;
172 printf("Value %s\n", value);
173 string_ptr= end_ptr + 2;
174 set_data(stat, key, value);
175 }
176 }
177
178 error:
179 memcached_deinit(&memc);
180
181 return rc;
182 }