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
33 #include <sys/types.h>
34 #include <yajl/yajl_common.h>
35 #include <yajl/yajl_gen.h>
36 #if HAVE_YAJL_YAJL_VERSION_H
37 # include <yajl/yajl_version.h>
39 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
40 # define HAVE_YAJL_V2 1
43 #define DEFAULT_LOGFILE LOCALSTATEDIR"/log/"PACKAGE_NAME".json.log"
46 static int log_level = LOG_DEBUG;
48 static int log_level = LOG_INFO;
49 #endif /* COLLECT_DEBUG */
51 static pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
53 static char *log_file = NULL;
55 static const char *config_keys[] =
60 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
62 static int log_logstash_config (const char *key, const char *value)
65 if (0 == strcasecmp (key, "LogLevel")) {
66 log_level = parse_log_severity(value);
69 ERROR("log_logstash: invalid loglevel [%s] defaulting to 'info'",
74 else if (0 == strcasecmp (key, "File")) {
76 log_file = strdup (value);
82 } /* int log_logstash_config (const char *, const char *) */
84 static void log_logstash_print (yajl_gen g, int severity,
85 cdtime_t timestamp_time)
89 struct tm timestamp_tm;
90 char timestamp_str[64];
91 const unsigned char *buf;
99 if (yajl_gen_string(g, (u_char *)"level", strlen("level")) !=
105 if (yajl_gen_string(g, (u_char *)"error", strlen("error")) !=
110 if (yajl_gen_string(g, (u_char *)"warning",
111 strlen("warning")) !=
116 if (yajl_gen_string(g, (u_char *)"notice", strlen("notice")) !=
121 if (yajl_gen_string(g, (u_char *)"info", strlen("info")) !=
126 if (yajl_gen_string(g, (u_char *)"debug", strlen("debug")) !=
131 if (yajl_gen_string(g, (u_char *)"unknown",
132 strlen("unknown")) !=
138 if (yajl_gen_string(g, (u_char *)"@timestamp", strlen("@timestamp")) !=
142 tt = CDTIME_T_TO_TIME_T (timestamp_time);
143 gmtime_r (&tt, ×tamp_tm);
146 * format time as a UTC ISO 8601 compliant string
148 strftime (timestamp_str, sizeof (timestamp_str),
149 "%Y-%m-%dT%H:%M:%SZ", ×tamp_tm);
150 timestamp_str[sizeof (timestamp_str) - 1] = '\0';
152 if (yajl_gen_string(g, (u_char *)timestamp_str,
153 strlen(timestamp_str)) !=
157 if (yajl_gen_map_close(g) != yajl_gen_status_ok)
160 if (yajl_gen_get_buf(g, &buf, &len) != yajl_gen_status_ok)
162 pthread_mutex_lock (&file_lock);
164 if (log_file == NULL)
166 fh = fopen (DEFAULT_LOGFILE, "a");
168 } else if (strcasecmp(log_file, "stdout") == 0) {
171 } else if (strcasecmp(log_file, "stderr") == 0) {
175 fh = fopen (log_file, "a");
182 fprintf (stderr, "log_logstash plugin: fopen (%s) failed: %s\n",
183 (log_file == NULL) ? DEFAULT_LOGFILE : log_file,
184 sstrerror (errno, errbuf, sizeof (errbuf)));
188 fprintf(fh, "%s\n", buf);
195 pthread_mutex_unlock (&file_lock);
201 fprintf(stderr, "Could not correctly generate JSON message\n");
203 } /* void log_logstash_print */
205 static void log_logstash_log (int severity, const char *msg,
206 user_data_t __attribute__((unused)) *user_data)
209 #if !defined(HAVE_YAJL_V2)
210 yajl_gen_config conf = {};
215 if (severity > log_level)
219 g = yajl_gen_alloc(NULL);
221 g = yajl_gen_alloc(&conf, NULL);
225 fprintf(stderr, "Could not allocate JSON generator.\n");
229 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
231 if (yajl_gen_string(g, (u_char *)"message", strlen("message")) !=
234 if (yajl_gen_string(g, (u_char *)msg, strlen(msg)) !=
238 log_logstash_print (g, severity, cdtime ());
242 fprintf(stderr, "Could not generate JSON message preamble\n");
245 } /* void log_logstash_log (int, const char *) */
247 static int log_logstash_notification (const notification_t *n,
248 user_data_t __attribute__((unused)) *user_data)
252 g = yajl_gen_alloc(NULL);
254 yajl_gen_config conf = {};
257 g = yajl_gen_alloc(&conf, NULL);
261 fprintf(stderr, "Could not allocate JSON generator.\n");
265 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
267 if (yajl_gen_string(g, (u_char *)"message", strlen("message")) !=
270 if (strlen(n->message) > 0) {
271 if (yajl_gen_string(g, (u_char *)n->message,
272 strlen(n->message)) !=
276 if (yajl_gen_string(g, (u_char *)"notification without a message",
277 strlen("notification without a message")) !=
282 if (strlen(n->host) > 0) {
283 if (yajl_gen_string(g, (u_char *)"host", strlen("host")) !=
286 if (yajl_gen_string(g, (u_char *)n->host, strlen(n->host)) !=
291 if (strlen(n->plugin) > 0) {
292 if (yajl_gen_string(g, (u_char *)"plugin", strlen("plugin")) !=
295 if (yajl_gen_string(g, (u_char *)n->plugin, strlen(n->plugin)) !=
299 if (strlen(n->plugin_instance) > 0) {
300 if (yajl_gen_string(g, (u_char *)"plugin_instance",
301 strlen("plugin_instance")) !=
304 if (yajl_gen_string(g, (u_char *)n->plugin_instance,
305 strlen(n->plugin_instance)) !=
309 if (strlen(n->type) > 0) {
310 if (yajl_gen_string(g, (u_char *)"type", strlen("type")) !=
313 if (yajl_gen_string(g, (u_char *)n->type, strlen(n->type)) !=
317 if (strlen(n->type_instance) > 0) {
318 if (yajl_gen_string(g, (u_char *)"type_instance",
319 strlen("type_instance")) !=
322 if (yajl_gen_string(g, (u_char *)n->type_instance,
323 strlen(n->type_instance)) !=
328 if (yajl_gen_string(g, (u_char *)"severity",
329 strlen("severity")) !=
333 switch (n->severity) {
335 if (yajl_gen_string(g, (u_char *)"failure",
336 strlen("failure")) !=
341 if (yajl_gen_string(g, (u_char *)"warning",
342 strlen("warning")) !=
347 if (yajl_gen_string(g, (u_char *)"ok",
353 if (yajl_gen_string(g, (u_char *)"unknown",
354 strlen("unknown")) !=
360 log_logstash_print (g, LOG_INFO, (n->time != 0) ? n->time : cdtime ());
365 fprintf(stderr, "Could not correctly generate JSON notification\n");
367 } /* int log_logstash_notification */
369 void module_register (void)
371 plugin_register_config ("log_logstash",
375 plugin_register_log ("log_logstash",
377 /* user_data = */ NULL);
378 plugin_register_notification ("log_logstash",
379 log_logstash_notification,
380 /* user_data = */ NULL);
381 } /* void module_register (void) */
383 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */