Beispiel #1
0
func TestCollector(t *testing.T) {
	window := time.Millisecond
	c := xfer.NewCollector(window)

	r1 := report.MakeReport()
	r1.Endpoint.NodeMetadatas["foo"] = report.MakeNodeMetadata()

	r2 := report.MakeReport()
	r2.Endpoint.NodeMetadatas["bar"] = report.MakeNodeMetadata()

	if want, have := report.MakeReport(), c.Report(); !reflect.DeepEqual(want, have) {
		t.Error(test.Diff(want, have))
	}

	c.Add(r1)
	if want, have := r1, c.Report(); !reflect.DeepEqual(want, have) {
		t.Error(test.Diff(want, have))
	}

	c.Add(r2)

	merged := report.MakeReport()
	merged = merged.Merge(r1)
	merged = merged.Merge(r2)
	if want, have := merged, c.Report(); !reflect.DeepEqual(want, have) {
		t.Error(test.Diff(want, have))
	}
}
Beispiel #2
0
func TestMerge(t *testing.T) {
	log.SetOutput(ioutil.Discard)

	var (
		p1Addr = "localhost:7888"
		p2Addr = "localhost:7889"
	)

	p1, err := xfer.NewTCPPublisher(p1Addr)
	if err != nil {
		t.Fatal(err)
	}
	defer p1.Close()

	p2, err := xfer.NewTCPPublisher(p2Addr)
	if err != nil {
		t.Fatal(err)
	}
	defer p2.Close()

	batchTime := 100 * time.Millisecond
	c := xfer.NewCollector(batchTime, "id")
	c.Add(p1Addr)
	c.Add(p2Addr)
	defer c.Stop()
	time.Sleep(batchTime / 10) // connect

	k1, k2 := report.MakeHostNodeID("p1"), report.MakeHostNodeID("p2")

	{
		r := report.MakeReport()
		r.Host.NodeMetadatas[k1] = report.NodeMetadata{"host_name": "test1"}
		p1.Publish(r)
	}
	{
		r := report.MakeReport()
		r.Host.NodeMetadatas[k2] = report.NodeMetadata{"host_name": "test2"}
		p2.Publish(r)
	}

	success := make(chan struct{})
	go func() {
		defer close(success)
		for r := range c.Reports() {
			if r.Host.NodeMetadatas[k1]["host_name"] != "test1" {
				continue
			}
			if r.Host.NodeMetadatas[k2]["host_name"] != "test2" {
				continue
			}
			return
		}
	}()

	select {
	case <-success:
	case <-time.After(2 * batchTime):
		t.Errorf("collector didn't capture both reports")
	}
}
Beispiel #3
0
func main() {
	var (
		window       = flag.Duration("window", 15*time.Second, "window")
		listen       = flag.String("http.address", ":"+strconv.Itoa(xfer.AppPort), "webserver listen address")
		logPrefix    = flag.String("log.prefix", "<app>", "prefix for each log line")
		printVersion = flag.Bool("version", false, "print version number and exit")
	)
	flag.Parse()

	if *printVersion {
		fmt.Println(version)
		return
	}

	if !strings.HasSuffix(*logPrefix, " ") {
		*logPrefix += " "
	}
	log.SetPrefix(*logPrefix)

	defer log.Print("app exiting")

	rand.Seed(time.Now().UnixNano())
	uniqueID = strconv.FormatInt(rand.Int63(), 16)
	log.Printf("app starting, version %s, ID %s", version, uniqueID)

	c := xfer.NewCollector(*window)
	http.Handle("/", Router(c))
	go func() {
		log.Printf("listening on %s", *listen)
		log.Print(http.ListenAndServe(*listen, nil))
	}()
	log.Printf("%s", <-interrupt())
}
Beispiel #4
0
func main() {
	var (
		window       = flag.Duration("window", 15*time.Second, "window")
		listen       = flag.String("http.address", ":"+strconv.Itoa(xfer.AppPort), "webserver listen address")
		printVersion = flag.Bool("version", false, "print version number and exit")
	)
	flag.Parse()

	if *printVersion {
		fmt.Println(version)
		return
	}

	rand.Seed(time.Now().UnixNano())
	id := strconv.FormatInt(rand.Int63(), 16)
	log.Printf("app starting, version %s, ID %s", version, id)

	c := xfer.NewCollector(*window)
	http.Handle("/", Router(c))
	irq := interrupt()
	go func() {
		log.Printf("listening on %s", *listen)
		log.Print(http.ListenAndServe(*listen, nil))
		irq <- syscall.SIGINT
	}()
	<-irq
	log.Printf("shutting down")
}
Beispiel #5
0
func main() {
	var (
		defaultProbes = fmt.Sprintf("localhost:%d", xfer.ProbePort)
		probes        = flag.String("probes", defaultProbes, "list of probe endpoints, comma separated")
		batch         = flag.Duration("batch", 1*time.Second, "batch interval")
		window        = flag.Duration("window", 15*time.Second, "window")
		listen        = flag.String("http.address", ":"+strconv.Itoa(xfer.AppPort), "webserver listen address")
	)
	flag.Parse()

	xfer.MaxBackoff = 10 * time.Second
	c := xfer.NewCollector(*batch, "id")
	for _, addr := range strings.Split(*probes, ",") {
		c.Add(addr)
	}
	defer c.Stop()
	lifo := NewReportLIFO(c, *window)
	defer lifo.Stop()

	http.Handle("/svg", handleSVG(lifo))
	http.Handle("/txt", handleTXT(lifo))
	http.Handle("/", http.HandlerFunc(handleHTML))

	irq := interrupt()
	go func() {
		log.Printf("listening on %s", *listen)
		log.Print(http.ListenAndServe(*listen, nil))
		irq <- syscall.SIGINT
	}()
	<-irq
	log.Printf("shutting down")
}
Beispiel #6
0
func main() {
	var (
		listen  = flag.String("listen", ":"+strconv.Itoa(xfer.ProbePort), "listen address")
		probes  = flag.String("probes", "", "list of all initial probes, comma separated")
		batch   = flag.Duration("batch", 1*time.Second, "batch interval")
		version = flag.Bool("version", false, "print version number and exit")
	)
	flag.Parse()

	if len(flag.Args()) != 0 {
		flag.Usage()
		os.Exit(1)
	}

	if *version {
		//fmt.Printf("%s\n", probe.Version)
		return
	}

	if *probes == "" {
		log.Fatal("no probes given via -probes")
	}

	log.Printf("starting")

	fixedAddresses := strings.Split(*probes, ",")

	// Collector deals with the probes, and generates a single merged report
	// every second.
	c := xfer.NewCollector(*batch, "id")
	for _, addr := range fixedAddresses {
		c.Add(addr)
	}
	defer c.Stop()

	publisher, err := xfer.NewTCPPublisher(*listen)
	if err != nil {
		log.Fatal(err)
	}
	defer publisher.Close()
	log.Printf("listening on %s\n", *listen)

	var fixedIPs []string
	for _, a := range fixedAddresses {
		if addr, _, err := net.SplitHostPort(a); err == nil {
			fixedIPs = append(fixedIPs, addr)
		}
	}
	go discover(c, publisher, fixedIPs)

	<-interrupt()

	log.Printf("shutting down")
}
Beispiel #7
0
func main() {
	var (
		defaultProbes = fmt.Sprintf("localhost:%d", xfer.ProbePort)
		probes        = flag.String("probes", defaultProbes, "list of probe endpoints, comma separated")
	)
	flag.Parse()
	if len(flag.Args()) != 0 {
		flag.Usage()
		os.Exit(1)
	}

	// Collector deals with the probes, and generates merged reports.
	xfer.MaxBackoff = 1 * time.Second
	c := xfer.NewCollector(1*time.Second, "id")
	for _, addr := range strings.Split(*probes, ",") {
		c.Add(addr)
	}
	defer c.Stop()

	report := report.MakeReport()
	irq := interrupt()
OUTER:
	for {
		select {
		case r := <-c.Reports():
			report.Merge(r)
		case <-irq:
			break OUTER
		}
	}

	b, err := json.Marshal(report)
	if err != nil {
		panic(err)
	}
	fmt.Print(string(b))
}
Beispiel #8
0
func main() {
	var (
		defaultProbes = []string{fmt.Sprintf("localhost:%d", xfer.ProbePort), fmt.Sprintf("scope.weave.local:%d", xfer.ProbePort)}
		logfile       = flag.String("log", "stderr", "stderr, syslog, or filename")
		batch         = flag.Duration("batch", 1*time.Second, "batch interval")
		window        = flag.Duration("window", 15*time.Second, "window")
		listen        = flag.String("http.address", ":"+strconv.Itoa(xfer.AppPort), "webserver listen address")
		printVersion  = flag.Bool("version", false, "print version number and exit")
	)
	flag.Parse()
	probes := append(defaultProbes, flag.Args()...)

	if *printVersion {
		fmt.Println(version)
		return
	}

	switch *logfile {
	case "stderr":
		break // by default

	case "syslog":
		w, err := syslog.New(syslog.LOG_INFO, "scope-app")
		if err != nil {
			log.Print(err)
			return
		}
		defer w.Close()
		log.SetFlags(0)
		log.SetOutput(w)

	default: // file
		f, err := os.OpenFile(*logfile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
		if err != nil {
			log.Print(err)
			return
		}
		defer f.Close()
		log.SetOutput(f)
	}

	rand.Seed(time.Now().UnixNano())
	id := strconv.FormatInt(rand.Int63(), 16)
	log.Printf("app starting, version %s, id %s", version, id)

	// Collector deals with the probes, and generates merged reports.
	c := xfer.NewCollector(*batch, id)
	defer c.Stop()

	r := newStaticResolver(probes, c.Add)
	defer r.Stop()

	lifo := NewReportLIFO(c, *window)
	defer lifo.Stop()

	http.Handle("/", Router(lifo))
	irq := interrupt()
	go func() {
		log.Printf("listening on %s", *listen)
		log.Print(http.ListenAndServe(*listen, nil))
		irq <- syscall.SIGINT
	}()
	<-irq
	log.Printf("shutting down")
}