diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2019-10-09 09:09:58 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2019-10-09 09:18:27 +0300 |
commit | 79b0eb417b0431e316964e003a9efe6b6725b94a (patch) | |
tree | a6596cdbf5fb393dda168723d1278ac3db6abdaf | |
parent | 10e75a2790e7b36095c2f0a4a2ae529b0af59d82 (diff) | |
download | certmon-79b0eb417b0431e316964e003a9efe6b6725b94a.tar.gz certmon-79b0eb417b0431e316964e003a9efe6b6725b94a.tar.bz2 |
Rename to certmon. Eliminate alternate names to avoid spurious polling.
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | certmon.go (renamed from certwatch.go) | 52 |
2 files changed, 46 insertions, 7 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0185e2f --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +certmon diff --git a/certwatch.go b/certmon.go index 188de0c..155ff0f 100644 --- a/certwatch.go +++ b/certmon.go @@ -48,11 +48,47 @@ func CertMatch(cert *x509.Certificate, cn string) bool { return false } +// Argument list +type ArgList struct { + args []string +} + +func NewArgList(a []string) *ArgList { + var args ArgList + if len(a) > 0 { + args.args = a + } else { + args.args = []string{``} + } + return &args +} + +func (a *ArgList) Next() (string) { + s := a.args[0] + a.args = a.args[1:] + return s +} + +func (a *ArgList) DropMatches(cert *x509.Certificate) { + for i := 0; i < len(a.args); { + if CertMatch(cert, a.args[i]) { + a.args = append(a.args[:i], a.args[i+1:]...) + } else { + i++ + } + } +} + +func (a *ArgList) More() bool { + return len(a.args) > 0 +} + // Command line options var warnLimit time.Duration var critLimit time.Duration var verboseOption bool var helpOption bool +var quietOption bool var host string // Intitialize command line parser @@ -62,6 +98,7 @@ func init() { flag.BoolVar(&verboseOption, `v`, false, `verbose mode`) flag.BoolVar(&helpOption, `h`, false, `show help summary`) flag.StringVar(&host, `H`, ``, `host name`) + flag.BoolVar(&quietOption, `q`, false, `quiet mode: print nothing, exit with a meaningful status`) flag.Usage = func() { if helpOption { flag.CommandLine.SetOutput(os.Stdout) @@ -88,14 +125,13 @@ func main() { } res := CertResultList{Address: host, Status: StatusOK} - if len(flag.Args()) > 0 { - for _, cn := range flag.Args() { - res.Check(cn) - } - } else { - res.Check(``) + + for args := NewArgList(flag.Args()); args.More(); { + res.Check(args) } + if !quietOption { res.Format() + } os.Exit(res.Status) } @@ -143,7 +179,8 @@ func (rl *CertResultList) Append(res CertResult) { } } -func (rl *CertResultList) Check(cn string) { +func (rl *CertResultList) Check(args *ArgList) { + cn := args.Next() addr := rl.Address; a := strings.Split(addr, `:`) switch (len(a)) { @@ -183,6 +220,7 @@ func (rl *CertResultList) Check(cn string) { if !CertMatch(cert, cn) { continue } + args.DropMatches(cert) res := CertResult{Subject: cn, Status: StatusOK} res.Ttl = time.Until(cert.NotAfter) if res.Ttl < critLimit { |