diff options
| author | arf20 <aruizfernandez05@gmail.com> | 2025-10-27 16:54:00 +0100 |
|---|---|---|
| committer | arf20 <aruizfernandez05@gmail.com> | 2025-10-27 16:54:00 +0100 |
| commit | 52dc621b426fcb55ef3887e67de08a612b15480d (patch) | |
| tree | 2f4fcef15f5ce15f1bb482d2bc6148a15094ba2b | |
| parent | 8e937f139b118317fdaf7c2dbbb0669b508eb5ed (diff) | |
| download | arfnet2-status-52dc621b426fcb55ef3887e67de08a612b15480d.tar.gz arfnet2-status-52dc621b426fcb55ef3887e67de08a612b15480d.zip | |
| -rw-r--r-- | events.log | 4 | ||||
| -rw-r--r-- | main.c | 14 | ||||
| -rw-r--r-- | monitor.c | 46 | ||||
| -rw-r--r-- | monitor.h | 2 |
4 files changed, 52 insertions, 14 deletions
@@ -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 @@ -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); } } @@ -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]); @@ -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 */ |
