From 43be0d7be77ee5829058c5704a76db0765fe19c8 Mon Sep 17 00:00:00 2001 From: arf20 Date: Mon, 20 Oct 2025 22:10:00 +0200 Subject: C webapp --- Makefile | 14 ++++++++ README.md | 7 ++++ index.htm.tmpl | 8 ++++- main.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++ monitor | Bin 0 -> 29912 bytes monitor.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ monitor.cfg | 6 ++++ monitor.h | 8 +++++ 8 files changed, 245 insertions(+), 1 deletion(-) create mode 100644 Makefile create mode 100644 main.c create mode 100755 monitor create mode 100644 monitor.c create mode 100644 monitor.cfg create mode 100644 monitor.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..bc05321 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +CC = gcc +CFLAGS = -g -Wall -pedantic +LDFLAGS = -lmicrohttpd + +BIN = monitor +SRC = main.c monitor.c + +$(BIN): $(SRC) + $(CC) -o $@ $(CFLAGS) $^ $(LDFLAGS) + +.PHONY: clean +clean: + rm $(BIN) + diff --git a/README.md b/README.md index 2f0b344..a373746 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,10 @@ # arfnet2-status ARFNET Status Monitor + +## Features (TODO) + + - Current service status table + - Outage events table + - Uptime counter (total, month) + diff --git a/index.htm.tmpl b/index.htm.tmpl index a9022d8..462f744 100644 --- a/index.htm.tmpl +++ b/index.htm.tmpl @@ -4,6 +4,11 @@ ARFNET + @@ -15,7 +20,8 @@

Status Monitor

This webapp monitors the status of the main ARFNET services from outside the ARFNET network

- + + %s
TypeServiceStatus
diff --git a/main.c b/main.c new file mode 100644 index 0000000..f01d928 --- /dev/null +++ b/main.c @@ -0,0 +1,98 @@ +#include +#include +#include + +#include +#include + +#include + +#include +#include +#include +#include + +#include "monitor.h" + +#define PORT 8888 +#define RES_BUFF 65535 + +static char *index_format_template = NULL; + + +enum MHD_Result answer_to_connection( + void *cls, struct MHD_Connection *connection, + const char *url, + const char *method, + const char *version, + const char *upload_data, + size_t *upload_data_size, + void **ptr +) { + char buff[RES_BUFF]; + + const struct sockaddr_in **coninfo = + (const struct sockaddr_in**)MHD_get_connection_info( + connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS); + + printf("%s - %s %s: ", inet_ntoa((*coninfo)->sin_addr), method, url); + + struct MHD_Response *response; + int ret; + + if (strcmp(method, "GET") == 0 && strcmp(url, "/") == 0) { + snprintf(buff, RES_BUFF, + index_format_template, monitor_generate_status_html()); + + response = MHD_create_response_from_buffer(strlen(buff), (void*)buff, + MHD_RESPMEM_PERSISTENT); + + printf("%d\n", 200); + ret = MHD_queue_response(connection, MHD_HTTP_OK, response); + MHD_destroy_response(response); + } + else { + response = MHD_create_response_from_buffer(0, (void*)NULL, 0); + printf("%d\n", 418); + ret = MHD_queue_response(connection, 418, response); + MHD_destroy_response(response); + } + return ret; +} + +int main() { + /* read index template file */ + FILE *tf = fopen("index.htm.tmpl", "r"); + if (!tf) { + fprintf(stderr, "error opening index template file: %s\n", + strerror(errno)); + return 1; + } + fseek(tf, 0, SEEK_END); + size_t tfs = ftell(tf); + rewind(tf); + index_format_template = malloc(tfs); + fread(index_format_template, 1, tfs, tf); + fclose(tf); + + /* start server */ + struct MHD_Daemon *daemon; + + daemon = MHD_start_daemon( + MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_EPOLL, + PORT, NULL, NULL, + &answer_to_connection, NULL, MHD_OPTION_END); + + if (!daemon) { + fprintf(stderr, "error starting libmicrohttpd daemon: \n"); + return 1; + } + + monitor_init("monitor.cfg"); + + while (1) { + monitor_check(); + sleep(5); + } +} + diff --git a/monitor b/monitor new file mode 100755 index 0000000..3992ddc Binary files /dev/null and b/monitor differ 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() +{ + +} + diff --git a/monitor.cfg b/monitor.cfg new file mode 100644 index 0000000..1e2f9dc --- /dev/null +++ b/monitor.cfg @@ -0,0 +1,6 @@ +# Monitor config +# type=target +reach=2.59.235.35 +dns=arf20.com +web=http://arf20.com + diff --git a/monitor.h b/monitor.h new file mode 100644 index 0000000..f9d4458 --- /dev/null +++ b/monitor.h @@ -0,0 +1,8 @@ +#ifndef _MONITOR_H +#define _MONITOR_H + +int monitor_init(const char *cfg_file); +const char *monitor_generate_status_html(); +void monitor_check(); + +#endif /* _MONITOR_H */ -- cgit v1.2.3