Beispiel #1
0
func main() {
	var (
		configFile  string
		help        bool
		fieldName   string
		monitorAddr string
		showVersion bool
	)
	flag.StringVar(&configFile, "c", "", "configuration file path")
	flag.BoolVar(&help, "h", false, "show help message")
	flag.BoolVar(&help, "help", false, "show help message")
	flag.StringVar(&fieldName, "f", hydra.DefaultFieldName, "fieldname of fluentd log message attribute (DEFAULT: message)")
	flag.StringVar(&monitorAddr, "monitor", "", "monitor HTTP server address")
	flag.StringVar(&monitorAddr, "m", "", "monitor HTTP server address")
	flag.BoolVar(&showVersion, "v", false, "show version")
	flag.BoolVar(&showVersion, "version", false, "show version")
	flag.Parse()

	if showVersion {
		fmt.Println("version:", version)
		fmt.Printf("compiler:%s %s\n", runtime.Compiler, runtime.Version())
		fmt.Println("build:", buildDate)
		os.Exit(0)
	}
	if help {
		usage()
	}
	if pprofile := os.Getenv("PPROF"); pprofile != "" {
		f, err := os.Create(pprofile)
		if err != nil {
			log.Fatal("[error] Can't create profiling stat file.", err)
		}
		log.Println("[info] StartCPUProfile() stat file", f.Name())
		pprof.StartCPUProfile(f)
	}

	done := make(chan os.Signal, 1)
	signal.Notify(done, trapSignals...)
	if configFile != "" {
		config, err := hydra.ReadConfig(configFile)
		if err != nil {
			log.Println("Can't load config", err)
			os.Exit(2)
		}
		run(config)
	} else if args := flag.Args(); len(args) >= 3 {
		config := hydra.NewConfigByArgs(args, fieldName, monitorAddr)
		run(config)
	} else {
		usage()
	}
	sig := <-done
	log.Println("[info] SIGNAL", sig, "exit.")
	pprof.StopCPUProfile()
	os.Exit(0)
}
func TestReadConfig(t *testing.T) {
	config, err := hydra.ReadConfig("./config_test.toml")
	if err != nil {
		t.Error("read config failed", err)
	}
	fmt.Printf("%#v\n", config)
	if config.TagPrefix != "foo" {
		t.Error("invalid TagPrefix got", config.TagPrefix, "expected", "foo")
	}
	if config.FieldName != "message" {
		t.Error("invalid FieldName got", config.FieldName, "expected", "msg")
	}
	if config.ReadBufferSize != 1024 {
		t.Error("invalid ReadBufferSize got", config.ReadBufferSize)
	}
	if config.ServerRoundRobin != true {
		t.Error("invalid ServerRoundRobin got", config.ServerRoundRobin)
	}

	if len(config.Servers) != 2 {
		t.Errorf("invalid Servers got %#v", config.Servers)
	}
	if config.Servers[0].Host != "127.0.0.1" || config.Servers[0].Port != 24224 {
		t.Errorf("invalid Servers[0] got %#v", config.Servers[0])
	}
	if config.Servers[1].Host != "127.0.0.1" || config.Servers[1].Port != 24225 {
		t.Errorf("invalid Servers[1] got %#v", config.Servers[1])
	}

	if len(config.Logs) != 5 {
		t.Errorf("invalid Logs got %#v", config.Logs)
	}
	if c := config.Logs[0]; c.Tag != "foo.tag1" ||
		c.File != "/tmp/foo.log" ||
		c.FieldName != "message" ||
		c.TimeParse != false {
		t.Errorf("invalid Logs[0] got %#v", c)
	}

	if c := config.Logs[1]; c.Tag != "foo.tag2" ||
		c.File != "/tmp/bar.log" ||
		c.FieldName != "msg" ||
		c.TimeParse != false {
		t.Errorf("invalid Logs[1] got %#v", c)
	}

	if c := config.Logs[2]; c.Tag != "foo.ltsv" ||
		c.File != "/tmp/baz.log" ||
		c.TimeParse != true ||
		c.TimeKey != "time" ||
		c.TimeFormat != "2006-01-02T15:04:05Z07:00" {
		t.Errorf("invalid Logs[2] got %#v", c)
	}

	if c := config.Logs[3]; c.Tag != "foo.ltsv" ||
		c.File != "/tmp/bazz.log" ||
		c.TimeParse != true ||
		c.TimeKey != "timestamp" ||
		c.TimeFormat != "02/Jan/2006:15:04:05 Z0700" {
		t.Errorf("invalid Logs[3] got %#v", c)
	}

	if c := config.Logs[4]; c.Tag != "foo.regexp" ||
		c.File != "/tmp/regexp.log" ||
		c.Format != hydra.FormatRegexp ||
		c.TimeParse != true ||
		c.TimeKey != "time" ||
		c.TimeFormat != hydra.TimeFormatApache {
		t.Errorf("invalid Logs[4] got %#v", c)
	}

	if config.Receiver.Host != "localhost" || config.Receiver.Port != 24224 {
		t.Errorf("invalid Receiver got %#v", config.Receiver)
	}

	if config.Monitor.Host != "127.0.0.2" || config.Monitor.Port != 24223 {
		t.Errorf("invalid Monitor got %#v", config.Monitor)
	}
}
Beispiel #3
0
func main() {
	var (
		configFile  string
		help        bool
		fieldName   string
		monitorAddr string
		showVersion bool
	)
	flag.StringVar(&configFile, "c", "", "configuration file path")
	flag.BoolVar(&help, "h", false, "show help message")
	flag.BoolVar(&help, "help", false, "show help message")
	flag.StringVar(&fieldName, "f", hydra.DefaultFieldName, "fieldname of fluentd log message attribute (DEFAULT: message)")
	flag.StringVar(&monitorAddr, "monitor", "", "monitor HTTP server address")
	flag.StringVar(&monitorAddr, "m", "", "monitor HTTP server address")
	flag.BoolVar(&showVersion, "v", false, "show version")
	flag.BoolVar(&showVersion, "version", false, "show version")
	flag.Parse()

	if showVersion {
		fmt.Println("version:", version)
		fmt.Printf("compiler:%s %s\n", runtime.Compiler, runtime.Version())
		fmt.Println("build:", buildDate)
		os.Exit(0)
	}
	if help {
		usage()
	}
	if pprofile := os.Getenv("PPROF"); pprofile != "" {
		f, err := os.Create(pprofile)
		if err != nil {
			log.Fatal("[error] Can't create profiling stat file.", err)
		}
		log.Println("[info] StartCPUProfile() stat file", f.Name())
		pprof.StartCPUProfile(f)
	}

	var (
		config *hydra.Config
		err    error
	)
	sigCh := make(chan os.Signal, 1)
	signal.Notify(sigCh, trapSignals...)
	if configFile != "" {
		config, err = hydra.ReadConfig(configFile)
		if err != nil {
			log.Println("Can't load config", err)
			os.Exit(2)
		}
	} else if args := flag.Args(); len(args) >= 3 {
		config = hydra.NewConfigByArgs(args, fieldName, monitorAddr)
	} else {
		usage()
	}

	context := hydra.Run(config)
	go func() {
		context.InputProcess.Wait()
		sigCh <- hydra.NewSignal("all input processes terminated")
	}()

	// waiting for all input processes are terminated or got os signal
	sig := <-sigCh

	log.Println("[info] SIGNAL", sig, "shutting down")
	pprof.StopCPUProfile()

	go func() {
		time.Sleep(1 * time.Second) // at least wait 1 sec
		sig, ok := <-sigCh
		if !ok {
			return // closed
		}
		log.Println("[warn] SIGNAL", sig, "before shutdown completed. aborted")
		os.Exit(1)
	}()

	context.Shutdown()
	os.Exit(0)
}