log_logstash plugin: fix indentation
[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 <yajl/yajl_common.h>
35 #include <yajl/yajl_gen.h>
36 #if HAVE_YAJL_YAJL_VERSION_H
37 # include <yajl/yajl_version.h>
38 #endif
39 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
40 # define HAVE_YAJL_V2 1
41 #endif
42
43 #define DEFAULT_LOGFILE LOCALSTATEDIR"/log/"PACKAGE_NAME".json.log"
44
45 #if COLLECT_DEBUG
46 static int log_level = LOG_DEBUG;
47 #else
48 static int log_level = LOG_INFO;
49 #endif /* COLLECT_DEBUG */
50
51 static pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
52
53 static char *log_file = NULL;
54
55 static const char *config_keys[] =
56 {
57         "LogLevel",
58         "File"
59 };
60 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
61
62 static int log_logstash_config (const char *key, const char *value)
63 {
64
65         if (0 == strcasecmp (key, "LogLevel")) {
66                 log_level = parse_log_severity(value);
67                 if (log_level < 0) {
68                         log_level = LOG_INFO;
69                         ERROR("log_logstash: invalid loglevel [%s] defaulting to 'info'",
70                                 value);
71                         return 1;
72                 }
73         }
74         else if (0 == strcasecmp (key, "File")) {
75                 sfree (log_file);
76                 log_file = strdup (value);
77         }
78         else {
79                 return -1;
80         }
81         return 0;
82 } /* int log_logstash_config (const char *, const char *) */
83
84 static void log_logstash_print (yajl_gen g, int severity,
85                 cdtime_t timestamp_time)
86 {
87         FILE *fh;
88         _Bool do_close = 0;
89         struct tm timestamp_tm;
90         char timestamp_str[64];
91         const unsigned char *buf;
92         time_t tt;
93 #if HAVE_YAJL_V2
94         size_t len;
95 #else
96         unsigned int len;
97 #endif
98
99         if (yajl_gen_string(g, (u_char *)"level", strlen("level")) !=
100             yajl_gen_status_ok)
101                 goto err;
102
103         switch (severity) {
104         case LOG_ERR:
105                 if (yajl_gen_string(g, (u_char *)"error", strlen("error")) !=
106                     yajl_gen_status_ok)
107                         goto err;
108                 break;
109         case LOG_WARNING:
110                 if (yajl_gen_string(g, (u_char *)"warning",
111                                     strlen("warning")) !=
112                     yajl_gen_status_ok)
113                         goto err;
114                 break;
115         case LOG_NOTICE:
116                 if (yajl_gen_string(g, (u_char *)"notice", strlen("notice")) !=
117                     yajl_gen_status_ok)
118                         goto err;
119                 break;
120         case LOG_INFO:
121                 if (yajl_gen_string(g, (u_char *)"info", strlen("info")) !=
122                     yajl_gen_status_ok)
123                         goto err;
124                 break;
125         case LOG_DEBUG:
126                 if (yajl_gen_string(g, (u_char *)"debug", strlen("debug")) !=
127                     yajl_gen_status_ok)
128                         goto err;
129                 break;
130         default:
131                 if (yajl_gen_string(g, (u_char *)"unknown",
132                                     strlen("unknown")) !=
133                     yajl_gen_status_ok)
134                         goto err;
135                 break;
136         }
137
138         if (yajl_gen_string(g, (u_char *)"@timestamp", strlen("@timestamp")) !=
139             yajl_gen_status_ok)
140                 goto err;
141
142         tt = CDTIME_T_TO_TIME_T (timestamp_time);
143         gmtime_r (&tt, &timestamp_tm);
144
145         /*
146          * format time as a UTC ISO 8601 compliant string
147          */
148         strftime (timestamp_str, sizeof (timestamp_str),
149                   "%Y-%m-%dT%H:%M:%SZ", &timestamp_tm);
150         timestamp_str[sizeof (timestamp_str) - 1] = '\0';
151
152         if (yajl_gen_string(g, (u_char *)timestamp_str,
153                             strlen(timestamp_str)) !=
154             yajl_gen_status_ok)
155                 goto err;
156
157         if (yajl_gen_map_close(g) != yajl_gen_status_ok)
158                 goto err;
159
160         if (yajl_gen_get_buf(g, &buf, &len) != yajl_gen_status_ok)
161                 goto err;
162         pthread_mutex_lock (&file_lock);
163
164         if (log_file == NULL)
165         {
166                 fh = fopen (DEFAULT_LOGFILE, "a");
167                 do_close = 1;
168         } else if (strcasecmp(log_file, "stdout") == 0) {
169         fh = stdout;
170         do_close = 0;
171         } else if (strcasecmp(log_file, "stderr") == 0) {
172         fh = stderr;
173         do_close = 0;
174         } else {
175                 fh = fopen (log_file, "a");
176                 do_close = 1;
177         }
178
179         if (fh == NULL)
180         {
181                         char errbuf[1024];
182                         fprintf (stderr, "log_logstash plugin: fopen (%s) failed: %s\n",
183                                         (log_file == NULL) ? DEFAULT_LOGFILE : log_file,
184                                         sstrerror (errno, errbuf, sizeof (errbuf)));
185         }
186         else
187         {
188                 fprintf(fh, "%s\n", buf);
189                 if (do_close) {
190                         fclose (fh);
191                 } else {
192                         fflush(fh);
193                 }
194         }
195         pthread_mutex_unlock (&file_lock);
196         yajl_gen_free(g);
197         return;
198
199  err:
200         yajl_gen_free(g);
201         fprintf(stderr, "Could not correctly generate JSON message\n");
202         return;
203 } /* void log_logstash_print */
204
205 static void log_logstash_log (int severity, const char *msg,
206                 user_data_t __attribute__((unused)) *user_data)
207 {
208         yajl_gen g;
209 #if !defined(HAVE_YAJL_V2)
210         yajl_gen_config conf = {};
211
212         conf.beautify = 0;
213 #endif
214
215         if (severity > log_level)
216                 return;
217
218 #if HAVE_YAJL_V2
219         g = yajl_gen_alloc(NULL);
220 #else
221         g = yajl_gen_alloc(&conf, NULL);
222 #endif
223
224         if (g == NULL) {
225                 fprintf(stderr, "Could not allocate JSON generator.\n");
226                 return;
227         }
228
229         if (yajl_gen_map_open(g) != yajl_gen_status_ok)
230                 goto err;
231         if (yajl_gen_string(g, (u_char *)"message", strlen("message")) !=
232             yajl_gen_status_ok)
233                 goto err;
234         if (yajl_gen_string(g, (u_char *)msg, strlen(msg)) !=
235             yajl_gen_status_ok)
236                 goto err;
237
238         log_logstash_print (g, severity, cdtime ());
239         return;
240  err:
241         yajl_gen_free(g);
242         fprintf(stderr, "Could not generate JSON message preamble\n");
243         return;
244
245 } /* void log_logstash_log (int, const char *) */
246
247 static int log_logstash_notification (const notification_t *n,
248                 user_data_t __attribute__((unused)) *user_data)
249 {
250         yajl_gen g;
251 #if HAVE_YAJL_V2
252         g = yajl_gen_alloc(NULL);
253 #else
254         yajl_gen_config conf = {};
255
256         conf.beautify = 0;
257         g = yajl_gen_alloc(&conf, NULL);
258 #endif
259
260         if (g == NULL) {
261                 fprintf(stderr, "Could not allocate JSON generator.\n");
262                 return (0);
263         }
264
265         if (yajl_gen_map_open(g) != yajl_gen_status_ok)
266                 goto err;
267         if (yajl_gen_string(g, (u_char *)"message", strlen("message")) !=
268             yajl_gen_status_ok)
269                 goto err;
270         if (strlen(n->message) > 0) {
271                 if (yajl_gen_string(g, (u_char *)n->message,
272                                     strlen(n->message)) !=
273                     yajl_gen_status_ok)
274                         goto err;
275         } else {
276                 if (yajl_gen_string(g, (u_char *)"notification without a message",
277                                     strlen("notification without a message")) !=
278                     yajl_gen_status_ok)
279                         goto err;
280         }
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
360         log_logstash_print (g, LOG_INFO, (n->time != 0) ? n->time : cdtime ());
361         return (0);
362
363  err:
364         yajl_gen_free(g);
365         fprintf(stderr, "Could not correctly generate JSON notification\n");
366         return (0);
367 } /* int log_logstash_notification */
368
369 void module_register (void)
370 {
371         plugin_register_config ("log_logstash",
372                                 log_logstash_config,
373                                 config_keys,
374                                 config_keys_num);
375         plugin_register_log ("log_logstash",
376                              log_logstash_log,
377                              /* user_data = */ NULL);
378         plugin_register_notification ("log_logstash",
379                                       log_logstash_notification,
380                                       /* user_data = */ NULL);
381 } /* void module_register (void) */
382
383 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */