Merge branch 'collectd-5.6'
[collectd.git] / src / log_logstash.c
1 /**
2  * collectd - src/log_logstash.c
3  * Copyright (C) 2013       Pierre-Yves Ritschard
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
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.
22  *
23  * Authors:
24  *   Pierre-Yves Ritschard <pyr at spootnik.org>
25  * Acknowledgements:
26  *   This file is largely inspired by logfile.c
27  **/
28
29 #include "collectd.h"
30
31 #include "common.h"
32 #include "plugin.h"
33
34 #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>
39 #endif
40 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
41 #define HAVE_YAJL_V2 1
42 #endif
43
44 #define DEFAULT_LOGFILE LOCALSTATEDIR "/log/" PACKAGE_NAME ".json.log"
45
46 #if COLLECT_DEBUG
47 static int log_level = LOG_DEBUG;
48 #else
49 static int log_level = LOG_INFO;
50 #endif /* COLLECT_DEBUG */
51
52 static pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
53
54 static char *log_file = NULL;
55
56 static const char *config_keys[] = {"LogLevel", "File"};
57 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
58
59 static int log_logstash_config(const char *key, const char *value) {
60
61   if (0 == strcasecmp(key, "LogLevel")) {
62     log_level = parse_log_severity(value);
63     if (log_level < 0) {
64       log_level = LOG_INFO;
65       ERROR("log_logstash: invalid loglevel [%s] defaulting to 'info'", value);
66       return 1;
67     }
68   } else if (0 == strcasecmp(key, "File")) {
69     sfree(log_file);
70     log_file = strdup(value);
71   } else {
72     return -1;
73   }
74   return 0;
75 } /* int log_logstash_config (const char *, const char *) */
76
77 static void log_logstash_print(yajl_gen g, int severity,
78                                cdtime_t timestamp_time) {
79   FILE *fh;
80   _Bool do_close = 0;
81   struct tm timestamp_tm;
82   char timestamp_str[64];
83   const unsigned char *buf;
84 #if HAVE_YAJL_V2
85   size_t len;
86 #else
87   unsigned int len;
88 #endif
89
90   if (yajl_gen_string(g, (u_char *)"level", strlen("level")) !=
91       yajl_gen_status_ok)
92     goto err;
93
94   switch (severity) {
95   case LOG_ERR:
96     if (yajl_gen_string(g, (u_char *)"error", strlen("error")) !=
97         yajl_gen_status_ok)
98       goto err;
99     break;
100   case LOG_WARNING:
101     if (yajl_gen_string(g, (u_char *)"warning", strlen("warning")) !=
102         yajl_gen_status_ok)
103       goto err;
104     break;
105   case LOG_NOTICE:
106     if (yajl_gen_string(g, (u_char *)"notice", strlen("notice")) !=
107         yajl_gen_status_ok)
108       goto err;
109     break;
110   case LOG_INFO:
111     if (yajl_gen_string(g, (u_char *)"info", strlen("info")) !=
112         yajl_gen_status_ok)
113       goto err;
114     break;
115   case LOG_DEBUG:
116     if (yajl_gen_string(g, (u_char *)"debug", strlen("debug")) !=
117         yajl_gen_status_ok)
118       goto err;
119     break;
120   default:
121     if (yajl_gen_string(g, (u_char *)"unknown", strlen("unknown")) !=
122         yajl_gen_status_ok)
123       goto err;
124     break;
125   }
126
127   if (yajl_gen_string(g, (u_char *)"@timestamp", strlen("@timestamp")) !=
128       yajl_gen_status_ok)
129     goto err;
130
131   gmtime_r(&CDTIME_T_TO_TIME_T(timestamp_time), &timestamp_tm);
132
133   /*
134    * format time as a UTC ISO 8601 compliant string
135    */
136   strftime(timestamp_str, sizeof(timestamp_str), "%Y-%m-%dT%H:%M:%SZ",
137            &timestamp_tm);
138   timestamp_str[sizeof(timestamp_str) - 1] = '\0';
139
140   if (yajl_gen_string(g, (u_char *)timestamp_str, strlen(timestamp_str)) !=
141       yajl_gen_status_ok)
142     goto err;
143
144   if (yajl_gen_map_close(g) != yajl_gen_status_ok)
145     goto err;
146
147   if (yajl_gen_get_buf(g, &buf, &len) != yajl_gen_status_ok)
148     goto err;
149   pthread_mutex_lock(&file_lock);
150
151   if (log_file == NULL) {
152     fh = fopen(DEFAULT_LOGFILE, "a");
153     do_close = 1;
154   } else if (strcasecmp(log_file, "stdout") == 0) {
155     fh = stdout;
156     do_close = 0;
157   } else if (strcasecmp(log_file, "stderr") == 0) {
158     fh = stderr;
159     do_close = 0;
160   } else {
161     fh = fopen(log_file, "a");
162     do_close = 1;
163   }
164
165   if (fh == NULL) {
166     char errbuf[1024];
167     fprintf(stderr, "log_logstash plugin: fopen (%s) failed: %s\n",
168             (log_file == NULL) ? DEFAULT_LOGFILE : log_file,
169             sstrerror(errno, errbuf, sizeof(errbuf)));
170   } else {
171     fprintf(fh, "%s\n", buf);
172     if (do_close) {
173       fclose(fh);
174     } else {
175       fflush(fh);
176     }
177   }
178   pthread_mutex_unlock(&file_lock);
179   yajl_gen_free(g);
180   return;
181
182 err:
183   yajl_gen_free(g);
184   fprintf(stderr, "Could not correctly generate JSON message\n");
185   return;
186 } /* void log_logstash_print */
187
188 static void log_logstash_log(int severity, const char *msg,
189                              user_data_t __attribute__((unused)) * user_data) {
190   yajl_gen g;
191 #if !defined(HAVE_YAJL_V2)
192   yajl_gen_config conf = {};
193
194   conf.beautify = 0;
195 #endif
196
197   if (severity > log_level)
198     return;
199
200 #if HAVE_YAJL_V2
201   g = yajl_gen_alloc(NULL);
202 #else
203   g = yajl_gen_alloc(&conf, NULL);
204 #endif
205
206   if (g == NULL) {
207     fprintf(stderr, "Could not allocate JSON generator.\n");
208     return;
209   }
210
211   if (yajl_gen_map_open(g) != yajl_gen_status_ok)
212     goto err;
213   if (yajl_gen_string(g, (u_char *)"message", strlen("message")) !=
214       yajl_gen_status_ok)
215     goto err;
216   if (yajl_gen_string(g, (u_char *)msg, strlen(msg)) != yajl_gen_status_ok)
217     goto err;
218
219   log_logstash_print(g, severity, cdtime());
220   return;
221 err:
222   yajl_gen_free(g);
223   fprintf(stderr, "Could not generate JSON message preamble\n");
224   return;
225
226 } /* void log_logstash_log (int, const char *) */
227
228 static int log_logstash_notification(const notification_t *n,
229                                      user_data_t __attribute__((unused)) *
230                                          user_data) {
231   yajl_gen g;
232 #if HAVE_YAJL_V2
233   g = yajl_gen_alloc(NULL);
234 #else
235   yajl_gen_config conf = {};
236
237   conf.beautify = 0;
238   g = yajl_gen_alloc(&conf, NULL);
239 #endif
240
241   if (g == NULL) {
242     fprintf(stderr, "Could not allocate JSON generator.\n");
243     return (0);
244   }
245
246   if (yajl_gen_map_open(g) != yajl_gen_status_ok)
247     goto err;
248   if (yajl_gen_string(g, (u_char *)"message", strlen("message")) !=
249       yajl_gen_status_ok)
250     goto err;
251   if (strlen(n->message) > 0) {
252     if (yajl_gen_string(g, (u_char *)n->message, strlen(n->message)) !=
253         yajl_gen_status_ok)
254       goto err;
255   } else {
256     if (yajl_gen_string(g, (u_char *)"notification without a message",
257                         strlen("notification without a message")) !=
258         yajl_gen_status_ok)
259       goto err;
260   }
261
262   if (strlen(n->host) > 0) {
263     if (yajl_gen_string(g, (u_char *)"host", strlen("host")) !=
264         yajl_gen_status_ok)
265       goto err;
266     if (yajl_gen_string(g, (u_char *)n->host, strlen(n->host)) !=
267         yajl_gen_status_ok)
268       goto err;
269   }
270   if (strlen(n->plugin) > 0) {
271     if (yajl_gen_string(g, (u_char *)"plugin", strlen("plugin")) !=
272         yajl_gen_status_ok)
273       goto err;
274     if (yajl_gen_string(g, (u_char *)n->plugin, strlen(n->plugin)) !=
275         yajl_gen_status_ok)
276       goto err;
277   }
278   if (strlen(n->plugin_instance) > 0) {
279     if (yajl_gen_string(g, (u_char *)"plugin_instance",
280                         strlen("plugin_instance")) != yajl_gen_status_ok)
281       goto err;
282     if (yajl_gen_string(g, (u_char *)n->plugin_instance,
283                         strlen(n->plugin_instance)) != yajl_gen_status_ok)
284       goto err;
285   }
286   if (strlen(n->type) > 0) {
287     if (yajl_gen_string(g, (u_char *)"type", strlen("type")) !=
288         yajl_gen_status_ok)
289       goto err;
290     if (yajl_gen_string(g, (u_char *)n->type, strlen(n->type)) !=
291         yajl_gen_status_ok)
292       goto err;
293   }
294   if (strlen(n->type_instance) > 0) {
295     if (yajl_gen_string(g, (u_char *)"type_instance",
296                         strlen("type_instance")) != yajl_gen_status_ok)
297       goto err;
298     if (yajl_gen_string(g, (u_char *)n->type_instance,
299                         strlen(n->type_instance)) != yajl_gen_status_ok)
300       goto err;
301   }
302
303   if (yajl_gen_string(g, (u_char *)"severity", strlen("severity")) !=
304       yajl_gen_status_ok)
305     goto err;
306
307   switch (n->severity) {
308   case NOTIF_FAILURE:
309     if (yajl_gen_string(g, (u_char *)"failure", strlen("failure")) !=
310         yajl_gen_status_ok)
311       goto err;
312     break;
313   case NOTIF_WARNING:
314     if (yajl_gen_string(g, (u_char *)"warning", strlen("warning")) !=
315         yajl_gen_status_ok)
316       goto err;
317     break;
318   case NOTIF_OKAY:
319     if (yajl_gen_string(g, (u_char *)"ok", strlen("ok")) != yajl_gen_status_ok)
320       goto err;
321     break;
322   default:
323     if (yajl_gen_string(g, (u_char *)"unknown", strlen("unknown")) !=
324         yajl_gen_status_ok)
325       goto err;
326     break;
327   }
328
329   log_logstash_print(g, LOG_INFO, (n->time != 0) ? n->time : cdtime());
330   return (0);
331
332 err:
333   yajl_gen_free(g);
334   fprintf(stderr, "Could not correctly generate JSON notification\n");
335   return (0);
336 } /* int log_logstash_notification */
337
338 void module_register(void) {
339   plugin_register_config("log_logstash", log_logstash_config, config_keys,
340                          config_keys_num);
341   plugin_register_log("log_logstash", log_logstash_log,
342                       /* user_data = */ NULL);
343   plugin_register_notification("log_logstash", log_logstash_notification,
344                                /* user_data = */ NULL);
345 } /* void module_register (void) */
346
347 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */