Head 입력 (Head)
파일의 앞부분(head)에서 이벤트를 읽어들이고 싶다면 Head 입력 플러그인을 써요. 리눅스의 head 명령과 비슷하게 동작해요.
출처: 문서
본문
{% hint style="info" %}
지원 이벤트 유형: logs
{% endhint %}
Head 입력 플러그인은 파일의 앞부분에서 이벤트를 읽어요. 동작은 head 명령과 비슷해요.
구성 파라미터
플러그인은 다음 구성 파라미터를 지원해요:
| Key | Description | Default |
|---|---|---|
add_path |
활성화하면 각 레코드에 경로가 추가돼요. | false |
buf_size |
파일을 읽는 버퍼 크기. | 256 |
file |
대상 파일의 절대 경로. 예: /proc/uptime. |
none |
interval_nsec |
폴링 간격(나노초). | 0 |
interval_sec |
폴링 간격(초). | 1 |
key |
키 이름 변경. | head |
lines |
읽을 줄 번호. 숫자 N이 설정되면 in_head는 head(1) -n처럼 첫 N줄을 읽어요. |
0 |
split_line |
활성화하면 in_head가 줄마다 키-값 쌍을 생성해요. |
false |
threaded |
이 입력을 자체 스레드에서 실행할지 여부. | false |
시작하기
파일의 앞부분을 읽으려면 커맨드 라인으로 또는 구성 파일을 통해 플러그인을 실행할 수 있어요.
커맨드 라인
다음 예시는 /proc/uptime 파일에서 이벤트를 읽고, 레코드에 uptime이라는 태그를 붙여 stdout 플러그인으로 플러시해요:
fluent-bit -i head -t uptime -p File=/proc/uptime -o stdout -m '*'
출력은 다음과 비슷해요:
Fluent Bit v1.x.x
* Copyright (C) 2019-2020 The Fluent Bit Authors
* Copyright (C) 2015-2018 Treasure Data
* Fluent Bit is a CNCF sub-project under the umbrella of Fluentd
* https://fluentbit.io
[2016/05/17 21:53:54] [ info] starting engine
[0] uptime: [1463543634, {"head"=>"133517.70 194870.97"}]
[1] uptime: [1463543635, {"head"=>"133518.70 194872.85"}]
[2] uptime: [1463543636, {"head"=>"133519.70 194876.63"}]
[3] uptime: [1463543637, {"head"=>"133520.70 194879.72"}]
구성 파일
메인 구성 파일에 다음을 추가해요:
{% tabs %} {% tab title="fluent-bit.yaml" %}
pipeline:
inputs:
- name: head
tag: uptime
file: /proc/uptime
buf_size: 256
interval_sec: 1
interval_nsec: 0
outputs:
- name: stdout
match: '*'
{% endtab %}
{% tab title="fluent-bit.conf" %}
[INPUT]
Name head
Tag uptime
File /proc/uptime
Buf_Size 256
Interval_Sec 1
Interval_Nsec 0
[OUTPUT]
Name stdout
Match *
{% endtab %} {% endtabs %}
간격은 이렇게 계산돼요:
Total interval (sec) = interval_sec + (interval_nsec / 1000000000).
예를 들어: 1.5s = 1s + 500000000ns.
줄 분리 모드 (Split line mode)
특정 줄을 얻고 싶을 때 이 모드를 사용해요. 다음 예시는 /proc/cpuinfo에서 CPU 주파수를 가져와요.
/proc/cpuinfo는 CPU 정보를 얻기 위한 특수 파일이에요.
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 42
model name : Intel(R) Core(TM) i7-2640M CPU @ 2.80GHz
stepping : 7
microcode : 41
cpu MHz : 2791.009
cache size : 4096 KB
physical id : 0
siblings : 1
CPU 주파수는 cpu MHz : 2791.009예요. 다음 구성 파일은 필요한 줄을 가져와요:
{% tabs %} {% tab title="fluent-bit.yaml" %}
pipeline:
inputs:
- name: head
tag: head.cpu
file: /proc/cpuinfo
lines: 8
split_line: true
filters:
- name: record_modifier
match: '*'
whitelist_key: line7
outputs:
- name: stdout
match: '*'
{% endtab %}
{% tab title="fluent-bit.conf" %}
[INPUT]
Name head
Tag head.cpu
File /proc/cpuinfo
Lines 8
Split_Line true
# {"line0":"processor : 0", "line1":"vendor_id : GenuineIntel" ...}
[FILTER]
Name record_modifier
Match *
Whitelist_key line7
[OUTPUT]
Name stdout
Match *
{% endtab %} {% endtabs %}
다음 명령을 실행하면:
fluent-bit -c head.conf
출력은 다음과 비슷해요:
Fluent Bit v1.x.x
* Copyright (C) 2019-2020 The Fluent Bit Authors
* Copyright (C) 2015-2018 Treasure Data
* Fluent Bit is a CNCF sub-project under the umbrella of Fluentd
* https://fluentbit.io
[2017/06/26 22:38:24] [ info] [engine] started
[0] head.cpu: [1498484305.000279805, {"line7"=>"cpu MHz : 2791.009"}]
[1] head.cpu: [1498484306.011680137, {"line7"=>"cpu MHz : 2791.009"}]
[2] head.cpu: [1498484307.010042482, {"line7"=>"cpu MHz : 2791.009"}]
[3] head.cpu: [1498484308.008447978, {"line7"=>"cpu MHz : 2791.009"}]