move hwc and hwl into this project

This commit is contained in:
Stephen McQuay 2018-03-08 14:07:14 -08:00
parent 1b7c1aaf41
commit 73f15774d6
Signed by: sm
GPG Key ID: 4E4B72F479BA3CE5
7 changed files with 129 additions and 1 deletions

View File

@ -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

View File

@ -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"`

4
cmd/hwc/Dockerfile Normal file
View File

@ -0,0 +1,4 @@
FROM scratch
ADD etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
ADD bin/hwc /bin/
CMD ["/bin/hwc"]

96
cmd/hwc/main.go Normal file
View File

@ -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 hostname>")
}
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)
}
}

4
cmd/hwl/Dockerfile Normal file
View File

@ -0,0 +1,4 @@
FROM scratch
ADD etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
ADD bin/hwl /bin/
CMD ["/bin/hwl"]

15
cmd/hwl/main.go Normal file
View File

@ -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)
}
}