Exemplo n.º 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)
	}
}
Exemplo n.º 2
0
func main() {
	watcher, err := lotf.NewTailWatcher()
	if err != nil {
		fmt.Fprintf(os.Stderr, "error - could not create watcher: %s\n", err)
	}

	// glog has been set up in parseFlags()
	flags, nlines, err := parseFlags()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err)
		os.Exit(1)
	}

	errch := make(chan error, 512) // XXX: magic number
	rcs := make([]resource, len(flags))
	for i, rc := range flags {
		if rc.tcpaddr == nil && rc.udpaddr == nil {
			fmt.Fprintf(os.Stderr, "error - no inet4 server specified\n")
			os.Exit(1)
		}

		glog.Infof("adding watch - path: %s, filter: %s", rc.filename, rc.filter)
		if rcs[i].tail, err = watcher.Add(rc.filename, nlines, rc.filter, rc.buflines); err != nil {
			glog.Fatalf("could not watch: %s\n", err)
		}
		rcs[i].filter = rc.filter
		glog.Infof("watch added - path: %s, filter: %s", rc.filename, rc.filter)

		if rc.tcpaddr != nil {
			glog.Infof("starting TCP service - addr: %v)", rc.tcpaddr)
			if rcs[i].ssvr, err = NewTCPServer(rcs[i].tail, rc.tcpaddr); err != nil {
				fmt.Fprintf(os.Stderr, "error - could not start TCP service: %s\n", err)
				os.Exit(1)
			}
			go rcs[i].ssvr.Run(errch)
		}
		if rc.udpaddr != nil {
			glog.Infof("starting UDP service - addr: %v", rc.udpaddr)
			if rcs[i].usvr, err = NewUDPServer(rcs[i].tail, rc.udpaddr); err != nil {
				fmt.Fprintf(os.Stderr, "error - could not start UDP service: %s\n", err)
				os.Exit(1)
			}
			go rcs[i].usvr.Run(errch)
		}
	}

	// signal handler
	go sighandler(watcher, rcs, errch)

	// daemonize?
	for err := range errch {
		glog.Errorf("%s", err)
		break
	}
}
Exemplo n.º 3
0
func main() {
	var err error

	cfg, err = parseFlags()
	if err != nil {
		glog.Fatalf("config error: %s", err)
	}

	cookies = NewTickMap(time.Duration(cfg.interval) * time.Second)
	watcher, err := lotf.NewTailWatcher()
	if err != nil {
		glog.Fatalf("NewTailWatcher: %s", err)
	}
	go func() {
		for err := range watcher.Error {
			glog.Errorf("error from watcher: %s", err)
		}
	}()

	templateNames := make(map[string]*template.Template)
	defaultTemplate := template.Must(template.ParseFiles(cfg.template))
	templates[defaultTemplate.Name()] = defaultTemplate
	for k, v := range cfg.lotfs {
		glog.Infof("creating tail: %s", v.filename)
		t, err := watcher.Add(v.filename, cfg.buflines, v.filter, cfg.lastlines)
		if err != nil {
			glog.Fatalf("Add to watcher - %s: %s", v.filename, err)
		}
		if len(v.template) == 0 {
			templates[k] = defaultTemplate
		} else {
			t := template.Must(template.ParseFiles(v.template))
			if _, found := templateNames[t.Name()]; !found {
				templateNames[t.Name()] = t
			}
			templates[k] = templateNames[t.Name()]
		}
		tails[k] = t
	}

	http.HandleFunc(cfg.root, handler)
	l, err := net.Listen("tcp", cfg.addr)
	if err != nil {
		glog.Fatalf("listen: %s", err)
	}
	s := &http.Server{}
	glog.Infof("start serving - addr: %s, path: %s", cfg.addr, cfg.root)
	if err := s.Serve(l); err != nil {
		glog.Fatalf("http.Serve: %s", err)
	}
}