1
0
Fork 0
mirror of https://github.com/eosswedenorg/antelope-api-healthcheck synced 2026-06-18 05:00:03 +02:00

Move source files to "src" folder.

This commit is contained in:
Henrik Hautakoski 2020-03-06 11:35:36 +01:00
parent 8e8393d560
commit 4ef714a750
6 changed files with 1 additions and 1 deletions

68
src/eosapi/functions.go Normal file
View file

@ -0,0 +1,68 @@
package eosapi;
import (
"time"
"net/url"
"io/ioutil"
"github.com/imroc/req"
"github.com/liamylian/jsontime/v2"
)
var json = v2.ConfigWithCustomTimeFormat
func init() {
// EOS Api does not specify timezone in timestamps (they are always UTC tho).
v2.SetDefaultTimeFormat("2006-01-02T15:04:05", time.UTC)
}
func send(method string, api_url string) (*req.Resp, error) {
u, err := url.Parse(api_url)
if err != nil {
return nil, err
}
// Go's net.http (that `req` uses) sends the port in the host header.
// nodeos api does not like that, so we need to provide our
// own Host header with just the host.
headers := req.Header{
"Host": u.Host,
}
r := req.New()
return r.Do(method, api_url, headers)
}
// GetInfo - Fetches get_info from API
// ---------------------------------------------------------
func GetInfo(url string) (Info, error) {
var info Info
r, err := send("GET", url + "/v1/chain/get_info")
if err == nil {
resp := r.Response()
body, _ := ioutil.ReadAll(resp.Body)
// Parse json
err = json.Unmarshal(body, &info)
}
return info, err
}
func GetHealth(url string) (Health, error) {
var health Health;
r, err := send("GET", url + "/v2/health")
if err == nil {
resp := r.Response()
body, _ := ioutil.ReadAll(resp.Body)
// Parse json
err = json.Unmarshal(body, &health)
}
return health, err
}

25
src/eosapi/types.go Normal file
View file

@ -0,0 +1,25 @@
package eosapi;
import "time"
// get_info format (not all fields).
type Info struct {
ServerVersion string `json:"server_version"`
HeadBlockNum int64 `json:"head_block_num"`
HeadBlockTime time.Time `json:"head_block_time"`
}
// Service struct from /v2/health
type Service struct {
Name string `json:"service"`
Status string `json:"status"`
Data map[string]interface{} `json:"service_data"`
Time int64 `json:"time"` // unix timestamp.
}
// /v2/health format (not all fields).
type Health struct {
VersionHash string `json:"version_hash"`
Health []Service `json:"health"`
}

15
src/haproxy/types.go Normal file
View file

@ -0,0 +1,15 @@
package haproxy;
// All supported health check values for HAproxy.
// See https://cbonte.github.io/haproxy-dconv/1.7/configuration.html#5.2-agent-check
type HealthCheckStatus string
const (
HealthCheckUp = "up"
HealthCheckDown = "down"
HealthCheckMaint = "maint"
HealthCheckReady = "ready"
HealthCheckDrain = "drain"
HealthCheckFailed = "failed"
HealthCheckStopped = "Stopped"
)

49
src/log/log.go Normal file
View file

@ -0,0 +1,49 @@
package log
import "fmt"
// Colors
// ---------------------------------------------------------
const (
InfoColor = "\033[1;34m%s\033[0m"
NoticeColor = "\033[1;36m%s\033[0m"
WarningColor = "\033[1;33m%s\033[0m"
ErrorColor = "\033[1;31m%s\033[0m"
)
// Define LogFunc prototype
// Function that takes a format string and variadic number
// of arguments (like printf)
// ---------------------------------------------------------
type LogFunc func(format string, args ...interface{})
// Create a log function.
// This is the base logging function. by providing a prefix
// a new log function of LogFunc type will be created
// appending "[ <prefix> ]" before the message.
// ---------------------------------------------------------
func logfn(prefix string) LogFunc {
return func(format string, args ...interface{}) {
format = "[" + prefix + "] " + format + "\n"
fmt.Printf(format, args...)
}
}
// Declare our different log functions.
// ---------------------------------------------------------
var Info LogFunc
var Notice LogFunc
var Warning LogFunc
var Error LogFunc
// Initilize log module
// ---------------------------------------------------------
func init() {
// Initilize functions.
Info = logfn(fmt.Sprintf(InfoColor, "INFO"))
Notice = logfn(fmt.Sprintf(NoticeColor, "NOTICE"))
Warning = logfn(fmt.Sprintf(WarningColor, "WARN"))
Error = logfn(fmt.Sprintf(ErrorColor, "ERROR"))
}

166
src/server.go Normal file
View file

@ -0,0 +1,166 @@
package main
import (
"os"
"fmt"
"time"
"strings"
"strconv"
"./log"
"./haproxy"
"./eosapi"
"github.com/firstrow/tcp_server"
)
// check_api - Validates head block time.
// ---------------------------------------------------------
func check_api(url string, block_time float64) (haproxy.HealthCheckStatus, string) {
info, err := eosapi.GetInfo(url)
if err != nil {
msg := fmt.Sprintf("%s", err);
return haproxy.HealthCheckFailed, msg
}
// Validate head block.
now := time.Now().In(time.UTC)
diff := now.Sub(info.HeadBlockTime).Seconds()
if diff > block_time {
return haproxy.HealthCheckDown,
fmt.Sprintf("Taking offline because head block is lagging %.0f seconds", diff)
} else if diff < -block_time {
return haproxy.HealthCheckDown,
fmt.Sprintf("Taking offline because head block is %.0f seconds into the future", diff)
}
return haproxy.HealthCheckUp, "OK"
}
// check_api_v2 (hyperion)
// Validates block num diff between
// nodeos and elasticsearch
// ---------------------------------------------------------
func check_api_v2(url string, offset int64) (haproxy.HealthCheckStatus, string) {
health, err := eosapi.GetHealth(url)
if err != nil {
msg := fmt.Sprintf("%s", err);
return haproxy.HealthCheckFailed, msg
}
// Fetch elasticsearch and nodeos block numbers from json.
var es_block int64 = 0
var node_block int64 = 0
for _, v := range health.Health {
if v.Name == "Elasticsearch" {
es_block = (int64) (v.Data["last_indexed_block"].(float64))
} else if v.Name == "NodeosRPC" {
node_block = (int64) (v.Data["head_block_num"].(float64))
}
}
// Error out if ether or both are zero.
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
}
// Check if ES is behind or in the future.
diff := node_block - es_block;
if diff > offset {
return haproxy.HealthCheckDown,
fmt.Sprintf("Taking offline because Elastic is %d blocks behind", diff)
} else if diff < -offset {
return haproxy.HealthCheckDown,
fmt.Sprintf("Taking offline because Elastic is %d blocks into the future", -1 * diff)
}
return haproxy.HealthCheckUp, "OK"
}
// argv_listen_addr
// Parse listen address from command line.
// ---------------------------------------------------------
func argv_listen_addr() string {
var addr string
argv := os.Args[1:]
if len(argv) > 0 {
addr = argv[0]
} else {
addr = "127.0.0.1"
}
addr += ":"
if len(argv) > 1 {
addr += argv[1]
} else {
addr += "1337"
}
return addr
}
// main
// ---------------------------------------------------------
func main() {
server := tcp_server.New(argv_listen_addr())
// TCP Client connect.
server.OnNewClient(func(c *tcp_server.Client) {
//fmt.Println("# Client connected")
});
// TCP Client sends message.
server.OnNewMessage(func(c *tcp_server.Client, message string) {
var url string
var block_time int = 10
var version string = "v1"
// Parse host + port.
split := strings.Split(strings.TrimSpace(message), "|")
url = split[0]
if len(split) > 1 {
p, err := strconv.ParseInt(split[1], 10, 32)
if err == nil {
block_time = int(p)
}
}
if len(split) > 2 {
version = split[2]
}
// Check api.
var status haproxy.HealthCheckStatus
var msg string
if version == "v2" {
status, msg = check_api_v2(url, int64(block_time / 2))
} else {
version = "v1"
status, msg = check_api(url, float64(block_time))
}
log.Info("Status %s - %s (%d blocks): %s",
version, url, block_time / 2, status)
if status != haproxy.HealthCheckUp && len(msg) > 0 {
log.Warning(msg)
}
// Report status to HAproxy
c.Send(fmt.Sprintln(status))
c.Close()
});
// TCP Client disconnect.
server.OnClientConnectionClosed(func(c *tcp_server.Client, err error) {
//fmt.Println("# Client disconnected")
});
server.Listen()
}