feat: Add initial monitoring
Some checks failed
Build Docker / BuildSite (push) Failing after 23s

This commit is contained in:
Nathan Woodburn 2024-06-22 17:34:01 +10:00
parent 9efd158b1f
commit f4fdc29d80
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1
5 changed files with 157 additions and 25 deletions

51
main.go
View File

@ -1,16 +1,22 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"html/template" "html/template"
"log" "log"
"net/http" "net/http"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"time"
"github.com/joho/godotenv" "github.com/joho/godotenv"
) )
const logFilePath = "monitor.log"
const testFilePath = "test.sh"
func main() { func main() {
// Load the .env file // Load the .env file
err := godotenv.Load() err := godotenv.Load()
@ -22,10 +28,15 @@ func main() {
log.Fatal("Error loading .env.example file") log.Fatal("Error loading .env.example file")
} }
} }
// Start ticker
go startTicker()
// Start the server
http.HandleFunc("/", mainHandler) http.HandleFunc("/", mainHandler)
http.HandleFunc("/assets/", assetHandler) 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)) log.Fatal(http.ListenAndServe(":3000", nil))
} }
@ -62,8 +73,14 @@ func getHandler(relPath string, w http.ResponseWriter, r *http.Request) {
return 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 // 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 // Execute the template
err = tmpl.Execute(w, requestData) err = tmpl.Execute(w, requestData)
@ -108,6 +125,9 @@ func indexPostHandler(w http.ResponseWriter, r *http.Request) {
} }
err = tmpl.Execute(w, requestData) 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 // Serve static files from templates/assets
http.ServeFile(w, r, filepath.Join("templates", filepath.Clean(r.URL.Path))) 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
}
}

18
monitor.log Normal file
View File

@ -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

View File

@ -1,13 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Go HTML Template</title>
<link rel="stylesheet" href="/assets/css/index.css">
<link rel="icon" href="/assets/img/favicon.png">
</head>
<body>
<h1>About page</h1>
</body>
</html>

View File

@ -8,17 +8,17 @@
<link rel="icon" href="/assets/img/favicon.png"> <link rel="icon" href="/assets/img/favicon.png">
</head> </head>
<body> <body>
<h1>Hello, World!</h1> <h1>HNSDoH Node Status</h1>
{{if .envMessage }} {{ if .logContent}}
<p>{{.envMessage}}</p> <h2>Log Output</h2>
<pre>{{.logContent}}</pre>
{{end}} {{end}}
{{if .message}} <!-- Reload automatically every 30 seconds -->
<p>{{.message}}</p> <script>
{{end}} setTimeout(function(){
<form action="/" method="post"> window.location.reload(1);
<input type="text" name="message" placeholder="Enter your message"> }, 30000);
<button type="submit">Submit</button> </script>
</form>
</body> </body>
</html> </html>

80
test.sh Executable file
View File

@ -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