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_cmd_putval.h"
32 #include "utils_format_graphite.h"
33 #include "utils_format_json.h"
36 #include <librdkafka/rdkafka.h>
39 struct kafka_topic_context {
40 #define KAFKA_FORMAT_JSON 0
41 #define KAFKA_FORMAT_COMMAND 1
42 #define KAFKA_FORMAT_GRAPHITE 2
44 unsigned int graphite_flags;
46 rd_kafka_topic_conf_t *conf;
47 rd_kafka_topic_t *topic;
48 rd_kafka_conf_t *kafka_conf;
58 static int kafka_handle(struct kafka_topic_context *);
59 static int kafka_write(const data_set_t *, const value_list_t *, user_data_t *);
60 static int32_t kafka_partition(const rd_kafka_topic_t *, const void *, size_t,
61 int32_t, void *, void *);
63 #if defined HAVE_LIBRDKAFKA_LOGGER || defined HAVE_LIBRDKAFKA_LOG_CB
64 static void kafka_log(const rd_kafka_t *, int, const char *, const char *);
66 static void kafka_log(const rd_kafka_t *rkt, int level,
67 const char *fac, const char *msg)
69 plugin_log(level, "%s", msg);
73 static uint32_t kafka_hash(const char *keydata, size_t keylen)
76 for (; keylen > 0; keylen--)
77 hash = ((hash << 5) + hash) + keydata[keylen - 1];
81 static int32_t kafka_partition(const rd_kafka_topic_t *rkt,
82 const void *keydata, size_t keylen,
83 int32_t partition_cnt, void *p, void *m)
85 uint32_t key = kafka_hash(keydata, keylen);
86 uint32_t target = key % partition_cnt;
87 int32_t i = partition_cnt;
89 while (--i > 0 && !rd_kafka_topic_partition_available(rkt, target)) {
90 target = (target + 1) % partition_cnt;
95 static int kafka_handle(struct kafka_topic_context *ctx) /* {{{ */
98 rd_kafka_conf_t *conf;
99 rd_kafka_topic_conf_t *topic_conf;
101 if (ctx->kafka != NULL && ctx->topic != NULL)
104 if (ctx->kafka == NULL) {
105 if ((conf = rd_kafka_conf_dup(ctx->kafka_conf)) == NULL) {
106 ERROR("write_kafka plugin: cannot duplicate kafka config");
110 if ((ctx->kafka = rd_kafka_new(RD_KAFKA_PRODUCER, conf,
111 errbuf, sizeof(errbuf))) == NULL) {
112 ERROR("write_kafka plugin: cannot create kafka handle.");
116 rd_kafka_conf_destroy(ctx->kafka_conf);
117 ctx->kafka_conf = NULL;
119 INFO ("write_kafka plugin: created KAFKA handle : %s", rd_kafka_name(ctx->kafka));
121 #if defined(HAVE_LIBRDKAFKA_LOGGER) && !defined(HAVE_LIBRDKAFKA_LOG_CB)
122 rd_kafka_set_logger(ctx->kafka, kafka_log);
126 if (ctx->topic == NULL ) {
127 if ((topic_conf = rd_kafka_topic_conf_dup(ctx->conf)) == NULL) {
128 ERROR("write_kafka plugin: cannot duplicate kafka topic config");
132 if ((ctx->topic = rd_kafka_topic_new(ctx->kafka, ctx->topic_name,
133 topic_conf)) == NULL) {
134 ERROR("write_kafka plugin: cannot create topic : %s\n",
135 rd_kafka_err2str(rd_kafka_errno2err(errno)));
139 rd_kafka_topic_conf_destroy(ctx->conf);
142 INFO ("write_kafka plugin: handle created for topic : %s", rd_kafka_topic_name(ctx->topic));
147 } /* }}} int kafka_handle */
149 static int kafka_write(const data_set_t *ds, /* {{{ */
150 const value_list_t *vl,
157 size_t bfree = sizeof(buffer);
160 struct kafka_topic_context *ctx = ud->data;
162 if ((ds == NULL) || (vl == NULL) || (ctx == NULL))
165 pthread_mutex_lock (&ctx->lock);
166 status = kafka_handle(ctx);
167 pthread_mutex_unlock (&ctx->lock);
171 bzero(buffer, sizeof(buffer));
173 switch (ctx->format) {
174 case KAFKA_FORMAT_COMMAND:
175 status = create_putval(buffer, sizeof(buffer), ds, vl);
177 ERROR("write_kafka plugin: create_putval failed with status %i.",
181 blen = strlen(buffer);
183 case KAFKA_FORMAT_JSON:
184 format_json_initialize(buffer, &bfill, &bfree);
185 format_json_value_list(buffer, &bfill, &bfree, ds, vl,
187 format_json_finalize(buffer, &bfill, &bfree);
188 blen = strlen(buffer);
190 case KAFKA_FORMAT_GRAPHITE:
191 status = format_graphite(buffer, sizeof(buffer), ds, vl,
192 ctx->prefix, ctx->postfix, ctx->escape_char,
193 ctx->graphite_flags);
195 ERROR("write_kafka plugin: format_graphite failed with status %i.",
199 blen = strlen(buffer);
202 ERROR("write_kafka plugin: invalid format %i.", ctx->format);
208 keylen = strlen (key);
212 rd_kafka_produce(ctx->topic, RD_KAFKA_PARTITION_UA,
213 RD_KAFKA_MSG_F_COPY, buffer, blen,
217 } /* }}} int kafka_write */
219 static void kafka_topic_context_free(void *p) /* {{{ */
221 struct kafka_topic_context *ctx = p;
226 if (ctx->topic_name != NULL)
227 sfree(ctx->topic_name);
228 if (ctx->topic != NULL)
229 rd_kafka_topic_destroy(ctx->topic);
230 if (ctx->conf != NULL)
231 rd_kafka_topic_conf_destroy(ctx->conf);
232 if (ctx->kafka_conf != NULL)
233 rd_kafka_conf_destroy(ctx->kafka_conf);
234 if (ctx->kafka != NULL)
235 rd_kafka_destroy(ctx->kafka);
238 } /* }}} void kafka_topic_context_free */
240 static void kafka_config_topic(rd_kafka_conf_t *conf, oconfig_item_t *ci) /* {{{ */
244 struct kafka_topic_context *tctx;
247 char callback_name[DATA_MAX_NAME_LEN];
250 oconfig_item_t *child;
251 rd_kafka_conf_res_t ret;
253 if ((tctx = calloc(1, sizeof (*tctx))) == NULL) {
254 ERROR ("write_kafka plugin: calloc failed.");
258 tctx->escape_char = '.';
259 tctx->store_rates = 1;
260 tctx->format = KAFKA_FORMAT_JSON;
263 if ((tctx->kafka_conf = rd_kafka_conf_dup(conf)) == NULL) {
265 ERROR("write_kafka plugin: cannot allocate memory for kafka config");
269 #ifdef HAVE_LIBRDKAFKA_LOG_CB
270 rd_kafka_conf_set_log_cb(tctx->kafka_conf, kafka_log);
273 if ((tctx->conf = rd_kafka_topic_conf_new()) == NULL) {
274 rd_kafka_conf_destroy(tctx->kafka_conf);
276 ERROR ("write_kafka plugin: cannot create topic configuration.");
280 if (ci->values_num != 1) {
281 WARNING("kafka topic name needed.");
285 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
286 WARNING("kafka topic needs a string argument.");
290 if ((tctx->topic_name = strdup(ci->values[0].value.string)) == NULL) {
291 ERROR("write_kafka plugin: cannot copy topic name.");
295 for (i = 0; i < ci->children_num; i++) {
297 * The code here could be simplified but makes room
298 * for easy adding of new options later on.
300 child = &ci->children[i];
303 if (strcasecmp ("Property", child->key) == 0) {
304 if (child->values_num != 2) {
305 WARNING("kafka properties need both a key and a value.");
308 if (child->values[0].type != OCONFIG_TYPE_STRING ||
309 child->values[1].type != OCONFIG_TYPE_STRING) {
310 WARNING("kafka properties needs string arguments.");
313 key = child->values[0].value.string;
314 val = child->values[1].value.string;
315 ret = rd_kafka_topic_conf_set(tctx->conf,key, val,
316 errbuf, sizeof(errbuf));
317 if (ret != RD_KAFKA_CONF_OK) {
318 WARNING("cannot set kafka topic property %s to %s: %s.",
323 } else if (strcasecmp ("Key", child->key) == 0) {
324 cf_util_get_string (child, &tctx->key);
325 assert (tctx->key != NULL);
326 } else if (strcasecmp ("Format", child->key) == 0) {
327 status = cf_util_get_string(child, &key);
333 if (strcasecmp(key, "Command") == 0) {
334 tctx->format = KAFKA_FORMAT_COMMAND;
336 } else if (strcasecmp(key, "Graphite") == 0) {
337 tctx->format = KAFKA_FORMAT_GRAPHITE;
339 } else if (strcasecmp(key, "Json") == 0) {
340 tctx->format = KAFKA_FORMAT_JSON;
343 WARNING ("write_kafka plugin: Invalid format string: %s",
349 } else if (strcasecmp ("StoreRates", child->key) == 0) {
350 status = cf_util_get_boolean (child, &tctx->store_rates);
351 (void) cf_util_get_flag (child, &tctx->graphite_flags,
352 GRAPHITE_STORE_RATES);
354 } else if (strcasecmp ("GraphiteSeparateInstances", child->key) == 0) {
355 status = cf_util_get_flag (child, &tctx->graphite_flags,
356 GRAPHITE_SEPARATE_INSTANCES);
358 } else if (strcasecmp ("GraphiteAlwaysAppendDS", child->key) == 0) {
359 status = cf_util_get_flag (child, &tctx->graphite_flags,
360 GRAPHITE_ALWAYS_APPEND_DS);
362 } else if (strcasecmp ("GraphitePrefix", child->key) == 0) {
363 status = cf_util_get_string (child, &tctx->prefix);
364 } else if (strcasecmp ("GraphitePostfix", child->key) == 0) {
365 status = cf_util_get_string (child, &tctx->postfix);
366 } else if (strcasecmp ("GraphiteEscapeChar", child->key) == 0) {
367 char *tmp_buff = NULL;
368 status = cf_util_get_string (child, &tmp_buff);
369 if (strlen (tmp_buff) > 1)
370 WARNING ("write_kafka plugin: The option \"GraphiteEscapeChar\" handles "
371 "only one character. Others will be ignored.");
372 tctx->escape_char = tmp_buff[0];
375 WARNING ("write_kafka plugin: Invalid directive: %s.", child->key);
382 rd_kafka_topic_conf_set_partitioner_cb(tctx->conf, kafka_partition);
383 rd_kafka_topic_conf_set_opaque(tctx->conf, tctx);
385 ssnprintf(callback_name, sizeof(callback_name),
386 "write_kafka/%s", tctx->topic_name);
389 ud.free_func = kafka_topic_context_free;
391 status = plugin_register_write (callback_name, kafka_write, &ud);
393 WARNING ("write_kafka plugin: plugin_register_write (\"%s\") "
394 "failed with status %i.",
395 callback_name, status);
399 pthread_mutex_init (&tctx->lock, /* attr = */ NULL);
403 if (tctx->topic_name != NULL)
404 free(tctx->topic_name);
405 if (tctx->conf != NULL)
406 rd_kafka_topic_conf_destroy(tctx->conf);
407 if (tctx->kafka_conf != NULL)
408 rd_kafka_conf_destroy(tctx->kafka_conf);
410 } /* }}} int kafka_config_topic */
412 static int kafka_config(oconfig_item_t *ci) /* {{{ */
415 oconfig_item_t *child;
416 rd_kafka_conf_t *conf;
417 rd_kafka_conf_res_t ret;
420 if ((conf = rd_kafka_conf_new()) == NULL) {
421 WARNING("cannot allocate kafka configuration.");
424 for (i = 0; i < ci->children_num; i++) {
425 child = &ci->children[i];
427 if (strcasecmp("Topic", child->key) == 0) {
428 kafka_config_topic (conf, child);
429 } else if (strcasecmp(child->key, "Property") == 0) {
433 if (child->values_num != 2) {
434 WARNING("kafka properties need both a key and a value.");
437 if (child->values[0].type != OCONFIG_TYPE_STRING ||
438 child->values[1].type != OCONFIG_TYPE_STRING) {
439 WARNING("kafka properties needs string arguments.");
442 if ((key = strdup(child->values[0].value.string)) == NULL) {
443 WARNING("cannot allocate memory for attribute key.");
446 if ((val = strdup(child->values[1].value.string)) == NULL) {
447 WARNING("cannot allocate memory for attribute value.");
451 ret = rd_kafka_conf_set(conf, key, val, errbuf, sizeof(errbuf));
452 if (ret != RD_KAFKA_CONF_OK) {
453 WARNING("cannot set kafka property %s to %s: %s",
462 WARNING ("write_kafka plugin: Ignoring unknown "
463 "configuration option \"%s\" at top level.",
468 rd_kafka_conf_destroy(conf);
472 rd_kafka_conf_destroy(conf);
474 } /* }}} int kafka_config */
476 void module_register(void)
478 plugin_register_complex_config ("write_kafka", kafka_config);