support emitting to stdout or stderr
[collectd.git] / src / log_logstash.c
1 /**
2  * collectd - src/log_logstash.c
3  * Copyright (C) 2013 Pierre-Yves Ritschard
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Pierre-Yves Ritschard <pyr at spootnik.org>
20  * Acknowledgements:
21  *   This file is largely inspired by logfile.c
22  **/
23
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27
28 #include <sys/types.h>
29 #include <pthread.h>
30 #include <yajl/yajl_common.h>
31 #include <yajl/yajl_gen.h>
32 #if HAVE_YAJL_YAJL_VERSION_H
33 # include <yajl/yajl_version.h>
34 #endif
35 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
36 # define HAVE_YAJL_V2 1
37 #endif
38
39 #define DEFAULT_LOGFILE LOCALSTATEDIR"/log/collectd.json.log"
40
41 #if COLLECT_DEBUG
42 static int log_level = LOG_DEBUG;
43 #else
44 static int log_level = LOG_INFO;
45 #endif /* COLLECT_DEBUG */
46
47 static pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
48
49 static char *log_file = NULL;
50
51 static const char *config_keys[] =
52 {
53         "LogLevel",
54         "File"
55 };
56 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
57
58 static int log_logstash_config (const char *key, const char *value)
59 {
60
61         if (0 == strcasecmp (key, "LogLevel")) {
62                 log_level = parse_log_severity(value);
63                 printf("parsed log level: %d\n", log_level);
64                 if (log_level == -1) return 1; /* to keep previous behaviour */
65         }
66         else if (0 == strcasecmp (key, "File")) {
67                 sfree (log_file);
68                 log_file = strdup (value);
69         }
70         else {
71                 return -1;
72         }
73         return 0;
74 } /* int log_logstash_config (const char *, const char *) */
75
76 static void log_logstash_print (yajl_gen g, int severity,
77                 cdtime_t timestamp_time)
78 {
79         FILE *fh;
80         _Bool do_close = 0;
81         struct tm timestamp_tm;
82         char timestamp_str[64];
83         const unsigned char *buf;
84         time_t tt;
85 #if HAVE_YAJL_V2
86         size_t len;
87 #else
88         unsigned int len;
89 #endif
90
91         if (yajl_gen_string(g, (u_char *)"@level", strlen("@level")) !=
92             yajl_gen_status_ok)
93                 goto err;
94
95         switch (severity) {
96         case LOG_ERR:
97                 if (yajl_gen_string(g, (u_char *)"error", strlen("error")) !=
98                     yajl_gen_status_ok)
99                         goto err;
100                 break;
101         case LOG_WARNING:
102                 if (yajl_gen_string(g, (u_char *)"warning",
103                                     strlen("warning")) !=
104                     yajl_gen_status_ok)
105                         goto err;
106                 break;
107         case LOG_NOTICE:
108                 if (yajl_gen_string(g, (u_char *)"notice", strlen("notice")) !=
109                     yajl_gen_status_ok)
110                         goto err;
111                 break;
112         case LOG_INFO:
113                 if (yajl_gen_string(g, (u_char *)"info", strlen("info")) !=
114                     yajl_gen_status_ok)
115                         goto err;
116                 break;
117         case LOG_DEBUG:
118                 if (yajl_gen_string(g, (u_char *)"debug", strlen("debug")) !=
119                     yajl_gen_status_ok)
120                         goto err;
121                 break;
122         default:
123                 if (yajl_gen_string(g, (u_char *)"unknown",
124                                     strlen("unknown")) !=
125                     yajl_gen_status_ok)
126                         goto err;
127                 break;
128         }
129
130         if (yajl_gen_string(g, (u_char *)"@timestamp", strlen("@timestamp")) !=
131             yajl_gen_status_ok)
132                 goto err;
133
134         tt = CDTIME_T_TO_TIME_T (timestamp_time);
135         gmtime_r (&tt, &timestamp_tm);
136
137         /*
138          * format time as a UTC ISO 8601 compliant string
139          */
140         strftime (timestamp_str, sizeof (timestamp_str),
141                   "%Y-%m-%d %H:%M:%SZ", &timestamp_tm);
142         timestamp_str[sizeof (timestamp_str) - 1] = '\0';
143
144         if (yajl_gen_string(g, (u_char *)timestamp_str,
145                             strlen(timestamp_str)) !=
146             yajl_gen_status_ok)
147                 goto err;
148
149         if (yajl_gen_map_close(g) != yajl_gen_status_ok)
150                 goto err;
151
152         if (yajl_gen_get_buf(g, &buf, &len) != yajl_gen_status_ok)
153                 goto err;
154         pthread_mutex_lock (&file_lock);
155
156         if (log_file == NULL)
157         {
158                 fh = fopen (DEFAULT_LOGFILE, "a");
159                 do_close = 1;
160         } else if (strcasecmp(log_file, "stdout") == 0) {
161         fh = stdout;
162         do_close = 0;
163         } else if (strcasecmp(log_file, "stderr") == 0) {
164         fh = stderr;
165         do_close = 0;
166         } else {
167                 fh = fopen (log_file, "a");
168                 do_close = 1;
169         }
170
171         if (fh == NULL)
172         {
173                         char errbuf[1024];
174                         fprintf (stderr, "log_logstash plugin: fopen (%s) failed: %s\n",
175                                         (log_file == NULL) ? DEFAULT_LOGFILE : log_file,
176                                         sstrerror (errno, errbuf, sizeof (errbuf)));
177         }
178         else
179         {
180                 fprintf(fh, "%s\n", buf);
181                 if (do_close) {
182                         fclose (fh);
183                 } else {
184                         fflush(fh);
185                 }
186         }
187         pthread_mutex_unlock (&file_lock);
188         yajl_gen_free(g);
189         return;
190
191  err:
192         yajl_gen_free(g);
193         fprintf(stderr, "Could not correctly generate JSON message\n");
194         return;
195 } /* void log_logstash_print */
196
197 static void log_logstash_log (int severity, const char *msg,
198                 user_data_t __attribute__((unused)) *user_data)
199 {
200         yajl_gen g;
201 #if !defined(HAVE_YAJL_V2)
202         yajl_gen_config conf;
203
204         conf.beautify = 0;
205 #endif
206
207         if (severity > log_level)
208                 return;
209
210 #if HAVE_YAJL_V2
211         g = yajl_gen_alloc(NULL);
212 #else
213         g = yajl_gen_alloc(&conf, NULL);
214 #endif
215
216         if (g == NULL) {
217                 fprintf(stderr, "Could not allocate JSON generator.\n");
218                 return;
219         }
220
221         if (yajl_gen_map_open(g) != yajl_gen_status_ok)
222                 goto err;
223         if (yajl_gen_string(g, (u_char *)"@message", strlen("@message")) !=
224             yajl_gen_status_ok)
225                 goto err;
226         if (yajl_gen_string(g, (u_char *)msg, strlen(msg)) !=
227             yajl_gen_status_ok)
228                 goto err;
229
230         log_logstash_print (g, severity, cdtime ());
231         return;
232  err:
233         yajl_gen_free(g);
234         fprintf(stderr, "Could not generate JSON message preamble\n");
235         return;
236
237 } /* void log_logstash_log (int, const char *) */
238
239 static int log_logstash_notification (const notification_t *n,
240                 user_data_t __attribute__((unused)) *user_data)
241 {
242         yajl_gen g;
243 #if HAVE_YAJL_V2
244         g = yajl_gen_alloc(NULL);
245 #else
246         yajl_gen_config conf;
247
248         conf.beautify = 0;
249         g = yajl_gen_alloc(&conf, NULL);
250 #endif
251
252         if (g == NULL) {
253                 fprintf(stderr, "Could not allocate JSON generator.\n");
254                 return (0);
255         }
256
257         if (yajl_gen_map_open(g) != yajl_gen_status_ok)
258                 goto err;
259         if (yajl_gen_string(g, (u_char *)"@message", strlen("@message")) !=
260             yajl_gen_status_ok)
261                 goto err;
262         if (strlen(n->message) > 0) {
263                 if (yajl_gen_string(g, (u_char *)n->message,
264                                     strlen(n->message)) !=
265                     yajl_gen_status_ok)
266                         goto err;
267         } else {
268                 if (yajl_gen_string(g, (u_char *)"notification without a message",
269                                     strlen("notification without a message")) !=
270                     yajl_gen_status_ok)
271                         goto err;
272         }
273
274
275         if (yajl_gen_string(g, (u_char *)"@fields", strlen("@fields")) !=
276             yajl_gen_status_ok)
277                 goto err;
278         if (yajl_gen_map_open(g) !=
279             yajl_gen_status_ok)
280                 goto err;
281
282         if (strlen(n->host) > 0) {
283                 if (yajl_gen_string(g, (u_char *)"host", strlen("host")) !=
284                     yajl_gen_status_ok)
285                         goto err;
286                 if (yajl_gen_string(g, (u_char *)n->host, strlen(n->host)) !=
287                     yajl_gen_status_ok)
288                         goto err;
289
290         }
291         if (strlen(n->plugin) > 0) {
292                 if (yajl_gen_string(g, (u_char *)"plugin", strlen("plugin")) !=
293                     yajl_gen_status_ok)
294                         goto err;
295                 if (yajl_gen_string(g, (u_char *)n->plugin, strlen(n->plugin)) !=
296                     yajl_gen_status_ok)
297                         goto err;
298         }
299         if (strlen(n->plugin_instance) > 0) {
300                 if (yajl_gen_string(g, (u_char *)"plugin_instance",
301                                     strlen("plugin_instance")) !=
302                     yajl_gen_status_ok)
303                         goto err;
304                 if (yajl_gen_string(g, (u_char *)n->plugin_instance,
305                                     strlen(n->plugin_instance)) !=
306                     yajl_gen_status_ok)
307                         goto err;
308         }
309         if (strlen(n->type) > 0) {
310                 if (yajl_gen_string(g, (u_char *)"type", strlen("type")) !=
311                     yajl_gen_status_ok)
312                         goto err;
313                 if (yajl_gen_string(g, (u_char *)n->type, strlen(n->type)) !=
314                     yajl_gen_status_ok)
315                         goto err;
316         }
317         if (strlen(n->type_instance) > 0) {
318                 if (yajl_gen_string(g, (u_char *)"type_instance",
319                                     strlen("type_instance")) !=
320                     yajl_gen_status_ok)
321                         goto err;
322                 if (yajl_gen_string(g, (u_char *)n->type_instance,
323                                     strlen(n->type_instance)) !=
324                     yajl_gen_status_ok)
325                         goto err;
326         }
327
328         if (yajl_gen_string(g, (u_char *)"severity",
329                             strlen("severity")) !=
330             yajl_gen_status_ok)
331                 goto err;
332
333         switch (n->severity) {
334         case NOTIF_FAILURE:
335                 if (yajl_gen_string(g, (u_char *)"failure",
336                                     strlen("failure")) !=
337                     yajl_gen_status_ok)
338                         goto err;
339                 break;
340         case NOTIF_WARNING:
341                 if (yajl_gen_string(g, (u_char *)"warning",
342                                     strlen("warning")) !=
343                     yajl_gen_status_ok)
344                         goto err;
345                 break;
346         case NOTIF_OKAY:
347                 if (yajl_gen_string(g, (u_char *)"ok",
348                                     strlen("ok")) !=
349                     yajl_gen_status_ok)
350                         goto err;
351                 break;
352         default:
353                 if (yajl_gen_string(g, (u_char *)"unknown",
354                                     strlen("unknown")) !=
355                     yajl_gen_status_ok)
356                         goto err;
357                 break;
358         }
359         if (yajl_gen_map_close(g) != yajl_gen_status_ok)
360                 goto err;
361
362         log_logstash_print (g, LOG_INFO, (n->time != 0) ? n->time : cdtime ());
363         return (0);
364
365  err:
366         yajl_gen_free(g);
367         fprintf(stderr, "Could not correctly generate JSON notification\n");
368         return (0);
369 } /* int log_logstash_notification */
370
371 void module_register (void)
372 {
373         plugin_register_config ("log_logstash",
374                                 log_logstash_config,
375                                 config_keys,
376                                 config_keys_num);
377         plugin_register_log ("log_logstash",
378                              log_logstash_log,
379                              /* user_data = */ NULL);
380         plugin_register_notification ("log_logstash",
381                                       log_logstash_notification,
382                                       /* user_data = */ NULL);
383 } /* void module_register (void) */
384
385 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */