mirror of
https://github.com/nikdoof/hg612-exporter.git
synced 2025-12-14 02:32:15 +00:00
init
This commit is contained in:
54
internal/metrics/collector.go
Normal file
54
internal/metrics/collector.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/jakekeeys/hg612-exporter/pkg/hg612"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Collector interface {
|
||||
Collect()
|
||||
Start()
|
||||
Stop()
|
||||
}
|
||||
|
||||
type MetricsCollector struct {
|
||||
ctx context.Context
|
||||
collectIntervalSeconds int
|
||||
dslMetricsCollector dslMetricsCollector
|
||||
}
|
||||
|
||||
func New(client hg612.Client, host string, collectIntervalSeconds int) Collector {
|
||||
return MetricsCollector{
|
||||
collectIntervalSeconds: collectIntervalSeconds,
|
||||
ctx: context.Background(),
|
||||
dslMetricsCollector: newDSLMetricsCollector(client, host),
|
||||
}
|
||||
}
|
||||
|
||||
func (c MetricsCollector) Collect() {
|
||||
err := c.dslMetricsCollector.collect()
|
||||
if err != nil {
|
||||
logrus.Error(errors.Wrap(err, "error collecting dsl metrics"))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (c MetricsCollector) Start() {
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-time.After(time.Second * time.Duration(c.collectIntervalSeconds)):
|
||||
c.Collect()
|
||||
case <-c.ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (c MetricsCollector) Stop() {
|
||||
c.ctx.Done()
|
||||
}
|
||||
291
internal/metrics/dsl.go
Normal file
291
internal/metrics/dsl.go
Normal file
@@ -0,0 +1,291 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"github.com/jakekeeys/hg612-exporter/pkg/hg612"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
)
|
||||
|
||||
type dslMetricsCollector struct {
|
||||
client hg612.Client
|
||||
host string
|
||||
|
||||
status *prometheus.GaugeVec
|
||||
|
||||
upCurrRate *prometheus.GaugeVec
|
||||
downCurrRate *prometheus.GaugeVec
|
||||
upCurrRate2 *prometheus.GaugeVec
|
||||
downCurrRate2 *prometheus.GaugeVec
|
||||
upMaxRate *prometheus.GaugeVec
|
||||
downMaxRate *prometheus.GaugeVec
|
||||
upSNR *prometheus.GaugeVec
|
||||
downSNR *prometheus.GaugeVec
|
||||
upAttenuation *prometheus.GaugeVec
|
||||
downAttenuation *prometheus.GaugeVec
|
||||
upPower *prometheus.GaugeVec
|
||||
downPower *prometheus.GaugeVec
|
||||
|
||||
downHEC *prometheus.GaugeVec
|
||||
upHEC *prometheus.GaugeVec
|
||||
downCRC *prometheus.GaugeVec
|
||||
upCRC *prometheus.GaugeVec
|
||||
downFEC *prometheus.GaugeVec
|
||||
upFEC *prometheus.GaugeVec
|
||||
downHEC2 *prometheus.GaugeVec
|
||||
upHEC2 *prometheus.GaugeVec
|
||||
downCRC2 *prometheus.GaugeVec
|
||||
upCRC2 *prometheus.GaugeVec
|
||||
downFEC2 *prometheus.GaugeVec
|
||||
upFEC2 *prometheus.GaugeVec
|
||||
}
|
||||
|
||||
func newDSLMetricsCollector(client hg612.Client, host string) dslMetricsCollector {
|
||||
status := promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "dsl",
|
||||
Name: "status",
|
||||
},
|
||||
[]string{"host", "status", "modulation", "dataPath"},
|
||||
)
|
||||
|
||||
upCurrRate := promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "dsl",
|
||||
Name: "up_current_rate",
|
||||
},
|
||||
[]string{"host"},
|
||||
)
|
||||
downCurrRate := promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "dsl",
|
||||
Name: "down_current_rate",
|
||||
},
|
||||
[]string{"host"},
|
||||
)
|
||||
upCurrRate2 := promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "dsl",
|
||||
Name: "up_current_rate_2",
|
||||
},
|
||||
[]string{"host"},
|
||||
)
|
||||
downCurrRate2 := promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "dsl",
|
||||
Name: "down_current_rate_2",
|
||||
},
|
||||
[]string{"host"},
|
||||
)
|
||||
upMaxRate := promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "dsl",
|
||||
Name: "up_max_rate",
|
||||
},
|
||||
[]string{"host"},
|
||||
)
|
||||
downMaxRate := promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "dsl",
|
||||
Name: "down_max_rate",
|
||||
},
|
||||
[]string{"host"},
|
||||
)
|
||||
upSNR := promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "dsl",
|
||||
Name: "up_snr",
|
||||
},
|
||||
[]string{"host"},
|
||||
)
|
||||
downSNR := promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "dsl",
|
||||
Name: "down_snr",
|
||||
},
|
||||
[]string{"host"},
|
||||
)
|
||||
upAttenuation := promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "dsl",
|
||||
Name: "up_attenuation",
|
||||
},
|
||||
[]string{"host"},
|
||||
)
|
||||
downAttenuation := promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "dsl",
|
||||
Name: "down_attenuation",
|
||||
},
|
||||
[]string{"host"},
|
||||
)
|
||||
upPower := promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "dsl",
|
||||
Name: "up_power",
|
||||
},
|
||||
[]string{"host"},
|
||||
)
|
||||
downPower := promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "dsl",
|
||||
Name: "down_power",
|
||||
},
|
||||
[]string{"host"},
|
||||
)
|
||||
|
||||
downHEC := promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "dsl",
|
||||
Name: "down_hec",
|
||||
},
|
||||
[]string{"host"},
|
||||
)
|
||||
upHEC := promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "dsl",
|
||||
Name: "up_hec",
|
||||
},
|
||||
[]string{"host"},
|
||||
)
|
||||
downCRC := promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "dsl",
|
||||
Name: "down_crc",
|
||||
},
|
||||
[]string{"host"},
|
||||
)
|
||||
upCRC := promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "dsl",
|
||||
Name: "up_crc",
|
||||
},
|
||||
[]string{"host"},
|
||||
)
|
||||
downFEC := promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "dsl",
|
||||
Name: "down_fec",
|
||||
},
|
||||
[]string{"host"},
|
||||
)
|
||||
upFEC := promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "dsl",
|
||||
Name: "up_fec",
|
||||
},
|
||||
[]string{"host"},
|
||||
)
|
||||
downHEC2 := promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "dsl",
|
||||
Name: "down_hec_2",
|
||||
},
|
||||
[]string{"host"},
|
||||
)
|
||||
upHEC2 := promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "dsl",
|
||||
Name: "up_hec_2",
|
||||
},
|
||||
[]string{"host"},
|
||||
)
|
||||
downCRC2 := promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "dsl",
|
||||
Name: "down_crc_2",
|
||||
},
|
||||
[]string{"host"},
|
||||
)
|
||||
upCRC2 := promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "dsl",
|
||||
Name: "up_crc_2",
|
||||
},
|
||||
[]string{"host"},
|
||||
)
|
||||
downFEC2 := promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "dsl",
|
||||
Name: "down_fec_2",
|
||||
},
|
||||
[]string{"host"},
|
||||
)
|
||||
upFEC2 := promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "dsl",
|
||||
Name: "up_fec_2",
|
||||
},
|
||||
[]string{"host"},
|
||||
)
|
||||
|
||||
return dslMetricsCollector{
|
||||
client: client,
|
||||
host: host,
|
||||
|
||||
status: status,
|
||||
|
||||
upCurrRate: upCurrRate,
|
||||
downCurrRate: downCurrRate,
|
||||
upCurrRate2: upCurrRate2,
|
||||
downCurrRate2: downCurrRate2,
|
||||
upMaxRate: upMaxRate,
|
||||
downMaxRate: downMaxRate,
|
||||
upSNR: upSNR,
|
||||
downSNR: downSNR,
|
||||
upAttenuation: upAttenuation,
|
||||
downAttenuation: downAttenuation,
|
||||
upPower: upPower,
|
||||
downPower: downPower,
|
||||
|
||||
downHEC: downHEC,
|
||||
upHEC: upHEC,
|
||||
downCRC: downCRC,
|
||||
upCRC: upCRC,
|
||||
downFEC: downFEC,
|
||||
upFEC: upFEC,
|
||||
downHEC2: downHEC2,
|
||||
upHEC2: upHEC2,
|
||||
downCRC2: downCRC2,
|
||||
upCRC2: upCRC2,
|
||||
downFEC2: downFEC2,
|
||||
upFEC2: upFEC2,
|
||||
}
|
||||
}
|
||||
|
||||
func (c dslMetricsCollector) collect() error {
|
||||
status, err := c.client.DSLStatus()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error getting dsl status")
|
||||
}
|
||||
|
||||
c.status.WithLabelValues(c.host, status.DSLCfg.Status, status.DSLCfg.Modulation, status.DSLCfg.DataPath).Set(1)
|
||||
|
||||
c.upCurrRate.WithLabelValues(c.host).Set(float64(status.DSLCfg.UpCurrRate))
|
||||
c.downCurrRate.WithLabelValues(c.host).Set(float64(status.DSLCfg.DownCurrRate))
|
||||
c.upCurrRate2.WithLabelValues(c.host).Set(float64(status.DSLCfg.UpCurrRate2))
|
||||
c.downCurrRate2.WithLabelValues(c.host).Set(float64(status.DSLCfg.DownCurrRate2))
|
||||
c.upMaxRate.WithLabelValues(c.host).Set(float64(status.DSLCfg.UpMaxRate))
|
||||
c.downMaxRate.WithLabelValues(c.host).Set(float64(status.DSLCfg.DownMaxRate))
|
||||
c.upSNR.WithLabelValues(c.host).Set(float64(status.DSLCfg.UpSNR))
|
||||
c.downSNR.WithLabelValues(c.host).Set(float64(status.DSLCfg.DownSNR))
|
||||
c.upAttenuation.WithLabelValues(c.host).Set(float64(status.DSLCfg.UpAttenuation))
|
||||
c.downAttenuation.WithLabelValues(c.host).Set(float64(status.DSLCfg.DownAttenuation))
|
||||
c.upPower.WithLabelValues(c.host).Set(float64(status.DSLCfg.UpPower))
|
||||
c.downPower.WithLabelValues(c.host).Set(float64(status.DSLCfg.DownPower))
|
||||
|
||||
c.downHEC.WithLabelValues(c.host).Set(float64(status.DSLStats.DownHEC))
|
||||
c.upHEC.WithLabelValues(c.host).Set(float64(status.DSLStats.UpHEC))
|
||||
c.downCRC.WithLabelValues(c.host).Set(float64(status.DSLStats.DownCRC))
|
||||
c.upCRC.WithLabelValues(c.host).Set(float64(status.DSLStats.UpCRC))
|
||||
c.downFEC.WithLabelValues(c.host).Set(float64(status.DSLStats.DownFEC))
|
||||
c.upFEC.WithLabelValues(c.host).Set(float64(status.DSLStats.UpFEC))
|
||||
c.downHEC2.WithLabelValues(c.host).Set(float64(status.DSLStats.DownHEC2))
|
||||
c.upHEC2.WithLabelValues(c.host).Set(float64(status.DSLStats.UpHEC2))
|
||||
c.downCRC2.WithLabelValues(c.host).Set(float64(status.DSLStats.DownCRC2))
|
||||
c.upCRC2.WithLabelValues(c.host).Set(float64(status.DSLStats.UpCRC2))
|
||||
c.downFEC2.WithLabelValues(c.host).Set(float64(status.DSLStats.DownFEC2))
|
||||
c.upFEC2.WithLabelValues(c.host).Set(float64(status.DSLStats.UpFEC2))
|
||||
|
||||
return nil
|
||||
}
|
||||
46
internal/rest/server.go
Normal file
46
internal/rest/server.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gorilla/handlers"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"github.com/sirupsen/logrus"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
httpSrv *http.Server
|
||||
}
|
||||
|
||||
func New(bind string) *Server {
|
||||
var s Server
|
||||
|
||||
r := http.NewServeMux()
|
||||
r.Handle("/prometheus", promhttp.Handler())
|
||||
|
||||
server := http.Server{
|
||||
Addr: bind,
|
||||
Handler: handlers.LoggingHandler(os.Stdout, r),
|
||||
}
|
||||
|
||||
s.httpSrv = &server
|
||||
|
||||
return &s
|
||||
}
|
||||
|
||||
func (s Server) Start() {
|
||||
go func() {
|
||||
err := s.httpSrv.ListenAndServe()
|
||||
if err != nil {
|
||||
logrus.Panic(err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (s Server) Stop() {
|
||||
err := s.httpSrv.Shutdown(context.Background())
|
||||
if err != nil {
|
||||
logrus.Warn(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user