mirror of
https://github.com/Theldus/alertik.git
synced 2024-11-21 15:33:15 +01:00
Add forward mode
This commit is contained in:
parent
82f59c760b
commit
db858ecdf8
@ -60,6 +60,8 @@ int main(void)
|
||||
panic("No event was configured, please configure at least one\n"
|
||||
"before proceeding!\n");
|
||||
|
||||
syslog_init_forward();
|
||||
|
||||
fd = syslog_create_udp_socket();
|
||||
if (pthread_create(&handler, NULL, handle_messages, NULL))
|
||||
panic_errno("Unable to create hanler thread!");
|
||||
|
2
log.h
2
log.h
@ -27,6 +27,8 @@
|
||||
exit(EXIT_FAILURE); \
|
||||
} while(0);
|
||||
|
||||
#define log_errno(s) log_msg("%s: %s", (s), strerror(errno))
|
||||
|
||||
#define LOG_FILE "log/log.txt"
|
||||
|
||||
extern char *get_formatted_time(time_t time, char *time_str);
|
||||
|
76
syslog.c
76
syslog.c
@ -9,6 +9,7 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "events.h"
|
||||
@ -19,6 +20,10 @@
|
||||
* UDP message handling and FIFO.
|
||||
*/
|
||||
|
||||
/* Forward server data. */
|
||||
static struct addrinfo *fwd_addr_info;
|
||||
static int fwd_fd;
|
||||
|
||||
/* Circular message buffer. */
|
||||
static struct circ_buffer {
|
||||
int head;
|
||||
@ -64,9 +69,72 @@ int syslog_create_udp_socket(void)
|
||||
return fd;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Initializes the forwarding to the syslog server if
|
||||
* the required environment vars were informed.
|
||||
*
|
||||
* @return Returns 0.
|
||||
*/
|
||||
int syslog_init_forward(void)
|
||||
{
|
||||
struct addrinfo hints, *results, *try;
|
||||
char *host, *port;
|
||||
int sock = 0;
|
||||
|
||||
/* Check if we should forward messages. */
|
||||
host = getenv("FORWARD_HOST");
|
||||
port = getenv("FORWARD_PORT");
|
||||
if (!host && !port) {
|
||||
log_msg("Forward Mode: disabled\n\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!host || !port)
|
||||
panic("FORWARD_ADDR and FORWARD_PORT must be specified!\n");
|
||||
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_DGRAM;
|
||||
|
||||
if (getaddrinfo(host, port, &hints, &results) != 0)
|
||||
panic_errno("Unable to getaddrinfo...");
|
||||
|
||||
/* Iterate over results. */
|
||||
for (try = results; try != NULL; try = try->ai_next) {
|
||||
sock = socket(try->ai_family, try->ai_socktype, try->ai_protocol);
|
||||
if (sock < 0)
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
|
||||
if (sock < 0)
|
||||
panic("Unable to create a socket for forward...\n");
|
||||
|
||||
fwd_fd = sock;
|
||||
fwd_addr_info = try;
|
||||
|
||||
log_msg("Forward Mode: enabled:\n");
|
||||
log_msg("----------------------\n");
|
||||
log_msg("FORWARD_HOST: %s\n", host);
|
||||
log_msg("FORWARD_PORT: %s\n\n", port);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Sends a message @p msg of length @p len to the
|
||||
* configured syslog server.
|
||||
*/
|
||||
static int syslog_fwd_msg(const char *msg, size_t len) {
|
||||
return (
|
||||
sendto(fwd_fd, msg, len, 0, fwd_addr_info->ai_addr,
|
||||
fwd_addr_info->ai_addrlen)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Receives a new UDP message and then adds it
|
||||
* to the message queue.
|
||||
* to the message queue. Additionally, also forwards
|
||||
* the message to a previously configured syslog server.
|
||||
*
|
||||
* @param fd UDP file descriptor to receive from.
|
||||
*
|
||||
@ -86,6 +154,12 @@ int syslog_enqueue_new_upd_msg(int fd)
|
||||
if (ret < 0)
|
||||
return -1;
|
||||
|
||||
/* Forward message if forwarding was configured. */
|
||||
if (fwd_fd) {
|
||||
if (syslog_fwd_msg(msg, ret) < 0)
|
||||
log_errno("Unable to forward message...\n");
|
||||
}
|
||||
|
||||
if (syslog_push_msg_into_fifo(msg, time(NULL)) < 0)
|
||||
panic("Circular buffer full! (size: %d)\n", FIFO_MAX);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user