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
45 static int log_level = LOG_DEBUG;
47 static int log_level = LOG_INFO;
48 #endif /* COLLECT_DEBUG */
50 static pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
52 static char *log_file = NULL;
54 static const char *config_keys[] = {"LogLevel", "File"};
55 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
57 static int log_logstash_config(const char *key, const char *value) {
59 if (0 == strcasecmp(key, "LogLevel")) {
60 log_level = parse_log_severity(value);
63 ERROR("log_logstash: invalid loglevel [%s] defaulting to 'info'", value);
66 } else if (0 == strcasecmp(key, "File")) {
68 log_file = strdup(value);
73 } /* int log_logstash_config (const char *, const char *) */
75 static void log_logstash_print(yajl_gen g, int severity,
76 cdtime_t timestamp_time) {
79 struct tm timestamp_tm;
80 char timestamp_str[64];
81 const unsigned char *buf;
88 if (yajl_gen_string(g, (u_char *)"level", strlen("level")) !=
94 if (yajl_gen_string(g, (u_char *)"error", strlen("error")) !=
99 if (yajl_gen_string(g, (u_char *)"warning", strlen("warning")) !=
104 if (yajl_gen_string(g, (u_char *)"notice", strlen("notice")) !=
109 if (yajl_gen_string(g, (u_char *)"info", strlen("info")) !=
114 if (yajl_gen_string(g, (u_char *)"debug", strlen("debug")) !=
119 if (yajl_gen_string(g, (u_char *)"unknown", strlen("unknown")) !=
125 if (yajl_gen_string(g, (u_char *)"@timestamp", strlen("@timestamp")) !=
129 gmtime_r(&CDTIME_T_TO_TIME_T(timestamp_time), ×tamp_tm);
132 * format time as a UTC ISO 8601 compliant string
134 strftime(timestamp_str, sizeof(timestamp_str), "%Y-%m-%dT%H:%M:%SZ",
136 timestamp_str[sizeof(timestamp_str) - 1] = '\0';
138 if (yajl_gen_string(g, (u_char *)timestamp_str, strlen(timestamp_str)) !=
142 if (yajl_gen_map_close(g) != yajl_gen_status_ok)
145 if (yajl_gen_get_buf(g, &buf, &len) != yajl_gen_status_ok)
147 pthread_mutex_lock(&file_lock);
149 if (log_file == NULL) {
151 } else if (strcasecmp(log_file, "stdout") == 0) {
154 } else if (strcasecmp(log_file, "stderr") == 0) {
158 fh = fopen(log_file, "a");
164 fprintf(stderr, "log_logstash plugin: fopen (%s) failed: %s\n", log_file,
165 sstrerror(errno, errbuf, sizeof(errbuf)));
167 fprintf(fh, "%s\n", buf);
174 pthread_mutex_unlock(&file_lock);
180 fprintf(stderr, "Could not correctly generate JSON message\n");
182 } /* void log_logstash_print */
184 static void log_logstash_log(int severity, const char *msg,
185 user_data_t __attribute__((unused)) * user_data) {
187 #if !defined(HAVE_YAJL_V2)
188 yajl_gen_config conf = {};
193 if (severity > log_level)
197 g = yajl_gen_alloc(NULL);
199 g = yajl_gen_alloc(&conf, NULL);
203 fprintf(stderr, "Could not allocate JSON generator.\n");
207 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
209 if (yajl_gen_string(g, (u_char *)"message", strlen("message")) !=
212 if (yajl_gen_string(g, (u_char *)msg, strlen(msg)) != yajl_gen_status_ok)
215 log_logstash_print(g, severity, cdtime());
219 fprintf(stderr, "Could not generate JSON message preamble\n");
222 } /* void log_logstash_log (int, const char *) */
224 static int log_logstash_notification(const notification_t *n,
225 user_data_t __attribute__((unused)) *
229 g = yajl_gen_alloc(NULL);
231 yajl_gen_config conf = {};
234 g = yajl_gen_alloc(&conf, NULL);
238 fprintf(stderr, "Could not allocate JSON generator.\n");
242 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
244 if (yajl_gen_string(g, (u_char *)"message", strlen("message")) !=
247 if (strlen(n->message) > 0) {
248 if (yajl_gen_string(g, (u_char *)n->message, strlen(n->message)) !=
252 if (yajl_gen_string(g, (u_char *)"notification without a message",
253 strlen("notification without a message")) !=
258 if (strlen(n->host) > 0) {
259 if (yajl_gen_string(g, (u_char *)"host", strlen("host")) !=
262 if (yajl_gen_string(g, (u_char *)n->host, strlen(n->host)) !=
266 if (strlen(n->plugin) > 0) {
267 if (yajl_gen_string(g, (u_char *)"plugin", strlen("plugin")) !=
270 if (yajl_gen_string(g, (u_char *)n->plugin, strlen(n->plugin)) !=
274 if (strlen(n->plugin_instance) > 0) {
275 if (yajl_gen_string(g, (u_char *)"plugin_instance",
276 strlen("plugin_instance")) != yajl_gen_status_ok)
278 if (yajl_gen_string(g, (u_char *)n->plugin_instance,
279 strlen(n->plugin_instance)) != yajl_gen_status_ok)
282 if (strlen(n->type) > 0) {
283 if (yajl_gen_string(g, (u_char *)"type", strlen("type")) !=
286 if (yajl_gen_string(g, (u_char *)n->type, strlen(n->type)) !=
290 if (strlen(n->type_instance) > 0) {
291 if (yajl_gen_string(g, (u_char *)"type_instance",
292 strlen("type_instance")) != yajl_gen_status_ok)
294 if (yajl_gen_string(g, (u_char *)n->type_instance,
295 strlen(n->type_instance)) != yajl_gen_status_ok)
299 if (yajl_gen_string(g, (u_char *)"severity", strlen("severity")) !=
303 switch (n->severity) {
305 if (yajl_gen_string(g, (u_char *)"failure", strlen("failure")) !=
310 if (yajl_gen_string(g, (u_char *)"warning", strlen("warning")) !=
315 if (yajl_gen_string(g, (u_char *)"ok", strlen("ok")) != yajl_gen_status_ok)
319 if (yajl_gen_string(g, (u_char *)"unknown", strlen("unknown")) !=
325 log_logstash_print(g, LOG_INFO, (n->time != 0) ? n->time : cdtime());
330 fprintf(stderr, "Could not correctly generate JSON notification\n");
332 } /* int log_logstash_notification */
334 void module_register(void) {
335 plugin_register_config("log_logstash", log_logstash_config, config_keys,
337 plugin_register_log("log_logstash", log_logstash_log,
338 /* user_data = */ NULL);
339 plugin_register_notification("log_logstash", log_logstash_notification,
340 /* user_data = */ NULL);
341 } /* void module_register (void) */