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