From e417a94a9f7f59d87226671f582c8910df01a207 Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Thu, 5 Dec 2019 22:05:51 -0500 Subject: [PATCH 1/2] Added getinfo Signed-off-by: Ben Wilson --- collector.go | 14 ++++++++++++++ main.go | 26 ++++++++++++++++++++++++++ rpc.go | 6 ++++++ 3 files changed, 46 insertions(+) diff --git a/collector.go b/collector.go index 9fae20f..9ecf7e9 100644 --- a/collector.go +++ b/collector.go @@ -6,6 +6,18 @@ import ( //Define the metrics we wish to expose var ( + zcashdBlockchainInfo = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "zcash_network", + Help: "Current network name as defined in BIP70"}, + []string{"network", "blocks"}, + ) + zcashdInfo = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "zcashd_info", + Help: "Node state info"}, + []string{"version"}, + ) zcashdBlocks = prometheus.NewGauge(prometheus.GaugeOpts{ Name: "zcash_blocks", Help: "the current number of blocks processed in the server"}) zcashdDifficulty = prometheus.NewGauge(prometheus.GaugeOpts{ @@ -78,6 +90,8 @@ var ( func init() { //Register metrics with prometheus + prometheus.MustRegister(zcashdBlockchainInfo) + prometheus.MustRegister(zcashdInfo) prometheus.MustRegister(zcashdBlocks) prometheus.MustRegister(zcashdDifficulty) prometheus.MustRegister(zcashdSizeOnDisk) diff --git a/main.go b/main.go index 07d1b0f..7b3611c 100644 --- a/main.go +++ b/main.go @@ -52,6 +52,7 @@ func main() { `)) }) + go getInfo() go getBlockchainInfo() go getMemPoolInfo() go getWalletInfo() @@ -63,6 +64,27 @@ func main() { } +func getInfo() { + basicAuth := base64.StdEncoding.EncodeToString([]byte(*rpcUser + ":" + *rpcPassword)) + rpcClient := jsonrpc.NewClientWithOpts("http://"+*rpcHost+":"+*rpcPort, + &jsonrpc.RPCClientOpts{ + CustomHeaders: map[string]string{ + "Authorization": "Basic " + basicAuth, + }}) + var info *GetInfo + + for { + if err := rpcClient.CallFor(&info, "getinfo"); err != nil { + log.Warnln("Error calling getinfo", err) + } else { + zcashdInfo.WithLabelValues( + strconv.Itoa(info.Version)).Set(1) + } + time.Sleep(time.Duration(30) * time.Second) + } + +} + func getBlockchainInfo() { basicAuth := base64.StdEncoding.EncodeToString([]byte(*rpcUser + ":" + *rpcPassword)) rpcClient := jsonrpc.NewClientWithOpts("http://"+*rpcHost+":"+*rpcPort, @@ -76,6 +98,10 @@ func getBlockchainInfo() { if err := rpcClient.CallFor(&blockinfo, "getblockchaininfo"); err != nil { log.Warnln("Error calling getblockchaininfo", err) } else { + + zcashdBlockchainInfo.WithLabelValues( + blockinfo.Chain, strconv.Itoa(blockinfo.Blocks)).Set(1) + zcashdBlocks.Set(float64(blockinfo.Blocks)) zcashdDifficulty.Set(blockinfo.Difficulty) zcashdVerificationProgress.Set(blockinfo.VerificationProgress) diff --git a/rpc.go b/rpc.go index 746012d..bfc12b1 100644 --- a/rpc.go +++ b/rpc.go @@ -10,6 +10,12 @@ type GetBlockchainInfo struct { SizeOnDisk float64 `json:"size_on_disk"` } +// GetInfo Returns an object containing various state info. +// https://zcash-rpc.github.io/getinfo.html +type GetInfo struct { + Version int `json:"version"` +} + // GetMemPoolInfo return the zcashd rpc `getmempoolinfo` // https://zcash-rpc.github.io/getmempoolinfo.html type GetMemPoolInfo struct { From 75a5ac2179370725cf49110c4a25f49237d479be Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Mon, 9 Dec 2019 09:57:07 -0500 Subject: [PATCH 2/2] Renamed zcash_network metric to better reflect the rpc method source --- collector.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/collector.go b/collector.go index 9ecf7e9..ef8e5af 100644 --- a/collector.go +++ b/collector.go @@ -8,8 +8,8 @@ import ( var ( zcashdBlockchainInfo = prometheus.NewGaugeVec( prometheus.GaugeOpts{ - Name: "zcash_network", - Help: "Current network name as defined in BIP70"}, + Name: "zcash_blockchain_info", + Help: "Information about the current state of the block chain"}, []string{"network", "blocks"}, ) zcashdInfo = prometheus.NewGaugeVec(