安装 Elasticsearch 输出插件
我们可以使用以下命令安装 Elasticsearch 输出插件。
>logstash-plugin install Logstash-output-elasticsearch
logstash.conf
此配置文件包含一个 Elasticsearch 插件,它将输出事件存储在本地安装的 Elasticsearch 中。
input {
file {
path => "C:/tpwork/logstash/bin/log/input.log"
}
}
filter {
grok {
match => [ "message", "%{LOGLEVEL:loglevel} -
%{NOTSPACE:taskid} - %{NOTSPACE:logger} -
%{WORD:label}( - %{INT:duration:int})?" ]
}
if [logger] == "TRANSACTION_START" {
aggregate {
task_id => "%{taskid}"
code => "map['sql_duration'] = 0"
map_action => "create"
}
}
if [logger] == "SQL" {
aggregate {
task_id => "%{taskid}"
code => "map['sql_duration'] ||= 0 ;
map['sql_duration'] += event.get('duration')"
}
}
if [logger] == "TRANSACTION_END" {
aggregate {
task_id => "%{taskid}"
code => "event.set('sql_duration', map['sql_duration'])"
end_of_task => true
timeout => 120
}
}
mutate {
add_field => {"user" => "cainiaoya.com"}
}
}
output {
elasticsearch {
hosts => ["127.0.0.1:9200"]
}
}
输入日志
以下代码块显示了输入日志数据。
INFO - 48566 - TRANSACTION_START - start
INFO - 48566 - SQL - transaction1 - 320
INFO - 48566 - SQL - transaction1 - 200
INFO - 48566 - TRANSACTION_END - end
在 Localhost 启动 Elasticsearch
要在 localhost 启动 Elasticsearch,您应该使用以下命令。
C:\elasticsearch\bin> elasticsearch
一旦 Elasticsearch 准备就绪,您可以通过在浏览器中输入以下 URL 来检查它。
http://localhost:9200/
回复
以下代码块显示了 Elasticsearch 在 localhost 的响应。
{
"name" : "Doctor Dorcas",
"cluster_name" : "elasticsearch",
"version" : {
"number" : "2.1.1",
"build_hash" : "40e2c53a6b6c2972b3d13846e450e66f4375bd71",
"build_timestamp" : "2015-12-15T13:05:55Z",
"build_snapshot" : false,
"lucene_version" : "5.3.1"
},
"tagline" : "You Know, for Search"
}
Note− 有关 Elasticsearch 的更多信息,您可以单击以下链接。
现在,使用上述 Logstash.conf 运行 Logstash
>Logstash –f Logstash.conf
在输出日志中粘贴上述文本后,该文本将由 Logstash 存储在 Elasticsearch 中。您可以通过在浏览器中键入以下 URL 来检查存储的数据。
http://localhost:9200/logstash-2017.01.01/_search?pretty
回复
它是存储在索引 Logstash-2017.01.01 中的 JSON 格式的数据。
{
"took" : 20,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 10,
"max_score" : 1.0,
"hits" : [ {
"_index" : "logstash-2017.01.01",
"_type" : "logs",
"_id" : "AVlZ9vF8hshdrGm02KOs",
"_score" : 1.0,
"_source":{
"duration":200,"path":"C:/tpwork/logstash/bin/log/input.log",
"@timestamp":"2017-01-01T12:17:49.140Z","loglevel":"INFO",
"logger":"SQL","@version":"1","host":"wcnlab-PC",
"label":"transaction1",
"message":" INFO - 48566 - SQL - transaction1 - 200\r",
"user":"cainiaoya.com","taskid":"48566","tags":[]
}
},
{
"_index" : "logstash-2017.01.01",
"_type" : "logs",
"_id" : "AVlZ9vF8hshdrGm02KOt",
"_score" : 1.0,
"_source":{
"sql_duration":520,"path":"C:/tpwork/logstash/bin/log/input.log",
"@timestamp":"2017-01-01T12:17:49.145Z","loglevel":"INFO",
"logger":"TRANSACTION_END","@version":"1","host":"wcnlab-PC",
"label":"end",
"message":" INFO - 48566 - TRANSACTION_END - end\r",
"user":"cainiaoya.com","taskid":"48566","tags":[]
}
}
}
}