This repository was archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththresholds.go
180 lines (161 loc) · 5.89 KB
/
thresholds.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package miningmonitor
import (
"fmt"
"strconv"
"github.com/golang/glog"
)
// ThresholdFunc contains logic to determine if any actions need to be taken on the client
type ThresholdFunc func(stats *Statistics) []error
// IntComparison will comapre the two integers provided
type IntComparison func(a, b int) bool
// FloatComparison will compare the two floats provided
type FloatComparison func(a, b float64) bool
// StringComparison will compare the two strings provided
type StringComparison func(a, b string) bool
// IntGreaterThan returns true is a > b
func IntGreaterThan(a, b int) bool {
return a > b
}
// IntLessThan returns true if a < b
func IntLessThan(a, b int) bool {
return a < b
}
// FloatGreaterThan returns true is a > b
func FloatGreaterThan(a, b float64) bool {
return a > b
}
// FloatLessThan returns true if a < b
func FloatLessThan(a, b float64) bool {
return a < b
}
// FloatComparatorFromString will parse a string of the format "<10.0" to use the proper comparator
func FloatComparatorFromString(s string) FloatComparison {
switch []rune(s)[0] {
case []rune(">")[0]:
return FloatGreaterThan
case []rune("<")[0]:
return FloatLessThan
default:
panic(fmt.Errorf("unknown threshold found %s, a threshold must have a first character of '>|<' followed by a number", s))
}
}
// IntComparatorFromString will parse a string of the format "<10.0" to use the proper comparator
func IntComparatorFromString(s string) IntComparison {
switch []rune(s)[0] {
case []rune(">")[0]:
return IntGreaterThan
case []rune("<")[0]:
return IntLessThan
default:
panic(fmt.Errorf("unknown threshold found %s, a threshold must have a first character of '>|<' followed by a number", s))
}
}
// Threshold used to take action on a client if exceeded
type Threshold struct {
Check ThresholdFunc
Threshold string
CauseReboot bool
SendEmail bool
Name string
}
// String human readable format ofa threshold
func (t Threshold) String() string {
return fmt.Sprintf("%s: %s - [Cause Reboot? %t, Send Email? %t]", t.Name, t.Threshold, t.CauseReboot, t.SendEmail)
}
// NewHashRateThreshold returns a Threshold that will check if a client has exceeded the given hash rate.
// threshold should be of the format "<20000" or ">20000".
func NewHashRateThreshold(threshold string, causeReboot, sendEmail bool) (*Threshold, error) {
comp := IntComparatorFromString(threshold)
number, err := strconv.Atoi(threshold[1:])
if err != nil {
return nil, fmt.Errorf("unknown threshold found %s, a threshold must have a first character of '>|<' followed by a number: %s", threshold, err)
}
return &Threshold{
Check: func(stats *Statistics) []error {
var errors []error
for i, hash := range stats.MainGpuHashRate {
glog.V(2).Infof("GPU %d hashrate %0.2f", i, hash)
if comp(int(hash), number) {
errors = append(errors, fmt.Errorf("GPU %d threshold exceeded %d%s", i, int(hash), threshold))
}
}
return errors
},
Threshold: threshold,
CauseReboot: causeReboot,
SendEmail: sendEmail,
Name: "HashRate",
}, nil
}
// NewPowerThreshold returns a Threshold that will check if a client has exceeded the given power wattage.
// threshold should be of the format "<200" or ">200".
func NewPowerThreshold(threshold string, causeReboot, sendEmail bool) (*Threshold, error) {
comp := FloatComparatorFromString(threshold)
number, err := strconv.ParseFloat(threshold[1:], 64)
if err != nil {
return nil, fmt.Errorf("unknown threshold found %s, a threshold must have a first character of '>|<' followed by a number: %s", threshold, err)
}
return &Threshold{
Check: func(stats *Statistics) []error {
glog.V(2).Infof("rig power %0.2f", stats.PowerState.Power)
if comp(stats.PowerState.Power, number) {
return []error{fmt.Errorf("power threshold exceeded %0.2f%s", stats.PowerState.Power, threshold)}
}
return nil
},
Threshold: threshold,
CauseReboot: causeReboot,
SendEmail: sendEmail,
Name: "Power",
}, nil
}
// NewTemperatureThreshold returns a Threshold that will check if a client has exceeded the given temperature.
// threshold should be of the format "<20" or ">20".
func NewTemperatureThreshold(threshold string, causeReboot, sendEmail bool) (*Threshold, error) {
comp := FloatComparatorFromString(threshold)
number, err := strconv.ParseFloat(threshold[1:], 64)
if err != nil {
return nil, fmt.Errorf("unknown threshold found %s, a threshold must have a first character of '>|<' followed by a number: %s", threshold, err)
}
return &Threshold{
Check: func(stats *Statistics) []error {
var errors []error
for i, temp := range stats.GpuTemperatures {
glog.V(2).Infof("GPU %d temperature %0.2f", i, temp)
if comp(temp, number) {
errors = append(errors, fmt.Errorf("GPU %d temperature threshold exceeded %0.2f%s", i, temp, threshold))
}
}
return errors
},
Threshold: threshold,
CauseReboot: causeReboot,
SendEmail: sendEmail,
Name: "Temp",
}, nil
}
// NewFanPercentThreshold returns a Threshold that will check if a client has exceeded the given fan percent.
// threshold should be of the format "<20" or ">20".
func NewFanPercentThreshold(threshold string, causeReboot, sendEmail bool) (*Threshold, error) {
comp := FloatComparatorFromString(threshold)
number, err := strconv.ParseFloat(threshold[1:], 64)
if err != nil {
return nil, fmt.Errorf("unknown threshold found %s, a threshold must have a first character of '>|<' followed by a number: %s", threshold, err)
}
return &Threshold{
Check: func(stats *Statistics) []error {
var errors []error
for i, fp := range stats.GpuFanPercents {
glog.V(2).Infof("GPU %d fan percent %0.2f", i, fp)
if comp(fp, number) {
errors = append(errors, fmt.Errorf("GPU %d fan percent threshold exceeded %0.2f%s", i, fp, threshold))
}
}
return errors
},
Threshold: threshold,
CauseReboot: causeReboot,
SendEmail: sendEmail,
Name: "FanPercent",
}, nil
}