forked from chhibber/pgme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgme.go
219 lines (174 loc) · 5.65 KB
/
pgme.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
package main
import (
"bytes"
"context"
"encoding/csv"
"fmt"
"html/template"
"net/http"
"log"
"os"
"os/exec"
"strconv"
"syscall"
"os/signal"
"path"
)
type PageVariables struct {
PageTitle string
Metrics []string
VersionInfo map[string]string
}
var (
// BuildTime is a time label of the moment when the binary was built
BuildTime = "unset"
// Commit is a last commit hash at the moment when the binary was built
Commit = "unset"
// Release is a semantic version of current build
Release = "unset"
)
func getEnv(key, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
}
return value
}
// healthz is a liveness probe.
func healthz(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}
// name, index, temperature.gpu, utilization.gpu,
// utilization.memory, memory.total, memory.free, memory.used
func home(w http.ResponseWriter, r *http.Request) {
metricList := []string {
"gpu_power_draw",
"gpu_power_limit",
"gpu_clock_shader_current",
"gpu_clock_shader_maximum",
"gpu_clock_streaming_multiprocessor_current",
"gpu_clock_streaming_multiprocessor_maximum",
"gpu_clock_memory_current",
"gpu_clock_memory_maximum",
"gpu_temperature_processor",
"gpu_temperature_memory",
"gpu_utilization_processor",
"gpu_utilization_memory",
"gpu_utilization_fan",
"gpu_memory_ecc_mode",
"gpu_memory_free",
"gpu_memory_used",
"gpu_memory_total",
"gpu_count",
}
verInfo := make(map[string]string)
verInfo["Buildtime"] = BuildTime
verInfo["Commit"] = Commit
verInfo["Release"] = Release
pv := PageVariables{
PageTitle: "Prometheus nVidia GPU Metrics Exporter",
Metrics: metricList,
VersionInfo: verInfo,
}
filepath := path.Join(path.Dir("./template/home.html"), "home.html")
template.ParseFiles()
t, err := template.ParseFiles(filepath)
if err != nil {
log.Print("Template parsing error: ", err)
}
err = t.Execute(w, pv)
if err != nil {
log.Print("Template execution error: ", err)
}
}
func isNumeric(s string) bool {
_, err := strconv.ParseFloat(s, 64)
return err == nil
}
func parseCSV(records [][]string) string {
metricList := []string {
"gpu_power_draw",
"gpu_power_limit",
"gpu_clock_shader_current",
"gpu_clock_shader_maximum",
"gpu_clock_streaming_multiprocessor_current",
"gpu_clock_streaming_multiprocessor_maximum",
"gpu_clock_memory_current",
"gpu_clock_memory_maximum",
"gpu_temperature_processor",
"gpu_temperature_memory",
"gpu_utilization_processor",
"gpu_utilization_memory",
"gpu_utilization_fan",
"gpu_memory_free",
"gpu_memory_used",
"gpu_memory_total",
}
result := ""
for _, row := range records {
name := fmt.Sprintf("%s[%s]", row[0], row[1])
for idx, value := range row[2:] {
if isNumeric(value) {
result = fmt.Sprintf("%s%s{gpu=\"%s\"} %s\n", result, metricList[idx], name, value)
}
}
}
return result
}
func metrics(response http.ResponseWriter, request *http.Request) {
out, err := exec.Command(
"nvidia-smi",
// power clock temperature utilization memory
// 0 1 0 1 2 3 4 5 0 1 0 1 3 0 1 2
"--query-gpu=name,index,power.draw,power.limit,clocks.gr,clocks.max.gr,clocks.sm,clocks.max.sm,clocks.mem,clocks.max.mem,temperature.gpu,temperature.memory,utilization.gpu,utilization.memory,fan.speed,memory.free,memory.used,memory.total",
"--format=csv,noheader,nounits",
).Output()
result := ""
gpu_count := 0
if err != nil {
log.Printf("ERROR: %s\n", err)
} else {
csvReader := csv.NewReader(bytes.NewReader(out))
csvReader.TrimLeadingSpace = true
records, err := csvReader.ReadAll()
gpu_count = len(records)
if err != nil {
log.Printf("%s\n", err, len(records))
} else {
result = parseCSV(records)
}
}
result = fmt.Sprintf("%s%s{} %d\n", result, "gpu_count", gpu_count)
fmt.Fprintf(response, result)
}
func main() {
log.Print("Starting the service...")
port := getEnv("PORT", "9101");
addr := ":"+port
log.Print("- PORT set to "+ port +". If environment variable PORT is not set the default is 9101")
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
srv := &http.Server{
Addr: addr,
}
go func() {
http.HandleFunc("/", home)
http.HandleFunc("/healthz", healthz)
http.HandleFunc("/metrics/", metrics)
err := srv.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}()
log.Print("The service is listening on ", port)
killSignal := <-interrupt
switch killSignal {
case os.Interrupt:
log.Print("Got SIGINT...")
case syscall.SIGTERM:
log.Print("Got SIGTERM...")
}
log.Print("The service is shutting down...")
srv.Shutdown(context.Background())
log.Print("Done")
}