-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
148 lines (119 loc) · 3.56 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
package main
import (
"flag"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
"gitlab.com/NebulousLabs/Sia/build"
sia "gitlab.com/NebulousLabs/Sia/node/api/client"
)
var (
debug bool
module string
log *logrus.Logger
)
//initSiaClient sets the values of the client so we can communicate with the
//Sia Daemon.
func findPassword() (string, error) {
// Check environment variables
apiPassword := os.Getenv("SIA_API_PASSWORD")
if apiPassword != "" {
log.Info("Using SIA_API_PASSWORD environment variable")
return apiPassword, nil
}
// No password passed in, fetch the API Password
pw, err := build.APIPassword()
if err != nil {
log.Fatal("Exiting: Error getting API Password:", err)
}
return pw, err
}
// initLogger initializes the logger
func initLogger(debug bool) {
log = logrus.New()
// Define logger level
if debug {
log.SetLevel(logrus.DebugLevel)
// Print out file names and line numbers
log.SetReportCaller(true)
} else {
log.SetLevel(logrus.InfoLevel)
}
}
// boolToFloat64 converts a bool to a float64
func boolToFloat64(b bool) float64 {
if b {
return float64(1)
}
return float64(0)
}
// startMonitor refreshes the Sia metrics periodically as defined by refreshRate
func startMonitor(refreshRate time.Duration, sc *sia.Client) {
for range time.Tick(time.Minute * refreshRate) {
updateMetrics(sc)
}
}
// updateMetrics calls the various metric collection functions
func updateMetrics(sc *sia.Client) {
log.Debug("Updating metrics for modules:", module)
log.Debug("Updating Daemon Metrics")
daemonMetrics(sc)
if strings.Contains(module, "r") {
log.Debug("Updating Renter Metrics")
renterMetrics(sc)
log.Debug("Updating hostdb Metrics")
hostdbMetrics(sc)
}
if strings.Contains(module, "c") {
log.Debug("Updating Consensus Metrics")
consensusMetrics(sc)
}
if strings.Contains(module, "w") {
log.Debug("Updating Wallet Metrics")
walletMetrics(sc)
}
if strings.Contains(module, "g") {
log.Debug("Updating Gateway Metrics")
gatewayMetrics(sc)
}
if strings.Contains(module, "h") {
log.Debug("Updating Host Metrics")
hostMetrics(sc)
}
if strings.Contains(module, "m") {
log.Info("Miner metrics are not implemented yet")
}
if strings.Contains(module, "t") {
log.Info("Transactionpool metrics are not implemented yet")
}
}
func main() {
// Flags
flag.BoolVar(&debug, "debug", false, "Enable debug mode. Warning: generates a lot of output.")
address := flag.String("address", "127.0.0.1:9980", "Sia's API address")
agent := flag.String("agent", "Sia-Agent", "Sia agent")
refresh := flag.Int("refresh", 5, "Frequency to get Metrics from Sia (minutes)")
port := flag.Int("port", 9983, "Port to serve Prometheus Metrics on")
flag.StringVar(&module, "modules", "cghmrtw", "Sia Modules to monitor")
flag.Parse()
// Initialize the logger
initLogger(debug)
// Set the Sia Client connection information
sc := sia.New(sia.Options{Address: *address})
sc.UserAgent = *agent
sc.Password, _ = findPassword()
// Set the metrics initially before starting the monitor and HTTP server
// If you don't do this all the metrics start with a "0" until they are set
updateMetrics(sc)
// start the metrics collector
go startMonitor(time.Duration(*refresh), sc)
// This section will start the HTTP server and expose
// any metrics on the /metrics endpoint.
http.Handle("/metrics", promhttp.Handler())
log.Info("Beginning to metrics at http://<your ip address>:", *port, "/metrics")
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(*port), nil))
}