forked from zhebrak/nvidia_smi_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnvidia_smi_exporter.go
87 lines (75 loc) · 1.91 KB
/
nvidia_smi_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
package main
import (
"bytes"
"encoding/csv"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
)
const (
GpuLostOutput = "GPU is lost" // Substring to look for in output when GPU is broken
GpuMetric = "gpu_lost" // Metrics name
)
func metrics(response http.ResponseWriter, request *http.Request) {
out, err := exec.Command(
"nvidia-smi",
"--query-gpu=name,index,count,temperature.gpu,utilization.gpu,utilization.memory,memory.total,memory.free,memory.used,power.draw",
"--format=csv,noheader,nounits").Output()
outStr := strings.ToLower(string(out))
result := ""
if err != nil {
// Check the output of nvidia-smi for "gpu is lost" to determine GPU failures
if strings.Contains(outStr, strings.ToLower(GpuLostOutput)) {
fmt.Println("ALERT: nvidia-smi reports a GPU is lost/broken")
result = fmt.Sprintf(
"%s%s %.0f\n",
result,
GpuMetric,
1.0)
}
// fmt.Printf("%s\n", err)
fmt.Fprintf(response, result)
return
}
csvReader := csv.NewReader(bytes.NewReader(out))
csvReader.TrimLeadingSpace = true
records, err := csvReader.ReadAll()
if err != nil {
fmt.Println("Error csv")
fmt.Printf("%s\n", err)
return
}
metricList := []string{
"count",
"temperature.gpu",
"utilization.gpu",
"utilization.memory", "memory.total", "memory.free", "memory.used", "power.draw"}
for _, row := range records {
name := fmt.Sprintf("%s[%s]", row[0], row[1])
for idx, value := range row[2:] {
floatVal, _ := strconv.ParseFloat(value, 64)
result = fmt.Sprintf(
"%s%s{gpu=\"%s\"} %.2f\n",
result,
strings.Replace(metricList[idx], ".", "_", -1),
strings.Replace(name, ".", "_", -1),
floatVal)
}
}
fmt.Fprintf(response, result)
}
func main() {
addr := ":9101"
if len(os.Args) > 1 {
addr = ":" + os.Args[1]
}
http.HandleFunc("/metrics/", metrics)
err := http.ListenAndServe(addr, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}