forked from Dirbaio/gominer
-
Notifications
You must be signed in to change notification settings - Fork 79
/
monitor.go
108 lines (86 loc) · 2.34 KB
/
monitor.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
package main
import (
"encoding/json"
"net/http"
"time"
"github.com/decred/gominer/util"
)
type MinerStatus struct {
ValidShares uint64 `json:"validShares"`
StaleShares uint64 `json:"staleShares"`
InvalidShares uint64 `json:"invalidShares"`
TotalShares uint64 `json:"totalShares"`
SharesPerMinute float64 `json:"sharesPerMinute"`
Started uint32 `json:"started"`
Uptime uint32 `json:"uptime"`
Devices []*DeviceStatus `json:"devices"`
Pool *PoolStatus `json:"pool,omitempty"`
}
type DeviceStatus struct {
Index int `json:"index"`
DeviceName string `json:"deviceName"`
DeviceType string `json:"deviceType"`
HashRate float64 `json:"hashRate"`
HashRateFormatted string `json:"hashRateFormatted"`
FanPercent uint32 `json:"fanPercent"`
Temperature uint32 `json:"temperature"`
Started uint32 `json:"started"`
}
type PoolStatus struct {
Started uint32 `json:"started"`
Uptime uint32 `json:"uptime"`
}
var (
m *Miner
)
func RunMonitor(tm *Miner) {
m = tm
if len(cfg.APIListeners) != 0 {
http.HandleFunc("/", getMinerStatus)
for _, addr := range cfg.APIListeners {
err := http.ListenAndServe(addr, nil)
if err != nil {
mainLog.Warnf("Unable to create monitor: %v", err)
return
}
}
}
}
func getMinerStatus(w http.ResponseWriter, req *http.Request) {
ms := &MinerStatus{
Started: m.started,
Uptime: uint32(time.Now().Unix()) - m.started,
}
if !cfg.Benchmark {
valid, invalid, stale, total, sharesPerMinute := m.Status()
ms.ValidShares = valid
ms.InvalidShares = invalid
ms.StaleShares = stale
ms.TotalShares = total
ms.SharesPerMinute = sharesPerMinute
if cfg.Pool != "" {
ms.Pool = &PoolStatus{
Started: m.started,
Uptime: uint32(time.Now().Unix()) - m.started,
}
}
}
for _, d := range m.devices {
d.UpdateFanTemp()
averageHashRate,
fanPercent,
temperature := d.Status()
ms.Devices = append(ms.Devices, &DeviceStatus{
Index: d.index,
DeviceName: d.deviceName,
DeviceType: d.deviceType,
HashRate: averageHashRate,
HashRateFormatted: util.FormatHashRate(averageHashRate),
FanPercent: fanPercent,
Temperature: temperature,
Started: d.started,
})
}
w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(ms)
}