Last active
June 7, 2019 00:33
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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