-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
610 lines (527 loc) · 17.4 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"math"
"net/http"
"runtime"
"sync"
"time"
promModel "github.com/prometheus/client_model/go"
promExpfmt "github.com/prometheus/common/expfmt"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
"github.com/sirupsen/logrus"
)
// Build information. Populated at build-time
var (
GitDate = "undefined"
GitCommit = "undefined"
GoVersion = runtime.Version()
)
var options = struct {
ServerAddress string
ServerTimeout time.Duration
BeaconnodeType string
BeaconnodeAddress string
ValidatorType string
ValidatorAddress string
Interval time.Duration
Partition string
Debug bool
}{}
type ClientType string
const (
PrysmBeaconnodeMetricsClientType ClientType = "prysm-beaconnode-metrics"
PrysmValidatorMetricsClientType ClientType = "prysm-validator-metrics"
NimbusBeaconnodeMetricsClientType ClientType = "nimbus-beaconnode-metrics"
)
type ClientEndpoint struct {
Type ClientType
Address string
}
type ServerResponse struct {
Status string `json:"status"`
Data interface{} `json:"data"`
}
var clientEndpoints = []ClientEndpoint{}
var httpClient *http.Client
var specVersion = int64(2)
var exporterVersion = ""
func main() {
flag.BoolVar(&options.Debug, "debug", false, "enable debugging")
flag.DurationVar(&options.Interval, "interval", time.Second*62, "interval of sending metrics to server")
flag.StringVar(&options.ServerAddress, "server.address", "", "address of server to push metrics to")
flag.DurationVar(&options.ServerTimeout, "server.timeout", time.Second*10, "timeout for sending data to the server")
flag.StringVar(&options.Partition, "system.partition", "/", "mountpoint of partition which will be tracked for usage, if empty-string the highest usage of any partition will be recorded")
flag.StringVar(&options.BeaconnodeType, "beaconnode.type", "prysm", "endpoint to scrape metrics from")
flag.StringVar(&options.BeaconnodeAddress, "beaconnode.address", "", "address of beaconnode-endpoint to scrape metrics from (eg: http://localhost:8080/metrics), disabled if empty string")
flag.StringVar(&options.ValidatorType, "validator.type", "prysm", "endpoint to scrape metrics from")
flag.StringVar(&options.ValidatorAddress, "validator.address", "", "address of validator-endpoint to scrape metrics from (eg: http://localhost:8081/metrics), disabled if emtpy string")
versionFlag := flag.Bool("version", false, "show version and exit")
flag.Parse()
if *versionFlag {
fmt.Printf("git-commit: %v\ngit-date: %v\ngo-version: %v\n", GitCommit, GitDate, GoVersion)
return
}
if options.Debug {
logrus.SetLevel(logrus.DebugLevel)
}
if options.ServerAddress == "" {
logrus.Fatal("Server address not provided.")
}
if options.BeaconnodeAddress != "" {
var clientType ClientType
switch options.BeaconnodeType {
case "prysm":
clientType = PrysmBeaconnodeMetricsClientType
case "nimbus":
clientType = NimbusBeaconnodeMetricsClientType
default:
logrus.Fatal("invalid beaconnode.type")
}
clientEndpoints = append(clientEndpoints, ClientEndpoint{
Type: clientType,
Address: options.BeaconnodeAddress,
})
}
if options.ValidatorAddress != "" {
var clientType ClientType
switch options.ValidatorType {
case "prysm":
clientType = PrysmValidatorMetricsClientType
default:
logrus.Fatal("invalid validator.type")
}
clientEndpoints = append(clientEndpoints, ClientEndpoint{
Type: clientType,
Address: options.ValidatorAddress,
})
}
if options.BeaconnodeAddress == "" && options.ValidatorAddress == "" {
logrus.Fatal("Neither beacon node nor validator address provided.")
}
exporterVersion = fmt.Sprintf("beaconcha.in@%v", GitCommit)
httpClient = &http.Client{
Timeout: options.ServerTimeout,
}
beaconchain := `
_ _ _
| | | | (_)
| |__ ___ __ _ ___ ___ _ __ ___| |__ __ _ _ _ __
| '_ \ / _ \/ _' |/ __/ _ \| '_ \ / __| '_ \ / _' | | | '_ \
| |_) | __/ (_| | (_| (_) | | | | (__| | | | (_| |_| | | | |
|_.__/ \___|\__,_|\___\___/|_| |_|\___|_| |_|\__,_(_)_|_| |_|
`
fmt.Println(beaconchain)
logrus.WithFields(logrus.Fields{
// "ServerAddress": options.ServerAddress, // may contain secrets, don't log
"ServerTimeout": options.ServerTimeout,
"BeaconnodeType": options.BeaconnodeType,
"BeaconnodeAddress": options.BeaconnodeAddress,
"ValidatorType": options.ValidatorType,
"ValidatorAddress": options.ValidatorAddress,
"Interval": options.Interval,
"Partition": options.Partition,
"Debug": options.Debug,
"Version": exporterVersion,
}).Infof("starting exporter")
collectDataLoop()
}
func collectDataLoop() {
t := time.NewTicker(options.Interval)
defer t.Stop()
for {
t0 := time.Now()
d, err := collectData()
if err != nil {
logrus.WithError(err).Error("failed collecting data")
time.Sleep(time.Second * 10)
t.Reset(options.Interval)
continue
}
logrus.WithFields(logrus.Fields{"duration": time.Since(t0)}).Info("collected data")
t1 := time.Now()
err = sendData(d)
if err != nil {
logrus.WithError(err).Error("failed sending data")
time.Sleep(time.Second * 10)
t.Reset(options.Interval)
continue
}
logrus.WithFields(logrus.Fields{"duration": time.Since(t1)}).Info("sent data")
select {
case <-t.C:
}
}
}
func collectData() ([]interface{}, error) {
var wg sync.WaitGroup
results := make(chan interface{}, len(clientEndpoints)+1)
ts := uint64(time.Now().UnixNano() / int64(time.Millisecond))
wg.Add(1)
go func() {
defer wg.Done()
d, err := getSystemData(ts)
if err != nil {
logrus.WithFields(logrus.Fields{"error": err, "type": "system"}).Errorf("failed getting data")
return
}
results <- d
}()
for _, c := range clientEndpoints {
wg.Add(1)
go func(c ClientEndpoint) {
defer wg.Done()
switch c.Type {
case PrysmBeaconnodeMetricsClientType:
d, err := getPrysmBeaconnodeData(c.Address, ts)
if err != nil {
logrus.WithFields(logrus.Fields{"error": err, "address": c.Address, "type": c.Type}).Errorf("failed getting data")
return
}
results <- d
case PrysmValidatorMetricsClientType:
d, err := getPrysmValidatorData(c.Address, ts)
if err != nil {
logrus.WithFields(logrus.Fields{"error": err, "address": c.Address, "type": c.Type}).Errorf("failed getting data")
return
}
results <- d
case NimbusBeaconnodeMetricsClientType:
d, err := getNimbusBeaconnodeData(c.Address, ts)
if err != nil {
logrus.WithFields(logrus.Fields{"error": err, "address": c.Address, "type": c.Type}).Errorf("failed getting data")
return
}
results <- d
default:
logrus.Fatalf("unknown client-endpoint-type: %v", c.Type)
}
return
}(c)
}
go func() {
wg.Wait()
close(results)
}()
result := []interface{}{}
for r := range results {
result = append(result, r)
}
return result, nil
}
func sendData(data []interface{}) error {
dataJSON, err := json.Marshal(data)
if err != nil {
err = fmt.Errorf("failed marshaling data: %w", err)
return err
}
logrus.WithFields(logrus.Fields{"json": fmt.Sprintf("%s", dataJSON)}).Debug("sending data")
req, err := http.NewRequest("POST", options.ServerAddress, bytes.NewBuffer(dataJSON))
if err != nil {
err = fmt.Errorf("failed creating request: %w", err)
return err
}
req.Header.Set("Content-Type", "application/json")
res, err := httpClient.Do(req)
if err != nil {
err = fmt.Errorf("failed sending request: %w", err)
return err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
if res.StatusCode != http.StatusOK || len(body) != 0 {
err = fmt.Errorf("got error-response from server: %s", body)
return err
}
return nil
}
func getSystemData(ts uint64) (*SystemData, error) {
systemData := &SystemData{}
systemData.Version = specVersion
systemData.Timestamp = ts
systemData.ExporterVersion = exporterVersion
systemData.Process = "system"
cpuThreads, err := cpu.Counts(true)
if err != nil {
return nil, fmt.Errorf("failed getting cpu_threads: %w", err)
}
systemData.CPUThreads = int64(cpuThreads)
cpuCores, err := cpu.Counts(false)
if err != nil {
return nil, fmt.Errorf("failed getting cpu_cores: %w", err)
}
systemData.CPUCores = int64(cpuCores)
cpuTimes, err := cpu.Times(false)
if err != nil {
return nil, fmt.Errorf("failed getting cpu times: %w", err)
}
for _, t := range cpuTimes {
systemData.CPUNodeIdleSecondsTotal += uint64(t.Idle)
systemData.CPUNodeUserSecondsTotal += uint64(t.User)
systemData.CPUNodeIOWaitSecondsTotal += uint64(t.Iowait)
// note: currently beaconcha.in expects this to be everything
systemData.CPUNodeSystemSecondsTotal += uint64(t.System) + uint64(t.Iowait) + uint64(t.User) + uint64(t.Idle)
}
memStat, err := mem.VirtualMemory()
if err != nil {
return nil, fmt.Errorf("failed getting memory stats: %w", err)
}
systemData.MemoryNodeBytesTotal = memStat.Total
systemData.MemoryNodeBytesFree = memStat.Free
systemData.MemoryNodeBytesCached = memStat.Cached
systemData.MemoryNodeBytesBuffers = memStat.Buffers
if options.Partition != "" {
stat, err := disk.Usage(options.Partition)
if err != nil {
return nil, fmt.Errorf("failed getting disk partition stats for mountpoint: %s: %w", options.Partition, err)
}
systemData.DiskNodeBytesTotal += stat.Total
systemData.DiskNodeBytesFree += stat.Free
} else {
parts, err := disk.Partitions(true)
if err != nil {
return nil, fmt.Errorf("failed getting disk partitions: %w", err)
}
var mostUsedPartStat *disk.UsageStat
for _, p := range parts {
stat, err := disk.Usage(p.Mountpoint)
if err != nil {
logrus.WithFields(logrus.Fields{"error": err, "mountpoint": p.Mountpoint}).Error("failed getting disk partition stats")
} else {
if mostUsedPartStat == nil || stat.UsedPercent > mostUsedPartStat.UsedPercent {
mostUsedPartStat = stat
systemData.DiskNodeBytesTotal += stat.Total
systemData.DiskNodeBytesFree += stat.Free
}
}
}
logrus.WithFields(logrus.Fields{"path": mostUsedPartStat.Path, "usedPercent": mostUsedPartStat.UsedPercent, "totalBytes": mostUsedPartStat.Total, "freeBytes": mostUsedPartStat.Free}).Infof("highest disk usage: %2.f%%", mostUsedPartStat.UsedPercent)
}
ioCounters, err := disk.IOCounters()
if err != nil {
return nil, fmt.Errorf("failed getting disk io counterss: %w", err)
}
for _, c := range ioCounters {
_ = c
systemData.DiskNodeIOSeconds = c.IoTime
systemData.DiskNodeReadsTotal = c.ReadCount // c.MergedReadCount ?
systemData.DiskNodeWritesTotal = c.WriteCount // c.MergedWriteCount ?
}
netCounters, err := net.IOCounters(false)
if err != nil {
return nil, fmt.Errorf("failed getting net io counters: %w", err)
}
if len(netCounters) == 0 {
return nil, fmt.Errorf("no net.IOCounters")
}
systemData.NetworkNodeBytesTotalReceive = netCounters[0].BytesRecv
systemData.NetworkNodeBytesTotalTransmit = netCounters[0].BytesSent
bootTime, err := host.BootTime()
if err != nil {
return nil, fmt.Errorf("failed getting boot time: %w", err)
}
systemData.MiscNodeBootTSSeconds = bootTime
systemData.MiscOS = runtime.GOOS
if len(systemData.MiscOS) > 3 {
systemData.MiscOS = systemData.MiscOS[:3]
}
return systemData, nil
}
func getMetricValue(pb *promModel.Metric) float64 {
if pb.Gauge != nil {
return pb.Gauge.GetValue()
}
if pb.Counter != nil {
return pb.Counter.GetValue()
}
if pb.Untyped != nil {
return pb.Untyped.GetValue()
}
return math.NaN()
}
func getMetricValueFromFamilyMap(m map[string]*promModel.MetricFamily, name string) float64 {
metricFamily, exists := m[name]
if exists {
m := metricFamily.GetMetric()
if len(m) > 0 {
return getMetricValue(m[0])
}
}
return 0
}
func getMetrics(endpoint string) (map[string]*promModel.MetricFamily, error) {
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return nil, err
}
req.Header.Set("Cache-control", "no-cache")
res, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
var parser promExpfmt.TextParser
metricFamilies, err := parser.TextToMetricFamilies(res.Body)
if err != nil {
return nil, err
}
return metricFamilies, nil
}
func getPrysmBeaconnodeData(endpoint string, ts uint64) (*BeaconnodeData, error) {
metrics, err := getMetrics(endpoint)
if err != nil {
return nil, err
}
data := &BeaconnodeData{}
// CommonData
data.Version = specVersion
data.Timestamp = ts
data.ExporterVersion = exporterVersion
data.Process = "beaconnode"
// ProcessData
data.CPUProcessSecondsTotal = uint64(getMetricValueFromFamilyMap(metrics, "process_cpu_seconds_total"))
data.MemoryProcessBytes = uint64(getMetricValueFromFamilyMap(metrics, "process_resident_memory_bytes"))
data.ClientName = "prysm"
prysmVersionMetric, exists := metrics["prysm_version"]
if exists {
ms := prysmVersionMetric.GetMetric()
if len(ms) > 0 {
ls := ms[0].GetLabel()
for _, l := range ls {
if l.Name != nil && l.Value != nil && *l.Name == "version" {
data.ClientVersion = *l.Value
break
}
}
}
}
data.ClientBuild = 0
data.SyncEth2FallbackConfigured = false
data.SyncEth2FallbackConnected = false
// BeaconnodeData
data.DiskBeaconchainBytesTotal = uint64(getMetricValueFromFamilyMap(metrics, "bcnode_disk_beaconchain_bytes_total"))
p2pMessageReceivedTotalMetric, exists := metrics["p2p_message_received_total"]
if exists {
ms := p2pMessageReceivedTotalMetric.GetMetric()
total := uint64(0)
for _, m := range ms {
total += uint64(getMetricValue(m))
}
data.NetworkLibP2PBytesTotalReceive = total
}
data.NetworkLibP2PBytesTotalTransmit = 0
p2pPeerCount, exists := metrics["p2p_peer_count"]
if exists {
ms := p2pPeerCount.GetMetric()
for _, m := range ms {
ls := m.GetLabel()
for _, l := range ls {
if l.Name != nil && l.Value != nil && *l.Name == "State" && *l.Value == "Connected" {
data.ClientVersion = *l.Value
data.NetworkPeersConnected = uint64(getMetricValue(m))
break
}
}
}
}
data.SyncEth1Connected = true // todo
data.SyncEth2Synced = true // todo
data.SyncBeaconHeadSlot = uint64(getMetricValueFromFamilyMap(metrics, "beacon_head_slot"))
data.SyncEth1FallbackConfigured = false
data.SyncEth1FallbackConnected = false
data.SlasherActive = false
return data, nil
}
func getPrysmValidatorData(endpoint string, ts uint64) (*ValidatorData, error) {
metrics, err := getMetrics(endpoint)
if err != nil {
return nil, err
}
data := &ValidatorData{}
// CommonData
data.Version = specVersion
data.Timestamp = ts
data.ExporterVersion = exporterVersion
data.Process = "validator"
// ProcessData
data.CPUProcessSecondsTotal = uint64(getMetricValueFromFamilyMap(metrics, "process_cpu_seconds_total"))
data.MemoryProcessBytes = uint64(getMetricValueFromFamilyMap(metrics, "process_resident_memory_bytes"))
data.ClientName = "prysm"
prysmVersionMetric, exists := metrics["prysm_version"]
if exists {
ms := prysmVersionMetric.GetMetric()
if len(ms) > 0 {
ls := ms[0].GetLabel()
for _, l := range ls {
if l.Name != nil && l.Value != nil && *l.Name == "version" {
data.ClientVersion = *l.Value
break
}
}
}
}
data.ClientBuild = 0
data.SyncEth2FallbackConfigured = false
data.SyncEth2FallbackConnected = false
// ValidatorData
validatorStatuses, exists := metrics["validator_statuses"]
if exists {
ms := validatorStatuses.GetMetric()
for _, m := range ms {
data.ValidatorTotal++
if getMetricValue(m) == 3 {
data.ValidatorActive++
}
}
}
return data, nil
}
func getNimbusBeaconnodeData(endpoint string, ts uint64) (*BeaconnodeData, error) {
metrics, err := getMetrics(endpoint)
if err != nil {
return nil, err
}
data := &BeaconnodeData{}
// CommonData
data.Version = specVersion
data.Timestamp = ts
data.ExporterVersion = exporterVersion
data.Process = "beaconnode"
// ProcessData
data.CPUProcessSecondsTotal = uint64(getMetricValueFromFamilyMap(metrics, "process_cpu_seconds_total"))
data.MemoryProcessBytes = uint64(getMetricValueFromFamilyMap(metrics, "process_resident_memory_bytes"))
data.ClientName = "nimbus"
versionMetric, exists := metrics["version"]
if exists {
ms := versionMetric.GetMetric()
if len(ms) > 0 {
ls := ms[0].GetLabel()
for _, l := range ls {
if l.Name != nil && l.Value != nil && *l.Name == "version" {
data.ClientVersion = *l.Value
break
}
}
}
}
// BeaconnodeData
data.ClientBuild = 0
data.SyncEth2FallbackConfigured = false
data.SyncEth2FallbackConnected = false
data.NetworkPeersConnected = uint64(getMetricValueFromFamilyMap(metrics, "nbc_peers"))
data.SyncBeaconHeadSlot = uint64(getMetricValueFromFamilyMap(metrics, "beacon_head_slot"))
data.SyncEth1Connected = true // todo
data.SyncEth2Synced = true // todo
return data, nil
}