This commit is contained in:
Colin 2024-04-18 10:42:45 -04:00
parent 14c403ec22
commit 2ec13eb2f9
5 changed files with 33 additions and 10 deletions

9
build.sh Executable file → Normal file
View File

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

BIN
dist/host_check vendored Executable file

Binary file not shown.

BIN
dist/host_check_linux_arm vendored Executable file

Binary file not shown.

BIN
dist/host_check_linux_arm64 vendored Executable file

Binary file not shown.

View File

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