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/"PACKAGE_NAME".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);
65 ERROR("log_logstash: invalid loglevel [%s] defaulting to 'info'",
70 else if (0 == strcasecmp (key, "File")) {
72 log_file = strdup (value);
78 } /* int log_logstash_config (const char *, const char *) */
80 static void log_logstash_print (yajl_gen g, int severity,
81 cdtime_t timestamp_time)
85 struct tm timestamp_tm;
86 char timestamp_str[64];
87 const unsigned char *buf;
95 if (yajl_gen_string(g, (u_char *)"@level", strlen("@level")) !=
101 if (yajl_gen_string(g, (u_char *)"error", strlen("error")) !=
106 if (yajl_gen_string(g, (u_char *)"warning",
107 strlen("warning")) !=
112 if (yajl_gen_string(g, (u_char *)"notice", strlen("notice")) !=
117 if (yajl_gen_string(g, (u_char *)"info", strlen("info")) !=
122 if (yajl_gen_string(g, (u_char *)"debug", strlen("debug")) !=
127 if (yajl_gen_string(g, (u_char *)"unknown",
128 strlen("unknown")) !=
134 if (yajl_gen_string(g, (u_char *)"@timestamp", strlen("@timestamp")) !=
138 tt = CDTIME_T_TO_TIME_T (timestamp_time);
139 gmtime_r (&tt, ×tamp_tm);
142 * format time as a UTC ISO 8601 compliant string
144 strftime (timestamp_str, sizeof (timestamp_str),
145 "%Y-%m-%d %H:%M:%SZ", ×tamp_tm);
146 timestamp_str[sizeof (timestamp_str) - 1] = '\0';
148 if (yajl_gen_string(g, (u_char *)timestamp_str,
149 strlen(timestamp_str)) !=
153 if (yajl_gen_map_close(g) != yajl_gen_status_ok)
156 if (yajl_gen_get_buf(g, &buf, &len) != yajl_gen_status_ok)
158 pthread_mutex_lock (&file_lock);
160 if (log_file == NULL)
162 fh = fopen (DEFAULT_LOGFILE, "a");
164 } else if (strcasecmp(log_file, "stdout") == 0) {
167 } else if (strcasecmp(log_file, "stderr") == 0) {
171 fh = fopen (log_file, "a");
178 fprintf (stderr, "log_logstash plugin: fopen (%s) failed: %s\n",
179 (log_file == NULL) ? DEFAULT_LOGFILE : log_file,
180 sstrerror (errno, errbuf, sizeof (errbuf)));
184 fprintf(fh, "%s\n", buf);
191 pthread_mutex_unlock (&file_lock);
197 fprintf(stderr, "Could not correctly generate JSON message\n");
199 } /* void log_logstash_print */
201 static void log_logstash_log (int severity, const char *msg,
202 user_data_t __attribute__((unused)) *user_data)
205 #if !defined(HAVE_YAJL_V2)
206 yajl_gen_config conf;
211 if (severity > log_level)
215 g = yajl_gen_alloc(NULL);
217 g = yajl_gen_alloc(&conf, NULL);
221 fprintf(stderr, "Could not allocate JSON generator.\n");
225 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
227 if (yajl_gen_string(g, (u_char *)"@message", strlen("@message")) !=
230 if (yajl_gen_string(g, (u_char *)msg, strlen(msg)) !=
234 log_logstash_print (g, severity, cdtime ());
238 fprintf(stderr, "Could not generate JSON message preamble\n");
241 } /* void log_logstash_log (int, const char *) */
243 static int log_logstash_notification (const notification_t *n,
244 user_data_t __attribute__((unused)) *user_data)
248 g = yajl_gen_alloc(NULL);
250 yajl_gen_config conf;
253 g = yajl_gen_alloc(&conf, NULL);
257 fprintf(stderr, "Could not allocate JSON generator.\n");
261 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
263 if (yajl_gen_string(g, (u_char *)"@message", strlen("@message")) !=
266 if (strlen(n->message) > 0) {
267 if (yajl_gen_string(g, (u_char *)n->message,
268 strlen(n->message)) !=
272 if (yajl_gen_string(g, (u_char *)"notification without a message",
273 strlen("notification without a message")) !=
279 if (yajl_gen_string(g, (u_char *)"@fields", strlen("@fields")) !=
282 if (yajl_gen_map_open(g) !=
286 if (strlen(n->host) > 0) {
287 if (yajl_gen_string(g, (u_char *)"host", strlen("host")) !=
290 if (yajl_gen_string(g, (u_char *)n->host, strlen(n->host)) !=
295 if (strlen(n->plugin) > 0) {
296 if (yajl_gen_string(g, (u_char *)"plugin", strlen("plugin")) !=
299 if (yajl_gen_string(g, (u_char *)n->plugin, strlen(n->plugin)) !=
303 if (strlen(n->plugin_instance) > 0) {
304 if (yajl_gen_string(g, (u_char *)"plugin_instance",
305 strlen("plugin_instance")) !=
308 if (yajl_gen_string(g, (u_char *)n->plugin_instance,
309 strlen(n->plugin_instance)) !=
313 if (strlen(n->type) > 0) {
314 if (yajl_gen_string(g, (u_char *)"type", strlen("type")) !=
317 if (yajl_gen_string(g, (u_char *)n->type, strlen(n->type)) !=
321 if (strlen(n->type_instance) > 0) {
322 if (yajl_gen_string(g, (u_char *)"type_instance",
323 strlen("type_instance")) !=
326 if (yajl_gen_string(g, (u_char *)n->type_instance,
327 strlen(n->type_instance)) !=
332 if (yajl_gen_string(g, (u_char *)"severity",
333 strlen("severity")) !=
337 switch (n->severity) {
339 if (yajl_gen_string(g, (u_char *)"failure",
340 strlen("failure")) !=
345 if (yajl_gen_string(g, (u_char *)"warning",
346 strlen("warning")) !=
351 if (yajl_gen_string(g, (u_char *)"ok",
357 if (yajl_gen_string(g, (u_char *)"unknown",
358 strlen("unknown")) !=
363 if (yajl_gen_map_close(g) != yajl_gen_status_ok)
366 log_logstash_print (g, LOG_INFO, (n->time != 0) ? n->time : cdtime ());
371 fprintf(stderr, "Could not correctly generate JSON notification\n");
373 } /* int log_logstash_notification */
375 void module_register (void)
377 plugin_register_config ("log_logstash",
381 plugin_register_log ("log_logstash",
383 /* user_data = */ NULL);
384 plugin_register_notification ("log_logstash",
385 log_logstash_notification,
386 /* user_data = */ NULL);
387 } /* void module_register (void) */
389 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */