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