Logstash分析MySQL慢查詢日誌

來源:互聯網
上載者:User

標籤:logstash elk

最近在使用ELKStack對系統日誌進行分析,在網上也有看到有使用logstash的案例,但是發現不能正常解析出來,於是重新花時間去進行正則計算,主要代碼如下:

input {  file {    type => "mysql-slow"    path => "/var/lib/mysql/slow.log"        start_position => beginning                sincedb_write_interval => 0    codec => multiline {      pattern => "^# [email protected]:"      negate => true      what => "previous"    }  }}filter {if [message] =~ "^Tcp" {        drop {}}if [message] =~ "^Time" {        drop {}}if [message] =~ "^\/usr" {        drop {}}  grok {    match => { "message" => "SELECT SLEEP" }    add_tag => [ "sleep_drop" ]    tag_on_failure => []  }  if "sleep_drop" in [tags] {    drop {}  }    grok {    match => { "message" => "(?m)^# [email protected]: %{USER:User}\[[^\]]+\] @ (?:(?<clienthost>\S*) )?\[(?:%{IP:Client_IP})?\]\s.*# Query_time: %{NUMBER:Query_Time:float}\s+Lock_time: %{NUMBER:Lock_Time:float}\s+Rows_sent: %{NUMBER:Rows_Sent:int}\s+Rows_examined: %{NUMBER:Rows_Examined:int}\s*(?:use %{DATA:Database};\s*)?SET timestamp=%{NUMBER:timestamp};\s*(?<Query>(?<Action>\w+)\s+.*)\n# Time:.*$" }    }  date {    match => [ "timestamp", "UNIX" ]    remove_field => [ "timestamp" ]  }}output {            redis {                host => "192.168.1.2:6379"                data_type => "list"                key => "logstash:mysql_slow_log"            }}

說明:

在使用codec/multiline搭配使用的時候,需要注意,grok和普通正則一樣預設是不支援匹配斷行符號換行的。就像你需要=~//m一樣也需要單獨指定,具體寫法是在運算式開始位置加(?m)標記


開始的時候一直沒加(?m),在http://grokdebug.herokuapp.com/調試的時候正常通過,但是到了logstash執行的時候就是不能正常解析。





本文出自 “楓林晚” 部落格,請務必保留此出處http://fengwan.blog.51cto.com/508652/1758920

Logstash分析MySQL慢查詢日誌

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.