mirror of
https://github.com/eosswedenorg/antelope-api-healthcheck
synced 2026-06-20 09:56:49 +02:00
Update to github.com/eosswedenorg-go/haproxy@v0.1.0
This commit is contained in:
parent
fa740af0b1
commit
3aece958b1
7 changed files with 90 additions and 51 deletions
|
|
@ -4,7 +4,7 @@ package api
|
|||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"github.com/eosswedenorg-go/haproxy"
|
||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||
contract_api "github.com/eosswedenorg-go/eos-contract-api-client"
|
||||
)
|
||||
|
||||
|
|
@ -32,30 +32,40 @@ func (e EosioContract) LogInfo() LogParams {
|
|||
|
||||
// check_api - Validates head block time.
|
||||
// ---------------------------------------------------------
|
||||
func (e EosioContract) Call() (haproxy.HealthCheckStatus, string) {
|
||||
func (e EosioContract) Call() (agentcheck.Response, string) {
|
||||
|
||||
h, err := e.client.GetHealth()
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("%s", err);
|
||||
return haproxy.HealthCheckFailed, msg
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "Failed to contact api")
|
||||
return resp, err.Error()
|
||||
}
|
||||
|
||||
// Check HTTP Status Code
|
||||
if h.HTTPStatusCode > 299 {
|
||||
return haproxy.HealthCheckDown,
|
||||
fmt.Sprintf("Taking offline because %v was received from backend", h.HTTPStatusCode)
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down,
|
||||
fmt.Sprintf("HTTP %v", h.HTTPStatusCode))
|
||||
|
||||
msg := "Taking offline because %v was received from backend"
|
||||
return resp, fmt.Sprintf(msg, h.HTTPStatusCode)
|
||||
}
|
||||
|
||||
// Check postgres
|
||||
if h.Data.Postgres.Status != "OK" {
|
||||
return haproxy.HealthCheckDown,
|
||||
fmt.Sprintf("Taking offline because Postgres reported '%s'", h.Data.Postgres.Status)
|
||||
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down,
|
||||
fmt.Sprintf("Postgres: %s", h.Data.Postgres.Status))
|
||||
|
||||
msg := "Taking offline because Postgres reported '%s'"
|
||||
return resp, fmt.Sprintf(msg, h.Data.Postgres.Status)
|
||||
}
|
||||
|
||||
// Check redis
|
||||
if h.Data.Redis.Status != "OK" {
|
||||
return haproxy.HealthCheckDown,
|
||||
fmt.Sprintf("Taking offline because Redis reported '%s'", h.Data.Redis.Status)
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down,
|
||||
fmt.Sprintf("Redis: %s", h.Data.Redis.Status))
|
||||
|
||||
msg := "Taking offline because Redis reported '%s'"
|
||||
return resp, fmt.Sprintf(msg, h.Data.Redis.Status)
|
||||
}
|
||||
|
||||
// Validate head block.
|
||||
|
|
@ -63,12 +73,19 @@ func (e EosioContract) Call() (haproxy.HealthCheckStatus, string) {
|
|||
diff := now.Sub(h.Data.Chain.HeadTime).Seconds()
|
||||
|
||||
if diff > e.block_time {
|
||||
return haproxy.HealthCheckDown,
|
||||
fmt.Sprintf("Taking offline because head block is lagging %.0f seconds", diff)
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down,
|
||||
fmt.Sprintf("headblock is %.0f seconds behind", diff))
|
||||
|
||||
msg := "Taking offline because head block is lagging %.0f seconds"
|
||||
return resp, fmt.Sprintf(msg, diff)
|
||||
} else if diff < -e.block_time {
|
||||
return haproxy.HealthCheckDown,
|
||||
fmt.Sprintf("Taking offline because head block is %.0f seconds into the future", diff)
|
||||
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down,
|
||||
fmt.Sprintf("headblock is %.0f into the future", diff))
|
||||
|
||||
msg := "Taking offline because head block is %.0f seconds into the future"
|
||||
return resp, fmt.Sprintf(msg, diff)
|
||||
}
|
||||
|
||||
return haproxy.HealthCheckUp, "OK"
|
||||
return agentcheck.NewStatusResponse(agentcheck.Up), "OK"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"github.com/eosswedenorg-go/haproxy"
|
||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||
)
|
||||
|
||||
type ApiInterface interface {
|
||||
|
|
@ -11,5 +11,5 @@ type ApiInterface interface {
|
|||
LogInfo() LogParams
|
||||
|
||||
// Call api and validate it's status.
|
||||
Call() (haproxy.HealthCheckStatus, string)
|
||||
Call() (agentcheck.Response, string)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ package api
|
|||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"github.com/eosswedenorg-go/haproxy"
|
||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||
"github.com/eosswedenorg-go/eosapi"
|
||||
)
|
||||
|
||||
|
|
@ -35,18 +35,22 @@ func (e EosioV1) LogInfo() LogParams {
|
|||
return p
|
||||
}
|
||||
|
||||
func (e EosioV1) Call() (haproxy.HealthCheckStatus, string) {
|
||||
func (e EosioV1) Call() (agentcheck.Response, string) {
|
||||
|
||||
info, err := eosapi.GetInfo(e.params)
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("%s", err);
|
||||
return haproxy.HealthCheckFailed, msg
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "Failed to contact api")
|
||||
return resp, err.Error()
|
||||
}
|
||||
|
||||
// Check HTTP Status Code
|
||||
if info.HTTPStatusCode > 299 {
|
||||
return haproxy.HealthCheckDown,
|
||||
fmt.Sprintf("Taking offline because %v was received from backend", info.HTTPStatusCode)
|
||||
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down,
|
||||
fmt.Sprintf("HTTP %v", info.HTTPStatusCode))
|
||||
|
||||
msg := "Taking offline because %v was received from backend"
|
||||
return resp, fmt.Sprintf(msg, info.HTTPStatusCode)
|
||||
}
|
||||
|
||||
// Validate head block.
|
||||
|
|
@ -54,11 +58,19 @@ func (e EosioV1) Call() (haproxy.HealthCheckStatus, string) {
|
|||
diff := now.Sub(info.HeadBlockTime).Seconds()
|
||||
|
||||
if diff > e.block_time {
|
||||
return haproxy.HealthCheckDown,
|
||||
fmt.Sprintf("Taking offline because head block is lagging %.0f seconds", diff)
|
||||
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down,
|
||||
fmt.Sprintf("headblock is %.0f seconds behind", diff))
|
||||
|
||||
msg := "Taking offline because head block is lagging %.0f seconds"
|
||||
return resp, fmt.Sprintf(msg, diff)
|
||||
} else if diff < -e.block_time {
|
||||
return haproxy.HealthCheckDown,
|
||||
fmt.Sprintf("Taking offline because head block is %.0f seconds into the future", diff)
|
||||
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down,
|
||||
fmt.Sprintf("headblock is %.0f into the future", diff))
|
||||
|
||||
msg := "Taking offline because head block is %.0f seconds into the future"
|
||||
return resp, fmt.Sprintf(msg, diff)
|
||||
}
|
||||
return haproxy.HealthCheckUp, "OK"
|
||||
return agentcheck.NewStatusResponse(agentcheck.Up), "OK"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ package api
|
|||
import (
|
||||
"fmt"
|
||||
"github.com/eosswedenorg/eosio-api-healthcheck/src/utils"
|
||||
"github.com/eosswedenorg-go/haproxy"
|
||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||
"github.com/eosswedenorg-go/eosapi"
|
||||
)
|
||||
|
||||
|
|
@ -35,18 +35,18 @@ func (e EosioV2) LogInfo() LogParams {
|
|||
return p
|
||||
}
|
||||
|
||||
func (e EosioV2) Call() (haproxy.HealthCheckStatus, string) {
|
||||
func (e EosioV2) Call() (agentcheck.Response, string) {
|
||||
|
||||
health, err := eosapi.GetHealth(e.params)
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("%s", err);
|
||||
return haproxy.HealthCheckFailed, msg
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "Failed to contact api")
|
||||
return resp, err.Error()
|
||||
}
|
||||
|
||||
// Check HTTP Status Code
|
||||
if health.HTTPStatusCode > 299 {
|
||||
return haproxy.HealthCheckDown,
|
||||
fmt.Sprintf("Taking offline because %v was received from backend", health.HTTPStatusCode)
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, fmt.Sprintf("HTTP %v", health.HTTPStatusCode))
|
||||
return resp, fmt.Sprintf("Taking offline because %v was received from backend", health.HTTPStatusCode)
|
||||
}
|
||||
|
||||
// Fetch elasticsearch and nodeos block numbers from json.
|
||||
|
|
@ -65,17 +65,21 @@ func (e EosioV2) Call() (haproxy.HealthCheckStatus, string) {
|
|||
if es_block == 0 || node_block == 0 {
|
||||
msg := fmt.Sprintf("Failed to get Elasticsearch and/or nodeos " +
|
||||
"block numbers (es: %d, eos: %d)", es_block, node_block)
|
||||
return haproxy.HealthCheckFailed, msg
|
||||
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, msg)
|
||||
return resp, msg
|
||||
}
|
||||
|
||||
// Check if ES is behind or in the future.
|
||||
diff := node_block - es_block;
|
||||
if diff > e.offset {
|
||||
return haproxy.HealthCheckDown,
|
||||
fmt.Sprintf("Taking offline because Elastic is %d blocks behind", diff)
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down,
|
||||
fmt.Sprintf("Elastic is %d blocks behind", diff))
|
||||
return resp, fmt.Sprintf("Taking offline because Elastic is %d blocks behind", diff)
|
||||
} else if diff < -e.offset {
|
||||
return haproxy.HealthCheckDown,
|
||||
fmt.Sprintf("Taking offline because Elastic is %d blocks into the future", -1 * diff)
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down,
|
||||
fmt.Sprintf("Elastic is %d blocks into the future", -1 * diff))
|
||||
return resp, fmt.Sprintf("Taking offline because Elastic is %d blocks into the future", -1 * diff)
|
||||
}
|
||||
return haproxy.HealthCheckUp, "OK"
|
||||
return agentcheck.NewStatusResponse(agentcheck.Up), "OK"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import (
|
|||
"strconv"
|
||||
"github.com/eosswedenorg/eosio-api-healthcheck/src/api"
|
||||
"github.com/eosswedenorg-go/eosapi"
|
||||
"github.com/eosswedenorg-go/haproxy"
|
||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||
"github.com/eosswedenorg-go/tcp_server"
|
||||
)
|
||||
|
||||
|
|
@ -53,7 +53,9 @@ func onTcpMessage(c *tcp_server.Client, args string) {
|
|||
msg := "Invalid number of parameters in agent request"
|
||||
|
||||
logger.Warn("Agent request error", "message", msg, "args", split)
|
||||
c.WriteString(fmt.Sprintf("%s#%s\n", haproxy.HealthCheckFailed, msg))
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, msg)
|
||||
|
||||
c.WriteString(resp.String())
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
|
|
@ -82,7 +84,9 @@ func onTcpMessage(c *tcp_server.Client, args string) {
|
|||
healthCheckApi, err := createApi(&a)
|
||||
if err != nil {
|
||||
logger.Warn("Agent request error", "message", err)
|
||||
c.WriteString(fmt.Sprintf("%s#%s\n", haproxy.HealthCheckFailed, err))
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, err.Error())
|
||||
|
||||
c.WriteString(resp.String())
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
|
|
@ -90,15 +94,15 @@ func onTcpMessage(c *tcp_server.Client, args string) {
|
|||
status, msg := healthCheckApi.Call()
|
||||
|
||||
logger.Info("API Check", append([]interface{}{
|
||||
"status", status},
|
||||
"status", strings.TrimSpace(status.String())},
|
||||
healthCheckApi.LogInfo().ToSlice()...)...)
|
||||
|
||||
if status != haproxy.HealthCheckUp && len(msg) > 0 {
|
||||
if msg != "OK" && len(msg) > 0 {
|
||||
logger.Warn("API Check Failed", "message", msg)
|
||||
}
|
||||
|
||||
// Report status to HAproxy
|
||||
c.WriteString(fmt.Sprintln(status))
|
||||
c.WriteString(status.String())
|
||||
c.Close()
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue