Log Agent - Fluent Bit Input元件 与 Tail浅谈

Fluent bit回顾
Log Agent - Fluent Bit 简介
Log Agent - Fluent Bit 安装与常见架构模式
Log Agent - Fluent Bit Service配置与内建 API

Input元件

Fluent bit提供了各式各样的plugin来收集不同来源的Log file.
比如说从Log file来收集、从OS收集一些metric.
我们可以设定多个input设定,
每个设定其实都会在fluent-bit内创见一个instance, 也都有自己的配置设定.
https://docs.fluentbit.io/manual/pipeline/inputs

设定格式如下:

[INPUT]
    Name input元件类型名称
    Tag 後面会介绍
    对应input元件property 对应的value

挑几个玩一下

  • CPU Metrics
  • Disk I/O
  • Memory
  • Network I/O
    这几个input设定方式都一样.
    就是设定Interval_Sec 多久取得这些metric一次.
    预设都是1秒.
[SERVICE]
    flush 1
    Daemon off
    # 这里先改成debug, 目的试看input启动了哪些
    log_level debug
    health_check on
    hc_errors_count 5
    hc_retry_failure_count 5
    hc_period 5
    #开启http server
    http_server on
    #监听所有网卡
    http_listen 0.0.0.0
    #监听导到port 2020的请求
    http_port 2020
    storage.metrics on

[INPUT]    
    Name cpu   
    Interval_Sec 5
    Interval_NSec 0
    Tag  my_cpu

[INPUT]
    Name          netif
    Interval_Sec  5
    Interval_NSec 0
    Interface     eth0
    Alias net_eth0
    Verbose true
    Tag my_net_eth0

[INPUT]
    Name          disk
    Interval_Sec  5
    Interval_NSec 0
    Tag my_disk

[INPUT]
    Name   mem
    Interval_Sec  5
    Tag    my_memory

[OUTPUT]
    Name stdout
    match *
    Format json

一开始启动就会看到如下的画面,
Inputs这里刚好就是我们启用的4个input

fluentd_1  | [2021/10/11 07:02:21] [ info] Configuration:
fluentd_1  | [2021/10/11 07:02:21] [ info]  flush time     | 1.000000 seconds
fluentd_1  | [2021/10/11 07:02:21] [ info]  grace          | 5 seconds
fluentd_1  | [2021/10/11 07:02:21] [ info]  daemon         | 0
fluentd_1  | [2021/10/11 07:02:21] [ info] ___________
fluentd_1  | [2021/10/11 07:02:21] [ info]  inputs:
fluentd_1  | [2021/10/11 07:02:21] [ info]      cpu
fluentd_1  | [2021/10/11 07:02:21] [ info]      netif
fluentd_1  | [2021/10/11 07:02:21] [ info]      disk
fluentd_1  | [2021/10/11 07:02:21] [ info]      mem
fluentd_1  | [2021/10/11 07:02:21] [ info] ___________
fluentd_1  | [2021/10/11 07:02:21] [ info]  filters:
fluentd_1  | [2021/10/11 07:02:21] [ info] ___________
fluentd_1  | [2021/10/11 07:02:21] [ info]  outputs:
fluentd_1  | [2021/10/11 07:02:21] [ info]      stdout.0
fluentd_1  | [2021/10/11 07:02:21] [ info] ___________

可以看到内容有出现input元件所采集到的内容
这里出现cpu0-cpu11, 是因为我主机刚好就是12Thread
cpu_p 则是目前整个系统所使用的cpu使用率
user_p则是目前User mode底下所使用的cpu使用率
system_p则是Kernel mode底下所使用的cpu使用率

[{"date":1633935975.657292,"cpu_p":4.633333333333334,"user_p":4.3,"system_p":0.3333333333333334,"cpu0.p_cpu":1.4,"cpu0.p_user":1.2,"cpu0.p_system":0.2,"cpu1.p_cpu":1.4,"cpu1.p_user":1.0,"cpu1.p_system":0.4,"cpu2.p_cpu":1.4,"cpu2.p_user":0.8,"cpu2.p_system":0.6,"cpu3.p_cpu":5.0,"cpu3.p_user":4.6,"cpu3.p_system":0.4,"cpu4.p_cpu":7.2,"cpu4.p_user":7.0,"cpu4.p_system":0.2,"cpu5.p_cpu":5.4,"cpu5.p_user":5.0,"cpu5.p_system":0.4,"cpu6.p_cpu":1.4,"cpu6.p_user":1.0,"cpu6.p_system":0.4,"cpu7.p_cpu":1.6,"cpu7.p_user":0.8,"cpu7.p_system":0.8,"cpu8.p_cpu":1.0,"cpu8.p_user":1.0,"cpu8.p_system":0.0,"cpu9.p_cpu":1.0,"cpu9.p_user":0.8,"cpu9.p_system":0.2,"cpu10.p_cpu":13.0,"cpu10.p_user":12.8,"cpu10.p_system":0.2,"cpu11.p_cpu":16.4,"cpu11.p_user":16.2,"cpu11.p_system":0.2}]

接着可以看到net interface相关的资讯

[{"date":1633935975.657417,"eth0.rx.bytes":0,"eth0.rx.packets":0,"eth0.rx.errors":0,"eth0.rx.drop":0,"eth0.rx.fifo":0,"eth0.rx.frame":0,"eth0.rx.compressed":0,"eth0.rx.multicast":0,"eth0.tx.bytes":0,"eth0.tx.packets":0,"eth0.tx.errors":0,"eth0.tx.drop":0,"eth0.tx.fifo":0,"eth0.tx.collisions":0,"eth0.tx.carrier":0,"eth0.tx.compressepd":0}]

disk目前的吞吐量throughput

[{"date":1633935975.657755,"read_size":0,"write_size":6356992}]

memory跟swap空间的使用情况

[{"date":1633935975.657776,"Mem.total":32813924,"Mem.used":16895336,"Mem.free":15918588,"Swap.total":2097148,"Swap.used":0,"Swap.free":2097148}]

然後回头看一下设定档, 这里我们有给Alias,
像net interface这里, 我们可以设定多个, 因为网卡可以有多张,
如果不给个alias, 则在某些情况则难以判别
回头用上篇讲的fluent REST API, 就能发现只有net_eth0是用alias做物件名称
其他都是按照input 写的顺序从0开始, 加上input name, (cpu_0, disk_2这样)

[INPUT]
    Name          netif
    Interval_Sec  5
    Interval_NSec 0
    Interface     eth0
    Alias net_eth0
    Verbose true
    Tag my_net_eth0
curl -s http://127.0.0.1:2020/api/v1/metrics/ | jq          
{
  "input": {
    "cpu.0": {
      "records": 14,
      "bytes": 11718
    },
    "net_eth0": {
      "records": 13,
      "bytes": 3624
    },
    "disk.2": {
      "records": 13,
      "bytes": 511
    },
    "mem.3": {
      "records": 14,
      "bytes": 1358
    }
  },
  "filter": {},
  "output": {
    "stdout.0": {
      "proc_records": 50,
      "proc_bytes": 15958,
      "errors": 0,
      "retries": 0,
      "retries_failed": 0,
      "dropped_records": 0,
      "retried_records": 0
    }
  }
}

Tail插件

通常Application不管有没有以container方式运行, 大家应该都还是会写Log到特定文件内.
有些是把Log写到MessageQueue里面, 让Broker慢慢的写入给Logstah、Fluentbit这类的.
但多一个服务就要多管理跟运维成本.
写到本机的档案还是简单些, 只要做好rotate就好.
tail元件就是专门爬这些Log file用的

Parameters

  • Path

    用来指定要采集的log file的位子
    如果有多个可以用,来串接多个位子
    对於档名或路径, 支持*wildcard

  • Refresh_Interval

    设定秒数, 来监听档案
    预设是60秒

  • Rotate_Wait

    日志文件被rotated後, 等待一段时间後, 再来继续监控该文件
    当原有日志数据被rotate到另一个档名时, 很可能会被重新读取
    预设5秒
    能参考File Rotation

  • Mem_Buf_Limit

    memory buffer size, 用来指定之前data pipeline中的Buffer区块的大小
    如果buffer内还没处理完成的资料size超过这限制, 则tail会暂停采集, 直到Buffer区块内的资料被flush到Output区块
    能避免记忆体胡乱增长
    预设是没指定, 但建议还是指定

  • Path_Key

    这里指定的是label的名称, 描述log内关於这log资料来自哪个档案
    如果这里写file_key, 输出时这样显示"file_key"=>"/var/log/syslog"
    预设是不会输出

  • Read_from_Head

    一个log file要不要从头开始读取
    预设是false

  • DB

    纪录被监控的文件跟其offset偏移量的档案
    Fluent bit使用SQLite作为数据库引擎来纪录

  • DB.Journal_Mode

    跟一些资料库一样, 就是log要写入db时有什麽处理方式
    有DELETE | TRUNCATE | PERSIST | MEMORY | WAL | OFF
    预设是WAL (Write-Ahead-Log)
    能参考WAL and Memory Usage

  • Parser

    下篇聊
    主要就剖析日志资料

  • Multiline.parser

    跟Parser同功能, 但针对的是Multiline log资料

来尝试玩一下Tail
我直接来抓取syslog的资料

[SERVICE]
    flush 1
    Daemon off
    log_level info
    health_check on

[INPUT]
    name tail
    tag demo
    path /var/log/syslog #指定采集日志的位子, 这里指定syslog
    path_key file_key 
    db  /home/nathan/demo.db #指定db位子, 我是放自己的目录下就是了

[OUTPUT]
    Name stdout
    match *

大概会看到像这样的资料
其中"file_key"=>"/var/log/syslog",
file_key就是path_key file_key这里的设置

tag是demo, 也看到了

Oct 11 23:45:32 nathan td-agent-bit[907186]: [157] demo: [1633967127.693605530, {"file_key"=>"/var/log/syslog", "log"=>"Oct 11 23:45:27 nathan td-agent-bit[907186]: #033[1mFluent Bit v1.8.7#033[0m"}]
Oct 11 23:45:32 nathan td-agent-bit[907186]: [158] demo: [1633967127.693605831, {"file_key"=>"/var/log/syslog", "log"=>"Oct 11 23:45:27 nathan td-agent-bit[907186]: * #033[1m#033[93mCopyright (C) 2019-2021 The Fluent Bit Authors#033[0m"}]

来看db资料, 在指定的路径下会出现3个档案
因为是使用SQLite, 加上我又开启WAL模式,
所以出现例外两个档案, 别管它, 让SQLite自己管理

-rw-r--r--  1 root   root      8192  十  11 23:48 demo.db
-rw-r--r--  1 root   root     32768  十  11 23:48 demo.db-shm
-rw-r--r--  1 root   root   4120032  十  11 23:48 demo.db-wal

来看看demo.db存的资料, 使用sqlite3来操作db文件

sqlite3 demo.db
sqlite> .headers on
sqlite> .mode column

# 查看有哪些table
sqlite> .tables
in_tail_files

# 只有一张in_tail_files
sqlite>  select * from in_tail_files;
id          name             offset      inode       created     rotated   
----------  ---------------  ----------  ----------  ----------  ----------
1           /var/log/syslog  7400014404  558         1633960199  0   

可以看到这里面纪录的栏位就这样, 但其实也没必要特别维护这文件, 让fluent bit来维护就好

本日小结

Fluent bit真的提供很多input plugin, 甚至能跟Prometheus所提供的node exporter结合,
不知道能不能跟其他exporter相互应用就是了, 但我想应该是可以. 日後再来尝试.

官网跟ELK的说明相比, 蛮多什麽描述跟范例真的有差距, 也许要等社团的力量慢慢补齐吧.
但基本功能, 官网都有教学. 也堪用了


<<:  Day28:28 - 後端&前端 - 按赞收藏

>>:  Day27 小乌龟动工的基本指令集

建构 Spring boot 容器 Image

要将自己开发的应用程序容器化,想必需要制作自己的 Image。制作 Image 也是一种艺术,我们要...

Angular Stock Route Guards (Day31)

虽然铁人赛比完了,但是我依然会把这个专案继续写下去,如果还没看之前Angular Stock的朋友,...

Python 语言和你 SAY HELLO!!

第二十三天 各位点进来的朋友,你们好阿 小的不才只能做这个系列的文章,但还是希望分享给点进来的朋友,...

Day 22 - Shortest Distance to a Character

大家好,我是毛毛。ヾ(´∀ ˋ)ノ 废话不多说开始今天的解题Day~ 821. Shortest D...

.Net Core Web Api_笔记09_web api的属性路由模板两种写法_路由模板使用

在.net core mvc跟.net core web api专案中预设各自采用的一些配置 有不太...