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