Browse Source

Adds readiness and liveness endpoints.

master
Christian Fritz 4 years ago
parent
commit
1cf0dfb5ae
No known key found for this signature in database GPG Key ID: AB40486FCA9FA29C
  1. 2
      go.mod
  2. 17
      health.go
  3. 38
      main.go

2
go.mod

@ -3,7 +3,9 @@ module github.com/sberk42/fritzbox_exporter
go 1.15 go 1.15
require ( require (
github.com/heptiolabs/healthcheck v0.0.0-20180807145615-6ff867650f40
github.com/namsral/flag v1.7.4-pre github.com/namsral/flag v1.7.4-pre
github.com/prometheus/client_golang v1.10.0 github.com/prometheus/client_golang v1.10.0
golang.org/x/text v0.3.6 golang.org/x/text v0.3.6
gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0 // indirect
) )

17
health.go

@ -0,0 +1,17 @@
package main
import (
"github.com/heptiolabs/healthcheck"
"time"
)
// createHealthChecks will create the readiness and liveness endpoints and add the check functions.
func createHealthChecks(gatewayUrl string) healthcheck.Handler {
health := healthcheck.NewHandler()
health.AddReadinessCheck("FRITZ!Box connection",
healthcheck.HTTPGetCheck(gatewayUrl+"/any.xml", time.Duration(3)*time.Second))
health.AddLivenessCheck("go-routines", healthcheck.GoroutineCountCheck(100))
return health
}

38
main.go

@ -52,11 +52,11 @@ var (
flagDisableLua = flag.Bool("nolua", false, "disable collecting lua metrics") flagDisableLua = flag.Bool("nolua", false, "disable collecting lua metrics")
flagLuaMetricsFile = flag.String("lua-metrics-file", "metrics-lua.json", "The JSON file with the lua metric definitions.") flagLuaMetricsFile = flag.String("lua-metrics-file", "metrics-lua.json", "The JSON file with the lua metric definitions.")
flagGatewayURL = flag.String("gateway-url", "http://fritz.box:49000", "The URL of the FRITZ!Box") flagGatewayURL = flag.String("gateway-url", "http://fritz.box:49000", "The URL of the FRITZ!Box")
flagGatewayLuaURL = flag.String("gateway-luaurl", "http://fritz.box", "The URL of the FRITZ!Box UI") flagGatewayLuaURL = flag.String("gateway-luaurl", "http://fritz.box", "The URL of the FRITZ!Box UI")
flagUsername = flag.String("username", "", "The user for the FRITZ!Box UPnP service") flagUsername = flag.String("username", "", "The user for the FRITZ!Box UPnP service")
flagPassword = flag.String("password", "", "The password for the FRITZ!Box UPnP service") flagPassword = flag.String("password", "", "The password for the FRITZ!Box UPnP service")
flagGatewayVerifyTLS = flag.Bool("verifyTls", false, "Verify the tls connection when connecting to the FRITZ!Box") flagGatewayVerifyTLS = flag.Bool("verifyTls", false, "Verify the tls connection when connecting to the FRITZ!Box")
) )
var ( var (
@ -180,11 +180,11 @@ var luaCache map[string]*luaCacheEntry
// FritzboxCollector main struct // FritzboxCollector main struct
type FritzboxCollector struct { type FritzboxCollector struct {
URL string URL string
Gateway string Gateway string
Username string Username string
Password string Password string
VerifyTls bool VerifyTls bool
// support for lua collector // support for lua collector
LuaSession *lua.LuaSession LuaSession *lua.LuaSession
@ -803,13 +803,13 @@ func main() {
} }
collector := &FritzboxCollector{ collector := &FritzboxCollector{
URL: *flagGatewayURL, URL: *flagGatewayURL,
Gateway: u.Hostname(), Gateway: u.Hostname(),
Username: *flagUsername, Username: *flagUsername,
Password: *flagPassword, Password: *flagPassword,
VerifyTls: *flagGatewayVerifyTLS, VerifyTls: *flagGatewayVerifyTLS,
LuaSession: luaSession, LuaSession: luaSession,
LabelRenames: luaLabelRenames, LabelRenames: luaLabelRenames,
} }
@ -847,8 +847,14 @@ func main() {
prometheus.MustRegister(collectLuaResultsLoaded) prometheus.MustRegister(collectLuaResultsLoaded)
} }
healthChecks := createHealthChecks(*flagGatewayURL)
http.Handle("/metrics", promhttp.Handler()) http.Handle("/metrics", promhttp.Handler())
fmt.Printf("metrics available at http://%s/metrics\n", *flagAddr) fmt.Printf("metrics available at http://%s/metrics\n", *flagAddr)
http.HandleFunc("/ready", healthChecks.ReadyEndpoint)
fmt.Printf("readyness check available at http://%s/ready\n", *flagAddr)
http.HandleFunc("/live", healthChecks.LiveEndpoint)
fmt.Printf("liveness check available at http://%s/live\n", *flagAddr)
log.Fatal(http.ListenAndServe(*flagAddr, nil)) log.Fatal(http.ListenAndServe(*flagAddr, nil))
} }

Loading…
Cancel
Save