-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod_log_graylog2.c
More file actions
699 lines (623 loc) · 23.1 KB
/
mod_log_graylog2.c
File metadata and controls
699 lines (623 loc) · 23.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/*
* mod_log_graylog2 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2014-2015, Grasshopper
* Copyright (C) 2026, Docheia SRL
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mod_graylog2 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is Grasshopper
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Christopher Rienzo <crienzo@grasshopper.com>
* Michael McGuinness <mmcguinness@grasshopper.com>
* Matteo Location <matteo@docheia.it>
*
* mod_log_graylog2.c -- Graylog2 GELF logger with TCP support, reconnection, and buffering
*
*/
#include <switch.h>
#include <sys/types.h>
#include <unistd.h>
SWITCH_MODULE_LOAD_FUNCTION(mod_log_graylog2_load);
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_log_graylog2_shutdown);
SWITCH_MODULE_DEFINITION(mod_log_graylog2, mod_log_graylog2_load, mod_log_graylog2_shutdown, NULL);
#define MAX_GELF_LOG_LEN 8192
#define UNCOMPRESSED_MAGIC "\037\074"
#define UNCOMPRESSED_MAGIC_LEN 2
typedef enum {
PROTOCOL_TCP = 0,
PROTOCOL_UDP = 1
} log_graylog2_protocol_t;
typedef enum {
CONN_DISCONNECTED = 0,
CONN_CONNECTED
} log_graylog2_conn_state_t;
static struct {
/** memory pool for this module */
switch_memory_pool_t *pool;
/** graylog2 server address */
const char *server_host;
/** graylog2 server port */
switch_port_t server_port;
/** minimum log level to allow */
switch_log_level_t log_level;
/** shutdown flag */
int shutdown;
/** prevents context shutdown until all threads are finished */
switch_thread_rwlock_t *shutdown_rwlock;
/** log delivery queue */
switch_queue_t *log_queue;
/** Fields to automatically add to session logs */
switch_event_t *session_fields;
/** If true, byte header for uncompressed GELF is sent. Might be required if using logstash */
int send_uncompressed_header;
/** GELF JSON Format */
switch_log_json_format_t gelf_format;
/** Protocol: TCP (default) or UDP */
log_graylog2_protocol_t protocol;
/** Maximum queue size (buffer capacity) */
uint32_t queue_size;
/** Base reconnect interval in milliseconds */
uint32_t reconnect_interval_ms;
/** Maximum reconnect backoff in milliseconds */
uint32_t reconnect_max_interval_ms;
/** DNS re-resolve interval in seconds (UDP only - not actual DNS TTL) */
uint32_t dns_resolve_interval_sec;
/** Count of messages dropped due to full queue */
volatile uint32_t queue_drops;
/** Timestamp of last queue-full warning (to rate-limit console output) */
switch_time_t last_queue_full_warning;
} globals;
/**
* Convert log level to graylog2 log level
*/
static int to_graylog2_level(switch_log_level_t level)
{
switch (level) {
case SWITCH_LOG_DEBUG: return 7;
case SWITCH_LOG_INFO: return 6;
case SWITCH_LOG_NOTICE: return 5;
case SWITCH_LOG_WARNING: return 4;
case SWITCH_LOG_ERROR: return 3;
case SWITCH_LOG_CRIT: return 2;
case SWITCH_LOG_ALERT: return 1;
default: return 8;
}
}
/**
* Encode log as GELF
*/
static char *to_gelf(const switch_log_node_t *node, switch_log_level_t log_level)
{
char *gelf_text = NULL;
cJSON *gelf = switch_log_node_to_json(node, to_graylog2_level(log_level), &globals.gelf_format, globals.session_fields);
cJSON_AddItemToObject(gelf, "_microtimestamp", cJSON_CreateNumber(node->timestamp));
gelf_text = cJSON_PrintUnformatted(gelf);
cJSON_Delete(gelf);
return gelf_text;
}
/**
* Resolve server hostname to sockaddr.
* Returns the full linked list of addresses via ->next for multi-record DNS.
* Does not log on failure - callers are responsible for logging.
*/
static switch_status_t resolve_server(switch_sockaddr_t **sa, switch_memory_pool_t *pool)
{
if (switch_sockaddr_info_get(sa, globals.server_host, SWITCH_UNSPEC, globals.server_port, 0, pool) != SWITCH_STATUS_SUCCESS) {
return SWITCH_STATUS_FALSE;
}
return SWITCH_STATUS_SUCCESS;
}
/**
* Check if needle address is present anywhere in the resolved address set.
* The set is a linked list via ->next as returned by switch_sockaddr_info_get().
*/
static switch_bool_t addr_in_set(switch_sockaddr_t *needle, switch_sockaddr_t *set)
{
switch_sockaddr_t *cur;
for (cur = set; cur; cur = cur->next) {
if (switch_cmp_addr(needle, cur, SWITCH_FALSE)) {
return SWITCH_TRUE;
}
}
return SWITCH_FALSE;
}
/**
* Open a TCP socket and connect to the resolved address
*/
static switch_socket_t *open_tcp_socket(switch_sockaddr_t *sa, switch_memory_pool_t *pool)
{
switch_socket_t *sock = NULL;
if (switch_socket_create(&sock, switch_sockaddr_get_family(sa), SOCK_STREAM, SWITCH_PROTO_TCP, pool) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "log_graylog2: failed to create TCP socket\n");
return NULL;
}
switch_socket_opt_set(sock, SWITCH_SO_KEEPALIVE, 1);
switch_socket_opt_set(sock, SWITCH_SO_TCP_NODELAY, 1);
switch_socket_opt_set(sock, SWITCH_SO_TCP_KEEPIDLE, 30);
switch_socket_opt_set(sock, SWITCH_SO_TCP_KEEPINTVL, 30);
/* Set send timeout to 30 seconds to bound failure detection */
switch_socket_timeout_set(sock, 30000000);
if (switch_socket_connect(sock, sa) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "log_graylog2: TCP connection failed to %s:%d\n", globals.server_host, globals.server_port);
switch_socket_close(sock);
return NULL;
}
return sock;
}
/**
* Open a UDP socket and connect to the resolved address
*/
static switch_socket_t *open_udp_socket(switch_sockaddr_t *sa, switch_memory_pool_t *pool)
{
switch_socket_t *sock = NULL;
if (switch_socket_create(&sock, switch_sockaddr_get_family(sa), SOCK_DGRAM, 0, pool) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "log_graylog2: failed to create UDP socket\n");
return NULL;
}
if (switch_socket_connect(sock, sa) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "log_graylog2: UDP connect failed to %s:%d\n", globals.server_host, globals.server_port);
switch_socket_close(sock);
return NULL;
}
return sock;
}
/**
* Close socket and set pointer to NULL
*/
static void close_socket(switch_socket_t **sock)
{
if (sock && *sock) {
switch_socket_close(*sock);
*sock = NULL;
}
}
/**
* Send GELF message over TCP with null-byte delimiter
*/
static switch_status_t send_gelf_tcp(switch_socket_t *sock, const char *log, switch_size_t len)
{
switch_status_t status;
switch_size_t send_len;
if (globals.send_uncompressed_header) {
send_len = UNCOMPRESSED_MAGIC_LEN;
status = switch_socket_send(sock, UNCOMPRESSED_MAGIC, &send_len);
if (status != SWITCH_STATUS_SUCCESS) return status;
}
send_len = len;
status = switch_socket_send(sock, log, &send_len);
if (status != SWITCH_STATUS_SUCCESS) return status;
/* GELF TCP null-byte delimiter */
send_len = 1;
status = switch_socket_send(sock, "\0", &send_len);
return status;
}
/**
* Send GELF message over UDP
*/
static switch_status_t send_gelf_udp(switch_socket_t *sock, const char *log, switch_size_t len)
{
switch_status_t status;
switch_size_t max_len = globals.send_uncompressed_header ? MAX_GELF_LOG_LEN - UNCOMPRESSED_MAGIC_LEN : MAX_GELF_LOG_LEN;
if (len > max_len) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Skipping large log (%u > %u)\n", (unsigned)len, (unsigned)max_len);
return SWITCH_STATUS_SUCCESS;
}
if (globals.send_uncompressed_header) {
char buf[MAX_GELF_LOG_LEN];
memcpy(buf, UNCOMPRESSED_MAGIC, UNCOMPRESSED_MAGIC_LEN);
memcpy(buf + UNCOMPRESSED_MAGIC_LEN, log, len);
len += UNCOMPRESSED_MAGIC_LEN;
status = switch_socket_send_nonblock(sock, (void *)buf, &len);
} else {
status = switch_socket_send_nonblock(sock, (void *)log, &len);
}
return status;
}
/**
* Sleep in small chunks so we can respond to shutdown quickly
*/
static void interruptible_sleep(uint32_t ms)
{
uint32_t remaining = ms;
while (remaining > 0 && !globals.shutdown) {
uint32_t chunk = (remaining > 100) ? 100 : remaining;
switch_yield(chunk * 1000);
remaining -= chunk;
}
}
/**
* Thread that delivers logs to graylog2 server
* Uses a state machine with reconnection and DNS re-resolution.
* Messages are buffered in the queue while disconnected.
*/
static void *SWITCH_THREAD_FUNC deliver_graylog2_thread(switch_thread_t *thread, void *obj)
{
switch_socket_t *sock = NULL;
switch_sockaddr_t *sa = NULL;
switch_memory_pool_t *conn_pool = NULL;
log_graylog2_conn_state_t state = CONN_DISCONNECTED;
uint32_t backoff_ms;
switch_time_t next_dns_check = 0;
uint32_t dns_resolve_failures = 0;
char *pending_log = NULL;
(void)thread;
(void)obj;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "log_graylog2: delivery thread started\n");
switch_thread_rwlock_rdlock(globals.shutdown_rwlock);
backoff_ms = globals.reconnect_interval_ms;
while (!globals.shutdown) {
switch (state) {
case CONN_DISCONNECTED:
{
/* Close existing socket and reclaim connection pool memory */
close_socket(&sock);
if (conn_pool) {
switch_core_destroy_memory_pool(&conn_pool);
}
switch_core_new_memory_pool(&conn_pool);
/* Resolve DNS */
if (resolve_server(&sa, conn_pool) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE,
"log_graylog2: DNS resolution failed for %s:%d, retry in %u ms\n",
globals.server_host, globals.server_port, backoff_ms);
interruptible_sleep(backoff_ms);
backoff_ms = (backoff_ms * 2 > globals.reconnect_max_interval_ms) ? globals.reconnect_max_interval_ms : backoff_ms * 2;
break;
}
/* Open socket */
if (globals.protocol == PROTOCOL_TCP) {
sock = open_tcp_socket(sa, conn_pool);
} else {
sock = open_udp_socket(sa, conn_pool);
}
if (!sock) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE,
"log_graylog2: connection to %s:%d failed, retry in %u ms\n",
globals.server_host, globals.server_port, backoff_ms);
interruptible_sleep(backoff_ms);
backoff_ms = (backoff_ms * 2 > globals.reconnect_max_interval_ms) ? globals.reconnect_max_interval_ms : backoff_ms * 2;
break;
}
/* Connected - reset backoff and DNS failure state */
backoff_ms = globals.reconnect_interval_ms;
dns_resolve_failures = 0;
next_dns_check = switch_micro_time_now() + ((switch_time_t)globals.dns_resolve_interval_sec * 1000000);
state = CONN_CONNECTED;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE,
"log_graylog2: connected to %s:%d via %s\n",
globals.server_host, globals.server_port,
globals.protocol == PROTOCOL_TCP ? "TCP" : "UDP");
break;
}
case CONN_CONNECTED:
{
switch_status_t send_status;
switch_size_t len;
/* UDP: periodically re-resolve DNS and reconnect only if address changed */
if (globals.protocol == PROTOCOL_UDP && globals.dns_resolve_interval_sec > 0) {
switch_time_t now = switch_micro_time_now();
if (now >= next_dns_check) {
switch_sockaddr_t *new_sa = NULL;
switch_memory_pool_t *tmp_pool = NULL;
/* Always schedule next check regardless of outcome */
next_dns_check = now + ((switch_time_t)globals.dns_resolve_interval_sec * 1000000);
switch_core_new_memory_pool(&tmp_pool);
if (resolve_server(&new_sa, tmp_pool) == SWITCH_STATUS_SUCCESS) {
if (!addr_in_set(sa, new_sa)) {
/* Current address no longer in resolved set - reconnect */
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE,
"log_graylog2: server address changed, reconnecting\n");
switch_core_destroy_memory_pool(&tmp_pool);
dns_resolve_failures = 0;
state = CONN_DISCONNECTED;
break;
}
/* Current address still in resolved set - no action needed */
dns_resolve_failures = 0;
} else {
dns_resolve_failures++;
if (dns_resolve_failures >= 3) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
"log_graylog2: %u consecutive DNS re-resolve failures for %s, forcing reconnect\n",
dns_resolve_failures, globals.server_host);
switch_core_destroy_memory_pool(&tmp_pool);
dns_resolve_failures = 0;
state = CONN_DISCONNECTED;
break;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
"log_graylog2: DNS re-resolve failed for %s (%u/%u), keeping current connection\n",
globals.server_host, dns_resolve_failures, 3);
}
switch_core_destroy_memory_pool(&tmp_pool);
}
}
/* Get a message: use pending_log if we have one from a failed send */
if (!pending_log) {
if (switch_queue_pop_timeout(globals.log_queue, (void *)&pending_log, 500000) != SWITCH_STATUS_SUCCESS) {
/* Timeout or interrupted - loop back to check shutdown */
pending_log = NULL;
break;
}
}
if (zstr(pending_log)) {
switch_safe_free(pending_log);
pending_log = NULL;
break;
}
len = strlen(pending_log);
if (globals.protocol == PROTOCOL_TCP) {
send_status = send_gelf_tcp(sock, pending_log, len);
} else {
send_status = send_gelf_udp(sock, pending_log, len);
}
if (send_status != SWITCH_STATUS_SUCCESS) {
/* Send failed - keep pending_log for retry after reconnect */
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE,
"log_graylog2: send failed, will reconnect\n");
state = CONN_DISCONNECTED;
} else {
/* Success - free the message */
switch_safe_free(pending_log);
pending_log = NULL;
}
break;
}
} /* switch */
} /* while */
/* Shutdown: clean up */
switch_safe_free(pending_log);
{
char *log;
while (switch_queue_trypop(globals.log_queue, (void *)&log) == SWITCH_STATUS_SUCCESS) {
switch_safe_free(log);
}
}
close_socket(&sock);
if (conn_pool) {
switch_core_destroy_memory_pool(&conn_pool);
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "log_graylog2 delivery thread finished\n");
switch_thread_rwlock_unlock(globals.shutdown_rwlock);
return NULL;
}
/**
* Create a new delivery thread
*/
static void start_deliver_graylog2_thread(switch_memory_pool_t *pool)
{
switch_thread_t *thread;
switch_threadattr_t *thd_attr = NULL;
switch_threadattr_create(&thd_attr, pool);
switch_threadattr_detach_set(thd_attr, 1);
switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
switch_thread_create(&thread, thd_attr, deliver_graylog2_thread, NULL, pool);
}
/**
* Stop delivery thread
*/
static void stop_deliver_graylog2_thread(void)
{
globals.shutdown = 1;
switch_queue_interrupt_all(globals.log_queue);
switch_thread_rwlock_wrlock(globals.shutdown_rwlock);
}
/**
* Handle log from core
*/
static switch_status_t mod_log_graylog2_logger(const switch_log_node_t *node, switch_log_level_t level)
{
if (!globals.shutdown && level <= globals.log_level && level != SWITCH_LOG_CONSOLE) {
if (!zstr(node->content) && !zstr(node->content + 1)) {
char *log = to_gelf(node, level);
if (switch_queue_trypush(globals.log_queue, log) != SWITCH_STATUS_SUCCESS) {
switch_time_t now = switch_micro_time_now();
globals.queue_drops++;
/* Rate-limit the console warning to at most once per 10 seconds */
if ((now - globals.last_queue_full_warning) > 10000000) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE,
"log_graylog2: queue full, dropped %u message(s) so far\n",
globals.queue_drops);
globals.last_queue_full_warning = now;
}
free(log);
}
}
}
return SWITCH_STATUS_SUCCESS;
}
/**
* Configure module
*/
static switch_status_t do_config(void)
{
switch_xml_t cfg, xml, settings;
if (!(xml = switch_xml_open_cfg("log_graylog2.conf", &cfg, NULL))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Open of log_graylog2.conf failed\n");
return SWITCH_STATUS_TERM;
}
/* set defaults */
globals.log_level = SWITCH_LOG_WARNING;
globals.server_host = "127.0.0.1";
globals.server_port = 12201;
globals.send_uncompressed_header = 0;
globals.protocol = PROTOCOL_TCP;
globals.queue_size = 25000;
globals.reconnect_interval_ms = 1000;
globals.reconnect_max_interval_ms = 30000;
globals.dns_resolve_interval_sec = 60;
if ((settings = switch_xml_child(cfg, "settings"))) {
switch_xml_t param;
switch_xml_t fields;
for (param = switch_xml_child(settings, "param"); param; param = param->next) {
char *name = (char *) switch_xml_attr_soft(param, "name");
char *value = (char *) switch_xml_attr_soft(param, "value");
if (zstr(name)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Ignoring empty param\n");
continue;
}
if (zstr(value)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Ignoring empty value for param \"%s\"\n", name);
continue;
}
if (!strcasecmp(name, "server-host")) {
globals.server_host = switch_core_strdup(globals.pool, value);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\"%s\" = \"%s\"\n", name, value);
} else if (!strcasecmp(name, "server-port")) {
int port = -1;
if (switch_is_number(value)) {
port = atoi(value);
}
if (port > 0 && port <= 65535) {
globals.server_port = port;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\"%s\" = \"%s\"\n", name, value);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Invalid port: \"%s\"\n", value);
}
} else if (!strcasecmp(name, "loglevel")) {
switch_log_level_t log_level = switch_log_str2level(value);
if (log_level == SWITCH_LOG_INVALID) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Ignoring invalid log level: \"%s\"\n", value);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\"%s\" = \"%s\"\n", name, value);
globals.log_level = log_level;
}
} else if (!strcasecmp(name, "send-uncompressed-header")) {
globals.send_uncompressed_header = switch_true(value);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\"%s\" = \"%s\"\n", name, value);
} else if (!strcasecmp(name, "protocol")) {
if (!strcasecmp(value, "udp")) {
globals.protocol = PROTOCOL_UDP;
} else {
globals.protocol = PROTOCOL_TCP;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\"%s\" = \"%s\"\n", name, value);
} else if (!strcasecmp(name, "queue-size")) {
int qs = atoi(value);
if (qs > 0) {
globals.queue_size = (uint32_t)qs;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\"%s\" = \"%s\"\n", name, value);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Invalid queue-size: \"%s\"\n", value);
}
} else if (!strcasecmp(name, "reconnect-interval")) {
int ri = atoi(value);
if (ri > 0) {
globals.reconnect_interval_ms = (uint32_t)ri;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\"%s\" = \"%s\"\n", name, value);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Invalid reconnect-interval: \"%s\"\n", value);
}
} else if (!strcasecmp(name, "reconnect-max-interval")) {
int ri = atoi(value);
if (ri > 0) {
globals.reconnect_max_interval_ms = (uint32_t)ri;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\"%s\" = \"%s\"\n", name, value);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Invalid reconnect-max-interval: \"%s\"\n", value);
}
} else if (!strcasecmp(name, "ident")) {
globals.gelf_format.ident.value = switch_core_strdup(globals.pool, value);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\"%s\" = \"%s\"\n", name, value);
} else if (!strcasecmp(name, "dns-resolve-interval")) {
int ttl = atoi(value);
if (ttl > 0) {
globals.dns_resolve_interval_sec = (uint32_t)ttl;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\"%s\" = \"%s\"\n", name, value);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Invalid dns-resolve-interval: \"%s\"\n", value);
}
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Ignoring unknown param: \"%s\"\n", name);
}
}
/* map session fields to channel variables */
if ((fields = switch_xml_child(settings, "fields"))) {
switch_xml_t field;
for (field = switch_xml_child(fields, "field"); field; field = field->next) {
char *name = (char *) switch_xml_attr_soft(field, "name");
char *variable = (char *) switch_xml_attr_soft(field, "variable");
if (zstr(name)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Ignoring unnamed session field\n");
continue;
}
if (zstr(variable)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Ignoring empty channel variable for session field \"%s\"\n", name);
continue;
}
switch_event_add_header_string(globals.session_fields, SWITCH_STACK_BOTTOM,
switch_core_strdup(globals.pool, name), switch_core_strdup(globals.pool, variable));
}
}
}
switch_xml_free(xml);
return SWITCH_STATUS_SUCCESS;
}
SWITCH_MODULE_LOAD_FUNCTION(mod_log_graylog2_load)
{
*module_interface = switch_loadable_module_create_module_interface(pool, modname);
memset(&globals, 0, sizeof(globals));
globals.pool = pool;
// define GELF JSON format mappings
globals.gelf_format.version.name = "version";
globals.gelf_format.version.value = "1.1";
globals.gelf_format.host.name = "host";
globals.gelf_format.timestamp.name = "timestamp";
globals.gelf_format.timestamp_divisor = 1000000; // convert microseconds to seconds
globals.gelf_format.level.name = "level";
globals.gelf_format.ident.name = "_ident";
globals.gelf_format.ident.value = "freeswitch";
globals.gelf_format.pid.name = "_pid";
globals.gelf_format.pid.value = switch_core_sprintf(pool, "%d", (int)getpid());
globals.gelf_format.uuid.name = "_uuid";
globals.gelf_format.file.name = "_file";
globals.gelf_format.line.name = "_line";
globals.gelf_format.function.name = "_function";
globals.gelf_format.full_message.name = "full_message";
globals.gelf_format.short_message.name = "short_message";
globals.gelf_format.custom_field_prefix = "_";
globals.gelf_format.sequence.name = "_sequence";
switch_event_create_plain(&globals.session_fields, SWITCH_EVENT_CHANNEL_DATA);
if (do_config() != SWITCH_STATUS_SUCCESS) {
return SWITCH_STATUS_TERM;
}
switch_thread_rwlock_create(&globals.shutdown_rwlock, pool);
switch_queue_create(&globals.log_queue, globals.queue_size, pool);
start_deliver_graylog2_thread(globals.pool);
switch_log_bind_logger(mod_log_graylog2_logger, SWITCH_LOG_DEBUG, SWITCH_FALSE);
return SWITCH_STATUS_SUCCESS;
}
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_log_graylog2_shutdown)
{
switch_log_unbind_logger(mod_log_graylog2_logger);
stop_deliver_graylog2_thread();
if (globals.session_fields) {
switch_event_destroy(&globals.session_fields);
}
return SWITCH_STATUS_SUCCESS;
}
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
*/