2 * collectd - src/log_logstash.c
3 * Copyright (C) 2013 Pierre-Yves Ritschard
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Pierre-Yves Ritschard <pyr at spootnik.org>
21 * This file is largely inspired by logfile.c
28 #include <sys/types.h>
30 #include <yajl/yajl_common.h>
31 #include <yajl/yajl_gen.h>
32 #if HAVE_YAJL_YAJL_VERSION_H
33 # include <yajl/yajl_version.h>
35 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
36 # define HAVE_YAJL_V2 1
39 #define DEFAULT_LOGFILE LOCALSTATEDIR"/log/collectd.json.log"
42 static int log_level = LOG_DEBUG;
44 static int log_level = LOG_INFO;
45 #endif /* COLLECT_DEBUG */
47 static pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
49 static char *log_file = NULL;
51 static const char *config_keys[] =
56 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
58 static int log_logstash_config (const char *key, const char *value)
61 if (0 == strcasecmp (key, "LogLevel")) {
62 log_level = parse_log_severity(value);
63 printf("parsed log level: %d\n", log_level);
64 if (log_level == -1) return 1; /* to keep previous behaviour */
66 else if (0 == strcasecmp (key, "File")) {
68 log_file = strdup (value);
74 } /* int log_logstash_config (const char *, const char *) */
76 static void log_logstash_print (yajl_gen g, int severity,
77 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",
103 strlen("warning")) !=
108 if (yajl_gen_string(g, (u_char *)"notice", strlen("notice")) !=
113 if (yajl_gen_string(g, (u_char *)"info", strlen("info")) !=
118 if (yajl_gen_string(g, (u_char *)"debug", strlen("debug")) !=
123 if (yajl_gen_string(g, (u_char *)"unknown",
124 strlen("unknown")) !=
130 if (yajl_gen_string(g, (u_char *)"@timestamp", strlen("@timestamp")) !=
134 tt = CDTIME_T_TO_TIME_T (timestamp_time);
135 gmtime_r (&tt, ×tamp_tm);
138 * format time as a UTC ISO 8601 compliant string
140 strftime (timestamp_str, sizeof (timestamp_str),
141 "%Y-%m-%d %H:%M:%SZ", ×tamp_tm);
142 timestamp_str[sizeof (timestamp_str) - 1] = '\0';
144 if (yajl_gen_string(g, (u_char *)timestamp_str,
145 strlen(timestamp_str)) !=
149 if (yajl_gen_map_close(g) != yajl_gen_status_ok)
152 if (yajl_gen_get_buf(g, &buf, &len) != yajl_gen_status_ok)
154 pthread_mutex_lock (&file_lock);
156 if (log_file == NULL)
158 fh = fopen (DEFAULT_LOGFILE, "a");
160 } else if (strcasecmp(log_file, "stdout") == 0) {
163 } else if (strcasecmp(log_file, "stderr") == 0) {
167 fh = fopen (log_file, "a");
174 fprintf (stderr, "log_logstash plugin: fopen (%s) failed: %s\n",
175 (log_file == NULL) ? DEFAULT_LOGFILE : log_file,
176 sstrerror (errno, errbuf, sizeof (errbuf)));
180 fprintf(fh, "%s\n", buf);
187 pthread_mutex_unlock (&file_lock);
193 fprintf(stderr, "Could not correctly generate JSON message\n");
195 } /* void log_logstash_print */
197 static void log_logstash_log (int severity, const char *msg,
198 user_data_t __attribute__((unused)) *user_data)
201 #if !defined(HAVE_YAJL_V2)
202 yajl_gen_config conf;
207 if (severity > log_level)
211 g = yajl_gen_alloc(NULL);
213 g = yajl_gen_alloc(&conf, NULL);
217 fprintf(stderr, "Could not allocate JSON generator.\n");
221 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
223 if (yajl_gen_string(g, (u_char *)"@message", strlen("@message")) !=
226 if (yajl_gen_string(g, (u_char *)msg, strlen(msg)) !=
230 log_logstash_print (g, severity, cdtime ());
234 fprintf(stderr, "Could not generate JSON message preamble\n");
237 } /* void log_logstash_log (int, const char *) */
239 static int log_logstash_notification (const notification_t *n,
240 user_data_t __attribute__((unused)) *user_data)
244 g = yajl_gen_alloc(NULL);
246 yajl_gen_config conf;
249 g = yajl_gen_alloc(&conf, NULL);
253 fprintf(stderr, "Could not allocate JSON generator.\n");
257 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
259 if (yajl_gen_string(g, (u_char *)"@message", strlen("@message")) !=
262 if (strlen(n->message) > 0) {
263 if (yajl_gen_string(g, (u_char *)n->message,
264 strlen(n->message)) !=
268 if (yajl_gen_string(g, (u_char *)"notification without a message",
269 strlen("notification without a message")) !=
275 if (yajl_gen_string(g, (u_char *)"@fields", strlen("@fields")) !=
278 if (yajl_gen_map_open(g) !=
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")) !=
359 if (yajl_gen_map_close(g) != yajl_gen_status_ok)
362 log_logstash_print (g, LOG_INFO, (n->time != 0) ? n->time : cdtime ());
367 fprintf(stderr, "Could not correctly generate JSON notification\n");
369 } /* int log_logstash_notification */
371 void module_register (void)
373 plugin_register_config ("log_logstash",
377 plugin_register_log ("log_logstash",
379 /* user_data = */ NULL);
380 plugin_register_notification ("log_logstash",
381 log_logstash_notification,
382 /* user_data = */ NULL);
383 } /* void module_register (void) */
385 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */