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