forked from HewlettPackard/lustre_exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lustre_exporter.go
173 lines (151 loc) · 6.34 KB
/
lustre_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
// (C) Copyright 2017 Hewlett Packard Enterprise Development LP
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"net/http"
"sync"
"time"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/HewlettPackard/lustre_exporter/sources"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
)
var (
scrapeDurations = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Namespace: sources.Namespace,
Subsystem: "exporter",
Name: "scrape_duration_seconds",
Help: "lustre_exporter: Duration of a scrape job.",
},
[]string{"source", "result"},
)
)
//LustreSource is a list of all sources that the user would like to collect.
type LustreSource struct {
sourceList map[string]sources.LustreSource
}
//Describe implements the prometheus.Describe interface
func (l LustreSource) Describe(ch chan<- *prometheus.Desc) {
scrapeDurations.Describe(ch)
}
//Collect implements the prometheus.Collect interface
func (l LustreSource) Collect(ch chan<- prometheus.Metric) {
wg := sync.WaitGroup{}
wg.Add(len(l.sourceList))
for name, c := range l.sourceList {
go func(name string, c sources.LustreSource) {
collectFromSource(name, c, ch)
wg.Done()
}(name, c)
}
wg.Wait()
scrapeDurations.Collect(ch)
}
func collectFromSource(name string, s sources.LustreSource, ch chan<- prometheus.Metric) {
result := "success"
begin := time.Now()
err := s.Update(ch)
duration := time.Since(begin)
if err != nil {
log.Errorf("ERROR: %q source failed after %f seconds: %s", name, duration.Seconds(), err)
result = "error"
} else {
log.Debugf("OK: %q source succeeded after %f seconds: %s", name, duration.Seconds(), err)
}
scrapeDurations.WithLabelValues(name, result).Observe(duration.Seconds())
}
func loadSources(list []string) (map[string]sources.LustreSource, error) {
sourceList := map[string]sources.LustreSource{}
for _, name := range list {
fn, ok := sources.Factories[name]
if !ok {
return nil, fmt.Errorf("source %q not available", name)
}
c := fn()
sourceList[name] = c
}
return sourceList, nil
}
func init() {
prometheus.MustRegister(version.NewCollector("lustre_exporter"))
}
func main() {
kingpin.Version(version.Print("lustre_exporter"))
kingpin.HelpFlag.Short('h')
var (
clientEnabled = kingpin.Flag("collector.client", "Set client metric level. Valid levels: [extended, core, disabled]").Default("extended").Enum("extended", "core", "disabled")
genericEnabled = kingpin.Flag("collector.generic", "Set generic metric level. Valid levels: [extended, core, disabled]").Default("extended").Enum("extended", "core", "disabled")
lnetEnabled = kingpin.Flag("collector.lnet", "Set LNET metric level. Valid levels: [extended, core, disabled]").Default("extended").Enum("extended", "core", "disabled")
mdsEnabled = kingpin.Flag("collector.mds", "Set MDS metric level. Valid levels: [extended, core, disabled]").Default("extended").Enum("extended", "core", "disabled")
mdtEnabled = kingpin.Flag("collector.mdt", "Set MDT metric level. Valid levels: [extended, core, disabled]").Default("extended").Enum("extended", "core", "disabled")
mgsEnabled = kingpin.Flag("collector.mgs", "Set MGS metric level. Valid levels: [extended, core, disabled]").Default("extended").Enum("extended", "core", "disabled")
ostEnabled = kingpin.Flag("collector.ost", "Set OST metric level. Valid levels: [extended, core, disabled]").Default("extended").Enum("extended", "core", "disabled")
healthStatusEnabled = kingpin.Flag("collector.health", "Set Health metric level. Valid levels: [extended, core, disabled]").Default("extended").Enum("extended", "core", "disabled")
listenAddress = kingpin.Flag("web.listen-address", "Address to use to expose Lustre metrics.").Default(":9169").String()
metricsPath = kingpin.Flag("web.telemetry-path", "Path to use to expose Lustre metrics.").Default("/metrics").String()
)
kingpin.Parse()
log.Infoln("Starting lustre_exporter", version.Info())
log.Infoln("Build context", version.BuildContext())
log.Infof("Collector status:")
sources.OstEnabled = *ostEnabled
log.Infof(" - OST State: %s", sources.OstEnabled)
sources.MdtEnabled = *mdtEnabled
log.Infof(" - MDT State: %s", sources.MdtEnabled)
sources.MgsEnabled = *mgsEnabled
log.Infof(" - MGS State: %s", sources.MgsEnabled)
sources.MdsEnabled = *mdsEnabled
log.Infof(" - MDS State: %s", sources.MdsEnabled)
sources.ClientEnabled = *clientEnabled
log.Infof(" - Client State: %s", sources.ClientEnabled)
sources.GenericEnabled = *genericEnabled
log.Infof(" - Generic State: %s", sources.GenericEnabled)
sources.LnetEnabled = *lnetEnabled
log.Infof(" - Lnet State: %s", sources.LnetEnabled)
sources.HealthStatusEnabled = *healthStatusEnabled
log.Infof(" - Health State: %s", sources.HealthStatusEnabled)
enabledSources := []string{"procfs", "procsys", "sysfs"}
sourceList, err := loadSources(enabledSources)
if err != nil {
log.Fatalf("Couldn't load sources: %q", err)
}
log.Infof("Available sources:")
for s := range sourceList {
log.Infof(" - %s", s)
}
prometheus.MustRegister(LustreSource{sourceList: sourceList})
handler := promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{ErrorLog: log.NewErrorLogger()})
http.Handle(*metricsPath, prometheus.InstrumentHandler("prometheus", handler))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
var num int
num, err = w.Write([]byte(`<html>
<head><title>Lustre Exporter</title></head>
<body>
<h1>Lustre Exporter</h1>
<p><a href="` + *metricsPath + `">Metrics</a></p>
</body>
</html>`))
if err != nil {
log.Fatal(num, err)
}
})
log.Infoln("Listening on", *listenAddress)
err = http.ListenAndServe(*listenAddress, nil)
if err != nil {
log.Fatal(err)
}
}