1.Logstash部署
Logstash一般部署在监控其日志的服务器中,需要java环境。
[root@node2 ~]# rpm -ivh jre-8u271-linux-x64.rpm [root@node2 ~]# rpm -ivh logstash-6.6.0.rpm [root@node2 ~]# systemctl start logstash [root@node2 ~]# ln -s /usr/share/logstash/bin/logstash /usr/local/bin/
2.测试Logstash
[root@node2 ~]# logstash -e 'input { stdin{} } output { stdout{} }'
[root@node2 ~]# logstash -e 'input { stdin{} } output { elasticsearch {hosts=>["192.168.1.122:9200"] } }'
3.logstash收集nginx日志到kibana
编写Logstash配置文件
[root@node2 ~]# cd /etc/logstash/conf.d/ [root@node2 /etc/logstash/conf.d]# cat nginx.conf input { file { path => "/var/log/nginx/access.log" #nginx日志文件路径 type => "access" #类型为access start_position => "beginning" #从开始处收集 } file { path => "/var/log/nginx/error.log" #nginx错误日志文件路径 type => "error" start_position => "beginning" } } output { if [type] == "access" { #如果类型为access则输出到elasticsearch elasticsearch { hosts => ["192.168.1.122:9200"] #es服务所在的服务器 index => "nginx_access-%{+YYYY.MM.dd}" #索引格式 } } if [type] == "error" { elasticsearch { hosts => ["192.168.1.122:9200"] index => "nginx_error-%{+YYYY.MM.dd}" } } }
运行logstash实时收集日志
[root@node2 /etc/logstash/conf.d]# logstash -f nginx.conf & #虽然,是后台运行,还是会输出信息,运行完这条命名后,等待,会输出一堆东西,等到,输出下面这句话后,然后回车 logstash.agent - Successfully started Logstash API endpoint {:port=>9600}