From 6bf70db630afa19d0407d6468df8de307bccfecd Mon Sep 17 00:00:00 2001 From: arf20 Date: Mon, 10 Nov 2025 23:50:02 +0100 Subject: fix config --- config.c | 25 +++++++++++++++++++++++-- config.h | 2 ++ main.c | 14 +++++--------- monitor.c | 6 ++++-- monitor.cfg.example | 5 ++++- 5 files changed, 38 insertions(+), 14 deletions(-) diff --git a/config.c b/config.c index 1d485d7..1e2cd55 100644 --- a/config.c +++ b/config.c @@ -5,8 +5,9 @@ #include #include -unsigned short port = DEFAULT_PORT; -char *log_path = DEFAULT_LOG_PATH; +unsigned short port = 0; +char *tmpl_path = NULL; +char *log_path = NULL; monitor_config_t monitor_config = { .interval = DEFAULT_INTERVAL }; alert_config_t alert_config = { 0 }; @@ -60,6 +61,9 @@ config_load(const char *conf_path) fprintf(stderr, "[config] invalid interval: %s\n", line); return -1; } + } else if (strcmp(line, "interval") == 0) { + tmpl_path = strdup(value); + printf("\ttemplate: %s\n", tmpl_path); } else if (strcmp(line, "log") == 0) { value[strlen(value) - 1] = '\0'; log_path = strdup(value); @@ -97,6 +101,23 @@ config_load(const char *conf_path) fclose(cfgf); + if (port == 0) { + fprintf(stderr, "[config] W: no port, using default\n"); + port = DEFAULT_PORT; + } + + if (!tmpl_path) { + fprintf(stderr, "[config] W: no template file given, using default\n"); + tmpl_path = DEFAULT_TMPL_PATH; + } + + if (!log_path) { + fprintf(stderr, "[config] W: no template event log path given, " + "using default\n"); + log_path = DEFAULT_LOG_PATH; + } + + if (!alert_config.from || !alert_config.mail_server) fprintf(stderr, "[config] W: no mail\n"); diff --git a/config.h b/config.h index 0c0b502..4801393 100644 --- a/config.h +++ b/config.h @@ -10,6 +10,7 @@ #define DEFAULT_PORT 8888 #define DEFAULT_INTERVAL 60 +#define DEFAULT_TMPL_PATH "index.htm.tmpl" #define DEFAULT_LOG_PATH "events.log" /* config types */ @@ -29,6 +30,7 @@ typedef struct { /* config objects */ extern unsigned short port; +extern char *tmpl_path; extern char *log_path; extern monitor_config_t monitor_config; extern alert_config_t alert_config; diff --git a/main.c b/main.c index 784c0e2..92ff700 100644 --- a/main.c +++ b/main.c @@ -17,9 +17,6 @@ #include "check.h" #include "alert.h" -#define CFG_FILE "monitor.cfg" -#define TMPL_FILE "index.htm.tmpl" -#define LOG_FILE "events.log" static char *index_format_template = NULL; @@ -76,8 +73,11 @@ enum MHD_Result answer_to_connection( int main() { printf("ARFNET Status Monitor (C) 2025 under GPLv3\n"); + if (config_load(CONFIG_PATH) < 0) + return 1; + /* read index template file */ - FILE *tf = fopen(TMPL_FILE, "r"); + FILE *tf = fopen(tmpl_path, "r"); if (!tf) { fprintf(stderr, "error opening index template file: %s\n", strerror(errno)); @@ -90,9 +90,6 @@ int main() { fread(index_format_template, 1, tfs, tf); fclose(tf); - if (config_load(CONFIG_PATH) < 0) - return 1; - if (check_init() < 0) return 1; @@ -115,10 +112,9 @@ int main() { return 1; } - while (1) { check_perform(targets, targets_size); - monitor_update_events(LOG_FILE); + monitor_update_events(log_path); sleep(monitor_config.interval); } } diff --git a/monitor.c b/monitor.c index a35e7ba..3cae6d0 100644 --- a/monitor.c +++ b/monitor.c @@ -181,7 +181,7 @@ monitor_init() /* read monitor log */ FILE *logf = fopen(log_path, "r"); if (!logf) { - fprintf(stderr, "Error opening log: %s\n", strerror(errno)); + fprintf(stderr, "Error opening log for reading: %s\n", strerror(errno)); return -1; } @@ -461,9 +461,11 @@ commit_event(const char *log_path, const target_t *target, struct tm *tm_event = gmtime(&event->time); strftime(buff, 256, "%FT%T%z", tm_event); - fprintf(logf, "%s,%s,%s\n", + int bytes =fprintf(logf, "%s,%s,%s\n", target->name, buff, status_str[event->status]); + if (bytes <= 0) + fprintf(stderr, "error committing event\n"); fclose(logf); } diff --git a/monitor.cfg.example b/monitor.cfg.example index 821f108..b3a9911 100644 --- a/monitor.cfg.example +++ b/monitor.cfg.example @@ -5,7 +5,10 @@ port=8888 # monitor interval in seconds (sleep) -interval=5 +interval=60 + +# html template path +template=index.htm.tmpl # monitor events log path log=events.log -- cgit v1.2.3