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 **/
29 #include "collectd.h"
30 #include "common.h"
31 #include "plugin.h"
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
44 #define DEFAULT_LOGFILE LOCALSTATEDIR"/log/"PACKAGE_NAME".json.log"
46 #if COLLECT_DEBUG
47 static int log_level = LOG_DEBUG;
48 #else
49 static int log_level = LOG_INFO;
50 #endif /* COLLECT_DEBUG */
52 static pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
54 static char *log_file = NULL;
56 static const char *config_keys[] =
57 {
58 "LogLevel",
59 "File"
60 };
61 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
63 static int log_logstash_config (const char *key, const char *value)
64 {
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 *) */
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
100 if (yajl_gen_string(g, (u_char *)"level", strlen("level")) !=
101 yajl_gen_status_ok)
102 goto err;
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 }
139 if (yajl_gen_string(g, (u_char *)"@timestamp", strlen("@timestamp")) !=
140 yajl_gen_status_ok)
141 goto err;
143 tt = CDTIME_T_TO_TIME_T (timestamp_time);
144 gmtime_r (&tt, ×tamp_tm);
146 /*
147 * format time as a UTC ISO 8601 compliant string
148 */
149 strftime (timestamp_str, sizeof (timestamp_str),
150 "%Y-%m-%dT%H:%M:%SZ", ×tamp_tm);
151 timestamp_str[sizeof (timestamp_str) - 1] = '\0';
153 if (yajl_gen_string(g, (u_char *)timestamp_str,
154 strlen(timestamp_str)) !=
155 yajl_gen_status_ok)
156 goto err;
158 if (yajl_gen_map_close(g) != yajl_gen_status_ok)
159 goto err;
161 if (yajl_gen_get_buf(g, &buf, &len) != yajl_gen_status_ok)
162 goto err;
163 pthread_mutex_lock (&file_lock);
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 }
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;
200 err:
201 yajl_gen_free(g);
202 fprintf(stderr, "Could not correctly generate JSON message\n");
203 return;
204 } /* void log_logstash_print */
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 = {};
213 conf.beautify = 0;
214 #endif
216 if (severity > log_level)
217 return;
219 #if HAVE_YAJL_V2
220 g = yajl_gen_alloc(NULL);
221 #else
222 g = yajl_gen_alloc(&conf, NULL);
223 #endif
225 if (g == NULL) {
226 fprintf(stderr, "Could not allocate JSON generator.\n");
227 return;
228 }
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;
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;
246 } /* void log_logstash_log (int, const char *) */
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 = {};
257 conf.beautify = 0;
258 g = yajl_gen_alloc(&conf, NULL);
259 #endif
261 if (g == NULL) {
262 fprintf(stderr, "Could not allocate JSON generator.\n");
263 return (0);
264 }
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 }
283 if (strlen(n->host) > 0) {
284 if (yajl_gen_string(g, (u_char *)"host", strlen("host")) !=
285 yajl_gen_status_ok)
286 goto err;
287 if (yajl_gen_string(g, (u_char *)n->host, strlen(n->host)) !=
288 yajl_gen_status_ok)
289 goto err;
291 }
292 if (strlen(n->plugin) > 0) {
293 if (yajl_gen_string(g, (u_char *)"plugin", strlen("plugin")) !=
294 yajl_gen_status_ok)
295 goto err;
296 if (yajl_gen_string(g, (u_char *)n->plugin, strlen(n->plugin)) !=
297 yajl_gen_status_ok)
298 goto err;
299 }
300 if (strlen(n->plugin_instance) > 0) {
301 if (yajl_gen_string(g, (u_char *)"plugin_instance",
302 strlen("plugin_instance")) !=
303 yajl_gen_status_ok)
304 goto err;
305 if (yajl_gen_string(g, (u_char *)n->plugin_instance,
306 strlen(n->plugin_instance)) !=
307 yajl_gen_status_ok)
308 goto err;
309 }
310 if (strlen(n->type) > 0) {
311 if (yajl_gen_string(g, (u_char *)"type", strlen("type")) !=
312 yajl_gen_status_ok)
313 goto err;
314 if (yajl_gen_string(g, (u_char *)n->type, strlen(n->type)) !=
315 yajl_gen_status_ok)
316 goto err;
317 }
318 if (strlen(n->type_instance) > 0) {
319 if (yajl_gen_string(g, (u_char *)"type_instance",
320 strlen("type_instance")) !=
321 yajl_gen_status_ok)
322 goto err;
323 if (yajl_gen_string(g, (u_char *)n->type_instance,
324 strlen(n->type_instance)) !=
325 yajl_gen_status_ok)
326 goto err;
327 }
329 if (yajl_gen_string(g, (u_char *)"severity",
330 strlen("severity")) !=
331 yajl_gen_status_ok)
332 goto err;
334 switch (n->severity) {
335 case NOTIF_FAILURE:
336 if (yajl_gen_string(g, (u_char *)"failure",
337 strlen("failure")) !=
338 yajl_gen_status_ok)
339 goto err;
340 break;
341 case NOTIF_WARNING:
342 if (yajl_gen_string(g, (u_char *)"warning",
343 strlen("warning")) !=
344 yajl_gen_status_ok)
345 goto err;
346 break;
347 case NOTIF_OKAY:
348 if (yajl_gen_string(g, (u_char *)"ok",
349 strlen("ok")) !=
350 yajl_gen_status_ok)
351 goto err;
352 break;
353 default:
354 if (yajl_gen_string(g, (u_char *)"unknown",
355 strlen("unknown")) !=
356 yajl_gen_status_ok)
357 goto err;
358 break;
359 }
361 log_logstash_print (g, LOG_INFO, (n->time != 0) ? n->time : cdtime ());
362 return (0);
364 err:
365 yajl_gen_free(g);
366 fprintf(stderr, "Could not correctly generate JSON notification\n");
367 return (0);
368 } /* int log_logstash_notification */
370 void module_register (void)
371 {
372 plugin_register_config ("log_logstash",
373 log_logstash_config,
374 config_keys,
375 config_keys_num);
376 plugin_register_log ("log_logstash",
377 log_logstash_log,
378 /* user_data = */ NULL);
379 plugin_register_notification ("log_logstash",
380 log_logstash_notification,
381 /* user_data = */ NULL);
382 } /* void module_register (void) */
384 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */