2 * collectd - src/log_logstash.c
3 * Copyright (C) 2013 Pierre-Yves Ritschard
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Pierre-Yves Ritschard <pyr at spootnik.org>
26 * This file is largely inspired by logfile.c
34 #include <sys/types.h>
35 #include <yajl/yajl_common.h>
36 #include <yajl/yajl_gen.h>
37 #if HAVE_YAJL_YAJL_VERSION_H
38 #include <yajl/yajl_version.h>
40 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
41 #define HAVE_YAJL_V2 1
44 #define DEFAULT_LOGFILE LOCALSTATEDIR "/log/" PACKAGE_NAME ".json.log"
47 static int log_level = LOG_DEBUG;
49 static int log_level = LOG_INFO;
50 #endif /* COLLECT_DEBUG */
52 static pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
54 static char *log_file = NULL;
56 static const char *config_keys[] = {"LogLevel", "File"};
57 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
59 static int log_logstash_config(const char *key, const char *value) {
61 if (0 == strcasecmp(key, "LogLevel")) {
62 log_level = parse_log_severity(value);
65 ERROR("log_logstash: invalid loglevel [%s] defaulting to 'info'", value);
68 } else if (0 == strcasecmp(key, "File")) {
70 log_file = strdup(value);
75 } /* int log_logstash_config (const char *, const char *) */
77 static void log_logstash_print(yajl_gen g, int severity,
78 cdtime_t timestamp_time) {
81 struct tm timestamp_tm;
82 char timestamp_str[64];
83 const unsigned char *buf;
91 if (yajl_gen_string(g, (u_char *)"level", strlen("level")) !=
97 if (yajl_gen_string(g, (u_char *)"error", strlen("error")) !=
102 if (yajl_gen_string(g, (u_char *)"warning", strlen("warning")) !=
107 if (yajl_gen_string(g, (u_char *)"notice", strlen("notice")) !=
112 if (yajl_gen_string(g, (u_char *)"info", strlen("info")) !=
117 if (yajl_gen_string(g, (u_char *)"debug", strlen("debug")) !=
122 if (yajl_gen_string(g, (u_char *)"unknown", strlen("unknown")) !=
128 if (yajl_gen_string(g, (u_char *)"@timestamp", strlen("@timestamp")) !=
132 tt = CDTIME_T_TO_TIME_T(timestamp_time);
133 gmtime_r(&tt, ×tamp_tm);
136 * format time as a UTC ISO 8601 compliant string
138 strftime(timestamp_str, sizeof(timestamp_str), "%Y-%m-%dT%H:%M:%SZ",
140 timestamp_str[sizeof(timestamp_str) - 1] = '\0';
142 if (yajl_gen_string(g, (u_char *)timestamp_str, strlen(timestamp_str)) !=
146 if (yajl_gen_map_close(g) != yajl_gen_status_ok)
149 if (yajl_gen_get_buf(g, &buf, &len) != yajl_gen_status_ok)
151 pthread_mutex_lock(&file_lock);
153 if (log_file == NULL) {
154 fh = fopen(DEFAULT_LOGFILE, "a");
156 } else if (strcasecmp(log_file, "stdout") == 0) {
159 } else if (strcasecmp(log_file, "stderr") == 0) {
163 fh = fopen(log_file, "a");
169 fprintf(stderr, "log_logstash plugin: fopen (%s) failed: %s\n",
170 (log_file == NULL) ? DEFAULT_LOGFILE : log_file,
171 sstrerror(errno, errbuf, sizeof(errbuf)));
173 fprintf(fh, "%s\n", buf);
180 pthread_mutex_unlock(&file_lock);
186 fprintf(stderr, "Could not correctly generate JSON message\n");
188 } /* void log_logstash_print */
190 static void log_logstash_log(int severity, const char *msg,
191 user_data_t __attribute__((unused)) * user_data) {
193 #if !defined(HAVE_YAJL_V2)
194 yajl_gen_config conf = {};
199 if (severity > log_level)
203 g = yajl_gen_alloc(NULL);
205 g = yajl_gen_alloc(&conf, NULL);
209 fprintf(stderr, "Could not allocate JSON generator.\n");
213 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
215 if (yajl_gen_string(g, (u_char *)"message", strlen("message")) !=
218 if (yajl_gen_string(g, (u_char *)msg, strlen(msg)) != yajl_gen_status_ok)
221 log_logstash_print(g, severity, cdtime());
225 fprintf(stderr, "Could not generate JSON message preamble\n");
228 } /* void log_logstash_log (int, const char *) */
230 static int log_logstash_notification(const notification_t *n,
231 user_data_t __attribute__((unused)) *
235 g = yajl_gen_alloc(NULL);
237 yajl_gen_config conf = {};
240 g = yajl_gen_alloc(&conf, NULL);
244 fprintf(stderr, "Could not allocate JSON generator.\n");
248 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
250 if (yajl_gen_string(g, (u_char *)"message", strlen("message")) !=
253 if (strlen(n->message) > 0) {
254 if (yajl_gen_string(g, (u_char *)n->message, strlen(n->message)) !=
258 if (yajl_gen_string(g, (u_char *)"notification without a message",
259 strlen("notification without a message")) !=
264 if (strlen(n->host) > 0) {
265 if (yajl_gen_string(g, (u_char *)"host", strlen("host")) !=
268 if (yajl_gen_string(g, (u_char *)n->host, strlen(n->host)) !=
272 if (strlen(n->plugin) > 0) {
273 if (yajl_gen_string(g, (u_char *)"plugin", strlen("plugin")) !=
276 if (yajl_gen_string(g, (u_char *)n->plugin, strlen(n->plugin)) !=
280 if (strlen(n->plugin_instance) > 0) {
281 if (yajl_gen_string(g, (u_char *)"plugin_instance",
282 strlen("plugin_instance")) != yajl_gen_status_ok)
284 if (yajl_gen_string(g, (u_char *)n->plugin_instance,
285 strlen(n->plugin_instance)) != yajl_gen_status_ok)
288 if (strlen(n->type) > 0) {
289 if (yajl_gen_string(g, (u_char *)"type", strlen("type")) !=
292 if (yajl_gen_string(g, (u_char *)n->type, strlen(n->type)) !=
296 if (strlen(n->type_instance) > 0) {
297 if (yajl_gen_string(g, (u_char *)"type_instance",
298 strlen("type_instance")) != yajl_gen_status_ok)
300 if (yajl_gen_string(g, (u_char *)n->type_instance,
301 strlen(n->type_instance)) != yajl_gen_status_ok)
305 if (yajl_gen_string(g, (u_char *)"severity", strlen("severity")) !=
309 switch (n->severity) {
311 if (yajl_gen_string(g, (u_char *)"failure", strlen("failure")) !=
316 if (yajl_gen_string(g, (u_char *)"warning", strlen("warning")) !=
321 if (yajl_gen_string(g, (u_char *)"ok", strlen("ok")) != yajl_gen_status_ok)
325 if (yajl_gen_string(g, (u_char *)"unknown", strlen("unknown")) !=
331 log_logstash_print(g, LOG_INFO, (n->time != 0) ? n->time : cdtime());
336 fprintf(stderr, "Could not correctly generate JSON notification\n");
338 } /* int log_logstash_notification */
340 void module_register(void) {
341 plugin_register_config("log_logstash", log_logstash_config, config_keys,
343 plugin_register_log("log_logstash", log_logstash_log,
344 /* user_data = */ NULL);
345 plugin_register_notification("log_logstash", log_logstash_notification,
346 /* user_data = */ NULL);
347 } /* void module_register (void) */
349 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */