forked from jordyv/prometheus-p1-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
134 lines (122 loc) · 4.49 KB
/
main.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
package main
import (
"flag"
"net/http"
"os"
"strconv"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
"github.com/skoef/gop1"
)
var listenAddr string
var verbose bool
var metricNamePrefix = "p1_"
var (
registry = prometheus.NewRegistry()
electricityUsageHighMetric = prometheus.NewGauge(prometheus.GaugeOpts{
Name: metricNamePrefix + "usage_electricity_high",
Help: "Electricity usage high tariff",
})
electricityUsageLowMetric = prometheus.NewGauge(prometheus.GaugeOpts{
Name: metricNamePrefix + "usage_electricity_low",
Help: "Electricity usage low tariff",
})
electricityReturnedHighMetric = prometheus.NewGauge(prometheus.GaugeOpts{
Name: metricNamePrefix + "returned_electricity_high",
Help: "Electricity returned high tariff",
})
electricityReturnedLowMetric = prometheus.NewGauge(prometheus.GaugeOpts{
Name: metricNamePrefix + "returned_electricity_low",
Help: "Electricity returned low tariff",
})
actualElectricityDeliveredMetric = prometheus.NewGauge(prometheus.GaugeOpts{
Name: metricNamePrefix + "actual_electricity_delivered",
Help: "Actual electricity power delivered to client",
})
actualElectricityGeneratedMetric = prometheus.NewGauge(prometheus.GaugeOpts{
Name: metricNamePrefix + "actual_electricity_generated",
Help: "Actual electricity power generated by client",
})
activeTarrifMetric = prometheus.NewGauge(prometheus.GaugeOpts{
Name: metricNamePrefix + "active_tariff",
Help: "Active tariff",
})
powerFailuresLongMetric = prometheus.NewGauge(prometheus.GaugeOpts{
Name: metricNamePrefix + "power_failures_long",
Help: "Power failures long",
})
powerFailuresShortMetric = prometheus.NewGauge(prometheus.GaugeOpts{
Name: metricNamePrefix + "power_failures_short",
Help: "Power failures short",
})
gasUsageMetric = prometheus.NewGauge(prometheus.GaugeOpts{
Name: metricNamePrefix + "usage_gas",
Help: "Gas usage",
})
)
func init() {
registry.MustRegister(electricityUsageHighMetric)
registry.MustRegister(electricityUsageLowMetric)
registry.MustRegister(electricityReturnedHighMetric)
registry.MustRegister(electricityReturnedLowMetric)
registry.MustRegister(actualElectricityDeliveredMetric)
registry.MustRegister(actualElectricityGeneratedMetric)
registry.MustRegister(activeTarrifMetric)
registry.MustRegister(powerFailuresLongMetric)
registry.MustRegister(powerFailuresShortMetric)
registry.MustRegister(gasUsageMetric)
}
func floatValue(input string) (fval float64) {
fval, _ = strconv.ParseFloat(input, 64)
return
}
func main() {
flag.StringVar(&listenAddr, "listen", "127.0.0.1:8888", "Listen address for HTTP metrics")
flag.BoolVar(&verbose, "verbose", false, "Verbose output logging")
flag.Parse()
p1, err := gop1.New(gop1.P1Config{
USBDevice: "/dev/ttyUSB0",
})
if err != nil {
logrus.Errorln("Quitting because of error opening p1", err)
os.Exit(1)
}
// Start
p1.Start()
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
if verbose {
logrus.SetLevel(logrus.DebugLevel)
}
go func() {
for tgram := range p1.Incoming {
for _, obj := range tgram.Objects {
switch obj.Type {
case gop1.OBISTypeElectricityDeliveredTariff1:
electricityUsageLowMetric.Set(floatValue(obj.Values[0].Value))
case gop1.OBISTypeElectricityDeliveredTariff2:
electricityUsageHighMetric.Set(floatValue(obj.Values[0].Value))
case gop1.OBISTypeElectricityGeneratedTariff1:
electricityReturnedLowMetric.Set(floatValue(obj.Values[0].Value))
case gop1.OBISTypeElectricityGeneratedTariff2:
electricityReturnedHighMetric.Set(floatValue(obj.Values[0].Value))
case gop1.OBISTypeElectricityDelivered:
actualElectricityDeliveredMetric.Set(floatValue(obj.Values[0].Value))
case gop1.OBISTypeElectricityGenerated:
actualElectricityGeneratedMetric.Set(floatValue(obj.Values[0].Value))
case gop1.OBISTypeNumberOfPowerFailures:
powerFailuresShortMetric.Set(floatValue(obj.Values[0].Value))
case gop1.OBISTypeNumberOfLongPowerFailures:
powerFailuresLongMetric.Set(floatValue(obj.Values[0].Value))
case gop1.OBISTypeElectricityTariffIndicator:
activeTarrifMetric.Set(floatValue(obj.Values[0].Value))
case gop1.OBISTypeGasDelivered:
gasUsageMetric.Set(floatValue(obj.Values[0].Value))
}
}
}
}()
logrus.Infoln("Start listening at", listenAddr)
http.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))
logrus.Fatalln(http.ListenAndServe(listenAddr, nil))
}