From 34bb88bc5159b2e5572d65c44266aca061c2a294 Mon Sep 17 00:00:00 2001 From: yuki Date: Thu, 8 Aug 2024 20:01:08 +0800 Subject: [PATCH] update check sp db --- .github/workflows/build.yml | 54 -------------- abci/abci.go | 34 +++++++++ checks/CheckSpDbObjectNumsPeriod.go | 108 ++++++++++++++++++++++++++++ go.mod | 11 +++ go.sum | 43 +++++++++++ main.go | 24 ++++--- utils/httputils.go | 26 +++++++ 7 files changed, 236 insertions(+), 64 deletions(-) delete mode 100644 .github/workflows/build.yml create mode 100644 abci/abci.go create mode 100644 checks/CheckSpDbObjectNumsPeriod.go create mode 100644 utils/httputils.go diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 5774b7b..0000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,54 +0,0 @@ -name: Build Test - -on: - push: - branches: - - master - - develop - - pull_request: - branches: - - master - - develop - -jobs: - build-test: - strategy: - matrix: - go-version: [1.22.x] - os: [ubuntu-latest] - runs-on: ${{ matrix.os }} - env: - GOPRIVATE: github.com/bnb-chain - GH_ACCESS_TOKEN: ${{ secrets.GH_TOKEN }} - steps: - - name: Install Go - uses: actions/setup-go@v3 - with: - go-version: ${{ matrix.go-version }} - - - name: Checkout code - uses: actions/checkout@v3 - - - uses: actions/cache@v3 - with: - # In order: - # * Module download cache - # * Build cache (Linux) - # * Build cache (Mac) - # * Build cache (Windows) - path: | - ~/go/pkg/mod - ~/.cache/go-build - ~/Library/Caches/go-build - %LocalAppData%\go-build - key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} - restore-keys: | - ${{ runner.os }}-go- - - - name: Setup GitHub Token - run: git config --global url.https://$GH_ACCESS_TOKEN@github.com/.insteadOf https://github.com/ - - - name: Test Build - run: | - make build diff --git a/abci/abci.go b/abci/abci.go new file mode 100644 index 0000000..9e786ed --- /dev/null +++ b/abci/abci.go @@ -0,0 +1,34 @@ +package abci + +import ( + "fmt" + "github.com/bnb-chain/gnfd-qa-test-monitor/utils" + "github.com/tidwall/gjson" + "io" + "net/http" +) + +func LastBlockHeight() (int64, error) { + url := `https://gnfd-testnet-fullnode-tendermint-us.bnbchain.org/abci_info?last_block_height` + resp, err := http.Get(url) + if err != nil { + return 0, err + } + defer utils.CloseBody(resp.Body) + body, err := io.ReadAll(resp.Body) + 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) + //fmt.Println(url) + resp, err := http.Get(url) + if err != nil { + return "", err + } + defer utils.CloseBody(resp.Body) + body, err := io.ReadAll(resp.Body) + return string(body), nil +} diff --git a/checks/CheckSpDbObjectNumsPeriod.go b/checks/CheckSpDbObjectNumsPeriod.go new file mode 100644 index 0000000..1f8ec14 --- /dev/null +++ b/checks/CheckSpDbObjectNumsPeriod.go @@ -0,0 +1,108 @@ +package checks + +import ( + "fmt" + "github.com/bnb-chain/gnfd-qa-test-monitor/abci" + "github.com/bnb-chain/gnfd-qa-test-monitor/utils" + "github.com/tidwall/gjson" +) + +var SelfMainNetSpHost = []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", +} + +var SelfTestNetSpHost = []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 Code float64 + +const ( + OK Code = iota + GetObjectTotalCountErr + GetObjectSealCountErr + CheckObjectTotalCountErr + CheckObjectSealCountErr +) + +func CheckSpDbObjectNumsPeriod(spHostArray []string) (block int64, errCode Code) { + chainHeight, err := abci.LastBlockHeight() + if err != nil { + fmt.Println(err) + } + fmt.Printf("height: %d\n", chainHeight) + calcHeight := chainHeight / 3600 * 3600 + fmt.Printf("calcHeight: %d\n", calcHeight) + + resObjectCount := make(map[string][]gjson.Result) + resObjectSealCount := make(map[string][]gjson.Result) + + // get everyone sp object count + for _, spHost := range spHostArray { + xmlResult, err := abci.BsDBInfoBlockHeight(spHost, calcHeight) + if err != nil { + fmt.Println(err) + } + + objectResString := utils.GetXmlPath(xmlResult, "GfSpGetBsDBInfoResponse/ObjectTotalCount") + if objectResString == "" { + fmt.Printf("sp: %v, ObjectTotalCount error\n", spHost) + return calcHeight, GetObjectTotalCountErr + } else { + ObjectTotalCount := gjson.Parse(objectResString).Array() + resObjectCount[spHost] = ObjectTotalCount + } + + objectSealResString := utils.GetXmlPath(xmlResult, "GfSpGetBsDBInfoResponse/ObjectSealCount") + if objectSealResString == "" { + fmt.Printf("sp: %v, ObjectSealCount error\n", spHost) + return calcHeight, GetObjectSealCountErr + } else { + ObjectSealCount := gjson.Parse(objectSealResString).Array() + resObjectSealCount[spHost] = ObjectSealCount + } + } + + // check sp object count + for i := 0; i < 64; i++ { + sumObject := int64(0) + sumSp1 := int64(0) + for _, objectCount := range resObjectCount { + sumObject = sumObject + objectCount[i].Int() + sumSp1++ + } + sumSealedObject := int64(0) + sumSp2 := int64(0) + for _, sealObjectCount := range resObjectSealCount { + sumSealedObject = sumSealedObject + sealObjectCount[i].Int() + sumSp2++ + } + + objectAverage := sumObject / sumSp1 + sealObjectAverage := sumSealedObject / sumSp2 + for _, eachValue := range resObjectCount { + if objectAverage != eachValue[i].Int() { + return calcHeight, CheckObjectTotalCountErr + } + } + for _, eachValue := range resObjectSealCount { + if sealObjectAverage != eachValue[i].Int() { + return calcHeight, CheckObjectSealCountErr + } + } + } + + return calcHeight, OK +} diff --git a/go.mod b/go.mod index bf23d43..dbccb7a 100644 --- a/go.mod +++ b/go.mod @@ -5,13 +5,24 @@ go 1.21 require github.com/prometheus/client_golang v1.17.0 require ( + github.com/antchfx/xmlquery v1.3.17 + github.com/tidwall/gjson v1.14.2 + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.0 // indirect +) + +require ( + github.com/antchfx/xpath v1.2.4 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect github.com/prometheus/common v0.44.0 // indirect github.com/prometheus/procfs v0.11.1 // indirect + golang.org/x/net v0.10.0 // indirect golang.org/x/sys v0.11.0 // indirect + golang.org/x/text v0.9.0 // indirect google.golang.org/protobuf v1.31.0 // indirect ) diff --git a/go.sum b/go.sum index 0aec189..3862aef 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,15 @@ +github.com/antchfx/xmlquery v1.3.17 h1:d0qWjPp/D+vtRw7ivCwT5ApH/3CkQU8JOeo3245PpTk= +github.com/antchfx/xmlquery v1.3.17/go.mod h1:Afkq4JIeXut75taLSuI31ISJ/zeq+3jG7TunF7noreA= +github.com/antchfx/xpath v1.2.4 h1:dW1HB/JxKvGtJ9WyVGJ0sIoEcqftV3SqIstujI+B9XY= +github.com/antchfx/xpath v1.2.4/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= +github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= @@ -21,9 +27,46 @@ github.com/prometheus/common v0.44.0 h1:+5BrQJwiBB9xsMygAB3TNvpQKOwlkc25LbISbrdO github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO7x0VV9VvuY= github.com/prometheus/procfs v0.11.1 h1:xRC8Iq1yyca5ypa9n1EZnWZkt7dwcoRPQwX/5gwaUuI= github.com/prometheus/procfs v0.11.1/go.mod h1:eesXgaPo1q7lBpVMoMy0ZOFTth9hBn4W/y0/p/ScXhY= +github.com/tidwall/gjson v1.14.2 h1:6BBkirS0rAHjumnjHF6qgy5d2YAJ1TLIaFE2lzfOLqo= +github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M= +golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= diff --git a/main.go b/main.go index c6db72f..18f19e0 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,8 @@ package main import ( + "fmt" + "github.com/bnb-chain/gnfd-qa-test-monitor/checks" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -9,24 +11,26 @@ import ( ) func recordMetrics() { + checkBlock := promauto.NewGauge(prometheus.GaugeOpts{ + Name: "sp_db_object_nums_period_check_block_height", + }) + checkErrCode := promauto.NewGauge(prometheus.GaugeOpts{ + Name: "sp_db_object_nums_period_check_error_code", + }) + go func() { for { - opsProcessed.Inc() - time.Sleep(2 * time.Second) + block, result := checks.CheckSpDbObjectNumsPeriod(checks.SelfTestNetSpHost) + fmt.Printf("block: %v, result: %v \n", block, result) + checkBlock.Set(float64(block)) + checkErrCode.Set(float64(result)) + time.Sleep(time.Minute * 10) } }() } -var ( - opsProcessed = promauto.NewCounter(prometheus.CounterOpts{ - Name: "myapp_processed_ops_total", - Help: "The total number of processed events", - }) -) - func main() { recordMetrics() - http.Handle("/metrics", promhttp.Handler()) err := http.ListenAndServe(":24367", nil) if err != nil { diff --git a/utils/httputils.go b/utils/httputils.go new file mode 100644 index 0000000..3f2fbe9 --- /dev/null +++ b/utils/httputils.go @@ -0,0 +1,26 @@ +package utils + +import ( + "github.com/antchfx/xmlquery" + "io" + "strings" +) + +func GetXmlPath(res, path string) string { + doc, err := xmlquery.Parse(strings.NewReader(res)) + if err != nil { + panic(err) + } + result := "" + if n := doc.SelectElement(path); n != nil { + result = n.InnerText() + } + return result +} + +func CloseBody(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + panic(err) + } +}