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 <sys/types.h>
38 #include <librdkafka/rdkafka.h>
39 #include <pthread.h>
40 #include <zlib.h>
42 struct kafka_topic_context {
43 #define KAFKA_FORMAT_JSON 0
44 #define KAFKA_FORMAT_COMMAND 1
45 #define KAFKA_FORMAT_GRAPHITE 2
46 u_int8_t format;
47 unsigned int graphite_flags;
48 _Bool store_rates;
49 rd_kafka_topic_conf_t *conf;
50 rd_kafka_topic_t *topic;
51 rd_kafka_t *kafka;
52 int has_key;
53 u_int32_t key;
54 char *prefix;
55 char *postfix;
56 char escape_char;
57 char *topic_name;
58 };
60 static int kafka_write(const data_set_t *, const value_list_t *, user_data_t *);
61 static int32_t kafka_partition(const rd_kafka_topic_t *, const void *, size_t,
62 int32_t, void *, void *);
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)
68 {
69 plugin_log(level, "%s", msg);
70 }
72 static int32_t kafka_partition(const rd_kafka_topic_t *rkt,
73 const void *keydata, size_t keylen,
74 int32_t partition_cnt, void *p, void *m)
75 {
76 u_int32_t key = *((u_int32_t *)keydata );
78 return key % partition_cnt;
79 }
81 static int kafka_write(const data_set_t *ds, /* {{{ */
82 const value_list_t *vl,
83 user_data_t *ud)
84 {
85 int status = 0;
86 u_int32_t key;
87 char buffer[8192];
88 size_t bfree = sizeof(buffer);
89 size_t bfill = 0;
90 size_t blen = 0;
91 struct kafka_topic_context *ctx = ud->data;
93 if ((ds == NULL) || (vl == NULL) || (ctx == NULL))
94 return EINVAL;
96 bzero(buffer, sizeof(buffer));
98 switch (ctx->format) {
99 case KAFKA_FORMAT_COMMAND:
100 status = create_putval(buffer, sizeof(buffer), ds, vl);
101 if (status != 0) {
102 ERROR("write_kafka plugin: create_putval failed with status %i.",
103 status);
104 return status;
105 }
106 blen = strlen(buffer);
107 break;
108 case KAFKA_FORMAT_JSON:
110 format_json_initialize(buffer, &bfill, &bfree);
111 format_json_value_list(buffer, &bfill, &bfree, ds, vl,
112 ctx->store_rates);
113 format_json_finalize(buffer, &bfill, &bfree);
114 blen = strlen(buffer);
115 break;
116 case KAFKA_FORMAT_GRAPHITE:
117 status = format_graphite(buffer, sizeof(buffer), ds, vl,
118 ctx->prefix, ctx->postfix, ctx->escape_char,
119 ctx->graphite_flags);
120 if (status != 0) {
121 ERROR("write_kafka plugin: format_graphite failed with status %i.",
122 status);
123 return status;
124 }
125 blen = strlen(buffer);
126 break;
127 default:
128 ERROR("write_kafka plugin: invalid format %i.", ctx->format);
129 return -1;
130 }
132 /*
133 * We partition our stream by metric name
134 */
135 if (ctx->has_key)
136 key = ctx->key;
137 else
138 key = rand();
140 rd_kafka_produce(ctx->topic, RD_KAFKA_PARTITION_UA,
141 RD_KAFKA_MSG_F_COPY, buffer, blen,
142 &key, sizeof(key), NULL);
144 return status;
145 } /* }}} int kafka_write */
147 static void kafka_topic_context_free(void *p) /* {{{ */
148 {
149 struct kafka_topic_context *ctx = p;
151 if (ctx == NULL)
152 return;
154 if (ctx->topic_name != NULL)
155 sfree(ctx->topic_name);
156 if (ctx->topic != NULL)
157 rd_kafka_topic_destroy(ctx->topic);
158 if (ctx->conf != NULL)
159 rd_kafka_topic_conf_destroy(ctx->conf);
161 sfree(ctx);
162 } /* }}} void kafka_topic_context_free */
164 static void kafka_config_topic(rd_kafka_conf_t *conf, oconfig_item_t *ci) /* {{{ */
165 {
166 int status;
167 int i;
168 struct kafka_topic_context *tctx;
169 char *key = NULL;
170 char *val;
171 char callback_name[DATA_MAX_NAME_LEN];
172 char errbuf[1024];
173 user_data_t ud;
174 oconfig_item_t *child;
175 rd_kafka_conf_res_t ret;
177 if ((tctx = calloc(1, sizeof (*tctx))) == NULL) {
178 ERROR ("write_kafka plugin: calloc failed.");
179 return;
180 }
182 tctx->escape_char = '.';
183 tctx->store_rates = 1;
184 tctx->format = KAFKA_FORMAT_JSON;
186 #ifdef HAVE_LIBRDKAFKA_LOG_CB
187 rd_kafka_conf_set_log_cb(conf, kafka_log);
188 #endif
189 if ((tctx->kafka = rd_kafka_new(RD_KAFKA_PRODUCER, conf,
190 errbuf, sizeof(errbuf))) == NULL) {
191 sfree(tctx);
192 ERROR("write_kafka plugin: cannot create kafka handle.");
193 return;
194 }
195 #ifdef HAVE_LIBRDKAFKA_LOGGER
196 rd_kafka_conf_set_logger(tctx->kafka, kafka_log);
197 #endif
198 conf = NULL;
200 if ((tctx->conf = rd_kafka_topic_conf_new()) == NULL) {
201 rd_kafka_destroy(tctx->kafka);
202 sfree(tctx);
203 ERROR ("write_kafka plugin: cannot create topic configuration.");
204 return;
205 }
207 if (ci->values_num != 1) {
208 WARNING("kafka topic name needed.");
209 goto errout;
210 }
212 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
213 WARNING("kafka topic needs a string argument.");
214 goto errout;
215 }
217 if ((tctx->topic_name = strdup(ci->values[0].value.string)) == NULL) {
218 ERROR("write_kafka plugin: cannot copy topic name.");
219 goto errout;
220 }
222 for (i = 0; i < ci->children_num; i++) {
223 /*
224 * The code here could be simplified but makes room
225 * for easy adding of new options later on.
226 */
227 child = &ci->children[i];
228 status = 0;
230 if (strcasecmp ("Property", child->key) == 0) {
231 if (child->values_num != 2) {
232 WARNING("kafka properties need both a key and a value.");
233 goto errout;
234 }
235 if (child->values[0].type != OCONFIG_TYPE_STRING ||
236 child->values[1].type != OCONFIG_TYPE_STRING) {
237 WARNING("kafka properties needs string arguments.");
238 goto errout;
239 }
240 key = child->values[0].value.string;
241 val = child->values[0].value.string;
242 ret = rd_kafka_topic_conf_set(tctx->conf,key, val,
243 errbuf, sizeof(errbuf));
244 if (ret != RD_KAFKA_CONF_OK) {
245 WARNING("cannot set kafka topic property %s to %s: %s.",
246 key, val, errbuf);
247 goto errout;
248 }
250 } else if (strcasecmp ("Key", child->key) == 0) {
251 char *tmp_buf = NULL;
252 status = cf_util_get_string(child, &tmp_buf);
253 if (status != 0) {
254 WARNING("write_kafka plugin: invalid key supplied");
255 break;
256 }
258 if (strcasecmp(tmp_buf, "Random") != 0) {
259 tctx->has_key = 1;
260 tctx->key = crc32_buffer((u_char *)tmp_buf, strlen(tmp_buf));
261 }
262 sfree(tmp_buf);
264 } else if (strcasecmp ("Format", child->key) == 0) {
265 status = cf_util_get_string(child, &key);
266 if (status != 0)
267 goto errout;
269 assert(key != NULL);
271 if (strcasecmp(key, "Command") == 0) {
272 tctx->format = KAFKA_FORMAT_COMMAND;
274 } else if (strcasecmp(key, "Graphite") == 0) {
275 tctx->format = KAFKA_FORMAT_GRAPHITE;
277 } else if (strcasecmp(key, "Json") == 0) {
278 tctx->format = KAFKA_FORMAT_JSON;
280 } else {
281 WARNING ("write_kafka plugin: Invalid format string: %s",
282 key);
283 }
285 sfree(key);
287 } else if (strcasecmp ("StoreRates", child->key) == 0) {
288 status = cf_util_get_boolean (child, &tctx->store_rates);
289 (void) cf_util_get_flag (child, &tctx->graphite_flags,
290 GRAPHITE_STORE_RATES);
292 } else if (strcasecmp ("GraphiteSeparateInstances", child->key) == 0) {
293 status = cf_util_get_flag (child, &tctx->graphite_flags,
294 GRAPHITE_SEPARATE_INSTANCES);
296 } else if (strcasecmp ("GraphiteAlwaysAppendDS", child->key) == 0) {
297 status = cf_util_get_flag (child, &tctx->graphite_flags,
298 GRAPHITE_ALWAYS_APPEND_DS);
300 } else if (strcasecmp ("GraphitePrefix", child->key) == 0) {
301 status = cf_util_get_string (child, &tctx->prefix);
302 } else if (strcasecmp ("GraphitePostfix", child->key) == 0) {
303 status = cf_util_get_string (child, &tctx->postfix);
304 } else if (strcasecmp ("GraphiteEscapeChar", child->key) == 0) {
305 char *tmp_buff = NULL;
306 status = cf_util_get_string (child, &tmp_buff);
307 if (strlen (tmp_buff) > 1)
308 WARNING ("write_kafka plugin: The option \"GraphiteEscapeChar\" handles "
309 "only one character. Others will be ignored.");
310 tctx->escape_char = tmp_buff[0];
311 sfree (tmp_buff);
312 } else {
313 WARNING ("write_kafka plugin: Invalid directive: %s.", child->key);
314 }
316 if (status != 0)
317 break;
318 }
320 rd_kafka_topic_conf_set_partitioner_cb(tctx->conf, kafka_partition);
321 rd_kafka_topic_conf_set_opaque(tctx->conf, tctx);
323 if ((tctx->topic = rd_kafka_topic_new(tctx->kafka, tctx->topic_name,
324 tctx->conf)) == NULL) {
325 ERROR("write_kafka plugin: cannot create topic.");
326 goto errout;
327 }
328 tctx->conf = NULL;
330 ssnprintf(callback_name, sizeof(callback_name),
331 "write_kafka/%s", tctx->topic_name);
333 ud.data = tctx;
334 ud.free_func = kafka_topic_context_free;
336 status = plugin_register_write (callback_name, kafka_write, &ud);
337 if (status != 0) {
338 WARNING ("write_kafka plugin: plugin_register_write (\"%s\") "
339 "failed with status %i.",
340 callback_name, status);
341 goto errout;
342 }
343 return;
344 errout:
345 if (conf != NULL)
346 rd_kafka_conf_destroy(conf);
347 if (tctx->kafka != NULL)
348 rd_kafka_destroy(tctx->kafka);
349 if (tctx->topic != NULL)
350 rd_kafka_topic_destroy(tctx->topic);
351 if (tctx->topic_name != NULL)
352 free(tctx->topic_name);
353 if (tctx->conf != NULL)
354 rd_kafka_topic_conf_destroy(tctx->conf);
355 sfree(tctx);
356 } /* }}} int kafka_config_topic */
358 static int kafka_config(oconfig_item_t *ci) /* {{{ */
359 {
360 int i;
361 oconfig_item_t *child;
362 rd_kafka_conf_t *conf;
363 rd_kafka_conf_t *cloned;
364 rd_kafka_conf_res_t ret;
365 char errbuf[1024];
367 if ((conf = rd_kafka_conf_new()) == NULL) {
368 WARNING("cannot allocate kafka configuration.");
369 return -1;
370 }
372 for (i = 0; i < ci->children_num; i++) {
373 child = &ci->children[i];
375 if (strcasecmp("Topic", child->key) == 0) {
376 if ((cloned = rd_kafka_conf_dup(conf)) == NULL) {
377 WARNING("write_kafka plugin: cannot allocate memory for kafka config");
378 goto errout;
379 }
380 kafka_config_topic (cloned, child);
381 } else if (strcasecmp(child->key, "Property") == 0) {
382 char *key = NULL;
383 char *val = NULL;
385 if (child->values_num != 2) {
386 WARNING("kafka properties need both a key and a value.");
387 goto errout;
388 }
389 if (child->values[0].type != OCONFIG_TYPE_STRING ||
390 child->values[1].type != OCONFIG_TYPE_STRING) {
391 WARNING("kafka properties needs string arguments.");
392 goto errout;
393 }
394 if ((key = strdup(child->values[0].value.string)) == NULL) {
395 WARNING("cannot allocate memory for attribute key.");
396 goto errout;
397 }
398 if ((val = strdup(child->values[1].value.string)) == NULL) {
399 WARNING("cannot allocate memory for attribute value.");
400 goto errout;
401 }
402 ret = rd_kafka_conf_set(conf, key, val, errbuf, sizeof(errbuf));
403 if (ret != RD_KAFKA_CONF_OK) {
404 WARNING("cannot set kafka property %s to %s: %s",
405 key, val, errbuf);
406 goto errout;
407 }
408 sfree(key);
409 sfree(val);
410 } else {
411 WARNING ("write_kafka plugin: Ignoring unknown "
412 "configuration option \"%s\" at top level.",
413 child->key);
414 }
415 }
416 if (conf != NULL)
417 rd_kafka_conf_destroy(conf);
418 return (0);
419 errout:
420 if (conf != NULL)
421 rd_kafka_conf_destroy(conf);
422 return -1;
423 } /* }}} int kafka_config */
425 void module_register(void)
426 {
427 plugin_register_complex_config ("write_kafka", kafka_config);
428 }
430 /* vim: set sw=8 sts=8 ts=8 noet : */