Browse Source

Add configuration support

master
Teran McKinney 2 years ago
parent
commit
8054c406d6
  1. 21
      configuration.go
  2. 15
      host_check_statsd.go
  3. 11
      main.go
  4. 1
      samples/invalid_configuration.json
  5. 1
      samples/valid_configuration.json
  6. 5
      test.sh

21
configuration.go

@ -0,0 +1,21 @@
package main
import (
"encoding/json"
"os"
)
type Configuration struct {
Hosts []string `json:"hosts"`
}
func configuration(jsonFile string) (configuration Configuration, err error) {
fp, err := os.Open(jsonFile)
if err != nil {
return
}
decoder := json.NewDecoder(fp)
decoder.DisallowUnknownFields()
err = decoder.Decode(&configuration)
return
}

15
host_check_statsd.go

@ -1,6 +1,8 @@
package main
import (
"log"
"gopkg.in/alexcesaro/statsd.v2"
)
@ -19,3 +21,16 @@ func hostCheckStatsd(host string) (secondsTillExpiry int, err error) {
s.Gauge("expiry."+host, secondsTillExpiry)
return
}
func hostCheckStatsdConfiguration(conf Configuration) (err error) {
for _, host := range conf.Hosts {
seconds, hostCheckError := hostCheckStatsd(host)
if hostCheckError == nil {
log.Printf("%s: %d seconds remaining.", host, seconds)
} else {
err = hostCheckError
log.Printf("Got error on %s: %s", host, hostCheckError.Error())
}
}
return
}

11
main.go

@ -15,6 +15,8 @@ func usage() {
fmt.Fprintln(os.Stderr, "Usage: sslexpirystatsd <command> [argument]")
fmt.Fprintln(os.Stderr, "Command: check <host:port> (Example: github.com:443)")
fmt.Fprintln(os.Stderr, "Command: checkstatsd <host:port> (Example: github.com:443)")
fmt.Fprintln(os.Stderr, "Command: checkstatsdconfiguration <configuration.json>")
fmt.Fprintln(os.Stderr, "Command: validate_configuration <configuration.json>")
os.Exit(2)
}
@ -46,6 +48,15 @@ func main() {
seconds, err := hostCheckStatsd(os.Args[2])
fatal_error(err)
fmt.Printf("%s expires in %d seconds.\n", os.Args[2], seconds)
case "checkstatsdconfiguration":
exactly_arguments(3)
conf, err := configuration(os.Args[2])
err = hostCheckStatsdConfiguration(conf)
fatal_error(err)
case "validate_configuration":
exactly_arguments(3)
_, err := configuration(os.Args[2])
fatal_error(err)
default:
usage()
}

1
samples/invalid_configuration.json

@ -0,0 +1 @@
{ "something_else": [ "go-beyond.org:443", "github.com:443" ] }

1
samples/valid_configuration.json

@ -0,0 +1 @@
{ "hosts": [ "go-beyond.org:443", "github.com:443" ] }

5
test.sh

@ -11,7 +11,7 @@ go test
go build
# strip -s decensor
strip -s sslexpirystatsd
cleanup() {
echo "Cleaning up."
@ -25,4 +25,7 @@ fail() {
exit 1
}
./sslexpirystatsd validate_configuration samples/valid_configuration.json
./sslexpirystatsd validate_configuration samples/invalid_configuration.json && fail "We thought an invalid configuration was valid."
echo Success
Loading…
Cancel
Save