14268d2caa747b0bf38b191c983547749f07ba4e
[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         }
161         else
162         {
163                 fh = fopen (log_file, "a");
164                 do_close = 1;
165         }
166
167         if (fh == NULL)
168         {
169                         char errbuf[1024];
170                         fprintf (stderr, "log_logstash plugin: fopen (%s) failed: %s\n",
171                                         (log_file == NULL) ? DEFAULT_LOGFILE : log_file,
172                                         sstrerror (errno, errbuf, sizeof (errbuf)));
173         }
174         else
175         {
176                 fprintf(fh, "%s\n", buf);
177                 if (do_close) {
178                         fclose (fh);
179                 } else {
180                         fflush(fh);
181                 }
182         }
183         pthread_mutex_unlock (&file_lock);
184         yajl_gen_free(g);
185         return;
186
187  err:
188         yajl_gen_free(g);
189         fprintf(stderr, "Could not correctly generate JSON message\n");
190         return;
191 } /* void log_logstash_print */
192
193 static void log_logstash_log (int severity, const char *msg,
194                 user_data_t __attribute__((unused)) *user_data)
195 {
196         yajl_gen g;
197 #if !defined(HAVE_YAJL_V2)
198         yajl_gen_config conf;
199
200         conf.beautify = 0;
201 #endif  
202
203         if (severity > log_level)
204                 return;
205
206 #if HAVE_YAJL_V2
207         g = yajl_gen_alloc(NULL);
208 #else
209         g = yajl_gen_alloc(&conf, NULL);
210 #endif
211
212         if (g == NULL) {
213                 fprintf(stderr, "Could not allocate JSON generator.\n");
214                 return;
215         }
216
217         if (yajl_gen_map_open(g) != yajl_gen_status_ok)
218                 goto err;
219         if (yajl_gen_string(g, (u_char *)"@message", strlen("@message")) !=
220             yajl_gen_status_ok)
221                 goto err;
222         if (yajl_gen_string(g, (u_char *)msg, strlen(msg)) !=
223             yajl_gen_status_ok)
224                 goto err;
225
226         log_logstash_print (g, severity, cdtime ());
227         return;
228  err:
229         yajl_gen_free(g);
230         fprintf(stderr, "Could not generate JSON message preamble\n");
231         return;
232         
233 } /* void log_logstash_log (int, const char *) */
234
235 static int log_logstash_notification (const notification_t *n,
236                 user_data_t __attribute__((unused)) *user_data)
237 {
238         yajl_gen g;
239 #if HAVE_YAJL_V2
240         g = yajl_gen_alloc(NULL);
241 #else
242         yajl_gen_config conf;
243
244         conf.beautify = 0;
245         g = yajl_gen_alloc(&conf, NULL);
246 #endif
247
248         if (g == NULL) {
249                 fprintf(stderr, "Could not allocate JSON generator.\n");
250                 return (0);
251         }
252
253         if (yajl_gen_map_open(g) != yajl_gen_status_ok)
254                 goto err;
255         if (yajl_gen_string(g, (u_char *)"@message", strlen("@message")) !=
256             yajl_gen_status_ok)
257                 goto err;
258         if (strlen(n->message) > 0) {
259                 if (yajl_gen_string(g, (u_char *)n->message,
260                                     strlen(n->message)) !=
261                     yajl_gen_status_ok)
262                         goto err;
263         } else {
264                 if (yajl_gen_string(g, (u_char *)"notification without a message",
265                                     strlen("notification without a message")) !=
266                     yajl_gen_status_ok)
267                         goto err;
268         }
269
270
271         if (yajl_gen_string(g, (u_char *)"@fields", strlen("@fields")) !=
272             yajl_gen_status_ok)
273                 goto err;
274         if (yajl_gen_map_open(g) !=
275             yajl_gen_status_ok)
276                 goto err;
277
278         if (strlen(n->host) > 0) {
279                 if (yajl_gen_string(g, (u_char *)"host", strlen("host")) !=
280                     yajl_gen_status_ok)
281                         goto err;
282                 if (yajl_gen_string(g, (u_char *)n->host, strlen(n->host)) !=
283                     yajl_gen_status_ok)
284                         goto err;
285
286         }
287         if (strlen(n->plugin) > 0) {
288                 if (yajl_gen_string(g, (u_char *)"plugin", strlen("plugin")) !=
289                     yajl_gen_status_ok)
290                         goto err;
291                 if (yajl_gen_string(g, (u_char *)n->plugin, strlen(n->plugin)) !=
292                     yajl_gen_status_ok)
293                         goto err;
294         }
295         if (strlen(n->plugin_instance) > 0) {
296                 if (yajl_gen_string(g, (u_char *)"plugin_instance",
297                                     strlen("plugin_instance")) !=
298                     yajl_gen_status_ok)
299                         goto err;
300                 if (yajl_gen_string(g, (u_char *)n->plugin_instance,
301                                     strlen(n->plugin_instance)) !=
302                     yajl_gen_status_ok)
303                         goto err;
304         }
305         if (strlen(n->type) > 0) {
306                 if (yajl_gen_string(g, (u_char *)"type", strlen("type")) !=
307                     yajl_gen_status_ok)
308                         goto err;
309                 if (yajl_gen_string(g, (u_char *)n->type, strlen(n->type)) !=
310                     yajl_gen_status_ok)
311                         goto err;
312         }
313         if (strlen(n->type_instance) > 0) {
314                 if (yajl_gen_string(g, (u_char *)"type_instance",
315                                     strlen("type_instance")) !=
316                     yajl_gen_status_ok)
317                         goto err;
318                 if (yajl_gen_string(g, (u_char *)n->type_instance,
319                                     strlen(n->type_instance)) != 
320                     yajl_gen_status_ok)
321                         goto err;
322         }
323
324         if (yajl_gen_string(g, (u_char *)"severity",
325                             strlen("severity")) !=
326             yajl_gen_status_ok)
327                 goto err;
328
329         switch (n->severity) {
330         case NOTIF_FAILURE:
331                 if (yajl_gen_string(g, (u_char *)"failure",
332                                     strlen("failure")) !=
333                     yajl_gen_status_ok)
334                         goto err;
335                 break;
336         case NOTIF_WARNING:
337                 if (yajl_gen_string(g, (u_char *)"warning",
338                                     strlen("warning")) !=
339                     yajl_gen_status_ok)
340                         goto err;
341                 break;
342         case NOTIF_OKAY:
343                 if (yajl_gen_string(g, (u_char *)"ok",
344                                     strlen("ok")) !=
345                     yajl_gen_status_ok)
346                         goto err;
347                 break;
348         default:
349                 if (yajl_gen_string(g, (u_char *)"unknown",
350                                     strlen("unknown")) !=
351                     yajl_gen_status_ok)
352                         goto err;
353                 break;
354         }
355         if (yajl_gen_map_close(g) != yajl_gen_status_ok)
356                 goto err;
357
358         log_logstash_print (g, LOG_INFO, (n->time != 0) ? n->time : cdtime ());
359         return (0);
360
361  err:
362         yajl_gen_free(g);
363         fprintf(stderr, "Could not correctly generate JSON notification\n");
364         return (0);
365 } /* int log_logstash_notification */
366
367 void module_register (void)
368 {
369         plugin_register_config ("log_logstash",
370                                 log_logstash_config,
371                                 config_keys,
372                                 config_keys_num);
373         plugin_register_log ("log_logstash",
374                              log_logstash_log,
375                              /* user_data = */ NULL);
376         plugin_register_notification ("log_logstash",
377                                       log_logstash_notification,
378                                       /* user_data = */ NULL);
379 } /* void module_register (void) */
380
381 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
382