Ejemplo n.º 1
0
func parseConfig(name string, prim toml.Primitive) {
	config := LogstreamerConfig{
		OldestDuration: "720h",
		Differentiator: []string{name},
		LogDirectory:   "/var/log",
	}
	if err := toml.PrimitiveDecode(prim, &config); err != nil {
		client.LogError.Printf("Error decoding config file: %s", err)
		return
	}

	if len(config.FileMatch) > 0 && config.FileMatch[len(config.FileMatch)-1:] != "$" {
		config.FileMatch += "$"
	}

	sp := &logstreamer.SortPattern{
		FileMatch:      config.FileMatch,
		Translation:    config.Translation,
		Priority:       config.Priority,
		Differentiator: config.Differentiator,
	}
	oldest, _ := time.ParseDuration(config.OldestDuration)
	ls, err := logstreamer.NewLogstreamSet(sp, oldest, config.LogDirectory, "")
	if err != nil {
		client.LogError.Fatalf("Error initializing LogstreamSet: %s\n", err.Error())
	}
	streams, errs := ls.ScanForLogstreams()
	if errs.IsError() {
		client.LogError.Fatalf("Error scanning: %s\n", errs)
	}

	fmt.Printf("Found %d Logstream(s) for section [%s].\n", len(streams), name)
	for _, name := range streams {
		stream, _ := ls.GetLogstream(name)
		fmt.Printf("\nLogstream name: [%s]\n", name)
		fmt.Printf("Files: %d (printing oldest to newest)\n", len(stream.GetLogfiles()))
		for _, logfile := range stream.GetLogfiles() {
			fmt.Printf("\t%s\n", logfile.FileName)
		}
	}
}
Ejemplo n.º 2
0
func (li *LogstreamerInput) Init(config interface{}) (err error) {
	var (
		errs    *ls.MultipleError
		oldest  time.Duration
		plugins []string
	)
	conf := config.(*LogstreamerInputConfig)

	// Setup the journal dir
	if err = os.MkdirAll(conf.JournalDirectory, 0744); err != nil {
		return err
	}

	if len(conf.FileMatch) > 0 && conf.FileMatch[len(conf.FileMatch)-1:] != "$" {
		conf.FileMatch += "$"
	}

	li.decoderName = conf.Decoder
	li.parser = conf.ParserType
	li.delimiter = conf.Delimiter
	li.delimiterLocation = conf.DelimiterLocation
	li.plugins = make(map[string]*LogstreamInput)

	// Setup the rescan interval
	if li.rescanInterval, err = time.ParseDuration(conf.RescanInterval); err != nil {
		return
	}

	// Parse the oldest duration
	if oldest, err = time.ParseDuration(conf.OldestDuration); err != nil {
		return
	}

	// If no differentiator is present than we use the plugin
	if len(conf.Differentiator) == 0 {
		conf.Differentiator = []string{li.pluginName}
	}

	for name, submap := range conf.Translation {
		if len(submap) == 1 {
			if _, ok := submap["missing"]; !ok {
				err = fmt.Errorf("A translation map with one entry ('%s') must be "+
					"specifying a 'missing' key.", name)
				return
			}
		}
	}

	// Create the main sort pattern
	sp := &ls.SortPattern{
		FileMatch:      conf.FileMatch,
		Translation:    conf.Translation,
		Priority:       conf.Priority,
		Differentiator: conf.Differentiator,
	}

	// Create the main logstream set
	li.logstreamSetLock.Lock()
	defer li.logstreamSetLock.Unlock()
	li.logstreamSet, err = ls.NewLogstreamSet(sp, oldest, conf.LogDirectory,
		conf.JournalDirectory)
	if err != nil {
		return
	}

	// Initial scan for logstreams
	plugins, errs = li.logstreamSet.ScanForLogstreams()
	if errs.IsError() {
		return errs
	}

	// Verify we can make a parser
	if _, _, err = CreateParser(li.parser, li.delimiter,
		li.delimiterLocation, li.decoderName); err != nil {
		return
	}

	// Declare our hostname
	if conf.Hostname == "" {
		li.hostName, err = os.Hostname()
		if err != nil {
			return
		}
	} else {
		li.hostName = conf.Hostname
	}

	// Create all our initial logstream plugins for the logstreams found
	for _, name := range plugins {
		stream, ok := li.logstreamSet.GetLogstream(name)
		if !ok {
			continue
		}
		stParser, parserFunc, _ := CreateParser(li.parser, li.delimiter,
			li.delimiterLocation, li.decoderName)
		li.plugins[name] = NewLogstreamInput(stream, stParser, parserFunc,
			name, li.hostName)
	}
	li.stopLogstreamChans = make([]chan chan bool, 0, len(plugins))
	li.stopChan = make(chan bool)
	return
}