diff --git a/build.sh b/build.sh old mode 100755 new mode 100644 index fb2435b..f13c755 --- a/build.sh +++ b/build.sh @@ -29,10 +29,17 @@ build_binary() { mkdir -p dist echo "Building for ${os}/${arch} -> dist/${output_name}" - GOOS=${os} GOARCH=${arch} go build -o dist/${output_name} $go_dir + GOOS=${os} GOARCH=${arch} go build -v -o dist/${output_name} $go_dir } # Main Build Process +# Initialize Go module if needed +if [ ! -f "go.mod" ]; then + echo "Initializing Go module..." + go mod init git.nixc.us/Nixius/host_check + go mod tidy +fi + for arch in "${ARCHITECTURES[@]}"; do IFS='/' read -r -a parts <<< "$arch" os=${parts[0]} diff --git a/dist/host_check b/dist/host_check new file mode 100755 index 0000000..ea69307 Binary files /dev/null and b/dist/host_check differ diff --git a/dist/host_check_linux_arm b/dist/host_check_linux_arm new file mode 100755 index 0000000..7430251 Binary files /dev/null and b/dist/host_check_linux_arm differ diff --git a/dist/host_check_linux_arm64 b/dist/host_check_linux_arm64 new file mode 100755 index 0000000..4513534 Binary files /dev/null and b/dist/host_check_linux_arm64 differ diff --git a/host_check.go b/host_check.go index 9ce96f7..576ee50 100644 --- a/host_check.go +++ b/host_check.go @@ -9,27 +9,43 @@ import ( ) func main() { - // ... (Hostname handling as before) ... + hostnames := os.Getenv("HOSTNAMES") + if hostnames == "" { + fmt.Println("Error: Environment variable HOSTNAMES not set or empty.") + return + } + hosts := strings.Split(hostnames, ",") + // Environment Variables failOnError := os.Getenv("FAIL_ON_ERROR") == "true" - commandStr := os.Getenv("COMMAND") - errorCommandStr := os.Getenv("ERROR_COMMAND") // Fetch error command + commandStr := os.Getenv("COMMAND") + errorCommandStr := os.Getenv("ERROR_COMMAND") for _, host := range hosts { - // ... (DNS Resolution as before) ... + host = strings.TrimSpace(host) + + fmt.Println("\n---", host, "---") + + // DNS Resolution (Remove if you don't need this) + ip, err := net.LookupIP(host) + if err != nil { + fmt.Println("DNS Resolution Failed:", err) + } else { + fmt.Println("Resolved IPs:", ip) + } // Ping - pingCmd := exec.Command("ping", "-c", "3", host) + pingCmd := exec.Command("ping", "-c", "3", host) pingOutput, err := pingCmd.Output() if err != nil { fmt.Println("Ping Failed:", err) if failOnError { - os.Exit(1) // Exit with error code + os.Exit(1) } // Execute error command if specified if errorCommandStr != "" { - cmdParts := strings.Fields(errorCommandStr) + cmdParts := strings.Fields(errorCommandStr) cmd := exec.Command(cmdParts[0], cmdParts[1:]...) cmdOutput, cmdErr := cmd.Output() if cmdErr != nil { @@ -44,11 +60,11 @@ func main() { // Execute additional command if specified if commandStr != "" { - cmdParts := strings.Fields(commandStr) // Split on spaces + cmdParts := strings.Fields(commandStr) cmd := exec.Command(cmdParts[0], cmdParts[1:]...) cmdOutput, err := cmd.Output() if err != nil { - fmt.Println("Command Execution Failed:", cmdStr, err) + fmt.Println("Command Execution Failed:", commandStr, err) } else { fmt.Println("Command Output:\n", string(cmdOutput)) }