/
nms.go
109 lines (94 loc) · 2.91 KB
/
nms.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
package data
import (
"fmt"
"log"
"github.com/tidwall/gjson"
)
/*Filter is like Array.filter from JS, probably. Blatantly stolen from:
https://stackoverflow.com/questions/37562873/most-idiomatic-way-to-select-elements-from-an-array-in-golang
*/
func filterDataPoints(ss []DataPoint, test func(DataPoint) bool) (ret []DataPoint) {
for _, s := range ss {
if test(s) {
ret = append(ret, s)
}
}
return
}
func getPDUs() (ids []string) {
headers = make(map[string]string)
headers["X-Auth-Token"] = apiKey
b, err := request(baseURL, "api", "v0", "devicegroups", "PDUs")
if err != nil {
panic(err)
}
d := gjson.Parse(b)
devices := d.Get("devices")
devices.ForEach(func(idx, value gjson.Result) bool {
ids = append(ids, value.Get("device_id").String())
return true
})
return ids
}
func getSensors(deviceIDs []string) []DataPoint {
var data []DataPoint
for _, id := range deviceIDs {
d, err := request(baseURL, "api", "v0", "devices", id)
if err != nil {
panic(err)
}
device := gjson.Parse(d)
deviceName := device.Get("devices.0.sysName").Str
deviceLoc := device.Get("devices.0.location").Str
bCurrent, err := request(baseURL, "api", "v0", "devices", id, "health", "device_current")
if err != nil {
panic(err)
}
bVoltage, err := request(baseURL, "api", "v0", "devices", id, "health", "device_voltage")
if err != nil {
panic(err)
}
dCurrent := gjson.Parse(bCurrent)
dVoltage := gjson.Parse(bVoltage)
sCurrent := dCurrent.Get("graphs.0.sensor_id").Int()
sVoltage := dVoltage.Get("graphs.0.sensor_id").Int()
sCurrentData, err := request(baseURL, "api", "v0", "devices", id, "health", "device_current", fmt.Sprintf("%d", sCurrent))
if err != nil {
panic(err)
}
sVoltageData, err := request(baseURL, "api", "v0", "devices", id, "health", "device_voltage", fmt.Sprintf("%d", sVoltage))
if err != nil {
panic(err)
}
currentValue := gjson.Parse(sCurrentData).Get("graphs.0.sensor_current").Num
voltageValue := gjson.Parse(sVoltageData).Get("graphs.0.sensor_current").Num
point := DataPoint{device: deviceName, location: deviceLoc, current: currentValue, voltage: voltageValue}
data = append(data, point)
}
return data
}
func GetData() (map[string]LocationData, map[string][]DataPoint) {
ids := getPDUs()
data := getSensors(ids)
locations := make(map[string]LocationData)
locationData := make(map[string][]DataPoint)
for _, pdu := range data {
value := LocationData{current: 0, voltage: 0}
locations[pdu.location] = value
}
for loc := range locations {
points := filterDataPoints(data, func(d DataPoint) bool {
if d.location == loc {
return true
}
return false
})
for _, p := range points {
value := LocationData{current: locations[loc].current + p.current, voltage: locations[loc].voltage + p.voltage}
locations[loc] = value
}
locationData[loc] = points
}
log.Println("Finished gathering data from LibreNMS")
return locations, locationData
}