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
|
#include "../../uwsgi.h"
#include <zlib.h>
extern struct uwsgi_server uwsgi;
#define MAX_GELF 8192
struct graylog2_config {
char *host;
char json_buf[MAX_GELF];
char escaped_buf[MAX_GELF];
size_t escaped_len;
} g2c;
ssize_t uwsgi_graylog2_logger(struct uwsgi_logger *ul, char *message, size_t len) {
size_t i;
if (!ul->configured) {
if (!ul->arg) {
uwsgi_log_safe("invalid graylog2 syntax\n");
exit(1);
}
ul->fd = socket(AF_INET, SOCK_DGRAM, 0);
if (ul->fd < 0) {
uwsgi_error_safe("socket()");
exit(1);
}
uwsgi_socket_nb(ul->fd);
char *comma = strchr(ul->arg, ',');
if (!comma) {
uwsgi_log_safe("invalid graylog2 syntax\n");
exit(1);
}
g2c.host = comma + 1;
*comma = 0;
char *colon = strchr(ul->arg, ':');
if (!colon) {
uwsgi_log_safe("invalid graylog2 syntax\n");
exit(1);
}
ul->addr_len = socket_to_in_addr(ul->arg, colon, 0, &ul->addr.sa_in);
*comma = ',';
ul->buf = uwsgi_malloc(MAX_GELF);
ul->configured = 1;
}
g2c.escaped_len = 0;
int truncated = 0;
char *ptr = g2c.escaped_buf;
uLongf destLen = MAX_GELF;
for(i=0;i<len;i++) {
if (message[i] == '\\') {
*ptr ++= '\\';
g2c.escaped_len++;
}
else if (message[i] == '"') {
*ptr ++= '\\';
g2c.escaped_len++;
}
*ptr ++= message[i];
g2c.escaped_len++;
if (!truncated) {
if (g2c.escaped_len == 128) {
truncated = 1;
}
else if (g2c.escaped_len > 128) {
truncated = 2;
}
}
}
if (truncated) truncated = 128 - (truncated-1);
else (truncated = g2c.escaped_len);
int rlen = snprintf(g2c.json_buf, MAX_GELF, "{ \"version\": \"1.0\", \"host\": \"%s\", \"short_message\": \"%.*s\", \"full_message\": \"%.*s\", \"timestamp\": %d, \"level\": 5, \"facility\": \"uWSGI-%s\" }",
g2c.host, truncated, g2c.escaped_buf, (int)g2c.escaped_len, g2c.escaped_buf, (int) uwsgi_now(), UWSGI_VERSION);
if (rlen > 0 && rlen < MAX_GELF) {
if (compressBound((uLong) rlen) <= MAX_GELF) {
if (compress((Bytef *) ul->buf, &destLen, (Bytef *) g2c.json_buf, (uLong) rlen) == Z_OK) {
return sendto(ul->fd, ul->buf, destLen, 0, (const struct sockaddr *) &ul->addr, ul->addr_len);
}
}
}
return -1;
}
void uwsgi_graylog2_register() {
uwsgi_register_logger("graylog2", uwsgi_graylog2_logger);
}
struct uwsgi_plugin graylog2_plugin = {
.name = "graylog2",
.on_load = uwsgi_graylog2_register,
};
|