diff --git a/cert.go b/cert.go index 1b65955..2f8dc17 100644 --- a/cert.go +++ b/cert.go @@ -7,6 +7,8 @@ import ( "net/url" "strings" "time" + + log "github.com/sirupsen/logrus" ) type probeResult uint @@ -37,6 +39,8 @@ func (p probeResult) String() string { } func checkCertificate(probeURL *url.URL) (probeResult, *x509.Certificate) { + checkLogger := log.WithFields(log.Fields{"probe_url": probeURL}) + req, _ := http.NewRequest("HEAD", probeURL.String(), nil) req.Header.Set("User-Agent", fmt.Sprintf("Mozilla/5.0 (compatible; PromCertcheck/%s; +https://github.com/Luzifer/promcertcheck)", version)) @@ -44,6 +48,7 @@ func checkCertificate(probeURL *url.URL) (probeResult, *x509.Certificate) { switch err.(type) { case nil, redirectFoundError: default: + checkLogger.WithError(err).Error("HTTP request failed") if !strings.Contains(err.Error(), "Found a redirect.") { return generalFailure, nil } @@ -67,6 +72,7 @@ func checkCertificate(probeURL *url.URL) (probeResult, *x509.Certificate) { } if verifyCert == nil { + checkLogger.Debug("Certificate not found") return certificateNotFound, nil } @@ -78,12 +84,15 @@ func checkCertificate(probeURL *url.URL) (probeResult, *x509.Certificate) { } if !verificationResult { + checkLogger.Debug("Certificate invalid") return certificateInvalid, verifyCert } if verifyCert.NotAfter.Sub(time.Now()) < config.ExpireWarning { + checkLogger.Debug("Certificate expires soon") return certificateExpiresSoon, verifyCert } + checkLogger.Debug("Certificate OK") return certificateOK, verifyCert } diff --git a/http.go b/http.go index 2235ea3..76f71bb 100644 --- a/http.go +++ b/http.go @@ -4,10 +4,10 @@ package main import ( "encoding/json" - "log" "net/http" "github.com/flosch/pongo2" + log "github.com/sirupsen/logrus" ) func htmlHandler(res http.ResponseWriter, r *http.Request) { diff --git a/main.go b/main.go index 4bfb9f8..3586aa9 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main // import "github.com/Luzifer/promcertcheck" import ( "crypto/x509" "fmt" - "log" "net/http" "net/url" "strings" @@ -13,12 +12,14 @@ import ( "github.com/gorilla/mux" "github.com/prometheus/client_golang/prometheus" "github.com/robfig/cron" + log "github.com/sirupsen/logrus" ) var ( config = struct { Debug bool `flag:"debug" default:"false" description:"Output debugging data"` ExpireWarning time.Duration `flag:"expire-warning" default:"744h" description:"When to warn about a soon expiring certificate"` + LogLevel string `flag:"log-level" default:"info" description:"Verbosity of logs to use (debug, info, warning, error, ...)"` Probes []string `flag:"probe" default:"" description:"URLs to check for certificate issues"` }{} version = "dev" @@ -42,6 +43,12 @@ func init() { if err := rconfig.Parse(&config); err != nil { log.Fatalf("Unable to parse CLI parameters: %s", err) } + + if logLevel, err := log.ParseLevel(config.LogLevel); err == nil { + log.SetLevel(logLevel) + } else { + log.Fatalf("Unable to parse log level: %s", err) + } } func main() { diff --git a/promcertcheck b/promcertcheck index 00c775c..e5db544 100755 Binary files a/promcertcheck and b/promcertcheck differ