From f4fdc29d80d53af0287411ee1b994adcff4160df Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Sat, 22 Jun 2024 17:34:01 +1000 Subject: [PATCH] feat: Add initial monitoring --- main.go | 51 ++++++++++++++++++++++++++-- monitor.log | 18 ++++++++++ templates/about.html | 13 ------- templates/index.html | 20 +++++------ test.sh | 80 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 157 insertions(+), 25 deletions(-) create mode 100644 monitor.log delete mode 100644 templates/about.html create mode 100755 test.sh diff --git a/main.go b/main.go index 0a3377e..4651e4e 100644 --- a/main.go +++ b/main.go @@ -1,16 +1,22 @@ package main import ( + "bytes" "fmt" "html/template" "log" "net/http" "os" + "os/exec" "path/filepath" + "time" "github.com/joho/godotenv" ) +const logFilePath = "monitor.log" +const testFilePath = "test.sh" + func main() { // Load the .env file err := godotenv.Load() @@ -22,10 +28,15 @@ func main() { log.Fatal("Error loading .env.example file") } } + + // Start ticker + go startTicker() + + // Start the server http.HandleFunc("/", mainHandler) http.HandleFunc("/assets/", assetHandler) - fmt.Println("Starting server on :3000 ...") + fmt.Println("Starting server on http://127.0.0.1:3000 ...") log.Fatal(http.ListenAndServe(":3000", nil)) } @@ -62,8 +73,14 @@ func getHandler(relPath string, w http.ResponseWriter, r *http.Request) { return } + // Try to get monitor.log file + logData, err := os.ReadFile(logFilePath) + if err != nil { + logData = []byte("No log file found") + } + // Create a map to hold the data - requestData := map[string]interface{}{"envMessage": message} + requestData := map[string]interface{}{"envMessage": message, "logContent": string(logData)} // Execute the template err = tmpl.Execute(w, requestData) @@ -108,6 +125,9 @@ func indexPostHandler(w http.ResponseWriter, r *http.Request) { } err = tmpl.Execute(w, requestData) + if err != nil { + errorPage(500, w, r) + } } @@ -148,3 +168,30 @@ func assetHandler(w http.ResponseWriter, r *http.Request) { // Serve static files from templates/assets http.ServeFile(w, r, filepath.Join("templates", filepath.Clean(r.URL.Path))) } + +func startTicker() { + ticker := time.NewTicker(10 * time.Second) + defer ticker.Stop() + + for { + select { + case <-ticker.C: + runBashScript() + } + } +} + +func runBashScript() { + // Log the time + log.Printf("Running script at %v", time.Now()) + + cmd := exec.Command("bash", testFilePath) + var out bytes.Buffer + cmd.Stdout = &out + cmd.Stderr = &out + err := cmd.Run() + if err != nil { + log.Printf("Error running script: %v", err) + return + } +} diff --git a/monitor.log b/monitor.log new file mode 100644 index 0000000..83ebc27 --- /dev/null +++ b/monitor.log @@ -0,0 +1,18 @@ +Sat 22 Jun 2024 17:29:46 AEST - Test +Sat 22 Jun 2024 17:30:35 AEST - Test +Sat 22 Jun 2024 17:30:50 AEST - Test +Sat 22 Jun 2024 17:31:01 AEST - Test +Sat 22 Jun 2024 17:31:14 AEST - Test +Sat 22 Jun 2024 17:31:25 AEST - Test +Sat 22 Jun 2024 17:31:38 AEST - Test +Sat 22 Jun 2024 17:31:51 AEST - Test +Sat 22 Jun 2024 17:32:04 AEST - Test +Sat 22 Jun 2024 17:32:09 AEST - kdig +tls-ca +https failed for NODE_IP=172.105.120.203 +Sat 22 Jun 2024 17:32:20 AEST - Test +Sat 22 Jun 2024 17:32:33 AEST - Test +Sat 22 Jun 2024 17:32:47 AEST - Test +Sat 22 Jun 2024 17:32:59 AEST - Test +Sat 22 Jun 2024 17:33:11 AEST - Test +Sat 22 Jun 2024 17:33:25 AEST - Test +Sat 22 Jun 2024 17:33:37 AEST - Test +Sat 22 Jun 2024 17:33:51 AEST - Test diff --git a/templates/about.html b/templates/about.html deleted file mode 100644 index 15acde2..0000000 --- a/templates/about.html +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - Go HTML Template - - - - -

About page

- - \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index d9ef1c7..b91ed4f 100644 --- a/templates/index.html +++ b/templates/index.html @@ -8,17 +8,17 @@ -

Hello, World!

- {{if .envMessage }} -

{{.envMessage}}

+

HNSDoH Node Status

+ {{ if .logContent}} +

Log Output

+
{{.logContent}}
{{end}} - {{if .message}} -

{{.message}}

- {{end}} -
- - -
+ + \ No newline at end of file diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..a5a6cd1 --- /dev/null +++ b/test.sh @@ -0,0 +1,80 @@ +#!/bin/bash + +# Check if script has ip address as argument +if [ -z "$1" ]; then + # Get Node IPs + RESOLVED_IPS=$(dig +short hnsdoh.com) + NODE_IPS=($RESOLVED_IPS) + if [ ${#NODE_IPS[@]} -eq 0 ]; then + echo "No IP addresses resolved for hnsdoh.com. Exiting." + exit 1 + fi +fi + +# If script has ip address as argument, use that +if [ -n "$1" ]; then + # Add all arguments to NODE_IPS array + NODE_IPS=("$@") +fi + + + +# Define the domain and host for kdig commands +TLS_HOST="hnsdoh.com" +DOH_URL="https://hnsdoh.com/dns-query" + +#!/bin/bash + +# Check if script has IP address as argument +if [ -z "$1" ]; then + # Get Node IPs + RESOLVED_IPS=$(dig +short hnsdoh.com) + NODE_IPS=($RESOLVED_IPS) + if [ ${#NODE_IPS[@]} -eq 0 ]; then + echo "No IP addresses resolved for hnsdoh.com. Exiting." + exit 1 + fi +fi + +# If script has IP address as argument, use that +if [ -n "$1" ]; then + # Add all arguments to NODE_IPS array + NODE_IPS=("$@") +fi + +# Define the domain and host for dig commands +TLS_HOST="hnsdoh.com" +DOH_URL="https://hnsdoh.com/dns-query" + +# Function to check dig command result +check_dig() { + if [ $? -eq 0 ]; then + echo "$(date) - $1 succeeded for NODE_IP=$NODE_IP" + else + echo "$(date) - $1 failed for NODE_IP=$NODE_IP" + # Save the failed IP + echo "$(date) - $1 failed for NODE_IP=$NODE_IP" >> monitor.log + fi +} + +# Save test time +echo "$(date) - Test" >> monitor.log + + +# Loop over each IP and run the dig commands +for NODE_IP in "${NODE_IPS[@]}" +do + echo "$(date) - Running dig commands for NODE_IP=$NODE_IP" + + # Run the dig commands + kdig +tls +tls-host=$TLS_HOST @$NODE_IP 1.wdbrn TXT +short + check_dig "kdig +tls" + + kdig +tls-ca +https=@$DOH_URL @$NODE_IP 2.wdbrn TXT +short + check_dig "kdig +tls-ca +https" + + kdig @$NODE_IP 3.wdbrn TXT +short + check_dig "kdig" + + echo "$(date) - --------------------------------------------" +done