forked from flashcatcloud/categraf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnvidia_smi.go
More file actions
114 lines (91 loc) · 3.06 KB
/
Copy pathnvidia_smi.go
File metadata and controls
114 lines (91 loc) · 3.06 KB
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
package nvidia_smi
// This is a fork of https://github.com/utkuozdemir/nvidia_gpu_exporter
import (
"log"
"strings"
"time"
"flashcat.cloud/categraf/config"
"flashcat.cloud/categraf/inputs"
"flashcat.cloud/categraf/types"
)
const inputName = "nvidia_smi"
type GPUStats struct {
config.PluginConfig
NvidiaSmiCommand string `toml:"nvidia_smi_command"`
QueryFieldNames string `toml:"query_field_names"`
QueryTimeOut config.Duration `toml:"query_timeout"`
qFields []qField
qFieldToMetricInfoMap map[qField]MetricInfo
}
func init() {
inputs.Add(inputName, func() inputs.Input {
return &GPUStats{}
})
}
func (s *GPUStats) Clone() inputs.Input {
return &GPUStats{}
}
func (s *GPUStats) Name() string {
return inputName
}
func (s *GPUStats) Init() error {
if s.NvidiaSmiCommand == "" {
return types.ErrInstancesEmpty
}
if s.QueryTimeOut == 0 {
s.QueryTimeOut = config.Duration(5 * time.Second)
}
qFieldsOrdered, qFieldToRFieldMap, err := s.buildQFieldToRFieldMap()
if err != nil {
return err
}
s.qFields = qFieldsOrdered
s.qFieldToMetricInfoMap = buildQFieldToMetricInfoMap(qFieldToRFieldMap)
return nil
}
func (s *GPUStats) Gather(slist *types.SampleList) {
if s.NvidiaSmiCommand == "" {
return
}
begun := time.Now()
// scrape use seconds
defer func(begun time.Time) {
use := time.Since(begun).Seconds()
slist.PushFront(types.NewSample(inputName, "scrape_use_seconds", use))
}(begun)
currentTable, err := s.scrape(s.qFields)
if err != nil {
slist.PushFront(types.NewSample(inputName, "scraper_up", 0))
return
}
slist.PushFront(types.NewSample(inputName, "scraper_up", 1))
for _, currentRow := range currentTable.rows {
uuid := strings.TrimPrefix(strings.ToLower(currentRow.qFieldToCells[uuidQField].rawValue), "gpu-")
index := currentRow.qFieldToCells[indexQField].rawValue
name := currentRow.qFieldToCells[nameQField].rawValue
driverModelCurrent := currentRow.qFieldToCells[driverModelCurrentQField].rawValue
driverModelPending := currentRow.qFieldToCells[driverModelPendingQField].rawValue
vBiosVersion := currentRow.qFieldToCells[vBiosVersionQField].rawValue
driverVersion := currentRow.qFieldToCells[driverVersionQField].rawValue
slist.PushFront(types.NewSample(inputName, "gpu_info", 1, map[string]string{
"uuid": uuid,
"index": index,
"name": name,
"driver_model_current": driverModelCurrent,
"driver_model_pending": driverModelPending,
"vbios_version": vBiosVersion,
"driver_version": driverVersion,
}))
for _, currentCell := range currentRow.cells {
metricInfo := s.qFieldToMetricInfoMap[currentCell.qField]
num, err := transformRawValue(currentCell.rawValue, metricInfo.valueMultiplier)
if err != nil {
if s.DebugMod {
log.Println("D! failed to transform gpu field:", currentCell.qField, "raw value:", currentCell.rawValue, "error:", err)
}
continue
}
slist.PushFront(types.NewSample(inputName, metricInfo.metricName, num, map[string]string{"uuid": uuid, "index": index}))
}
}
}