1. 程式人生 > >配置文件格式(三)

配置文件格式(三)

match index 完整路徑 建議 時間 cse with disabled 拼接

一、YAML格式簡介
Beats的配置文件基於YAML,這種文件格式比XML和JSON等常用的數據格式更容易讀寫。在Beats中所有的YAML文件都以字典開始,一個無序的鍵值對。除了字典,YAML還支持列表、數值、字符串和其他的數據類型。相同列表或字典的所有成員必須有相同的縮進級別。
1、字典由具有相同縮進級別的key:value組成,冒號後面必須有一個空格3 1
name: John Doe
2
age: 34
3
country: Canada

2、列表由“-”表示,所有的列表成員在相同縮進級別由“-”開始
3 1
- Red
2
- Green
3
- Blue

3、beats中使用列表和字典來構建結構化配置8 1
filebeat:
2
  inputs:
3
    - type: log
4
      paths:
5
        - /var/log/*.log
6
      multiline:
7
        pattern: ‘^[‘
8
        match: after

4、列表和字典也可以用縮寫的方式表示,縮寫方式有點類似於JSON格式,字典使用{},列表使用[]2 1
person: {name: "John Doe", age: 34, country: "Canada"}
2
colors: ["Red", "Green", "Blue"]

二、Namespacing
1、所有的設置都使用字典和列表。讀取配置文件時,通過使用設置名稱及其父結構的名稱的完整路徑,這些設置將被壓縮到名稱設置中。
如:3 1
output:
2
  elasticsearch:
3
    index: ‘beat-%{[beat.version]}-%{+yyyy.MM.dd}‘
將被壓縮成: output.elasticsearch.index: ‘beat-%{[beat.version]}-%{+yyyy.MM.dd}‘ 全名涉及到所有的父級結構
2、列表創建的時候以0開始
x
1
filebeat:
2
  inputs:
3
    - type: log
將被壓縮為:filebeat.inputs.0.type: log

不僅可以以縮進的方式,也可以以折疊的方式
三、配置文件的數據類型
配置文件中的設置的值將被轉換為beats所需的值,如果設置的值的類型不是配置文件所要求的值類型,那麽啟動將會報錯。
1、Boolean1 1
enabled: true
2
disabled: false

2、Number1 1
integer: 123
2
negative: -1
3
float: 5.4

3、String在YAML中,字符串支持多種格式,雙引號(“ ”)、單引號(’ ‘)、無引號()
4、Duration 時間必須帶有單位,如ns, us, ms, s, m, h1 1
duration1: 2.5s
2
duration2: 6h
3
duration_disabled: -1s

5、正則表達式因為在YAML和正則表達式中使用"\"進行字符串的轉義,因此建議在使用正則表達式的時候使用單引號字符串,當使用單引號字符串的時候\不會被轉義
6、格式串(打印的格式)1 1
constant-format-string: ‘constant string‘
2
field-format-string: ‘%{[fieldname]} string‘
3
format-string-with-date: ‘%{[fieldname]}-%{+yyyy.MM.dd}‘

四、環境變量1、可以在配置文件中引用環境變量,設置為在運行期間需要用到的值;如1 1
${VAR}
var是環境變量的名稱
2、當啟動時,變量的值將會被環境變量的值替換。除非指定默認值或自定義錯誤文本,否則會被當做空字符串處理指定默認值:1 1
${VAR:default_value}

指定自定義錯誤文本1 1
${VAR:?error_text}

例子:技術分享圖片

4、在環境變量中指定復雜的變量可以使用類似JSON的語法使用復雜的變量,如字典、列表;如:1 1
ES_HOSTS="10.45.3.2:9220,10.45.3.1:9230"
1 1
output.elasticsearch:
2
  hosts: ‘${ES_HOSTS}‘

五、引用變量
1、Beats中設置可以引用其他設置,將多個可選的自定義值拼接到新值中,如Filebeat默認的註冊表文件為:1 1
filebeat.registry: ${path.data}/registry
path.data是隱式設置的,可以在命令行重寫,也可以在配置文件重寫
2、如es.host1 1
es.host: ‘${ES_HOST:localhost}‘
2
3
output.elasticsearch:
4
  hosts: [‘http://${es.host}:9200‘]

3、普通的引用,沒有默認的值,沒有拼接,通過其他字符串來引用完整的命名空間,如:
1 1
namespace1:
2
  subnamespace:
3
    host: localhost
4
    sleep: 1s
5
6
namespace2:
7
  subnamespace:
8
    host: localhost
9
    sleep: 1s
可以重寫為:1 1
namespace1: ${shared}
2
namespace2: ${shared}
3
4
shared:
5
  subnamespace:
6
    host: localhost
7
    sleep: 1s


六、命令行參數
使用-c指定配置文件,如果不指定則使用默認的配置文件;可以通過在啟動時使用-E指定配置文件
技術分享圖片
如:配置文件裏的配置如下:1 1
output.elasticsearch:
2
  hosts: ["http://localhost:9200"]
3
  username: username
4
  password: password

如果在啟動的時候,加上1 1
-E output=‘{elasticsearch.enabled: false, console.pretty: true}‘

那麽配置文件相當於: 1
output.elasticsearch:
2
  enabled: false
3
  hosts: ["http://localhost:9200"]
4
  username: username
5
  password: password
6
7
output.console:
8
  pretty: true

官方文檔:https://www.elastic.co/guide/en/beats/libbeat/6.6/config-file-format.html

配置文件格式(三)