1.安装 : 解压tar.gz安装包
2. 控制台采集数据,控制台输出
在安装目录下执行:
bin/logstash -e 'input { stdin {} } output { stdout {} }'
等待logstash启动后,在控制台输入:hello word,回车 结果如下:
json格式输出:
bin/logstash -e 'input { stdin {} } output { stdout { codec => json } }'
启动后,输入 name,回车如下:
3. 把命令放在文件中使用
在安装目录下,新建test.conf文件(可以放在任意目录下,或者新建一个conf目录)
vi test.conf
写入:
input { stdin { } } output { stdout { } } #可以换行,便于理解
执行:
bin/logstash -f test.conf
启动后在控制台输入 hello word ,如下:
4.监控指定的文件
例:监控文件/deng/log/host.log,当该文件有更新时,会在控制台输入新增的内容
vi test.conf
写入:
input {
file { path => "/deng/log/host.log" }
}
output { stdout { } }
在控制台输入:bin/logstash -f test.conf 回车:
然后在新的会话中,输入:
echo “hello word” >> /deng/log/host.log
控制台输出结果如下:
5. 监控文件,并输出到elasticsearch中
vi test.conf
写入:
input {
file{ path => "/deng/log/host.log"
type => "log"
start_position => "beginning"
}
}
output {
elasticsearch {
hosts => ["127.0.0.1:9200"] #es地址
index => "deng" #进入es的索引
}
}
在控制台输入:bin/logstash -f test.conf 回车
6. 过滤器插件grok
vi test.conf
写入:
input {
file { path => "/deng/log/host.log" }
}
filter {
grok { match => { "message" => "%{IP:client} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes}" } } }
output { stdout { } }
在控制台输入:bin/logstash -f test.conf 回车,然后在host.log文件中写入"55.3.244.1 GET /index.html 15824", logstash控制台输出如下:
7.插件+自定义正则
例:解析日志 “2019-06-24 15:42:54,www.china-xyz.com:80,58.213.75.210,/dtwz/index/images/link_zgm.gif,304”
定义自定义正则匹配文件:在安装目录下
vi patterns
TIME_LOG (\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2}) #匹配时间
vi test.conf
在控制台输入:bin/logstash -f test.conf 回车,然后在host.log文件中写入"2019-06-24 15:42:54,www.china-xyz.com:80,58.213.75.210,/dtwz/index/images/link_zgm.gif,304", logstash控制台输出如下:
grok内置常用正则见: https://blog.csdn.net/yl20175514/article/details/81737440
8. 自定义正则
在grok中自定义正则匹配,采用(?<field_name>表达式)方式
vi test.conf
在控制台输入:bin/logstash -f test.conf 回车,然后在host.log文件中写入"2019-06-24 15:42:54,www.china-xyz.com:80,58.213.75.210,/dtwz/index/images/link_zgm.gif,304", logstash控制台输出如下:
logstash安装脚本:
#!/bin/bash
#install logstash
logstash_src="logstash.tar.gz"
target_path="/data"
if [ ! -d $target_path ];then
mkdir -p $target_path
fi
echo "Decompression logstash.tar.gz"
tar zxvf $logstash_src -C $target_path
`cp -f logstash.conf $target_path/logstash`
cat>$target_path/log_up.sh<<EOF
export PATH="$PATH"
time=\$(date "+%Y-%m-%d %H:%M:%S")
echo \$time >> /tmp/2.log
echo "start" >> /tmp/2.log
ps -fe|grep $target_path/logstash|grep -v grep
if [ \$? -ne 0 ]
then
echo "logstash 未启动" >> /tmp/2.log
nohup $target_path/logstash/bin/logstash -f $target_path/logstash/logstash.conf &
echo "logstash 完成启动" >> /tmp/2.log
else
echo "logstash 正在运行" >> /tmp/2.log
fi
echo "end" >> /tmp/2.log
echo " " >> /tmp/2.log
EOF
chmod 755 $target_path/log_up.sh
chmod 755 $target_path/logstash/bin/logstash
echo "@reboot (source $target_path/log_up.sh)" >> /var/spool/cron/root
echo "0 */1 * * * (source $target_path/log_up.sh)" >> /var/spool/cron/root
echo "start logstash"
source $target_path/log_up.sh
echo "Finished"