-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
173 lines (148 loc) · 4 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"flag"
"fmt"
"net/http"
"strconv"
"strings"
gopwrstat "github.com/kerwenwwer/pwrstat-exporter/pwrstat"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
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.")
// Metric Descriptors
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,
)
)
func init() {
flag.Parse()
}
func main() {
fmt.Printf("Info: Serving metrics on http://localhost%s%s\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>UPS Exporter</title></head>
<body>
<h1>UPS Exporter</h1>
<p><a href="` + *metricsPath + `">Metrics</a></p>
</body>
</html>`))
})
err := http.ListenAndServe(*listenAddress, nil)
if err != nil {
fmt.Printf("Error starting server: %s\n", err)
return
}
}
type PwrstatCollector struct{}
func NewPwrstatCollector() *PwrstatCollector {
return &PwrstatCollector{}
}
func (collector *PwrstatCollector) Describe(ch chan<- *prometheus.Desc) {
prometheus.DescribeByCollect(collector, ch)
}
func (collector *PwrstatCollector) Collect(ch chan<- prometheus.Metric) {
status, err := gopwrstat.NewFromSystem()
if err != nil {
fmt.Printf("Error fetching UPS status: %s\n", err)
return
}
parseAndCollectMetrics(status, ch)
}
func parseAndCollectMetrics(status *gopwrstat.Pwrstat, ch chan<- prometheus.Metric) {
model := status.Status["Model Name"]
for k, v := range status.Status {
switch k {
case "Load", "Battery Capacity", "Remaining Runtime", "Utility Voltage", "Output Voltage":
collectNumericMetric(k, v, model, ch)
case "State":
collectStateMetric(v, model, ch)
case "Test Result":
collectTestResultMetric(v, model, ch)
}
}
}
func collectNumericMetric(metricType, valueStr, model string, ch chan<- prometheus.Metric) {
valueFields := strings.Fields(valueStr)
if value, err := strconv.ParseFloat(valueFields[0], 64); err == nil {
desc := getMetricDesc(metricType)
if desc != nil {
ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, value, model)
}
}
}
func collectStateMetric(value, model string, ch chan<- prometheus.Metric) {
state := 0.0
if value == "Normal" {
state = 1.0
}
ch <- prometheus.MustNewConstMetric(StateDesc, prometheus.GaugeValue, state, model)
}
func collectTestResultMetric(value, model string, ch chan<- prometheus.Metric) {
result := 0.0
if value == "Passed" {
result = 1.0
}
ch <- prometheus.MustNewConstMetric(TestDesc, prometheus.GaugeValue, result, model)
}
func getMetricDesc(metricType string) *prometheus.Desc {
switch metricType {
case "Load":
return LoadDesc
case "Battery Capacity":
return BatteryDesc
case "Remaining Runtime":
return RtimeDesc
case "Utility Voltage":
return InVoltageDesc
case "Output Voltage":
return OutVoltageDesc
default:
return nil
}
}