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,
62 value_list_t vl = VALUE_LIST_INIT;
64 values[0].gauge = val;
68 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
69 sstrncpy(vl.plugin, "zookeeper", sizeof(vl.plugin));
70 sstrncpy(vl.type, type, sizeof(vl.type));
71 if (type_inst != NULL)
72 sstrncpy(vl.type_instance, type_inst, sizeof(vl.type_instance));
74 plugin_dispatch_values(&vl);
75 } /* zookeeper_submit_gauge */
77 static void zookeeper_submit_derive(const char *type, const char *type_inst,
80 value_list_t vl = VALUE_LIST_INIT;
82 values[0].derive = val;
86 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
87 sstrncpy(vl.plugin, "zookeeper", sizeof(vl.plugin));
88 sstrncpy(vl.type, type, sizeof(vl.type));
89 if (type_inst != NULL)
90 sstrncpy(vl.type_instance, type_inst, sizeof(vl.type_instance));
92 plugin_dispatch_values(&vl);
93 } /* zookeeper_submit_derive */
95 static int zookeeper_connect(void) {
98 struct addrinfo *ai_list;
102 host = (zk_host != NULL) ? zk_host : ZOOKEEPER_DEF_HOST;
103 port = (zk_port != NULL) ? zk_port : ZOOKEEPER_DEF_PORT;
105 struct addrinfo ai_hints = {.ai_family = AF_UNSPEC,
106 .ai_socktype = SOCK_STREAM};
108 status = getaddrinfo(host, port, &ai_hints, &ai_list);
111 INFO("getaddrinfo failed: %s",
112 (status == EAI_SYSTEM) ? sstrerror(errno, errbuf, sizeof(errbuf))
113 : gai_strerror(status));
117 for (struct addrinfo *ai = ai_list; ai != NULL; ai = ai->ai_next) {
118 sk = socket(ai->ai_family, SOCK_STREAM, 0);
121 WARNING("zookeeper: socket(2) failed: %s",
122 sstrerror(errno, errbuf, sizeof(errbuf)));
125 status = (int)connect(sk, ai->ai_addr, ai->ai_addrlen);
130 WARNING("zookeeper: connect(2) failed: %s",
131 sstrerror(errno, errbuf, sizeof(errbuf)));
139 freeaddrinfo(ai_list);
141 } /* int zookeeper_connect */
143 static int zookeeper_query(char *buffer, size_t buffer_size) {
147 sk = zookeeper_connect();
149 ERROR("zookeeper: Could not connect to daemon");
153 status = (int)swrite(sk, "mntr\r\n", strlen("mntr\r\n"));
156 ERROR("zookeeper: write(2) failed: %s",
157 sstrerror(errno, errbuf, sizeof(errbuf)));
162 memset(buffer, 0, buffer_size);
165 while ((status = (int)recv(sk, buffer + buffer_fill,
166 buffer_size - buffer_fill, /* flags = */ 0)) !=
170 if ((errno == EAGAIN) || (errno == EINTR))
172 ERROR("zookeeper: Error reading from socket: %s",
173 sstrerror(errno, errbuf, sizeof(errbuf)));
178 buffer_fill += (size_t)status;
182 if (buffer_fill == 0) {
183 WARNING("zookeeper: No data returned by MNTR command.");
189 } /* int zookeeper_query */
191 static int zookeeper_read(void) {
198 if (zookeeper_query(buf, sizeof(buf)) < 0) {
204 while ((line = strtok_r(ptr, "\n\r", &save_ptr)) != NULL) {
206 if (strsplit(line, fields, 2) != 2) {
209 #define FIELD_CHECK(check, expected) \
210 (strncmp(check, expected, strlen(expected)) == 0)
212 if (FIELD_CHECK(fields[0], "zk_avg_latency")) {
213 zookeeper_submit_gauge("latency", "avg", atol(fields[1]));
214 } else if (FIELD_CHECK(fields[0], "zk_min_latency")) {
215 zookeeper_submit_gauge("latency", "min", atol(fields[1]));
216 } else if (FIELD_CHECK(fields[0], "zk_max_latency")) {
217 zookeeper_submit_gauge("latency", "max", atol(fields[1]));
218 } else if (FIELD_CHECK(fields[0], "zk_packets_received")) {
219 zookeeper_submit_derive("packets", "received", atol(fields[1]));
220 } else if (FIELD_CHECK(fields[0], "zk_packets_sent")) {
221 zookeeper_submit_derive("packets", "sent", atol(fields[1]));
222 } else if (FIELD_CHECK(fields[0], "zk_num_alive_connections")) {
223 zookeeper_submit_gauge("current_connections", NULL, atol(fields[1]));
224 } else if (FIELD_CHECK(fields[0], "zk_outstanding_requests")) {
225 zookeeper_submit_gauge("requests", "outstanding", atol(fields[1]));
226 } else if (FIELD_CHECK(fields[0], "zk_znode_count")) {
227 zookeeper_submit_gauge("gauge", "znode", atol(fields[1]));
228 } else if (FIELD_CHECK(fields[0], "zk_watch_count")) {
229 zookeeper_submit_gauge("gauge", "watch", atol(fields[1]));
230 } else if (FIELD_CHECK(fields[0], "zk_ephemerals_count")) {
231 zookeeper_submit_gauge("gauge", "ephemerals", atol(fields[1]));
232 } else if (FIELD_CHECK(fields[0], "zk_ephemerals_count")) {
233 zookeeper_submit_gauge("gauge", "ephemerals", atol(fields[1]));
234 } else if (FIELD_CHECK(fields[0], "zk_ephemerals_count")) {
235 zookeeper_submit_gauge("gauge", "ephemerals", atol(fields[1]));
236 } else if (FIELD_CHECK(fields[0], "zk_approximate_data_size")) {
237 zookeeper_submit_gauge("bytes", "approximate_data_size", atol(fields[1]));
238 } else if (FIELD_CHECK(fields[0], "zk_followers")) {
239 zookeeper_submit_gauge("count", "followers", atol(fields[1]));
240 } else if (FIELD_CHECK(fields[0], "zk_synced_followers")) {
241 zookeeper_submit_gauge("count", "synced_followers", atol(fields[1]));
242 } else if (FIELD_CHECK(fields[0], "zk_pending_syncs")) {
243 zookeeper_submit_gauge("count", "pending_syncs", atol(fields[1]));
245 DEBUG("Uncollected zookeeper MNTR field %s", fields[0]);
250 } /* zookeeper_read */
252 void module_register(void) {
253 plugin_register_config("zookeeper", zookeeper_config, config_keys,
255 plugin_register_read("zookeeper", zookeeper_read);
256 } /* void module_register */