mirror of
https://github.com/Theldus/alertik.git
synced 2024-11-27 18:13:23 +01:00
Major rework on notifiers structure
This commit is contained in:
parent
34963499c3
commit
2fb5a9fa4e
13
env_events.c
13
env_events.c
@ -241,6 +241,7 @@ static int handle_regex(struct log_event *ev, int idx_env)
|
|||||||
char time_str[32] = {0};
|
char time_str[32] = {0};
|
||||||
regmatch_t pmatch[MAX_MATCHES] = {0};
|
regmatch_t pmatch[MAX_MATCHES] = {0};
|
||||||
struct str_ab notif_message;
|
struct str_ab notif_message;
|
||||||
|
struct notifier *self;
|
||||||
|
|
||||||
int ret;
|
int ret;
|
||||||
int notif_idx;
|
int notif_idx;
|
||||||
@ -248,6 +249,7 @@ static int handle_regex(struct log_event *ev, int idx_env)
|
|||||||
|
|
||||||
env_ev = &env_events[idx_env];
|
env_ev = &env_events[idx_env];
|
||||||
notif_idx = env_ev->ev_notifier_idx;
|
notif_idx = env_ev->ev_notifier_idx;
|
||||||
|
self = ¬ifiers[notif_idx];
|
||||||
|
|
||||||
if (regexec(&env_ev->regex, ev->msg, MAX_MATCHES, pmatch, 0) == REG_NOMATCH)
|
if (regexec(&env_ev->regex, ev->msg, MAX_MATCHES, pmatch, 0) == REG_NOMATCH)
|
||||||
return 0;
|
return 0;
|
||||||
@ -286,7 +288,7 @@ static int handle_regex(struct log_event *ev, int idx_env)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (notifiers[notif_idx].send_notification(notif_message.buff) < 0) {
|
if (self->send_notification(self, notif_message.buff) < 0) {
|
||||||
log_msg("unable to send the notification through %s\n",
|
log_msg("unable to send the notification through %s\n",
|
||||||
notifiers_str[notif_idx]);
|
notifiers_str[notif_idx]);
|
||||||
}
|
}
|
||||||
@ -307,11 +309,14 @@ static int handle_substr(struct log_event *ev, int idx_env)
|
|||||||
int ret;
|
int ret;
|
||||||
int notif_idx;
|
int notif_idx;
|
||||||
char time_str[32] = {0};
|
char time_str[32] = {0};
|
||||||
|
|
||||||
|
struct notifier *self;
|
||||||
struct env_event *env_ev;
|
struct env_event *env_ev;
|
||||||
struct str_ab notif_message;
|
struct str_ab notif_message;
|
||||||
|
|
||||||
env_ev = &env_events[idx_env];
|
env_ev = &env_events[idx_env];
|
||||||
notif_idx = env_ev->ev_notifier_idx;
|
notif_idx = env_ev->ev_notifier_idx;
|
||||||
|
self = ¬ifiers[notif_idx];
|
||||||
|
|
||||||
if (!strstr(ev->msg, env_ev->ev_match_str))
|
if (!strstr(ev->msg, env_ev->ev_match_str))
|
||||||
return 0;
|
return 0;
|
||||||
@ -332,7 +337,7 @@ static int handle_substr(struct log_event *ev, int idx_env)
|
|||||||
if (ret)
|
if (ret)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (notifiers[notif_idx].send_notification(notif_message.buff) < 0) {
|
if (self->send_notification(self, notif_message.buff) < 0) {
|
||||||
log_msg("unable to send the notification through %s\n",
|
log_msg("unable to send the notification through %s\n",
|
||||||
notifiers_str[notif_idx]);
|
notifiers_str[notif_idx]);
|
||||||
}
|
}
|
||||||
@ -372,6 +377,7 @@ int process_environment_event(struct log_event *ev)
|
|||||||
*/
|
*/
|
||||||
int init_environment_events(void)
|
int init_environment_events(void)
|
||||||
{
|
{
|
||||||
|
struct notifier *self;
|
||||||
char *tmp;
|
char *tmp;
|
||||||
tmp = getenv("ENV_EVENTS");
|
tmp = getenv("ENV_EVENTS");
|
||||||
|
|
||||||
@ -412,7 +418,8 @@ int init_environment_events(void)
|
|||||||
);
|
);
|
||||||
|
|
||||||
/* Try to setup notifier if not yet. */
|
/* Try to setup notifier if not yet. */
|
||||||
notifiers[env_events[i].ev_notifier_idx].setup();
|
self = ¬ifiers[env_events[i].ev_notifier_idx];
|
||||||
|
self->setup(self);
|
||||||
|
|
||||||
/* If regex, compile it first. */
|
/* If regex, compile it first. */
|
||||||
if (env_events[i].ev_match_type == EVNT_REGEX) {
|
if (env_events[i].ev_match_type == EVNT_REGEX) {
|
||||||
|
9
events.c
9
events.c
@ -121,6 +121,7 @@ int process_static_event(struct log_event *ev)
|
|||||||
*/
|
*/
|
||||||
int init_static_events(void)
|
int init_static_events(void)
|
||||||
{
|
{
|
||||||
|
struct notifier *self;
|
||||||
char *ptr, *end;
|
char *ptr, *end;
|
||||||
long ev;
|
long ev;
|
||||||
|
|
||||||
@ -172,7 +173,8 @@ int init_static_events(void)
|
|||||||
);
|
);
|
||||||
|
|
||||||
/* Try to setup notifier if not yet. */
|
/* Try to setup notifier if not yet. */
|
||||||
notifiers[static_events[i].ev_notifier_idx].setup();
|
self = ¬ifiers[static_events[i].ev_notifier_idx];
|
||||||
|
self->setup(self);
|
||||||
|
|
||||||
/* If regex, compile it first. */
|
/* If regex, compile it first. */
|
||||||
if (static_events[i].ev_match_type == EVNT_REGEX) {
|
if (static_events[i].ev_match_type == EVNT_REGEX) {
|
||||||
@ -254,6 +256,7 @@ static void handle_wifi_login_attempts(struct log_event *ev, int idx_env)
|
|||||||
char mac_addr[32] = {0};
|
char mac_addr[32] = {0};
|
||||||
char wifi_iface[32] = {0};
|
char wifi_iface[32] = {0};
|
||||||
struct str_ab notif_message;
|
struct str_ab notif_message;
|
||||||
|
struct notifier *self;
|
||||||
int notif_idx;
|
int notif_idx;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
@ -279,7 +282,9 @@ 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 = static_events[idx_env].ev_notifier_idx;
|
notif_idx = static_events[idx_env].ev_notifier_idx;
|
||||||
if (notifiers[notif_idx].send_notification(notif_message.buff) < 0) {
|
self = ¬ifiers[notif_idx];
|
||||||
|
|
||||||
|
if (self->send_notification(self, notif_message.buff) < 0) {
|
||||||
log_msg("unable to send the notification!\n");
|
log_msg("unable to send the notification!\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
115
notifiers.c
115
notifiers.c
@ -17,6 +17,11 @@
|
|||||||
* Notification handling/notifiers
|
* Notification handling/notifiers
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
struct webhook_data {
|
||||||
|
char *webhook_url;
|
||||||
|
const char *env_var;
|
||||||
|
};
|
||||||
|
|
||||||
/* EPOCH in secs of last sent notification. */
|
/* EPOCH in secs of last sent notification. */
|
||||||
static time_t time_last_sent_notify;
|
static time_t time_last_sent_notify;
|
||||||
|
|
||||||
@ -220,12 +225,14 @@ static int send_generic_webhook(const char *url, const char *text)
|
|||||||
static char *telegram_bot_token;
|
static char *telegram_bot_token;
|
||||||
static char *telegram_chat_id;
|
static char *telegram_chat_id;
|
||||||
|
|
||||||
void setup_telegram(void)
|
void setup_telegram(struct notifier *self)
|
||||||
{
|
{
|
||||||
static int setup = 0;
|
static int setup = 0;
|
||||||
if (setup)
|
if (setup)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
((void)self);
|
||||||
|
|
||||||
telegram_bot_token = getenv("TELEGRAM_BOT_TOKEN");
|
telegram_bot_token = getenv("TELEGRAM_BOT_TOKEN");
|
||||||
telegram_chat_id = getenv("TELEGRAM_CHAT_ID");
|
telegram_chat_id = getenv("TELEGRAM_CHAT_ID");
|
||||||
if (!telegram_bot_token || !telegram_chat_id) {
|
if (!telegram_bot_token || !telegram_chat_id) {
|
||||||
@ -239,13 +246,15 @@ void setup_telegram(void)
|
|||||||
setup = 1;
|
setup = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int send_telegram_notification(const char *msg)
|
static int send_telegram_notification(const struct notifier *self, const char *msg)
|
||||||
{
|
{
|
||||||
struct str_ab full_request_url;
|
struct str_ab full_request_url;
|
||||||
char *escaped_msg = NULL;
|
char *escaped_msg = NULL;
|
||||||
CURL *hnd = NULL;
|
CURL *hnd = NULL;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
|
((void)self);
|
||||||
|
|
||||||
if (!(hnd = curl_easy_init())) {
|
if (!(hnd = curl_easy_init())) {
|
||||||
log_msg("Failed to initialize libcurl!\n");
|
log_msg("Failed to initialize libcurl!\n");
|
||||||
return -1;
|
return -1;
|
||||||
@ -272,81 +281,42 @@ static int send_telegram_notification(const char *msg)
|
|||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
////////////////////////////////// SLACK //////////////////////////////////////
|
///////////////////////////////// GENERIC /////////////////////////////////////
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
/* Slack settings. */
|
void setup_generic_webhook(struct notifier *self)
|
||||||
static char *slack_webhook_url;
|
|
||||||
|
|
||||||
void setup_slack(void)
|
|
||||||
{
|
{
|
||||||
static int setup = 0;
|
struct webhook_data *data = self->data;
|
||||||
if (setup)
|
|
||||||
|
if (data->webhook_url)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
slack_webhook_url = getenv("SLACK_WEBHOOK_URL");
|
data->webhook_url = getenv(data->env_var);
|
||||||
if (!slack_webhook_url) {
|
if (!data->webhook_url) {
|
||||||
panic("Unable to find env vars for Slack, please check if you have set "
|
panic("Unable to find env vars, please check if you have set the %s!!\n",
|
||||||
"the SLACK_WEBHOOK_URL!!\n");
|
data->env_var);
|
||||||
}
|
}
|
||||||
setup = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int send_slack_notification(const char *msg) {
|
static int send_generic_webhook_notification(
|
||||||
return send_generic_webhook(slack_webhook_url, msg);
|
const struct notifier *self, const char *msg)
|
||||||
}
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
////////////////////////////////// TEAMS //////////////////////////////////////
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
/* Teams settings. */
|
|
||||||
static char *teams_webhook_url;
|
|
||||||
|
|
||||||
void setup_teams(void)
|
|
||||||
{
|
{
|
||||||
static int setup = 0;
|
struct webhook_data *data = self->data;
|
||||||
if (setup)
|
return send_generic_webhook(data->webhook_url, msg);
|
||||||
return;
|
|
||||||
|
|
||||||
teams_webhook_url = getenv("TEAMS_WEBHOOK_URL");
|
|
||||||
if (!teams_webhook_url) {
|
|
||||||
panic("Unable to find env vars for Teams, please check if you have set "
|
|
||||||
"the TEAMS_WEBHOOK_URL!!\n");
|
|
||||||
}
|
|
||||||
setup = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int send_teams_notification(const char *msg) {
|
|
||||||
return send_generic_webhook(teams_webhook_url, msg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
///////////////////////////////// DISCORD /////////////////////////////////////
|
///////////////////////////////// DISCORD /////////////////////////////////////
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
/* Discord settings. */
|
|
||||||
static char *discord_webhook_url;
|
|
||||||
|
|
||||||
void setup_discord(void)
|
|
||||||
{
|
|
||||||
static int setup = 0;
|
|
||||||
if (setup)
|
|
||||||
return;
|
|
||||||
|
|
||||||
discord_webhook_url = getenv("DISCORD_WEBHOOK_URL");
|
|
||||||
if (!discord_webhook_url) {
|
|
||||||
panic("Unable to find env vars for Discord, please check if you have set "
|
|
||||||
"the DISCORD_WEBHOOK_URL!!\n");
|
|
||||||
}
|
|
||||||
setup = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Discord in Slack-compatible mode. */
|
/* Discord in Slack-compatible mode. */
|
||||||
static int send_discord_notification(const char *msg) {
|
static int send_discord_notification(
|
||||||
|
const struct notifier *self, const char *msg)
|
||||||
|
{
|
||||||
|
struct webhook_data *data = self->data;
|
||||||
struct str_ab url;
|
struct str_ab url;
|
||||||
ab_init(&url);
|
ab_init(&url);
|
||||||
if (ab_append_fmt("%s/slack", discord_webhook_url) < 0)
|
if (ab_append_fmt(&url, "%s/slack", data->webhook_url) < 0)
|
||||||
return 1;
|
return 1;
|
||||||
return send_generic_webhook(url.buff, msg);
|
return send_generic_webhook(url.buff, msg);
|
||||||
}
|
}
|
||||||
@ -363,21 +333,32 @@ struct notifier notifiers[] = {
|
|||||||
/* Telegram. */
|
/* Telegram. */
|
||||||
{
|
{
|
||||||
.setup = setup_telegram,
|
.setup = setup_telegram,
|
||||||
.send_notification = send_telegram_notification
|
.send_notification = send_telegram_notification,
|
||||||
},
|
},
|
||||||
/* Slack. */
|
/* Slack. */
|
||||||
{
|
{
|
||||||
.setup = setup_slack,
|
.setup = setup_generic_webhook,
|
||||||
.send_notification = send_slack_notification
|
.send_notification = send_generic_webhook_notification,
|
||||||
|
.data = &(struct webhook_data)
|
||||||
|
{.env_var = "SLACK_WEBHOOK_URL"},
|
||||||
},
|
},
|
||||||
/* Teams. */
|
/* Teams. */
|
||||||
{
|
{
|
||||||
.setup = setup_teams,
|
.setup = setup_generic_webhook,
|
||||||
.send_notification = send_teams_notification
|
.send_notification = send_generic_webhook_notification,
|
||||||
|
.data = &(struct webhook_data)
|
||||||
|
{.env_var = "TEAMS_WEBHOOK_URL"},
|
||||||
},
|
},
|
||||||
/* Discord. */
|
/*
|
||||||
|
* Discord:
|
||||||
|
* Since Discord doesn't follow like the others, we need
|
||||||
|
* to slightly change the URL before proceeding, so this
|
||||||
|
* is why its function is not generic!.
|
||||||
|
*/
|
||||||
{
|
{
|
||||||
.setup = setup_discord,
|
.setup = setup_generic_webhook,
|
||||||
.send_notification = send_discord_notification
|
.send_notification = send_discord_notification,
|
||||||
|
.data = &(struct webhook_data)
|
||||||
|
{.env_var = "DISCORD_WEBHOOK_URL"},
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -17,8 +17,8 @@
|
|||||||
#define NUM_NOTIFIERS 8
|
#define NUM_NOTIFIERS 8
|
||||||
#define NOTIFY_IDX_TELE 0
|
#define NOTIFY_IDX_TELE 0
|
||||||
#define NOTIFY_IDX_SLACK 1
|
#define NOTIFY_IDX_SLACK 1
|
||||||
#define NOTIFY_IDX_DISCORD 2
|
#define NOTIFY_IDX_TEAMS 2
|
||||||
#define NOTIFY_IDX_TEAMS 3
|
#define NOTIFY_IDX_DISCORD 3
|
||||||
#define NOTIFY_IDX_GENRC1 (NUM_NOTIFIERS-4)
|
#define NOTIFY_IDX_GENRC1 (NUM_NOTIFIERS-4)
|
||||||
#define NOTIFY_IDX_GENRC2 (NUM_NOTIFIERS-3)
|
#define NOTIFY_IDX_GENRC2 (NUM_NOTIFIERS-3)
|
||||||
#define NOTIFY_IDX_GENRC3 (NUM_NOTIFIERS-2)
|
#define NOTIFY_IDX_GENRC3 (NUM_NOTIFIERS-2)
|
||||||
@ -40,8 +40,9 @@
|
|||||||
|
|
||||||
/* Notifier struct. */
|
/* Notifier struct. */
|
||||||
struct notifier {
|
struct notifier {
|
||||||
void(*setup)(void);
|
void *data;
|
||||||
int(*send_notification)(const char *msg);
|
void(*setup)(struct notifier *self);
|
||||||
|
int(*send_notification)(const struct notifier *self, const char *msg);
|
||||||
};
|
};
|
||||||
|
|
||||||
extern struct notifier notifiers[NUM_NOTIFIERS];
|
extern struct notifier notifiers[NUM_NOTIFIERS];
|
||||||
|
Loading…
Reference in New Issue
Block a user