logstash推送mysql慢查詢日誌

來源:互聯網
上載者:User

標籤:logstash   mysql   slowlog   kibana   

本文將將介紹通過logstash用來收集mysql的慢查詢日誌,然後推送給elasticsearch,並建立自訂的索引,最終通過kibana進行web展示。

環境介紹:

作業系統版本:centos6.6 64bit

Mysql版本: mysql5.6.17與mysql5.1.36

Logstash版本: logstash-2.0.0.tar.gz

Elasticsearch版本:elasticsearch-2.1.0.tar.gz

Kibana版本:Kibana 4.2.1

Java版本:1.8.0_45

 

一:mysql5.1.36版本

1:配置mysql5.1.36版本慢查詢日誌,這裡為了測試,將查詢時間超過0.1s的均記錄到慢查詢日誌中

mysql> show variables like ‘%slow%‘;mysql> show variables like ‘%long%‘;

650) this.width=650;" src="http://s4.51cto.com/wyfs02/M00/76/F8/wKiom1Zf4h3hwRsSAABQzxZFlpM360.png" title="圖片1.png" alt="wKiom1Zf4h3hwRsSAABQzxZFlpM360.png" />

2:配置logstash

# cat /usr/local/logstash/etc/logstach.conf input {  file {    type => "mysql-slow"    path => "/mydata/slow-query.log"    codec => multiline {      pattern => "^# [email protected]:"      negate => true      what => "previous"    }  }}#input節的配置定義了輸入的日誌類型為mysql慢查詢日誌類型以及日誌路徑,採用合并多行資料。negate欄位是一個選擇開關,可以正向匹配和反向匹配filter {  # drop sleep events  grok {    match => { "message" => "SELECT SLEEP" }    add_tag => [ "sleep_drop" ]    tag_on_failure => [] # prevent default _grokparsefailure tag on real records  }  if "sleep_drop" in [tags] {    drop {}  }#filter節的配置定義了過濾mysql查詢為sleep狀態SQL語句grok {match => [ "message", "(?m)^# [email protected]: %{USER:user}\[[^\]]+\] @ (?:(?<clienthost>\S*) )?\[(?:%{IP:clientip})?\]\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" ]  }}#grok節定義了慢查詢日誌輸出的正則切割,這個容易頭暈眼花!output { stdout { codec => rubydebug {}} elasticsearch {  hosts => "192.168.1.226:9200"  index => "mysql-server81-%{+YYYY.MM.dd}"} }#output節定義了輸出,這裡除了列印到螢幕上之外,還輸入到elasticsearch,同時起一個自訂的索引名稱

3:啟動測試

# /usr/local/logstash/bin/logstash -f /usr/local/logstash/etc/logstach.conf 

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/76/F7/wKioL1Zf4xqB8tebAAFfsjKAAVo381.png" title="圖片2.png" alt="wKioL1Zf4xqB8tebAAFfsjKAAVo381.png" />

# tail -f /mydata/slow-query.log 

650) this.width=650;" src="http://s1.51cto.com/wyfs02/M02/76/F8/wKiom1Zf4tXw7KpzAABc7oIura8872.png" title="圖片3.png" alt="wKiom1Zf4tXw7KpzAABc7oIura8872.png" />

650) this.width=650;" src="http://s2.51cto.com/wyfs02/M00/76/F8/wKiom1Zf4wHjCSXLAACu9og2lg8186.png" style="float:none;" title="圖片4.png" alt="wKiom1Zf4wHjCSXLAACu9og2lg8186.png" />

650) this.width=650;" src="http://s4.51cto.com/wyfs02/M01/76/F8/wKiom1Zf4wODVhEYAAHZCwYKraA264.png" style="float:none;" title="圖片5.png" alt="wKiom1Zf4wODVhEYAAHZCwYKraA264.png" />

650) this.width=650;" src="http://s2.51cto.com/wyfs02/M01/76/F7/wKioL1Zf423jbuOHAACbOVrvnsY108.png" style="float:none;" title="圖片6.png" alt="wKioL1Zf423jbuOHAACbOVrvnsY108.png" />

二:mysql5.6.17版本

由於mysql5.6.17版本的的slowlog多了一個id欄位,所以需要調整grok節的正則配置。

Mysql5.1.36的slowlog:

# tail -f /mydata/slow-query.log # Time: 151202 17:29:24# [email protected]: root[root] @  [192.168.1.156]# Query_time: 6.578696  Lock_time: 0.000039 Rows_sent: 999424  Rows_examined: 999424SET timestamp=1449048564;select * from users_test;

Mysql5.6.17的slowlog:對比mysql5.1.36版本的慢查詢日誌輸出,多了Id: 84589。

# tail -f /mydata/slow-query.log # Time: 151202 16:09:54# [email protected]: root[root] @  [192.168.1.156]  Id: 84589# Query_time: 7.089324  Lock_time: 0.000112 Rows_sent: 1  Rows_examined: 33554432SET timestamp=1449043794;select count(*) from t1;

這裡順便說一下,之前還測試了Percona Server 5.5.34版本,發現慢查詢日誌多了Thread_id,Schema,Last_errno,Killed 4個欄位。

# tail -f /mydata5.5/slow-query.log # [email protected]: root[root] @  [192.168.1.228]# Thread_id: 1164217  Schema: mgr  Last_errno: 0  Killed: 0# Query_time: 0.371185  Lock_time: 0.000056  Rows_sent: 0  Rows_examined: 0  Rows_affected: 2  Rows_read: 0# Bytes_sent: 11SET timestamp=1449105655;REPLACE INTO  edgemgr_dbcache(id, type, data, expire_time)  VALUES(UNHEX(‘ec124ee5766c4a31819719c645dab895‘), ‘sermap‘, ‘{\"storages\":{\"sg1-s1\":[{\"download_port\":9083,\"p2p_port\":9035,\"rtmp_port\":9035,\"addr\":\"{\\\"l\\\":{\\\"https://192.168.1.227:9184/storage\\\":\\\"\\\"},\\\"m\\\":{},\\\"i\\\":{\\\"https://192.168.1.227:9184/storage\\\":\\\"\\\"}}\",\"cpu\":6,\"mem\":100,\"bandwidth\":0,\"disk\":0,\"dead\":0}]},\"lives\":{}}‘, ‘2016-01-02 09:20:55‘);

因而5.6.17版本只需要修改logstash.conf設定檔中的grok節內容如下後重啟logstash進程即可。

grok {match => [ "message", "(?m)^# [email protected]: %{USER:user}\[[^\]]+\] @ (?:(?<clienthost>\S*) )?\[(?:%{IP:clientip})?\]\s*Id: %{NUMBER:id:int}\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:.*$" ]}

650) this.width=650;" src="http://s1.51cto.com/wyfs02/M00/76/F8/wKiom1Zf45Cyh9OFAACafD6n1Qk068.png" title="圖片7.png" alt="wKiom1Zf45Cyh9OFAACafD6n1Qk068.png" />

Kibana日誌輸出

650) this.width=650;" src="http://s4.51cto.com/wyfs02/M02/76/F7/wKioL1Zf5BagM636AACeCxz_-AA033.png" title="圖片8.png" alt="wKioL1Zf5BagM636AACeCxz_-AA033.png" />

本文出自 “斬月” 部落格,謝絕轉載!

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.