| 1 |
/** |
| 2 |
* collectd - src/zookeeper.c |
| 3 |
* Copyright (C) 2014 Google, Inc. |
| 4 |
* |
| 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: |
| 11 |
* |
| 12 |
* The above copyright notice and this permission notice shall be included in |
| 13 |
* all copies or substantial portions of the Software. |
| 14 |
* |
| 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. |
| 22 |
* |
| 23 |
* Authors: |
| 24 |
* Jeremy Katz <jeremy at katzbox.net> |
| 25 |
**/ |
| 26 |
|
| 27 |
#include "collectd.h" |
| 28 |
|
| 29 |
#include "plugin.h" |
| 30 |
#include "utils/common/common.h" |
| 31 |
|
| 32 |
#include <netdb.h> |
| 33 |
#include <netinet/in.h> |
| 34 |
#include <netinet/tcp.h> |
| 35 |
#include <sys/un.h> |
| 36 |
|
| 37 |
#define ZOOKEEPER_DEF_HOST "127.0.0.1" |
| 38 |
#define ZOOKEEPER_DEF_PORT "2181" |
| 39 |
|
| 40 |
static char *zk_host; |
| 41 |
static char *zk_port; |
| 42 |
|
| 43 |
static const char *config_keys[] = {"Host", "Port"}; |
| 44 |
static int config_keys_num = STATIC_ARRAY_SIZE(config_keys); |
| 45 |
|
| 46 |
static int zookeeper_config(const char *key, const char *value) { |
| 47 |
if (strncmp(key, "Host", strlen("Host")) == 0) { |
| 48 |
sfree(zk_host); |
| 49 |
zk_host = strdup(value); |
| 50 |
} else if (strncmp(key, "Port", strlen("Port")) == 0) { |
| 51 |
sfree(zk_port); |
| 52 |
zk_port = strdup(value); |
| 53 |
} else { |
| 54 |
return -1; |
| 55 |
} |
| 56 |
return 0; |
| 57 |
} |
| 58 |
|
| 59 |
static void zookeeper_submit_gauge(const char *type, const char *type_inst, |
| 60 |
gauge_t value) { |
| 61 |
value_list_t vl = VALUE_LIST_INIT; |
| 62 |
|
| 63 |
vl.values = &(value_t){.gauge = value}; |
| 64 |
vl.values_len = 1; |
| 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)); |
| 69 |
|
| 70 |
plugin_dispatch_values(&vl); |
| 71 |
} /* zookeeper_submit_gauge */ |
| 72 |
|
| 73 |
static void zookeeper_submit_derive(const char *type, const char *type_inst, |
| 74 |
derive_t value) { |
| 75 |
value_list_t vl = VALUE_LIST_INIT; |
| 76 |
|
| 77 |
vl.values = &(value_t){.derive = value}; |
| 78 |
vl.values_len = 1; |
| 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)); |
| 83 |
|
| 84 |
plugin_dispatch_values(&vl); |
| 85 |
} /* zookeeper_submit_derive */ |
| 86 |
|
| 87 |
static int zookeeper_connect(void) { |
| 88 |
int sk = -1; |
| 89 |
int status; |
| 90 |
struct addrinfo *ai_list; |
| 91 |
const char *host; |
| 92 |
const char *port; |
| 93 |
|
| 94 |
host = (zk_host != NULL) ? zk_host : ZOOKEEPER_DEF_HOST; |
| 95 |
port = (zk_port != NULL) ? zk_port : ZOOKEEPER_DEF_PORT; |
| 96 |
|
| 97 |
struct addrinfo ai_hints = {.ai_family = AF_UNSPEC, |
| 98 |
.ai_socktype = SOCK_STREAM}; |
| 99 |
|
| 100 |
status = getaddrinfo(host, port, &ai_hints, &ai_list); |
| 101 |
if (status != 0) { |
| 102 |
INFO("getaddrinfo failed: %s", |
| 103 |
(status == EAI_SYSTEM) ? STRERRNO : gai_strerror(status)); |
| 104 |
return -1; |
| 105 |
} |
| 106 |
|
| 107 |
for (struct addrinfo *ai = ai_list; ai != NULL; ai = ai->ai_next) { |
| 108 |
sk = socket(ai->ai_family, SOCK_STREAM, 0); |
| 109 |
if (sk < 0) { |
| 110 |
WARNING("zookeeper: socket(2) failed: %s", STRERRNO); |
| 111 |
continue; |
| 112 |
} |
| 113 |
status = (int)connect(sk, ai->ai_addr, ai->ai_addrlen); |
| 114 |
if (status != 0) { |
| 115 |
close(sk); |
| 116 |
sk = -1; |
| 117 |
WARNING("zookeeper: connect(2) failed: %s", STRERRNO); |
| 118 |
continue; |
| 119 |
} |
| 120 |
|
| 121 |
/* connected */ |
| 122 |
break; |
| 123 |
} |
| 124 |
|
| 125 |
freeaddrinfo(ai_list); |
| 126 |
return sk; |
| 127 |
} /* int zookeeper_connect */ |
| 128 |
|
| 129 |
static int zookeeper_query(char *buffer, size_t buffer_size) { |
| 130 |
int sk, status; |
| 131 |
size_t buffer_fill; |
| 132 |
|
| 133 |
sk = zookeeper_connect(); |
| 134 |
if (sk < 0) { |
| 135 |
ERROR("zookeeper: Could not connect to daemon"); |
| 136 |
return -1; |
| 137 |
} |
| 138 |
|
| 139 |
status = (int)swrite(sk, "mntr\r\n", strlen("mntr\r\n")); |
| 140 |
if (status != 0) { |
| 141 |
ERROR("zookeeper: write(2) failed: %s", STRERRNO); |
| 142 |
close(sk); |
| 143 |
return -1; |
| 144 |
} |
| 145 |
|
| 146 |
memset(buffer, 0, buffer_size); |
| 147 |
buffer_fill = 0; |
| 148 |
|
| 149 |
while ((status = (int)recv(sk, buffer + buffer_fill, |
| 150 |
buffer_size - buffer_fill, /* flags = */ 0)) != |
| 151 |
0) { |
| 152 |
if (status < 0) { |
| 153 |
if ((errno == EAGAIN) || (errno == EINTR)) |
| 154 |
continue; |
| 155 |
ERROR("zookeeper: Error reading from socket: %s", STRERRNO); |
| 156 |
close(sk); |
| 157 |
return -1; |
| 158 |
} |
| 159 |
|
| 160 |
buffer_fill += (size_t)status; |
| 161 |
} /* while (recv) */ |
| 162 |
|
| 163 |
status = 0; |
| 164 |
if (buffer_fill == 0) { |
| 165 |
WARNING("zookeeper: No data returned by MNTR command."); |
| 166 |
status = -1; |
| 167 |
} |
| 168 |
|
| 169 |
close(sk); |
| 170 |
return status; |
| 171 |
} /* int zookeeper_query */ |
| 172 |
|
| 173 |
static int zookeeper_read(void) { |
| 174 |
char buf[4096]; |
| 175 |
char *ptr; |
| 176 |
char *save_ptr; |
| 177 |
char *line; |
| 178 |
char *fields[2]; |
| 179 |
long followers = 0; |
| 180 |
|
| 181 |
if (zookeeper_query(buf, sizeof(buf)) < 0) { |
| 182 |
zookeeper_submit_gauge("count", "quorum", -1); |
| 183 |
return 0; |
| 184 |
} |
| 185 |
|
| 186 |
ptr = buf; |
| 187 |
save_ptr = NULL; |
| 188 |
while ((line = strtok_r(ptr, "\n\r", &save_ptr)) != NULL) { |
| 189 |
ptr = NULL; |
| 190 |
if (strsplit(line, fields, 2) != 2) { |
| 191 |
continue; |
| 192 |
} |
| 193 |
#define FIELD_CHECK(check, expected) \ |
| 194 |
(strncmp(check, expected, strlen(expected)) == 0) |
| 195 |
|
| 196 |
if (FIELD_CHECK(fields[0], "zk_avg_latency")) { |
| 197 |
zookeeper_submit_gauge("latency", "avg", atol(fields[1])); |
| 198 |
} else if (FIELD_CHECK(fields[0], "zk_min_latency")) { |
| 199 |
zookeeper_submit_gauge("latency", "min", atol(fields[1])); |
| 200 |
} else if (FIELD_CHECK(fields[0], "zk_max_latency")) { |
| 201 |
zookeeper_submit_gauge("latency", "max", atol(fields[1])); |
| 202 |
} else if (FIELD_CHECK(fields[0], "zk_packets_received")) { |
| 203 |
zookeeper_submit_derive("packets", "received", atol(fields[1])); |
| 204 |
} else if (FIELD_CHECK(fields[0], "zk_packets_sent")) { |
| 205 |
zookeeper_submit_derive("packets", "sent", atol(fields[1])); |
| 206 |
} else if (FIELD_CHECK(fields[0], "zk_num_alive_connections")) { |
| 207 |
zookeeper_submit_gauge("current_connections", NULL, atol(fields[1])); |
| 208 |
} else if (FIELD_CHECK(fields[0], "zk_outstanding_requests")) { |
| 209 |
zookeeper_submit_gauge("requests", "outstanding", atol(fields[1])); |
| 210 |
} else if (FIELD_CHECK(fields[0], "zk_znode_count")) { |
| 211 |
zookeeper_submit_gauge("gauge", "znode", atol(fields[1])); |
| 212 |
} else if (FIELD_CHECK(fields[0], "zk_watch_count")) { |
| 213 |
zookeeper_submit_gauge("gauge", "watch", atol(fields[1])); |
| 214 |
} else if (FIELD_CHECK(fields[0], "zk_ephemerals_count")) { |
| 215 |
zookeeper_submit_gauge("gauge", "ephemerals", atol(fields[1])); |
| 216 |
} else if (FIELD_CHECK(fields[0], "zk_open_file_descriptor_count")) { |
| 217 |
zookeeper_submit_gauge("file_handles", "open", atol(fields[1])); |
| 218 |
} else if (FIELD_CHECK(fields[0], "zk_max_file_descriptor_count")) { |
| 219 |
zookeeper_submit_gauge("file_handles", "max", atol(fields[1])); |
| 220 |
} else if (FIELD_CHECK(fields[0], "zk_approximate_data_size")) { |
| 221 |
zookeeper_submit_gauge("bytes", "approximate_data_size", atol(fields[1])); |
| 222 |
} else if (FIELD_CHECK(fields[0], "zk_followers")) { |
| 223 |
followers = atol(fields[1]); |
| 224 |
zookeeper_submit_gauge("count", "followers", followers); |
| 225 |
} else if (FIELD_CHECK(fields[0], "zk_synced_followers")) { |
| 226 |
zookeeper_submit_gauge("count", "synced_followers", atol(fields[1])); |
| 227 |
} else if (FIELD_CHECK(fields[0], "zk_pending_syncs")) { |
| 228 |
zookeeper_submit_gauge("count", "pending_syncs", atol(fields[1])); |
| 229 |
} else if (FIELD_CHECK(fields[0], "zk_last_proposal_size")) { |
| 230 |
zookeeper_submit_gauge("bytes", "last_proposal", atol(fields[1])); |
| 231 |
} else if (FIELD_CHECK(fields[0], "zk_min_proposal_size")) { |
| 232 |
zookeeper_submit_gauge("bytes", "min_proposal", atol(fields[1])); |
| 233 |
} else if (FIELD_CHECK(fields[0], "zk_max_proposal_size")) { |
| 234 |
zookeeper_submit_gauge("bytes", "max_proposal", atol(fields[1])); |
| 235 |
} else { |
| 236 |
DEBUG("Uncollected zookeeper MNTR field %s", fields[0]); |
| 237 |
} |
| 238 |
} |
| 239 |
/* Reports 0 for followers, # when zk_followers present. Intended to be used |
| 240 |
* for quorum detection by taking max for each time period. */ |
| 241 |
zookeeper_submit_gauge("count", "quorum", followers); |
| 242 |
|
| 243 |
return 0; |
| 244 |
} /* zookeeper_read */ |
| 245 |
|
| 246 |
void module_register(void) { |
| 247 |
plugin_register_config("zookeeper", zookeeper_config, config_keys, |
| 248 |
config_keys_num); |
| 249 |
plugin_register_read("zookeeper", zookeeper_read); |
| 250 |
} /* void module_register */ |