2.ELK平臺搭建html
系統環境:window7 ,jdk1.7 ,logstash 2.3.4,es 2.3.5 java
備註:logstash要求jdk1.7以上。node
2.1 es搭建mysql
2.1.1 es下載地址:https://www.elastic.co/downloads/ 選中本身的版本,我選擇的是window版本的web
2.1.2 解壓壓文件:elasticsearch-2.3.5redis
2.1.3 安裝插件:Headsql
>cd d:/elasticsearch-2.3.5/bin數據庫
>d:apache
>plugin install mobz/elasticsearch-headjson
進入到/elasticsearch-2.3.5/plugin有一個head的目錄結構,正面已經安裝完成。
2.1.4 配置文件
d:/elasticsearch-2.3.5/config/elasticsearch.yml
修改配置文件:
cluster.name=es_cluster ####集羣名稱
###### 節點名稱 node.name=node0 ###### 數據存儲路徑 path.data=/elasticsearch/data ###### 日誌文件存儲路徑 path.logs=/elasticsearch/logs #當前hostname或IP,我這裏是 network.host=127.0.0.1 # 端口 network.port=9200
2.1.5 啓動es:
>cd d:/elasticsearch-2.3.5/bin
>d:
>elasticsearch
訪問:127.0.0.1:9200 出現下圖,正面已經配置成功
2.2 logstash搭建
logstash是一個日誌收集器,主要三部分組成,input,filter,output.本介紹中,咱們的input是log4j日誌文件,而output是經過es來存儲輸出文件。
2.2.1. 下載地址見2.1.1
2.2.2. 加壓logstash-2.3.4
2.2.3. 配置文件修改
>cd d:/logstash-2.3.4/bin ###添加配置文件config/log4j.conf
a> 經過log4j發送日誌方式
input { log4j { mode => "server" host => "127.0.0.1" port => 4567 } } filter { #Only matched data are send to output. } output { # For detail config for elasticsearch as output, # See: https://www.elastic.co/guide/en/logstash/current/plugins-outputs-elasticsearch.html elasticsearch { action => "index" #The operation on ES hosts => "127.0.0.1:9200" #ElasticSearch host, can be array. index => "AppLog" #The index to write data to. } }
b>經過file文件方式發送
input {
file {
path => "D:/data/file.txt"
start_position => beginning
ignore_older => 0
}
}
c>mq方式發送日誌
zeromq {
address => ["tcp://127.0.0.1:8889"]
mode => "client"
type => "zmq-input"
topic => "weblog"
topology => "pubsub"
codec => "json"
}
}
d>jdbc發送日誌
安裝插件:/plugin install logstash-input-jdbc
input {
jdbc {
# mysql jdbc connection string to our backup databse 後面的test對應mysql中的test數據庫
jdbc_connection_string => "jdbc:mysql://192.168.1.1:3306/test"
# the user we wish to excute our statement as
jdbc_user => "root"
jdbc_password => "******"
# the path to our downloaded jdbc driver
jdbc_driver_library => "/elasticsearch-jdbc-2.3.2.0/lib/mysql-connector-java-5.1.38.jar"
# the name of the driver class for
mysql jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "50000"
#如下對應着要執行的sql的絕對路徑。
statement_filepath => "/usr/local/logstash/bin/logstash_jdbc_test/jdbc.sql"
#定時字段 各字段含義(由左至右)分、時、天、月、年,所有爲*默認含義爲每分鐘都更新(測試結果,不一樣的話請留言指出)
schedule => "* * * * *"
#設定ES索引類型
type => "cc_type"
} }
e>redis插件使用
input {
redis {
batch_count => 1
#返回的事件數量,此屬性僅在list模式下起做用。
data_type => "list"
#logstash redis插件工做方式
key => "logstash-test-list" #監聽的鍵值
host => "127.0.0.1" #redis地址
port => 6379 #redis端口號
password => "123qwe" #若是有安全認證,此項爲密碼
db => 0 #redis數據庫的編號
threads => 1 #啓用線程數量
} }
2.3.4. 啓動
logstash兩種啓動方式,1.logstash -e "" 2.logstash -f [配置文件]基於配置文件啓動方式
>logstash -f config/log4j.conf
2.3 log4j配置:很少介紹
log4j.logger.com.demo.elk=DEBUG, socket
# appender socket
log4j.appender.socket=org.apache.log4j.net.SocketAppender
log4j.appender.socket.Port=4567 #logstash input參數與此端口保持一致
log4j.appender.socket.RemoteHost=127.0.0.1
log4j.appender.socket.layout=org.apache.log4j.PatternLayout
log4j.appender.socket.layout.ConversionPattern=%d [%-5p] [%l] %m%n
log4j.appender.socket.ReconnectionDelay=10000
2.4 es java api簡單使用
2.4.1. pom依賴
<!-- es --> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>2.3.5</version> </dependency>
2.4.2. java代碼
public static void main(String[] args) throws Exception { // applog-對應配置中的index types是類型 size是返回的數據總數 默認不設置時爲10條數據 SearchRequestBuilder search = client.prepareSearch("AppLog").setTypes("logs").setSize(100); showResult(search); client.close(); } /*** *獲取search請求的結果,並輸出打印結果信息 * @param search * @throws Exception */ public static void showResult(SearchRequestBuilder search) throws Exception { SearchResponse r = search.get();// 獲得查詢結果 for (SearchHit hits : r.getHits()) { String data=hits.getSource().get("message").toString(); //if(data.startsWith("{")){ System.out.println(data); //} } long hits = r.getHits().getTotalHits();// 讀取命中數量 System.out.println(hits); }