Beispiel #1
0
func main() {
	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
	flag.Parse()

	ctx := context.Background()
	conn, err := client.NewRpcClient(ctx, *connectAddress)
	if err != nil {
		log.Fatalf("Error connecting to %s: %s", *connectAddress, err)
	}
	defer conn.Close()

	switch os.Args[1] {
	case "get":
		response, err := conn.GetConfig(ctx)
		if err != nil {
			log.Fatal(err)
		}

		fmt.Println(openinstrument.ProtoText(response))

	case "add_loadtest":
		runAddLoadtest(ctx, conn)
	case "slow_add_loadtest":
		runSlowAddLoadtest(ctx)
	}
}
Beispiel #2
0
func runSlowAddLoadtest(ctx context.Context) {
	log.Println("Running ADD load test")

	var sent, received int

	go func() {
		tick := time.Tick(1 * time.Second)
		for {
			select {
			case <-tick:
				log.Printf("Sent %d, received %d in the last second, latency %0.02fms", sent, received, 1.0/float64(received)*1000.0)
				received = 0
				sent = 0
			}
		}
	}()
	for {
		conn, err := client.NewRpcClient(ctx, *connectAddress)
		if err != nil {
			log.Fatalf("Error connecting to %s: %s", *connectAddress, err)
		}

		in, out, err := conn.Add(ctx)
		if err != nil {
			log.Printf("Error starting Add RPC: %s", err)
			return
		}

		v := variable.NewFromString("/test/var1{host=rage}").AsProto()
		request := &oproto.AddRequest{
			Stream: []*oproto.ValueStream{
				{
					Variable: v,
					Value: []*oproto.Value{
						{
							Timestamp:   openinstrument.NowMs(),
							DoubleValue: 1.0,
						},
					},
				},
			},
		}
		in <- request
		close(in)
		sent++
		<-out
		received++

		conn.Close()
	}
	log.Println("Complete")
}
Beispiel #3
0
func main() {
	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
	flag.Parse()
	if len(flag.Args()) < 1 {
		log.Fatal("Specify at least one variable to retrieve")
	}

	q, err := query.Parse(flag.Arg(0))
	if err != nil {
		log.Fatal("Invalid query:", err)
	}

	conn, err := client.NewRpcClient(context.Background(), *connectAddress)
	if err != nil {
		log.Fatalf("Error connecting to %s: %s", *connectAddress, err)
	}
	defer conn.Close()

	request := &oproto.GetRequest{
		Query:        q.AsProto(),
		MaxValues:    uint32(*maxValues),
		MaxVariables: uint32(*maxVariables),
	}

	log.Printf("Sending query: %s", openinstrument.ProtoText(q.AsProto()))
	c, err := conn.Get(context.Background(), request)
	if err != nil {
		log.Fatal(err)
	}

	for response := range c {
		for _, stream := range response.Stream {
			variable := variable.NewFromProto(stream.Variable).String()
			for _, value := range stream.Value {
				fmt.Printf("%s\t%s\t", variable, time.Unix(int64(value.Timestamp/1000), 0))
				if value.StringValue == "" {
					fmt.Printf("%f\n", value.DoubleValue)
				} else {
					fmt.Printf("%s\n", value.StringValue)
				}
			}
		}
	}

}
Beispiel #4
0
func main() {
	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
	flag.Parse()
	if len(flag.Args()) < 1 {
		log.Fatal("Specify at least one variable to retrieve")
	}

	if *connectAddress == "" {
		log.Fatal("--connect address:port required")
	}

	request := &oproto.ListRequest{
		Prefix:       variable.NewFromString(flag.Arg(0)).AsProto(),
		MaxVariables: uint32(*maxVariables),
	}
	if *maxAge != "" {
		d, _ := time.ParseDuration(*maxAge)
		request.Prefix.MinTimestamp = -d.Nanoseconds() / 1000000
	}

	conn, err := client.NewRpcClient(context.Background(), *connectAddress)
	if err != nil {
		log.Fatalf("Error connecting to %s: %s", *connectAddress, err)
	}
	defer conn.Close()

	c, err := conn.List(context.Background(), request)
	if err != nil {
		log.Fatal(err)
	}
	vars := []string{}
	for response := range c {
		for _, v := range response.Variable {
			vars = append(vars, variable.NewFromProto(v).String())
		}
	}
	sort.Strings(vars)
	for _, variable := range vars {
		fmt.Println(variable)
	}
}
Beispiel #5
0
func main() {
	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
	flag.Parse()
	ctx := context.Background()

	conn, err := client.NewRpcClient(ctx, *connectAddress)
	if err != nil {
		log.Fatalf("Error connecting to %s: %s", *connectAddress, err)
	}
	defer conn.Close()
	in, out, err := conn.Add(ctx)
	if err != nil {
		log.Fatal(err)
	}

	snmp := gosnmp.Default
	for _, host := range []string{"192.168.1.1"} {
		snmp.Target = host
		snmp.Community = "public"
		if err := snmp.Connect(); err != nil {
			log.Fatal(err)
		}
		defer snmp.Conn.Close()

		// VDSL-LINE-MIB::vdslPhysCurrLineRate.1 = Gauge32: 20265 kbps
		// VDSL-LINE-MIB::vdslPhysCurrLineRate.2 = Gauge32: 1315 kbps
		oids := []string{"1.3.6.1.2.1.10.97.1.1.2.1.10.1", "1.3.6.1.2.1.10.97.1.1.2.1.10.2"}
		result, err := snmp.Get(oids)
		if err != nil {
			log.Fatal(err)
		}

		request := &oproto.AddRequest{
			Stream: []*oproto.ValueStream{},
		}

		for _, snmpVar := range result.Variables {
			switch snmpVar.Name {
			case ".1.3.6.1.2.1.10.97.1.1.2.1.10.1":
				request.Stream = append(request.Stream, &oproto.ValueStream{
					Variable: variable.NewFromString(fmt.Sprintf("/network/adsl/downstream_rate{host=%s,unit=bps}", host)).AsProto(),
					Value: []*oproto.Value{
						{
							DoubleValue: float64(snmpVar.Value.(uint) * 1000),
							Timestamp:   openinstrument.NowMs(),
						},
					},
				})
			case ".1.3.6.1.2.1.10.97.1.1.2.1.10.2":
				request.Stream = append(request.Stream, &oproto.ValueStream{
					Variable: variable.NewFromString(fmt.Sprintf("/network/adsl/upstream_rate{host=%s,unit=bps}", host)).AsProto(),
					Value: []*oproto.Value{
						{
							DoubleValue: float64(snmpVar.Value.(uint) * 1000),
							Timestamp:   openinstrument.NowMs(),
						},
					},
				})
			}
		}

		in <- request
	}

	close(in)
	for response := range out {
		if !response.Success {
			fmt.Println(openinstrument.ProtoText(response))
		}
	}
}
Beispiel #6
0
func main() {
	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
	flag.Parse()

	ctx := context.Background()

	conn, err := client.NewRpcClient(ctx, *connectAddress)
	if err != nil {
		log.Fatalf("Error connecting to %s: %s", *connectAddress, err)
	}
	defer conn.Close()

	re, err := regexp.Compile("^([^\\s]+) (.+)$")
	if err != nil {
		log.Fatalf("Error compiling regex: %s", err)
	}

	timestamp := openinstrument.NowMs()

	// Read from stdin
	c := make(chan *oproto.ValueStream)
	go func() {
		defer close(c)
		scanner := bufio.NewScanner(os.Stdin)
		for scanner.Scan() {
			matches := re.FindStringSubmatch(scanner.Text())
			if len(matches) != 3 {
				log.Printf("Invalid input line: %s", scanner.Text())
				continue
			}
			v := variable.NewFromString(matches[1])
			value := &oproto.Value{Timestamp: timestamp}
			f, err := strconv.ParseFloat(matches[2], 64)
			if err != nil {
				value.StringValue = matches[2]
			} else {
				value.DoubleValue = f
			}
			c <- &oproto.ValueStream{
				Variable: v.AsProto(),
				Value:    []*oproto.Value{value},
			}
		}
	}()

	if *readTimeout != 0 {
		ctx, _ = context.WithTimeout(ctx, *readTimeout)
	}
	in, out, err := conn.Add(ctx)
	if err != nil {
		log.Fatal(err)
	}

	func() {
		defer close(in)
		interval := time.Tick(1 * time.Second)
		request := &oproto.AddRequest{}
		for {
			select {
			case stream := <-c:
				if stream == nil {
					in <- request
					return
				}
				request.Stream = append(request.Stream, stream)
			case <-interval:
				in <- request
				request = &oproto.AddRequest{}
				timestamp = openinstrument.NowMs()
			case response := <-out:
				if response == nil {
					return
				}
				log.Printf("%s", response)
			}
		}
		in <- request
		log.Printf("done")
	}()

	for response := range out {
		log.Printf("%s", response)
	}
}