ELK日誌分析單機系統詳解
日誌分析ELK平臺,由ElasticSearch、Logstash和Kiabana三個開源工具組成。
官方網站:https://www.elastic.co/products
Elasticsearch是個開源分散式搜尋引擎,它的特點有:分散式,零配置,自動發現,索引自動分片,索引副本機制,restful風格介面,多資料來源,自動搜尋負載等。
Logstash是一個完全開源的工具,他可以對你的日誌進行收集、過濾,並將其儲存供以後使用(如,搜尋)。
Kibana 也是一個開源和免費的工具,它Kibana可以為 Logstash 和 ElasticSearch 提供的日誌分析友好的 Web 介面,可以幫助您彙總、分析和搜尋重要資料日誌。
ELK原理圖:
如圖:Logstash收集AppServer產生的Log,並存放到ElasticSearch叢集中,而Kibana則從ES叢集中查詢資料生成圖表,再返回給Browser。
ELK平臺搭建
系統環境
System: Centos release 6.7(Final)
ElasticSearch: elasticsearch-5.3.1.tar.gz
Logstash: logstash-5.3.1.tar.gz
Kibana: kibana-5.3.1-linux-x86_64.tar.gz
Java: openjdk version ”1.8.0_131″
ELK官網下載:https://www.elastic.co/downloads/
JAVA環境配置
下載最新版本1.8.0_131
cd /tmp/
tar zxfjdk-8u131-linux-x64.tar.gz -C /usr/local/
vim /etc/profile
新增如下內容
JAVA_HOME=/usr/local/jdk1.8.0_131
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME PATH CLASSPATH
source /etc/profile
編輯limits.conf 新增類似如下內容
vi /etc/security/limits.conf
新增如下內容:
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
修改配置sysctl.conf
vi /etc/sysctl.conf
新增下面配置:
vm.max_map_count=655360
並執行命令:
sysctl -p
ElasticSearch配置
wgethttps://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.3.1.tar.gz
useradd elktest
tar-zxvfelasticsearch-5.3.1.tar.gz
cdelasticsearch-5.3.1
viconfig/elasticsearch.yml
修改以下配置項:
(路徑需要先自行建立,並且elktest使用者可讀寫)
cluster.name:elk_cluster
node.name:node0
path.data:/tmp/elasticsearch/data
path.logs:/tmp/elasticsearch/logs
network.host: 192.168.1.5
http.port:9200
其他的選項保持預設,然後啟動ES:
su elktest#ES不允許root啟動服務 必須是普通使用者
./bin/elasticsearch&
配置Logstash
wgethttps://artifacts.elastic.co/downloads/logstash/logstash-5.3.1.tar.gz
tar-zxvflogstash-5.3.1.tar.gz
cdlogstash-5.3.1
viconfig/logstash.conf
input {
file {
path => ["/var/log/messages"]
type => "syslog"
}
}
filter {
grok {
match => [ "message", "%{SYSLOGBASE} %{GREEDYDATA:content}" ]
}
}
output {
elasticsearch {
hosts => ["192.168.1.5:9200"]
index => "syslog-%{+YYY.MM.dd}"
}
stdout { codec => rubydebug }
}
./bin/logstash -f config/logstash.conf &#啟動服務
配置Kibana:
wgethttps://artifacts.elastic.co/downloads/kibana/kibana-5.3.1-linux-x86_64.tar.gz
tar-zxvfkibana-5.3.1-linux-x86.tar.gz
cdkibana-5.3.1-linux-x86
viconfig/kibana.yml
server.port:5601
server.host:"192.168.1.5"
elasticsearch.url:http://192.168.1.5:9200
kibana.index:".kibana.yml"
./bin/kibana &
開啟kibana介面192.168.1.5:5601
建立索引(logstash.conf 中的index名字index => "syslog-%{+YYY.MM.dd}")
回到discover介面,並在機器上執行安裝操作 yum install http -y (用於輸出系統日誌),可以看到kibana上有日誌下刷了
展開最新的一條日誌
-------------------------------------------------------------------------------
增加多檔案輸入的logstash配置
[[email protected]config]#catlogstash.conf input{ file{ path=>["/var/log/messages"] type=>"syslog" } file{ path=>["/var/log/httpd/access_log"] type=>"httpdlog" } } filter{ grok{ match=>["message","%{SYSLOGBASE}%{GREEDYDATA:content}"] } } output{ if[type]=="syslog"{ elasticsearch{ hosts=>["192.168.0.153:9200"] index=>"syslog-%{+YYY.MM.dd}" } } if[type]=="httpdlog"{ elasticsearch{ hosts=>["192.168.0.153:9200"] index=>"httpdlog-%{+YYY.MM.dd}" } } stdout{codec=>rubydebug} }
然後重啟logstash,回到kibana上建立httpdlog索引,即可檢視不同索引的日誌輸出情況
---------------------------------------------------------------------------------------
filebeat的加入
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.5.2-linux-x86_64.tar.gz
tar zxf filebeat-5.5.2-linux-x86_64.tar.gz
mv filebeat-5.5.2-linux-x86_64 filebeat
cd filebeat/
cp filebeat.yml filebeat.yml.default
vifilebeat.yml [[email protected]filebeat]#catfilebeat.yml|grep-v"^$"|grep-v"^#"|grep-v"#" filebeat.prospectors: -input_type:log paths: -/var/log/httpd/access_log fields: logIndex:httpdlog docType:httpd-access project:httpd output.logstash: hosts:["192.168.0.153:5044"]
然後啟動filebeat
./filebeat &
修改logstash.conf
[[email protected]config]#catlogstash.conf input{ beats{ port=>"5044" } } filter{ grok{ match=>["message","%{SYSLOGBASE}%{GREEDYDATA:content}"] } } output{ elasticsearch{ hosts=>["192.168.0.153:9200"] index=>"httpdlog-%{+YYY.MM.dd}" } stdout{codec=>rubydebug} }
重啟logstash
./bin/logstash -f config/logstash.conf &
kibana中檢視輸出正常
------------------------------------------------------------
增加mysql慢日誌監控
安裝mariadb
yum install mariadb* -y
vim /etc/my.cnf
增加開啟慢日誌配置
slow_query_log = ON
slow_query_log_file = /var/log/mariadb/slow.log
long_query_time = 1
啟動資料庫 systemctl start mariadb
執行慢日誌語句
[[email protected]config]#mysql WelcometotheMariaDBmonitor.Commandsendwith;or\g. YourMariaDBconnectionidis2 Serverversion:5.5.60-MariaDBMariaDBServer Copyright(c)2000,2018,Oracle,MariaDBCorporationAbandothers. Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement. MariaDB[(none)]>selectsleep(2); +----------+ |sleep(2)| +----------+ |0| +----------+ 1rowinset(2.00sec)
檢視慢日誌
[[email protected]logstash]#tailf/var/log/mariadb/slow.log /usr/libexec/mysqld,Version:5.5.60-MariaDB(MariaDBServer).startedwith: Tcpport:0Unixsocket:/var/lib/mysql/mysql.sock TimeIdCommandArgument #Time:19040417:11:50 #[email protected]:root[root]@localhost[] #Thread_id:2Schema:QC_hit:No #Query_time:2.002019Lock_time:0.000000Rows_sent:1Rows_examined:0 SETtimestamp=1554369110; selectsleep(2);
增加mysql日誌到logstash
在config下建立一個配置目錄test ,將所有的conf檔案都放到test目錄下 ,
test目錄下必須是同類型的檔案 不能和beat形式的logstash配置混合使用。
啟動時候用指定多配置檔案目錄 例如: ./bin/logstash -f config/test
新建mysqllog.conf的配置
input{ file{ path=>["/var/log/mariadb/slow.log"] type=>"mysqllog" } } filter{ grok{ match=>["message","#[email protected]:\s+%{WORD:user1}\[%{WORD:user2}\]\[email protected]\s+\[(?:%{IP:clientip})?\]\s+#\s+Thread_id:\s+%{NUMBER:thread_id:int}\s+Schema:\s+%{WORD:schema}\s+QC_hit:\s+%{WORD:qc_hit}\s+#\s+Query_time:\s+%{NUMBER:query_time:float}\s+Lock_time:\s+%{NUMBER:lock_time:float}\s+Rows_sent:\s+%{NUMBER:rows_sent:int}\s+Rows_examined:\s+%{NUMBER:rows_examined:int}\s+#\s+Rows_affected:\s+%{NUMBER:rows_affected:int}\s+SET\s+timestamp=%{NUMBER:timestamp};\s+(?<query>(?<action>\w+)\s+.*);"] } } output{ elasticsearch{ hosts=>["192.168.0.153:9200"] index=>"mysqllog-%{+YYY.MM.dd}" } stdout{codec=>rubydebug} }
啟動./bin/logstash -f config/test (多個配置檔案 啟動時指定目錄)
啟動後回到頁面建立mysqllog的index,然後檢視慢日誌
部署中的常見錯誤及解決方法
1、啟動elasticsearch 如出現異常 can not run elasticsearch as root
解決方法:建立ES 賬戶,修改資料夾 檔案 所屬使用者 組
2、啟動異常:ERROR: bootstrap checks failed
system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
解決方法:在elasticsearch.yml中配置bootstrap.system_call_filter為false,注意要在Memory下面:
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
3、啟動後,如果只有本地可以訪問,嘗試修改配置檔案 elasticsearch.yml
中network.host(注意配置檔案格式不是以 # 開頭的要空一格, : 後要空一格)
為 network.host: 0.0.0.0
預設埠是 9200
注意:關閉防火牆 或者開放9200埠
4、ERROR: bootstrap checks failed
max file descriptors [4096] for elasticsearch process likely too low, increase to at least [65536]
max number of threads [1024] for user [lishang] likely too low, increase to at least [2048]
解決方法:切換到root使用者,編輯limits.conf 新增類似如下內容
vi /etc/security/limits.conf
新增如下內容:
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
5、max number of threads [1024] for user [lish] likely too low, increase to at least [2048]
解決方法:切換到root使用者,進入limits.d目錄下修改配置檔案。
vi /etc/security/limits.d/90-nproc.conf
修改如下內容:
* soft nproc 1024
#修改為
* soft nproc 2048
6、max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]
解決方法:切換到root使用者修改配置sysctl.conf
vi /etc/sysctl.conf
新增下面配置:
vm.max_map_count=655360
並執行命令:
sysctl -p
然後,重新啟動elasticsearch,即可啟動成功。
7 載入多配置檔案
我們知道在啟動logstash的時候,只要加上-f /you_path_to_config_file就可以載入配置檔案了,如果我們需要載入多個配置檔案,只需要-f /you_path_to_config_directory就可以了。簡單說,就是在-f後面加上目錄就可以。
注意:目錄後面不能加 * 號,否則只會讀取一個檔案,但是在讀取日誌檔案時,*可以匹配所有,比如sys.log*可以匹配所有以sys.log開頭的日誌檔案,如sys.log1,sys.log2等。
示例如下: //比如/home/husen/config/目錄下有 //in1.conf、in2.conf、filter1.conf、filter2.conf、out.conf這5個檔案 //我們使用/logstash-5.5.2/bin/logstash-f/home/husen/config啟動logtstash //logstash會自動載入這個5個配置檔案,併合併成1個整體的配置檔案
8 一個配置讀取多檔案寫法示例
input{ stdin{} file{ path=>"D:\ELK\logstash_6.2.3\bin\conf\data_for_test\accounts.json" start_position=>"beginning" type=>"accounts" } file{ path=>"D:\ELK\logstash_6.2.3\bin\conf\data_for_test\logs.jsonl" start_position=>"beginning" type=>"logs" } file{ path=>"D:\ELK\logstash_6.2.3\bin\conf\data_for_test\shakespeare.json" start_position=>"beginning" type=>"shakespeare" } } filter{ json{ source=>"message"//以JSON格式解析 target=>"doc"//解析到doc下面 remove_field=>["message"]//移除message } } output{ if[type]=="accounts"{ elasticsearch{ hosts=>"localhost:9200" index=>"blog_001" document_type=>"accounts" } } if[type]=="logs"{ elasticsearch{ hosts=>"localhost:9200" index=>"blog_002" document_type=>"logs" } } if[type]=="shakespeare"{ elasticsearch{ hosts=>"localhost:9200" index=>"blog_003" document_type=>"shakespeare" } } stdout{ codec=>rubydebug } }
轉載於:https://blog.51cto.com/superleedo/1918771