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;
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) {
78 bool do_close = false;
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");
163 fprintf(stderr, "log_logstash plugin: fopen (%s) failed: %s\n", log_file,
166 fprintf(fh, "%s\n", buf);
173 pthread_mutex_unlock(&file_lock);
179 fprintf(stderr, "Could not correctly generate JSON message\n");
181 } /* void log_logstash_print */
183 static void log_logstash_log(int severity, const char *msg,
184 user_data_t __attribute__((unused)) * user_data) {
186 #if !defined(HAVE_YAJL_V2)
187 yajl_gen_config conf = {};
192 if (severity > log_level)
196 g = yajl_gen_alloc(NULL);
198 g = yajl_gen_alloc(&conf, NULL);
202 fprintf(stderr, "Could not allocate JSON generator.\n");
206 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
208 if (yajl_gen_string(g, (u_char *)"message", strlen("message")) !=
211 if (yajl_gen_string(g, (u_char *)msg, strlen(msg)) != yajl_gen_status_ok)
214 log_logstash_print(g, severity, cdtime());
218 fprintf(stderr, "Could not generate JSON message preamble\n");
221 } /* void log_logstash_log (int, const char *) */
223 static int log_logstash_notification(const notification_t *n,
224 user_data_t __attribute__((unused)) *
228 g = yajl_gen_alloc(NULL);
230 yajl_gen_config conf = {};
233 g = yajl_gen_alloc(&conf, NULL);
237 fprintf(stderr, "Could not allocate JSON generator.\n");
241 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
243 if (yajl_gen_string(g, (u_char *)"message", strlen("message")) !=
246 if (strlen(n->message) > 0) {
247 if (yajl_gen_string(g, (u_char *)n->message, strlen(n->message)) !=
251 if (yajl_gen_string(g, (u_char *)"notification without a message",
252 strlen("notification without a message")) !=
257 if (strlen(n->host) > 0) {
258 if (yajl_gen_string(g, (u_char *)"host", strlen("host")) !=
261 if (yajl_gen_string(g, (u_char *)n->host, strlen(n->host)) !=
265 if (strlen(n->plugin) > 0) {
266 if (yajl_gen_string(g, (u_char *)"plugin", strlen("plugin")) !=
269 if (yajl_gen_string(g, (u_char *)n->plugin, strlen(n->plugin)) !=
273 if (strlen(n->plugin_instance) > 0) {
274 if (yajl_gen_string(g, (u_char *)"plugin_instance",
275 strlen("plugin_instance")) != yajl_gen_status_ok)
277 if (yajl_gen_string(g, (u_char *)n->plugin_instance,
278 strlen(n->plugin_instance)) != yajl_gen_status_ok)
281 if (strlen(n->type) > 0) {
282 if (yajl_gen_string(g, (u_char *)"type", strlen("type")) !=
285 if (yajl_gen_string(g, (u_char *)n->type, strlen(n->type)) !=
289 if (strlen(n->type_instance) > 0) {
290 if (yajl_gen_string(g, (u_char *)"type_instance",
291 strlen("type_instance")) != yajl_gen_status_ok)
293 if (yajl_gen_string(g, (u_char *)n->type_instance,
294 strlen(n->type_instance)) != yajl_gen_status_ok)
298 if (yajl_gen_string(g, (u_char *)"severity", strlen("severity")) !=
302 switch (n->severity) {
304 if (yajl_gen_string(g, (u_char *)"failure", strlen("failure")) !=
309 if (yajl_gen_string(g, (u_char *)"warning", strlen("warning")) !=
314 if (yajl_gen_string(g, (u_char *)"ok", strlen("ok")) != yajl_gen_status_ok)
318 if (yajl_gen_string(g, (u_char *)"unknown", strlen("unknown")) !=
324 log_logstash_print(g, LOG_INFO, (n->time != 0) ? n->time : cdtime());
329 fprintf(stderr, "Could not correctly generate JSON notification\n");
331 } /* int log_logstash_notification */
333 void module_register(void) {
334 plugin_register_config("log_logstash", log_logstash_config, config_keys,
336 plugin_register_log("log_logstash", log_logstash_log,
337 /* user_data = */ NULL);
338 plugin_register_notification("log_logstash", log_logstash_notification,
339 /* user_data = */ NULL);
340 } /* void module_register (void) */