// sendData handles the actual sending of data to the aggregator. func sendData(cmd, svc string, u *whiplash.ClientUpdate) { // create a new aclient instance pc, err := pclient.NewTCP(pcconf) if err != nil { log.Println(err) return } defer pc.Close() // success, so turn the update into json jupdate, err := json.Marshal(u) if err != nil { log.Println(err) return } req := []byte(cmd) req = append(req, 32) req = append(req, jupdate...) _, err = pc.Dispatch(req) if err != nil { log.Println("err dispatching", cmd, "for", svc, ":", err) return } }
func main() { flag.Parse() args = flag.Args() // read whiplash config. genconf should be FALSE in the // whiplash.New call for a wlq instance: we don't expect to hacve // ceph services around on a machine running a query. wl, err := whiplash.New(whipconf, false) if err != nil { quit(fmt.Errorf("can't read configuration file: %s\n", err)) } // validate user input err = validateInput() if err != nil { quit(err) } // handle non-networked commands switch args[0] { case "help": showHelp() quit(nil) case "version": showVersion() quit(nil) } // set up configuration and create aclient instance pcconf := &pclient.Config{ Addr: wl.Aggregator.BindAddr + ":" + wl.Aggregator.QueryPort, Timeout: 100, } c, err := pclient.NewTCP(pcconf) if err != nil { quit(fmt.Errorf("can't connect to aggregator: %s", err)) } defer c.Close() // stitch together the non-option arguments into our request req := strings.Join(flag.Args(), " ") // and dispatch it to the server! respj, err := c.Dispatch([]byte(req)) if err != nil { quit(fmt.Errorf("sending request to aggregator failed: %s", err)) } // vivify response and handle errors resp := new(whiplash.QueryResponse) err = json.Unmarshal(respj, &resp) if err != nil { quit(fmt.Errorf("failure unmarshaling json\nerror: %s\njson: %s", err, string(respj))) } if resp.Code >= 400 { quit(fmt.Errorf("there was a problem with the request:\n%s", string(respj))) } // if -j has been specified, print the raw response data and exit if dumpjson { fmt.Println(string(resp.Data)) os.Exit(0) } // else, we have to hand off to a pretty-printing routine // TODO write a pretty-printing routine }