This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.go
203 lines (178 loc) · 5.17 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
// Aerospike prometheus exporter
//
// Collects statistics for a single Aerospike node and makes it available as
// metrics for Prometheus.
//
// Statistics collected:
// aerospike_node_*: node wide statistics. e.g. memory usage, cluster state.
// aerospike_ns_*: per namespace. e.g. objects, migrations.
// aerospike_sets_*: statistics per set: objects, memory usage
// aerospike_latency_*: read/write/etc latency rates(!) (as asinfo -v "latency:" reports").
// aerospike_ops_*: read/write/etc ops per second
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
as "github.com/aerospike/aerospike-client-go"
"github.com/aerospike/aerospike-client-go/pkg/bcrypt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
namespace = "aerospike"
secondaryIndex = "sindex"
systemNode = "node"
systemNamespace = "ns"
systemLatency = "latency"
systemLatencyHist = "latency_hist" // total number of ops
systemOps = "ops"
systemSet = "set"
xdrDC = "xdr"
)
var (
version = "master"
showVersion = flag.Bool("version", false, "show version")
addr = flag.String("listen", ":9145", "listen address for prometheus. ENV variable EXPORTER_ADDRESS")
nodeAddr = flag.String("node", "127.0.0.1:3000", "aerospike node")
username = flag.String("username", "", "username. Leave empty for no authentication. ENV variable AS_USERNAME, if set, will override this.")
password = flag.String("password", "", "password. ENV variable AS_PASSWORD, if set, will override this.")
landingPage = `<html>
<head><title>Aerospike exporter</title></head>
<body>
<h1>Aerospike exporter</h1>
<p><a href="/metrics">Metrics</a></p>
</body>
</html>`
upDesc = prometheus.NewDesc(
namespace+"_"+systemNode+"_up",
"Is this node up",
nil,
nil,
)
)
func main() {
flag.Parse()
if len(flag.Args()) != 0 {
log.Fatal("usage error")
}
user := os.Getenv("AS_USERNAME")
if user != "" {
*username = user
}
pass := os.Getenv("AS_PASSWORD")
if pass != "" {
*password = pass
}
exporterAddr := os.Getenv("EXPORTER_ADDRESS")
if exporterAddr != "" {
*addr = exporterAddr
}
if *showVersion {
fmt.Printf("asprom %s\n", version)
os.Exit(0)
}
col := newAsCollector(*nodeAddr, *username, *password)
req := prometheus.NewRegistry()
req.MustRegister(col)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(landingPage))
})
http.Handle("/metrics", promhttp.HandlerFor(req, promhttp.HandlerOpts{ErrorLog: log.New(os.Stdout, "err: ", 0)}))
log.Printf("starting asprom. listening on %s\n", *addr)
log.Fatal(http.ListenAndServe(*addr, nil))
}
type collector interface {
collect(*as.Connection) ([]prometheus.Metric, error)
describe(ch chan<- *prometheus.Desc)
}
type asCollector struct {
nodeAddr string
username string
password string
totalScrapes prometheus.Counter
collectors []collector
}
func newAsCollector(nodeAddr, username, password string) *asCollector {
totalScrapes := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: systemNode,
Name: "scrapes_total",
Help: "Total number of times Aerospike was scraped for metrics.",
})
return &asCollector{
nodeAddr: nodeAddr,
username: username,
password: password,
totalScrapes: totalScrapes,
collectors: []collector{
newLatencyCollector(),
newNSCollector(),
newSetCollector(),
newSindexCollector(),
newStatsCollector(),
newXdrDCCollector(),
},
}
}
// Describe implements the prometheus.Collector interface.
func (asc *asCollector) Describe(ch chan<- *prometheus.Desc) {
asc.totalScrapes.Describe(ch)
ch <- upDesc
for _, c := range asc.collectors {
c.describe(ch)
}
}
// Collect implements the prometheus.Collector interface.
func (asc *asCollector) Collect(ch chan<- prometheus.Metric) {
asc.totalScrapes.Inc()
ch <- asc.totalScrapes
ms, err := asc.collect()
if err != nil {
log.Print(err)
ch <- prometheus.MustNewConstMetric(upDesc, prometheus.GaugeValue, 0.0)
return
}
ch <- prometheus.MustNewConstMetric(upDesc, prometheus.GaugeValue, 1.0)
for _, m := range ms {
ch <- m
}
}
func (asc *asCollector) collect() ([]prometheus.Metric, error) {
conn, err := as.NewConnection(asc.nodeAddr, 3*time.Second)
if err != nil {
return nil, err
}
defer conn.Close()
if asc.username != "" {
hp, err := hashPassword(asc.password)
if err != nil {
return nil, fmt.Errorf("hashPassword: %s", err)
}
if err := conn.Authenticate(asc.username, hp); err != nil {
return nil, fmt.Errorf("auth error: %s", err)
}
}
var metrics []prometheus.Metric
for _, c := range asc.collectors {
ms, err := c.collect(conn)
if err != nil {
return nil, err
}
metrics = append(metrics, ms...)
}
return metrics, nil
}
// take from github.com/aerospike/aerospike-client-go/admin_command.go
func hashPassword(password string) ([]byte, error) {
// Hashing the password with the cost of 10, with a static salt
const salt = "$2a$10$7EqJtq98hPqEX7fNZaFWoO"
hashedPassword, err := bcrypt.Hash(password, salt)
if err != nil {
return nil, err
}
return []byte(hashedPassword), nil
}