-
Notifications
You must be signed in to change notification settings - Fork 1
/
collector.go
207 lines (177 loc) · 5.07 KB
/
collector.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
package main
import (
goflag "flag"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"github.com/NVIDIA/gpu-monitoring-tools/bindings/go/nvml"
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
)
// NvidiaCollector nvidia gpu collector
type NvidiaCollector struct {
sync.Mutex
numDevices prometheus.Gauge
memoryUsed *prometheus.GaugeVec
memoryTotal *prometheus.GaugeVec
powerUsage *prometheus.GaugeVec
temperature *prometheus.GaugeVec
}
func newNvidiaCollector() *NvidiaCollector {
namespace := "nvidia_gpu"
labels := []string{"path", "uuid", "name"}
nc := &NvidiaCollector{
numDevices: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "num_devices",
Help: "Number of Nvidia GPU devices",
},
),
memoryUsed: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "memory_used_mb",
Help: "Memory used by the GPU device in MB",
},
labels,
),
memoryTotal: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "memory_total_mb",
Help: "Memory Total of the GPU device in MB",
},
labels,
),
powerUsage: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "power_usage",
Help: "Power usage of the GPU device in watts",
},
labels,
),
temperature: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "temperature",
Help: "Temperature of the GPU device in celsius",
},
labels,
),
}
return nc
}
// Describe implement prometheus Collector interface
func (nc *NvidiaCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- nc.numDevices.Desc()
nc.memoryUsed.Describe(ch)
nc.memoryTotal.Describe(ch)
nc.powerUsage.Describe(ch)
nc.temperature.Describe(ch)
}
// Collect implement prometheus Collector interface
func (nc *NvidiaCollector) Collect(ch chan<- prometheus.Metric) {
nc.Lock()
defer nc.Unlock()
nc.memoryUsed.Reset()
nc.memoryTotal.Reset()
nc.powerUsage.Reset()
nc.temperature.Reset()
count, err := nvml.GetDeviceCount()
if err != nil {
glog.Errorf("Failed to GetDeviceCount(): %v.", err)
return
}
nc.numDevices.Set(float64(count))
ch <- nc.numDevices
for i := uint(0); i < count; i++ {
device, err := nvml.NewDevice(i)
if err != nil {
glog.Warningf("Failed to NewDevice %d: %v.", i, err)
continue
}
st, err := device.Status()
if err != nil {
glog.Warningf("Failed to get device[%d] status: %v.", i, err)
}
path := device.Path
name := device.Model
uuid := device.UUID
nc.memoryUsed.WithLabelValues(path, uuid, *name).Set(float64(*st.Memory.Global.Used))
nc.memoryTotal.WithLabelValues(path, uuid, *name).Set(float64(*device.Memory))
nc.powerUsage.WithLabelValues(path, uuid, *name).Set(float64(*st.Power))
nc.temperature.WithLabelValues(path, uuid, *name).Set(float64(*st.Temperature))
}
nc.memoryUsed.Collect(ch)
nc.memoryTotal.Collect(ch)
nc.powerUsage.Collect(ch)
nc.temperature.Collect(ch)
}
// newCollectorCommand create and returns a new gpu collector
func newCollectorCommand() *cobra.Command {
var (
listenAddress string
metricsPath string
)
cmd := &cobra.Command{
Use: "run",
Short: "Run web interface",
Long: "Run web interface expose metrics",
Run: func(cmd *cobra.Command, args []string) {
goflag.Parse()
glog.Infof("Starting gpu_exporter %s", version)
if err := nvml.Init(); err != nil {
glog.Errorf("Failed to initialize nvidia device: %v.", err)
return
}
defer func() {
glog.Info("Shudown NVML library.")
nvml.Shutdown()
}()
prometheus.MustRegister(newNvidiaCollector())
shudownC := make(chan struct{})
go listenToSystemSignal(shudownC)
http.Handle(metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>GPU Exporter</title></head>
<body>
<h1>GPU Exporter</h1>
<p><a href="` + metricsPath + `">Metrics</a></p>
</body>
</html>`))
})
go func() {
http.ListenAndServe(listenAddress, nil)
}()
<-shudownC
glog.Info("GPU exporter shutdown completed.")
return
},
}
cmd.PersistentFlags().AddGoFlagSet(goflag.CommandLine)
flags := cmd.Flags()
flags.SetInterspersed(false)
flags.StringVar(&listenAddress, "web.listen-address", ":9470", "Address on which to expose metrics and web interface.")
flags.StringVar(&metricsPath, "web.path", "/metrics", "Path under which to expose metrics.")
return cmd
}
// listenToSystemSignal listen system signal and exit exporter.
func listenToSystemSignal(stopC chan<- struct{}) {
glog.V(5).Info("Listen to system signal.")
signalChan := make(chan os.Signal, 1)
ignoreChan := make(chan os.Signal, 1)
signal.Notify(ignoreChan, syscall.SIGHUP)
signal.Notify(signalChan, os.Interrupt, os.Kill, syscall.SIGTERM)
select {
case sig := <-signalChan:
glog.V(3).Infof("GPU exporter shutdown by system signal: %s", sig)
stopC <- struct{}{}
}
}