forked from adaptant-labs/edgetpu-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (79 loc) · 2.04 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
package main
import (
"flag"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"net/http"
"os"
"sync"
)
const (
namespace = "edgetpu"
)
var (
labels = []string{"name"}
sysfsRoot = "/sys"
)
type EdgeTPUCollector struct {
sync.Mutex
numDevices prometheus.Gauge
temperature *prometheus.GaugeVec
}
func NewEdgeTPUCollector() *EdgeTPUCollector {
return &EdgeTPUCollector{
numDevices: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "num_devices",
Help: "Number of EdgeTPU devices",
},
),
temperature: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "temperature_celsius",
Help: "EdgeTPU device temperature in Celsius",
},
labels,
),
}
}
func (c *EdgeTPUCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.numDevices.Desc()
c.temperature.Describe(ch)
}
func (c *EdgeTPUCollector) Collect(ch chan<- prometheus.Metric) {
// Only allow one collection at a time
c.Lock()
defer c.Unlock()
c.temperature.Reset()
devices := FindEdgeTPUDevices()
c.numDevices.Set(float64(len(devices)))
ch <- c.numDevices
for i := 0; i < len(devices); i++ {
device := devices[i]
temp := device.Temperature()
// Temperature reading is not supported on all devices, skip the ones we don't know anything about
if temp > 0.0 {
c.temperature.WithLabelValues(device.name).Set(temp)
}
}
c.temperature.Collect(ch)
}
func main() {
var port int
flag.IntVar(&port, "port", 8080, "Port to listen to")
flag.StringVar(&sysfsRoot, "sysfs", "/sys", "Mountpoint of sysfs instance to scan")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "EdgeTPU Prometheus Exporter\n")
fmt.Fprintf(os.Stderr, "Usage: edgetpu-exporter [flags]\n\n")
flag.PrintDefaults()
}
flag.Parse()
prometheus.MustRegister(NewEdgeTPUCollector())
addr := fmt.Sprintf(":%d", port)
log.Printf("Listening on %s...\n", addr)
log.Fatalf("ListenAndServe error: %v", http.ListenAndServe(addr, promhttp.Handler()))
}