1 /**
2 * collectd - src/write_kafka.c
3 *
4 * Copyright (C) 2014 Pierre-Yves Ritschard
5 *
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.
9 *
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.
17 *
18 * Authors:
19 * Pierre-Yves Ritschard <pyr at spootnik.org>
20 */
22 #include "collectd.h"
23 #include "plugin.h"
24 #include "common.h"
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>
34 #include <pthread.h>
35 #include <zlib.h>
37 struct kafka_topic_context {
38 #define KAFKA_FORMAT_COMMAND 1
39 #define KAFKA_FORMAT_GRAPHITE 2
40 #define KAFKA_FORMAT_JSON 3
41 u_int8_t format;
42 unsigned int graphite_flags;
43 _Bool store_rates;
44 rd_kafka_topic_conf_t *conf;
45 rd_kafka_topic_t *topic;
46 rd_kafka_t *kafka;
47 int has_key;
48 u_int32_t key;
49 char *prefix;
50 char *postfix;
51 char escape_char;
52 char *topic_name;
53 };
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)
62 {
63 plugin_log(level, "%s", msg);
64 }
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)
69 {
70 u_int32_t key = *((u_int32_t *)keydata );
72 return key % partition_cnt;
73 }
75 static int kafka_write(const data_set_t *ds, /* {{{ */
76 const value_list_t *vl,
77 user_data_t *ud)
78 {
79 int status = 0;
80 u_int32_t key;
81 char buffer[8192];
82 size_t bfree = sizeof(buffer);
83 size_t bfill = 0;
84 size_t blen = 0;
85 struct kafka_topic_context *ctx = ud->data;
87 if ((ds == NULL) || (vl == NULL) || (ctx == NULL))
88 return EINVAL;
90 bzero(buffer, sizeof(buffer));
92 switch (ctx->format) {
93 case KAFKA_FORMAT_COMMAND:
94 status = create_putval(buffer, sizeof(buffer), ds, vl);
95 if (status != 0) {
96 ERROR("write_kafka plugin: create_putval failed with status %i.",
97 status);
98 return status;
99 }
100 blen = strlen(buffer);
101 break;
102 case KAFKA_FORMAT_JSON:
104 format_json_initialize(buffer, &bfill, &bfree);
105 format_json_value_list(buffer, &bfill, &bfree, ds, vl,
106 ctx->store_rates);
107 format_json_finalize(buffer, &bfill, &bfree);
108 blen = strlen(buffer);
109 break;
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);
114 if (status != 0) {
115 ERROR("write_kafka plugin: format_graphite failed with status %i.",
116 status);
117 return status;
118 }
119 blen = strlen(buffer);
120 break;
121 default:
122 ERROR("write_kafka plugin: invalid format %i.", ctx->format);
123 return -1;
124 }
126 /*
127 * We partition our stream by metric name
128 */
129 if (ctx->has_key)
130 key = ctx->key;
131 else
132 key = rand();
134 rd_kafka_produce(ctx->topic, RD_KAFKA_PARTITION_UA,
135 RD_KAFKA_MSG_F_COPY, buffer, blen,
136 &key, sizeof(key), NULL);
138 return status;
139 } /* }}} int kafka_write */
141 static void kafka_topic_context_free(void *p) /* {{{ */
142 {
143 struct kafka_topic_context *ctx = p;
145 if (ctx == NULL)
146 return;
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);
155 sfree(ctx);
156 } /* }}} void kafka_topic_context_free */
158 static void kafka_config_topic(rd_kafka_conf_t *conf, oconfig_item_t *ci) /* {{{ */
159 {
160 int status;
161 int i;
162 struct kafka_topic_context *tctx;
163 char *key;
164 char *val;
165 char callback_name[DATA_MAX_NAME_LEN];
166 char errbuf[1024];
167 user_data_t ud;
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.");
173 return;
174 }
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) {
182 sfree(tctx);
183 ERROR("write_kafka plugin: cannot create kafka handle.");
184 return;
185 }
186 conf = NULL;
188 if ((tctx->conf = rd_kafka_topic_conf_new()) == NULL) {
189 rd_kafka_destroy(tctx->kafka);
190 sfree(tctx);
191 ERROR ("write_kafka plugin: cannot create topic configuration.");
192 return;
193 }
195 if (ci->values_num != 1) {
196 WARNING("kafka topic name needed.");
197 goto errout;
198 }
200 if (ci->values[0].type != OCONFIG_TYPE_STRING) {
201 WARNING("kafka topic needs a string argument.");
202 goto errout;
203 }
205 if ((tctx->topic_name = strdup(ci->values[0].value.string)) == NULL) {
206 ERROR("write_kafka plugin: cannot copy topic name.");
207 goto errout;
208 }
210 for (i = 0; i < ci->children_num; i++) {
211 /*
212 * The code here could be simplified but makes room
213 * for easy adding of new options later on.
214 */
215 child = &ci->children[i];
216 status = 0;
218 if (strcasecmp ("Property", child->key) == 0) {
219 if (child->values_num != 2) {
220 WARNING("kafka properties need both a key and a value.");
221 goto errout;
222 }
223 if (child->values[0].type != OCONFIG_TYPE_STRING ||
224 child->values[1].type != OCONFIG_TYPE_STRING) {
225 WARNING("kafka properties needs string arguments.");
226 goto errout;
227 }
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.",
234 key, val, errbuf);
235 goto errout;
236 }
238 } else if (strcasecmp ("Key", child->key) == 0) {
239 char *tmp_buf = NULL;
240 status = cf_util_get_string(child, &tmp_buf);
241 if (status != 0) {
242 WARNING("write_kafka plugin: invalid key supplied");
243 break;
244 }
246 if (strcasecmp(tmp_buf, "Random") != 0) {
247 tctx->has_key = 1;
248 tctx->key = crc32_buffer((u_char *)tmp_buf, strlen(tmp_buf));
249 }
250 sfree(tmp_buf);
252 } else if (strcasecmp ("Format", child->key) == 0) {
253 status = cf_util_get_string(child, &key);
254 if (status != 0)
255 goto errout;
257 assert(key != NULL);
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;
269 } else {
270 WARNING ("write_kafka plugin: Invalid format string: %s",
271 key);
272 }
273 sfree(key);
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];
299 sfree (tmp_buff);
300 } else {
301 WARNING ("write_kafka plugin: Invalid directive: %s.", child->key);
302 }
304 if (status != 0)
305 break;
306 }
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.");
314 goto errout;
315 }
316 tctx->conf = NULL;
318 ssnprintf(callback_name, sizeof(callback_name),
319 "write_kafka/%s", tctx->topic_name);
321 ud.data = tctx;
322 ud.free_func = kafka_topic_context_free;
324 status = plugin_register_write (callback_name, kafka_write, &ud);
325 if (status != 0) {
326 WARNING ("write_kafka plugin: plugin_register_write (\"%s\") "
327 "failed with status %i.",
328 callback_name, status);
329 goto errout;
330 }
331 return;
332 errout:
333 if (conf != NULL)
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);
343 sfree(tctx);
344 } /* }}} int kafka_config_topic */
346 static int kafka_config(oconfig_item_t *ci) /* {{{ */
347 {
348 int i;
349 oconfig_item_t *child;
350 rd_kafka_conf_t *conf;
351 rd_kafka_conf_t *cloned;
352 rd_kafka_conf_res_t ret;
353 char errbuf[1024];
355 if ((conf = rd_kafka_conf_new()) == NULL) {
356 WARNING("cannot allocate kafka configuration.");
357 return -1;
358 }
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");
366 goto errout;
367 }
368 kafka_config_topic (cloned, child);
369 } else if (strcasecmp(child->key, "Property") == 0) {
370 char *key = NULL;
371 char *val = NULL;
373 if (child->values_num != 2) {
374 WARNING("kafka properties need both a key and a value.");
375 goto errout;
376 }
377 if (child->values[0].type != OCONFIG_TYPE_STRING ||
378 child->values[1].type != OCONFIG_TYPE_STRING) {
379 WARNING("kafka properties needs string arguments.");
380 goto errout;
381 }
382 if ((key = strdup(child->values[0].value.string)) == NULL) {
383 WARNING("cannot allocate memory for attribute key.");
384 goto errout;
385 }
386 if ((val = strdup(child->values[1].value.string)) == NULL) {
387 WARNING("cannot allocate memory for attribute value.");
388 goto errout;
389 }
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",
393 key, val, errbuf);
394 goto errout;
395 }
396 sfree(key);
397 sfree(val);
398 } else {
399 WARNING ("write_kafka plugin: Ignoring unknown "
400 "configuration option \"%s\" at top level.",
401 child->key);
402 }
403 }
404 if (conf != NULL)
405 rd_kafka_conf_destroy(conf);
406 return (0);
407 errout:
408 if (conf != NULL)
409 rd_kafka_conf_destroy(conf);
410 return -1;
411 } /* }}} int kafka_config */
413 void module_register(void)
414 {
415 plugin_register_complex_config ("write_kafka", kafka_config);
416 }
418 /* vim: set sw=8 sts=8 ts=8 noet : */