Пример #1
0
func main() {
	flag.Usage = usage
	flag.Parse()
	if flag.NArg() < 1 {
		flag.Usage()
		os.Exit(1)
	}

	var err error

	argl := list.New()
	for _, s := range os.Args[1:] {
		args := strings.Split(s, ":")
		arg := &Arg{args[0], nil, 0}
		if len(args) > 1 && len(args[1]) > 0 {
			if arg.filter, err = lotf.RegexpFilter(args[1]); err != nil {
				glog.Fatalf("could not create filter from: %s, error: %s", args[1], err)
			}
		}
		if len(args) > 2 {
			if arg.lines, err = strconv.ParseUint(args[2], 0, 64); err != nil {
				glog.Fatalf("invalid number of lines: %s", args[2])
			}
		}
		argl.PushBack(arg)
	}

	tw, err := lotf.NewTailWatcher()
	if err != nil {
		glog.Fatalf("could not create watcher: %s", err)
	}
	go func() {
		for err = range tw.Error {
			fmt.Printf("ERROR: %s\n", err)
			os.Exit(1)
		}
	}()

	ch := make(chan string)
	for e := argl.Front(); e != nil; e = e.Next() {
		arg := e.Value.(*Arg)
		go func() {
			maxlines := 1
			if arg.lines > 0 {
				maxlines = int(arg.lines)
			}
			tail, err := tw.Add(arg.fname, maxlines, arg.filter, int(arg.lines))
			if err != nil {
				glog.Fatalf("could not add %s to watcher: %s", arg.fname, err)
			}
			for s := tail.WaitNext(); s != nil; s = tail.WaitNext() {
				ch <- *s
			}
		}()
	}

	for line := range ch {
		fmt.Println(line)
	}
}
Пример #2
0
func makeResources(fname string) ([]LTFResource, error) {
	var err error

	r, err := os.Open(fname)
	if err != nil {
		return nil, err
	}

	s := make([]RCEntry, 0)
	dec := json.NewDecoder(r)
	for {
		if err := dec.Decode(&s); err == io.EOF {
			break
		} else if err != nil {
			return nil, err
		}
	}

	t := make([]LTFResource, len(s))
	for i, e := range s {
		t[i].filename = e.File
		t[i].buflines = e.Buflines
		if len(e.Filter) > 0 {
			if t[i].filter, err = lotf.RegexpFilter(e.Filter); err != nil {
				return nil, err
			}
		}

		if len(e.Udpaddr) > 0 {
			if t[i].udpaddr, err = net.ResolveUDPAddr("udp4", e.Udpaddr); err != nil {
				return nil, err
			}
		}
		if len(e.Tcpaddr) > 0 {
			if t[i].tcpaddr, err = net.ResolveTCPAddr("tcp4", e.Tcpaddr); err != nil {
				return nil, err
			}
		}
	}

	return t, nil
}
Пример #3
0
func makeResources(fname string) (*config, error) {
	r, err := os.Open(fname)
	if err != nil {
		return nil, err
	}

	s := &Config{Lotfs: make([]LotfConfig, 0)}
	dec := json.NewDecoder(r)
	for {
		if err := dec.Decode(s); err == io.EOF {
			break
		} else if err != nil {
			return nil, err
		}
	}

	lotfs := make(map[string]*lotfConfig)
	for _, v := range s.Lotfs {
		// XXX: check required json entries
		if len(v.Name) == 0 {
			return nil, errors.New("no name specified in lotfs")
		}
		if _, found := lotfs[v.Name]; found {
			return nil, errors.New(fmt.Sprintf("founnd dup name: %s", v.Name))
		}
		var filter lotf.Filter
		if len(v.Filter) > 0 {
			filter, err = lotf.RegexpFilter(v.Filter)
			if err != nil {
				return nil, errors.New(fmt.Sprintf("create filter: %s", v.Filter))
			}
		} else {
			filter = nil
		}
		if len(v.File) == 0 {
			return nil, errors.New(fmt.Sprintf("no file specified: %s", v.Name))
		}

		lotfs[v.Name] = &lotfConfig{
			filename: v.File,
			filter:   filter,
			template: v.Template,
		}
	}

	if len(s.Address) == 0 {
		return nil, errors.New("address is not specified")
	}
	if len(s.Root) == 0 {
		return nil, errors.New("root is not specified")
	}
	if len(s.Template) == 0 {
		return nil, errors.New("default template is not specified")
	}
	if s.Interval == 0 {
		return nil, errors.New("interval is not specified")
	}
	if s.Buflines == 0 {
		return nil, errors.New("buflines is not specified")
	}
	if s.Lastlines == 0 {
		return nil, errors.New("lastlines is not specified")
	}
	if len(lotfs) == 0 {
		return nil, errors.New("no lotf specified")
	}

	if s.Root[len(s.Root)-1] != '/' {
		s.Root += "/"
	}
	return &config{
		addr:      s.Address,
		root:      s.Root,
		template:  s.Template,
		interval:  s.Interval,
		buflines:  s.Buflines,
		lastlines: s.Lastlines,
		lotfs:     lotfs,
	}, nil
}