forked from kerwenwwer/pwrstat-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
157 lines (138 loc) · 4.09 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"flag"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/kerwenwwer/gopwrstat"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
LoadDesc = prometheus.NewDesc(
"ups_load",
"UPS power load (Watt)",
[]string{"device"},
nil)
StateDesc = prometheus.NewDesc(
"ups_state",
"UPS status (1 -> Normal, 0 -> Not)",
[]string{"device"},
nil)
BatteryDesc = prometheus.NewDesc(
"ups_battery_capacity",
"UPS battery capacity(%)",
[]string{"device"},
nil)
RtimeDesc = prometheus.NewDesc(
"ups_remaining_runtime",
"UPS Remaining Runtime(min)",
[]string{"device"},
nil)
InVoltageDesc = prometheus.NewDesc(
"ups_in_voltage",
"UPS Input Voltage(V): pass-> 1 non_pass -> 0",
[]string{"device"},
nil)
OutVoltageDesc = prometheus.NewDesc(
"ups_out_voltage",
"UPS Output Voltage(V): pass-> 1 non_pass -> 0",
[]string{"device"},
nil)
TestDesc = prometheus.NewDesc(
"ups_test_result",
"UPS Test Result",
[]string{"device"},
nil)
)
type (
PwrstatCollector struct{}
)
func NewPwrstatCollector() *PwrstatCollector {
return &PwrstatCollector{}
}
func (l *PwrstatCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- LoadDesc
ch <- StateDesc
ch <- BatteryDesc
ch <- RtimeDesc
ch <- OutVoltageDesc
ch <- TestDesc
}
func (l *PwrstatCollector) Collect(ch chan<- prometheus.Metric) {
status, err := gopwrstat.NewFromSystem()
if err != nil {
panic(err)
}
for k, v := range status.Status {
if k == "Load" {
value_arr := strings.Fields(v)
if value, err := strconv.ParseFloat(value_arr[0], 64); err == nil {
ch <- prometheus.MustNewConstMetric(LoadDesc,
prometheus.GaugeValue, value, status.Status["Model Name"])
}
} else if k == "State" {
var state = 0
if v == "Normal" {
state = 1
}
ch <- prometheus.MustNewConstMetric(StateDesc,
prometheus.GaugeValue, float64(state), status.Status["Model Name"])
} else if k == "Battery Capacity" {
value_arr := strings.Fields(v)
if value, err := strconv.ParseFloat(value_arr[0], 64); err == nil {
ch <- prometheus.MustNewConstMetric(BatteryDesc,
prometheus.GaugeValue, value, status.Status["Model Name"])
}
} else if k == "Remaining Runtime" {
value_arr := strings.Fields(v)
if value, err := strconv.ParseFloat(value_arr[0], 64); err == nil {
ch <- prometheus.MustNewConstMetric(RtimeDesc,
prometheus.GaugeValue, value, status.Status["Model Name"])
}
} else if k == "Utility Voltage" {
value_arr := strings.Fields(v)
if value, err := strconv.ParseFloat(value_arr[0], 64); err == nil {
ch <- prometheus.MustNewConstMetric(InVoltageDesc,
prometheus.GaugeValue, value, status.Status["Model Name"])
}
} else if k == "Output Voltage" {
value_arr := strings.Fields(v)
if value, err := strconv.ParseFloat(value_arr[0], 64); err == nil {
ch <- prometheus.MustNewConstMetric(OutVoltageDesc,
prometheus.GaugeValue, value, status.Status["Model Name"])
}
} else if k == "Test Result" {
value_arr := strings.Fields(v)
if value_arr[0] == "Passed" {
ch <- prometheus.MustNewConstMetric(TestDesc,
prometheus.GaugeValue, 1, status.Status["Model Name"])
} else {
ch <- prometheus.MustNewConstMetric(TestDesc,
prometheus.GaugeValue, 0, status.Status["Model Name"])
}
}
}
}
func main() {
var (
listenAddress = flag.String("web.listen-address", ":8088", "Address on which to expose metrics and web interface.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
)
flag.Parse()
fmt.Printf("Info:: \n Address: http://localhost%v%v \n::", *listenAddress, *metricsPath)
pwrstatCollector := NewPwrstatCollector()
prometheus.MustRegister(pwrstatCollector)
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Sensor Exporter</title></head>
<body>
<h1>Sensor Exporter</h1>
<p><a href="` + *metricsPath + `">Metrics</a></p>
</body>
</html>`))
})
http.ListenAndServe(*listenAddress, nil)
}