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