-
Notifications
You must be signed in to change notification settings - Fork 10
/
pgme.go
166 lines (125 loc) · 3.53 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
package main
import (
"bytes"
"context"
"encoding/csv"
"fmt"
"html/template"
"net/http"
"log"
"os"
"os/exec"
"strings"
"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 {
"temperature.gpu", "utilization.gpu",
"utilization.memory", "memory.total", "memory.free", "memory.used"}
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 metrics(response http.ResponseWriter, request *http.Request) {
out, err := exec.Command(
"nvidia-smi",
"--query-gpu=name,index,temperature.gpu,utilization.gpu,utilization.memory,memory.total,memory.free,memory.used",
"--format=csv,noheader,nounits").Output()
if err != nil {
log.Printf("ERROR: %s\n", err)
return
}
csvReader := csv.NewReader(bytes.NewReader(out))
csvReader.TrimLeadingSpace = true
records, err := csvReader.ReadAll()
if err != nil {
log.Printf("%s\n", err)
return
}
metricList := []string {
"temperature.gpu", "utilization.gpu",
"utilization.memory", "memory.total", "memory.free", "memory.used"}
result := ""
for _, row := range records {
name := fmt.Sprintf("%s[%s]", row[0], row[1])
for idx, value := range row[2:] {
result = fmt.Sprintf("%s%s{gpu=\"%s\"} %s\n", result, metricList[idx], name, value)
}
}
fmt.Fprintf(response, strings.Replace(result, ".", "_", -1))
}
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")
}