From 52dc621b426fcb55ef3887e67de08a612b15480d Mon Sep 17 00:00:00 2001 From: arf20 Date: Mon, 27 Oct 2025 16:54:00 +0100 Subject: implemented commiting events to disk --- events.log | 4 +++- main.c | 14 ++++++++++---- monitor.c | 46 ++++++++++++++++++++++++++++++++++++++-------- monitor.h | 2 +- 4 files changed, 52 insertions(+), 14 deletions(-) diff --git a/events.log b/events.log index 85c4116..d74a182 100644 --- a/events.log +++ b/events.log @@ -73,4 +73,6 @@ http,2025-08-26T14:39:43Z,down http,2025-08-26T14:44:45Z,up http,2025-10-24T10:49:20Z,down http,2025-10-24T12:38:19Z,up - +ipv4,2025-10-27T15:43:17+0000,up +dns,2025-10-27T15:43:17+0000,up +https,2025-10-27T15:43:17+0000,up diff --git a/main.c b/main.c index 197d1b2..d262ac0 100644 --- a/main.c +++ b/main.c @@ -16,9 +16,15 @@ #include "monitor.h" -#define PORT 8888 #define RES_BUFF 65535 + +#define PORT 8888 + +#define CFG_FILE "monitor.cfg" +#define TMPL_FILE "index.htm.tmpl" +#define LOG_FILE "events.log" + static char *index_format_template = NULL; @@ -71,7 +77,7 @@ enum MHD_Result answer_to_connection( int main() { /* read index template file */ - FILE *tf = fopen("index.htm.tmpl", "r"); + FILE *tf = fopen(TMPL_FILE, "r"); if (!tf) { fprintf(stderr, "error opening index template file: %s\n", strerror(errno)); @@ -97,11 +103,11 @@ int main() { return 1; } - monitor_init("monitor.cfg", "events.log"); + monitor_init(CFG_FILE, LOG_FILE); while (1) { monitor_check(); - monitor_update_events(); + monitor_update_events(LOG_FILE); sleep(5); } } diff --git a/monitor.c b/monitor.c index d47238d..c277752 100644 --- a/monitor.c +++ b/monitor.c @@ -291,7 +291,7 @@ monitor_init(const char *cfg_path, const char *log_path) return 0; } -const char * +static const char * target_uptime(const target_t *target) { static char buff[256]; @@ -315,7 +315,7 @@ target_uptime(const target_t *target) return buff; } -float +static float target_perc_uptime_since(const target_t *target, time_t since) { time_t downtime = 0; @@ -340,7 +340,7 @@ target_perc_uptime_since(const target_t *target, time_t since) return 1.0f - ((float)downtime / (float)(time(NULL) - since)); } -float +static float target_perc_uptime_total(const target_t *target) { time_t downtime = 0, since = 0; @@ -358,13 +358,13 @@ target_perc_uptime_total(const target_t *target) return 1.0f - ((float)downtime / (float)(time(NULL) - since)); } -int +static int color_map(float perc) { return 255.0f*exp2f(100.0f*perc-100.0f); } -const char * +static const char * generate_timeline(const target_t *target, time_t since, time_t span) { static char buff[BUFF_SIZE]; @@ -574,7 +574,7 @@ monitor_check() static size_t check_num = 0; time_t time_now = time(NULL); struct tm *tm_now = gmtime(&time_now); - strftime(timestr, 256, "%Y-%m-%d %H:%M:%S", tm_now); + strftime(timestr, 256, "%F %T", tm_now); static const int (*check_funcs[])(const char *) = { check_reach, @@ -591,8 +591,36 @@ monitor_check() check_num++; } +static void +commit_event(const char *log_path, const target_t *target, + const event_t *event) +{ + static char *status_str[] = { + "down", + "up" + }; + + char buff[256]; + + FILE *logf = fopen(log_path, "a"); + if (!logf) { + fprintf(stderr, + "Error opening log file for writing: %s\n", strerror(errno)); + return; + } + + + struct tm *tm_event = gmtime(&event->time); + strftime(buff, 256, "%FT%T%z", tm_event); + + fprintf(logf, "%s,%s,%s\n", + target->name, buff, status_str[event->status]); + + fclose(logf); +} + void -monitor_update_events() +monitor_update_events(const char *log_path) { static char *status_str[] = { "down", @@ -601,7 +629,7 @@ monitor_update_events() time_t time_now = time(NULL); struct tm *tm_now = gmtime(&time_now); - strftime(timestr, 256, "%Y-%m-%d %H:%M:%S", tm_now); + strftime(timestr, 256, "%F %T", tm_now); for (size_t i = 0; i < targets_n; i++) { if (targets[i].events_size > 0 && ( @@ -623,6 +651,8 @@ monitor_update_events() target_events_push_ordered(&targets[i], &event); + commit_event(log_path, &targets[i], &event); + printf("[%s] [monitor] %s is now %s\n", timestr, targets[i].name, status_str[targets[i].status]); diff --git a/monitor.h b/monitor.h index 198908e..f9b1af3 100644 --- a/monitor.h +++ b/monitor.h @@ -5,6 +5,6 @@ int monitor_init(const char *cfg_file, const char *log_file); const char *monitor_generate_status_html(); const char *monitor_generate_incidents_html(); void monitor_check(); -void monitor_update_events(); +void monitor_update_events(const char *log_path); #endif /* _MONITOR_H */ -- cgit v1.2.3