added rsyslog plugin

This commit is contained in:
roberto@oneiric64
2011-12-13 20:33:17 +01:00
parent 2a27b66d96
commit e2cea376e8
6 changed files with 126 additions and 25 deletions
+26 -9
View File
@@ -6,7 +6,6 @@ extern struct uwsgi_server uwsgi;
#define MAX_GELF 8192
struct graylog2_config {
struct sockaddr_in sin;
char *host;
char json_buf[MAX_GELF];
char escaped_buf[MAX_GELF];
@@ -25,6 +24,8 @@ ssize_t uwsgi_graylog2_logger(struct uwsgi_logger *ul, char *message, size_t len
ul->fd = socket(AF_INET, SOCK_DGRAM, 0);
if (ul->fd < 0) return -1 ;
uwsgi_socket_nb(ul->fd);
char *comma = strchr(uwsgi.choosen_logger_arg, ',');
if (!comma) return -1;
@@ -35,13 +36,13 @@ ssize_t uwsgi_graylog2_logger(struct uwsgi_logger *ul, char *message, size_t len
char *colon = strchr(uwsgi.choosen_logger_arg, ':');
if (!colon) return -1;
memset(&g2c.sin, 0, sizeof(struct sockaddr_in));
g2c.sin.sin_family = AF_INET;
g2c.sin.sin_port = htons(atoi(colon + 1));
memset(&ul->sin, 0, sizeof(struct sockaddr_in));
ul->sin.sin_family = AF_INET;
ul->sin.sin_port = htons(atoi(colon + 1));
*colon = 0;
g2c.sin.sin_addr.s_addr = inet_addr(uwsgi.choosen_logger_arg);
ul->sin.sin_addr.s_addr = inet_addr(uwsgi.choosen_logger_arg);
*colon = ':';
*comma = ',';
@@ -51,7 +52,9 @@ ssize_t uwsgi_graylog2_logger(struct uwsgi_logger *ul, char *message, size_t len
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] == '\\') {
@@ -64,15 +67,29 @@ ssize_t uwsgi_graylog2_logger(struct uwsgi_logger *ul, char *message, size_t 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, \"facility\": \"uWSGI-%s\" }",
g2c.host, (int)g2c.escaped_len, g2c.escaped_buf, (int)g2c.escaped_len, g2c.escaped_buf, (int) time(NULL), UWSGI_VERSION);
g2c.host, truncated, g2c.escaped_buf, (int)g2c.escaped_len, g2c.escaped_buf, (int) time(NULL), UWSGI_VERSION);
uLongf destLen = MAX_GELF;
if (rlen > 0) {
compress((Bytef *) g2c.buffer, &destLen, (Bytef *) g2c.json_buf, (uLong) rlen);
return sendto(ul->fd, g2c.buffer, destLen, 0, (const struct sockaddr *) &g2c.sin, sizeof(struct sockaddr_in));
if (compressBound((uLong) rlen) <= MAX_GELF) {
if (compress((Bytef *) g2c.buffer, &destLen, (Bytef *) g2c.json_buf, (uLong) rlen) == Z_OK) {
return sendto(ul->fd, g2c.buffer, destLen, 0, (const struct sockaddr *) &ul->sin, sizeof(struct sockaddr_in));
}
}
}
return -1;
+80
View File
@@ -0,0 +1,80 @@
#include "../../uwsgi.h"
extern struct uwsgi_server uwsgi;
#define MAX_SYSLOG_PKT 1024
ssize_t uwsgi_rsyslog_logger(struct uwsgi_logger *ul, char *message, size_t len) {
char buf[MAX_SYSLOG_PKT];
time_t current_time;
int portn = 514;
int rlen;
if (!ul->configured) {
if (!uwsgi.choosen_logger_arg) return -1;
ul->fd = socket(AF_INET, SOCK_DGRAM, 0);
if (ul->fd < 0) return -1 ;
uwsgi_socket_nb(ul->fd);
char *comma = strchr(uwsgi.choosen_logger_arg, ',');
if (comma) {
ul->data = comma+1;
*comma = 0;
}
else {
ul->data = "uwsgi";
}
char *port = strchr(uwsgi.choosen_logger_arg, ':');
if (port) {
portn = atoi(port+1);
*port = 0;
}
memset(&ul->sin, 0, sizeof(struct sockaddr_in));
ul->sin.sin_family = AF_INET;
ul->sin.sin_port = htons(portn);
ul->sin.sin_addr.s_addr = inet_addr(uwsgi.choosen_logger_arg);
if (port) *port = ':';
if (comma) *comma = ',';
ul->configured = 1;
}
current_time = time(NULL);
// drop newline
if (message[len-1] == '\n') len--;
rlen = snprintf(buf, MAX_SYSLOG_PKT, "<29>%.*s %s %s: %.*s", 19, ctime(&current_time), uwsgi.hostname, (char *) ul->data, (int) len, message);
if (rlen > 0) {
return sendto(ul->fd, buf, rlen, 0, (const struct sockaddr *) &ul->sin, sizeof(struct sockaddr_in));
}
return -1;
}
void uwsgi_rsyslog_register() {
uwsgi_register_logger("rsyslog", uwsgi_rsyslog_logger);
}
int uwsgi_rsyslog_init() {
return 0;
}
struct uwsgi_plugin rsyslog_plugin = {
.name = "rsyslog",
.on_load = uwsgi_rsyslog_register,
.init = uwsgi_rsyslog_init
};
+6
View File
@@ -0,0 +1,6 @@
NAME='rsyslog'
CFLAGS = []
LDFLAGS = []
LIBS = []
GCC_LIST = ['rsyslog_plugin']