2 * collectd - src/write_kafka.c
3 * Copyright (C) 2014 Pierre-Yves Ritschard
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 * Pierre-Yves Ritschard <pyr at spootnik.org>
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"
38 #include <librdkafka/rdkafka.h>
43 struct kafka_topic_context {
44 #define KAFKA_FORMAT_JSON 0
45 #define KAFKA_FORMAT_COMMAND 1
46 #define KAFKA_FORMAT_GRAPHITE 2
48 unsigned int graphite_flags;
50 rd_kafka_topic_conf_t *conf;
51 rd_kafka_topic_t *topic;
52 rd_kafka_conf_t *kafka_conf;
62 static int kafka_handle(struct kafka_topic_context *);
63 static int kafka_write(const data_set_t *, const value_list_t *, user_data_t *);
64 static int32_t kafka_partition(const rd_kafka_topic_t *, const void *, size_t,
65 int32_t, void *, void *);
67 #if defined HAVE_LIBRDKAFKA_LOGGER || defined HAVE_LIBRDKAFKA_LOG_CB
68 static void kafka_log(const rd_kafka_t *, int, const char *, const char *);
70 static void kafka_log(const rd_kafka_t *rkt, int level,
71 const char *fac, const char *msg)
73 plugin_log(level, "%s", msg);
77 static int32_t kafka_partition(const rd_kafka_topic_t *rkt,
78 const void *keydata, size_t keylen,
79 int32_t partition_cnt, void *p, void *m)
81 uint32_t key = *((uint32_t *)keydata );
82 uint32_t target = key % partition_cnt;
83 int32_t i = partition_cnt;
85 while (--i > 0 && !rd_kafka_topic_partition_available(rkt, target)) {
86 target = (target + 1) % partition_cnt;
91 static int kafka_handle(struct kafka_topic_context *ctx) /* {{{ */
94 rd_kafka_conf_t *conf;
95 rd_kafka_topic_conf_t *topic_conf;
97 if (ctx->kafka != NULL && ctx->topic != NULL)
100 if (ctx->kafka == NULL) {
101 if ((conf = rd_kafka_conf_dup(ctx->kafka_conf)) == NULL) {
102 ERROR("write_kafka plugin: cannot duplicate kafka config");
106 if ((ctx->kafka = rd_kafka_new(RD_KAFKA_PRODUCER, conf,
107 errbuf, sizeof(errbuf))) == NULL) {
108 ERROR("write_kafka plugin: cannot create kafka handle.");
112 rd_kafka_conf_destroy(ctx->kafka_conf);
113 ctx->kafka_conf = NULL;
115 INFO ("write_kafka plugin: created KAFKA handle : %s", rd_kafka_name(ctx->kafka));
117 #if defined(HAVE_LIBRDKAFKA_LOGGER) && !defined(HAVE_LIBRDKAFKA_LOG_CB)
118 rd_kafka_set_logger(ctx->kafka, kafka_log);
122 if (ctx->topic == NULL ) {
123 if ((topic_conf = rd_kafka_topic_conf_dup(ctx->conf)) == NULL) {
124 ERROR("write_kafka plugin: cannot duplicate kafka topic config");
128 if ((ctx->topic = rd_kafka_topic_new(ctx->kafka, ctx->topic_name,
129 topic_conf)) == NULL) {
130 ERROR("write_kafka plugin: cannot create topic : %s\n",
131 rd_kafka_err2str(rd_kafka_errno2err(errno)));
135 rd_kafka_topic_conf_destroy(ctx->conf);
138 INFO ("write_kafka plugin: handle created for topic : %s", rd_kafka_topic_name(ctx->topic));
143 } /* }}} int kafka_handle */
145 static int kafka_write(const data_set_t *ds, /* {{{ */
146 const value_list_t *vl,
153 size_t bfree = sizeof(buffer);
156 struct kafka_topic_context *ctx = ud->data;
158 if ((ds == NULL) || (vl == NULL) || (ctx == NULL))
161 pthread_mutex_lock (&ctx->lock);
162 status = kafka_handle(ctx);
163 pthread_mutex_unlock (&ctx->lock);
167 bzero(buffer, sizeof(buffer));
169 switch (ctx->format) {
170 case KAFKA_FORMAT_COMMAND:
171 status = create_putval(buffer, sizeof(buffer), ds, vl);
173 ERROR("write_kafka plugin: create_putval failed with status %i.",
177 blen = strlen(buffer);
179 case KAFKA_FORMAT_JSON:
180 format_json_initialize(buffer, &bfill, &bfree);
181 format_json_value_list(buffer, &bfill, &bfree, ds, vl,
183 format_json_finalize(buffer, &bfill, &bfree);
184 blen = strlen(buffer);
186 case KAFKA_FORMAT_GRAPHITE:
187 status = format_graphite(buffer, sizeof(buffer), ds, vl,
188 ctx->prefix, ctx->postfix, ctx->escape_char,
189 ctx->graphite_flags);
191 ERROR("write_kafka plugin: format_graphite failed with status %i.",
195 blen = strlen(buffer);
198 ERROR("write_kafka plugin: invalid format %i.", ctx->format);
204 keylen = strlen (key);
208 rd_kafka_produce(ctx->topic, RD_KAFKA_PARTITION_UA,
209 RD_KAFKA_MSG_F_COPY, buffer, blen,
213 } /* }}} int kafka_write */
215 static void kafka_topic_context_free(void *p) /* {{{ */
217 struct kafka_topic_context *ctx = p;
222 if (ctx->topic_name != NULL)
223 sfree(ctx->topic_name);
224 if (ctx->topic != NULL)
225 rd_kafka_topic_destroy(ctx->topic);
226 if (ctx->conf != NULL)
227 rd_kafka_topic_conf_destroy(ctx->conf);
228 if (ctx->kafka_conf != NULL)
229 rd_kafka_conf_destroy(ctx->kafka_conf);
230 if (ctx->kafka != NULL)
231 rd_kafka_destroy(ctx->kafka);
234 } /* }}} void kafka_topic_context_free */
236 static void kafka_config_topic(rd_kafka_conf_t *conf, oconfig_item_t *ci) /* {{{ */
240 struct kafka_topic_context *tctx;
243 char callback_name[DATA_MAX_NAME_LEN];
246 oconfig_item_t *child;
247 rd_kafka_conf_res_t ret;
249 if ((tctx = calloc(1, sizeof (*tctx))) == NULL) {
250 ERROR ("write_kafka plugin: calloc failed.");
254 tctx->escape_char = '.';
255 tctx->store_rates = 1;
256 tctx->format = KAFKA_FORMAT_JSON;
259 if ((tctx->kafka_conf = rd_kafka_conf_dup(conf)) == NULL) {
261 ERROR("write_kafka plugin: cannot allocate memory for kafka config");
265 #ifdef HAVE_LIBRDKAFKA_LOG_CB
266 rd_kafka_conf_set_log_cb(tctx->kafka_conf, kafka_log);
269 if ((tctx->conf = rd_kafka_topic_conf_new()) == NULL) {
270 rd_kafka_conf_destroy(tctx->kafka_conf);
272 ERROR ("write_kafka plugin: cannot create topic configuration.");
276 if (ci->values_num != 1) {
277 WARNING("kafka topic name needed.");
281 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
282 WARNING("kafka topic needs a string argument.");
286 if ((tctx->topic_name = strdup(ci->values[0].value.string)) == NULL) {
287 ERROR("write_kafka plugin: cannot copy topic name.");
291 for (i = 0; i < ci->children_num; i++) {
293 * The code here could be simplified but makes room
294 * for easy adding of new options later on.
296 child = &ci->children[i];
299 if (strcasecmp ("Property", child->key) == 0) {
300 if (child->values_num != 2) {
301 WARNING("kafka properties need both a key and a value.");
304 if (child->values[0].type != OCONFIG_TYPE_STRING ||
305 child->values[1].type != OCONFIG_TYPE_STRING) {
306 WARNING("kafka properties needs string arguments.");
309 key = child->values[0].value.string;
310 val = child->values[1].value.string;
311 ret = rd_kafka_topic_conf_set(tctx->conf,key, val,
312 errbuf, sizeof(errbuf));
313 if (ret != RD_KAFKA_CONF_OK) {
314 WARNING("cannot set kafka topic property %s to %s: %s.",
319 } else if (strcasecmp ("Key", child->key) == 0) {
320 cf_util_get_string (child, &tctx->key);
321 assert (tctx->key != NULL);
322 } else if (strcasecmp ("Format", child->key) == 0) {
323 status = cf_util_get_string(child, &key);
329 if (strcasecmp(key, "Command") == 0) {
330 tctx->format = KAFKA_FORMAT_COMMAND;
332 } else if (strcasecmp(key, "Graphite") == 0) {
333 tctx->format = KAFKA_FORMAT_GRAPHITE;
335 } else if (strcasecmp(key, "Json") == 0) {
336 tctx->format = KAFKA_FORMAT_JSON;
339 WARNING ("write_kafka plugin: Invalid format string: %s",
345 } else if (strcasecmp ("StoreRates", child->key) == 0) {
346 status = cf_util_get_boolean (child, &tctx->store_rates);
347 (void) cf_util_get_flag (child, &tctx->graphite_flags,
348 GRAPHITE_STORE_RATES);
350 } else if (strcasecmp ("GraphiteSeparateInstances", child->key) == 0) {
351 status = cf_util_get_flag (child, &tctx->graphite_flags,
352 GRAPHITE_SEPARATE_INSTANCES);
354 } else if (strcasecmp ("GraphiteAlwaysAppendDS", child->key) == 0) {
355 status = cf_util_get_flag (child, &tctx->graphite_flags,
356 GRAPHITE_ALWAYS_APPEND_DS);
358 } else if (strcasecmp ("GraphitePrefix", child->key) == 0) {
359 status = cf_util_get_string (child, &tctx->prefix);
360 } else if (strcasecmp ("GraphitePostfix", child->key) == 0) {
361 status = cf_util_get_string (child, &tctx->postfix);
362 } else if (strcasecmp ("GraphiteEscapeChar", child->key) == 0) {
363 char *tmp_buff = NULL;
364 status = cf_util_get_string (child, &tmp_buff);
365 if (strlen (tmp_buff) > 1)
366 WARNING ("write_kafka plugin: The option \"GraphiteEscapeChar\" handles "
367 "only one character. Others will be ignored.");
368 tctx->escape_char = tmp_buff[0];
371 WARNING ("write_kafka plugin: Invalid directive: %s.", child->key);
378 rd_kafka_topic_conf_set_partitioner_cb(tctx->conf, kafka_partition);
379 rd_kafka_topic_conf_set_opaque(tctx->conf, tctx);
381 ssnprintf(callback_name, sizeof(callback_name),
382 "write_kafka/%s", tctx->topic_name);
385 ud.free_func = kafka_topic_context_free;
387 status = plugin_register_write (callback_name, kafka_write, &ud);
389 WARNING ("write_kafka plugin: plugin_register_write (\"%s\") "
390 "failed with status %i.",
391 callback_name, status);
395 pthread_mutex_init (&tctx->lock, /* attr = */ NULL);
399 if (tctx->topic_name != NULL)
400 free(tctx->topic_name);
401 if (tctx->conf != NULL)
402 rd_kafka_topic_conf_destroy(tctx->conf);
403 if (tctx->kafka_conf != NULL)
404 rd_kafka_conf_destroy(tctx->kafka_conf);
406 } /* }}} int kafka_config_topic */
408 static int kafka_config(oconfig_item_t *ci) /* {{{ */
411 oconfig_item_t *child;
412 rd_kafka_conf_t *conf;
413 rd_kafka_conf_res_t ret;
416 if ((conf = rd_kafka_conf_new()) == NULL) {
417 WARNING("cannot allocate kafka configuration.");
420 for (i = 0; i < ci->children_num; i++) {
421 child = &ci->children[i];
423 if (strcasecmp("Topic", child->key) == 0) {
424 kafka_config_topic (conf, child);
425 } else if (strcasecmp(child->key, "Property") == 0) {
429 if (child->values_num != 2) {
430 WARNING("kafka properties need both a key and a value.");
433 if (child->values[0].type != OCONFIG_TYPE_STRING ||
434 child->values[1].type != OCONFIG_TYPE_STRING) {
435 WARNING("kafka properties needs string arguments.");
438 if ((key = strdup(child->values[0].value.string)) == NULL) {
439 WARNING("cannot allocate memory for attribute key.");
442 if ((val = strdup(child->values[1].value.string)) == NULL) {
443 WARNING("cannot allocate memory for attribute value.");
447 ret = rd_kafka_conf_set(conf, key, val, errbuf, sizeof(errbuf));
448 if (ret != RD_KAFKA_CONF_OK) {
449 WARNING("cannot set kafka property %s to %s: %s",
458 WARNING ("write_kafka plugin: Ignoring unknown "
459 "configuration option \"%s\" at top level.",
464 rd_kafka_conf_destroy(conf);
468 rd_kafka_conf_destroy(conf);
470 } /* }}} int kafka_config */
472 void module_register(void)
474 plugin_register_complex_config ("write_kafka", kafka_config);