-
Notifications
You must be signed in to change notification settings - Fork 3
/
alertik.c
75 lines (58 loc) · 1.55 KB
/
alertik.c
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
/*
* 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>
#include "events.h"
#include "env_events.h"
#include "log.h"
#include "notifiers.h"
#include "syslog.h"
/*
* Alertik
*/
static void *handle_messages(void *p)
{
((void)p);
int handled = 0;
struct log_event ev = {0};
while (syslog_pop_msg_from_fifo(&ev) >= 0) {
print_log_event(&ev);
if (!is_within_notify_threshold()) {
log_msg("ignoring, reason: too many notifications!\n");
continue;
}
handled = process_static_event(&ev);
handled += process_environment_event(&ev);
if (handled)
update_notify_last_sent();
else
log_msg("> Not handled!\n");
}
return NULL;
}
int main(void)
{
pthread_t handler;
int ret;
int fd;
log_init();
log_msg(
"Alertik (" GIT_HASH ") (built at " __DATE__ " " __TIME__ ")\n");
log_msg(" (https://github.com/Theldus/alertik)\n");
log_msg("-------------------------------------------------\n");
ret = init_static_events();
ret += init_environment_events();
if (!ret)
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!");
log_msg("Waiting for messages at :%d (UDP)...\n", SYSLOG_PORT);
while (syslog_enqueue_new_upd_msg(fd) >= 0);
return EXIT_SUCCESS;
}