Skip to content

Instantly share code, notes, and snippets.

@hkolbeck
Created February 11, 2014 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hkolbeck/8927496 to your computer and use it in GitHub Desktop.
Save hkolbeck/8927496 to your computer and use it in GitHub Desktop.
Collector
package main
import (
"flag"
"fmt"
"log"
"net/http"
"io"
"io/ioutil"
"encoding/json"
"errors"
"os"
"strings"
"time"
)
func main() {
interval := flag.Int64("i", 1000, "Collection interval in milliseconds")
url := flag.String("u", "", "URL to query")
jsonpath := flag.String("j", "", "Period delimited path of json keys")
flag.Parse()
if *interval < 1 || *url == "" || *jsonpath == "" {
flag.Usage()
os.Exit(1)
}
path := strings.Split(*jsonpath, ".")
client := &http.Client{}
req, _ := http.NewRequest("GET", *url, nil)
req.Close = true
for _ = range time.Tick(time.Duration(*interval) * time.Millisecond) {
resp, err := client.Do(req)
if err != nil || resp.StatusCode >= 400 {
log.Print(err)
continue
}
blob, err := readJson(resp.Body)
if err != nil {
log.Print(err)
continue
}
val, err := getVal(blob, path)
if err != nil {
log.Print(err)
continue
}
fmt.Println(val)
}
}
func readJson(stream io.ReadCloser) (map[string]interface{}, error) {
defer stream.Close()
raw, err := ioutil.ReadAll(stream)
if err != nil {
return nil, err
}
var blob map[string]interface{}
err = json.Unmarshal(raw, &blob)
if err != nil {
return nil, err
}
return blob, nil
}
func getVal(blob map[string]interface{}, path []string) (interface{}, error) {
for _, key := range path[:len(path) - 1] {
obj, _ := blob[key]
switch obj := obj.(type) {
case map[string]interface{}:
blob = obj
default:
return nil, errors.New("Json blob format didn't match provided path: " +
key + " not found or not an object.")
}
}
val, ok := blob[path[len(path) - 1]]
if ok {
return val, nil
}
return nil, errors.New("Json blob format didn't match provided path: " + path[len(path) - 1] + " not found.")
}
Example invocation, collect the value of a rash metric at 100ms intervals
./collector -u "http://s0193.prod:6667/reactorrpcmetrics_gauge/commandgetifpresentqueuesize" -j "data.Value" -i 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment