Skip to content
This repository has been archived by the owner on Oct 18, 2020. It is now read-only.

Commit

Permalink
feat(monitor): add custom err
Browse files Browse the repository at this point in the history
add custom err

Signed-off-by: mritd <[email protected]>
  • Loading branch information
mritd committed Oct 17, 2018
1 parent 196a23e commit bd37cf6
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions pkg/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package monitor

import (
"crypto/tls"
"errors"
"fmt"
"net/http"
"time"
Expand All @@ -41,6 +40,20 @@ type Config struct {
TimeOut time.Duration
}

type WebSiteError struct {
Message string
}

func (e *WebSiteError) Error() string {
return e.Message
}

func NewWebSiteError(msg string) *WebSiteError {
return &WebSiteError{
Message: msg,
}
}

func ExampleConfig() Config {
return Config{
WebSites: []string{
Expand All @@ -53,7 +66,7 @@ func ExampleConfig() Config {
}
}

func check(address string, beforeTime, timeout time.Duration) error {
func check(address string, beforeTime, timeout time.Duration) *WebSiteError {

logrus.Infof("Check website [%s]...", address)

Expand All @@ -66,17 +79,17 @@ func check(address string, beforeTime, timeout time.Duration) error {
}
resp, err := client.Get(address)
if !utils.CheckErr(err) {
return err
return nil
}
defer resp.Body.Close()

for _, cert := range resp.TLS.PeerCertificates {
if !cert.NotAfter.After(time.Now()) {
return errors.New(fmt.Sprintf("Website [%s] certificate has expired: %s", address, cert.NotAfter.Local().Format("2006-01-02 15:04:05")))
return NewWebSiteError(fmt.Sprintf("Website [%s] certificate has expired: %s", address, cert.NotAfter.Local().Format("2006-01-02 15:04:05")))
}

if cert.NotAfter.Sub(time.Now()) < beforeTime {
return errors.New(fmt.Sprintf("Website [%s] certificate will expire, remaining time: %fh", address, cert.NotAfter.Sub(time.Now()).Hours()))
return NewWebSiteError(fmt.Sprintf("Website [%s] certificate will expire, remaining time: %fh", address, cert.NotAfter.Sub(time.Now()).Hours()))
}
}

Expand Down

0 comments on commit bd37cf6

Please sign in to comment.