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>
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
44 #define DEFAULT_LOGFILE LOCALSTATEDIR"/log/"PACKAGE_NAME".json.log"
47 static int log_level = LOG_DEBUG;
49 static int log_level = LOG_INFO;
50 #endif /* COLLECT_DEBUG */
52 static pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
54 static char *log_file = NULL;
56 static const char *config_keys[] =
61 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
63 static int log_logstash_config (const char *key, const char *value)
66 if (0 == strcasecmp (key, "LogLevel")) {
67 log_level = parse_log_severity(value);
70 ERROR("log_logstash: invalid loglevel [%s] defaulting to 'info'",
75 else if (0 == strcasecmp (key, "File")) {
77 log_file = strdup (value);
83 } /* int log_logstash_config (const char *, const char *) */
85 static void log_logstash_print (yajl_gen g, int severity,
86 cdtime_t timestamp_time)
90 struct tm timestamp_tm;
91 char timestamp_str[64];
92 const unsigned char *buf;
100 if (yajl_gen_string(g, (u_char *)"level", strlen("level")) !=
106 if (yajl_gen_string(g, (u_char *)"error", strlen("error")) !=
111 if (yajl_gen_string(g, (u_char *)"warning",
112 strlen("warning")) !=
117 if (yajl_gen_string(g, (u_char *)"notice", strlen("notice")) !=
122 if (yajl_gen_string(g, (u_char *)"info", strlen("info")) !=
127 if (yajl_gen_string(g, (u_char *)"debug", strlen("debug")) !=
132 if (yajl_gen_string(g, (u_char *)"unknown",
133 strlen("unknown")) !=
139 if (yajl_gen_string(g, (u_char *)"@timestamp", strlen("@timestamp")) !=
143 tt = CDTIME_T_TO_TIME_T (timestamp_time);
144 gmtime_r (&tt, ×tamp_tm);
147 * format time as a UTC ISO 8601 compliant string
149 strftime (timestamp_str, sizeof (timestamp_str),
150 "%Y-%m-%d %H:%M:%SZ", ×tamp_tm);
151 timestamp_str[sizeof (timestamp_str) - 1] = '\0';
153 if (yajl_gen_string(g, (u_char *)timestamp_str,
154 strlen(timestamp_str)) !=
158 if (yajl_gen_map_close(g) != yajl_gen_status_ok)
161 if (yajl_gen_get_buf(g, &buf, &len) != yajl_gen_status_ok)
163 pthread_mutex_lock (&file_lock);
165 if (log_file == NULL)
167 fh = fopen (DEFAULT_LOGFILE, "a");
169 } else if (strcasecmp(log_file, "stdout") == 0) {
172 } else if (strcasecmp(log_file, "stderr") == 0) {
176 fh = fopen (log_file, "a");
183 fprintf (stderr, "log_logstash plugin: fopen (%s) failed: %s\n",
184 (log_file == NULL) ? DEFAULT_LOGFILE : log_file,
185 sstrerror (errno, errbuf, sizeof (errbuf)));
189 fprintf(fh, "%s\n", buf);
196 pthread_mutex_unlock (&file_lock);
202 fprintf(stderr, "Could not correctly generate JSON message\n");
204 } /* void log_logstash_print */
206 static void log_logstash_log (int severity, const char *msg,
207 user_data_t __attribute__((unused)) *user_data)
210 #if !defined(HAVE_YAJL_V2)
211 yajl_gen_config conf;
216 if (severity > log_level)
220 g = yajl_gen_alloc(NULL);
222 g = yajl_gen_alloc(&conf, NULL);
226 fprintf(stderr, "Could not allocate JSON generator.\n");
230 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
232 if (yajl_gen_string(g, (u_char *)"message", strlen("message")) !=
235 if (yajl_gen_string(g, (u_char *)msg, strlen(msg)) !=
239 log_logstash_print (g, severity, cdtime ());
243 fprintf(stderr, "Could not generate JSON message preamble\n");
246 } /* void log_logstash_log (int, const char *) */
248 static int log_logstash_notification (const notification_t *n,
249 user_data_t __attribute__((unused)) *user_data)
253 g = yajl_gen_alloc(NULL);
255 yajl_gen_config conf;
258 g = yajl_gen_alloc(&conf, NULL);
262 fprintf(stderr, "Could not allocate JSON generator.\n");
266 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
268 if (yajl_gen_string(g, (u_char *)"message", strlen("message")) !=
271 if (strlen(n->message) > 0) {
272 if (yajl_gen_string(g, (u_char *)n->message,
273 strlen(n->message)) !=
277 if (yajl_gen_string(g, (u_char *)"notification without a message",
278 strlen("notification without a message")) !=
283 if (strlen(n->host) > 0) {
284 if (yajl_gen_string(g, (u_char *)"host", strlen("host")) !=
287 if (yajl_gen_string(g, (u_char *)n->host, strlen(n->host)) !=
292 if (strlen(n->plugin) > 0) {
293 if (yajl_gen_string(g, (u_char *)"plugin", strlen("plugin")) !=
296 if (yajl_gen_string(g, (u_char *)n->plugin, strlen(n->plugin)) !=
300 if (strlen(n->plugin_instance) > 0) {
301 if (yajl_gen_string(g, (u_char *)"plugin_instance",
302 strlen("plugin_instance")) !=
305 if (yajl_gen_string(g, (u_char *)n->plugin_instance,
306 strlen(n->plugin_instance)) !=
310 if (strlen(n->type) > 0) {
311 if (yajl_gen_string(g, (u_char *)"type", strlen("type")) !=
314 if (yajl_gen_string(g, (u_char *)n->type, strlen(n->type)) !=
318 if (strlen(n->type_instance) > 0) {
319 if (yajl_gen_string(g, (u_char *)"type_instance",
320 strlen("type_instance")) !=
323 if (yajl_gen_string(g, (u_char *)n->type_instance,
324 strlen(n->type_instance)) !=
329 if (yajl_gen_string(g, (u_char *)"severity",
330 strlen("severity")) !=
334 switch (n->severity) {
336 if (yajl_gen_string(g, (u_char *)"failure",
337 strlen("failure")) !=
342 if (yajl_gen_string(g, (u_char *)"warning",
343 strlen("warning")) !=
348 if (yajl_gen_string(g, (u_char *)"ok",
354 if (yajl_gen_string(g, (u_char *)"unknown",
355 strlen("unknown")) !=
361 log_logstash_print (g, LOG_INFO, (n->time != 0) ? n->time : cdtime ());
366 fprintf(stderr, "Could not correctly generate JSON notification\n");
368 } /* int log_logstash_notification */
370 void module_register (void)
372 plugin_register_config ("log_logstash",
376 plugin_register_log ("log_logstash",
378 /* user_data = */ NULL);
379 plugin_register_notification ("log_logstash",
380 log_logstash_notification,
381 /* user_data = */ NULL);
382 } /* void module_register (void) */
384 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */