Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.