-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathups.go
306 lines (284 loc) · 8.96 KB
/
ups.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package nut
import (
"fmt"
"strconv"
"strings"
"regexp"
)
// UPS contains information about a specific UPS provided by the NUT instance.
type UPS struct {
Name string
Description string
Master bool
NumberOfLogins int
Clients []string
Variables []Variable
Commands []Command
nutClient *Client
}
// Variable describes a single variable related to a UPS.
type Variable struct {
Name string
Value interface{}
Type string
Description string
Writeable bool
MaximumLength int
OriginalType string
}
// Command describes an available command for a UPS.
type Command struct {
Name string
Description string
}
// NewUPS takes a UPS name and NUT client and returns an instantiated UPS struct.
func NewUPS(name string, client *Client) (UPS, error) {
newUPS := UPS{
Name: name,
nutClient: client,
}
_, err := newUPS.GetClients()
if err != nil {
return newUPS, err
}
_, err = newUPS.GetCommands()
if err != nil {
return newUPS, err
}
_, err = newUPS.GetDescription()
if err != nil {
return newUPS, err
}
_, err = newUPS.GetNumberOfLogins()
if err != nil {
return newUPS, err
}
_, err = newUPS.GetVariables()
if err != nil {
return newUPS, err
}
return newUPS, err
}
// GetNumberOfLogins returns the number of clients which have done LOGIN for this UPS.
func (u *UPS) GetNumberOfLogins() (int, error) {
resp, err := u.nutClient.SendCommand(fmt.Sprintf("GET NUMLOGINS %s", u.Name))
if err != nil {
return 0, err
}
atoi, err := strconv.Atoi(strings.TrimPrefix(resp[0], fmt.Sprintf("NUMLOGINS %s ", u.Name)))
if err != nil {
return 0, err
}
u.NumberOfLogins = atoi
return atoi, nil
}
// GetClients returns a list of NUT clients.
func (u *UPS) GetClients() ([]string, error) {
clientsList := []string{}
resp, err := u.nutClient.SendCommand(fmt.Sprintf("LIST CLIENT %s", u.Name))
if err != nil {
return clientsList, err
}
linePrefix := fmt.Sprintf("CLIENT %s ", u.Name)
for _, line := range resp[1 : len(resp)-1] {
clientsList = append(clientsList, strings.TrimPrefix(line, linePrefix))
}
u.Clients = clientsList
return clientsList, nil
}
// CheckIfMaster returns true if the session is authenticated with the master permission set.
func (u *UPS) CheckIfMaster() (bool, error) {
resp, err := u.nutClient.SendCommand(fmt.Sprintf("MASTER %s", u.Name))
if err != nil {
return false, err
}
if resp[0] == "OK" {
u.Master = true
return true, nil
}
return false, nil
}
// GetDescription the value of "desc=" from ups.conf for this UPS. If it is not set, upsd will return "Unavailable".
func (u *UPS) GetDescription() (string, error) {
resp, err := u.nutClient.SendCommand(fmt.Sprintf("GET UPSDESC %s", u.Name))
if err != nil {
return "", err
}
description := strings.TrimPrefix(strings.Replace(resp[0], `"`, "", -1), fmt.Sprintf(`UPSDESC %s `, u.Name))
u.Description = description
return description, nil
}
// GetVariables returns a slice of Variable structs for the UPS.
func (u *UPS) GetVariables() ([]Variable, error) {
vars := []Variable{}
resp, err := u.nutClient.SendCommand(fmt.Sprintf("LIST VAR %s", u.Name))
if err != nil {
return vars, err
}
offset := fmt.Sprintf("VAR %s ", u.Name)
for _, line := range resp[1 : len(resp)-1] {
newVar := Variable{}
cleanedLine := strings.TrimPrefix(line, offset)
splitLine := strings.Split(cleanedLine, `"`)
splitLine[1] = strings.Trim(splitLine[1], " ")
newVar.Name = strings.TrimSuffix(splitLine[0], " ")
newVar.Value = splitLine[1]
description, err := u.GetVariableDescription(newVar.Name)
if err != nil {
return vars, err
}
newVar.Description = description
varType, writeable, maximumLength, err := u.GetVariableType(newVar.Name)
if err != nil {
return vars, err
}
newVar.Type = varType
newVar.Writeable = writeable
newVar.MaximumLength = maximumLength
if splitLine[1] == "enabled" {
newVar.Value = true
newVar.Type = "BOOLEAN"
}
if splitLine[1] == "disabled" {
newVar.Value = false
newVar.Type = "BOOLEAN"
}
matched, _ := regexp.MatchString(`^-?[0-9\.]+$`, splitLine[1])
if matched {
if strings.Count(splitLine[1], ".") == 1 {
converted, err := strconv.ParseFloat(splitLine[1], 64)
if err == nil {
newVar.Value = converted
newVar.Type = "FLOAT_64"
newVar.OriginalType = varType
}
} else {
converted, err := strconv.ParseInt(splitLine[1], 10, 64)
if err == nil {
newVar.Value = converted
newVar.Type = "INTEGER"
newVar.OriginalType = varType
}
}
}
/* Failed conversion or not a numeric value - coax to STRING */
if err != nil || !matched {
newVar.Type = "STRING"
newVar.OriginalType = varType
}
vars = append(vars, newVar)
}
u.Variables = vars
return vars, nil
}
// GetVariableDescription returns a string that gives a brief explanation for the given variableName.
// upsd may return "Unavailable" if the file which provides this description is not installed.
func (u *UPS) GetVariableDescription(variableName string) (string, error) {
resp, err := u.nutClient.SendCommand(fmt.Sprintf("GET DESC %s %s", u.Name, variableName))
if err != nil {
return "", err
}
trimmedLine := strings.TrimPrefix(resp[0], fmt.Sprintf("DESC %s %s ", u.Name, variableName))
description := strings.Replace(trimmedLine, `"`, "", -1)
return description, nil
}
// GetVariableType returns the variable type, writeability and maximum length for the given variableName.
func (u *UPS) GetVariableType(variableName string) (string, bool, int, error) {
resp, err := u.nutClient.SendCommand(fmt.Sprintf("GET TYPE %s %s", u.Name, variableName))
if err != nil {
return "UNKNOWN", false, -1, err
}
trimmedLine := strings.TrimPrefix(resp[0], fmt.Sprintf("TYPE %s %s ", u.Name, variableName))
splitLine := strings.Split(trimmedLine, " ")
writeable := (splitLine[0] == "RW")
varType := "UNKNOWN"
maximumLength := 0
if writeable {
varType = splitLine[1]
if strings.HasPrefix(varType, "STRING:") {
splitType := strings.Split(varType, ":")
varType = splitType[0]
maximumLength, err = strconv.Atoi(splitType[1])
if err != nil {
return varType, writeable, -1, err
}
}
} else {
varType = splitLine[0]
}
return varType, writeable, maximumLength, nil
}
// GetCommands returns a slice of Command structs for the UPS.
func (u *UPS) GetCommands() ([]Command, error) {
commandsList := []Command{}
resp, err := u.nutClient.SendCommand(fmt.Sprintf("LIST CMD %s", u.Name))
if err != nil {
return commandsList, err
}
linePrefix := fmt.Sprintf("CMD %s ", u.Name)
for _, line := range resp[1 : len(resp)-1] {
cmdName := strings.TrimPrefix(line, linePrefix)
cmd := Command{
Name: cmdName,
}
description, err := u.GetCommandDescription(cmdName)
if err != nil {
return commandsList, err
}
cmd.Description = description
commandsList = append(commandsList, cmd)
}
u.Commands = commandsList
return commandsList, nil
}
// GetCommandDescription returns a string that gives a brief explanation for the given commandName.
func (u *UPS) GetCommandDescription(commandName string) (string, error) {
resp, err := u.nutClient.SendCommand(fmt.Sprintf("GET CMDDESC %s %s", u.Name, commandName))
if err != nil {
return "", err
}
trimmedLine := strings.TrimPrefix(resp[0], fmt.Sprintf("CMDDESC %s %s ", u.Name, commandName))
description := strings.Replace(trimmedLine, `"`, "", -1)
return description, err
}
// SetVariable sets the given variableName to the given value on the UPS.
func (u *UPS) SetVariable(variableName, value string) (bool, error) {
resp, err := u.nutClient.SendCommand(fmt.Sprintf(`SET VAR %s %s "%s"`, u.Name, variableName, value))
if err != nil {
return false, err
}
if resp[0] == "OK" {
return true, nil
}
return false, nil
}
// SendCommand sends a command to the UPS.
func (u *UPS) SendCommand(commandName string) (bool, error) {
resp, err := u.nutClient.SendCommand(fmt.Sprintf("INSTCMD %s %s", u.Name, commandName))
if err != nil {
return false, err
}
if resp[0] == "OK" {
return true, nil
}
return false, nil
}
// ForceShutdown sets the FSD flag on the UPS.
//
// This requires "upsmon master" in upsd.users, or "FSD" action granted in upsd.users
//
// upsmon in master mode is the primary user of this function. It sets this "forced shutdown" flag on any UPS when it plans to power it off. This is done so that slave systems will know about it and shut down before the power disappears.
//
// Setting this flag makes "FSD" appear in a STATUS request for this UPS. Finding "FSD" in a status request should be treated just like a "OB LB".
//
// It should be noted that FSD is currently a latch - once set, there is no way to clear it short of restarting upsd or dropping then re-adding it in the ups.conf. This may cause issues when upsd is running on a system that is not shut down due to the UPS event.
func (u *UPS) ForceShutdown() (bool, error) {
resp, err := u.nutClient.SendCommand(fmt.Sprintf("FSD %s", u.Name))
if err != nil {
return false, err
}
if resp[0] == "OK FSD-SET" {
return true, nil
}
return false, nil
}