Adora Laura Kalb
a21ebe5af2
All checks were successful
ci/woodpecker/pr/docker-deploy Pipeline was successful
160 lines
4.5 KiB
Go
160 lines
4.5 KiB
Go
/*
|
|
Copyright 2024 Adora Laura Kalb <adora@lila.network>
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"net/netip"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/alecthomas/kingpin/v2"
|
|
"github.com/go-kit/log"
|
|
"github.com/go-kit/log/level"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
"github.com/prometheus/common/promlog"
|
|
"github.com/prometheus/common/promlog/flag"
|
|
"github.com/prometheus/common/version"
|
|
"github.com/prometheus/exporter-toolkit/web"
|
|
webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag"
|
|
"golang.adora.codes/ntppool-exporter/collector"
|
|
)
|
|
|
|
const (
|
|
namespace = "ntppool"
|
|
)
|
|
|
|
var (
|
|
metricsPath = kingpin.Flag(
|
|
"web.telemetry-path",
|
|
"Path under which to expose metrics.",
|
|
).Default("/metrics").String()
|
|
//appListenIP = kingpin.Flag("web.listen-address", "Addresses on which to expose metrics and web interface").Default(":43609").String()
|
|
toolkitFlags = webflag.AddFlags(kingpin.CommandLine, ":43609")
|
|
|
|
// Metrics about the SNMP exporter itself.
|
|
ntppoolRequestErrors = promauto.NewCounter(
|
|
prometheus.CounterOpts{
|
|
Namespace: namespace,
|
|
Name: "request_errors_total",
|
|
Help: "Errors in requests to the SNMP exporter",
|
|
},
|
|
)
|
|
)
|
|
|
|
func handler(w http.ResponseWriter, r *http.Request, logger log.Logger, exporterMetrics collector.Metrics) {
|
|
|
|
// /metrics&target=2003%3Adb8%3A%3A1
|
|
// /metrics&target=1.2.3.4
|
|
query := r.URL.Query()
|
|
|
|
target := query.Get("target")
|
|
if len(query["target"]) != 1 || target == "" {
|
|
http.Error(w, "'target' parameter must be specified once", http.StatusBadRequest)
|
|
ntppoolRequestErrors.Inc()
|
|
return
|
|
}
|
|
|
|
targetIP, ipError := netip.ParseAddr(target)
|
|
if ipError != nil {
|
|
http.Error(w, "'target' parameter must be a valid IPv4/IPv6 address", http.StatusBadRequest)
|
|
ntppoolRequestErrors.Inc()
|
|
return
|
|
}
|
|
|
|
logger = log.With(logger, "target", target)
|
|
registry := prometheus.NewRegistry()
|
|
c := collector.New(r.Context(), targetIP, logger, exporterMetrics)
|
|
registry.MustRegister(c)
|
|
// Delegate http serving to Prometheus client library, which will call collector.Collect.
|
|
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
|
|
h.ServeHTTP(w, r)
|
|
}
|
|
|
|
func main() {
|
|
promlogConfig := &promlog.Config{}
|
|
flag.AddFlags(kingpin.CommandLine, promlogConfig)
|
|
kingpin.Version(version.Print("ntppool_exporter"))
|
|
kingpin.HelpFlag.Short('h')
|
|
kingpin.Parse()
|
|
logger := promlog.New(promlogConfig)
|
|
|
|
level.Info(logger).Log("msg", "Starting ntppool_exporter", "version", version.Info())
|
|
level.Info(logger).Log("build_context", version.BuildContext())
|
|
|
|
exporterMetrics := collector.Metrics{
|
|
NtpppolServerScore: promauto.NewGauge(
|
|
prometheus.GaugeOpts{
|
|
Namespace: namespace,
|
|
Name: "server_score",
|
|
Help: "Current ntppool server score",
|
|
},
|
|
),
|
|
}
|
|
|
|
http.HandleFunc(*metricsPath, func(w http.ResponseWriter, r *http.Request) {
|
|
handler(w, r, logger, exporterMetrics)
|
|
})
|
|
|
|
metricsPathValue := *metricsPath
|
|
|
|
if *metricsPath != "/" && *metricsPath != "" {
|
|
landingConfig := web.LandingConfig{
|
|
Name: "ntppool Exporter",
|
|
Description: "Prometheus Exporter for SNMP targets",
|
|
Version: version.Info(),
|
|
Form: web.LandingForm{
|
|
Action: metricsPathValue,
|
|
Inputs: []web.LandingFormInput{
|
|
{
|
|
Label: "Target",
|
|
Type: "text",
|
|
Name: "target",
|
|
Placeholder: "X.X.X.X/[::X]",
|
|
Value: "::1",
|
|
},
|
|
},
|
|
},
|
|
Links: []web.LandingLinks{
|
|
{
|
|
Address: *metricsPath,
|
|
Text: "Metrics",
|
|
},
|
|
},
|
|
}
|
|
|
|
landingPage, err := web.NewLandingPage(landingConfig)
|
|
if err != nil {
|
|
level.Error(logger).Log("err", err)
|
|
os.Exit(1)
|
|
}
|
|
http.Handle("/", landingPage)
|
|
}
|
|
|
|
srv := &http.Server{
|
|
ReadHeaderTimeout: 15 * time.Second,
|
|
ReadTimeout: 15 * time.Second,
|
|
WriteTimeout: 10 * time.Second,
|
|
IdleTimeout: 30 * time.Second,
|
|
}
|
|
if err := web.ListenAndServe(srv, toolkitFlags, logger); err != nil {
|
|
level.Error(logger).Log("msg", "Error starting HTTP server", "err", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|