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"
31 #include "riemann.pb-c.h"
33 #include <sys/types.h>
34 #include <librdkafka/rdkafka.h>
38 struct kafka_topic_context {
39 #define KAFKA_FORMAT_COMMAND 1
40 #define KAFKA_FORMAT_GRAPHITE 2
41 #define KAFKA_FORMAT_JSON 3
43 unsigned int graphite_flags;
45 rd_kafka_topic_conf_t *conf;
46 rd_kafka_topic_t *topic;
56 static int kafka_write(const data_set_t *, const value_list_t *, user_data_t *);
57 static int32_t kafka_partition(const rd_kafka_topic_t *, const void *, size_t,
58 int32_t, void *, void *);
60 static int32_t kafka_partition(const rd_kafka_topic_t *rkt,
61 const void *keydata, size_t keylen,
62 int32_t partition_cnt, void *p, void *m)
64 u_int32_t key = *((u_int32_t *)keydata );
66 return key % partition_cnt;
69 static int kafka_write(const data_set_t *ds, /* {{{ */
70 const value_list_t *vl,
76 size_t bfree = sizeof(buffer);
79 struct kafka_topic_context *ctx = ud->data;
81 if ((ds == NULL) || (vl == NULL) || (ctx == NULL))
84 bzero(buffer, sizeof(buffer));
86 switch (ctx->format) {
87 case KAFKA_FORMAT_COMMAND:
88 status = create_putval(buffer, sizeof(buffer), ds, vl);
90 ERROR("write_kafka plugin: create_putval failed with status %i.",
94 blen = strlen(buffer);
96 case KAFKA_FORMAT_JSON:
98 format_json_initialize(buffer, &bfill, &bfree);
99 format_json_value_list(buffer, &bfill, &bfree, ds, vl,
101 format_json_finalize(buffer, &bfill, &bfree);
102 blen = strlen(buffer);
104 case KAFKA_FORMAT_GRAPHITE:
105 status = format_graphite(buffer, sizeof(buffer), ds, vl,
106 ctx->prefix, ctx->postfix, ctx->escape_char,
107 ctx->graphite_flags);
109 ERROR("write_kafka plugin: format_graphite failed with status %i.",
113 blen = strlen(buffer);
116 ERROR("write_kafka plugin: invalid format %i.", ctx->format);
121 * We partition our stream by metric name
128 rd_kafka_produce(ctx->topic, RD_KAFKA_PARTITION_UA,
129 RD_KAFKA_MSG_F_COPY, buffer, blen,
130 &key, sizeof(key), NULL);
133 } /* }}} int kafka_write */
135 static void kafka_topic_context_free(void *p) /* {{{ */
137 struct kafka_topic_context *ctx = p;
142 if (ctx->topic_name != NULL)
143 sfree(ctx->topic_name);
144 if (ctx->topic != NULL)
145 rd_kafka_topic_destroy(ctx->topic);
146 if (ctx->conf != NULL)
147 rd_kafka_topic_conf_destroy(ctx->conf);
150 } /* }}} void kafka_topic_context_free */
152 static void kafka_config_topic(rd_kafka_conf_t *conf, oconfig_item_t *ci) /* {{{ */
156 struct kafka_topic_context *tctx;
159 char callback_name[DATA_MAX_NAME_LEN];
162 oconfig_item_t *child;
163 rd_kafka_conf_res_t ret;
165 if ((tctx = calloc(1, sizeof (*tctx))) == NULL) {
166 ERROR ("write_kafka plugin: calloc failed.");
170 tctx->escape_char = '.';
171 tctx->store_rates = 1;
173 if ((tctx->kafka = rd_kafka_new(RD_KAFKA_PRODUCER, conf,
174 errbuf, sizeof(errbuf))) == NULL) {
176 ERROR("write_kafka plugin: cannot create kafka handle.");
181 if ((tctx->conf = rd_kafka_topic_conf_new()) == NULL) {
182 rd_kafka_destroy(tctx->kafka);
184 ERROR ("write_kafka plugin: cannot create topic configuration.");
188 if (ci->values_num != 1) {
189 WARNING("kafka topic name needed.");
193 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
194 WARNING("kafka topic needs a string argument.");
198 if ((tctx->topic_name = strdup(ci->values[0].value.string)) == NULL) {
199 ERROR("write_kafka plugin: cannot copy topic name.");
203 for (i = 0; i < ci->children_num; i++) {
205 * The code here could be simplified but makes room
206 * for easy adding of new options later on.
208 child = &ci->children[i];
211 if (strcasecmp ("Property", child->key) == 0) {
212 if (child->values_num != 2) {
213 WARNING("kafka properties need both a key and a value.");
216 if (child->values[0].type != OCONFIG_TYPE_STRING ||
217 child->values[1].type != OCONFIG_TYPE_STRING) {
218 WARNING("kafka properties needs string arguments.");
221 key = child->values[0].value.string;
222 val = child->values[0].value.string;
223 ret = rd_kafka_topic_conf_set(tctx->conf,key, val,
224 errbuf, sizeof(errbuf));
225 if (ret != RD_KAFKA_CONF_OK) {
226 WARNING("cannot set kafka topic property %s to %s: %s.",
231 } else if (strcasecmp ("Key", child->key) == 0) {
232 char *tmp_buf = NULL;
233 status = cf_util_get_string(child, &tmp_buf);
235 WARNING("write_kafka plugin: invalid key supplied");
239 if (strcasecmp(tmp_buf, "Random") != 0) {
241 tctx->key = crc32_buffer((u_char *)tmp_buf, strlen(tmp_buf));
245 } else if (strcasecmp ("Format", child->key) == 0) {
246 status = cf_util_get_string(child, &key);
252 if (strcasecmp(key, "Command") == 0) {
254 tctx->format = KAFKA_FORMAT_COMMAND;
256 } else if (strcasecmp(key, "Graphite") == 0) {
257 tctx->format = KAFKA_FORMAT_GRAPHITE;
259 } else if (strcasecmp(key, "Json") == 0) {
260 tctx->format = KAFKA_FORMAT_JSON;
263 WARNING ("write_kafka plugin: Invalid format string: %s",
268 } else if (strcasecmp ("StoreRates", child->key) == 0) {
269 status = cf_util_get_boolean (child, &tctx->store_rates);
270 (void) cf_util_get_flag (child, &tctx->graphite_flags,
271 GRAPHITE_STORE_RATES);
273 } else if (strcasecmp ("GraphiteSeparateInstances", child->key) == 0) {
274 status = cf_util_get_flag (child, &tctx->graphite_flags,
275 GRAPHITE_SEPARATE_INSTANCES);
277 } else if (strcasecmp ("GraphiteAlwaysAppendDS", child->key) == 0) {
278 status = cf_util_get_flag (child, &tctx->graphite_flags,
279 GRAPHITE_ALWAYS_APPEND_DS);
281 } else if (strcasecmp ("GraphitePrefix", child->key) == 0) {
282 status = cf_util_get_string (child, &tctx->prefix);
283 } else if (strcasecmp ("GraphitePostfix", child->key) == 0) {
284 status = cf_util_get_string (child, &tctx->postfix);
285 } else if (strcasecmp ("GraphiteEscapeChar", child->key) == 0) {
286 char *tmp_buff = NULL;
287 status = cf_util_get_string (child, &tmp_buff);
288 if (strlen (tmp_buff) > 1)
289 WARNING ("write_kafka plugin: The option \"GraphiteEscapeChar\" handles "
290 "only one character. Others will be ignored.");
291 tctx->escape_char = tmp_buff[0];
294 WARNING ("write_kafka plugin: Invalid directive: %s.", child->key);
301 rd_kafka_topic_conf_set_partitioner_cb(tctx->conf, kafka_partition);
302 rd_kafka_topic_conf_set_opaque(tctx->conf, tctx);
304 if ((tctx->topic = rd_kafka_topic_new(tctx->kafka, tctx->topic_name,
305 tctx->conf)) == NULL) {
306 ERROR("write_kafka plugin: cannot create topic.");
311 ssnprintf(callback_name, sizeof(callback_name),
312 "write_kafka/%s", tctx->topic_name);
315 ud.free_func = kafka_topic_context_free;
317 status = plugin_register_write (callback_name, kafka_write, &ud);
319 WARNING ("write_kafka plugin: plugin_register_write (\"%s\") "
320 "failed with status %i.",
321 callback_name, status);
327 rd_kafka_conf_destroy(conf);
328 if (tctx->kafka != NULL)
329 rd_kafka_destroy(tctx->kafka);
330 if (tctx->topic != NULL)
331 rd_kafka_topic_destroy(tctx->topic);
332 if (tctx->topic_name != NULL)
333 free(tctx->topic_name);
334 if (tctx->conf != NULL)
335 rd_kafka_topic_conf_destroy(tctx->conf);
337 } /* }}} int kafka_config_topic */
339 static int kafka_config(oconfig_item_t *ci) /* {{{ */
342 oconfig_item_t *child;
343 rd_kafka_conf_t *conf;
344 rd_kafka_conf_t *cloned;
345 rd_kafka_conf_res_t ret;
348 if ((conf = rd_kafka_conf_new()) == NULL) {
349 WARNING("cannot allocate kafka configuration.");
353 for (i = 0; i < ci->children_num; i++) {
354 child = &ci->children[i];
356 if (strcasecmp("Topic", child->key) == 0) {
357 if ((cloned = rd_kafka_conf_dup(conf)) == NULL) {
358 WARNING("write_kafka plugin: cannot allocate memory for kafka config");
361 kafka_config_topic (cloned, child);
362 } else if (strcasecmp(child->key, "Property") == 0) {
366 if (child->values_num != 2) {
367 WARNING("kafka properties need both a key and a value.");
370 if (child->values[0].type != OCONFIG_TYPE_STRING ||
371 child->values[1].type != OCONFIG_TYPE_STRING) {
372 WARNING("kafka properties needs string arguments.");
375 if ((key = strdup(child->values[0].value.string)) == NULL) {
376 WARNING("cannot allocate memory for attribute key.");
379 if ((val = strdup(child->values[1].value.string)) == NULL) {
380 WARNING("cannot allocate memory for attribute value.");
383 ret = rd_kafka_conf_set(conf, key, val, errbuf, sizeof(errbuf));
384 if (ret != RD_KAFKA_CONF_OK) {
385 WARNING("cannot set kafka property %s to %s: %s",
392 WARNING ("write_kafka plugin: Ignoring unknown "
393 "configuration option \"%s\" at top level.",
398 rd_kafka_conf_destroy(conf);
402 rd_kafka_conf_destroy(conf);
404 } /* }}} int kafka_config */
406 void module_register(void)
408 plugin_register_complex_config ("write_kafka", kafka_config);
411 /* vim: set sw=8 sts=8 ts=8 noet : */