From 1e43e01b9650b0746a4a53c6ffa0a2b55e7b63b0 Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Thu, 14 Nov 2019 13:13:52 -0500 Subject: [PATCH] Added a few metrics for getpeerinfo --- collector.go | 29 +++++++++++++++++++++ main.go | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++ rpc.go | 26 +++++++++++++++++++ 3 files changed, 127 insertions(+) diff --git a/collector.go b/collector.go index cd989c4..9fae20f 100644 --- a/collector.go +++ b/collector.go @@ -27,6 +27,31 @@ var ( []string{ "type", }) + // []string{"id", "addr", "addrlocal", "services", "lastsend", "lastrecv", "bytessent", "bytesrecv", "conntime", "timeoffset", "pingtime", "pingwait", "version", "subver", "inbound", "startingheight", "banscore", "synced_headers", "synced_blocks"}, + zcashdPeerVerion = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "zcash_peer_version", + Help: "Peer node version."}, + []string{"addr", "addrlocal", "inbound", "banscore", "subver"}, + ) + zcashdPeerConnTime = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "zcash_peer_conn_time", + Help: "Peer node connection time."}, + []string{"addr", "addrlocal", "inbound", "banscore", "subver"}, + ) + zcashdPeerBytesSent = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "zcash_peer_bytes_sent", + Help: "Bytes sent to peer node."}, + []string{"addr", "addrlocal", "inbound", "banscore", "subver"}, + ) + zcashdPeerBytesRecv = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "zcash_peer_bytes_recv", + Help: "Bytes received from peer node."}, + []string{"addr", "addrlocal", "inbound", "banscore", "subver"}, + ) ) // ZCASH_PEERS = Gauge("zcash_peers", "Number of peers") @@ -61,4 +86,8 @@ func init() { prometheus.MustRegister(zcashdMemPoolBytes) prometheus.MustRegister(zcashdMemPoolUsage) prometheus.MustRegister(zcashdWalletBalance) + prometheus.MustRegister(zcashdPeerVerion) + prometheus.MustRegister(zcashdPeerConnTime) + prometheus.MustRegister(zcashdPeerBytesSent) + prometheus.MustRegister(zcashdPeerBytesRecv) } diff --git a/main.go b/main.go index 25065cc..07d1b0f 100644 --- a/main.go +++ b/main.go @@ -55,6 +55,7 @@ func main() { go getBlockchainInfo() go getMemPoolInfo() go getWalletInfo() + go getPeerInfo() log.Infoln("Listening on", *listenAddress) if err := http.ListenAndServe(*listenAddress, nil); err != nil { log.Fatal(err) @@ -133,3 +134,74 @@ func getWalletInfo() { } } + +func getPeerInfo() { + basicAuth := base64.StdEncoding.EncodeToString([]byte(*rpcUser + ":" + *rpcPassword)) + rpcClient := jsonrpc.NewClientWithOpts("http://"+*rpcHost+":"+*rpcPort, + &jsonrpc.RPCClientOpts{ + CustomHeaders: map[string]string{ + "Authorization": "Basic " + basicAuth, + }}) + var peerinfo *GetPeerInfo + + for { + if err := rpcClient.CallFor(&peerinfo, "getpeerinfo"); err != nil { + log.Warnln("Error calling getchaintips", err) + } else { + for _, pi := range *peerinfo { + log.Infoln("Got peerinfo: ", pi.Addr) + zcashdPeerVerion.WithLabelValues( + pi.Addr, + pi.AddrLocal, + strconv.FormatBool(pi.Inbound), + strconv.Itoa(pi.Banscore), + pi.Subver, + ).Set(float64(pi.Version)) + zcashdPeerConnTime.WithLabelValues( + pi.Addr, + pi.AddrLocal, + strconv.FormatBool(pi.Inbound), + strconv.Itoa(pi.Banscore), + pi.Subver, + ).Set(float64(pi.Conntime)) + zcashdPeerBytesSent.WithLabelValues( + pi.Addr, + pi.AddrLocal, + strconv.FormatBool(pi.Inbound), + strconv.Itoa(pi.Banscore), + pi.Subver, + ).Set(float64(pi.BytesSent)) + zcashdPeerBytesRecv.WithLabelValues( + pi.Addr, + pi.AddrLocal, + strconv.FormatBool(pi.Inbound), + strconv.Itoa(pi.Banscore), + pi.Subver, + ).Set(float64(pi.BytesRecv)) + // zcashdPeerInfo.WithLabelValues( + // strconv.Itoa(pi.ID), + // pi.Addr, + // pi.AddrLocal, + // pi.Services, + // strconv.Itoa(pi.LastSend), + // strconv.Itoa(pi.LastRecv), + // strconv.Itoa(pi.BytesSent), + // strconv.Itoa(pi.BytesRecv), + // strconv.Itoa(pi.Conntime), + // strconv.Itoa(pi.Timeoffset), + // strconv.FormatFloat(pi.PingTime, 'f', 2, 64), + // strconv.Itoa(pi.PingWait), + // strconv.Itoa(pi.Version), + // pi.Subver, + // strconv.FormatBool(pi.Inbound), + // strconv.Itoa(pi.Startingheight), + // strconv.Itoa(pi.Banscore), + // strconv.Itoa(pi.SyncedHeaders), + // strconv.Itoa(pi.SyncedBlocks), + // ).Set(float64(pi.ID)) + } + } + time.Sleep(time.Duration(30) * time.Second) + } + +} diff --git a/rpc.go b/rpc.go index 4ea6496..746012d 100644 --- a/rpc.go +++ b/rpc.go @@ -25,3 +25,29 @@ type ZGetTotalBalance struct { Private string `json:"private"` Total string `json:"total"` } + +// GetPeerInfo Returns data about each connected network node +// https://zcash-rpc.github.io/getpeerinfo.html +type GetPeerInfo []PeerInfo + +type PeerInfo struct { + ID int `json:"id"` + Addr string `json:"addr"` + AddrLocal string `json:"addrlocal"` + Services string `json:"services"` + LastSend int `json:"lastsend"` + LastRecv int `json:"lastrecv"` + BytesSent int `json:"bytessent"` + BytesRecv int `json:"bytesrecv"` + Conntime int `json:"conntime"` + Timeoffset int `json:"timeoffset"` + PingTime float64 `json:"pingtime"` + PingWait int `json:"pingwait"` + Version int `json:"version"` + Subver string `json:"subver"` + Inbound bool `json:"inbound"` + Startingheight int `json:"startingheight"` + Banscore int `json:"banscore"` + SyncedHeaders int `json:"synced_headers"` + SyncedBlocks int `json:"synced_blocks"` +}