Skip to content

Instantly share code, notes, and snippets.

@emaxerrno
Created July 11, 2013 19:05
Show Gist options
  • Select an option

  • Save emaxerrno/5978269 to your computer and use it in GitHub Desktop.

Select an option

Save emaxerrno/5978269 to your computer and use it in GitHub Desktop.
// Parses the following json object and creates stats for opentsdb
//
// {
// "Broadcast": {
// "Strategy": "1",
// "proxies": [
// {
// "domain": "test.com",
// "hosts": [
// {
// "URL": "http://192.168.2.74:9136/",
// "Host": "http://192.168.2.74:9136/",
// "Active": "true",
// "RequestCount": "0",
// "SlowestRequest": "0",
// "Version": ""
// },
// {
// "URL": "http://192.168.2.74:9443/",
// "Host": "http://192.168.2.74:9443/",
// "Active": "true",
// "RequestCount": "0",
// "SlowestRequest": "0",
// "Version": ""
// }
// ]
// }
// ]
// }
// }
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"time"
)
const (
StatsUrl = "http://localhost:11230/stats"
)
type Host struct {
URL string
Host string
Active string
RequestCount string
SlowestRequest string
Version string
}
type Proxy struct {
Domain string `json:"domain"`
Hosts []*Host `json:"hosts"`
}
type Broadcast struct {
Strategy string
Proxies []*Proxy `json:"proxies"`
}
func milli() long {
return time.Now().UnixNano() % 1e6 / 1e3
}
func main() {
for {
select {
case <-time.After(1 * time.Second):
resp, err := http.Get(StatsUrl)
if err != nil {
log.Fatalf("Error reading from the local proxy: %s", err)
}
decoder := json.NewDecoder(resp.Body)
bcast := &Broadcast{}
err = decoder.Decode(bcast)
if err != nil {
log.Fatalf("Could not decode json object: %s", err)
}
millisecond := milli()
fmt.Printf("routing.strategy %d %s", millisecond, bcast.Strategy)
fmt.Println("routing.proxies.total %d %d", millisecond, len(bcast.Proxies))
for _, proxy := range bcast.Proxies {
fmt.Printf("routing.proxy.%s.hosts %d %d\n", proxy.Domain, millisecond, len(proxy.Hosts))
for _, host := range proxy.Hosts {
byte := 0x0
if host.Active {
byte = 0x01
}
fmt.Printf("routing.proxy.%s.%s %d %d\n", proxy.Domain, host.URL, millisecond, byte)
}
}
//val output = s"${formatMBeanName(mbean)}.${name} ${ts} ${v} procName=${formatVal(procName)} ${getTagString(mbean)}"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment