-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.go
More file actions
94 lines (80 loc) · 2.3 KB
/
Copy pathserver.go
File metadata and controls
94 lines (80 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
currentBlock = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "current_block",
Help: "Latest Block known by node.",
})
currentStatus = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "status",
Help: "Status: PEERING=1, SYNCING=2, READY=3, MINING=4, UNKNOWN=5",
})
)
var (
status_request = []byte(`{"jsonrpc": "2.0", "id":"documentation", "method": "getnodestate", "params": []}`)
)
var (
rcp_address = flag.String("rpc-address", "http://127.0.0.1:3032", "The address of RPC server.")
listen_port = flag.String("port", ":9090", "The address to listen for metrics server.")
)
func init() {
// Metrics have to be registered to be exposed:
prometheus.MustRegister(currentBlock)
prometheus.MustRegister(currentStatus)
}
type NodestateResult struct {
Status string `json:"status"`
LatestBlockHeight int `json:"latest_block_height"`
}
type RPCNodestateResponse struct {
Result NodestateResult `json:"result"`
}
func main() {
flag.Parse()
http.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
resp, err := http.Post(*rcp_address, "application/json", bytes.NewBuffer(status_request))
if err != nil {
log.Fatal("Error getting response. ", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal("Error reading response. ", err)
}
var rpc_result RPCNodestateResponse
err = json.Unmarshal(body, &rpc_result)
if err != nil {
log.Fatal("Unable to unmarshall")
}
switch rpc_result.Result.Status {
case "Peering":
currentStatus.Set(1)
case "Syncing":
currentStatus.Set(2)
case "Ready":
currentStatus.Set(3)
case "Mining":
currentStatus.Set(4)
default:
currentStatus.Set(0)
}
currentBlock.Set(float64(rpc_result.Result.LatestBlockHeight))
next := promhttp.HandlerFor(
prometheus.DefaultGatherer, promhttp.HandlerOpts{})
next.ServeHTTP(w, r)
})
fmt.Printf("Starting server at port %s\n", *listen_port)
if err := http.ListenAndServe(*listen_port, nil); err != nil {
log.Fatal(err)
}
}