diff --git a/Makefile b/Makefile index 6008ed3..0022abc 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,14 @@ +.PHONY: default +default: bin/hw bin/hwc bin/hwl + bin/hw: cmd/hw/main.go bin GOOS=linux go build -v -o bin/hw ./cmd/hw +bin/hwc: cmd/hwc/main.go bin + GOOS=linux go build -v -o bin/hwc ./cmd/hwc + +bin/hwl: cmd/hwl/main.go bin + GOOS=linux go build -v -o bin/hwl ./cmd/hwl + bin: mkdir bin diff --git a/Dockerfile b/cmd/hw/Dockerfile similarity index 100% rename from Dockerfile rename to cmd/hw/Dockerfile diff --git a/cmd/hw/main.go b/cmd/hw/main.go index 170f9de..14fd41d 100644 --- a/cmd/hw/main.go +++ b/cmd/hw/main.go @@ -12,7 +12,7 @@ import ( "mcquay.me/metrics" ) -const version = "v0.1.0" +const version = "v0.1.1" type v struct { Hostname string `json:"hostname"` diff --git a/cmd/hwc/Dockerfile b/cmd/hwc/Dockerfile new file mode 100644 index 0000000..5d92c42 --- /dev/null +++ b/cmd/hwc/Dockerfile @@ -0,0 +1,4 @@ +FROM scratch +ADD etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ +ADD bin/hwc /bin/ +CMD ["/bin/hwc"] diff --git a/cmd/hwc/main.go b/cmd/hwc/main.go new file mode 100644 index 0000000..60a57c3 --- /dev/null +++ b/cmd/hwc/main.go @@ -0,0 +1,96 @@ +package main + +import ( + "encoding/json" + "fmt" + "log" + "net/http" + "os" + "sync" + "time" + + "github.com/prometheus/client_golang/prometheus/promhttp" + "mcquay.me/metrics" +) + +type v struct { + Hostname string `json:"hostname"` + V string `json:"version"` +} + +type state struct { + Hostname string `json:"hostname"` + V string `json:"version"` + + sync.RWMutex + Counts map[string]int `json:"counts"` +} + +func (s *state) update(target string) { + u := fmt.Sprintf("http://%s:8080/", target) + for { + time.Sleep(100 * time.Millisecond) + req, err := http.NewRequest("GET", u, nil) + if err != nil { + panic(err) + } + req.Close = true + httpResp, err := http.DefaultClient.Do(req) + if err != nil { + log.Printf("get: %+v", err) + continue + } + rv := v{} + if err := json.NewDecoder(httpResp.Body).Decode(&rv); err != nil { + panic(err) + } + if err := httpResp.Body.Close(); err != nil { + panic(err) + } + s.Lock() + s.Counts[rv.Hostname] += 1 + s.Unlock() + } +} + +const version = "v0.1.1" + +func main() { + if len(os.Args) < 2 { + log.Fatal("usage: hwc ") + } + target := os.Args[1] + + m, err := metrics.New("hw") + if err != nil { + log.Fatalf("metrics: %v", err) + } + + hn, err := os.Hostname() + if err != nil { + log.Fatalf("hostname: %+v", err) + } + + fetcher := state{ + Hostname: hn, + V: version, + Counts: map[string]int{}, + } + go fetcher.update(target) + + http.HandleFunc("/", m.WrapFunc("/", func(w http.ResponseWriter, req *http.Request) { + w.Header().Set("Content-Type", "application/json") + + fetcher.RLock() + defer fetcher.RUnlock() + if err := json.NewEncoder(w).Encode(fetcher); err != nil { + log.Printf("json: %+v", err) + } + })) + + http.Handle("/metrics", promhttp.Handler()) + + if err := http.ListenAndServe(":8081", nil); err != nil { + log.Fatalf("listen and serve: %v", err) + } +} diff --git a/cmd/hwl/Dockerfile b/cmd/hwl/Dockerfile new file mode 100644 index 0000000..eec560e --- /dev/null +++ b/cmd/hwl/Dockerfile @@ -0,0 +1,4 @@ +FROM scratch +ADD etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ +ADD bin/hwl /bin/ +CMD ["/bin/hwl"] diff --git a/cmd/hwl/main.go b/cmd/hwl/main.go new file mode 100644 index 0000000..22253be --- /dev/null +++ b/cmd/hwl/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "log" + "time" +) + +const version = "v0.1.1" + +func main() { + for { + log.Printf("hwl@%+v", version) + time.Sleep(1 * time.Second) + } +}