【问题标题】:How filebeat checks for new content in a file?filebeat 如何检查文件中的新内容?
【发布时间】:2016-04-17 22:01:15
【问题描述】:

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

【问题讨论】:

    标签: elasticsearch go logstash elastic-stack filebeat


    【解决方案1】:

    由于filebeat是开源的,你可以随时go look yourself

    这是来自上述链接文件的 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 的每个n-length 时间块都会调用上述函数。 ScanGlob 被调用,如下所示。

    // 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 使用操作系统报告的文件统计信息来查看自上次获取文件以来文件是否已更新。

    【讨论】:

      猜你喜欢
      • 2017-04-08
      • 1970-01-01
      • 2023-03-31
      • 2013-02-14
      • 2018-08-01
      • 2019-11-22
      • 2016-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多