diff options
| author | arf20 <aruizfernandez05@gmail.com> | 2025-10-20 22:10:00 +0200 |
|---|---|---|
| committer | arf20 <aruizfernandez05@gmail.com> | 2025-10-20 22:10:00 +0200 |
| commit | 43be0d7be77ee5829058c5704a76db0765fe19c8 (patch) | |
| tree | 3d8fbcb798a18b22f7a35b7c3c05ec56ef69d1e3 | |
| parent | d3895a2203d6ff2965c8c6d4ff4314a690ae7767 (diff) | |
| download | arfnet2-status-43be0d7be77ee5829058c5704a76db0765fe19c8.tar.gz arfnet2-status-43be0d7be77ee5829058c5704a76db0765fe19c8.zip | |
C webapp
| -rw-r--r-- | Makefile | 14 | ||||
| -rw-r--r-- | README.md | 7 | ||||
| -rw-r--r-- | index.htm.tmpl | 8 | ||||
| -rw-r--r-- | main.c | 98 | ||||
| -rwxr-xr-x | monitor | bin | 0 -> 29912 bytes | |||
| -rw-r--r-- | monitor.c | 105 | ||||
| -rw-r--r-- | monitor.cfg | 6 | ||||
| -rw-r--r-- | monitor.h | 8 |
8 files changed, 245 insertions, 1 deletions
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) + @@ -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 @@ <meta charset="UTF-8"> <link rel="stylesheet" type="text/css" href="https://arf20.com/style.css"> <title>ARFNET</title> + <style> +table, th, td { + border: 1px solid #abb2bf; +} + </style> </head> <body> @@ -15,7 +20,8 @@ <h2 class="center">Status Monitor</h2> <p>This webapp monitors the status of the main ARFNET services from outside the ARFNET network</p> <table> - + <tr><th>Type</th><th>Service</th><th>Status</th></tr> + %s </table> </main> </body> @@ -0,0 +1,98 @@ +#include <sys/types.h> +#include <sys/select.h> +#include <sys/socket.h> + +#include <netinet/in.h> +#include <arpa/inet.h> + +#include <microhttpd.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> + +#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); + } +} + Binary files differdiff --git a/monitor.c b/monitor.c new file mode 100644 index 0000000..c9903ce --- /dev/null +++ b/monitor.c @@ -0,0 +1,105 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> + +#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, + "<tr><td>%s</td><td>%s</td><td>%s</td></tr>\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 */ |
