24083a5081e1445d78d61002f3b111fdd0c14020
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"
31 #include "common.h"
32 #include "plugin.h"
34 #include <sys/types.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 #if HAVE_YAJL_V2
94 size_t len;
95 #else
96 unsigned int len;
97 #endif
99 if (yajl_gen_string(g, (u_char *)"level", strlen("level")) !=
100 yajl_gen_status_ok)
101 goto err;
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 }
138 if (yajl_gen_string(g, (u_char *)"@timestamp", strlen("@timestamp")) !=
139 yajl_gen_status_ok)
140 goto err;
142 gmtime_r (&CDTIME_T_TO_TIME_T (timestamp_time), ×tamp_tm);
144 /*
145 * format time as a UTC ISO 8601 compliant string
146 */
147 strftime (timestamp_str, sizeof (timestamp_str),
148 "%Y-%m-%dT%H:%M:%SZ", ×tamp_tm);
149 timestamp_str[sizeof (timestamp_str) - 1] = '\0';
151 if (yajl_gen_string(g, (u_char *)timestamp_str,
152 strlen(timestamp_str)) !=
153 yajl_gen_status_ok)
154 goto err;
156 if (yajl_gen_map_close(g) != yajl_gen_status_ok)
157 goto err;
159 if (yajl_gen_get_buf(g, &buf, &len) != yajl_gen_status_ok)
160 goto err;
161 pthread_mutex_lock (&file_lock);
163 if (log_file == NULL)
164 {
165 fh = fopen (DEFAULT_LOGFILE, "a");
166 do_close = 1;
167 } else if (strcasecmp(log_file, "stdout") == 0) {
168 fh = stdout;
169 do_close = 0;
170 } else if (strcasecmp(log_file, "stderr") == 0) {
171 fh = stderr;
172 do_close = 0;
173 } else {
174 fh = fopen (log_file, "a");
175 do_close = 1;
176 }
178 if (fh == NULL)
179 {
180 char errbuf[1024];
181 fprintf (stderr, "log_logstash plugin: fopen (%s) failed: %s\n",
182 (log_file == NULL) ? DEFAULT_LOGFILE : log_file,
183 sstrerror (errno, errbuf, sizeof (errbuf)));
184 }
185 else
186 {
187 fprintf(fh, "%s\n", buf);
188 if (do_close) {
189 fclose (fh);
190 } else {
191 fflush(fh);
192 }
193 }
194 pthread_mutex_unlock (&file_lock);
195 yajl_gen_free(g);
196 return;
198 err:
199 yajl_gen_free(g);
200 fprintf(stderr, "Could not correctly generate JSON message\n");
201 return;
202 } /* void log_logstash_print */
204 static void log_logstash_log (int severity, const char *msg,
205 user_data_t __attribute__((unused)) *user_data)
206 {
207 yajl_gen g;
208 #if !defined(HAVE_YAJL_V2)
209 yajl_gen_config conf = {};
211 conf.beautify = 0;
212 #endif
214 if (severity > log_level)
215 return;
217 #if HAVE_YAJL_V2
218 g = yajl_gen_alloc(NULL);
219 #else
220 g = yajl_gen_alloc(&conf, NULL);
221 #endif
223 if (g == NULL) {
224 fprintf(stderr, "Could not allocate JSON generator.\n");
225 return;
226 }
228 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
229 goto err;
230 if (yajl_gen_string(g, (u_char *)"message", strlen("message")) !=
231 yajl_gen_status_ok)
232 goto err;
233 if (yajl_gen_string(g, (u_char *)msg, strlen(msg)) !=
234 yajl_gen_status_ok)
235 goto err;
237 log_logstash_print (g, severity, cdtime ());
238 return;
239 err:
240 yajl_gen_free(g);
241 fprintf(stderr, "Could not generate JSON message preamble\n");
242 return;
244 } /* void log_logstash_log (int, const char *) */
246 static int log_logstash_notification (const notification_t *n,
247 user_data_t __attribute__((unused)) *user_data)
248 {
249 yajl_gen g;
250 #if HAVE_YAJL_V2
251 g = yajl_gen_alloc(NULL);
252 #else
253 yajl_gen_config conf = {};
255 conf.beautify = 0;
256 g = yajl_gen_alloc(&conf, NULL);
257 #endif
259 if (g == NULL) {
260 fprintf(stderr, "Could not allocate JSON generator.\n");
261 return (0);
262 }
264 if (yajl_gen_map_open(g) != yajl_gen_status_ok)
265 goto err;
266 if (yajl_gen_string(g, (u_char *)"message", strlen("message")) !=
267 yajl_gen_status_ok)
268 goto err;
269 if (strlen(n->message) > 0) {
270 if (yajl_gen_string(g, (u_char *)n->message,
271 strlen(n->message)) !=
272 yajl_gen_status_ok)
273 goto err;
274 } else {
275 if (yajl_gen_string(g, (u_char *)"notification without a message",
276 strlen("notification without a message")) !=
277 yajl_gen_status_ok)
278 goto err;
279 }
281 if (strlen(n->host) > 0) {
282 if (yajl_gen_string(g, (u_char *)"host", strlen("host")) !=
283 yajl_gen_status_ok)
284 goto err;
285 if (yajl_gen_string(g, (u_char *)n->host, strlen(n->host)) !=
286 yajl_gen_status_ok)
287 goto err;
289 }
290 if (strlen(n->plugin) > 0) {
291 if (yajl_gen_string(g, (u_char *)"plugin", strlen("plugin")) !=
292 yajl_gen_status_ok)
293 goto err;
294 if (yajl_gen_string(g, (u_char *)n->plugin, strlen(n->plugin)) !=
295 yajl_gen_status_ok)
296 goto err;
297 }
298 if (strlen(n->plugin_instance) > 0) {
299 if (yajl_gen_string(g, (u_char *)"plugin_instance",
300 strlen("plugin_instance")) !=
301 yajl_gen_status_ok)
302 goto err;
303 if (yajl_gen_string(g, (u_char *)n->plugin_instance,
304 strlen(n->plugin_instance)) !=
305 yajl_gen_status_ok)
306 goto err;
307 }
308 if (strlen(n->type) > 0) {
309 if (yajl_gen_string(g, (u_char *)"type", strlen("type")) !=
310 yajl_gen_status_ok)
311 goto err;
312 if (yajl_gen_string(g, (u_char *)n->type, strlen(n->type)) !=
313 yajl_gen_status_ok)
314 goto err;
315 }
316 if (strlen(n->type_instance) > 0) {
317 if (yajl_gen_string(g, (u_char *)"type_instance",
318 strlen("type_instance")) !=
319 yajl_gen_status_ok)
320 goto err;
321 if (yajl_gen_string(g, (u_char *)n->type_instance,
322 strlen(n->type_instance)) !=
323 yajl_gen_status_ok)
324 goto err;
325 }
327 if (yajl_gen_string(g, (u_char *)"severity",
328 strlen("severity")) !=
329 yajl_gen_status_ok)
330 goto err;
332 switch (n->severity) {
333 case NOTIF_FAILURE:
334 if (yajl_gen_string(g, (u_char *)"failure",
335 strlen("failure")) !=
336 yajl_gen_status_ok)
337 goto err;
338 break;
339 case NOTIF_WARNING:
340 if (yajl_gen_string(g, (u_char *)"warning",
341 strlen("warning")) !=
342 yajl_gen_status_ok)
343 goto err;
344 break;
345 case NOTIF_OKAY:
346 if (yajl_gen_string(g, (u_char *)"ok",
347 strlen("ok")) !=
348 yajl_gen_status_ok)
349 goto err;
350 break;
351 default:
352 if (yajl_gen_string(g, (u_char *)"unknown",
353 strlen("unknown")) !=
354 yajl_gen_status_ok)
355 goto err;
356 break;
357 }
359 log_logstash_print (g, LOG_INFO, (n->time != 0) ? n->time : cdtime ());
360 return (0);
362 err:
363 yajl_gen_free(g);
364 fprintf(stderr, "Could not correctly generate JSON notification\n");
365 return (0);
366 } /* int log_logstash_notification */
368 void module_register (void)
369 {
370 plugin_register_config ("log_logstash",
371 log_logstash_config,
372 config_keys,
373 config_keys_num);
374 plugin_register_log ("log_logstash",
375 log_logstash_log,
376 /* user_data = */ NULL);
377 plugin_register_notification ("log_logstash",
378 log_logstash_notification,
379 /* user_data = */ NULL);
380 } /* void module_register (void) */
382 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */