|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "net/http" |
| 6 | + "net/url" |
| 7 | + "os" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/prometheus/client_golang/prometheus" |
| 12 | + "github.com/prometheus/client_golang/prometheus/promhttp" |
| 13 | + |
| 14 | + "github.com/nerd2/neohubgo" |
| 15 | +) |
| 16 | + |
| 17 | +type neoCollector struct { |
| 18 | + currentTemp *prometheus.Desc |
| 19 | + targetTemp *prometheus.Desc |
| 20 | + isHeating *prometheus.Desc |
| 21 | +} |
| 22 | + |
| 23 | +func newNeoCollector() *neoCollector { |
| 24 | + return &neoCollector{ |
| 25 | + currentTemp: prometheus.NewDesc("current_temp", |
| 26 | + "The current temperature of the zone", |
| 27 | + []string{"zone", "device"}, nil, |
| 28 | + ), |
| 29 | + targetTemp: prometheus.NewDesc("target_temp", |
| 30 | + "The target temperature of the zone", |
| 31 | + []string{"zone", "device"}, nil, |
| 32 | + ), |
| 33 | + isHeating: prometheus.NewDesc("is_heating", |
| 34 | + "Whether the current zone is heating", |
| 35 | + []string{"zone", "device"}, nil, |
| 36 | + ), |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func (collector *neoCollector) Describe(ch chan<- *prometheus.Desc) { |
| 41 | + |
| 42 | + ch <- collector.currentTemp |
| 43 | + ch <- collector.targetTemp |
| 44 | + ch <- collector.isHeating |
| 45 | +} |
| 46 | + |
| 47 | +// Collect implements required collect function for all promehteus collectors |
| 48 | +func (collector *neoCollector) Collect(ch chan<- prometheus.Metric) { |
| 49 | + |
| 50 | + username := os.Getenv("NEOPROM_USERNAME") |
| 51 | + password := os.Getenv("NEOPROM_PASSWORD") |
| 52 | + |
| 53 | + nh := neohubgo.NewNeoHub(&neohubgo.Options{Username: username, Password: password}) |
| 54 | + devices, err := nh.Login() |
| 55 | + if err != nil { |
| 56 | + log.Println("Error : couldn't connect -> ", err) |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + for _, device := range devices { |
| 61 | + if device.Online { |
| 62 | + deviceName, _ := url.QueryUnescape(strings.Trim(device.DeviceName, " ")) |
| 63 | + |
| 64 | + data, err := nh.GetData(device.DeviceId) |
| 65 | + if err != nil { |
| 66 | + // If we've got this far, this may be recoverable and we may be able to query other devices |
| 67 | + log.Println("Warn : Unable to query device -> ", err) |
| 68 | + } |
| 69 | + |
| 70 | + for _, liveDev := range data.CacheValue.LiveInfo.Devices { |
| 71 | + actualTemp, _ := strconv.ParseFloat(liveDev.ActualTemp, 64) |
| 72 | + targetTemp, _ := strconv.ParseFloat(liveDev.SetTemp, 64) |
| 73 | + zoneName, _ := url.QueryUnescape(strings.Trim(liveDev.ZoneName, " ")) |
| 74 | + |
| 75 | + var isHeating float64 |
| 76 | + if liveDev.HeatOn { |
| 77 | + isHeating = 1.0 |
| 78 | + } else { |
| 79 | + isHeating = 0.0 |
| 80 | + } |
| 81 | + |
| 82 | + ch <- prometheus.MustNewConstMetric(collector.currentTemp, prometheus.GaugeValue, actualTemp, zoneName, deviceName) |
| 83 | + ch <- prometheus.MustNewConstMetric(collector.targetTemp, prometheus.GaugeValue, targetTemp, zoneName, deviceName) |
| 84 | + ch <- prometheus.MustNewConstMetric(collector.isHeating, prometheus.GaugeValue, isHeating, zoneName, deviceName) |
| 85 | + |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func main() { |
| 92 | + neo := newNeoCollector() |
| 93 | + prometheus.MustRegister(neo) |
| 94 | + port := "9101" |
| 95 | + log.Printf("Starting metrics server on port %s", port) |
| 96 | + http.Handle("/metrics", promhttp.Handler()) |
| 97 | + log.Fatal(http.ListenAndServe(":"+port, nil)) |
| 98 | +} |
0 commit comments