一、Logstash介绍
此文针对5.x 以前 logstach
Logstash是一个完全开源的工具,他可以对你的日志进行收集、分析,并将其存储供以后使用(如,搜索),您可以使用它。说到搜索,logstash带有一个web界面,搜索和展示所有日志。
kibana也是一个开源和免费的工具,他可以帮助您汇总、分析和搜索重要数据日志并提供友好的web界面。他可以为 Logstash 和 ElasticSearch 提供的日志分析的 Web 界面。
提示
: logstash 与 elasticSearch 是解耦
的
更多精彩:ELKstack 自动化日志相关文章
1) logstach在ELK中的作用:
1,INput 输入内容
2,OUTput 输出内容
首先LogStash是收集日志,它收集完日志就需要把日志存储下来,所以我们可以用运输者的身份来表示LogStash(INPUT or OUTPUT)LogStash可以在日志发送之前做一个过滤(OUTPUT之前)
二、LogStash部署与配置
出了上面的input+output,我们还需要更灵活的筛选(过滤)出需要的内容,我们就要使用到FILTER
1) 设置时区
[root@qiuyt ~]# yum install ntpdate –y
[root@qiuyt ~]# ntpdate time1.aliyun.com
[root@qiuyt ~]# timedatectl set-timezone Asia/Shanghai #设置时区
另一种设置时区方法
[root@qiuyt ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
像我这样高逼格的玩家一般都把这种命令用一条显示
[root@qiuyt ~]# yum install ntpdate -y && ntpdate time1.aliyun.com && timedatectl set-timezone Asia/Shanghai
2 ) 检查JDK并安装
[root@qiuyt ~]# yum install -y java
[root@qiuyt ~]# java -version
openjdk version "1.8.0_65"
OpenJDK Runtime Environment (build 1.8.0_65-b17)
OpenJDK 64-Bit Server VM (build 25.65-b01, mixed mode)
3)yum安装LogStach
#下载并安装GPG key
[root@qiuyt ~]# rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
#我们可以查询是否导入成功,可能有很多key
[root@qiuyt ~]# rpm -qa gpg-pubkey*
gpg-pubkey-f4a80eb5-53a7ff4b
gpg-pubkey-d88e42b4-52371eca
4)添加yum仓库
[root@qiuyt ~]# cat /etc/yum.repos.d/logstash.repo
[logstash-2.3]
name=Logstash repository for 2.3.x packages
baseurl=https://packages.elastic.co/logstash/2.3/centos
gpgcheck=1
gpgkey=https://packages.elastic.co/GPG-KEY-elasticsearch
enabled=1
5)安装logstach
[root@qiuyt~]# yum install -y logstash
[root@qiuyt~]# rpm -qa |grep logst
logstash-2.3.4-1.noarch
#请在2台服务器都进行安装
6)配置logstach
[root@qiuyt ~]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { stdout{} }'
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
Settings: Default pipeline workers: 1
Pipeline main started
www.qiuyt.com
2017-03-12T04:13:31.347Z qiuyt.com www.qiuyt.com
input插件stdin(标准输入)
output插件 stdout(标准输出)
/opt/logstash/bin/logstash 前台启动
提示:标准输入和标准输出的意思就是我们输入什么就会输出什么
输入:www.dgstack.com
输入:时间戳、节点名称、内容
7)使用codec rubydebug功能
[root@qiuyt ~]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { stdout{ codec => rubydebug} }'
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
Settings: Default pipeline workers: 1
Pipeline main started
www.qiuyt.com
{
"message" => "www.qiuyt.com", #内容
"@version" => "1", #版本
"@timestamp" => "2018-09-17 T04:18:06.702Z", #时间戳
"host" => "qiuyt.com" #主机
}
=> 代表等号
这样就可以让我们的输出内容显示的美观一些
8)将日志写入es里面
Logstach官方文档汇总
Logstach官方文档
Input插件文档
output插件文档
日志如何写入到elasitcsearch:https://www.elastic.co/guide/en/logstash/current/plugins-outputs-elasticsearch.html
三、生产场景案例
[root@ghzz /]# opt/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["192.168.0.33:9200"] index => "logstash-%{+YYYY.MM.dd}" } }'
#Settings: Default pipeline workers: 4
#Pipeline main started
#www.dgstack.com -qiuyuetao 这条信息已经写入到es中
访问地址: http://ip:9200/_plugin/head/
这样我们就可以看到我们输出的www.dgstack.cn ---time的信息,以及日志的时间等。我们写入完之后直接刷新就可以显示
提示:我们学习logstash需要灵活的运用本身的插件,来实现我们想要的任何的功能
我们现在把logstash放在前台运行,是为了更好的学习插件。现在我们需要把logstash运行在后台
修改配置文件logstach
1)查看配置文件
yum安装的目录需要将配置文件放在/etc/logstash/conf.d目录下,因为脚本会在这个目录进行读取
[root@qiuyt ~]# vim /etc/init.d/logstash
脚本启动的时候会去这个目录下找配置文件
2) 编写配置文件
cd /etc/logstash/conf.d/
[root@node3 conf.d]# cat file.conf
input {
file{
path => ["/var/log/messages","/var/log/secure","/var/log/cron"]
type => "system-log"
start_position => "end"
}
}
filter{
}
output{
elasticsearch {
hosts => ["192.168.0.33:9200"]
index => "system-log-%{+YYYY.MM.dd}"
}
}
#配置文件的语法需要包含INPUT or OUTPUT,其中filter可以没有。
#INPUT和OUTPUT中的{}不可以搞错,其中INPUT存放INPUT插件,OUTPUT存放OUTPUT插件
#(其中插件也需要有{花括号}) “=>”代表等于,写一个数组需要使用[中括号]多个可以使用逗号分隔。
#字符串需要使用“双引号”
#号代表注释
#日志中信息都是一行一行的,在logstash中叫做事件(因为logstash可以将多行进行合并)
#流程: 事件 --> input --> codec --> filter --> codec --> output
3) 指定配置文件启动
/opt/logstash/bin/logstash -f /etc/logstash/conf.d/file.conf
这时候我们写的配置文件已经可以实现简单的输出和将数据写入ES中
四、插件介绍
4.1 INPUT插件介绍
file插件地址:https://www.elastic.co/guide/en/logstash/current/plugins-inputs-file.html
1) 收集mesassage错误日志
[root@qiuyt ~]# vim /etc/logstash/conf.d/file.conf
input {
file{
path => ["/var/log/messages","/var/log/secure"]
type => "system-log"
start_position => "beginning"
}
}
filter{
}
output{
elasticsearch {
hosts => ["192.168.56.11:9200"] #这个IP就是es的地址
index => "system-log-%{+YYYY.MM}"
}
}
#path 定义日志路径
#start_position 记录日志从头读取数据还是从尾读取数据
#type 可以设置类型,进行if判断(简单来说可以将日志进行分类,错误放在一起,正常放在一起)
#端口默认是 9200
2)启动logstach
[root@qiuyt ~]# /opt/logstash/bin/logstash -f /etc/logstash/conf.d/file.conf
启动
我们可以访问head进行查看
]
五、通过logstach 收集多个日志文件(自定义日志)
input {
file {
path => "/var/log/messages" #日志路径
type => "systemlog" #事件的唯一类型
start_position => "beginning" #第一次收集日志的位置
stat_interval => "3" #日志收集的间隔时间
}
file {
path => "/var/log/secure"
type => "securelog"
start_position => "beginning" #是从头开始同步,还是结尾同步
stat_interval => "3"
}
}
output {
if [type] == "systemlog" {
elasticsearch {
hosts => ["192.168.56.11:9200"]
index => "system-log-%{+YYYY.MM.dd}"
}}
if [type] == "securelog" {
elasticsearch {
hosts => ["192.168.56.11:9200"]
index => "secury-log-%{+YYYY.MM.dd}"
}}
}
到此结束,如需拓展请自行查看后续文章,或直接查阅官方文档,谢谢大家。
- QQ精品交流群
-
- 微信公众号
-