mirror of
https://github.com/nshttpd/mikrotik-exporter.git
synced 2025-02-01 22:31:39 +01:00
88 lines
2.2 KiB
Go
88 lines
2.2 KiB
Go
package collector
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type PromMetrics struct {
|
|
InterfaceMetrics map[string]*prometheus.CounterVec
|
|
ResourceMetrics map[string]*prometheus.GaugeVec
|
|
}
|
|
|
|
func (p *PromMetrics) makeLabels(name, address string) prometheus.Labels {
|
|
labels := make(prometheus.Labels)
|
|
labels["name"] = metricStringCleanup(name)
|
|
labels["address"] = metricStringCleanup(address)
|
|
return labels
|
|
}
|
|
|
|
func (p *PromMetrics) makeInterfaceLabels(name, address, intf string) prometheus.Labels {
|
|
l := p.makeLabels(name, address)
|
|
l["interface"] = intf
|
|
return l
|
|
}
|
|
|
|
func (p *PromMetrics) SetupPrometheus(l zap.SugaredLogger) (http.Handler, error) {
|
|
|
|
p.InterfaceMetrics = make(map[string]*prometheus.CounterVec)
|
|
p.ResourceMetrics = make(map[string]*prometheus.GaugeVec)
|
|
|
|
for _, v := range InterfaceProps {
|
|
n := metricStringCleanup(v)
|
|
c := prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
Namespace: namespace,
|
|
Subsystem: "interface",
|
|
Name: n,
|
|
Help: fmt.Sprintf("Interface %s counter", v),
|
|
}, interfaceLabelNames)
|
|
|
|
if err := prometheus.Register(c); err != nil {
|
|
l.Errorw("error creating interface counter vector",
|
|
"property", v,
|
|
"error", err,
|
|
)
|
|
return nil, err
|
|
}
|
|
|
|
p.InterfaceMetrics[v] = c
|
|
|
|
}
|
|
|
|
for _, v := range ResourceProps {
|
|
n := metricStringCleanup(v)
|
|
c := prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
|
Namespace: namespace,
|
|
Subsystem: "resource",
|
|
Name: n,
|
|
Help: fmt.Sprintf("Resource %s counter", v),
|
|
}, resourceLabelNames)
|
|
|
|
if err := prometheus.Register(c); err != nil {
|
|
l.Errorw("error creating resource counter vec",
|
|
"property", v,
|
|
"error", err,
|
|
)
|
|
return nil, err
|
|
}
|
|
p.ResourceMetrics[v] = c
|
|
}
|
|
|
|
return promhttp.Handler(), nil
|
|
|
|
}
|
|
|
|
func (p *PromMetrics) IncrementInterface(prop, name, address, intf string, cnt float64) {
|
|
l := p.makeInterfaceLabels(name, address, intf)
|
|
p.InterfaceMetrics[prop].With(l).Add(cnt)
|
|
}
|
|
|
|
func (p *PromMetrics) UpdateResource(res, name, address string, v float64) {
|
|
l := p.makeLabels(name, address)
|
|
p.ResourceMetrics[res].With(l).Set(v)
|
|
}
|