1 /**
2 * collectd - src/write_kafka.c
3 * Copyright (C) 2014 Pierre-Yves Ritschard
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors:
24 * Pierre-Yves Ritschard <pyr at spootnik.org>
25 */
27 #include "collectd.h"
28 #include "plugin.h"
29 #include "common.h"
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"
37 #include <stdint.h>
38 #include <librdkafka/rdkafka.h>
39 #include <pthread.h>
40 #include <zlib.h>
41 #include <errno.h>
43 struct kafka_topic_context {
44 #define KAFKA_FORMAT_JSON 0
45 #define KAFKA_FORMAT_COMMAND 1
46 #define KAFKA_FORMAT_GRAPHITE 2
47 uint8_t format;
48 unsigned int graphite_flags;
49 _Bool store_rates;
50 rd_kafka_topic_conf_t *conf;
51 rd_kafka_topic_t *topic;
52 rd_kafka_conf_t *kafka_conf;
53 rd_kafka_t *kafka;
54 char *key;
55 char *prefix;
56 char *postfix;
57 char escape_char;
58 char *topic_name;
59 pthread_mutex_t lock;
60 };
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)
72 {
73 plugin_log(level, "%s", msg);
74 }
75 #endif
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)
80 {
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;
87 }
88 return target;
89 }
91 static int kafka_handle(struct kafka_topic_context *ctx) /* {{{ */
92 {
93 char errbuf[1024];
94 rd_kafka_conf_t *conf;
95 rd_kafka_topic_conf_t *topic_conf;
97 if (ctx->kafka != NULL && ctx->topic != NULL)
98 return(0);
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");
103 return(1);
104 }
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.");
109 return 1;
110 }
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 #ifdef HAVE_LIBRDKAFKA_LOGGER
118 rd_kafka_set_logger(ctx->kafka, kafka_log);
119 #endif
120 }
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");
125 return 1;
126 }
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)));
132 return errno;
133 }
135 rd_kafka_topic_conf_destroy(ctx->conf);
136 ctx->conf = NULL;
138 INFO ("write_kafka plugin: handle created for topic : %s", rd_kafka_topic_name(ctx->topic));
139 }
141 return(0);
143 } /* }}} int kafka_handle */
145 static int kafka_write(const data_set_t *ds, /* {{{ */
146 const value_list_t *vl,
147 user_data_t *ud)
148 {
149 int status = 0;
150 void *key;
151 size_t keylen = 0;
152 char buffer[8192];
153 size_t bfree = sizeof(buffer);
154 size_t bfill = 0;
155 size_t blen = 0;
156 struct kafka_topic_context *ctx = ud->data;
158 if ((ds == NULL) || (vl == NULL) || (ctx == NULL))
159 return EINVAL;
161 pthread_mutex_lock (&ctx->lock);
162 status = kafka_handle(ctx);
163 pthread_mutex_unlock (&ctx->lock);
164 if( status != 0 )
165 return status;
167 bzero(buffer, sizeof(buffer));
169 switch (ctx->format) {
170 case KAFKA_FORMAT_COMMAND:
171 status = create_putval(buffer, sizeof(buffer), ds, vl);
172 if (status != 0) {
173 ERROR("write_kafka plugin: create_putval failed with status %i.",
174 status);
175 return status;
176 }
177 blen = strlen(buffer);
178 break;
179 case KAFKA_FORMAT_JSON:
180 format_json_initialize(buffer, &bfill, &bfree);
181 format_json_value_list(buffer, &bfill, &bfree, ds, vl,
182 ctx->store_rates);
183 format_json_finalize(buffer, &bfill, &bfree);
184 blen = strlen(buffer);
185 break;
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);
190 if (status != 0) {
191 ERROR("write_kafka plugin: format_graphite failed with status %i.",
192 status);
193 return status;
194 }
195 blen = strlen(buffer);
196 break;
197 default:
198 ERROR("write_kafka plugin: invalid format %i.", ctx->format);
199 return -1;
200 }
202 key = ctx->key;
203 if (key != NULL)
204 keylen = strlen (key);
206 rd_kafka_produce(ctx->topic, RD_KAFKA_PARTITION_UA,
207 RD_KAFKA_MSG_F_COPY, buffer, blen,
208 key, keylen, NULL);
210 return status;
211 } /* }}} int kafka_write */
213 static void kafka_topic_context_free(void *p) /* {{{ */
214 {
215 struct kafka_topic_context *ctx = p;
217 if (ctx == NULL)
218 return;
220 if (ctx->topic_name != NULL)
221 sfree(ctx->topic_name);
222 if (ctx->topic != NULL)
223 rd_kafka_topic_destroy(ctx->topic);
224 if (ctx->conf != NULL)
225 rd_kafka_topic_conf_destroy(ctx->conf);
226 if (ctx->kafka_conf != NULL)
227 rd_kafka_conf_destroy(ctx->kafka_conf);
228 if (ctx->kafka != NULL)
229 rd_kafka_destroy(ctx->kafka);
231 sfree(ctx);
232 } /* }}} void kafka_topic_context_free */
234 static void kafka_config_topic(rd_kafka_conf_t *conf, oconfig_item_t *ci) /* {{{ */
235 {
236 int status;
237 int i;
238 struct kafka_topic_context *tctx;
239 char *key = NULL;
240 char *val;
241 char callback_name[DATA_MAX_NAME_LEN];
242 char errbuf[1024];
243 user_data_t ud;
244 oconfig_item_t *child;
245 rd_kafka_conf_res_t ret;
247 if ((tctx = calloc(1, sizeof (*tctx))) == NULL) {
248 ERROR ("write_kafka plugin: calloc failed.");
249 return;
250 }
252 tctx->escape_char = '.';
253 tctx->store_rates = 1;
254 tctx->format = KAFKA_FORMAT_JSON;
256 if ((tctx->kafka_conf = rd_kafka_conf_dup(conf)) == NULL) {
257 sfree(tctx);
258 ERROR("write_kafka plugin: cannot allocate memory for kafka config");
259 return;
260 }
262 #ifdef HAVE_LIBRDKAFKA_LOG_CB
263 rd_kafka_conf_set_log_cb(tctx->kafka_conf, kafka_log);
264 #endif
266 if ((tctx->conf = rd_kafka_topic_conf_new()) == NULL) {
267 rd_kafka_conf_destroy(tctx->kafka_conf);
268 sfree(tctx);
269 ERROR ("write_kafka plugin: cannot create topic configuration.");
270 return;
271 }
273 if (ci->values_num != 1) {
274 WARNING("kafka topic name needed.");
275 goto errout;
276 }
278 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
279 WARNING("kafka topic needs a string argument.");
280 goto errout;
281 }
283 if ((tctx->topic_name = strdup(ci->values[0].value.string)) == NULL) {
284 ERROR("write_kafka plugin: cannot copy topic name.");
285 goto errout;
286 }
288 for (i = 0; i < ci->children_num; i++) {
289 /*
290 * The code here could be simplified but makes room
291 * for easy adding of new options later on.
292 */
293 child = &ci->children[i];
294 status = 0;
296 if (strcasecmp ("Property", child->key) == 0) {
297 if (child->values_num != 2) {
298 WARNING("kafka properties need both a key and a value.");
299 goto errout;
300 }
301 if (child->values[0].type != OCONFIG_TYPE_STRING ||
302 child->values[1].type != OCONFIG_TYPE_STRING) {
303 WARNING("kafka properties needs string arguments.");
304 goto errout;
305 }
306 key = child->values[0].value.string;
307 val = child->values[1].value.string;
308 ret = rd_kafka_topic_conf_set(tctx->conf,key, val,
309 errbuf, sizeof(errbuf));
310 if (ret != RD_KAFKA_CONF_OK) {
311 WARNING("cannot set kafka topic property %s to %s: %s.",
312 key, val, errbuf);
313 goto errout;
314 }
316 } else if (strcasecmp ("Key", child->key) == 0) {
317 cf_util_get_string (child, &tctx->key);
318 } else if (strcasecmp ("Format", child->key) == 0) {
319 status = cf_util_get_string(child, &key);
320 if (status != 0)
321 goto errout;
323 assert(key != NULL);
325 if (strcasecmp(key, "Command") == 0) {
326 tctx->format = KAFKA_FORMAT_COMMAND;
328 } else if (strcasecmp(key, "Graphite") == 0) {
329 tctx->format = KAFKA_FORMAT_GRAPHITE;
331 } else if (strcasecmp(key, "Json") == 0) {
332 tctx->format = KAFKA_FORMAT_JSON;
334 } else {
335 WARNING ("write_kafka plugin: Invalid format string: %s",
336 key);
337 }
339 sfree(key);
341 } else if (strcasecmp ("StoreRates", child->key) == 0) {
342 status = cf_util_get_boolean (child, &tctx->store_rates);
343 (void) cf_util_get_flag (child, &tctx->graphite_flags,
344 GRAPHITE_STORE_RATES);
346 } else if (strcasecmp ("GraphiteSeparateInstances", child->key) == 0) {
347 status = cf_util_get_flag (child, &tctx->graphite_flags,
348 GRAPHITE_SEPARATE_INSTANCES);
350 } else if (strcasecmp ("GraphiteAlwaysAppendDS", child->key) == 0) {
351 status = cf_util_get_flag (child, &tctx->graphite_flags,
352 GRAPHITE_ALWAYS_APPEND_DS);
354 } else if (strcasecmp ("GraphitePrefix", child->key) == 0) {
355 status = cf_util_get_string (child, &tctx->prefix);
356 } else if (strcasecmp ("GraphitePostfix", child->key) == 0) {
357 status = cf_util_get_string (child, &tctx->postfix);
358 } else if (strcasecmp ("GraphiteEscapeChar", child->key) == 0) {
359 char *tmp_buff = NULL;
360 status = cf_util_get_string (child, &tmp_buff);
361 if (strlen (tmp_buff) > 1)
362 WARNING ("write_kafka plugin: The option \"GraphiteEscapeChar\" handles "
363 "only one character. Others will be ignored.");
364 tctx->escape_char = tmp_buff[0];
365 sfree (tmp_buff);
366 } else {
367 WARNING ("write_kafka plugin: Invalid directive: %s.", child->key);
368 }
370 if (status != 0)
371 break;
372 }
374 rd_kafka_topic_conf_set_partitioner_cb(tctx->conf, kafka_partition);
375 rd_kafka_topic_conf_set_opaque(tctx->conf, tctx);
377 ssnprintf(callback_name, sizeof(callback_name),
378 "write_kafka/%s", tctx->topic_name);
380 ud.data = tctx;
381 ud.free_func = kafka_topic_context_free;
383 status = plugin_register_write (callback_name, kafka_write, &ud);
384 if (status != 0) {
385 WARNING ("write_kafka plugin: plugin_register_write (\"%s\") "
386 "failed with status %i.",
387 callback_name, status);
388 goto errout;
389 }
391 pthread_mutex_init (&tctx->lock, /* attr = */ NULL);
393 return;
394 errout:
395 if (tctx->topic_name != NULL)
396 free(tctx->topic_name);
397 if (tctx->conf != NULL)
398 rd_kafka_topic_conf_destroy(tctx->conf);
399 if (tctx->kafka_conf != NULL)
400 rd_kafka_conf_destroy(tctx->kafka_conf);
401 sfree(tctx);
402 } /* }}} int kafka_config_topic */
404 static int kafka_config(oconfig_item_t *ci) /* {{{ */
405 {
406 int i;
407 oconfig_item_t *child;
408 rd_kafka_conf_t *conf;
409 rd_kafka_conf_res_t ret;
410 char errbuf[1024];
412 if ((conf = rd_kafka_conf_new()) == NULL) {
413 WARNING("cannot allocate kafka configuration.");
414 return -1;
415 }
416 for (i = 0; i < ci->children_num; i++) {
417 child = &ci->children[i];
419 if (strcasecmp("Topic", child->key) == 0) {
420 kafka_config_topic (conf, child);
421 } else if (strcasecmp(child->key, "Property") == 0) {
422 char *key = NULL;
423 char *val = NULL;
425 if (child->values_num != 2) {
426 WARNING("kafka properties need both a key and a value.");
427 goto errout;
428 }
429 if (child->values[0].type != OCONFIG_TYPE_STRING ||
430 child->values[1].type != OCONFIG_TYPE_STRING) {
431 WARNING("kafka properties needs string arguments.");
432 goto errout;
433 }
434 if ((key = strdup(child->values[0].value.string)) == NULL) {
435 WARNING("cannot allocate memory for attribute key.");
436 goto errout;
437 }
438 if ((val = strdup(child->values[1].value.string)) == NULL) {
439 WARNING("cannot allocate memory for attribute value.");
440 sfree(key);
441 goto errout;
442 }
443 ret = rd_kafka_conf_set(conf, key, val, errbuf, sizeof(errbuf));
444 if (ret != RD_KAFKA_CONF_OK) {
445 WARNING("cannot set kafka property %s to %s: %s",
446 key, val, errbuf);
447 sfree(key);
448 sfree(val);
449 goto errout;
450 }
451 sfree(key);
452 sfree(val);
453 } else {
454 WARNING ("write_kafka plugin: Ignoring unknown "
455 "configuration option \"%s\" at top level.",
456 child->key);
457 }
458 }
459 if (conf != NULL)
460 rd_kafka_conf_destroy(conf);
461 return (0);
462 errout:
463 if (conf != NULL)
464 rd_kafka_conf_destroy(conf);
465 return -1;
466 } /* }}} int kafka_config */
468 void module_register(void)
469 {
470 plugin_register_complex_config ("write_kafka", kafka_config);
471 }