-
Notifications
You must be signed in to change notification settings - Fork 48
/
lndmon.go
executable file
·104 lines (91 loc) · 2.42 KB
/
lndmon.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
package lndmon
import (
"fmt"
"os"
"path/filepath"
"time"
flags "github.com/jessevdk/go-flags"
"github.com/lightninglabs/lndclient"
"github.com/lightninglabs/lndmon/collectors"
"github.com/lightningnetwork/lnd/lnrpc/verrpc"
"github.com/lightningnetwork/lnd/routing/route"
"github.com/lightningnetwork/lnd/signal"
)
// Main is the true entrypoint to lndmon.
func Main() {
// TODO: Prevent from running twice.
err := start()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func start() error {
if _, err := flags.Parse(&cfg); err != nil {
if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
return nil
}
return err
}
quit := make(chan struct{})
interceptor, err := signal.Intercept()
if err != nil {
return fmt.Errorf("could not intercept signal: %v", err)
}
programStartTime := time.Now()
// Initialize our lnd client, requiring at least lnd v0.11.
lnd, err := lndclient.NewLndServices(
&lndclient.LndServicesConfig{
LndAddress: cfg.Lnd.Host,
Network: lndclient.Network(cfg.Lnd.Network),
CustomMacaroonPath: filepath.Join(
cfg.Lnd.MacaroonDir, cfg.Lnd.MacaroonName,
),
RPCTimeout: cfg.Lnd.RPCTimeout,
TLSPath: cfg.Lnd.TLSPath,
CheckVersion: &verrpc.Version{
AppMajor: 0,
AppMinor: 13,
},
BlockUntilUnlocked: true,
},
)
if err != nil {
return err
}
defer lnd.Close()
monitoringCfg := collectors.MonitoringConfig{
DisableGraph: cfg.DisableGraph,
DisableHtlc: cfg.DisableHtlc,
}
if cfg.PrimaryNode != "" {
primaryNode, err := route.NewVertexFromStr(cfg.PrimaryNode)
if err != nil {
return err
}
monitoringCfg.PrimaryNode = &primaryNode
}
monitoringCfg.ProgramStartTime = programStartTime
// Start our Prometheus exporter. This exporter spawns a goroutine
// that pulls metrics from our lnd client on a set interval.
exporter := collectors.NewPrometheusExporter(
cfg.Prometheus, &lnd.LndServices, &monitoringCfg, quit,
)
if err := exporter.Start(); err != nil {
return err
}
// Wait to get the signal to shutdown, or for an error to occur with
// our metric export.
var stopErr error
select {
case <-interceptor.ShutdownChannel():
close(quit)
fmt.Println("Exiting lndmon.")
case stopErr = <-exporter.Errors():
fmt.Printf("Lndmon exiting with error: %v\n", stopErr)
}
// Before we exit, stop our prometheus exporter, then return the error
// we originally exited for (if any).
exporter.Stop()
return stopErr
}