From 43be0d7be77ee5829058c5704a76db0765fe19c8 Mon Sep 17 00:00:00 2001 From: arf20 Date: Mon, 20 Oct 2025 22:10:00 +0200 Subject: C webapp --- monitor.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 monitor.c (limited to 'monitor.c') diff --git a/monitor.c b/monitor.c new file mode 100644 index 0000000..c9903ce --- /dev/null +++ b/monitor.c @@ -0,0 +1,105 @@ +#include +#include +#include +#include + +#include "monitor.h" + +typedef enum { + TYPE_REACH, + TYPE_DNS, + TYPE_WEB +} type_t; + +const char *type_str[] = { "reach", "dns", "web" }; + +typedef enum { + STATUS_DOWN, + STATUS_UP +} status_t; + +typedef struct { + type_t type; + char *target; + status_t status; +} target_t; + +static target_t targets[256]; +static size_t target_n = 0; + +int +monitor_init(const char *cfg_file) { + FILE *cfgf = fopen(cfg_file, "r"); + if (!cfgf) { + fprintf(stderr, "Error opening config: %s\n", strerror(errno)); + return -1; + } + + printf("monitor targets:\n"); + + char line[256]; + while (fgets(line, sizeof(line), cfgf)) { + if (*line == '#' || *line == '\n') + continue; + + char *type = line; + char *target = strchr(line, '='); + if (!target) { + fprintf(stderr, "malformed config line: %s\n", line); + continue; + } + + + *target = '\0'; + target++; + + if (strcmp(type, "reach") == 0) + targets[target_n].type = TYPE_REACH; + else if (strcmp(type, "dns") == 0) + targets[target_n].type = TYPE_DNS; + else if (strcmp(type, "web") == 0) + targets[target_n].type = TYPE_WEB; + + targets[target_n].target = strdup(target); + targets[target_n].status = STATUS_DOWN; + + printf("\t%s: %s", type_str[targets[target_n].type], + targets[target_n].target); + + target_n++; + } + + fclose(cfgf); + + return 0; +} + +const char * +monitor_generate_status_html() +{ + static char buff[65535]; + + static char *status_html[] = { + "down", + "up" + }; + + char *pos = buff; + + for (size_t i = 0; i < target_n; i++) { + pos += snprintf(pos, 65535, + "%s%s%s\n", + type_str[targets[i].type], + targets[i].target, + status_html[targets[i].status]); + } + + return buff; +} + +void +monitor_check() +{ + +} + -- cgit v1.2.3