Add regex for static events too

This commit is contained in:
Davidson Francis 2024-07-24 21:15:38 -03:00
parent 9e9150b641
commit 0e449c441f
4 changed files with 58 additions and 28 deletions

View File

@ -51,15 +51,15 @@ int main(void)
log_init(); log_init();
if (!init_static_events() && !init_environment_events())
panic("No event was configured, please configure at least one\n"
"before proceeding!\n");
log_msg( log_msg(
"Alertik (" GIT_HASH ") (built at " __DATE__ " " __TIME__ ")\n"); "Alertik (" GIT_HASH ") (built at " __DATE__ " " __TIME__ ")\n");
log_msg(" (https://github.com/Theldus/alertik)\n"); log_msg(" (https://github.com/Theldus/alertik)\n");
log_msg("-------------------------------------------------\n"); log_msg("-------------------------------------------------\n");
if (!init_static_events() && !init_environment_events())
panic("No event was configured, please configure at least one\n"
"before proceeding!\n");
fd = syslog_create_udp_socket(); fd = syslog_create_udp_socket();
if (pthread_create(&handler, NULL, handle_messages, NULL)) if (pthread_create(&handler, NULL, handle_messages, NULL))
panic_errno("Unable to create hanler thread!"); panic_errno("Unable to create hanler thread!");

View File

@ -19,7 +19,6 @@
regex_t regex; /* Compiled regex. */ regex_t regex; /* Compiled regex. */
}; };
extern struct env_event env_events[MAX_ENV_EVENTS];
extern int init_environment_events(void); extern int init_environment_events(void);
extern int process_environment_event(struct log_event *ev); extern int process_environment_event(struct log_event *ev);

View File

@ -13,17 +13,20 @@
#include "notifiers.h" #include "notifiers.h"
#include "log.h" #include "log.h"
static void handle_wifi_login_attempts(struct log_event *, int); /* Misc. */
#define MAX_MATCHES 32
static regmatch_t pmatch[MAX_MATCHES];
/* Handlers. */ /* Handlers. */
struct ev_handler handlers[NUM_EVENTS] = { static void handle_wifi_login_attempts(struct log_event *, int);
struct static_event static_events[NUM_EVENTS] = {
/* Failed login attempts. */ /* Failed login attempts. */
{ {
.str = "unicast key exchange timeout", .ev_match_str = "unicast key exchange timeout",
.hnd = handle_wifi_login_attempts, .hnd = handle_wifi_login_attempts,
.evnt_type = EVNT_SUBSTR, .ev_match_type = EVNT_SUBSTR,
.enabled = 0, .enabled = 0,
.evnt_notifier_idx = NOTIFY_IDX_TELE .ev_notifier_idx = NOTIFY_IDX_TELE
}, },
/* Add new handlers here. */ /* Add new handlers here. */
}; };
@ -63,15 +66,27 @@ int process_static_event(struct log_event *ev)
{ {
int i; int i;
int handled; int handled;
struct static_event *sta_ev;
for (i = 0, handled = 0; i < NUM_EVENTS; i++) { for (i = 0, handled = 0; i < NUM_EVENTS; i++) {
/* Skip not enabled events. */ /* Skip not enabled events. */
if (!handlers[i].enabled) if (!static_events[i].enabled)
continue; continue;
if (strstr(ev->msg, handlers[i].str)) { sta_ev = &static_events[i];
handlers[i].hnd(ev, i);
handled += 1; if (static_events[i].ev_match_type == EVNT_SUBSTR) {
if (strstr(ev->msg, static_events[i].ev_match_str)) {
static_events[i].hnd(ev, i);
handled += 1;
}
}
else {
if (regexec(&sta_ev->regex, ev->msg, MAX_MATCHES, pmatch, 0)) {
static_events[i].hnd(ev, i);
handled += 1;
}
} }
} }
return handled; return handled;
@ -114,9 +129,9 @@ int init_static_events(void)
ev, NUM_EVENTS - 1); ev, NUM_EVENTS - 1);
/* Try to retrieve & initialize notifier for the event. */ /* Try to retrieve & initialize notifier for the event. */
handlers[ev].evnt_notifier_idx = static_events[ev].ev_notifier_idx =
get_event_idx(ev, "NOTIFIER", notifiers_str, NUM_NOTIFIERS); get_event_idx(ev, "NOTIFIER", notifiers_str, NUM_NOTIFIERS);
handlers[ev].enabled = 1; static_events[ev].enabled = 1;
if (*end != ',' && *end != '\0') if (*end != ',' && *end != '\0')
panic("Wrong event number in STATIC_EVENTS_ENABLED, aborting...\n"); panic("Wrong event number in STATIC_EVENTS_ENABLED, aborting...\n");
@ -126,23 +141,38 @@ int init_static_events(void)
log_msg("Static events summary:\n"); log_msg("Static events summary:\n");
for (int i = 0; i < NUM_EVENTS; i++) { for (int i = 0; i < NUM_EVENTS; i++) {
if (!handlers[i].enabled) if (!static_events[i].enabled)
continue; continue;
printf( printf(
"STATIC_EVENT%d : enabled\n" "STATIC_EVENT%d : enabled\n"
"STATIC_EVENT%d_NOTIFIER: %s\n\n", "STATIC_EVENT%d_NOTIFIER: %s\n\n",
i, i, notifiers_str[handlers[i].evnt_notifier_idx] i, i, notifiers_str[static_events[i].ev_notifier_idx]
); );
/* Try to setup notifier if not yet. */ /* Try to setup notifier if not yet. */
notifiers[handlers[i].evnt_notifier_idx].setup(); notifiers[static_events[i].ev_notifier_idx].setup();
/* If regex, compile it first. */
if (static_events[i].ev_match_type == EVNT_REGEX) {
if (regcomp(
&static_events[i].regex,
static_events[i].ev_match_str,
REG_EXTENDED))
{
panic("Unable to compile regex (%s) for EVENT%d!!!",
static_events[i].ev_match_str, i);
}
}
} }
return 1; return 1;
} }
///////////////////////////////////////////////////////////////////////////////
///////////////////////////// FAILED LOGIN ATTEMPTS /////////////////////////// ///////////////////////////// FAILED LOGIN ATTEMPTS ///////////////////////////
///////////////////////////////////////////////////////////////////////////////
static int static int
parse_login_attempt_msg(const char *msg, char *wifi_iface, char *mac_addr) parse_login_attempt_msg(const char *msg, char *wifi_iface, char *mac_addr)
{ {
@ -203,7 +233,7 @@ static void handle_wifi_login_attempts(struct log_event *ev, int idx_env)
log_msg("> Retrieved info, MAC: (%s), Interface: (%s)\n", mac_addr, wifi_iface); log_msg("> Retrieved info, MAC: (%s), Interface: (%s)\n", mac_addr, wifi_iface);
notif_idx = handlers[idx_env].evnt_notifier_idx; notif_idx = static_events[idx_env].ev_notifier_idx;
if (notifiers[notif_idx].send_notification(notification_message) < 0) { if (notifiers[notif_idx].send_notification(notification_message) < 0) {
log_msg("unable to send the notification!\n"); log_msg("unable to send the notification!\n");
return; return;

View File

@ -6,6 +6,7 @@
#ifndef EVENTS_H #ifndef EVENTS_H
#define EVENTS_H #define EVENTS_H
#include <regex.h>
#include <time.h> #include <time.h>
#define MSG_MAX 2048 #define MSG_MAX 2048
@ -20,15 +21,15 @@
time_t timestamp; time_t timestamp;
}; };
struct ev_handler { struct static_event {
const char *str; /* Substr or regex to match. */
void(*hnd)(struct log_event *, int); /* Event handler. */ void(*hnd)(struct log_event *, int); /* Event handler. */
int evnt_type; /* Whether substr or regex. */ const char *ev_match_str; /* Substr or regex to match. */
int evnt_notifier_idx; /* Telegram, Discord... */ int ev_match_type; /* Whether substr or regex. */
int enabled; /* Whether if handler enabled or not. */ int ev_notifier_idx; /* Telegram, Discord... */
int enabled; /* Whether if handler enabled or not. */
regex_t regex; /* Compiled regex. */
}; };
extern struct ev_handler handlers[NUM_EVENTS];
extern int process_static_event(struct log_event *ev); extern int process_static_event(struct log_event *ev);
extern int init_static_events(void); extern int init_static_events(void);