2 * collectd - src/write_kafka.c
4 * Copyright (C) 2014 Pierre-Yves Ritschard
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Pierre-Yves Ritschard <pyr at spootnik.org>
25 #include "configfile.h"
26 #include "utils_cache.h"
27 #include "utils_cmd_putval.h"
28 #include "utils_format_graphite.h"
29 #include "utils_format_json.h"
30 #include "utils_crc32.h"
32 #include <sys/types.h>
33 #include <librdkafka/rdkafka.h>
37 struct kafka_topic_context {
38 #define KAFKA_FORMAT_COMMAND 1
39 #define KAFKA_FORMAT_GRAPHITE 2
40 #define KAFKA_FORMAT_JSON 3
42 unsigned int graphite_flags;
44 rd_kafka_topic_conf_t *conf;
45 rd_kafka_topic_t *topic;
55 static int kafka_write(const data_set_t *, const value_list_t *, user_data_t *);
56 static int32_t kafka_partition(const rd_kafka_topic_t *, const void *, size_t,
57 int32_t, void *, void *);
59 static int32_t kafka_partition(const rd_kafka_topic_t *rkt,
60 const void *keydata, size_t keylen,
61 int32_t partition_cnt, void *p, void *m)
63 u_int32_t key = *((u_int32_t *)keydata );
65 return key % partition_cnt;
68 static int kafka_write(const data_set_t *ds, /* {{{ */
69 const value_list_t *vl,
75 size_t bfree = sizeof(buffer);
78 struct kafka_topic_context *ctx = ud->data;
80 if ((ds == NULL) || (vl == NULL) || (ctx == NULL))
83 bzero(buffer, sizeof(buffer));
85 switch (ctx->format) {
86 case KAFKA_FORMAT_COMMAND:
87 status = create_putval(buffer, sizeof(buffer), ds, vl);
89 ERROR("write_kafka plugin: create_putval failed with status %i.",
93 blen = strlen(buffer);
95 case KAFKA_FORMAT_JSON:
97 format_json_initialize(buffer, &bfill, &bfree);
98 format_json_value_list(buffer, &bfill, &bfree, ds, vl,
100 format_json_finalize(buffer, &bfill, &bfree);
101 blen = strlen(buffer);
103 case KAFKA_FORMAT_GRAPHITE:
104 status = format_graphite(buffer, sizeof(buffer), ds, vl,
105 ctx->prefix, ctx->postfix, ctx->escape_char,
106 ctx->graphite_flags);
108 ERROR("write_kafka plugin: format_graphite failed with status %i.",
112 blen = strlen(buffer);
115 ERROR("write_kafka plugin: invalid format %i.", ctx->format);
120 * We partition our stream by metric name
127 rd_kafka_produce(ctx->topic, RD_KAFKA_PARTITION_UA,
128 RD_KAFKA_MSG_F_COPY, buffer, blen,
129 &key, sizeof(key), NULL);
132 } /* }}} int kafka_write */
134 static void kafka_topic_context_free(void *p) /* {{{ */
136 struct kafka_topic_context *ctx = p;
141 if (ctx->topic_name != NULL)
142 sfree(ctx->topic_name);
143 if (ctx->topic != NULL)
144 rd_kafka_topic_destroy(ctx->topic);
145 if (ctx->conf != NULL)
146 rd_kafka_topic_conf_destroy(ctx->conf);
149 } /* }}} void kafka_topic_context_free */
151 static void kafka_config_topic(rd_kafka_conf_t *conf, oconfig_item_t *ci) /* {{{ */
155 struct kafka_topic_context *tctx;
158 char callback_name[DATA_MAX_NAME_LEN];
161 oconfig_item_t *child;
162 rd_kafka_conf_res_t ret;
164 if ((tctx = calloc(1, sizeof (*tctx))) == NULL) {
165 ERROR ("write_kafka plugin: calloc failed.");
169 tctx->escape_char = '.';
170 tctx->store_rates = 1;
172 if ((tctx->kafka = rd_kafka_new(RD_KAFKA_PRODUCER, conf,
173 errbuf, sizeof(errbuf))) == NULL) {
175 ERROR("write_kafka plugin: cannot create kafka handle.");
180 if ((tctx->conf = rd_kafka_topic_conf_new()) == NULL) {
181 rd_kafka_destroy(tctx->kafka);
183 ERROR ("write_kafka plugin: cannot create topic configuration.");
187 if (ci->values_num != 1) {
188 WARNING("kafka topic name needed.");
192 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
193 WARNING("kafka topic needs a string argument.");
197 if ((tctx->topic_name = strdup(ci->values[0].value.string)) == NULL) {
198 ERROR("write_kafka plugin: cannot copy topic name.");
202 for (i = 0; i < ci->children_num; i++) {
204 * The code here could be simplified but makes room
205 * for easy adding of new options later on.
207 child = &ci->children[i];
210 if (strcasecmp ("Property", child->key) == 0) {
211 if (child->values_num != 2) {
212 WARNING("kafka properties need both a key and a value.");
215 if (child->values[0].type != OCONFIG_TYPE_STRING ||
216 child->values[1].type != OCONFIG_TYPE_STRING) {
217 WARNING("kafka properties needs string arguments.");
220 key = child->values[0].value.string;
221 val = child->values[0].value.string;
222 ret = rd_kafka_topic_conf_set(tctx->conf,key, val,
223 errbuf, sizeof(errbuf));
224 if (ret != RD_KAFKA_CONF_OK) {
225 WARNING("cannot set kafka topic property %s to %s: %s.",
230 } else if (strcasecmp ("Key", child->key) == 0) {
231 char *tmp_buf = NULL;
232 status = cf_util_get_string(child, &tmp_buf);
234 WARNING("write_kafka plugin: invalid key supplied");
238 if (strcasecmp(tmp_buf, "Random") != 0) {
240 tctx->key = crc32_buffer((u_char *)tmp_buf, strlen(tmp_buf));
244 } else if (strcasecmp ("Format", child->key) == 0) {
245 status = cf_util_get_string(child, &key);
251 if (strcasecmp(key, "Command") == 0) {
253 tctx->format = KAFKA_FORMAT_COMMAND;
255 } else if (strcasecmp(key, "Graphite") == 0) {
256 tctx->format = KAFKA_FORMAT_GRAPHITE;
258 } else if (strcasecmp(key, "Json") == 0) {
259 tctx->format = KAFKA_FORMAT_JSON;
262 WARNING ("write_kafka plugin: Invalid format string: %s",
267 } else if (strcasecmp ("StoreRates", child->key) == 0) {
268 status = cf_util_get_boolean (child, &tctx->store_rates);
269 (void) cf_util_get_flag (child, &tctx->graphite_flags,
270 GRAPHITE_STORE_RATES);
272 } else if (strcasecmp ("GraphiteSeparateInstances", child->key) == 0) {
273 status = cf_util_get_flag (child, &tctx->graphite_flags,
274 GRAPHITE_SEPARATE_INSTANCES);
276 } else if (strcasecmp ("GraphiteAlwaysAppendDS", child->key) == 0) {
277 status = cf_util_get_flag (child, &tctx->graphite_flags,
278 GRAPHITE_ALWAYS_APPEND_DS);
280 } else if (strcasecmp ("GraphitePrefix", child->key) == 0) {
281 status = cf_util_get_string (child, &tctx->prefix);
282 } else if (strcasecmp ("GraphitePostfix", child->key) == 0) {
283 status = cf_util_get_string (child, &tctx->postfix);
284 } else if (strcasecmp ("GraphiteEscapeChar", child->key) == 0) {
285 char *tmp_buff = NULL;
286 status = cf_util_get_string (child, &tmp_buff);
287 if (strlen (tmp_buff) > 1)
288 WARNING ("write_kafka plugin: The option \"GraphiteEscapeChar\" handles "
289 "only one character. Others will be ignored.");
290 tctx->escape_char = tmp_buff[0];
293 WARNING ("write_kafka plugin: Invalid directive: %s.", child->key);
300 rd_kafka_topic_conf_set_partitioner_cb(tctx->conf, kafka_partition);
301 rd_kafka_topic_conf_set_opaque(tctx->conf, tctx);
303 if ((tctx->topic = rd_kafka_topic_new(tctx->kafka, tctx->topic_name,
304 tctx->conf)) == NULL) {
305 ERROR("write_kafka plugin: cannot create topic.");
310 ssnprintf(callback_name, sizeof(callback_name),
311 "write_kafka/%s", tctx->topic_name);
314 ud.free_func = kafka_topic_context_free;
316 status = plugin_register_write (callback_name, kafka_write, &ud);
318 WARNING ("write_kafka plugin: plugin_register_write (\"%s\") "
319 "failed with status %i.",
320 callback_name, status);
326 rd_kafka_conf_destroy(conf);
327 if (tctx->kafka != NULL)
328 rd_kafka_destroy(tctx->kafka);
329 if (tctx->topic != NULL)
330 rd_kafka_topic_destroy(tctx->topic);
331 if (tctx->topic_name != NULL)
332 free(tctx->topic_name);
333 if (tctx->conf != NULL)
334 rd_kafka_topic_conf_destroy(tctx->conf);
336 } /* }}} int kafka_config_topic */
338 static int kafka_config(oconfig_item_t *ci) /* {{{ */
341 oconfig_item_t *child;
342 rd_kafka_conf_t *conf;
343 rd_kafka_conf_t *cloned;
344 rd_kafka_conf_res_t ret;
347 if ((conf = rd_kafka_conf_new()) == NULL) {
348 WARNING("cannot allocate kafka configuration.");
352 for (i = 0; i < ci->children_num; i++) {
353 child = &ci->children[i];
355 if (strcasecmp("Topic", child->key) == 0) {
356 if ((cloned = rd_kafka_conf_dup(conf)) == NULL) {
357 WARNING("write_kafka plugin: cannot allocate memory for kafka config");
360 kafka_config_topic (cloned, child);
361 } else if (strcasecmp(child->key, "Property") == 0) {
365 if (child->values_num != 2) {
366 WARNING("kafka properties need both a key and a value.");
369 if (child->values[0].type != OCONFIG_TYPE_STRING ||
370 child->values[1].type != OCONFIG_TYPE_STRING) {
371 WARNING("kafka properties needs string arguments.");
374 if ((key = strdup(child->values[0].value.string)) == NULL) {
375 WARNING("cannot allocate memory for attribute key.");
378 if ((val = strdup(child->values[1].value.string)) == NULL) {
379 WARNING("cannot allocate memory for attribute value.");
382 ret = rd_kafka_conf_set(conf, key, val, errbuf, sizeof(errbuf));
383 if (ret != RD_KAFKA_CONF_OK) {
384 WARNING("cannot set kafka property %s to %s: %s",
391 WARNING ("write_kafka plugin: Ignoring unknown "
392 "configuration option \"%s\" at top level.",
397 rd_kafka_conf_destroy(conf);
401 rd_kafka_conf_destroy(conf);
403 } /* }}} int kafka_config */
405 void module_register(void)
407 plugin_register_complex_config ("write_kafka", kafka_config);
410 /* vim: set sw=8 sts=8 ts=8 noet : */