mirror of
https://github.com/nikdoof/aaisp-chaos.git
synced 2025-12-13 14:52:17 +00:00
Previously if we fail to connect to CHAOS during a scrape no metrics are emitted. Now a aaisp_scrape_success metric is always emitted with values 0 for failure and 1 for success, along the lines of blackbox_exporter's probe_success metric.
106 lines
2.4 KiB
Go
106 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
|
|
chaos "github.com/jamesog/aaisp-chaos"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
)
|
|
|
|
var (
|
|
broadbandQuotaRemainingDesc = prometheus.NewDesc(
|
|
"aaisp_broadband_quota_remaining",
|
|
"Quota remaining in bytes",
|
|
[]string{"line_id"},
|
|
nil,
|
|
)
|
|
broadbandQuotaTotalDesc = prometheus.NewDesc(
|
|
"aaisp_broadband_quota_total",
|
|
"Quota total in bytes",
|
|
[]string{"line_id"},
|
|
nil,
|
|
)
|
|
broadbandTXRateDesc = prometheus.NewDesc(
|
|
"aaisp_broadband_tx_rate",
|
|
"Line transmit rate in bits per second",
|
|
[]string{"line_id"},
|
|
nil,
|
|
)
|
|
broadbandRXRateDesc = prometheus.NewDesc(
|
|
"aaisp_broadband_rx_rate",
|
|
"Line receive rate in bits per second",
|
|
[]string{"line_id"},
|
|
nil,
|
|
)
|
|
scrapeSuccessGauge = prometheus.NewGauge(prometheus.GaugeOpts{
|
|
Name: "aaisp_scrape_success",
|
|
Help: "Displays whether or not the AAISP API scrape was a success",
|
|
})
|
|
)
|
|
|
|
type broadbandCollector struct {
|
|
*chaos.API
|
|
}
|
|
|
|
func (bc broadbandCollector) Describe(ch chan<- *prometheus.Desc) {
|
|
prometheus.DescribeByCollect(bc, ch)
|
|
}
|
|
|
|
func (bc broadbandCollector) Collect(ch chan<- prometheus.Metric) {
|
|
lines, err := bc.BroadbandInfo()
|
|
if err != nil {
|
|
log.Printf("error getting broadband info: %v\n", err)
|
|
scrapeSuccessGauge.Set(0)
|
|
return
|
|
}
|
|
scrapeSuccessGauge.Set(1)
|
|
for _, line := range lines {
|
|
ch <- prometheus.MustNewConstMetric(
|
|
broadbandQuotaRemainingDesc,
|
|
prometheus.GaugeValue,
|
|
float64(line.QuotaRemaining),
|
|
strconv.Itoa(line.ID),
|
|
)
|
|
ch <- prometheus.MustNewConstMetric(
|
|
broadbandQuotaTotalDesc,
|
|
prometheus.CounterValue,
|
|
float64(line.QuotaMonthly),
|
|
strconv.Itoa(line.ID),
|
|
)
|
|
ch <- prometheus.MustNewConstMetric(
|
|
broadbandTXRateDesc,
|
|
prometheus.GaugeValue,
|
|
float64(line.TXRate),
|
|
strconv.Itoa(line.ID),
|
|
)
|
|
ch <- prometheus.MustNewConstMetric(
|
|
broadbandRXRateDesc,
|
|
prometheus.GaugeValue,
|
|
float64(line.RXRate),
|
|
strconv.Itoa(line.ID),
|
|
)
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
listen := flag.String("listen", ":8080", "listen `address`")
|
|
flag.Parse()
|
|
|
|
collector := broadbandCollector{
|
|
API: chaos.New(chaos.Auth{
|
|
ControlLogin: os.Getenv("CHAOS_CONTROL_LOGIN"),
|
|
ControlPassword: os.Getenv("CHAOS_CONTROL_PASSWORD"),
|
|
}),
|
|
}
|
|
|
|
prometheus.MustRegister(collector)
|
|
prometheus.MustRegister(scrapeSuccessGauge)
|
|
http.Handle("/metrics", promhttp.Handler())
|
|
log.Fatal(http.ListenAndServe(*listen, nil))
|
|
}
|