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 *);
58 static void kafka_log(const rd_kafka_t *, int, const char *, const char *);
60 static void kafka_log(const rd_kafka_t *rkt, int level,
61 const char *fac, const char *msg)
63 plugin_log(level, "%s", msg);
66 static int32_t kafka_partition(const rd_kafka_topic_t *rkt,
67 const void *keydata, size_t keylen,
68 int32_t partition_cnt, void *p, void *m)
70 u_int32_t key = *((u_int32_t *)keydata );
72 return key % partition_cnt;
75 static int kafka_write(const data_set_t *ds, /* {{{ */
76 const value_list_t *vl,
82 size_t bfree = sizeof(buffer);
85 struct kafka_topic_context *ctx = ud->data;
87 if ((ds == NULL) || (vl == NULL) || (ctx == NULL))
90 bzero(buffer, sizeof(buffer));
92 switch (ctx->format) {
93 case KAFKA_FORMAT_COMMAND:
94 status = create_putval(buffer, sizeof(buffer), ds, vl);
96 ERROR("write_kafka plugin: create_putval failed with status %i.",
100 blen = strlen(buffer);
102 case KAFKA_FORMAT_JSON:
104 format_json_initialize(buffer, &bfill, &bfree);
105 format_json_value_list(buffer, &bfill, &bfree, ds, vl,
107 format_json_finalize(buffer, &bfill, &bfree);
108 blen = strlen(buffer);
110 case KAFKA_FORMAT_GRAPHITE:
111 status = format_graphite(buffer, sizeof(buffer), ds, vl,
112 ctx->prefix, ctx->postfix, ctx->escape_char,
113 ctx->graphite_flags);
115 ERROR("write_kafka plugin: format_graphite failed with status %i.",
119 blen = strlen(buffer);
122 ERROR("write_kafka plugin: invalid format %i.", ctx->format);
127 * We partition our stream by metric name
134 rd_kafka_produce(ctx->topic, RD_KAFKA_PARTITION_UA,
135 RD_KAFKA_MSG_F_COPY, buffer, blen,
136 &key, sizeof(key), NULL);
139 } /* }}} int kafka_write */
141 static void kafka_topic_context_free(void *p) /* {{{ */
143 struct kafka_topic_context *ctx = p;
148 if (ctx->topic_name != NULL)
149 sfree(ctx->topic_name);
150 if (ctx->topic != NULL)
151 rd_kafka_topic_destroy(ctx->topic);
152 if (ctx->conf != NULL)
153 rd_kafka_topic_conf_destroy(ctx->conf);
156 } /* }}} void kafka_topic_context_free */
158 static void kafka_config_topic(rd_kafka_conf_t *conf, oconfig_item_t *ci) /* {{{ */
162 struct kafka_topic_context *tctx;
165 char callback_name[DATA_MAX_NAME_LEN];
168 oconfig_item_t *child;
169 rd_kafka_conf_res_t ret;
171 if ((tctx = calloc(1, sizeof (*tctx))) == NULL) {
172 ERROR ("write_kafka plugin: calloc failed.");
176 tctx->escape_char = '.';
177 tctx->store_rates = 1;
179 rd_kafka_conf_set_log_cb(conf, kafka_log);
180 if ((tctx->kafka = rd_kafka_new(RD_KAFKA_PRODUCER, conf,
181 errbuf, sizeof(errbuf))) == NULL) {
183 ERROR("write_kafka plugin: cannot create kafka handle.");
188 if ((tctx->conf = rd_kafka_topic_conf_new()) == NULL) {
189 rd_kafka_destroy(tctx->kafka);
191 ERROR ("write_kafka plugin: cannot create topic configuration.");
195 if (ci->values_num != 1) {
196 WARNING("kafka topic name needed.");
200 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
201 WARNING("kafka topic needs a string argument.");
205 if ((tctx->topic_name = strdup(ci->values[0].value.string)) == NULL) {
206 ERROR("write_kafka plugin: cannot copy topic name.");
210 for (i = 0; i < ci->children_num; i++) {
212 * The code here could be simplified but makes room
213 * for easy adding of new options later on.
215 child = &ci->children[i];
218 if (strcasecmp ("Property", child->key) == 0) {
219 if (child->values_num != 2) {
220 WARNING("kafka properties need both a key and a value.");
223 if (child->values[0].type != OCONFIG_TYPE_STRING ||
224 child->values[1].type != OCONFIG_TYPE_STRING) {
225 WARNING("kafka properties needs string arguments.");
228 key = child->values[0].value.string;
229 val = child->values[0].value.string;
230 ret = rd_kafka_topic_conf_set(tctx->conf,key, val,
231 errbuf, sizeof(errbuf));
232 if (ret != RD_KAFKA_CONF_OK) {
233 WARNING("cannot set kafka topic property %s to %s: %s.",
238 } else if (strcasecmp ("Key", child->key) == 0) {
239 char *tmp_buf = NULL;
240 status = cf_util_get_string(child, &tmp_buf);
242 WARNING("write_kafka plugin: invalid key supplied");
246 if (strcasecmp(tmp_buf, "Random") != 0) {
248 tctx->key = crc32_buffer((u_char *)tmp_buf, strlen(tmp_buf));
252 } else if (strcasecmp ("Format", child->key) == 0) {
253 status = cf_util_get_string(child, &key);
259 if (strcasecmp(key, "Command") == 0) {
261 tctx->format = KAFKA_FORMAT_COMMAND;
263 } else if (strcasecmp(key, "Graphite") == 0) {
264 tctx->format = KAFKA_FORMAT_GRAPHITE;
266 } else if (strcasecmp(key, "Json") == 0) {
267 tctx->format = KAFKA_FORMAT_JSON;
270 WARNING ("write_kafka plugin: Invalid format string: %s",
275 } else if (strcasecmp ("StoreRates", child->key) == 0) {
276 status = cf_util_get_boolean (child, &tctx->store_rates);
277 (void) cf_util_get_flag (child, &tctx->graphite_flags,
278 GRAPHITE_STORE_RATES);
280 } else if (strcasecmp ("GraphiteSeparateInstances", child->key) == 0) {
281 status = cf_util_get_flag (child, &tctx->graphite_flags,
282 GRAPHITE_SEPARATE_INSTANCES);
284 } else if (strcasecmp ("GraphiteAlwaysAppendDS", child->key) == 0) {
285 status = cf_util_get_flag (child, &tctx->graphite_flags,
286 GRAPHITE_ALWAYS_APPEND_DS);
288 } else if (strcasecmp ("GraphitePrefix", child->key) == 0) {
289 status = cf_util_get_string (child, &tctx->prefix);
290 } else if (strcasecmp ("GraphitePostfix", child->key) == 0) {
291 status = cf_util_get_string (child, &tctx->postfix);
292 } else if (strcasecmp ("GraphiteEscapeChar", child->key) == 0) {
293 char *tmp_buff = NULL;
294 status = cf_util_get_string (child, &tmp_buff);
295 if (strlen (tmp_buff) > 1)
296 WARNING ("write_kafka plugin: The option \"GraphiteEscapeChar\" handles "
297 "only one character. Others will be ignored.");
298 tctx->escape_char = tmp_buff[0];
301 WARNING ("write_kafka plugin: Invalid directive: %s.", child->key);
308 rd_kafka_topic_conf_set_partitioner_cb(tctx->conf, kafka_partition);
309 rd_kafka_topic_conf_set_opaque(tctx->conf, tctx);
311 if ((tctx->topic = rd_kafka_topic_new(tctx->kafka, tctx->topic_name,
312 tctx->conf)) == NULL) {
313 ERROR("write_kafka plugin: cannot create topic.");
318 ssnprintf(callback_name, sizeof(callback_name),
319 "write_kafka/%s", tctx->topic_name);
322 ud.free_func = kafka_topic_context_free;
324 status = plugin_register_write (callback_name, kafka_write, &ud);
326 WARNING ("write_kafka plugin: plugin_register_write (\"%s\") "
327 "failed with status %i.",
328 callback_name, status);
334 rd_kafka_conf_destroy(conf);
335 if (tctx->kafka != NULL)
336 rd_kafka_destroy(tctx->kafka);
337 if (tctx->topic != NULL)
338 rd_kafka_topic_destroy(tctx->topic);
339 if (tctx->topic_name != NULL)
340 free(tctx->topic_name);
341 if (tctx->conf != NULL)
342 rd_kafka_topic_conf_destroy(tctx->conf);
344 } /* }}} int kafka_config_topic */
346 static int kafka_config(oconfig_item_t *ci) /* {{{ */
349 oconfig_item_t *child;
350 rd_kafka_conf_t *conf;
351 rd_kafka_conf_t *cloned;
352 rd_kafka_conf_res_t ret;
355 if ((conf = rd_kafka_conf_new()) == NULL) {
356 WARNING("cannot allocate kafka configuration.");
360 for (i = 0; i < ci->children_num; i++) {
361 child = &ci->children[i];
363 if (strcasecmp("Topic", child->key) == 0) {
364 if ((cloned = rd_kafka_conf_dup(conf)) == NULL) {
365 WARNING("write_kafka plugin: cannot allocate memory for kafka config");
368 kafka_config_topic (cloned, child);
369 } else if (strcasecmp(child->key, "Property") == 0) {
373 if (child->values_num != 2) {
374 WARNING("kafka properties need both a key and a value.");
377 if (child->values[0].type != OCONFIG_TYPE_STRING ||
378 child->values[1].type != OCONFIG_TYPE_STRING) {
379 WARNING("kafka properties needs string arguments.");
382 if ((key = strdup(child->values[0].value.string)) == NULL) {
383 WARNING("cannot allocate memory for attribute key.");
386 if ((val = strdup(child->values[1].value.string)) == NULL) {
387 WARNING("cannot allocate memory for attribute value.");
390 ret = rd_kafka_conf_set(conf, key, val, errbuf, sizeof(errbuf));
391 if (ret != RD_KAFKA_CONF_OK) {
392 WARNING("cannot set kafka property %s to %s: %s",
399 WARNING ("write_kafka plugin: Ignoring unknown "
400 "configuration option \"%s\" at top level.",
405 rd_kafka_conf_destroy(conf);
409 rd_kafka_conf_destroy(conf);
411 } /* }}} int kafka_config */
413 void module_register(void)
415 plugin_register_complex_config ("write_kafka", kafka_config);
418 /* vim: set sw=8 sts=8 ts=8 noet : */