-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.go
64 lines (53 loc) · 1.59 KB
/
http.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
package main
import (
"encoding/json"
"fmt"
"net/http"
)
func getStatusResponseFromDevice(config configuration, d device) (*StatusResponse, error) {
httpClient := &http.Client{Timeout: config.RequestTimeout}
request, err := http.NewRequest("GET", d.getStatusURL(), nil)
if err != nil {
return nil, fmt.Errorf("could not create request: %v", err)
}
if d.Username != "" && d.Password != "" {
request.SetBasicAuth(d.Username, d.Password)
}
response, err := httpClient.Do(request)
if err != nil {
return nil, fmt.Errorf("error while doing the request for device '%s': %v", d.DisplayName, err)
}
defer response.Body.Close()
statusResponse := new(StatusResponse)
err = json.NewDecoder(response.Body).Decode(statusResponse)
if err != nil {
return nil, err
}
return statusResponse, nil
}
func bool2float64(b bool) float64 {
if b {
return 1
}
return 0
}
func fetchDevices(config configuration) {
for _, device := range config.Devices {
labels := map[string]string{
"name": device.DisplayName,
"mac": device.MACAddress,
"type": device.Type,
}
statusResponse, err := getStatusResponseFromDevice(config, device)
if err != nil {
fmt.Println(err)
errorCounter.With(labels).Inc()
continue
}
temperatureGauge.With(labels).Set(float64(statusResponse.Temperature))
isOvertemperatureGauge.With(labels).Set(bool2float64(statusResponse.Overtemperature))
voltageGauge.With(labels).Set(float64(statusResponse.Voltage))
uptimeGauge.With(labels).Set(float64(statusResponse.Uptime))
isUpdateAvailableGauge.With(labels).Set(bool2float64(statusResponse.HasUpdate))
}
}