2 * collectd - src/write_syslog.c
3 * Copyright (C) 2012 Pierre-Yves Ritschard
4 * Copyright (C) 2011 Scott Sanders
5 * Copyright (C) 2009 Paul Sadauskas
6 * Copyright (C) 2009 Doug MacEachern
7 * Copyright (C) 2007-2012 Florian octo Forster
8 * Copyright (C) 2013-2014 Limelight Networks, Inc.
9 * Copyright (C) 2019 Shirly Radco
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; only version 2 of the License is applicable.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 * Based on the write_graphite plugin. Authors:
24 * Florian octo Forster <octo at collectd.org>
25 * Doug MacEachern <dougm at hyperic.com>
26 * Paul Sadauskas <psadauskas at gmail.com>
27 * Scott Sanders <scott at jssjr.com>
28 * Pierre-Yves Ritschard <pyr at spootnik.org>
29 * Based on the write_tsdb plugin. Authors:
30 * Brett Hawn <bhawn at llnw.com>
31 * Kevin Bowling <kbowling@llnw.com>
32 * write_syslog. Authors:
33 * Shirly Radco <sradco@redhat.com>
36 /* write_syslog plugin configuration example
38 * <Plugin write_syslog>
43 * MessageFormat "human"
44 * HostTags "["prefix1" "example1"="example1_v"]
51 #include "utils/common/common.h"
54 #include "utils_cache.h"
55 #include "utils_random.h"
59 #define WS_DEFAULT_NODE "localhost"
61 #define WS_DEFAULT_SERVICE "44514"
63 #define WS_DEFAULT_FORMAT "human"
65 #define WS_DEFAULT_PREFIX "collectd"
67 #define WS_DEFAULT_ESCAPE '.'
69 /* Ethernet - (IPv6 + TCP) = 1500 - (40 + 32) = 1428 */
70 #define WS_SEND_BUF_SIZE 1428
77 cdtime_t ai_last_update;
86 bool always_append_ds;
88 char send_buf[WS_SEND_BUF_SIZE];
91 cdtime_t send_buf_init_time;
93 pthread_mutex_t send_lock;
95 bool connect_failed_log_enabled;
96 int connect_dns_failed_attempts_remaining;
97 cdtime_t next_random_ttl;
100 static cdtime_t resolve_interval;
101 static cdtime_t resolve_jitter;
106 static void ws_reset_buffer(struct ws_callback *cb) {
107 memset(cb->send_buf, 0, sizeof(cb->send_buf));
108 cb->send_buf_free = sizeof(cb->send_buf);
109 cb->send_buf_fill = 0;
110 cb->send_buf_init_time = cdtime();
113 static int ws_send_buffer(struct ws_callback *cb) {
116 status = swrite(cb->sock_fd, cb->send_buf, strlen(cb->send_buf));
118 ERROR("write_syslog plugin: send failed with status %zi (%s)", status,
121 if (cb->sock_fd > 0) {
132 /* NOTE: You must hold cb->send_lock when calling this function! */
133 static int ws_flush_nolock(cdtime_t timeout, struct ws_callback *cb) {
136 DEBUG("write_syslog plugin: ws_flush_nolock: timeout = %.3f; "
137 "send_buf_fill = %" PRIsz ";",
138 (double)timeout, cb->send_buf_fill);
140 /* timeout == 0 => flush unconditionally */
145 if ((cb->send_buf_init_time + timeout) > now)
149 if (cb->send_buf_fill == 0) {
150 cb->send_buf_init_time = cdtime();
154 status = ws_send_buffer(cb);
160 static cdtime_t new_random_ttl(void) {
161 if (resolve_jitter == 0)
164 return (cdtime_t)cdrand_range(0, (long)resolve_jitter);
167 static int ws_callback_init(struct ws_callback *cb) {
171 const char *node = cb->node ? cb->node : WS_DEFAULT_NODE;
172 const char *service = cb->service ? cb->service : WS_DEFAULT_SERVICE;
179 /* When we are here, we still have the IP in cache.
180 * If we have remaining attempts without calling the DNS, we update the
181 * last_update date so we keep the info until next time.
182 * If there is no more attempts, we need to flush the cache.
185 if ((cb->ai_last_update + resolve_interval + cb->next_random_ttl) < now) {
186 cb->next_random_ttl = new_random_ttl();
187 if (cb->connect_dns_failed_attempts_remaining > 0) {
188 /* Warning : this is run under send_lock mutex.
189 * This is why we do not use another mutex here.
191 cb->ai_last_update = now;
192 cb->connect_dns_failed_attempts_remaining--;
194 freeaddrinfo(cb->ai);
200 if (cb->ai == NULL) {
201 if ((cb->ai_last_update + resolve_interval + cb->next_random_ttl) >= now) {
202 DEBUG("write_syslog plugin: too many getaddrinfo(%s, %s) failures", node,
206 cb->ai_last_update = now;
207 cb->next_random_ttl = new_random_ttl();
209 struct addrinfo ai_hints = {
210 .ai_family = AF_UNSPEC,
211 .ai_flags = AI_ADDRCONFIG,
212 .ai_socktype = SOCK_STREAM,
215 status = getaddrinfo(node, service, &ai_hints, &cb->ai);
218 freeaddrinfo(cb->ai);
221 if (cb->connect_failed_log_enabled) {
222 ERROR("write_syslog plugin: getaddrinfo(%s, %s) failed: %s", node,
223 service, gai_strerror(status));
224 cb->connect_failed_log_enabled = 0;
230 assert(cb->ai != NULL);
231 for (struct addrinfo *ai = cb->ai; ai != NULL; ai = ai->ai_next) {
232 cb->sock_fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
236 set_sock_opts(cb->sock_fd);
238 status = connect(cb->sock_fd, ai->ai_addr, ai->ai_addrlen);
248 if (cb->sock_fd < 0) {
249 ERROR("write_syslog plugin: Connecting to %s:%s failed. "
250 "The last error was: %s",
251 node, service, STRERRNO);
255 if (cb->connect_failed_log_enabled == 0) {
256 INFO("write_syslog plugin: Connecting to %s:%s succeeded.", node, service);
257 cb->connect_failed_log_enabled = 1;
259 cb->connect_dns_failed_attempts_remaining = 1;
266 static void ws_callback_free(void *data) {
267 struct ws_callback *cb;
274 pthread_mutex_lock(&cb->send_lock);
276 ws_flush_nolock(0, cb);
283 sfree(cb->host_tags);
284 sfree(cb->msg_format);
285 sfree(cb->metrics_prefix);
287 pthread_mutex_unlock(&cb->send_lock);
288 pthread_mutex_destroy(&cb->send_lock);
293 static int ws_flush(cdtime_t timeout,
294 const char *identifier __attribute__((unused)),
295 user_data_t *user_data) {
296 struct ws_callback *cb;
299 if (user_data == NULL)
302 cb = user_data->data;
304 pthread_mutex_lock(&cb->send_lock);
306 if (cb->sock_fd < 0) {
307 status = ws_callback_init(cb);
309 ERROR("write_syslog plugin: ws_callback_init failed.");
310 pthread_mutex_unlock(&cb->send_lock);
315 status = ws_flush_nolock(timeout, cb);
316 pthread_mutex_unlock(&cb->send_lock);
321 static int ws_format_values(char *ret, size_t ret_len, int ds_num,
322 const data_set_t *ds, const value_list_t *vl,
326 gauge_t *rates = NULL;
328 assert(strcmp(ds->type, vl->type) == 0);
330 memset(ret, 0, ret_len);
332 #define BUFFER_ADD(...) \
334 status = snprintf(ret + offset, ret_len - offset, __VA_ARGS__); \
338 } else if (((size_t)status) >= (ret_len - offset)) { \
342 offset += ((size_t)status); \
345 if (ds->ds[ds_num].type == DS_TYPE_GAUGE)
346 BUFFER_ADD(GAUGE_FORMAT, vl->values[ds_num].gauge);
347 else if (store_rates) {
349 rates = uc_get_rate(ds, vl);
351 WARNING("format_values: "
352 "uc_get_rate failed.");
355 BUFFER_ADD(GAUGE_FORMAT, rates[ds_num]);
356 } else if (ds->ds[ds_num].type == DS_TYPE_COUNTER)
357 BUFFER_ADD("%" PRIu64, (uint64_t)vl->values[ds_num].counter);
358 else if (ds->ds[ds_num].type == DS_TYPE_DERIVE)
359 BUFFER_ADD("%" PRIi64, vl->values[ds_num].derive);
360 else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE)
361 BUFFER_ADD("%" PRIu64, vl->values[ds_num].absolute);
363 ERROR("format_values plugin: Unknown data source type: %i",
364 ds->ds[ds_num].type);
375 static int ws_format_name(char *ret, int ret_len, const value_list_t *vl,
376 const struct ws_callback *cb, const char *ds_name) {
378 if (ds_name != NULL) {
379 snprintf(ret, ret_len, "%s.%s", vl->type, ds_name);
380 } else { /* ds_name == NULL */
381 snprintf(ret, ret_len, "%s", vl->type);
387 static int ws_send_message(const char *key, const char *value, cdtime_t time,
388 struct ws_callback *cb, const char *plugin,
389 const char *plugin_instance,
390 const char *type_instance, const char *type,
391 const char *ds_name, cdtime_t interval,
396 char rfc3339_timestamp[64];
397 const char *host_tags = cb->host_tags ? cb->host_tags : "";
398 const char *host_tags_json_prefix = "";
399 const char *metrics_prefix =
400 cb->metrics_prefix ? cb->metrics_prefix : WS_DEFAULT_PREFIX;
401 const char *msg_format = cb->msg_format ? cb->msg_format : WS_DEFAULT_FORMAT;
406 rfc3339_local(rfc3339_timestamp, sizeof(rfc3339_timestamp), time);
408 /* skip if value is NaN */
412 if (strcasecmp("JSON", msg_format) == 0) {
414 host_tags_json_prefix = ",";
417 /* The metric key-values are are part of the syslog msg, in json
419 message, sizeof(message),
420 "<166>1 %s %s collectd %d - - {\"time\":%.0f, \"%s\":{ \"%s\":{ "
422 "\"plugin\":\"%s\", \"plugin_instance\":\"%s\", "
423 "\"type_instance\":\"%s\","
424 " \"type\":\"%s\", \"interval\":%.0f }, \"hostname\":\"%s\" %s "
426 rfc3339_timestamp, host, pid, CDTIME_T_TO_DOUBLE(time), metrics_prefix,
427 plugin, key, value, plugin, plugin_instance, type_instance, type,
428 CDTIME_T_TO_DOUBLE(interval), host, host_tags_json_prefix, host_tags);
431 /* The metric key-values are part of the syslog structrude data,
432 * MessageFormat = "human" */
433 message, sizeof(message),
434 "<166>1 %s %s collectd %d - [%s value=\"%s\""
435 " plugin=\"%s\" plugin_instance=\"%s\""
436 " type_instance=\"%s\" type=\"%s\""
437 " ds_name=\"%s\" interval=\"%.0f\"] %s %s.%s=\"%s\"\n",
438 rfc3339_timestamp, host, pid, metrics_prefix, value, plugin,
439 plugin_instance, type_instance, type, ds_name,
440 CDTIME_T_TO_DOUBLE(interval), host_tags, plugin, key, value);
444 message_len = (size_t)status;
446 if (message_len >= sizeof(message)) {
447 ERROR("write_syslog plugin: message buffer too small: "
448 "Need %" PRIsz " bytes.",
453 pthread_mutex_lock(&cb->send_lock);
455 if (cb->sock_fd < 0) {
456 status = ws_callback_init(cb);
458 ERROR("write_syslog plugin: ws_callback_init failed.");
459 pthread_mutex_unlock(&cb->send_lock);
464 if (message_len >= cb->send_buf_free) {
465 status = ws_flush_nolock(0, cb);
467 pthread_mutex_unlock(&cb->send_lock);
472 /* Assert that we have enough space for this message. */
473 assert(message_len < cb->send_buf_free);
475 /* `message_len + 1' because `message_len' does not include the
476 * trailing null byte. Neither does `send_buffer_fill'. */
477 memcpy(cb->send_buf + cb->send_buf_fill, message, message_len + 1);
478 cb->send_buf_fill += message_len;
479 cb->send_buf_free -= message_len;
481 DEBUG("write_syslog plugin: [%s]:%s buf %" PRIsz "/%" PRIsz
483 cb->node, cb->service, cb->send_buf_fill, sizeof(cb->send_buf),
484 100.0 * ((double)cb->send_buf_fill) / ((double)sizeof(cb->send_buf)),
487 pthread_mutex_unlock(&cb->send_lock);
492 static int ws_write_messages(const data_set_t *ds, const value_list_t *vl,
493 struct ws_callback *cb) {
494 char key[10 * DATA_MAX_NAME_LEN];
499 if (0 != strcmp(ds->type, vl->type)) {
500 ERROR("write_syslog plugin: DS type does not match "
505 for (size_t i = 0; i < ds->ds_num; i++) {
506 const char *ds_name = NULL;
508 if (cb->always_append_ds || (ds->ds_num > 1))
509 ds_name = ds->ds[i].name;
511 /* Copy the identifier to 'key' and escape it. */
512 status = ws_format_name(key, sizeof(key), vl, cb, ds_name);
514 ERROR("write_syslog plugin: error with format_name");
518 escape_string(key, sizeof(key));
519 /* Convert the values to an ASCII representation and put that into
522 ws_format_values(values, sizeof(values), i, ds, vl, cb->store_rates);
524 ERROR("write_syslog plugin: error with "
529 /* Send the message to tcp */
530 status = ws_send_message(key, values, vl->time, cb, vl->plugin,
531 vl->plugin_instance, vl->type_instance, vl->type,
532 ds_name, vl->interval, vl->host);
534 ERROR("write_syslog plugin: error with "
543 static int ws_write(const data_set_t *ds, const value_list_t *vl,
544 user_data_t *user_data) {
545 struct ws_callback *cb;
548 if (user_data == NULL)
551 cb = user_data->data;
553 status = ws_write_messages(ds, vl, cb);
558 static int ws_config_tsd(oconfig_item_t *ci) {
559 struct ws_callback *cb;
560 char callback_name[DATA_MAX_NAME_LEN];
562 cb = calloc(1, sizeof(*cb));
564 ERROR("write_syslog plugin: calloc failed.");
568 cb->connect_failed_log_enabled = 1;
569 cb->next_random_ttl = new_random_ttl();
571 pthread_mutex_init(&cb->send_lock, NULL);
573 for (int i = 0; i < ci->children_num; i++) {
574 oconfig_item_t *child = ci->children + i;
576 if (strcasecmp("Host", child->key) == 0)
577 cf_util_get_string(child, &cb->node);
578 else if (strcasecmp("Port", child->key) == 0)
579 cf_util_get_service(child, &cb->service);
580 else if (strcasecmp("MessageFormat", child->key) == 0)
581 cf_util_get_string(child, &cb->msg_format);
582 else if (strcasecmp("HostTags", child->key) == 0)
583 cf_util_get_string(child, &cb->host_tags);
584 else if (strcasecmp("StoreRates", child->key) == 0)
585 cf_util_get_boolean(child, &cb->store_rates);
586 else if (strcasecmp("AlwaysAppendDS", child->key) == 0)
587 cf_util_get_boolean(child, &cb->always_append_ds);
588 else if (strcasecmp("Prefix", child->key) == 0)
589 cf_util_get_string(child, &cb->metrics_prefix);
591 ERROR("write_syslog plugin: Invalid configuration "
598 snprintf(callback_name, sizeof(callback_name), "write_syslog/%s/%s",
599 cb->node != NULL ? cb->node : WS_DEFAULT_NODE,
600 cb->service != NULL ? cb->service : WS_DEFAULT_SERVICE);
602 user_data_t user_data = {.data = cb, .free_func = ws_callback_free};
604 plugin_register_write(callback_name, ws_write, &user_data);
606 user_data.free_func = NULL;
607 plugin_register_flush(callback_name, ws_flush, &user_data);
612 static int ws_config(oconfig_item_t *ci) {
613 if ((resolve_interval == 0) && (resolve_jitter == 0))
614 resolve_interval = resolve_jitter = plugin_get_interval();
616 for (int i = 0; i < ci->children_num; i++) {
617 oconfig_item_t *child = ci->children + i;
619 if (strcasecmp("Node", child->key) == 0) {
620 if (ws_config_tsd(child) < 0)
622 } else if (strcasecmp("ResolveInterval", child->key) == 0)
623 cf_util_get_cdtime(child, &resolve_interval);
624 else if (strcasecmp("ResolveJitter", child->key) == 0)
625 cf_util_get_cdtime(child, &resolve_jitter);
627 ERROR("write_syslog plugin: Invalid configuration "
637 void module_register(void) {
638 plugin_register_complex_config("write_syslog", ws_config);