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