Added two parameters

Also ran gofmt
This commit is contained in:
Egon Rijpkema 2018-06-06 10:20:59 +02:00
parent 5972087e93
commit 9e786b3456

View File

@ -1,66 +1,65 @@
package main package main
import ( import (
"bytes" "bytes"
"encoding/csv" "encoding/csv"
"fmt" "fmt"
"net/http" "log"
"log" "net/http"
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
) )
// name, index, temperature.gpu, utilization.gpu, // name, index, temperature.gpu, utilization.gpu,
// utilization.memory, memory.total, memory.free, memory.used // utilization.memory, memory.total, memory.free, memory.used
func metrics(response http.ResponseWriter, request *http.Request) { func metrics(response http.ResponseWriter, request *http.Request) {
out, err := exec.Command( out, err := exec.Command(
"nvidia-smi", "nvidia-smi",
"--query-gpu=name,index,temperature.gpu,utilization.gpu,utilization.memory,memory.total,memory.free,memory.used", "--query-gpu=name,index,temperature.gpu,utilization.gpu,utilization.memory,memory.total,memory.free,memory.used,ecc.errors.uncorrected.aggregate.total,ecc.errors.uncorrected.volatile.total",
"--format=csv,noheader,nounits").Output() "--format=csv,noheader,nounits").Output()
if err != nil { if err != nil {
fmt.Printf("%s\n", err) fmt.Printf("%s\n", err)
return return
} }
csvReader := csv.NewReader(bytes.NewReader(out)) csvReader := csv.NewReader(bytes.NewReader(out))
csvReader.TrimLeadingSpace = true csvReader.TrimLeadingSpace = true
records, err := csvReader.ReadAll() records, err := csvReader.ReadAll()
if err != nil { if err != nil {
fmt.Printf("%s\n", err) fmt.Printf("%s\n", err)
return return
} }
metricList := []string { metricList := []string{
"temperature.gpu", "utilization.gpu", "temperature.gpu", "utilization.gpu",
"utilization.memory", "memory.total", "memory.free", "memory.used"} "utilization.memory", "memory.total", "memory.free", "memory.used", "eccerrorsaggregate.total", "eccuncorrectedvolatile.total"}
result := "" result := ""
for _, row := range records { for _, row := range records {
name := fmt.Sprintf("%s[%s]", row[0], row[1]) name := fmt.Sprintf("%s[%s]", row[0], row[1])
for idx, value := range row[2:] { for idx, value := range row[2:] {
result = fmt.Sprintf( result = fmt.Sprintf(
"%s%s{gpu=\"%s\"} %s\n", result, "%s%s{gpu=\"%s\"} %s\n", result,
metricList[idx], name, value) metricList[idx], name, value)
} }
} }
fmt.Fprintf(response, strings.Replace(result, ".", "_", -1)) fmt.Fprintf(response, strings.Replace(result, ".", "_", -1))
} }
func main() { func main() {
addr := ":9101" addr := ":9101"
if len(os.Args) > 1 { if len(os.Args) > 1 {
addr = ":" + os.Args[1] addr = ":" + os.Args[1]
} }
http.HandleFunc("/metrics/", metrics) http.HandleFunc("/metrics/", metrics)
err := http.ListenAndServe(addr, nil) err := http.ListenAndServe(addr, nil)
if err != nil { if err != nil {
log.Fatal("ListenAndServe: ", err) log.Fatal("ListenAndServe: ", err)
} }
} }