Skip to content

Instantly share code, notes, and snippets.

@ThijsFeryn
Last active June 7, 2019 00:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThijsFeryn/f0fc564498b862836e26 to your computer and use it in GitHub Desktop.
Save ThijsFeryn/f0fc564498b862836e26 to your computer and use it in GitHub Desktop.
A Go script that retrieves the Varnish memory consumption and returns it via a RESTful API using GoJi
package main
import (
"strconv"
"net/http"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
"os/exec"
"encoding/json"
)
func usage(c web.C, w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
encoder := json.NewEncoder(w)
cmdName := "varnishstat"
cmdArgs := []string{"-f", "SMA.s0.g_bytes", "-j"}
cmd := exec.Command(cmdName, cmdArgs...)
stdout, err := cmd.Output()
if(err != nil) {
http.Error(w, err.Error(), 500)
return
}
var varnishstatInterface interface{}
err = json.Unmarshal([]byte(stdout), &varnishstatInterface)
if(err != nil) {
http.Error(w, err.Error(), 500)
return
}
varnishstatMap := varnishstatInterface.(map[string]interface{})
valueMap := varnishstatMap["SMA.s0.g_bytes"].(map[string]interface{})
encoder.Encode(strconv.FormatFloat(valueMap["value"].(float64), 'f', 0, 32))
}
func main() {
goji.Get("/",usage)
goji.Serve()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment