mirror of
https://github.com/eosswedenorg/antelope-api-healthcheck
synced 2026-07-03 11:53:43 +02:00
Convert hard tabs to soft.
This commit is contained in:
parent
1dc31db4e1
commit
fa9a943215
7 changed files with 259 additions and 259 deletions
80
compile.sh
80
compile.sh
|
|
@ -5,19 +5,19 @@ SYSTEMS=( windows linux freebsd )
|
||||||
ARCHS=( 386 amd64 amd64p32 arm arm64 ppc ppc64 )
|
ARCHS=( 386 amd64 amd64p32 arm arm64 ppc ppc64 )
|
||||||
|
|
||||||
function usage() {
|
function usage() {
|
||||||
echo "Usage: ${0##*/} [ -h|--help ] [ -t|--target <system> ] [ -a|--arch <arch> ]"
|
echo "Usage: ${0##*/} [ -h|--help ] [ -t|--target <system> ] [ -a|--arch <arch> ]"
|
||||||
echo ""
|
echo ""
|
||||||
echo " Valid systems:"
|
echo " Valid systems:"
|
||||||
for i in "${SYSTEMS[@]}"; do
|
for i in "${SYSTEMS[@]}"; do
|
||||||
echo " * ${i}"
|
echo " * ${i}"
|
||||||
done
|
done
|
||||||
echo ""
|
echo ""
|
||||||
echo " Valid architectures:"
|
echo " Valid architectures:"
|
||||||
for i in "${ARCHS[@]}"; do
|
for i in "${ARCHS[@]}"; do
|
||||||
echo " * ${i}"
|
echo " * ${i}"
|
||||||
done
|
done
|
||||||
echo ""
|
echo ""
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
options=$(getopt -n "${0##*/}" -o "ht:a:" -l "help,target:,arch:" -- "$@")
|
options=$(getopt -n "${0##*/}" -o "ht:a:" -l "help,target:,arch:" -- "$@")
|
||||||
|
|
@ -30,40 +30,40 @@ MAKE_TARGET="all"
|
||||||
|
|
||||||
while true; do
|
while true; do
|
||||||
|
|
||||||
case $1 in
|
case $1 in
|
||||||
-t|--target)
|
-t|--target)
|
||||||
shift
|
shift
|
||||||
REGEX=$(echo "${SYSTEMS[@]}" | sed 's/[[:space:]]/|/g')
|
REGEX=$(echo "${SYSTEMS[@]}" | sed 's/[[:space:]]/|/g')
|
||||||
[[ ! "$1" =~ ^($REGEX)$ ]] && {
|
[[ ! "$1" =~ ^($REGEX)$ ]] && {
|
||||||
echo "Incorrect system '$1' provided"
|
echo "Incorrect system '$1' provided"
|
||||||
usage
|
usage
|
||||||
}
|
}
|
||||||
export GOOS=$1
|
export GOOS=$1
|
||||||
;;
|
;;
|
||||||
-a|--arch)
|
-a|--arch)
|
||||||
shift
|
shift
|
||||||
REGEX=$(echo "${ARCHS[@]}" | sed 's/[[:space:]]/|/g')
|
REGEX=$(echo "${ARCHS[@]}" | sed 's/[[:space:]]/|/g')
|
||||||
[[ ! "$1" =~ ^($REGEX)$ ]] && {
|
[[ ! "$1" =~ ^($REGEX)$ ]] && {
|
||||||
echo "Incorrect architecture '$1' provided"
|
echo "Incorrect architecture '$1' provided"
|
||||||
usage
|
usage
|
||||||
}
|
}
|
||||||
export GOARCH=$1
|
export GOARCH=$1
|
||||||
;;
|
;;
|
||||||
-h|--help) usage ;;
|
-h|--help) usage ;;
|
||||||
--) shift
|
--) shift
|
||||||
break
|
break
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
shift
|
shift
|
||||||
done
|
done
|
||||||
|
|
||||||
MESSAGE=""
|
MESSAGE=""
|
||||||
if [ ! -z "${GOOS}" ]; then
|
if [ ! -z "${GOOS}" ]; then
|
||||||
MESSAGE="[\e[34m::\e[0m] Crosscompiling for: ${GOOS}"
|
MESSAGE="[\e[34m::\e[0m] Crosscompiling for: ${GOOS}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ! -z "${GOARCH}" ]; then
|
if [ ! -z "${GOARCH}" ]; then
|
||||||
MESSAGE="${MESSAGE} (${GOARCH})"
|
MESSAGE="${MESSAGE} (${GOARCH})"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,15 +3,15 @@
|
||||||
BUILD_INFO=build/.buildinfo
|
BUILD_INFO=build/.buildinfo
|
||||||
|
|
||||||
if [ ! -f "${BUILD_INFO}" ]; then
|
if [ ! -f "${BUILD_INFO}" ]; then
|
||||||
echo "Could not find '${BUILD_INFO}' file, You need to compile first."
|
echo "Could not find '${BUILD_INFO}' file, You need to compile first."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
TYPE=$(cat "${BUILD_INFO}" | sed -n 's/^GOOS=\"\(.*\)\"/\1/p')
|
TYPE=$(cat "${BUILD_INFO}" | sed -n 's/^GOOS=\"\(.*\)\"/\1/p')
|
||||||
if [ "$TYPE" == "freebsd" ]; then
|
if [ "$TYPE" == "freebsd" ]; then
|
||||||
MAKE_TARGET="package_freebsd"
|
MAKE_TARGET="package_freebsd"
|
||||||
else
|
else
|
||||||
MAKE_TARGET="package_deb"
|
MAKE_TARGET="package_deb"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
make -B ${MAKE_TARGET}
|
make -B ${MAKE_TARGET}
|
||||||
|
|
|
||||||
|
|
@ -5,19 +5,19 @@ PACKAGE_RSYSLOGDIR=etc/rsyslog.d
|
||||||
PACKAGE_LOGROTATEDIR=etc/logrotate.d
|
PACKAGE_LOGROTATEDIR=etc/logrotate.d
|
||||||
|
|
||||||
if [[ -f /etc/upstream-release/lsb-release ]]; then
|
if [[ -f /etc/upstream-release/lsb-release ]]; then
|
||||||
source /etc/upstream-release/lsb-release
|
source /etc/upstream-release/lsb-release
|
||||||
elif [[ -f /etc/lsb-release ]]; then
|
elif [[ -f /etc/lsb-release ]]; then
|
||||||
source /etc/lsb-release
|
source /etc/lsb-release
|
||||||
else
|
else
|
||||||
echo "ERROR: could not determine debian release."
|
echo "ERROR: could not determine debian release."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
DISTRIB_ID=$(echo $DISTRIB_ID | tr '[:upper:]' '[:lower:]')
|
DISTRIB_ID=$(echo $DISTRIB_ID | tr '[:upper:]' '[:lower:]')
|
||||||
|
|
||||||
# Default to 1 if no release is set.
|
# Default to 1 if no release is set.
|
||||||
if [[ -z $RELEASE ]]; then
|
if [[ -z $RELEASE ]]; then
|
||||||
RELEASE="1"
|
RELEASE="1"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
PACKAGE_FULLNAME="${PACKAGE_NAME}_${PACKAGE_VERSION}-${RELEASE}-${DISTRIB_ID}-${DISTRIB_RELEASE}_amd64"
|
PACKAGE_FULLNAME="${PACKAGE_NAME}_${PACKAGE_VERSION}-${RELEASE}-${DISTRIB_ID}-${DISTRIB_RELEASE}_amd64"
|
||||||
|
|
@ -40,30 +40,30 @@ cat ${PACKAGE_TMPDIR}/DEBIAN/control
|
||||||
# Create service file
|
# Create service file
|
||||||
mkdir -p ${PACKAGE_TMPDIR}/${PACKAGE_SYSUNITDIR}
|
mkdir -p ${PACKAGE_TMPDIR}/${PACKAGE_SYSUNITDIR}
|
||||||
cat ${TEMPLATE_DIR}/sysunit.service \
|
cat ${TEMPLATE_DIR}/sysunit.service \
|
||||||
| sed "s~{{ PACKAGE_NAME }}~${PACKAGE_NAME}~" \
|
| sed "s~{{ PACKAGE_NAME }}~${PACKAGE_NAME}~" \
|
||||||
| sed "s~{{ DESCRIPTION }}~${PACKAGE_DESCRIPTION}~" \
|
| sed "s~{{ DESCRIPTION }}~${PACKAGE_DESCRIPTION}~" \
|
||||||
| sed "s~{{ PROGRAM }}~/${PACKAGE_PREFIX}/bin/${PACKAGE_NAME}~" \
|
| sed "s~{{ PROGRAM }}~/${PACKAGE_PREFIX}/bin/${PACKAGE_NAME}~" \
|
||||||
> ${PACKAGE_TMPDIR}/${PACKAGE_SYSUNITDIR}/${PACKAGE_NAME}.service
|
> ${PACKAGE_TMPDIR}/${PACKAGE_SYSUNITDIR}/${PACKAGE_NAME}.service
|
||||||
|
|
||||||
# Create rsyslog file
|
# Create rsyslog file
|
||||||
mkdir -p ${PACKAGE_TMPDIR}/${PACKAGE_RSYSLOGDIR}
|
mkdir -p ${PACKAGE_TMPDIR}/${PACKAGE_RSYSLOGDIR}
|
||||||
cat ${TEMPLATE_DIR}/rsyslog.conf \
|
cat ${TEMPLATE_DIR}/rsyslog.conf \
|
||||||
| sed "s~{{ PROGRAM }}~${PACKAGE_NAME}~" \
|
| sed "s~{{ PROGRAM }}~${PACKAGE_NAME}~" \
|
||||||
| sed "s~{{ LOG_FILE }}~${PACKAGE_LOGFILE}~" \
|
| sed "s~{{ LOG_FILE }}~${PACKAGE_LOGFILE}~" \
|
||||||
> ${PACKAGE_TMPDIR}/${PACKAGE_RSYSLOGDIR}/49-${PACKAGE_NAME}.conf
|
> ${PACKAGE_TMPDIR}/${PACKAGE_RSYSLOGDIR}/49-${PACKAGE_NAME}.conf
|
||||||
|
|
||||||
# Create logrotate file
|
# Create logrotate file
|
||||||
mkdir -p ${PACKAGE_TMPDIR}/${PACKAGE_LOGROTATEDIR}
|
mkdir -p ${PACKAGE_TMPDIR}/${PACKAGE_LOGROTATEDIR}
|
||||||
cat ${TEMPLATE_DIR}/logrotate.conf \
|
cat ${TEMPLATE_DIR}/logrotate.conf \
|
||||||
| sed "s~{{ LOG_FILE }}~${PACKAGE_LOGFILE}~" \
|
| sed "s~{{ LOG_FILE }}~${PACKAGE_LOGFILE}~" \
|
||||||
> ${PACKAGE_TMPDIR}/${PACKAGE_LOGROTATEDIR}/${PACKAGE_NAME}.conf
|
> ${PACKAGE_TMPDIR}/${PACKAGE_LOGROTATEDIR}/${PACKAGE_NAME}.conf
|
||||||
chmod 644 ${PACKAGE_TMPDIR}/${PACKAGE_LOGROTATEDIR}/${PACKAGE_NAME}.conf
|
chmod 644 ${PACKAGE_TMPDIR}/${PACKAGE_LOGROTATEDIR}/${PACKAGE_NAME}.conf
|
||||||
|
|
||||||
# Cerate config file
|
# Cerate config file
|
||||||
mkdir -p ${PACKAGE_TMPDIR}/${PACKAGE_ETCDIR}
|
mkdir -p ${PACKAGE_TMPDIR}/${PACKAGE_ETCDIR}
|
||||||
cat ${TEMPLATE_DIR}/config \
|
cat ${TEMPLATE_DIR}/config \
|
||||||
| sed "s~{{ PACKAGE_NAME }}~${PACKAGE_NAME}~" \
|
| sed "s~{{ PACKAGE_NAME }}~${PACKAGE_NAME}~" \
|
||||||
> ${PACKAGE_TMPDIR}/${PACKAGE_ETCDIR}/env
|
> ${PACKAGE_TMPDIR}/${PACKAGE_ETCDIR}/env
|
||||||
|
|
||||||
# Copy program
|
# Copy program
|
||||||
mkdir -p ${PACKAGE_TMPDIR}/${PACKAGE_BINDIR}
|
mkdir -p ${PACKAGE_TMPDIR}/${PACKAGE_BINDIR}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
if [ $# -lt 1 ]; then
|
if [ $# -lt 1 ]; then
|
||||||
echo "$0 <version>"
|
echo "$0 <version>"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
sed -i "s:PACKAGE_VERSION=\\\\\"\(.*\)\\\\\":PACKAGE_VERSION=\\\\\"$1\\\\\":g" Makefile
|
sed -i "s:PACKAGE_VERSION=\\\\\"\(.*\)\\\\\":PACKAGE_VERSION=\\\\\"$1\\\\\":g" Makefile
|
||||||
|
|
|
||||||
178
src/main.go
178
src/main.go
|
|
@ -2,12 +2,12 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
log "github.com/inconshreveable/log15"
|
log "github.com/inconshreveable/log15"
|
||||||
"github.com/eosswedenorg-go/pid"
|
"github.com/eosswedenorg-go/pid"
|
||||||
"github.com/pborman/getopt/v2"
|
"github.com/pborman/getopt/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Command line flags
|
// Command line flags
|
||||||
|
|
@ -29,43 +29,43 @@ var logger log.Logger
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
func argv_listen_addr() string {
|
func argv_listen_addr() string {
|
||||||
|
|
||||||
var addr string
|
var addr string
|
||||||
|
|
||||||
argv := getopt.Args()
|
argv := getopt.Args()
|
||||||
if len(argv) > 0 {
|
if len(argv) > 0 {
|
||||||
addr = argv[0]
|
addr = argv[0]
|
||||||
} else {
|
} else {
|
||||||
addr = "127.0.0.1"
|
addr = "127.0.0.1"
|
||||||
}
|
}
|
||||||
|
|
||||||
addr += ":"
|
addr += ":"
|
||||||
if len(argv) > 1 {
|
if len(argv) > 1 {
|
||||||
addr += argv[1]
|
addr += argv[1]
|
||||||
} else {
|
} else {
|
||||||
addr += "1337"
|
addr += "1337"
|
||||||
}
|
}
|
||||||
|
|
||||||
return addr
|
return addr
|
||||||
}
|
}
|
||||||
|
|
||||||
func setLogFile() {
|
func setLogFile() {
|
||||||
|
|
||||||
// Open file
|
// Open file
|
||||||
fd, err := os.OpenFile(logFile, os.O_APPEND | os.O_CREATE | os.O_WRONLY, 0644)
|
fd, err := os.OpenFile(logFile, os.O_APPEND | os.O_CREATE | os.O_WRONLY, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(err.Error())
|
logger.Error(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try close if old descriptor is defined.
|
// Try close if old descriptor is defined.
|
||||||
if logfd != nil {
|
if logfd != nil {
|
||||||
if err = logfd.Close(); err != nil {
|
if err = logfd.Close(); err != nil {
|
||||||
logger.Error(err.Error())
|
logger.Error(err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update variable and set log writer.
|
// Update variable and set log writer.
|
||||||
logfd = fd
|
logfd = fd
|
||||||
logger.SetHandler(log.StreamHandler(logfd, log.LogfmtFormat()))
|
logger.SetHandler(log.StreamHandler(logfd, log.LogfmtFormat()))
|
||||||
}
|
}
|
||||||
|
|
||||||
// signalEventLoop()
|
// signalEventLoop()
|
||||||
|
|
@ -74,88 +74,88 @@ func setLogFile() {
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
func signalEventLoop() {
|
func signalEventLoop() {
|
||||||
|
|
||||||
// Setup a channel
|
// Setup a channel
|
||||||
sig_ch := make(chan os.Signal, 1)
|
sig_ch := make(chan os.Signal, 1)
|
||||||
|
|
||||||
// subscribe to SIGHUP signal.
|
// subscribe to SIGHUP signal.
|
||||||
signal.Notify(sig_ch, syscall.SIGHUP)
|
signal.Notify(sig_ch, syscall.SIGHUP)
|
||||||
|
|
||||||
// Event loop (runs in a seperate thread)
|
// Event loop (runs in a seperate thread)
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
// Block until we get a signal.
|
// Block until we get a signal.
|
||||||
sig := <- sig_ch
|
sig := <- sig_ch
|
||||||
|
|
||||||
switch sig {
|
switch sig {
|
||||||
// SIGHUP is sent when logfile is rotated.
|
// SIGHUP is sent when logfile is rotated.
|
||||||
case syscall.SIGHUP :
|
case syscall.SIGHUP :
|
||||||
msg := "SIGHUP (Logfile was rotated): "
|
msg := "SIGHUP (Logfile was rotated): "
|
||||||
|
|
||||||
if logfd != nil {
|
if logfd != nil {
|
||||||
setLogFile()
|
setLogFile()
|
||||||
msg += "Filedescriptor was updated"
|
msg += "Filedescriptor was updated"
|
||||||
} else {
|
} else {
|
||||||
msg += "No Filedescriptor to update (most likely uses standard out/err streams)"
|
msg += "No Filedescriptor to update (most likely uses standard out/err streams)"
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Info(msg)
|
logger.Info(msg)
|
||||||
default:
|
default:
|
||||||
logger.Warn("Unknown signal", "signal", sig)
|
logger.Warn("Unknown signal", "signal", sig)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
// main
|
// main
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
var version bool
|
var version bool
|
||||||
var usage bool
|
var usage bool
|
||||||
var addr string;
|
var addr string;
|
||||||
|
|
||||||
logger = log.New()
|
logger = log.New()
|
||||||
|
|
||||||
// Command line parsing
|
// Command line parsing
|
||||||
getopt.SetParameters("[ip] [port]")
|
getopt.SetParameters("[ip] [port]")
|
||||||
getopt.FlagLong(&usage, "help", 'h', "Print this help text")
|
getopt.FlagLong(&usage, "help", 'h', "Print this help text")
|
||||||
getopt.FlagLong(&version, "version", 'v', "Print version")
|
getopt.FlagLong(&version, "version", 'v', "Print version")
|
||||||
getopt.FlagLong(&logFile, "log", 'l', "Path to log file", "file")
|
getopt.FlagLong(&logFile, "log", 'l', "Path to log file", "file")
|
||||||
getopt.FlagLong(&pidFile, "pid", 'p', "Path to pid file", "file")
|
getopt.FlagLong(&pidFile, "pid", 'p', "Path to pid file", "file")
|
||||||
getopt.Parse()
|
getopt.Parse()
|
||||||
|
|
||||||
if usage {
|
if usage {
|
||||||
getopt.Usage()
|
getopt.Usage()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if version {
|
if version {
|
||||||
print("Version: v1.1\n")
|
print("Version: v1.1\n")
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open logfile.
|
// Open logfile.
|
||||||
if len(logFile) > 0 {
|
if len(logFile) > 0 {
|
||||||
setLogFile()
|
setLogFile()
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Info("Process is starting", "pid", pid.Get())
|
logger.Info("Process is starting", "pid", pid.Get())
|
||||||
|
|
||||||
if len(pidFile) > 0 {
|
if len(pidFile) > 0 {
|
||||||
logger.Info("Writing pidfile", "file", pidFile)
|
logger.Info("Writing pidfile", "file", pidFile)
|
||||||
err := pid.Save(pidFile)
|
err := pid.Save(pidFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Failed to write pidfile", "msg", err)
|
logger.Error("Failed to write pidfile", "msg", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the signal event loop.
|
// Run the signal event loop.
|
||||||
signalEventLoop()
|
signalEventLoop()
|
||||||
|
|
||||||
addr = argv_listen_addr()
|
addr = argv_listen_addr()
|
||||||
|
|
||||||
logger.Info("TCP Server started", "addr", addr)
|
logger.Info("TCP Server started", "addr", addr)
|
||||||
|
|
||||||
// Start listening to TCP Connections
|
// Start listening to TCP Connections
|
||||||
spawnTcpServer(addr);
|
spawnTcpServer(addr);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
206
src/server.go
206
src/server.go
|
|
@ -2,12 +2,12 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
"strings"
|
"strings"
|
||||||
"strconv"
|
"strconv"
|
||||||
"internal/utils"
|
"internal/utils"
|
||||||
"github.com/eosswedenorg-go/eosapi"
|
"github.com/eosswedenorg-go/eosapi"
|
||||||
"github.com/eosswedenorg-go/haproxy"
|
"github.com/eosswedenorg-go/haproxy"
|
||||||
"github.com/eosswedenorg-go/tcp_server"
|
"github.com/eosswedenorg-go/tcp_server"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -15,30 +15,30 @@ import (
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
func check_api(p eosapi.ReqParams, block_time float64) (haproxy.HealthCheckStatus, string) {
|
func check_api(p eosapi.ReqParams, block_time float64) (haproxy.HealthCheckStatus, string) {
|
||||||
|
|
||||||
info, err := eosapi.GetInfo(p)
|
info, err := eosapi.GetInfo(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
msg := fmt.Sprintf("%s", err);
|
msg := fmt.Sprintf("%s", err);
|
||||||
return haproxy.HealthCheckFailed, msg
|
return haproxy.HealthCheckFailed, msg
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check HTTP Status Code
|
// Check HTTP Status Code
|
||||||
if info.HTTPStatusCode > 299 {
|
if info.HTTPStatusCode > 299 {
|
||||||
return haproxy.HealthCheckDown,
|
return haproxy.HealthCheckDown,
|
||||||
fmt.Sprintf("Taking offline because %v was received from backend", info.HTTPStatusCode)
|
fmt.Sprintf("Taking offline because %v was received from backend", info.HTTPStatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate head block.
|
// Validate head block.
|
||||||
now := time.Now().In(time.UTC)
|
now := time.Now().In(time.UTC)
|
||||||
diff := now.Sub(info.HeadBlockTime).Seconds()
|
diff := now.Sub(info.HeadBlockTime).Seconds()
|
||||||
|
|
||||||
if diff > block_time {
|
if diff > block_time {
|
||||||
return haproxy.HealthCheckDown,
|
return haproxy.HealthCheckDown,
|
||||||
fmt.Sprintf("Taking offline because head block is lagging %.0f seconds", diff)
|
fmt.Sprintf("Taking offline because head block is lagging %.0f seconds", diff)
|
||||||
} else if diff < -block_time {
|
} else if diff < -block_time {
|
||||||
return haproxy.HealthCheckDown,
|
return haproxy.HealthCheckDown,
|
||||||
fmt.Sprintf("Taking offline because head block is %.0f seconds into the future", diff)
|
fmt.Sprintf("Taking offline because head block is %.0f seconds into the future", diff)
|
||||||
}
|
}
|
||||||
return haproxy.HealthCheckUp, "OK"
|
return haproxy.HealthCheckUp, "OK"
|
||||||
}
|
}
|
||||||
|
|
||||||
// check_api_v2 (hyperion)
|
// check_api_v2 (hyperion)
|
||||||
|
|
@ -47,111 +47,111 @@ func check_api(p eosapi.ReqParams, block_time float64) (haproxy.HealthCheckStatu
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
func check_api_v2(p eosapi.ReqParams, offset int64) (haproxy.HealthCheckStatus, string) {
|
func check_api_v2(p eosapi.ReqParams, offset int64) (haproxy.HealthCheckStatus, string) {
|
||||||
|
|
||||||
health, err := eosapi.GetHealth(p)
|
health, err := eosapi.GetHealth(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
msg := fmt.Sprintf("%s", err);
|
msg := fmt.Sprintf("%s", err);
|
||||||
return haproxy.HealthCheckFailed, msg
|
return haproxy.HealthCheckFailed, msg
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check HTTP Status Code
|
// Check HTTP Status Code
|
||||||
if health.HTTPStatusCode > 299 {
|
if health.HTTPStatusCode > 299 {
|
||||||
return haproxy.HealthCheckDown,
|
return haproxy.HealthCheckDown,
|
||||||
fmt.Sprintf("Taking offline because %v was received from backend", health.HTTPStatusCode)
|
fmt.Sprintf("Taking offline because %v was received from backend", health.HTTPStatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch elasticsearch and nodeos block numbers from json.
|
// Fetch elasticsearch and nodeos block numbers from json.
|
||||||
var es_block int64 = 0
|
var es_block int64 = 0
|
||||||
var node_block int64 = 0
|
var node_block int64 = 0
|
||||||
|
|
||||||
for _, v := range health.Health {
|
for _, v := range health.Health {
|
||||||
if v.Name == "Elasticsearch" {
|
if v.Name == "Elasticsearch" {
|
||||||
es_block = utils.JsonGetInt64(v.Data["last_indexed_block"])
|
es_block = utils.JsonGetInt64(v.Data["last_indexed_block"])
|
||||||
} else if v.Name == "NodeosRPC" {
|
} else if v.Name == "NodeosRPC" {
|
||||||
node_block = utils.JsonGetInt64(v.Data["head_block_num"])
|
node_block = utils.JsonGetInt64(v.Data["head_block_num"])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error out if ether or both are zero.
|
// Error out if ether or both are zero.
|
||||||
if es_block == 0 || node_block == 0 {
|
if es_block == 0 || node_block == 0 {
|
||||||
msg := fmt.Sprintf("Failed to get Elasticsearch and/or nodeos " +
|
msg := fmt.Sprintf("Failed to get Elasticsearch and/or nodeos " +
|
||||||
"block numbers (es: %d, eos: %d)", es_block, node_block)
|
"block numbers (es: %d, eos: %d)", es_block, node_block)
|
||||||
return haproxy.HealthCheckFailed, msg
|
return haproxy.HealthCheckFailed, msg
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if ES is behind or in the future.
|
// Check if ES is behind or in the future.
|
||||||
diff := node_block - es_block;
|
diff := node_block - es_block;
|
||||||
if diff > offset {
|
if diff > offset {
|
||||||
return haproxy.HealthCheckDown,
|
return haproxy.HealthCheckDown,
|
||||||
fmt.Sprintf("Taking offline because Elastic is %d blocks behind", diff)
|
fmt.Sprintf("Taking offline because Elastic is %d blocks behind", diff)
|
||||||
} else if diff < -offset {
|
} else if diff < -offset {
|
||||||
return haproxy.HealthCheckDown,
|
return haproxy.HealthCheckDown,
|
||||||
fmt.Sprintf("Taking offline because Elastic is %d blocks into the future", -1 * diff)
|
fmt.Sprintf("Taking offline because Elastic is %d blocks into the future", -1 * diff)
|
||||||
}
|
}
|
||||||
return haproxy.HealthCheckUp, "OK"
|
return haproxy.HealthCheckUp, "OK"
|
||||||
}
|
}
|
||||||
|
|
||||||
// onTcpMessage callback function
|
// onTcpMessage callback function
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
func onTcpMessage(c *tcp_server.Client, args string) {
|
func onTcpMessage(c *tcp_server.Client, args string) {
|
||||||
params := eosapi.ReqParams{}
|
params := eosapi.ReqParams{}
|
||||||
var block_time int = 10
|
var block_time int = 10
|
||||||
var version string = "v1"
|
var version string = "v1"
|
||||||
|
|
||||||
// Parse arguments.
|
// Parse arguments.
|
||||||
// -------------------
|
// -------------------
|
||||||
split := strings.Split(strings.TrimSpace(args), "|")
|
split := strings.Split(strings.TrimSpace(args), "|")
|
||||||
|
|
||||||
// 1. url (scheme + ip/domain + port)
|
// 1. url (scheme + ip/domain + port)
|
||||||
params.Url = split[0]
|
params.Url = split[0]
|
||||||
|
|
||||||
// 2. Block time.
|
// 2. Block time.
|
||||||
if len(split) > 1 {
|
if len(split) > 1 {
|
||||||
p, err := strconv.ParseInt(split[1], 10, 32)
|
p, err := strconv.ParseInt(split[1], 10, 32)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
block_time = int(p)
|
block_time = int(p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Version
|
// 3. Version
|
||||||
if len(split) > 2 {
|
if len(split) > 2 {
|
||||||
version = split[2]
|
version = split[2]
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Host
|
// 4. Host
|
||||||
if len(split) > 3 {
|
if len(split) > 3 {
|
||||||
params.Host = split[3]
|
params.Host = split[3]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check api.
|
// Check api.
|
||||||
// -------------------
|
// -------------------
|
||||||
var status haproxy.HealthCheckStatus
|
var status haproxy.HealthCheckStatus
|
||||||
var msg string
|
var msg string
|
||||||
|
|
||||||
if version == "v2" {
|
if version == "v2" {
|
||||||
status, msg = check_api_v2(params, int64(block_time / 2))
|
status, msg = check_api_v2(params, int64(block_time / 2))
|
||||||
} else {
|
} else {
|
||||||
version = "v1"
|
version = "v1"
|
||||||
status, msg = check_api(params, float64(block_time))
|
status, msg = check_api(params, float64(block_time))
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Info("API Check", "version", version, "url", params.Url,
|
logger.Info("API Check", "version", version, "url", params.Url,
|
||||||
"block", block_time / 2, "status", status)
|
"block", block_time / 2, "status", status)
|
||||||
|
|
||||||
if status != haproxy.HealthCheckUp && len(msg) > 0 {
|
if status != haproxy.HealthCheckUp && len(msg) > 0 {
|
||||||
logger.Warn("API Check Failed", "message", msg)
|
logger.Warn("API Check Failed", "message", msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Report status to HAproxy
|
// Report status to HAproxy
|
||||||
c.WriteString(fmt.Sprintln(status))
|
c.WriteString(fmt.Sprintln(status))
|
||||||
c.Close()
|
c.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// spawnTcpServer
|
// spawnTcpServer
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
func spawnTcpServer(addr string) {
|
func spawnTcpServer(addr string) {
|
||||||
server := tcp_server.New(addr)
|
server := tcp_server.New(addr)
|
||||||
server.OnMessage(onTcpMessage)
|
server.OnMessage(onTcpMessage)
|
||||||
server.Listen()
|
server.Listen()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,9 +9,9 @@ package utils
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
func JsonGetInt64(input interface{}) (int64) {
|
func JsonGetInt64(input interface{}) (int64) {
|
||||||
v, res := input.(float64)
|
v, res := input.(float64)
|
||||||
if res {
|
if res {
|
||||||
return (int64) (v)
|
return (int64) (v)
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue