diff --git a/go.mod b/go.mod index 186e05a..5647070 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,9 @@ module github.com/sberk42/fritzbox_exporter go 1.15 require ( + github.com/heptiolabs/healthcheck v0.0.0-20180807145615-6ff867650f40 github.com/namsral/flag v1.7.4-pre github.com/prometheus/client_golang v1.10.0 golang.org/x/text v0.3.6 + gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0 // indirect ) diff --git a/health.go b/health.go new file mode 100644 index 0000000..d18fdb5 --- /dev/null +++ b/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 +} diff --git a/main.go b/main.go index 577066e..2c0ff18 100644 --- a/main.go +++ b/main.go @@ -52,11 +52,11 @@ var ( 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.") - 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") - flagUsername = flag.String("username", "", "The user 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") + 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") + flagUsername = flag.String("username", "", "The user 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") ) var ( @@ -180,11 +180,11 @@ var luaCache map[string]*luaCacheEntry // FritzboxCollector main struct type FritzboxCollector struct { - URL string - Gateway string - Username string - Password string - VerifyTls bool + URL string + Gateway string + Username string + Password string + VerifyTls bool // support for lua collector LuaSession *lua.LuaSession @@ -803,13 +803,13 @@ func main() { } collector := &FritzboxCollector{ - URL: *flagGatewayURL, - Gateway: u.Hostname(), - Username: *flagUsername, - Password: *flagPassword, - VerifyTls: *flagGatewayVerifyTLS, + URL: *flagGatewayURL, + Gateway: u.Hostname(), + Username: *flagUsername, + Password: *flagPassword, + VerifyTls: *flagGatewayVerifyTLS, - LuaSession: luaSession, + LuaSession: luaSession, LabelRenames: luaLabelRenames, } @@ -847,8 +847,14 @@ func main() { prometheus.MustRegister(collectLuaResultsLoaded) } + healthChecks := createHealthChecks(*flagGatewayURL) + http.Handle("/metrics", promhttp.Handler()) 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)) }