-
Notifications
You must be signed in to change notification settings - Fork 3
/
exporter.go
251 lines (219 loc) · 6.47 KB
/
exporter.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
package collector
import (
"bytes"
"database/sql"
"fmt"
"strconv"
"sync"
"time"
_ "github.com/SAP/go-hdb/driver"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
)
// Metric name parts.
const (
// Subsystem(s).
exporter = "exporter"
)
// SQL Queries.
const (
upQuery = `select SYSTEM_ID AS SID, DATABASE_NAME AS DB_NAME,Version from "SYS"."M_DATABASE";`
)
// Metric descriptors.
var (
HanaInfoLabelNames = []string{"sid", "db_name", "db_version"}
HanaInfoLabelValues = make([]string, 3, 3)
scrapeDurationDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, exporter, "collector_duration_seconds"),
"Collector time duration.",
[]string{"collector"}, nil,
)
hanaUpDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "up"),
"Collector time duration.",
nil, nil,
)
hanaInfoDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "info"),
"Collector time duration.",
HanaInfoLabelNames, nil,
)
)
// Exporter collects HANA metrics. It implements prometheus.Collector.
type Exporter struct {
dsn string
scrapers []Scraper
error prometheus.Gauge
totalScrapes prometheus.Counter
scrapeErrors *prometheus.CounterVec
}
// split string, use @ as delimiter to split the dsn to get hana instance
//func stringsplit(s rune) bool {
// if s == '@' {
// return true
// }
// return false
//}
// New returns a new HANA exporter for the provided DSN.
func New(host string, user string, password string, scrapers []Scraper) *Exporter {
// BaseLabelValues[0] = host
return &Exporter{
dsn: fmt.Sprintf("hdb://%s:%s@%s", user, password, host),
scrapers: scrapers,
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "scrapes_total",
Help: "Total number of times HANA was scraped for metrics.",
}),
scrapeErrors: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "scrape_errors_total",
Help: "Total number of times an error occurred scraping a HANA.",
}, []string{"collector"}),
error: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "last_scrape_error",
Help: "Whether the last scrape of metrics from HANA resulted in an error (1 for error, 0 for success).",
}),
}
}
// Describe implements prometheus.Collector.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
// We cannot know in advance what metrics the exporter will generate
// from HANA. So we use the poor man's describe method: Run a collect
// and send the descriptors of all the collected metrics. The problem
// here is that we need to connect to the HANA DB. If it is currently
// unavailable, the descriptors will be incomplete. Since this is a
// stand-alone exporter and not used as a library within other code
// implementing additional metrics, the worst that can happen is that we
// don't detect inconsistent metrics created by this exporter
// itself. Also, a change in the monitored HANA instance may change the
// exported metrics during the runtime of the exporter.
metricCh := make(chan prometheus.Metric)
doneCh := make(chan struct{})
go func() {
for m := range metricCh {
ch <- m.Desc()
}
close(doneCh)
}()
e.Collect(metricCh)
close(metricCh)
<-doneCh
}
// Collect implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.scrape(ch)
ch <- e.totalScrapes
ch <- e.error
e.scrapeErrors.Collect(ch)
}
func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
e.totalScrapes.Inc()
var err error
scrapeTime := time.Now()
db, err := sql.Open("hdb", e.dsn)
if err != nil {
log.Errorln("Error opening connection to database:", err)
e.error.Set(1)
return
}
defer db.Close()
// By design exporter should use maximum one connection per request.
db.SetMaxOpenConns(1)
db.SetMaxIdleConns(1)
// Set max lifetime for a connection.
db.SetConnMaxLifetime(1 * time.Minute)
isUpRows, err := db.Query(upQuery)
if err != nil {
log.Errorln("Error pinging hana:", err)
ch <- prometheus.MustNewConstMetric(hanaUpDesc, prometheus.GaugeValue, 0)
e.error.Set(1)
return
} else {
ch <- prometheus.MustNewConstMetric(hanaUpDesc, prometheus.GaugeValue, 1)
var sid string
var db_name string
var db_version string
for isUpRows.Next() {
if err := isUpRows.Scan(&sid, &db_name, &db_version); err != nil {
return
}
}
HanaInfoLabelValues = []string{sid, db_name, db_version}
ch <- prometheus.MustNewConstMetric(hanaInfoDesc, prometheus.GaugeValue, 1, HanaInfoLabelValues...)
}
isUpRows.Close()
ch <- prometheus.MustNewConstMetric(scrapeDurationDesc, prometheus.GaugeValue, time.Since(scrapeTime).Seconds(), "connection")
wg := &sync.WaitGroup{}
defer wg.Wait()
for _, scraper := range e.scrapers {
wg.Add(1)
go func(scraper Scraper) {
defer wg.Done()
label := "collect." + scraper.Name()
scrapeTime := time.Now()
if err := scraper.Scrape(db, ch); err != nil {
log.Errorln("Error scraping for "+label+":", err)
e.scrapeErrors.WithLabelValues(label).Inc()
e.error.Set(1)
}
ch <- prometheus.MustNewConstMetric(scrapeDurationDesc, prometheus.GaugeValue, time.Since(scrapeTime).Seconds(), label)
}(scraper)
}
}
func parseStatus(data sql.RawBytes) (float64, bool) {
// sys_m_service_statistics
if bytes.Equal(data, []byte("YES")) {
return 1, true
}
if bytes.Equal(data, []byte("NO")) {
return 0, true
}
if bytes.Equal(data, []byte("UNKNOWN")) {
return 2, true
}
if bytes.Equal(data, []byte("STARTING")) {
return 3, true
}
if bytes.Equal(data, []byte("STOPPING")) {
return 4, true
}
// sys_m_service_replication
if bytes.Equal(data, []byte("TRUE")) {
return 1, true
}
if bytes.Equal(data, []byte("FALSE")) {
return 0, true
}
if bytes.Equal(data, []byte("ACTIVE")) {
return 1, true
}
if bytes.Equal(data, []byte("ERROR")) {
return 0, true
}
if bytes.Equal(data, []byte("INITIALIZING")) {
return 3, true
}
if bytes.Equal(data, []byte("SYNCING")) {
return 4, true
}
//default transform to float64
value, err := strconv.ParseFloat(string(data), 64)
return value, err == nil
}
func parseConfigString(data string) (float64, bool) {
// log_mode
if bytes.Equal([]byte(data), []byte("overwrite")) {
return 1, true
}
if bytes.Equal([]byte(data), []byte("normal")) {
return 0, true
}
//default transform from string to float64
value, err := strconv.ParseFloat(data, 64)
return value, err == nil
}