From 24368747ab07e6cfc20848194724cb1dad60cfbf Mon Sep 17 00:00:00 2001 From: Guilhem Lettron Date: Thu, 23 Nov 2017 16:10:04 +0100 Subject: [PATCH] Use healthcheck for systemd watchdog --- cmd/traefik/healthcheck.go | 37 +++++++++++++++++++++---------------- cmd/traefik/traefik.go | 12 ++++++++++-- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/cmd/traefik/healthcheck.go b/cmd/traefik/healthcheck.go index f0b0431d4..876d0d04f 100644 --- a/cmd/traefik/healthcheck.go +++ b/cmd/traefik/healthcheck.go @@ -2,6 +2,7 @@ package main import ( "crypto/tls" + "errors" "fmt" "net/http" "os" @@ -33,22 +34,7 @@ func runHealthCheck(traefikConfiguration *TraefikConfiguration) func() error { os.Exit(1) } - pingEntryPoint, ok := traefikConfiguration.EntryPoints[traefikConfiguration.Ping.EntryPoint] - if !ok { - pingEntryPoint = &configuration.EntryPoint{Address: ":8080"} - } - - client := &http.Client{Timeout: 5 * time.Second} - protocol := "http" - if pingEntryPoint.TLS != nil { - protocol = "https" - tr := &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - } - client.Transport = tr - } - - resp, errPing := client.Head(protocol + "://" + pingEntryPoint.Address + traefikConfiguration.Web.Path + "ping") + resp, errPing := healthCheck(traefikConfiguration.GlobalConfiguration) if errPing != nil { fmt.Printf("Error calling healthcheck: %s\n", errPing) os.Exit(1) @@ -62,3 +48,22 @@ func runHealthCheck(traefikConfiguration *TraefikConfiguration) func() error { return nil } } + +func healthCheck(globalConfiguration configuration.GlobalConfiguration) (*http.Response, error) { + pingEntryPoint, ok := globalConfiguration.EntryPoints[globalConfiguration.Ping.EntryPoint] + if !ok { + return nil, errors.New("missing ping entrypoint") + } + + client := &http.Client{Timeout: 5 * time.Second} + protocol := "http" + if pingEntryPoint.TLS != nil { + protocol = "https" + tr := &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + } + client.Transport = tr + } + + return client.Head(protocol + "://" + pingEntryPoint.Address + globalConfiguration.Web.Path + "ping") +} diff --git a/cmd/traefik/traefik.go b/cmd/traefik/traefik.go index 75a4064d7..cad69e1fb 100644 --- a/cmd/traefik/traefik.go +++ b/cmd/traefik/traefik.go @@ -153,10 +153,12 @@ func run(globalConfiguration *configuration.GlobalConfiguration, configFile stri svr := server.NewServer(*globalConfiguration) svr.Start() defer svr.Close() + sent, err := daemon.SdNotify(false, "READY=1") if !sent && err != nil { log.Error("Fail to notify", err) } + t, err := daemon.SdWatchdogEnabled(false) if err != nil { log.Error("Problem with watchdog", err) @@ -167,12 +169,18 @@ func run(globalConfiguration *configuration.GlobalConfiguration, configFile stri safe.Go(func() { tick := time.Tick(t) for range tick { - if ok, _ := daemon.SdNotify(false, "WATCHDOG=1"); !ok { - log.Error("Fail to tick watchdog") + _, errHealthCheck := healthCheck(*globalConfiguration) + if globalConfiguration.Ping == nil || errHealthCheck == nil { + if ok, _ := daemon.SdNotify(false, "WATCHDOG=1"); !ok { + log.Error("Fail to tick watchdog") + } + } else { + log.Error(errHealthCheck) } } }) } + svr.Wait() log.Info("Shutting down") logrus.Exit(0)