Esempio n. 1
0
func (reader *ReaderType) Run(output chan common.MapStr) error {
	logp.Info("Attempting to start %d inputs", len(reader.Input))

	for _, plugin := range reader.Input {
		err := plugin.Run(output)
		if err != nil {
			logp.Err("Fail to start input plugin %s : %s", plugin.InputType(), err)
			return err
		} else {
			logp.Info("Started input plugin %s", plugin.InputType())
		}
	}
	return nil
}
Esempio n. 2
0
func (l *SyslogInput) Init(config inputs.MothershipConfig) error {

	if config.Port == 0 {
		return errors.New("No Input Port specified")
	}
	l.Port = config.Port

	if config.Type == "" {
		return errors.New("No Event Type specified")
	}
	l.Type = config.Type

	logp.Info("[SyslogInput] Using Port %d", l.Port)
	logp.Info("[SyslogInput] Adding Event Type %s", l.Type)

	return nil
}
Esempio n. 3
0
func (l *ProcfsInput) Init(config inputs.MothershipConfig) error {

	if config.Sleep_interval == 0 || config.Sleep_interval < 15 {
		l.Sleep = 15 /* default to 15s */
	} else {
		l.Sleep = config.Sleep_interval
	}

	logp.Info("[ProcfsInput] Initialized, using sleep interval " + strconv.Itoa(l.Sleep))

	return nil
}
Esempio n. 4
0
func newInputInstance(name string) inputs.InputInterface {
	logp.Info("creating new instance of type %s", name)
	switch name {
	case "tcp":
		return new(tcp.TcpInput)
	case "syslog":
		return new(syslog.SyslogInput)
	case "procfs":
		return new(procfs.ProcfsInput)
	case "null":
		return new(null.NullInput)
	}
	return nil
}
Esempio n. 5
0
func (l *TcpInput) Run(output chan common.MapStr) error {
	logp.Info("[TcpInput] Running TCP Input")
	server, err := net.Listen("tcp", ":"+strconv.Itoa(l.Port))
	if err != nil {
		logp.Err("couldn't start listening: " + err.Error())
		return nil
	}
	logp.Info("[TcpInput] Listening on port %d", l.Port)

	// dispatch the master listen thread
	go func(server net.Listener) {
		for {
			// accept incoming connections
			conn, err := server.Accept()
			if err != nil {
				logp.Err("Error accepting: ", err.Error())
			} else {
				// dispatch individual connection handler threads
				go l.handleConn(conn, output)
			}
		}
	}(server)
	return nil
}
Esempio n. 6
0
func (reader *ReaderType) Init(inputs map[string]inputs.MothershipConfig) error {
	logp.Info("reader input config", inputs)

	for inputId, config := range inputs {
		// default instance 0
		inputName, instance := inputId, "0"
		if strings.Contains(inputId, "_") {
			// otherwise grok tcp_2 as inputName = tcp, instance = 2
			sv := strings.Split(inputId, "_")
			inputName, instance = sv[0], sv[1]
		}
		logp.Info(fmt.Sprintf("input type: %s instance: %s\n", inputName, instance))
		logp.Debug("reader", "instance config: %s", config)

		plugin := newInputInstance(inputName)
		if plugin != nil && config.Enabled {
			err := plugin.Init(config)
			if err != nil {
				logp.Err("Fail to initialize %s plugin as input: %s", inputName, err)
				return err
			} else {
				logp.Info("Initialized %s plugin as input", inputName)
			}
			reader.Input = append(reader.Input, plugin)
		}
	}

	if len(reader.Input) == 0 {
		logp.Info("No inputs are defined. Please define one under the input section.")
		return errors.New("No input are defined. Please define one under the input section.")
	} else {
		logp.Info("%d inputs defined", len(reader.Input))
	}

	return nil
}
Esempio n. 7
0
func (l *TcpInput) handleConn(client net.Conn, output chan common.MapStr) {
	reader := bufio.NewReader(client)
	buffer := new(bytes.Buffer)

	var source string = client.RemoteAddr().String()
	var offset int64 = 0
	var line uint64 = 0
	var read_timeout = 10 * time.Second

	logp.Debug("tcpinput", "Handling New Connection from %s", source)

	now := func() time.Time {
		t := time.Now()
		return t
	}

	for {
		text, bytesread, err := l.readline(reader, buffer, read_timeout)

		if err != nil {
			logp.Info("Unexpected state reading from %v; error: %s\n", client.RemoteAddr().String, err)
			return
		}

		logp.Debug("tcpinputlines", "New Line: %s", &text)

		line++

		event := common.MapStr{}
		event["source"] = &source
		event["offset"] = offset
		event["line"] = line
		event["message"] = text
		event["type"] = l.Type

		event.EnsureTimestampField(now)
		event.EnsureCountField()

		offset += int64(bytesread)

		logp.Debug("tcpinput", "InputEvent: %v", event)
		output <- event // ship the new event downstream
		client.Write([]byte("OK"))
	}
	logp.Debug("tcpinput", "Closed Connection from %s", source)
}
Esempio n. 8
0
func (l *NullInput) Init(config inputs.MothershipConfig) error {
	l.Type = "null"
	logp.Info("[NullInput] Initialized")
	return nil
}
Esempio n. 9
0
func main() {
	// Use our own FlagSet, because some libraries pollute the global one
	var cmdLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
	configfile := cmdLine.String("c", "./"+Name+".yml", "Configuration file")
	printVersion := cmdLine.Bool("version", false, "Print version and exit")

	// Adds logging specific flags
	logp.CmdLineFlags(cmdLine)
	publisher.CmdLineFlags(cmdLine)

	cmdLine.Parse(os.Args[1:])
	if *printVersion {
		fmt.Printf("Turnbeat version %s (%s)\n", Version, runtime.GOARCH)
		return
	}

	// configuration file
	filecontent, err := ioutil.ReadFile(*configfile)
	if err != nil {
		fmt.Printf("Fail to read %s: %s. Exiting.\n", *configfile, err)
		os.Exit(1)
	}
	if err = yaml.Unmarshal(filecontent, &config.ConfigSingleton); err != nil {
		fmt.Printf("YAML config parsing failed on %s: %s. Exiting.\n", *configfile, err)
		os.Exit(1)
	}

	logp.Init(Name, &config.ConfigSingleton.Logging)

	logp.Info("Initializing output plugins")
	if err = publisher.Publisher.Init(config.ConfigSingleton.Output, config.ConfigSingleton.Shipper); err != nil {
		logp.Critical(err.Error())
		os.Exit(1)
	}

	stopCb := func() {
	}

	logp.Info("Initializing filter plugins")
	afterInputsQueue, err := filters.FiltersRun(
		config.ConfigSingleton.Filter,
		EnabledFilterPlugins,
		publisher.Publisher.Queue,
		stopCb)
	if err != nil {
		logp.Critical("%v", err)
		os.Exit(1)
	}

	logp.Info("Initializing input plugins")
	if err = reader.Reader.Init(config.ConfigSingleton.Input); err != nil {
		logp.Critical("Critical Error: %v", err)
		os.Exit(1)
	}

	if err = droppriv.DropPrivileges(config.ConfigSingleton.RunOptions); err != nil {
		logp.Critical("Critical Error: %v", err)
		os.Exit(1)
	}

	logp.Info("Starting input plugins")
	go reader.Reader.Run(afterInputsQueue)

	logp.Info("Turnbeat Started")
	signalChan := make(chan os.Signal, 1)
	cleanupDone := make(chan bool)
	signal.Notify(signalChan, os.Interrupt)
	go func() {
		for _ = range signalChan {
			fmt.Println("\nReceived an interrupt, stopping services...\n")
			//cleanup(services, c)
			cleanupDone <- true
		}
	}()
	<-cleanupDone

	//  for {
	//    event := <-afterInputsQueue
	//    reader.Reader.PrintReaderEvent(event)
	//    logp.Debug("events", "Event: %v", event)
	//  }
}