-
Notifications
You must be signed in to change notification settings - Fork 56
/
main.go
215 lines (189 loc) · 6.27 KB
/
main.go
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"log"
"math/big"
"net/http"
"os"
"strconv"
"strings"
"time"
)
var (
eth *ethclient.Client
geth *GethInfo
delay int
watchingAddresses string
addresses map[string]Address
)
func init() {
geth = new(GethInfo)
addresses = make(map[string]Address)
geth.TotalEth = big.NewInt(0)
}
type GethInfo struct {
GethServer string
ContractsCreated int64
TokenTransfers int64
ContractCalls int64
EthTransfers int64
BlockSize float64
LoadTime float64
TotalEth *big.Int
CurrentBlock *types.Block
Sync *ethereum.SyncProgress
LastBlockUpdate time.Time
SugGasPrice *big.Int
PendingTx uint
NetworkId *big.Int
}
type Address struct {
Balance *big.Int
Address string
Nonce uint64
}
func main() {
var err error
defer eth.Close()
geth.GethServer = os.Getenv("GETH")
watchingAddresses = os.Getenv("ADDRESSES")
delay, _ = strconv.Atoi(os.Getenv("DELAY"))
if delay == 0 {
delay = 500
}
log.Printf("Connecting to Ethereum node: %v\n", geth.GethServer)
eth, err = ethclient.Dial(geth.GethServer)
if err != nil {
panic(err)
}
geth.CurrentBlock, err = eth.BlockByNumber(context.TODO(), nil)
if err != nil {
panic(err)
}
go Routine()
log.Printf("Geth Exporter running on http://localhost:9090/metrics\n")
http.HandleFunc("/metrics", MetricsHttp)
err = http.ListenAndServe(":9090", nil)
if err != nil {
panic(err)
}
}
func CalculateTotals(block *types.Block) {
geth.TotalEth = big.NewInt(0)
geth.ContractsCreated = 0
geth.TokenTransfers = 0
geth.EthTransfers = 0
for _, b := range block.Transactions() {
if b.To() == nil {
geth.ContractsCreated++
}
if len(b.Data()) >= 4 {
method := hexutil.Encode(b.Data()[:4])
if method == "0xa9059cbb" {
geth.TokenTransfers++
}
}
if b.Value().Sign() == 1 {
geth.EthTransfers++
}
geth.TotalEth.Add(geth.TotalEth, b.Value())
}
size := strings.Split(geth.CurrentBlock.Size().String(), " ")
geth.BlockSize = stringToFloat(size[0]) * 1000
}
func Routine() {
var lastBlock *types.Block
ctx := context.Background()
for {
t1 := time.Now()
var err error
geth.CurrentBlock, err = eth.BlockByNumber(ctx, nil)
if err != nil {
log.Printf("issue with reponse from geth server: %v\n", geth.CurrentBlock)
time.Sleep(time.Duration(delay) * time.Millisecond)
continue
}
geth.SugGasPrice, _ = eth.SuggestGasPrice(ctx)
geth.PendingTx, _ = eth.PendingTransactionCount(ctx)
geth.NetworkId, _ = eth.NetworkID(ctx)
geth.Sync, _ = eth.SyncProgress(ctx)
if lastBlock == nil || geth.CurrentBlock.NumberU64() > lastBlock.NumberU64() {
log.Printf("Received block #%v with %v transactions (%v)\n", geth.CurrentBlock.NumberU64(), len(geth.CurrentBlock.Transactions()), geth.CurrentBlock.Hash().String())
geth.LastBlockUpdate = time.Now()
geth.LoadTime = time.Now().Sub(t1).Seconds()
}
if watchingAddresses != "" {
for _, a := range strings.Split(watchingAddresses, ",") {
addr := common.HexToAddress(a)
balance, _ := eth.BalanceAt(ctx, addr, geth.CurrentBlock.Number())
nonce, _ := eth.NonceAt(ctx, addr, geth.CurrentBlock.Number())
address := Address{
Address: addr.String(),
Balance: balance,
Nonce: nonce,
}
addresses[a] = address
}
}
lastBlock = geth.CurrentBlock
time.Sleep(time.Duration(delay) * time.Millisecond)
}
}
//
// HTTP response handler for /metrics
func MetricsHttp(w http.ResponseWriter, r *http.Request) {
var allOut []string
block := geth.CurrentBlock
if block == nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("issue receiving block from URL: %v", geth.GethServer)))
return
}
CalculateTotals(block)
allOut = append(allOut, fmt.Sprintf("geth_block %v", block.NumberU64()))
allOut = append(allOut, fmt.Sprintf("geth_seconds_last_block %0.2f", time.Now().Sub(geth.LastBlockUpdate).Seconds()))
allOut = append(allOut, fmt.Sprintf("geth_block_transactions %v", len(block.Transactions())))
allOut = append(allOut, fmt.Sprintf("geth_block_value %v", ToEther(geth.TotalEth)))
allOut = append(allOut, fmt.Sprintf("geth_block_gas_used %v", block.GasUsed()))
allOut = append(allOut, fmt.Sprintf("geth_block_gas_limit %v", block.GasLimit()))
allOut = append(allOut, fmt.Sprintf("geth_block_nonce %v", block.Nonce()))
allOut = append(allOut, fmt.Sprintf("geth_block_difficulty %v", block.Difficulty()))
allOut = append(allOut, fmt.Sprintf("geth_block_uncles %v", len(block.Uncles())))
allOut = append(allOut, fmt.Sprintf("geth_block_size_bytes %v", geth.BlockSize))
allOut = append(allOut, fmt.Sprintf("geth_gas_price %v", geth.SugGasPrice))
allOut = append(allOut, fmt.Sprintf("geth_pending_transactions %v", geth.PendingTx))
allOut = append(allOut, fmt.Sprintf("geth_network_id %v", geth.NetworkId))
allOut = append(allOut, fmt.Sprintf("geth_contracts_created %v", geth.ContractsCreated))
allOut = append(allOut, fmt.Sprintf("geth_token_transfers %v", geth.TokenTransfers))
allOut = append(allOut, fmt.Sprintf("geth_eth_transfers %v", geth.EthTransfers))
allOut = append(allOut, fmt.Sprintf("geth_load_time %0.4f", geth.LoadTime))
if geth.Sync != nil {
allOut = append(allOut, fmt.Sprintf("geth_known_states %v", int(geth.Sync.KnownStates)))
allOut = append(allOut, fmt.Sprintf("geth_highest_block %v", int(geth.Sync.HighestBlock)))
allOut = append(allOut, fmt.Sprintf("geth_pulled_states %v", int(geth.Sync.PulledStates)))
}
for _, v := range addresses {
allOut = append(allOut, fmt.Sprintf("geth_address_balance{address=\"%v\"} %v", v.Address, ToEther(v.Balance).String()))
allOut = append(allOut, fmt.Sprintf("geth_address_nonce{address=\"%v\"} %v", v.Address, v.Nonce))
}
w.Write([]byte(strings.Join(allOut, "\n")))
}
// stringToFloat will simply convert a string to a float
func stringToFloat(s string) float64 {
amount, _ := strconv.ParseFloat(s, 10)
return amount
}
//
// CONVERTS WEI TO ETH
func ToEther(o *big.Int) *big.Float {
pul, int := big.NewFloat(0), big.NewFloat(0)
int.SetInt(o)
pul.Mul(big.NewFloat(0.000000000000000001), int)
return pul
}