Esempio n. 1
0
// Reads a single arbitrary type and returns the proper StateValue.
func readField(val reflect.Value) *protocol.StateValue {
	msg := &protocol.StateValue{}
	switch f := val.(type) {
	case *reflect.BoolValue:
		msg.Type = protocol.NewStateValue_Type(protocol.StateValue_BOOL)
		msg.BoolVal = proto.Bool(f.Get())
	case *reflect.IntValue:
		msg.Type = protocol.NewStateValue_Type(protocol.StateValue_INT)
		msg.IntVal = proto.Int(int(f.Get()))
	case *reflect.FloatValue:
		msg.Type = protocol.NewStateValue_Type(protocol.StateValue_FLOAT)
		msg.FloatVal = proto.Float32(float32(f.Get()))
	case *reflect.StringValue:
		msg.Type = protocol.NewStateValue_Type(protocol.StateValue_STRING)
		msg.StringVal = proto.String(f.Get())
	case *reflect.SliceValue:
		msg.Type = protocol.NewStateValue_Type(protocol.StateValue_ARRAY)
		msg.ArrayVal = makeStateValueArray(f, f.Len())
	case *reflect.StructValue:
		msg = readStructField(f)
	case *reflect.PtrValue:
		return readField(reflect.Indirect(f)) // Dereference and recurse
	default:
		panic("State value not supported: " + val.Type().String())
	}
	return msg
}
Esempio n. 2
0
func newTestMessage() *pb.MyMessage {
	msg := &pb.MyMessage{
		Count: proto.Int32(42),
		Name:  proto.String("Dave"),
		Quote: proto.String(`"I didn't want to go."`),
		Pet:   []string{"bunny", "kitty", "horsey"},
		Inner: &pb.InnerMessage{
			Host:      proto.String("footrest.syd"),
			Port:      proto.Int32(7001),
			Connected: proto.Bool(true),
		},
		Others: []*pb.OtherMessage{
			&pb.OtherMessage{
				Key:   proto.Int64(0xdeadbeef),
				Value: []byte{1, 65, 7, 12},
			},
			&pb.OtherMessage{
				Weight: proto.Float32(6.022),
				Inner: &pb.InnerMessage{
					Host: proto.String("lesha.mtv"),
					Port: proto.Int32(8002),
				},
			},
		},
		Bikeshed: pb.NewMyMessage_Color(pb.MyMessage_BLUE),
		Somegroup: &pb.MyMessage_SomeGroup{
			GroupField: proto.Int32(8),
		},
		// One normally wouldn't do this.
		// This is an undeclared tag 13, as a varint (wire type 0) with value 4.
		XXX_unrecognized: []byte{13<<3 | 0, 4},
	}
	ext := &pb.Ext{
		Data: proto.String("Big gobs for big rats"),
	}
	if err := proto.SetExtension(msg, pb.E_Ext_More, ext); err != nil {
		panic(err)
	}

	// Add an unknown extension. We marshal a pb.Ext, and fake the ID.
	b, err := proto.Marshal(&pb.Ext{Data: proto.String("3G skiing")})
	if err != nil {
		panic(err)
	}
	b = append(proto.EncodeVarint(104<<3|proto.WireBytes), b...)
	proto.SetRawExtension(msg, 104, b)

	// Extensions can be plain fields, too, so let's test that.
	b = append(proto.EncodeVarint(105<<3|proto.WireVarint), 19)
	proto.SetRawExtension(msg, 105, b)

	return msg
}
Esempio n. 3
0
File: nagios.go Progetto: pjjw/ncd
// parse an individial element of perfdata
func parsePerfDataElement(str string) (pd *PerfData, err os.Error) {
	m := strings.SplitN(str, "=", 2)
	if len(m) < 2 {
		err = os.NewError("no value")
		return nil, err
	}
	name := m[0]
	pd = &PerfData{Name: proto.String(name)}
	n := strings.Split(m[1], ";")
	nf := make([]float32, len(n))
	var units string
	nf[0], units, err = splitUnits(n[0])
	pd.Value = proto.Float32(nf[0])
	if units != "" {
		pd.Units = proto.String(units)
	}
	// convert remaining values to floats if they exist
	for i, v := range n[1:len(n)] {
		nf[i+1], err = strconv.Atof32(v)
		if err != nil {
			return nil, err
		}
	}
	switch {
	case len(nf) >= 5:
		pd.Maximum = proto.Float32(nf[4])
		fallthrough
	case len(nf) >= 4:
		pd.Minimum = proto.Float32(nf[3])
		fallthrough
	case len(nf) >= 3:
		pd.Critical = proto.Float32(nf[2])
		fallthrough
	case len(nf) >= 2:
		pd.Warning = proto.Float32(nf[1])
	}
	return pd, nil
}
Esempio n. 4
0
	StartTimestamp: proto.Int64(time.Nanoseconds() - 11111390),
	EndTimestamp:   proto.Int64(time.Nanoseconds()),
}

type PerfElementCheck struct{ in, out string }
type PerfDataCheck struct{ in, outstr, outpd string }

var perfdatacheck = CheckResult{
	Hostname:       proto.String(hostname),
	ServiceName:    proto.String(servicename),
	Status:         &status,
	CheckOutput:    proto.String(conp),
	StartTimestamp: proto.Int64(time.Nanoseconds() - 11111390),
	EndTimestamp:   proto.Int64(time.Nanoseconds()),
	Perfdata: []*PerfData{
		&PerfData{Name: proto.String("potato"), Value: proto.Float32(1)},
		&PerfData{Name: proto.String("potatoe"), Value: proto.Float32(2)},
	},
}

var pdtests = [...]PerfDataCheck{
	{"OK: matched | time=2sec", "OK: matched ", "[name:\"time\" value:2 units:\"sec\" ]"},
	{"OK:\tcount to potato | potato=1\n hoooraaaay\n | potatoe=2", "OK:\tcount to potato \n hoooraaaay\n ", "[name:\"potato\" value:1  name:\"potatoe\" value:2 ]"},
	{"OK:\tcount to potato | potato=1\n hoooraaaay\n | potatoe=2%", "OK:\tcount to potato \n hoooraaaay\n ", "[name:\"potato\" value:1  name:\"potatoe\" value:2 units:\"%\" ]"},
}

var petests = [...]PerfElementCheck{
	{"data=1elem;5;10;1;4", "name:\"data\" value:1 units:\"elem\" warning:5 critical:10 minimum:1 maximum:4 "},
	{"bananas=3", "name:\"bananas\" value:3 "},
}