Merge branch 'collectd-5.8'
[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 #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   yajl_gen g;
186 #if !defined(HAVE_YAJL_V2)
187   yajl_gen_config conf = {};
188
189   conf.beautify = 0;
190 #endif
191
192   if (severity > log_level)
193     return;
194
195 #if HAVE_YAJL_V2
196   g = yajl_gen_alloc(NULL);
197 #else
198   g = yajl_gen_alloc(&conf, NULL);
199 #endif
200
201   if (g == NULL) {
202     fprintf(stderr, "Could not allocate JSON generator.\n");
203     return;
204   }
205
206   if (yajl_gen_map_open(g) != yajl_gen_status_ok)
207     goto err;
208   if (yajl_gen_string(g, (u_char *)"message", strlen("message")) !=
209       yajl_gen_status_ok)
210     goto err;
211   if (yajl_gen_string(g, (u_char *)msg, strlen(msg)) != yajl_gen_status_ok)
212     goto err;
213
214   log_logstash_print(g, severity, cdtime());
215   return;
216 err:
217   yajl_gen_free(g);
218   fprintf(stderr, "Could not generate JSON message preamble\n");
219   return;
220
221 } /* void log_logstash_log (int, const char *) */
222
223 static int log_logstash_notification(const notification_t *n,
224                                      user_data_t __attribute__((unused)) *
225                                          user_data) {
226   yajl_gen g;
227 #if HAVE_YAJL_V2
228   g = yajl_gen_alloc(NULL);
229 #else
230   yajl_gen_config conf = {};
231
232   conf.beautify = 0;
233   g = yajl_gen_alloc(&conf, NULL);
234 #endif
235
236   if (g == NULL) {
237     fprintf(stderr, "Could not allocate JSON generator.\n");
238     return 0;
239   }
240
241   if (yajl_gen_map_open(g) != yajl_gen_status_ok)
242     goto err;
243   if (yajl_gen_string(g, (u_char *)"message", strlen("message")) !=
244       yajl_gen_status_ok)
245     goto err;
246   if (strlen(n->message) > 0) {
247     if (yajl_gen_string(g, (u_char *)n->message, strlen(n->message)) !=
248         yajl_gen_status_ok)
249       goto err;
250   } else {
251     if (yajl_gen_string(g, (u_char *)"notification without a message",
252                         strlen("notification without a message")) !=
253         yajl_gen_status_ok)
254       goto err;
255   }
256
257   if (strlen(n->host) > 0) {
258     if (yajl_gen_string(g, (u_char *)"host", strlen("host")) !=
259         yajl_gen_status_ok)
260       goto err;
261     if (yajl_gen_string(g, (u_char *)n->host, strlen(n->host)) !=
262         yajl_gen_status_ok)
263       goto err;
264   }
265   if (strlen(n->plugin) > 0) {
266     if (yajl_gen_string(g, (u_char *)"plugin", strlen("plugin")) !=
267         yajl_gen_status_ok)
268       goto err;
269     if (yajl_gen_string(g, (u_char *)n->plugin, strlen(n->plugin)) !=
270         yajl_gen_status_ok)
271       goto err;
272   }
273   if (strlen(n->plugin_instance) > 0) {
274     if (yajl_gen_string(g, (u_char *)"plugin_instance",
275                         strlen("plugin_instance")) != yajl_gen_status_ok)
276       goto err;
277     if (yajl_gen_string(g, (u_char *)n->plugin_instance,
278                         strlen(n->plugin_instance)) != yajl_gen_status_ok)
279       goto err;
280   }
281   if (strlen(n->type) > 0) {
282     if (yajl_gen_string(g, (u_char *)"type", strlen("type")) !=
283         yajl_gen_status_ok)
284       goto err;
285     if (yajl_gen_string(g, (u_char *)n->type, strlen(n->type)) !=
286         yajl_gen_status_ok)
287       goto err;
288   }
289   if (strlen(n->type_instance) > 0) {
290     if (yajl_gen_string(g, (u_char *)"type_instance",
291                         strlen("type_instance")) != yajl_gen_status_ok)
292       goto err;
293     if (yajl_gen_string(g, (u_char *)n->type_instance,
294                         strlen(n->type_instance)) != yajl_gen_status_ok)
295       goto err;
296   }
297
298   if (yajl_gen_string(g, (u_char *)"severity", strlen("severity")) !=
299       yajl_gen_status_ok)
300     goto err;
301
302   switch (n->severity) {
303   case NOTIF_FAILURE:
304     if (yajl_gen_string(g, (u_char *)"failure", strlen("failure")) !=
305         yajl_gen_status_ok)
306       goto err;
307     break;
308   case NOTIF_WARNING:
309     if (yajl_gen_string(g, (u_char *)"warning", strlen("warning")) !=
310         yajl_gen_status_ok)
311       goto err;
312     break;
313   case NOTIF_OKAY:
314     if (yajl_gen_string(g, (u_char *)"ok", strlen("ok")) != yajl_gen_status_ok)
315       goto err;
316     break;
317   default:
318     if (yajl_gen_string(g, (u_char *)"unknown", strlen("unknown")) !=
319         yajl_gen_status_ok)
320       goto err;
321     break;
322   }
323
324   log_logstash_print(g, LOG_INFO, (n->time != 0) ? n->time : cdtime());
325   return 0;
326
327 err:
328   yajl_gen_free(g);
329   fprintf(stderr, "Could not correctly generate JSON notification\n");
330   return 0;
331 } /* int log_logstash_notification */
332
333 void module_register(void) {
334   plugin_register_config("log_logstash", log_logstash_config, config_keys,
335                          config_keys_num);
336   plugin_register_log("log_logstash", log_logstash_log,
337                       /* user_data = */ NULL);
338   plugin_register_notification("log_logstash", log_logstash_notification,
339                                /* user_data = */ NULL);
340 } /* void module_register (void) */