2 * collectd - src/zookeeper.c
3 * Copyright (C) 2014 Google, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Jeremy Katz <jeremy at katzbox.net>
33 #include <netinet/in.h>
34 #include <netinet/tcp.h>
37 #define ZOOKEEPER_DEF_HOST "127.0.0.1"
38 #define ZOOKEEPER_DEF_PORT "2181"
40 static char *zk_host = NULL;
41 static char *zk_port = NULL;
43 static const char *config_keys[] = {"Host", "Port"};
44 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
46 static int zookeeper_config(const char *key, const char *value) {
47 if (strncmp(key, "Host", strlen("Host")) == 0) {
49 zk_host = strdup(value);
50 } else if (strncmp(key, "Port", strlen("Port")) == 0) {
52 zk_port = strdup(value);
59 static void zookeeper_submit_gauge(const char *type, const char *type_inst,
61 value_list_t vl = VALUE_LIST_INIT;
63 vl.values = &(value_t){.gauge = value};
65 sstrncpy(vl.plugin, "zookeeper", sizeof(vl.plugin));
66 sstrncpy(vl.type, type, sizeof(vl.type));
67 if (type_inst != NULL)
68 sstrncpy(vl.type_instance, type_inst, sizeof(vl.type_instance));
70 plugin_dispatch_values(&vl);
71 } /* zookeeper_submit_gauge */
73 static void zookeeper_submit_derive(const char *type, const char *type_inst,
75 value_list_t vl = VALUE_LIST_INIT;
77 vl.values = &(value_t){.derive = value};
79 sstrncpy(vl.plugin, "zookeeper", sizeof(vl.plugin));
80 sstrncpy(vl.type, type, sizeof(vl.type));
81 if (type_inst != NULL)
82 sstrncpy(vl.type_instance, type_inst, sizeof(vl.type_instance));
84 plugin_dispatch_values(&vl);
85 } /* zookeeper_submit_derive */
87 static int zookeeper_connect(void) {
90 struct addrinfo *ai_list;
94 host = (zk_host != NULL) ? zk_host : ZOOKEEPER_DEF_HOST;
95 port = (zk_port != NULL) ? zk_port : ZOOKEEPER_DEF_PORT;
97 struct addrinfo ai_hints = {.ai_family = AF_UNSPEC,
98 .ai_socktype = SOCK_STREAM};
100 status = getaddrinfo(host, port, &ai_hints, &ai_list);
103 INFO("getaddrinfo failed: %s",
104 (status == EAI_SYSTEM) ? sstrerror(errno, errbuf, sizeof(errbuf))
105 : gai_strerror(status));
109 for (struct addrinfo *ai = ai_list; ai != NULL; ai = ai->ai_next) {
110 sk = socket(ai->ai_family, SOCK_STREAM, 0);
113 WARNING("zookeeper: socket(2) failed: %s",
114 sstrerror(errno, errbuf, sizeof(errbuf)));
117 status = (int)connect(sk, ai->ai_addr, ai->ai_addrlen);
122 WARNING("zookeeper: connect(2) failed: %s",
123 sstrerror(errno, errbuf, sizeof(errbuf)));
131 freeaddrinfo(ai_list);
133 } /* int zookeeper_connect */
135 static int zookeeper_query(char *buffer, size_t buffer_size) {
139 sk = zookeeper_connect();
141 ERROR("zookeeper: Could not connect to daemon");
145 status = (int)swrite(sk, "mntr\r\n", strlen("mntr\r\n"));
148 ERROR("zookeeper: write(2) failed: %s",
149 sstrerror(errno, errbuf, sizeof(errbuf)));
154 memset(buffer, 0, buffer_size);
157 while ((status = (int)recv(sk, buffer + buffer_fill,
158 buffer_size - buffer_fill, /* flags = */ 0)) !=
162 if ((errno == EAGAIN) || (errno == EINTR))
164 ERROR("zookeeper: Error reading from socket: %s",
165 sstrerror(errno, errbuf, sizeof(errbuf)));
170 buffer_fill += (size_t)status;
174 if (buffer_fill == 0) {
175 WARNING("zookeeper: No data returned by MNTR command.");
181 } /* int zookeeper_query */
183 static int zookeeper_read(void) {
190 if (zookeeper_query(buf, sizeof(buf)) < 0) {
196 while ((line = strtok_r(ptr, "\n\r", &save_ptr)) != NULL) {
198 if (strsplit(line, fields, 2) != 2) {
201 #define FIELD_CHECK(check, expected) \
202 (strncmp(check, expected, strlen(expected)) == 0)
204 if (FIELD_CHECK(fields[0], "zk_avg_latency")) {
205 zookeeper_submit_gauge("latency", "avg", atol(fields[1]));
206 } else if (FIELD_CHECK(fields[0], "zk_min_latency")) {
207 zookeeper_submit_gauge("latency", "min", atol(fields[1]));
208 } else if (FIELD_CHECK(fields[0], "zk_max_latency")) {
209 zookeeper_submit_gauge("latency", "max", atol(fields[1]));
210 } else if (FIELD_CHECK(fields[0], "zk_packets_received")) {
211 zookeeper_submit_derive("packets", "received", atol(fields[1]));
212 } else if (FIELD_CHECK(fields[0], "zk_packets_sent")) {
213 zookeeper_submit_derive("packets", "sent", atol(fields[1]));
214 } else if (FIELD_CHECK(fields[0], "zk_num_alive_connections")) {
215 zookeeper_submit_gauge("current_connections", NULL, atol(fields[1]));
216 } else if (FIELD_CHECK(fields[0], "zk_outstanding_requests")) {
217 zookeeper_submit_gauge("requests", "outstanding", atol(fields[1]));
218 } else if (FIELD_CHECK(fields[0], "zk_znode_count")) {
219 zookeeper_submit_gauge("gauge", "znode", atol(fields[1]));
220 } else if (FIELD_CHECK(fields[0], "zk_watch_count")) {
221 zookeeper_submit_gauge("gauge", "watch", atol(fields[1]));
222 } else if (FIELD_CHECK(fields[0], "zk_ephemerals_count")) {
223 zookeeper_submit_gauge("gauge", "ephemerals", atol(fields[1]));
224 } else if (FIELD_CHECK(fields[0], "zk_ephemerals_count")) {
225 zookeeper_submit_gauge("gauge", "ephemerals", atol(fields[1]));
226 } else if (FIELD_CHECK(fields[0], "zk_ephemerals_count")) {
227 zookeeper_submit_gauge("gauge", "ephemerals", atol(fields[1]));
228 } else if (FIELD_CHECK(fields[0], "zk_approximate_data_size")) {
229 zookeeper_submit_gauge("bytes", "approximate_data_size", atol(fields[1]));
230 } else if (FIELD_CHECK(fields[0], "zk_followers")) {
231 zookeeper_submit_gauge("count", "followers", atol(fields[1]));
232 } else if (FIELD_CHECK(fields[0], "zk_synced_followers")) {
233 zookeeper_submit_gauge("count", "synced_followers", atol(fields[1]));
234 } else if (FIELD_CHECK(fields[0], "zk_pending_syncs")) {
235 zookeeper_submit_gauge("count", "pending_syncs", atol(fields[1]));
237 DEBUG("Uncollected zookeeper MNTR field %s", fields[0]);
242 } /* zookeeper_read */
244 void module_register(void) {
245 plugin_register_config("zookeeper", zookeeper_config, config_keys,
247 plugin_register_read("zookeeper", zookeeper_read);
248 } /* void module_register */