alertik/alertik.c

74 lines
1.5 KiB
C
Raw Normal View History

2024-05-28 04:14:29 +02:00
/*
* Alertik: a tiny 'syslog' server & notification tool for Mikrotik routers.
* This is free and unencumbered software released into the public domain.
*/
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <pthread.h>
2024-05-31 03:28:28 +02:00
#include "alertik.h"
#include "events.h"
2024-07-16 03:16:27 +02:00
#include "env_events.h"
2024-07-16 03:47:57 +02:00
#include "log.h"
2024-07-16 03:16:27 +02:00
#include "notifiers.h"
2024-07-17 03:34:38 +02:00
#include "syslog.h"
2024-05-28 04:14:29 +02:00
/*
* Alertik
*/
2024-05-28 04:14:29 +02:00
static void *handle_messages(void *p)
{
((void)p);
int handled = 0;
2024-05-28 04:14:29 +02:00
struct log_event ev = {0};
2024-07-17 03:34:38 +02:00
while (syslog_pop_msg_from_fifo(&ev) >= 0) {
2024-05-28 04:14:29 +02:00
print_log_event(&ev);
if (!is_within_notify_threshold()) {
2024-05-31 03:28:28 +02:00
log_msg("ignoring, reason: too many notifications!\n");
continue;
}
handled = process_static_event(&ev);
handled += process_environment_event(&ev);
2024-05-28 04:14:29 +02:00
if (handled)
update_notify_last_sent();
else
log_msg("> Not handled!\n");
2024-05-28 04:14:29 +02:00
}
return NULL;
}
int main(void)
{
pthread_t handler;
int fd;
/* TODO: remove setup_notifiers()..
* think about env vars for the static events too, like enable/disable
*/
2024-07-16 03:47:57 +02:00
log_init();
2024-07-16 03:16:27 +02:00
setup_notifiers();
init_environment_events();
2024-05-28 04:14:29 +02:00
2024-05-31 01:49:35 +02:00
log_msg(
"Alertik (" GIT_HASH ") (built at " __DATE__ " " __TIME__ ")\n");
log_msg(" (https://github.com/Theldus/alertik)\n");
log_msg("-------------------------------------------------\n");
2024-05-28 04:14:29 +02:00
2024-07-17 03:34:38 +02:00
fd = syslog_create_udp_socket();
2024-05-28 04:14:29 +02:00
if (pthread_create(&handler, NULL, handle_messages, NULL))
panic_errno("Unable to create hanler thread!");
2024-05-31 01:49:35 +02:00
log_msg("Waiting for messages at :%d (UDP)...\n", SYSLOG_PORT);
2024-05-28 04:14:29 +02:00
2024-07-17 03:34:38 +02:00
while (syslog_enqueue_new_upd_msg(fd) >= 0);
2024-05-31 01:49:35 +02:00
return EXIT_SUCCESS;
2024-05-28 04:14:29 +02:00
}