forked from BugRoger/nvidia-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
207 lines (194 loc) · 5.83 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
204
205
206
207
package main
import (
"flag"
"fmt"
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
namespace = "nvidia"
)
type Exporter struct {
up prometheus.Gauge
info *prometheus.GaugeVec
deviceCount prometheus.Gauge
temperatures *prometheus.GaugeVec
deviceInfo *prometheus.GaugeVec
powerUsage *prometheus.GaugeVec
powerUsageAverage *prometheus.GaugeVec
memoryTotal *prometheus.GaugeVec
memoryUsed *prometheus.GaugeVec
utilizationMemory *prometheus.GaugeVec
utilizationGPU *prometheus.GaugeVec
utilizationGPUAverage *prometheus.GaugeVec
}
func main() {
var (
listenAddress = flag.String("web.listen-address", ":9401", "Address to listen on for web interface and telemetry.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
)
flag.Parse()
prometheus.MustRegister(NewExporter())
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>NVML Exporter</title></head>
<body>
<h1>NVML Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
fmt.Println("Starting HTTP server on", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}
func NewExporter() *Exporter {
return &Exporter{
up: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "up",
Help: "NVML Metric Collection Operational",
},
),
info: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "driver_info",
Help: "NVML Info",
},
[]string{"version"},
),
deviceCount: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "device_count",
Help: "Count of found nvidia devices",
},
),
deviceInfo: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "info",
Help: "Info as reported by the device",
},
[]string{"index", "minor", "uuid", "name"},
),
temperatures: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "temperatures",
Help: "Temperature as reported by the device",
},
[]string{"minor"},
),
powerUsage: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "power_usage",
Help: "Power usage as reported by the device",
},
[]string{"minor"},
),
powerUsageAverage: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "power_usage_average",
Help: "Power usage as reported by the device averaged over 10s",
},
[]string{"minor"},
),
memoryTotal: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "memory_total",
Help: "Total memory as reported by the device",
},
[]string{"minor"},
),
memoryUsed: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "memory_used",
Help: "Used memory as reported by the device",
},
[]string{"minor"},
),
utilizationMemory: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "utilization_memory",
Help: "Memory Utilization as reported by the device",
},
[]string{"minor"},
),
utilizationGPU: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "utilization_gpu",
Help: "GPU utilization as reported by the device",
},
[]string{"minor"},
),
utilizationGPUAverage: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "utilization_gpu_average",
Help: "Used memory as reported by the device averraged over 10s",
},
[]string{"minor"},
),
}
}
func (e *Exporter) Collect(metrics chan<- prometheus.Metric) {
data, err := collectMetrics()
if err != nil {
log.Printf("Failed to collect metrics: %s\n", err)
e.up.Set(0)
e.up.Collect(metrics)
return
}
e.up.Set(1)
e.info.WithLabelValues(data.Version).Set(1)
e.deviceCount.Set(float64(len(data.Devices)))
for i := 0; i < len(data.Devices); i++ {
d := data.Devices[i]
e.deviceInfo.WithLabelValues(d.Index, d.MinorNumber, d.Name, d.UUID).Set(1)
e.memoryTotal.WithLabelValues(d.MinorNumber).Set(d.MemoryTotal)
e.memoryUsed.WithLabelValues(d.MinorNumber).Set(d.MemoryUsed)
e.powerUsage.WithLabelValues(d.MinorNumber).Set(d.PowerUsage)
e.powerUsageAverage.WithLabelValues(d.MinorNumber).Set(d.PowerUsageAverage)
e.temperatures.WithLabelValues(d.MinorNumber).Set(d.Temperature)
e.utilizationGPU.WithLabelValues(d.MinorNumber).Set(d.UtilizationGPU)
e.utilizationGPUAverage.WithLabelValues(d.MinorNumber).Set(d.UtilizationGPUAverage)
e.utilizationMemory.WithLabelValues(d.MinorNumber).Set(d.UtilizationMemory)
}
e.deviceCount.Collect(metrics)
e.deviceInfo.Collect(metrics)
e.info.Collect(metrics)
e.memoryTotal.Collect(metrics)
e.memoryUsed.Collect(metrics)
e.powerUsage.Collect(metrics)
e.powerUsageAverage.Collect(metrics)
e.temperatures.Collect(metrics)
e.up.Collect(metrics)
e.utilizationGPU.Collect(metrics)
e.utilizationGPUAverage.Collect(metrics)
e.utilizationMemory.Collect(metrics)
}
func (e *Exporter) Describe(descs chan<- *prometheus.Desc) {
e.deviceCount.Describe(descs)
e.deviceInfo.Describe(descs)
e.info.Describe(descs)
e.memoryTotal.Describe(descs)
e.memoryUsed.Describe(descs)
e.powerUsage.Describe(descs)
e.powerUsageAverage.Describe(descs)
e.temperatures.Describe(descs)
e.up.Describe(descs)
e.utilizationGPU.Describe(descs)
e.utilizationGPUAverage.Describe(descs)
e.utilizationMemory.Describe(descs)
}