diff options
| author | arf20 <aruizfernandez05@gmail.com> | 2025-11-10 23:50:02 +0100 |
|---|---|---|
| committer | arf20 <aruizfernandez05@gmail.com> | 2025-11-10 23:50:02 +0100 |
| commit | 6bf70db630afa19d0407d6468df8de307bccfecd (patch) | |
| tree | 6f33f87121d6b00424fb3bec4230e8ac9feefc8c | |
| parent | 26aedc1555186b3be00cf719c2376585035b86af (diff) | |
| download | arfnet2-status-6bf70db630afa19d0407d6468df8de307bccfecd.tar.gz arfnet2-status-6bf70db630afa19d0407d6468df8de307bccfecd.zip | |
fix config
| -rw-r--r-- | config.c | 25 | ||||
| -rw-r--r-- | config.h | 2 | ||||
| -rw-r--r-- | main.c | 14 | ||||
| -rw-r--r-- | monitor.c | 6 | ||||
| -rw-r--r-- | monitor.cfg.example | 5 |
5 files changed, 38 insertions, 14 deletions
@@ -5,8 +5,9 @@ #include <string.h> #include <errno.h> -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"); @@ -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; @@ -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); } } @@ -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 |
