1 /**
2 * collectd - src/write_kafka.c
3 * Copyright (C) 2014 Pierre-Yves Ritschard
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 * Pierre-Yves Ritschard <pyr at spootnik.org>
25 */
27 #include "collectd.h"
28 #include "plugin.h"
29 #include "common.h"
30 #include "configfile.h"
31 #include "utils_cache.h"
32 #include "utils_cmd_putval.h"
33 #include "utils_format_graphite.h"
34 #include "utils_format_json.h"
35 #include "utils_crc32.h"
37 #include <stdint.h>
38 #include <librdkafka/rdkafka.h>
39 #include <zlib.h>
40 #include <errno.h>
42 struct kafka_topic_context {
43 #define KAFKA_FORMAT_JSON 0
44 #define KAFKA_FORMAT_COMMAND 1
45 #define KAFKA_FORMAT_GRAPHITE 2
46 uint8_t format;
47 unsigned int graphite_flags;
48 _Bool store_rates;
49 rd_kafka_topic_conf_t *conf;
50 rd_kafka_topic_t *topic;
51 rd_kafka_conf_t *kafka_conf;
52 rd_kafka_t *kafka;
53 char *key;
54 char *prefix;
55 char *postfix;
56 char escape_char;
57 char *topic_name;
58 pthread_mutex_t lock;
59 };
61 static int kafka_handle(struct kafka_topic_context *);
62 static int kafka_write(const data_set_t *, const value_list_t *, user_data_t *);
63 static int32_t kafka_partition(const rd_kafka_topic_t *, const void *, size_t,
64 int32_t, void *, void *);
66 #if defined HAVE_LIBRDKAFKA_LOGGER || defined HAVE_LIBRDKAFKA_LOG_CB
67 static void kafka_log(const rd_kafka_t *, int, const char *, const char *);
69 static void kafka_log(const rd_kafka_t *rkt, int level,
70 const char *fac, const char *msg)
71 {
72 plugin_log(level, "%s", msg);
73 }
74 #endif
76 static uint32_t kafka_hash(const char *keydata, size_t keylen)
77 {
78 uint32_t hash = 5381;
79 for (; keylen > 0; keylen--)
80 hash = ((hash << 5) + hash) + keydata[keylen - 1];
81 return hash;
82 }
84 static int32_t kafka_partition(const rd_kafka_topic_t *rkt,
85 const void *keydata, size_t keylen,
86 int32_t partition_cnt, void *p, void *m)
87 {
88 uint32_t key = kafka_hash(keydata, keylen);
89 uint32_t target = key % partition_cnt;
90 int32_t i = partition_cnt;
92 while (--i > 0 && !rd_kafka_topic_partition_available(rkt, target)) {
93 target = (target + 1) % partition_cnt;
94 }
95 return target;
96 }
98 static int kafka_handle(struct kafka_topic_context *ctx) /* {{{ */
99 {
100 char errbuf[1024];
101 rd_kafka_conf_t *conf;
102 rd_kafka_topic_conf_t *topic_conf;
104 if (ctx->kafka != NULL && ctx->topic != NULL)
105 return(0);
107 if (ctx->kafka == NULL) {
108 if ((conf = rd_kafka_conf_dup(ctx->kafka_conf)) == NULL) {
109 ERROR("write_kafka plugin: cannot duplicate kafka config");
110 return(1);
111 }
113 if ((ctx->kafka = rd_kafka_new(RD_KAFKA_PRODUCER, conf,
114 errbuf, sizeof(errbuf))) == NULL) {
115 ERROR("write_kafka plugin: cannot create kafka handle.");
116 return 1;
117 }
119 rd_kafka_conf_destroy(ctx->kafka_conf);
120 ctx->kafka_conf = NULL;
122 INFO ("write_kafka plugin: created KAFKA handle : %s", rd_kafka_name(ctx->kafka));
124 #if defined(HAVE_LIBRDKAFKA_LOGGER) && !defined(HAVE_LIBRDKAFKA_LOG_CB)
125 rd_kafka_set_logger(ctx->kafka, kafka_log);
126 #endif
127 }
129 if (ctx->topic == NULL ) {
130 if ((topic_conf = rd_kafka_topic_conf_dup(ctx->conf)) == NULL) {
131 ERROR("write_kafka plugin: cannot duplicate kafka topic config");
132 return 1;
133 }
135 if ((ctx->topic = rd_kafka_topic_new(ctx->kafka, ctx->topic_name,
136 topic_conf)) == NULL) {
137 ERROR("write_kafka plugin: cannot create topic : %s\n",
138 rd_kafka_err2str(rd_kafka_errno2err(errno)));
139 return errno;
140 }
142 rd_kafka_topic_conf_destroy(ctx->conf);
143 ctx->conf = NULL;
145 INFO ("write_kafka plugin: handle created for topic : %s", rd_kafka_topic_name(ctx->topic));
146 }
148 return(0);
150 } /* }}} int kafka_handle */
152 static int kafka_write(const data_set_t *ds, /* {{{ */
153 const value_list_t *vl,
154 user_data_t *ud)
155 {
156 int status = 0;
157 void *key;
158 size_t keylen = 0;
159 char buffer[8192];
160 size_t bfree = sizeof(buffer);
161 size_t bfill = 0;
162 size_t blen = 0;
163 struct kafka_topic_context *ctx = ud->data;
165 if ((ds == NULL) || (vl == NULL) || (ctx == NULL))
166 return EINVAL;
168 pthread_mutex_lock (&ctx->lock);
169 status = kafka_handle(ctx);
170 pthread_mutex_unlock (&ctx->lock);
171 if( status != 0 )
172 return status;
174 bzero(buffer, sizeof(buffer));
176 switch (ctx->format) {
177 case KAFKA_FORMAT_COMMAND:
178 status = create_putval(buffer, sizeof(buffer), ds, vl);
179 if (status != 0) {
180 ERROR("write_kafka plugin: create_putval failed with status %i.",
181 status);
182 return status;
183 }
184 blen = strlen(buffer);
185 break;
186 case KAFKA_FORMAT_JSON:
187 format_json_initialize(buffer, &bfill, &bfree);
188 format_json_value_list(buffer, &bfill, &bfree, ds, vl,
189 ctx->store_rates);
190 format_json_finalize(buffer, &bfill, &bfree);
191 blen = strlen(buffer);
192 break;
193 case KAFKA_FORMAT_GRAPHITE:
194 status = format_graphite(buffer, sizeof(buffer), ds, vl,
195 ctx->prefix, ctx->postfix, ctx->escape_char,
196 ctx->graphite_flags);
197 if (status != 0) {
198 ERROR("write_kafka plugin: format_graphite failed with status %i.",
199 status);
200 return status;
201 }
202 blen = strlen(buffer);
203 break;
204 default:
205 ERROR("write_kafka plugin: invalid format %i.", ctx->format);
206 return -1;
207 }
209 key = ctx->key;
210 if (key != NULL)
211 keylen = strlen (key);
212 else
213 keylen = 0;
215 rd_kafka_produce(ctx->topic, RD_KAFKA_PARTITION_UA,
216 RD_KAFKA_MSG_F_COPY, buffer, blen,
217 key, keylen, NULL);
219 return status;
220 } /* }}} int kafka_write */
222 static void kafka_topic_context_free(void *p) /* {{{ */
223 {
224 struct kafka_topic_context *ctx = p;
226 if (ctx == NULL)
227 return;
229 if (ctx->topic_name != NULL)
230 sfree(ctx->topic_name);
231 if (ctx->topic != NULL)
232 rd_kafka_topic_destroy(ctx->topic);
233 if (ctx->conf != NULL)
234 rd_kafka_topic_conf_destroy(ctx->conf);
235 if (ctx->kafka_conf != NULL)
236 rd_kafka_conf_destroy(ctx->kafka_conf);
237 if (ctx->kafka != NULL)
238 rd_kafka_destroy(ctx->kafka);
240 sfree(ctx);
241 } /* }}} void kafka_topic_context_free */
243 static void kafka_config_topic(rd_kafka_conf_t *conf, oconfig_item_t *ci) /* {{{ */
244 {
245 int status;
246 int i;
247 struct kafka_topic_context *tctx;
248 char *key = NULL;
249 char *val;
250 char callback_name[DATA_MAX_NAME_LEN];
251 char errbuf[1024];
252 user_data_t ud;
253 oconfig_item_t *child;
254 rd_kafka_conf_res_t ret;
256 if ((tctx = calloc(1, sizeof (*tctx))) == NULL) {
257 ERROR ("write_kafka plugin: calloc failed.");
258 return;
259 }
261 tctx->escape_char = '.';
262 tctx->store_rates = 1;
263 tctx->format = KAFKA_FORMAT_JSON;
264 tctx->key = NULL;
266 if ((tctx->kafka_conf = rd_kafka_conf_dup(conf)) == NULL) {
267 sfree(tctx);
268 ERROR("write_kafka plugin: cannot allocate memory for kafka config");
269 return;
270 }
272 #ifdef HAVE_LIBRDKAFKA_LOG_CB
273 rd_kafka_conf_set_log_cb(tctx->kafka_conf, kafka_log);
274 #endif
276 if ((tctx->conf = rd_kafka_topic_conf_new()) == NULL) {
277 rd_kafka_conf_destroy(tctx->kafka_conf);
278 sfree(tctx);
279 ERROR ("write_kafka plugin: cannot create topic configuration.");
280 return;
281 }
283 if (ci->values_num != 1) {
284 WARNING("kafka topic name needed.");
285 goto errout;
286 }
288 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
289 WARNING("kafka topic needs a string argument.");
290 goto errout;
291 }
293 if ((tctx->topic_name = strdup(ci->values[0].value.string)) == NULL) {
294 ERROR("write_kafka plugin: cannot copy topic name.");
295 goto errout;
296 }
298 for (i = 0; i < ci->children_num; i++) {
299 /*
300 * The code here could be simplified but makes room
301 * for easy adding of new options later on.
302 */
303 child = &ci->children[i];
304 status = 0;
306 if (strcasecmp ("Property", child->key) == 0) {
307 if (child->values_num != 2) {
308 WARNING("kafka properties need both a key and a value.");
309 goto errout;
310 }
311 if (child->values[0].type != OCONFIG_TYPE_STRING ||
312 child->values[1].type != OCONFIG_TYPE_STRING) {
313 WARNING("kafka properties needs string arguments.");
314 goto errout;
315 }
316 key = child->values[0].value.string;
317 val = child->values[1].value.string;
318 ret = rd_kafka_topic_conf_set(tctx->conf,key, val,
319 errbuf, sizeof(errbuf));
320 if (ret != RD_KAFKA_CONF_OK) {
321 WARNING("cannot set kafka topic property %s to %s: %s.",
322 key, val, errbuf);
323 goto errout;
324 }
326 } else if (strcasecmp ("Key", child->key) == 0) {
327 cf_util_get_string (child, &tctx->key);
328 assert (tctx->key != NULL);
329 } else if (strcasecmp ("Format", child->key) == 0) {
330 status = cf_util_get_string(child, &key);
331 if (status != 0)
332 goto errout;
334 assert(key != NULL);
336 if (strcasecmp(key, "Command") == 0) {
337 tctx->format = KAFKA_FORMAT_COMMAND;
339 } else if (strcasecmp(key, "Graphite") == 0) {
340 tctx->format = KAFKA_FORMAT_GRAPHITE;
342 } else if (strcasecmp(key, "Json") == 0) {
343 tctx->format = KAFKA_FORMAT_JSON;
345 } else {
346 WARNING ("write_kafka plugin: Invalid format string: %s",
347 key);
348 }
350 sfree(key);
352 } else if (strcasecmp ("StoreRates", child->key) == 0) {
353 status = cf_util_get_boolean (child, &tctx->store_rates);
354 (void) cf_util_get_flag (child, &tctx->graphite_flags,
355 GRAPHITE_STORE_RATES);
357 } else if (strcasecmp ("GraphiteSeparateInstances", child->key) == 0) {
358 status = cf_util_get_flag (child, &tctx->graphite_flags,
359 GRAPHITE_SEPARATE_INSTANCES);
361 } else if (strcasecmp ("GraphiteAlwaysAppendDS", child->key) == 0) {
362 status = cf_util_get_flag (child, &tctx->graphite_flags,
363 GRAPHITE_ALWAYS_APPEND_DS);
365 } else if (strcasecmp ("GraphitePrefix", child->key) == 0) {
366 status = cf_util_get_string (child, &tctx->prefix);
367 } else if (strcasecmp ("GraphitePostfix", child->key) == 0) {
368 status = cf_util_get_string (child, &tctx->postfix);
369 } else if (strcasecmp ("GraphiteEscapeChar", child->key) == 0) {
370 char *tmp_buff = NULL;
371 status = cf_util_get_string (child, &tmp_buff);
372 if (strlen (tmp_buff) > 1)
373 WARNING ("write_kafka plugin: The option \"GraphiteEscapeChar\" handles "
374 "only one character. Others will be ignored.");
375 tctx->escape_char = tmp_buff[0];
376 sfree (tmp_buff);
377 } else {
378 WARNING ("write_kafka plugin: Invalid directive: %s.", child->key);
379 }
381 if (status != 0)
382 break;
383 }
385 rd_kafka_topic_conf_set_partitioner_cb(tctx->conf, kafka_partition);
386 rd_kafka_topic_conf_set_opaque(tctx->conf, tctx);
388 ssnprintf(callback_name, sizeof(callback_name),
389 "write_kafka/%s", tctx->topic_name);
391 ud.data = tctx;
392 ud.free_func = kafka_topic_context_free;
394 status = plugin_register_write (callback_name, kafka_write, &ud);
395 if (status != 0) {
396 WARNING ("write_kafka plugin: plugin_register_write (\"%s\") "
397 "failed with status %i.",
398 callback_name, status);
399 goto errout;
400 }
402 pthread_mutex_init (&tctx->lock, /* attr = */ NULL);
404 return;
405 errout:
406 if (tctx->topic_name != NULL)
407 free(tctx->topic_name);
408 if (tctx->conf != NULL)
409 rd_kafka_topic_conf_destroy(tctx->conf);
410 if (tctx->kafka_conf != NULL)
411 rd_kafka_conf_destroy(tctx->kafka_conf);
412 sfree(tctx);
413 } /* }}} int kafka_config_topic */
415 static int kafka_config(oconfig_item_t *ci) /* {{{ */
416 {
417 int i;
418 oconfig_item_t *child;
419 rd_kafka_conf_t *conf;
420 rd_kafka_conf_res_t ret;
421 char errbuf[1024];
423 if ((conf = rd_kafka_conf_new()) == NULL) {
424 WARNING("cannot allocate kafka configuration.");
425 return -1;
426 }
427 for (i = 0; i < ci->children_num; i++) {
428 child = &ci->children[i];
430 if (strcasecmp("Topic", child->key) == 0) {
431 kafka_config_topic (conf, child);
432 } else if (strcasecmp(child->key, "Property") == 0) {
433 char *key = NULL;
434 char *val = NULL;
436 if (child->values_num != 2) {
437 WARNING("kafka properties need both a key and a value.");
438 goto errout;
439 }
440 if (child->values[0].type != OCONFIG_TYPE_STRING ||
441 child->values[1].type != OCONFIG_TYPE_STRING) {
442 WARNING("kafka properties needs string arguments.");
443 goto errout;
444 }
445 if ((key = strdup(child->values[0].value.string)) == NULL) {
446 WARNING("cannot allocate memory for attribute key.");
447 goto errout;
448 }
449 if ((val = strdup(child->values[1].value.string)) == NULL) {
450 WARNING("cannot allocate memory for attribute value.");
451 sfree(key);
452 goto errout;
453 }
454 ret = rd_kafka_conf_set(conf, key, val, errbuf, sizeof(errbuf));
455 if (ret != RD_KAFKA_CONF_OK) {
456 WARNING("cannot set kafka property %s to %s: %s",
457 key, val, errbuf);
458 sfree(key);
459 sfree(val);
460 goto errout;
461 }
462 sfree(key);
463 sfree(val);
464 } else {
465 WARNING ("write_kafka plugin: Ignoring unknown "
466 "configuration option \"%s\" at top level.",
467 child->key);
468 }
469 }
470 if (conf != NULL)
471 rd_kafka_conf_destroy(conf);
472 return (0);
473 errout:
474 if (conf != NULL)
475 rd_kafka_conf_destroy(conf);
476 return -1;
477 } /* }}} int kafka_config */
479 void module_register(void)
480 {
481 plugin_register_complex_config ("write_kafka", kafka_config);
482 }