1.完整的logstash配置文件
[root@logstash ~]# vim /etc/logstash/conf.d/my.conf
input {
file {
path => ["/tmp/c.log"]
type => "test"
start_position => "beginning"
sincedb_path => "/var/lib/logstash/sincedb"
}
}
filter{
grok {
match => { "message" => "%{HTTPD_COMBINEDLOG}" }
remove_field => ["message"]
}
}
output{
stdout{ codec => "rubydebug" }
elasticsearch {
hosts => ["es-0004:9200", "es-0005:9200"]
index => "weblog-%{+YYYY.MM.dd}"
}
}
input代表的是输入/tmp/c.log的文件,并且start_position => "beginning"代表的是每次都从上一次结束的位置开始,将之前的文件都存放在sincedb_path目录
filter 代表的是处理输入的文件,按照宏的模式,调用的是HTTPD_COMBINEDLOG的模式
output 将处理的数据在输出到es中,hosts代表的是存放数据的es,index代表的是索引的名称
1.1 filter grok插件
正则表达式分组匹配格式: (?<名字>正则表达式)正则表达式宏调用格式: %{宏名称:名字}宏文件路径 :/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-patterns-core-4.1.2/patterns
1.2 output elasticsearch插件
output{
stdout{ codec => "rubydebug" }
elasticsearch {
hosts => ["es-0004:9200", "es-0005:9200"]
index => "weblog-%{+YYYY.MM.dd}"
}
2.案例实战
该实验是在web服务器上面安装耗费资源较小的filebeat,然后配置filebeat文件,通过filebeat将日志文件,推送到logstash,经过logstash收集分析,存放到es中,在通过kibana展现出来。
因为logstash是及其耗费服务器资源的,所以在每台需要收集信息的服务器上面安装logstash是及其耗费资源,因此安装filebeat。
2.1 配置hosts
50 — 安装ES
51 — 安装logstash
52 — 安装kibana
[root@es ~]# vim /etc/hosts
192.168.199.50 es
192.168.199.51 logstash
192.168.199.52 kibana
2.2 安装对应的服务和jdk
2.2.1 安装es
[root@es elk]# yum install elasticsearch-6.8.8.rpm -y
[root@es elk]# java -version
java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)
[root@es elk]#
若启动ES找不到java,则创建软连接
[root@es ~]# systemctl status elasticsearch.service
● elasticsearch.service - Elasticsearch
Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since 四 2023-07-20 17:17:18 CST; 5min ago
Docs: http://www.elastic.co
Process: 12989 ExecStart=/usr/share/elasticsearch/bin/elasticsearch -p ${PID_DIR}/elasticsearch.pid --quiet (code=exited, status=1/FAILURE)
Main PID: 12989 (code=exited, status=1/FAILURE)
7月 20 17:17:18 es systemd[1]: Started Elasticsearch.
7月 20 17:17:18 es elasticsearch[12989]: which: no java in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin)
7月 20 17:17:18 es elasticsearch[12989]: warning: Falling back to java on path. This behavior is deprecated. Specify JAVA_HOME
7月 20 17:17:18 es elasticsearch[12989]: could not find java; set JAVA_HOME
7月 20 17:17:18 es systemd[1]: elasticsearch.service: main process exited, code=exited, status=1/FAILURE
7月 20 17:17:18 es systemd[1]: Unit elasticsearch.service entered failed state.
7月 20 17:17:18 es systemd[1]: elasticsearch.service failed.
[root@es ~]# ln -s /usr/local/jdk1.8/bin/java /usr/bin/java
2.2.2 安装logstash
[root@logstash elk]# yum install logstash-6.8.8.rpm -y
安装成功后 ln -s /etc/logstash /usr/share/logstash/config
2.2.3 安装kibana
[root@kibana elk]# yum install kibana-6.8.8-x86_64.rpm -y
修改kibana配置文件
vim /etc/kibana/kibana.yml
02 server.port: 5601
07 server.host: "0.0.0.0"
28 elasticsearch.hosts: ["http://es:9200"]
37 kibana.index: ".kibana"
40 kibana.defaultAppId: "home"
113 i18n.locale: "zh-CN"
启动kibana
[root@kibana elk]# systemctl start kibana.service
[root@kibana elk]# systemctl status kibana.service
2.3 安装httpd
在50安装httpd
原理是 httpd的日志通过filebeat传送到logstash,logstash收到数据在存放到es,kibana在把es里的数据展现出来。
2.4 filebeat
在需要采集的机器上面安装httpd,因为在每台机器安装logstash是及其耗费资源的,所以只需要在httpd的服务器上面安装filebeat,filebeat将收集到的信息发送给logstash。
2.4.1 安装filebeat
[root@es elk]# ls
elasticsearch-6.8.8.rpm logstash-6.8.8.rpm
filebeat-6.8.8-x86_64.rpm metricbeat-6.8.8-x86_64.rpm
kibana-6.8.8-x86_64.rpm
[root@es elk]# yum install -y filebeat
2.4.2 修改filebeat配置
vim /etc/filebeat/filebeat.yml
24: enabled: true # 打开收集模块
28: - /var/log/httpd/access_log # 定义日志路径
148: # 注释掉
150: # 注释掉
161: output.logstash: # 设置输出模块
163: hosts: [“logstash主机IP:5044”] # 输出给logstash
179: # 收集系统相关信息,可以注释掉
180: # 收集系统相关信息,可以注释掉
181: # 收集系统相关信息,可以注释掉
2.4.3 自定义日志标签
filebeat配置
vim /etc/filebeat/filebeat.yml
45: fields:
46: logtype: apache_log
[root@es elk]# grep -Pv "^\s*(#|$)" /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/httpd/access_log
fields:
logtype: apache_log
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
setup.template.settings:
index.number_of_shards: 3
setup.kibana:
output.logstash:
hosts: ["192.168.199.51:5044"]
[root@es elk]#
启动服务
[root@es elk]# systemctl restart filebeat.service httpd
2.5 logstash配置
[root@logstash conf.d]# vim my.conf
input {
beats {
port => 5044
}
}
filter{
if [fields][logtype] == "apache_log" {
grok {
match => { "message" => "%{HTTPD_COMBINEDLOG}" }
remove_field => ["message"]
}}
}
output{
stdout{ codec => "rubydebug" }
if [fields][logtype] == "apache_log" {
elasticsearch {
hosts => ["es:9200"]
index => "weblog-%{+YYYY.MM.dd}"
}}
}
启动logstash
[root@logstash conf.d]# /usr/share/logstash/bin/logstash
beat的端口启动启了 logstash也启动起来了
3.测试
访问50的httpd
我们发现logstash一直在输出
查看es
索引也出现
在kibana展现