## 一、前言
Logstash的配置文件支持各種條件判斷語法,條件查看和行為與編程語言中的條件相同。詳情可看[官方文檔](https://www.elastic.co/guide/en/logstash/7.x/event-dependent-configuration.html#conditionals)
## 二、條件語法
條件支持if,else if和else語句并且可以嵌套,語法如下:
~~~
if EXPRESSION {
...
} else if EXPRESSION {
...
} else {
...
}
~~~
### 2.1. 比較操作
* 相等:`==`,`!=`,`<`,`>`,`<=`,`>=`
* 正則:`=~`(匹配正則),`!~`(不匹配正則)
* 包含:`in`(包含),`not in`(不包含)
### 2.2. 布爾操作
* `and`(與),`or`(或),`nand`(非與),`xor`(非或)
### 2.3. 一元運算符
表達式可能很長且很復雜。表達式可以包含其他表達式,您可以使用!來取反表達式,并且可以使用括號(...)對它們進行分組。
* `!`(取反)
* `()`(復合表達式),`!()`(對復合表達式結果取反)
## 三、案例
### 3.1. 如若`action`的值是`login`,則通過`mutate`filter刪除`secret`字段:
~~~
filter {
if [action] == "login" {
mutate { remove_field => "secret" }
}
}
~~~
### 3.2. 若是需要通過正則匹配(即,當if的表達式為正則時),匹配成功后添加自定義字段:
~~~
filter {
if [message] =~ /\w+\s+\/\w+(\/learner\/course\/)/ {
mutate {
add_field => { "learner_type" => "course" }
}
}
}
~~~
### 3.3. 在一個條件里指定多個表達式:
~~~
output {
# Send production errors to pagerduty
if [loglevel] == "ERROR" and [deployment] == "production" {
pagerduty {
...
}
}
}
~~~
### 3.4. 在in條件,可以比較字段值:
~~~
filter {
if [foo] in [foobar] {
mutate { add_tag => "field in field" }
}
if [foo] in "foo" {
mutate { add_tag => "field in string" }
}
if "hello" in [greeting] {
mutate { add_tag => "string in field" }
}
if [foo] in ["hello", "world", "foo"] {
mutate { add_tag => "field in list" }
}
if [missing] in [alsomissing] {
mutate { add_tag => "shouldnotexist" }
}
if !("foo" in ["hello", "world"]) {
mutate { add_tag => "shouldexist" }
}
}
~~~
### 3.5. not in示例:
~~~
output {
if "_grokparsefailure" not in [tags] {
elasticsearch { ... }
}
}
~~~
### 3.6. @metadata field
在logstash1.5版本開始,有一個特殊的字段,叫做@metadata。@metadata包含的內容不會作為事件的一部分輸出。
~~~
input { stdin { } }
filter {
mutate { add_field => { "show" => "This data will be in the output" } }
mutate { add_field => { "[@metadata][test]" => "Hello" } }
mutate { add_field => { "[@metadata][no_show]" => "This data will not be in the output" } }
}
output {
if [@metadata][test] == "Hello" {
stdout { codec => rubydebug }
}
}
~~~
輸出結果
~~~
$ bin/logstash -f ../test.conf
Pipeline main started
asdf
{
"@timestamp" => 2016-06-30T02:42:51.496Z,
"@version" => "1",
"host" => "windcoder.com",
"show" => "This data will be in the output",
"message" => "asdf"
}
~~~
"asdf"變成message字段內容。條件與@metadata內嵌的test字段內容判斷成功,但是輸出并沒有展示@metadata字段和其內容。
不過,如果指定了metadata => true,rubydebug codec允許顯示@metadata字段的內容。
~~~
stdout { codec => rubydebug { metadata => true } }
~~~
輸出結果
~~~
$ bin/logstash -f ../test.conf
Pipeline main started
asdf
{
"@timestamp" => 2016-06-30T02:46:48.565Z,
"@metadata字段及其子字段內容。" => {
"test" => "Hello",
"no_show" => "This data will not be in the output"
},
"@version" => "1",
"host" => "windcoder.com",
"show" => "This data will be in the output",
"message" => "asdf"
}
~~~
現在就可以見到@metadata字段及其子字段內容。
只有rubydebug codec允許顯示@metadata字段的內容。
只要您需要臨時字段但不希望它在最終輸出中,就可以使用@metadata字段。
最常見的情景是filter的時間字段,需要一臨時的時間戳。如:
~~~
input { stdin { } }
filter {
grok { match => [ "message", "%{HTTPDATE:[@metadata][timestamp]}" ] }
date { match => [ "[@metadata][timestamp]", "dd/MMM/yyyy:HH:mm:ss Z" ] }
}
output {
stdout { codec => rubydebug }
}
~~~
輸出結果
~~~
$ bin/logstash -f ../test.conf
Pipeline main started
02/Mar/2014:15:36:43 +0100
{
"@timestamp" => 2014-03-02T14:36:43.000Z,
"@version" => "1",
"host" => "windcoder.com",
"message" => "02/Mar/2014:15:36:43 +0100"
}
~~~
上一篇:[Gro](http://www.hmoore.net/zlt2000/microservices-platform/2139576)
- springcloud
- springcloud的作用
- springboot服務提供者和消費者
- Eureka
- ribbon
- Feign
- feign在微服務中的使用
- feign充當http請求工具
- Hystrix 熔斷器
- Zuul 路由網關
- Spring Cloud Config 分布式配置中心
- config介紹與配置
- Spring Cloud Config 配置實戰
- Spring Cloud Bus
- gateway
- 概念講解
- 實例
- GateWay
- 統一日志追蹤
- 分布式鎖
- 1.redis
- springcloud Alibaba
- 1. Nacos
- 1.1 安裝
- 1.2 特性
- 1.3 實例
- 1. 整合nacos服務發現
- 2. 整合nacos配置功能
- 1.4 生產部署方案
- 環境隔離
- 原理講解
- 1. 服務發現
- 2. sentinel
- 3. Seata事務
- CAP理論
- 3.1 安裝
- 分布式協議
- 4.熔斷和降級
- springcloud與alibba
- oauth
- 1. abstract
- 2. oauth2 in micro-service
- 微服務框架付費
- SkyWalking
- 介紹與相關資料
- APM系統簡單對比(zipkin,pinpoint和skywalking)
- server安裝部署
- agent安裝
- 日志清理
- 統一日志中心
- docker安裝部署
- 安裝部署
- elasticsearch 7.x
- logstash 7.x
- kibana 7.x
- ES索引管理
- 定時清理數據
- index Lifecycle Management
- 沒數據排查思路
- ELK自身組件監控
- 多租戶方案
- 慢查詢sql
- 日志審計
- 開發
- 登錄認證
- 鏈路追蹤
- elk
- Filebeat
- Filebeat基礎
- Filebeat安裝部署
- 多行消息Multiline
- how Filebeat works
- Logstash
- 安裝
- rpm安裝
- docker安裝Logstash
- grok調試
- Grok語法調試
- Grok常用表達式
- 配置中常見判斷
- filter提取器
- elasticsearch
- 安裝
- rpm安裝
- docker安裝es
- 使用
- 概念
- 基礎
- 中文分詞
- 統計
- 排序
- 倒排與正排索引
- 自定義dynamic
- 練習
- nested object
- 父子關系模型
- 高亮
- 搜索提示
- kibana
- 安裝
- docker安裝
- rpm安裝
- 整合
- 收集日志
- 慢sql
- 日志審計s
- 云
- 分布式架構
- 分布式鎖
- Redis實現
- redisson
- 熔斷和降級