-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
342 additions
and
86 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,45 @@ | ||
FROM alpine | ||
FROM golang:1.20-alpine AS builder | ||
|
||
WORKDIR /build | ||
RUN apk add --no-cache make git bash protoc | ||
|
||
COPY monitor . | ||
ADD . /gnfd-qa-test-monitor | ||
|
||
CMD ["./monitor"] | ||
ENV CGO_ENABLED=1 | ||
ENV GO111MODULE=on | ||
|
||
# For Private REPO | ||
ARG GH_TOKEN="" | ||
RUN go env -w GOPRIVATE="github.com/bnb-chain/*" | ||
RUN git config --global url."https://${GH_TOKEN}@github.com".insteadOf "https://github.com" | ||
|
||
RUN apk add --no-cache build-base libc-dev | ||
|
||
RUN cd /gnfd-qa-test-monitor \ | ||
&& go build -o build/monitor main.go | ||
|
||
# Pull greenfield into a second stage deploy alpine container | ||
FROM alpine:3.17 | ||
|
||
ARG USER=sp | ||
ARG USER_UID=1000 | ||
ARG USER_GID=1000 | ||
|
||
ENV PACKAGES libstdc++ ca-certificates bash curl | ||
ENV WORKDIR=/app | ||
|
||
RUN apk add --no-cache $PACKAGES \ | ||
&& rm -rf /var/cache/apk/* \ | ||
&& addgroup -g ${USER_GID} ${USER} \ | ||
&& adduser -u ${USER_UID} -G ${USER} --shell /sbin/nologin --no-create-home -D ${USER} \ | ||
&& addgroup ${USER} tty \ | ||
&& sed -i -e "s/bin\/sh/bin\/bash/" /etc/passwd | ||
|
||
RUN echo "[ ! -z \"\$TERM\" -a -r /etc/motd ] && cat /etc/motd" >> /etc/bash/bashrc | ||
|
||
WORKDIR ${WORKDIR} | ||
|
||
COPY --from=builder /gnfd-qa-test-monitor/build/* ${WORKDIR}/ | ||
RUN chown -R ${USER_UID}:${USER_GID} ${WORKDIR} | ||
USER ${USER_UID}:${USER_GID} | ||
|
||
ENTRYPOINT ["/app/monitor"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package abci | ||
|
||
import ( | ||
"fmt" | ||
"github.com/bnb-chain/gnfd-qa-test-monitor/utils" | ||
"github.com/tidwall/gjson" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
func LastBlockHeight(rpc string) (int64, error) { | ||
// https://gnfd-testnet-fullnode-tendermint-us.bnbchain.org/abci_info?last_block_height | ||
url := fmt.Sprintf("%v/abci_info?last_block_height", rpc) | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
return 0, err | ||
} | ||
defer utils.CloseBody(resp.Body) | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return 0, err | ||
} | ||
result := gjson.GetBytes(body, "result.response.last_block_height") | ||
return result.Int(), nil | ||
} | ||
|
||
func BsDBInfoBlockHeight(spHost string, height int64) (string, error) { | ||
// https://gnfd-testnet-sp1.bnbchain.org/?bsdb-info&block_height=11037600 | ||
url := fmt.Sprintf("https://%v/?bsdb-info&block_height=%v", spHost, height) | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer utils.CloseBody(resp.Body) | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(body), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package checks | ||
|
||
import ( | ||
"fmt" | ||
"github.com/bnb-chain/gnfd-qa-test-monitor/abci" | ||
"github.com/bnb-chain/gnfd-qa-test-monitor/utils" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
"github.com/tidwall/gjson" | ||
"strings" | ||
) | ||
|
||
type Code uint32 | ||
|
||
const ( | ||
OK Code = iota | ||
GetBlockHeightErr | ||
GetObjectTotalCountErr | ||
GetObjectSealCountErr | ||
CheckObjectTotalCountErr | ||
CheckObjectSealCountErr | ||
) | ||
|
||
const ( | ||
TestNetRpc = "https://gnfd-testnet-fullnode-tendermint-us.bnbchain.org:443" | ||
MainNetRpc = "https://greenfield-chain.bnbchain.org:443" | ||
) | ||
|
||
var ( | ||
MainNetSpHosts = []string{ | ||
"greenfield-sp.bnbchain.org", | ||
"greenfield-sp.defibit.io", | ||
"greenfield-sp.ninicoin.io", | ||
"greenfield-sp.nariox.org", | ||
"greenfield-sp.lumibot.org", | ||
"greenfield-sp.voltbot.io", | ||
"greenfield-sp.nodereal.io", | ||
} | ||
|
||
TestNetSpHosts = []string{ | ||
"gnfd-testnet-sp1.bnbchain.org", | ||
"gnfd-testnet-sp2.bnbchain.org", | ||
"gnfd-testnet-sp3.bnbchain.org", | ||
"gnfd-testnet-sp4.bnbchain.org", | ||
"gnfd-testnet-sp1.nodereal.io", | ||
"gnfd-testnet-sp2.nodereal.io", | ||
"gnfd-testnet-sp3.nodereal.io", | ||
} | ||
) | ||
|
||
type DbShard struct { | ||
checkEnv string | ||
checkRpc string | ||
checkSpHosts []string | ||
blockMetrics prometheus.Gauge | ||
spErrCodeMetrics []prometheus.Gauge | ||
} | ||
|
||
func NewCheckDbShard(checkEnv, checkRpc string, checkSpHosts []string) *DbShard { | ||
checkSpErrCodeMetrics := make([]prometheus.Gauge, len(checkSpHosts)) | ||
for i, spHost := range checkSpHosts { | ||
metricsSpHost := strings.Replace(spHost, "-", "_", -1) | ||
metricsSpHost = strings.Replace(metricsSpHost, ".", "_", -1) | ||
checkSpErrCodeMetrics[i] = promauto.NewGauge(prometheus.GaugeOpts{Name: fmt.Sprintf("%v_sp_db_shard_error_code_%v", checkEnv, metricsSpHost)}) | ||
} | ||
|
||
return &DbShard{ | ||
checkEnv: checkEnv, | ||
checkRpc: checkRpc, | ||
checkSpHosts: checkSpHosts, | ||
blockMetrics: promauto.NewGauge(prometheus.GaugeOpts{Name: fmt.Sprintf("%v_sp_db_shard_check_block_height", checkEnv)}), | ||
spErrCodeMetrics: checkSpErrCodeMetrics, | ||
} | ||
} | ||
|
||
func (s *DbShard) CheckDbShard() { | ||
lastChainHeight, err := abci.LastBlockHeight(s.checkRpc) | ||
if err != nil { | ||
s.blockMetrics.Set(float64(GetBlockHeightErr)) | ||
return | ||
} | ||
calcHeight := lastChainHeight / 3600 * 3600 | ||
s.blockMetrics.Set(float64(calcHeight)) | ||
|
||
objCountArr := make([][]gjson.Result, len(s.checkSpHosts)) | ||
sealObjCountArr := make([][]gjson.Result, len(s.checkSpHosts)) | ||
isErr := false | ||
for i, spHost := range s.checkSpHosts { | ||
objCount, sealCount, errCode := s.getSpDbData(spHost, calcHeight) | ||
if errCode != OK { | ||
s.spErrCodeMetrics[i].Set(float64(errCode)) | ||
isErr = true | ||
} | ||
objCountArr[i] = objCount | ||
sealObjCountArr[i] = sealCount | ||
} | ||
|
||
if isErr { | ||
return | ||
} | ||
|
||
spIndex, errCode := s.checkDbData(objCountArr, sealObjCountArr) | ||
if errCode != OK { | ||
s.spErrCodeMetrics[spIndex].Set(float64(errCode)) | ||
return | ||
} | ||
|
||
for _, metric := range s.spErrCodeMetrics { | ||
metric.Set(float64(OK)) | ||
} | ||
} | ||
|
||
func (s *DbShard) getSpDbData(spHost string, height int64) (objCount, objSealCount []gjson.Result, errCode Code) { | ||
xmlResult, err := abci.BsDBInfoBlockHeight(spHost, height) | ||
if err != nil { | ||
return nil, nil, GetBlockHeightErr | ||
} | ||
|
||
objectResString := utils.GetXmlPath(xmlResult, "GfSpGetBsDBInfoResponse/ObjectTotalCount") | ||
if objectResString == "" { | ||
fmt.Printf("ObjectTotalCount error, %v\n", fmt.Sprintf("https://%v/?bsdb-info&block_height=%v", spHost, height)) | ||
return nil, nil, GetObjectTotalCountErr | ||
} else { | ||
objectTotalCount := gjson.Parse(objectResString).Array() | ||
objCount = objectTotalCount | ||
} | ||
|
||
objectSealResString := utils.GetXmlPath(xmlResult, "GfSpGetBsDBInfoResponse/ObjectSealCount") | ||
if objectSealResString == "" { | ||
fmt.Printf("ObjectSealCount error, %v\n", fmt.Sprintf("https://%v/?bsdb-info&block_height=%v", spHost, height)) | ||
return nil, nil, GetObjectSealCountErr | ||
} else { | ||
ObjectSealCount := gjson.Parse(objectSealResString).Array() | ||
objSealCount = ObjectSealCount | ||
} | ||
|
||
return objCount, objSealCount, OK | ||
} | ||
|
||
func (s *DbShard) checkDbData(spObjCounts, spObjSealCounts [][]gjson.Result) (spIndex int, errCode Code) { | ||
for i := 0; i < 64; i++ { | ||
sumObject := int64(0) | ||
sumSp1 := int64(0) | ||
for _, objectCount := range spObjCounts { | ||
sumObject = sumObject + objectCount[i].Int() | ||
sumSp1++ | ||
} | ||
sumSealedObject := int64(0) | ||
sumSp2 := int64(0) | ||
for _, sealObjectCount := range spObjSealCounts { | ||
sumSealedObject = sumSealedObject + sealObjectCount[i].Int() | ||
sumSp2++ | ||
} | ||
|
||
objectAverage := sumObject / sumSp1 | ||
for spIndex, eachValue := range spObjCounts { | ||
if objectAverage != eachValue[i].Int() { | ||
return spIndex, CheckObjectTotalCountErr | ||
} | ||
} | ||
|
||
sealObjectAverage := sumSealedObject / sumSp2 | ||
for _, eachValue := range spObjSealCounts { | ||
if sealObjectAverage != eachValue[i].Int() { | ||
return spIndex, CheckObjectSealCountErr | ||
} | ||
} | ||
} | ||
|
||
return 0, OK | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.