LogStash使用筆記

Logstash

1. grok

(1) 語(yǔ)法

這東西就是正則表達(dá)式匹配,不過(guò)由于日志太大了,如果你這個(gè)正則從頭寫(xiě)到尾估計(jì)寫(xiě)一半人就沒(méi)了,所以人家預(yù)定義好了許多的正則表達(dá)式,那些%xxx本質(zhì)上就是那些正則胎署,一樣的替代一下。

內(nèi)置的那些預(yù)定義好的正則表達(dá)式窑滞,在github上的地址:
https://github.com/logstash-plugins/logstash-patterns-core/blob/master/patterns/grok-patterns

這里直接粘過(guò)來(lái)省的去找

USERNAME [a-zA-Z0-9._-]+
USER %{USERNAME}
EMAILLOCALPART [a-zA-Z][a-zA-Z0-9_.+-=:]+
EMAILADDRESS %{EMAILLOCALPART}@%{HOSTNAME}
INT (?:[+-]?(?:[0-9]+))
BASE10NUM (?<![0-9.+-])(?>[+-]?(?:(?:[0-9]+(?:\.[0-9]+)?)|(?:\.[0-9]+)))
NUMBER (?:%{BASE10NUM})
BASE16NUM (?<![0-9A-Fa-f])(?:[+-]?(?:0x)?(?:[0-9A-Fa-f]+))
BASE16FLOAT \b(?<![0-9A-Fa-f.])(?:[+-]?(?:0x)?(?:(?:[0-9A-Fa-f]+(?:\.[0-9A-Fa-f]*)?)|(?:\.[0-9A-Fa-f]+)))\b

POSINT \b(?:[1-9][0-9]*)\b
NONNEGINT \b(?:[0-9]+)\b
WORD \b\w+\b
NOTSPACE \S+
SPACE \s*
DATA .*?
GREEDYDATA .*
QUOTEDSTRING (?>(?<!\\)(?>"(?>\\.|[^\\"]+)+"|""|(?>'(?>\\.|[^\\']+)+')|''|(?>`(?>\\.|[^\\`]+)+`)|``))
UUID [A-Fa-f0-9]{8}-(?:[A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}
# URN, allowing use of RFC 2141 section 2.3 reserved characters
URN urn:[0-9A-Za-z][0-9A-Za-z-]{0,31}:(?:%[0-9a-fA-F]{2}|[0-9A-Za-z()+,.:=@;$_!*'/?#-])+

# Networking
MAC (?:%{CISCOMAC}|%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\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?
IPV4 (?<![0-9])(?:(?:[0-1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])[.](?:[0-1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])[.](?:[0-1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])[.](?:[0-1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5]))(?![0-9])
IP (?:%{IPV6}|%{IPV4})
HOSTNAME \b(?:[0-9A-Za-z][0-9A-Za-z-]{0,62})(?:\.(?:[0-9A-Za-z][0-9A-Za-z-]{0,62}))*(\.?|\b)
IPORHOST (?:%{IP}|%{HOSTNAME})
HOSTPORT %{IPORHOST}:%{POSINT}

# paths
PATH (?:%{UNIXPATH}|%{WINPATH})
UNIXPATH (/([\w_%!$@:.,+~-]+|\\.)*)+
TTY (?:/dev/(pts|tty([pq])?)(\w+)?/?(?:[0-9]+))
WINPATH (?>[A-Za-z]+:|\\)(?:\\[^\\?*]*)+
URIPROTO [A-Za-z]([A-Za-z0-9+\-.]+)+
URIHOST %{IPORHOST}(?::%{POSINT:port})?
# uripath comes loosely from RFC1738, but mostly from what Firefox
# doesn't turn into %XX
URIPATH (?:/[A-Za-z0-9$.+!*'(){},~:;=@#%&_\-]*)+
#URIPARAM \?(?:[A-Za-z0-9]+(?:=(?:[^&]*))?(?:&(?:[A-Za-z0-9]+(?:=(?:[^&]*))?)?)*)?
URIPARAM \?[A-Za-z0-9$.+!*'|(){},~@#%&/=:;_?\-\[\]<>]*
URIPATHPARAM %{URIPATH}(?:%{URIPARAM})?
URI %{URIPROTO}://(?:%{USER}(?::[^@]*)?@)?(?:%{URIHOST})?(?:%{URIPATHPARAM})?

# Months: January, Feb, 3, 03, 12, December
MONTH \b(?:[Jj]an(?:uary|uar)?|[Ff]eb(?:ruary|ruar)?|[Mm](?:a|?)?r(?:ch|z)?|[Aa]pr(?:il)?|[Mm]a(?:y|i)?|[Jj]un(?:e|i)?|[Jj]ul(?:y|i)?|[Aa]ug(?:ust)?|[Ss]ep(?:tember)?|[Oo](?:c|k)?t(?:ober)?|[Nn]ov(?:ember)?|[Dd]e(?:c|z)(?:ember)?)\b
MONTHNUM (?:0?[1-9]|1[0-2])
MONTHNUM2 (?:0[1-9]|1[0-2])
MONTHDAY (?:(?:0[1-9])|(?:[12][0-9])|(?:3[01])|[1-9])

# Days: Monday, Tue, Thu, etc...
DAY (?:Mon(?:day)?|Tue(?:sday)?|Wed(?:nesday)?|Thu(?:rsday)?|Fri(?:day)?|Sat(?:urday)?|Sun(?:day)?)

# Years?
YEAR (?>\d\d){1,2}
HOUR (?:2[0123]|[01]?[0-9])
MINUTE (?:[0-5][0-9])
# '60' is a leap second in most time standards and thus is valid.
SECOND (?:(?:[0-5]?[0-9]|60)(?:[:.,][0-9]+)?)
TIME (?!<[0-9])%{HOUR}:%{MINUTE}(?::%{SECOND})(?![0-9])
# datestamp is YYYY/MM/DD-HH:MM:SS.UUUU (or something like it)
DATE_US %{MONTHNUM}[/-]%{MONTHDAY}[/-]%{YEAR}
DATE_EU %{MONTHDAY}[./-]%{MONTHNUM}[./-]%{YEAR}
ISO8601_TIMEZONE (?:Z|[+-]%{HOUR}(?::?%{MINUTE}))
ISO8601_SECOND (?:%{SECOND}|60)
TIMESTAMP_ISO8601 %{YEAR}-%{MONTHNUM}-%{MONTHDAY}[T ]%{HOUR}:?%{MINUTE}(?::?%{SECOND})?%{ISO8601_TIMEZONE}?
DATE %{DATE_US}|%{DATE_EU}
DATESTAMP %{DATE}[- ]%{TIME}
TZ (?:[APMCE][SD]T|UTC)
DATESTAMP_RFC822 %{DAY} %{MONTH} %{MONTHDAY} %{YEAR} %{TIME} %{TZ}
DATESTAMP_RFC2822 %{DAY}, %{MONTHDAY} %{MONTH} %{YEAR} %{TIME} %{ISO8601_TIMEZONE}
DATESTAMP_OTHER %{DAY} %{MONTH} %{MONTHDAY} %{TIME} %{TZ} %{YEAR}
DATESTAMP_EVENTLOG %{YEAR}%{MONTHNUM2}%{MONTHDAY}%{HOUR}%{MINUTE}%{SECOND}

# Syslog Dates: Month Day HH:MM:SS
SYSLOGTIMESTAMP %{MONTH} +%{MONTHDAY} %{TIME}
PROG [\x21-\x5a\x5c\x5e-\x7e]+
SYSLOGPROG %{PROG:program}(?:\[%{POSINT:pid}\])?
SYSLOGHOST %{IPORHOST}
SYSLOGFACILITY <%{NONNEGINT:facility}.%{NONNEGINT:priority}>
HTTPDATE %{MONTHDAY}/%{MONTH}/%{YEAR}:%{TIME} %{INT}

# Shortcuts
QS %{QUOTEDSTRING}

# Log formats
SYSLOGBASE %{SYSLOGTIMESTAMP:timestamp} (?:%{SYSLOGFACILITY} )?%{SYSLOGHOST:logsource} %{SYSLOGPROG}:

# Log Levels
LOGLEVEL ([Aa]lert|ALERT|[Tt]race|TRACE|[Dd]ebug|DEBUG|[Nn]otice|NOTICE|[Ii]nfo|INFO|[Ww]arn?(?:ing)?|WARN?(?:ING)?|[Ee]rr?(?:or)?|ERR?(?:OR)?|[Cc]rit?(?:ical)?|CRIT?(?:ICAL)?|[Ff]atal|FATAL|[Ss]evere|SEVERE|EMERG(?:ENCY)?|[Ee]merg(?:ency)?)
(2) gork提取信息
grok提取字段

不過(guò)grok這東西神煩琼牧,不能自定義提取,放兩個(gè)案例哀卫。
成功案例:
\[%{TIMESTAMP_ISO8601:timestamp}\]\s+(GatewayMessage:\s+)(\|)%{DATA:url}(\|)%{DATA:methodvalue}(\|)%{DATA:queryParams}(\|)%{DATA:remote}(\|)%{DATA:statusCode}(\|)%{DATA:time}(\|)%{DATA:response}

魔改案例:
\[%{TIMESTAMP_ISO8601:timestamp}\]\s+(GatewayMessage:\s+)(\|)(.*?)(\|)%{DATA:methodvalue}(\|)%{DATA:queryParams}(\|)%{DATA:remote}(\|)%{DATA:statusCode}(\|)%{DATA:time}(\|)%{DATA:response}

魔改案例里面巨坊,把第一個(gè)DATA改成了.*?,這樣的確可以起到占位符的作用聊训,直接把這一個(gè)跳過(guò)去抱究,不過(guò)如果想把自定義的這些正則表達(dá)式匹配的值,輸入到一個(gè)鍵值對(duì)里輸出出來(lái)带斑,那是不成的鼓寺。
如果要輸出出來(lái)勋拟,只能把自己定義的正則表達(dá)式命好名字,然后輸入到一個(gè)文件里面妈候,然后把這個(gè)文件告訴給Logstash敢靡,這樣才能用自定義輸出json鍵值對(duì)。

grok和json報(bào)錯(cuò)

最開(kāi)始的寫(xiě)法是%{DATA:response}苦银,用一個(gè)DATA指代后面GatewayMessage那一部分啸胧,這個(gè)在Kibana調(diào)試的時(shí)候沒(méi)任何問(wèn)題,不過(guò)在docker里面一跑就是json解析出錯(cuò)幔虏。
改成%{GREEDYDATA:response}之后就沒(méi)問(wèn)題了纺念。
DATA對(duì)應(yīng)的是.*?
GREEDYDATA對(duì)應(yīng).*

總結(jié)就是:DATA出錯(cuò)的地方改成GREEDYDATA。

解析出來(lái)的數(shù)據(jù):

這也就是filebeat的日志結(jié)構(gòu)

{
    #message是gateway輸出的信息
    "message" => "[2020-02-21 13:40:50] GatewayMessage: |http://39.100.144.125:7052/auth/test|GET|{}|/119.190.195.231:5388|200 OK|4ms|{\"code\":200,\"msg\":\"success\",\"result\":\"1\"}   ",
    #下面這一排是logstash解析出來(lái)的
    "timestamp" => "2020-02-21 13:40:50",
    "time" => "4ms",
    "methodvalue" => "GET",
    "queryParams" => "{}",
    "url" => "http://39.100.144.125:7052/auth/test",
    "remote" => "/119.190.195.231:5388",
    "statusCode" => "200 OK",
    "response" => "{\"code\":200,\"msg\":\"success\",\"result\":\"1\"}   ",
    
    #這下面是filebeat自帶的
    #這條日志的tag屬性
    "tags" => [
        [0] "gateway",
        [1] "beats_input_codec_json_applied"
    ],
    #生產(chǎn)這條日志的filebeat信息
    "agent" => {
        "hostname" => "a4f546cbb1c0",
        "version" => "7.6.0",
        "type" => "filebeat",
        "id" => "378e62e8-0934-4879-9173-b879b8870811",
        "ephemeral_id" => "374a14bb-8a4f-4115-adf9-2d9eee6d3204"
    },
    #這條日志的文件來(lái)源
    "log" => {
        "offset" => 7449808,
        "file" => {
        "path" => "/var/log/gateway.log"
        }
    },
    "input" => {
        "type" => "log"
    },
    "@version" => "1",
    "host" => {
        "name" => "a4f546cbb1c0"
    },
    "ecs" => {
        "version" => "1.4.0"
    },
    #時(shí)間戳
    "@timestamp" => 2020-02-21T05:40:52.728Z
}
(3) grok正則表達(dá)式調(diào)試

在沒(méi)寫(xiě)出gork正則的前提下想括,先別到docker里面去改配置文件調(diào)試陷谱,那樣有夠慢的,先在kibana下面調(diào)試一下瑟蜈,這個(gè)比較方便烟逊。

(4) 看docker日志

大的正則表達(dá)式已經(jīng)寫(xiě)完了,剩下一點(diǎn)小語(yǔ)法修修改改的铺根,就可以去docker里面改配置文件去調(diào)試了宪躯。

docker logs --tail="100" logstash

順便記另一個(gè),tail看文件

# 看末尾五行
tail -n 5 logstash.log
# -f實(shí)時(shí)刷新位迂,F(xiàn)5
tail -f logstash.log
(5) logstash具體的filter

首先split可以保證這個(gè)不出錯(cuò)访雪,因?yàn)橛袔讉€(gè)|這個(gè)都可以商量。
不過(guò)上場(chǎng)就用grok的話(huà)掂林,如果|對(duì)不上冬阳,就解析錯(cuò)誤了。
幸運(yùn)的是logstash解析錯(cuò)誤不報(bào)bug党饮,只是那條日志的輸出會(huì)帶上logstash錯(cuò)誤這樣的標(biāo)簽,所以盡情grok吧驳庭。

2. 其他filter

最終的logstash配置

input {
  beats {
    port => 5044
    }
}

filter {
  grok {
       match => { "message" => "\[%{TIMESTAMP_ISO8601:timestamp}\]\s+(GatewayMessage:\s+)(\|)%{DATA:url}(\|)%{DATA:methodvalue}(\|)%{DATA:queryParams}(\|)%{DATA:remote}(\|)%{DATA:statusCode}(\|)%{DATA:time}(\|)%{GREEDYDATA:response}" }
        remove_field => ["message"]
  }
#把java那段輸出改成json
  json {
       source => "response"
       target => "jsonresponse"
  }
  mutate {
        remove_field =>["response"]
  }
#地理信息順便山一大堆
  geoip {
        source => "remote"
        remove_field => ["[geoip][latitude]", "[geoip][longitude]",
        "[geoip][region_code]", "[geoip][country_name]", "[geoip][continent_code]",
        "[geoip][location]", "[geoip][country_code3]", "[geoip][ip]", "[geoip][country_code2]",
        "[geoip][timezone]"]
        }

}

output{
    elasticsearch {
        hosts=>["39.100.144.125:9200"]  
        index => "logstash-%{+YYYY.MM.dd}" #對(duì)日志進(jìn)行索引歸檔
    }
    
    #stdout{codec => rubydebug}
}

輸出到es的索引刑顺,是按照天來(lái)更改的,所以在es里面就每天建立一個(gè)索引饲常,方便刪除往期的數(shù)據(jù)蹲堂,以及這樣可以保證一個(gè)索引不會(huì)太大,能提高一點(diǎn)搜索效率贝淤。

3. 幾個(gè)有用的博客

https://www.cnblogs.com/FengGeBlog/p/10305318.html

https://www.cnblogs.com/dyh004/p/9699813.html

https://blog.csdn.net/wfs1994/article/details/80862952

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末柒竞,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子播聪,更是在濱河造成了極大的恐慌朽基,老刑警劉巖布隔,帶你破解...
    沈念sama閱讀 206,839評(píng)論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異稼虎,居然都是意外死亡衅檀,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,543評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門(mén)霎俩,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)哀军,“玉大人,你說(shuō)我怎么就攤上這事打却∩际剩” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 153,116評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵柳击,是天一觀的道長(zhǎng)猿推。 經(jīng)常有香客問(wèn)我,道長(zhǎng),這世上最難降的妖魔是什么邦危? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,371評(píng)論 1 279
  • 正文 為了忘掉前任毁菱,我火速辦了婚禮,結(jié)果婚禮上具垫,老公的妹妹穿的比我還像新娘。我一直安慰自己试幽,他們只是感情好筝蚕,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,384評(píng)論 5 374
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著铺坞,像睡著了一般起宽。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上济榨,一...
    開(kāi)封第一講書(shū)人閱讀 49,111評(píng)論 1 285
  • 那天坯沪,我揣著相機(jī)與錄音,去河邊找鬼擒滑。 笑死腐晾,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的丐一。 我是一名探鬼主播藻糖,決...
    沈念sama閱讀 38,416評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼库车!你這毒婦竟也來(lái)了巨柒?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 37,053評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎洋满,沒(méi)想到半個(gè)月后晶乔,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,558評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡芦岂,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,007評(píng)論 2 325
  • 正文 我和宋清朗相戀三年瘪弓,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片禽最。...
    茶點(diǎn)故事閱讀 38,117評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡腺怯,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出川无,到底是詐尸還是另有隱情呛占,我是刑警寧澤,帶...
    沈念sama閱讀 33,756評(píng)論 4 324
  • 正文 年R本政府宣布懦趋,位于F島的核電站晾虑,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏仅叫。R本人自食惡果不足惜帜篇,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,324評(píng)論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望诫咱。 院中可真熱鬧笙隙,春花似錦、人聲如沸坎缭。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,315評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)掏呼。三九已至坏快,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間憎夷,已是汗流浹背莽鸿。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,539評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留拾给,地道東北人富拗。 一個(gè)月前我還...
    沈念sama閱讀 45,578評(píng)論 2 355
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像鸣戴,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子粘拾,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,877評(píng)論 2 345