From f4d369c2d8e22e697fdd946298db53605e431f69 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Fri, 4 Sep 2015 15:54:08 +0200 Subject: [PATCH] Added JSON output --- README.md | 6 ++++++ http.go | 8 +++++++- main.go | 7 ++++--- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b3b2907..6b8df94 100644 --- a/README.md +++ b/README.md @@ -23,3 +23,9 @@ Usage of ./certcheck: PromCertcheck dev... Starting to listen on 0.0.0.0:3000 ``` + +## URLs + +- `/` - Shows you a human readable version of the check data +- `/metrics` - Prometheus compatible output of the check data +- `/results.json` - Gives you a JSON version of the check results including certificate details diff --git a/http.go b/http.go index 243f375..d1a17e5 100644 --- a/http.go +++ b/http.go @@ -3,13 +3,14 @@ package main //go:generate go-bindata display.html import ( + "encoding/json" "log" "net/http" "github.com/flosch/pongo2" ) -func httpHandler(res http.ResponseWriter, r *http.Request) { +func htmlHandler(res http.ResponseWriter, r *http.Request) { tplsrc, _ := Asset("display.html") template, err := pongo2.FromString(string(tplsrc)) @@ -24,3 +25,8 @@ func httpHandler(res http.ResponseWriter, r *http.Request) { "version": version, }, res) } + +func jsonHandler(res http.ResponseWriter, r *http.Request) { + res.Header().Set("Content-Type", "application/json") + json.NewEncoder(res).Encode(probeMonitors) +} diff --git a/main.go b/main.go index fea3a09..6ae001c 100644 --- a/main.go +++ b/main.go @@ -28,8 +28,8 @@ var ( ) type probeMonitor struct { - IsValid prometheus.Gauge - Expires prometheus.Gauge + IsValid prometheus.Gauge `json:"-"` + Expires prometheus.Gauge `json:"-"` Status probeResult Certificate *x509.Certificate } @@ -66,7 +66,8 @@ func main() { r := mux.NewRouter() r.Handle("/metrics", prometheus.Handler()) - r.HandleFunc("/", httpHandler) + r.HandleFunc("/", htmlHandler) + r.HandleFunc("/results.json", jsonHandler) http.ListenAndServe(":3000", r) }