Makes more checks for agent availability

main
Yasen Pramatarov 2024-10-22 17:38:02 +03:00
parent 79355f039c
commit 474f8fb098
1 changed files with 33 additions and 1 deletions

34
main.go
View File

@ -272,8 +272,40 @@ func statusHandler(config Config, w http.ResponseWriter, r *http.Request) {
// Check the status code from the response recorder
if rr.Result().StatusCode == http.StatusOK {
endpointStatuses[endpoint] = "available"
// Check if it's json
if rr.Header().Get("Content-Type") == "application/json" {
var result map[string]interface{}
if err := json.NewDecoder(rr.Body).Decode(&result); err == nil {
available := true
for key, value := range result {
if strings.HasSuffix(key, "_state") {
// If there is "*_state": "error" - it's accessible, but unavailable
if valueStr, ok := value(string); ok && valueStr == "error" {
available = false
break
}
// If there is "*_state": "running" - it's OK
if valueStr, ok := value(string); ok && valueStr == "running" {
available = true
break
}
}
}
if available {
endpointStatuses[endpoint] = "available"
} else {
endpointStatuses[endpoint] = "not available"
}
} else {
// Invalid JSON
endpointStatuses[endpoint] = "not available"
}
} else {
// It's not a JSON response
endpointStatuses[endpoint] = "not available"
}
} else {
// Reply not 200 OK
endpointStatuses[endpoint] = "not available"
}