aboutsummaryrefslogtreecommitdiff
path: root/cmd/main/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/main/main.go')
-rw-r--r--cmd/main/main.go97
1 files changed, 88 insertions, 9 deletions
diff --git a/cmd/main/main.go b/cmd/main/main.go
index 2e11184..34fc6f4 100644
--- a/cmd/main/main.go
+++ b/cmd/main/main.go
@@ -1,16 +1,26 @@
package main
import (
+ "bytes"
"flag"
"fmt"
"gopkg.in/yaml.v3"
"os"
+ "time"
"riedstra.dev/go/checkup"
)
var Version = "Development"
+// jobResponse lets us return a bit more info from each of the jobs
+// and track how the response has changed
+type jobResponse struct {
+ Id string
+ Message string
+ Err error
+}
+
type Config struct {
RocketChatURL string `yaml:"RocketChatURL"`
DiscordURL string `yaml:"DiscordURL"`
@@ -20,18 +30,63 @@ type Config struct {
ExpectedStatusCode int `yaml:"ExpectedStatusCode"`
StatusChecks map[string]*int `yaml:"StatusChecks"`
Workers int `yaml:"Workers"`
+ Interval int `yaml:"Interval"`
}
-func ReadConfig(fn string) (*Config, error) {
+func ReadConfig(fn string, conf *Config) error {
fh, err := os.Open(fn)
if err != nil {
- return nil, err
+ return err
}
dec := yaml.NewDecoder(fh)
- conf := &Config{}
err = dec.Decode(conf)
- return conf, err
+ return err
+}
+
+func jobNotifyDedup(conf *Config, prevJobs, newJobs map[string]*jobResponse) {
+ buf := &bytes.Buffer{}
+
+ for id, resp := range newJobs {
+ oldresp, ok := prevJobs[id]
+ if !ok {
+ buf.Write([]byte(resp.Message))
+ buf.Write([]byte{'\n'})
+ continue
+ }
+
+ if oldresp.Message == resp.Message {
+ continue
+ }
+
+ buf.Write([]byte(
+ oldresp.Message + " now --> " + resp.Message,
+ ))
+ buf.Write([]byte{'\n'})
+ }
+
+ for id, resp := range prevJobs {
+ _, ok := newJobs[id]
+ if !ok {
+ buf.Write([]byte(
+ "[CLEARED]: " + resp.Message + "\n",
+ ))
+ }
+ }
+
+ fmt.Print(string(buf.Bytes()))
+ notify(conf, buf.Bytes())
+}
+
+func jobNotify(conf *Config, jobs map[string]*jobResponse) {
+ buf := &bytes.Buffer{}
+
+ for _, resp := range jobs {
+ buf.Write([]byte(resp.Message))
+ buf.Write([]byte{'\n'})
+ }
+
+ notify(conf, buf.Bytes())
}
func notify(conf *Config, b []byte) {
@@ -62,6 +117,7 @@ func main() {
confFn := fl.String("c", "config.yml", "Configuration file path")
version := fl.Bool("v", false, "Print version and exit")
+ once := fl.Bool("o", false, "Run once then exit")
_ = fl.Parse(os.Args[1:])
@@ -70,17 +126,40 @@ func main() {
os.Exit(0)
}
- conf, err := ReadConfig(*confFn)
+ // Defaults to be overridden by config
+ conf := &Config{
+ Workers: 1,
+ DefaultCertPort: "443",
+ CertWindow: 15,
+ ExpectedStatusCode: 200,
+ Interval: 60,
+ }
+
+ err := ReadConfig(*confFn, conf)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
- if conf.Workers == 0 {
- conf.Workers = 1
+ if *once {
+ jobNotify(conf, checkCerts(conf))
+ jobNotify(conf, checkStatus(conf))
+ os.Exit(0)
}
- checkCerts(conf)
- checkStatus(conf)
+ certsPrev := map[string]*jobResponse{}
+ statusPrev := map[string]*jobResponse{}
+ for {
+ cert := checkCerts(conf)
+ status := checkStatus(conf)
+
+ jobNotifyDedup(conf, certsPrev, cert)
+ jobNotifyDedup(conf, statusPrev, status)
+
+ certsPrev = cert
+ statusPrev = status
+
+ time.Sleep(time.Duration(conf.Interval) * time.Second)
+ }
}