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);
66 ERROR("log_logstash: invalid loglevel [%s] defaulting to 'info'",
71 else if (0 == strcasecmp (key, "File")) {
73 log_file = strdup (value);
79 } /* int log_logstash_config (const char *, const char *) */
81 static void log_logstash_print (yajl_gen g, int severity,
82 cdtime_t timestamp_time)
86 struct tm timestamp_tm;
87 char timestamp_str[64];
88 const unsigned char *buf;
96 if (yajl_gen_string(g, (u_char *)"@level", strlen("@level")) !=
102 if (yajl_gen_string(g, (u_char *)"error", strlen("error")) !=
107 if (yajl_gen_string(g, (u_char *)"warning",
108 strlen("warning")) !=
113 if (yajl_gen_string(g, (u_char *)"notice", strlen("notice")) !=
118 if (yajl_gen_string(g, (u_char *)"info", strlen("info")) !=
123 if (yajl_gen_string(g, (u_char *)"debug", strlen("debug")) !=
128 if (yajl_gen_string(g, (u_char *)"unknown",
129 strlen("unknown")) !=
135 if (yajl_gen_string(g, (u_char *)"@timestamp", strlen("@timestamp")) !=
139 tt = CDTIME_T_TO_TIME_T (timestamp_time);
140 gmtime_r (&tt, ×tamp_tm);
143 * format time as a UTC ISO 8601 compliant string
145 strftime (timestamp_str, sizeof (timestamp_str),
146 "%Y-%m-%d %H:%M:%SZ", ×tamp_tm);
147 timestamp_str[sizeof (timestamp_str) - 1] = '\0';
149 if (yajl_gen_string(g, (u_char *)timestamp_str,
150 strlen(timestamp_str)) !=
154 if (yajl_gen_map_close(g) != yajl_gen_status_ok)
157 if (yajl_gen_get_buf(g, &buf, &len) != yajl_gen_status_ok)
159 pthread_mutex_lock (&file_lock);
161 if (log_file == NULL)
163 fh = fopen (DEFAULT_LOGFILE, "a");
165 } else if (strcasecmp(log_file, "stdout") == 0) {
168 } else if (strcasecmp(log_file, "stderr") == 0) {
172 fh = fopen (log_file, "a");
179 fprintf (stderr, "log_logstash plugin: fopen (%s) failed: %s\n",
180 (log_file == NULL) ? DEFAULT_LOGFILE : log_file,
181 sstrerror (errno, errbuf, sizeof (errbuf)));
185 fprintf(fh, "%s\n", buf);
192 pthread_mutex_unlock (&file_lock);
198 fprintf(stderr, "Could not correctly generate JSON message\n");
200 } /* void log_logstash_print */
202 static void log_logstash_log (int severity, const char *msg,
203 user_data_t __attribute__((unused)) *user_data)
206 #if !defined(HAVE_YAJL_V2)
207 yajl_gen_config conf;
212 if (severity > log_level)
216 g = yajl_gen_alloc(NULL);
218 g = yajl_gen_alloc(&conf, NULL);
222 fprintf(stderr, "Could not allocate JSON generator.\n");
226 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
228 if (yajl_gen_string(g, (u_char *)"@message", strlen("@message")) !=
231 if (yajl_gen_string(g, (u_char *)msg, strlen(msg)) !=
235 log_logstash_print (g, severity, cdtime ());
239 fprintf(stderr, "Could not generate JSON message preamble\n");
242 } /* void log_logstash_log (int, const char *) */
244 static int log_logstash_notification (const notification_t *n,
245 user_data_t __attribute__((unused)) *user_data)
249 g = yajl_gen_alloc(NULL);
251 yajl_gen_config conf;
254 g = yajl_gen_alloc(&conf, NULL);
258 fprintf(stderr, "Could not allocate JSON generator.\n");
262 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
264 if (yajl_gen_string(g, (u_char *)"@message", strlen("@message")) !=
267 if (strlen(n->message) > 0) {
268 if (yajl_gen_string(g, (u_char *)n->message,
269 strlen(n->message)) !=
273 if (yajl_gen_string(g, (u_char *)"notification without a message",
274 strlen("notification without a message")) !=
280 if (yajl_gen_string(g, (u_char *)"@fields", strlen("@fields")) !=
283 if (yajl_gen_map_open(g) !=
287 if (strlen(n->host) > 0) {
288 if (yajl_gen_string(g, (u_char *)"host", strlen("host")) !=
291 if (yajl_gen_string(g, (u_char *)n->host, strlen(n->host)) !=
296 if (strlen(n->plugin) > 0) {
297 if (yajl_gen_string(g, (u_char *)"plugin", strlen("plugin")) !=
300 if (yajl_gen_string(g, (u_char *)n->plugin, strlen(n->plugin)) !=
304 if (strlen(n->plugin_instance) > 0) {
305 if (yajl_gen_string(g, (u_char *)"plugin_instance",
306 strlen("plugin_instance")) !=
309 if (yajl_gen_string(g, (u_char *)n->plugin_instance,
310 strlen(n->plugin_instance)) !=
314 if (strlen(n->type) > 0) {
315 if (yajl_gen_string(g, (u_char *)"type", strlen("type")) !=
318 if (yajl_gen_string(g, (u_char *)n->type, strlen(n->type)) !=
322 if (strlen(n->type_instance) > 0) {
323 if (yajl_gen_string(g, (u_char *)"type_instance",
324 strlen("type_instance")) !=
327 if (yajl_gen_string(g, (u_char *)n->type_instance,
328 strlen(n->type_instance)) !=
333 if (yajl_gen_string(g, (u_char *)"severity",
334 strlen("severity")) !=
338 switch (n->severity) {
340 if (yajl_gen_string(g, (u_char *)"failure",
341 strlen("failure")) !=
346 if (yajl_gen_string(g, (u_char *)"warning",
347 strlen("warning")) !=
352 if (yajl_gen_string(g, (u_char *)"ok",
358 if (yajl_gen_string(g, (u_char *)"unknown",
359 strlen("unknown")) !=
364 if (yajl_gen_map_close(g) != yajl_gen_status_ok)
367 log_logstash_print (g, LOG_INFO, (n->time != 0) ? n->time : cdtime ());
372 fprintf(stderr, "Could not correctly generate JSON notification\n");
374 } /* int log_logstash_notification */
376 void module_register (void)
378 plugin_register_config ("log_logstash",
382 plugin_register_log ("log_logstash",
384 /* user_data = */ NULL);
385 plugin_register_notification ("log_logstash",
386 log_logstash_notification,
387 /* user_data = */ NULL);
388 } /* void module_register (void) */
390 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */