0

filebeat 是否使用 tail -f 检查文件中的新内容,然后将其刷新到所需的输出?或者有没有其他方法检查文件中的新内容?

4

1 回答 1

1

由于filebeat是开源的,你可以自己去看看

这是上述链接文件中的 go 代码,用于检查文件是否已更新。

我已经大量删减了这段代码,你看到的任何地方...都是一个不完全相关的代码块,我鼓励任何阅读这篇文章的人也去看看整个文件,它的一些写得很好。

 // Scan starts a scanGlob for each provided path/glob
func (p *ProspectorLog) scan() {

    newlastscan := time.Now()

    // Now let's do one quick scan to pick up new files
    for _, path := range p.config.Paths {
        p.scanGlob(path)
    }
    p.lastscan = newlastscan
}

上面的函数在配置中指定的每个n长度时间块都会被调用。nScanGlob 被调用,如下所示。

// Scans the specific path which can be a glob (/**/**/*.log)
// For all found files it is checked if a harvester should be started
func (p *ProspectorLog) scanGlob(glob string) {

    ...

    // Evaluate the path as a wildcards/shell glob
    matches, err := filepath.Glob(glob)
    ...

    // Check any matched files to see if we need to start a harvester
    for _, file := range matches {
        ...

对于与 glob 匹配的所有文件,使用操作系统特定调用检查文件的统计信息,对于 linux,这将是stat <file>

        // Stat the file, following any symlinks.
        fileinfo, err := os.Stat(file)
        ...

根据 stat 调用,决定是否需要启动一个收割机,这个 go 应用程序中读取文件的部分。

        // Conditions for starting a new harvester:
        // - file path hasn't been seen before
        // - the file's inode or device changed
        if !isKnown {
            p.checkNewFile(h)
        } else {
            h.Stat.Continue(&lastinfo)
            p.checkExistingFile(h, &newFile, &oldFile)
        }

        // Track the stat data for this file for later comparison to check for
        // rotation/etc
        p.prospectorList[h.Path] = *h.Stat
    }
}

TL;DR:Filebeat 使用操作系统报告的文件统计信息来查看自上次获取文件以来文件是否已更新。

于 2016-04-18T21:46:24.457 回答