Exemplo n.º 1
0
func (r *RRASpec) UnmarshalText(text []byte) error {
	r.Xff = 0.5
	parts := strings.SplitN(string(text), ":", 4)
	if len(parts) < 2 || len(parts) > 4 {
		return fmt.Errorf("Invalid RRA specification (not enough or too many elements): %q", string(text))
	}

	// If first character of first part is a digit, assume we're
	// skipping CF and default to WMEAN.
	if len(parts[0]) > 0 && strings.Contains("0123456789", string(parts[0][0])) {
		parts = append([]string{"WMEAN"}, parts...)
	}

	switch strings.ToUpper(parts[0]) {
	case "WMEAN":
		r.Function = rrd.WMEAN
	case "MIN":
		r.Function = rrd.MIN
	case "MAX":
		r.Function = rrd.MAX
	case "LAST":
		r.Function = rrd.LAST
	default:
		return fmt.Errorf("Invalid consolidation: %q (valid funcs: wmean, min, max, last)", parts[0])
	}

	var err error
	if r.Step, err = misc.BetterParseDuration(parts[1]); err != nil {
		return fmt.Errorf("Invalid Step: %q (%v)", parts[1], err)
	}
	if r.Size, err = misc.BetterParseDuration(parts[2]); err != nil {
		return fmt.Errorf("Invalid Size: %q (%v)", parts[2], err)
	}
	if (r.Size.Nanoseconds() % r.Step.Nanoseconds()) != 0 {
		newSize := time.Duration(r.Size.Nanoseconds()/r.Step.Nanoseconds()*r.Step.Nanoseconds()) * time.Nanosecond
		log.Printf("Size (%q) is not a multiple of Step (%q), auto adjusting Size to %v.", parts[2], parts[1], newSize)
		r.Size = newSize
		if newSize.Nanoseconds() == 0 {
			return fmt.Errorf("invalid Size (%v)", newSize)
		}
	}
	if len(parts) == 4 {
		var err error
		if r.Xff, err = strconv.ParseFloat(parts[3], 64); err != nil {
			return fmt.Errorf("Invalid XFF: %q (%v)", parts[3], err)
		}
	}
	return nil
}
Exemplo n.º 2
0
func parseTime(s string) (*time.Time, error) {

	if len(s) == 0 {
		return nil, nil
	}

	if s[0] == '-' { // relative
		if dur, err := misc.BetterParseDuration(s[1:len(s)]); err == nil {
			t := time.Now().Add(-dur)
			return &t, nil
		} else {
			return nil, fmt.Errorf("parseTime(): Error parsing relative time %q: %v", s, err)
		}
	} else { // absolute
		if s == "now" {
			t := time.Now()
			return &t, nil
		} else if i, err := strconv.ParseInt(s, 10, 64); err == nil {
			t := time.Unix(i, 0)
			return &t, nil
		} else {
			return nil, fmt.Errorf("parseTime(): Error parsing absolute time %q: %v", s, err)
		}
	}
}