-
Notifications
You must be signed in to change notification settings - Fork 2
/
exporter.go
165 lines (143 loc) · 4.5 KB
/
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
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
package main
import (
"log"
"strconv"
"strings"
"sync"
"time"
"github.com/blockassets/bw_exporter/cgminer"
"github.com/prometheus/client_golang/prometheus"
)
//
var (
idStateLabelNames = []string{"id", "state"}
idRateLabelNames = []string{"id", "rate"}
idLabelNames = []string{"id"}
)
//
func newGauge(metricName string, docString string, constLabels prometheus.Labels) prometheus.Gauge {
return prometheus.NewGauge(prometheus.GaugeOpts{
Name: metricName,
Help: docString,
ConstLabels: constLabels,
})
}
//
func newGaugeVec(metricName string, docString string, constLabels prometheus.Labels, labels []string) *prometheus.GaugeVec {
return prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: metricName,
Help: docString,
ConstLabels: constLabels,
},
labels,
)
}
// Collector interface
type Exporter struct {
hostname string
port int64
timeout time.Duration
chipStatGauge *prometheus.GaugeVec
devsHashRateGauge *prometheus.GaugeVec
devsHashCountGauge *prometheus.GaugeVec
devsErrorsGauge *prometheus.GaugeVec
devsTemperatureGauge prometheus.Gauge
sync.Mutex
}
//
type CgminerStats struct {
ChipStat *cgminer.ChipStat
Devs *[]cgminer.Devs
}
//
func (e *Exporter) fetchData() (*CgminerStats, error) {
log.Printf("fetchData(%s:%d, %s)", e.hostname, e.port, e.timeout)
miner := cgminer.New(e.hostname, e.port, e.timeout)
chipStat, err := miner.ChipStat()
if err != nil {
log.Println(err)
return nil, err
}
devs, err := miner.Devs()
if err != nil {
log.Println(err)
return nil, err
}
return &CgminerStats{chipStat, devs}, nil
}
//
func NewExporter(cgHost string, cgPort int64, cgTimeout time.Duration) *Exporter {
versionLabel := prometheus.Labels{}
version = cgminer.ReadVersionFile()
if len(version) > 0 {
versionLabel = prometheus.Labels{"version": version}
}
return &Exporter{
hostname: cgHost,
port: cgPort,
timeout: cgTimeout,
chipStatGauge: newGaugeVec("bw_chipstat", "Chip accept/reject", versionLabel, idStateLabelNames),
devsHashCountGauge: newGaugeVec("bw_devs_hashcount", "Device hash accept/reject", versionLabel, idStateLabelNames),
devsHashRateGauge: newGaugeVec("bw_devs_hashrate", "Device hashrate", versionLabel, idRateLabelNames),
devsErrorsGauge: newGaugeVec("bw_devs_errors", "Device hardware errors", versionLabel, idLabelNames),
devsTemperatureGauge: newGauge("bw_devs_temperature", "Device temperature", versionLabel),
}
}
// Parses the map[string]float64 into a gauge with labels.
// {"1_accept": 1, "1_reject": 100}
func collectChipStat(e *Exporter, cgStats *CgminerStats) {
chipStat := cgStats.ChipStat
for chip, value := range *chipStat {
s := strings.Split(chip, "_")
chipId, chipType := s[0], s[1]
e.chipStatGauge.WithLabelValues(chipId, chipType).Set(value)
}
}
//
func collectDevs(e *Exporter, cgStats *CgminerStats) {
devs := cgStats.Devs
for id, value := range *devs {
idStr := strconv.Itoa(id)
e.devsHashCountGauge.WithLabelValues(idStr, "accept").Set(float64(value.Accepted))
e.devsHashCountGauge.WithLabelValues(idStr, "reject").Set(float64(value.Rejected))
e.devsHashRateGauge.WithLabelValues(idStr, "MHS_av").Set(value.MHSav)
e.devsHashRateGauge.WithLabelValues(idStr, "MHS_5s").Set(value.MHS5s)
e.devsHashRateGauge.WithLabelValues(idStr, "MHS_1m").Set(value.MHS1m)
e.devsHashRateGauge.WithLabelValues(idStr, "MHS_5m").Set(value.MHS5m)
e.devsHashRateGauge.WithLabelValues(idStr, "MHS_15m").Set(value.MHS15m)
e.devsErrorsGauge.WithLabelValues(idStr).Set(float64(value.HardwareErrors))
// All 4 devs report the same temperature
e.devsTemperatureGauge.Set(value.Temperature)
}
}
// Outputs the gauge values on the channel
func collectGauges(e *Exporter, ch chan<- prometheus.Metric) {
e.chipStatGauge.Collect(ch)
e.devsHashRateGauge.Collect(ch)
e.devsHashCountGauge.Collect(ch)
e.devsErrorsGauge.Collect(ch)
e.devsTemperatureGauge.Collect(ch)
}
//
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
e.chipStatGauge.Describe(ch)
e.devsHashCountGauge.Describe(ch)
e.devsHashRateGauge.Describe(ch)
e.devsErrorsGauge.Describe(ch)
e.devsTemperatureGauge.Describe(ch)
}
//
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
// Prevents multiple concurrent calls
e.Lock()
defer e.Unlock()
// Fetch data from the worker
cgStats, err := e.fetchData()
if err != nil {
return
}
collectChipStat(e, cgStats)
collectDevs(e, cgStats)
collectGauges(e, ch)
}