ELK日常维护相关

配置启动ELK+FileBeats后台运行, 不随着窗口关闭而关闭

只要在后面加上&即可, 如

1
bin/logstash -f config/log4j-es.conf &

注意: 退出时使用Ctrl+D命令, 直接关闭窗口依然有可能使得进程关闭

LogStash自动重新加载配置

启动logstash时在后面加上命令–config.reload.automatic

1
bin/logstash -f config/log4j-es.conf --config.reload.automatic &

重新发送filebeat抓取的内容

1.删除registry下的data.json
如:

1
rm -rf /home/elk/filebeats/filebeat-7.3.1-linux-x86_64/data/registry/filebeat

2.重启filebeat
如果存在正在运行的filebeat, 先kill掉

1
2
ps aux | grep filebeat  
kill -s 9 pid

然后执行

1
./filebeat -e -c filebeat.yml

设置索引内容30天过期删除

本来我理解的理论上应该可以用索引的生命周期来实现, 但是半天没整明白, 所以现在
是简单粗暴的使用linux的cron任务来实现, 更具体的cron配置详见cron详解
1.编辑shell脚本, 文件名为removeDataByTime.sh, 内容如下

1
2
3
4
5
6
#!/bin/bash
limit_date=30
for var in `curl 'localhost:9200/_cat/indices' | awk '!/kibana*/ {print $3}'`
do
curl -H "Content-Type:application/json" -X POST -d '{"query": {"range": {"@timestamp": {"lt": "now-'${limit_date}'d/d"}}}}' "http://localhost:9200/${var}/_delete_by_query"
done

2.编辑cron文件, 执行crantab -e, 打开crontab配置文件
加上刚才的shell脚本, 设置每天0点15分执行

1
15 00 * * * /home/elk/removeDataByTime.sh

设置cron重新启动, 执行/sbin/service crond restart

3.查看cron执行情况
查看cron任务列表 : crontab -l
查看cron执行日志 : grep ‘removeDataByTime’ /var/log/cron

设置LogStash不写入指定的内容

在logstash中写入如下内容即可 :

1
2
3
4
5
6
7
8
9
10
filter {
# 过滤message中的内容 , 注意drop后面有一个空格
if "somethingA" in [message] {
drop {}
}
# 过滤tags中的内容
if "somethingB" in [tags] {
drop {}
}
}

设置索引别名来实现中文索引搜索

有时候需要在搜索页面使用中文的索引名,但是在ES中不支持直接用中文名来命名索引,原因我认为应该是跟索引的Btree算法有关。

但是我们可以给索引设置别名来在搜索页面使用中文的索引来搜索。

可以按照如下方式来设置

  • 在postman发送如下请求(前提:在ElasticSearch中已经存在datadocking和activity-provider两个索引):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
POST: 192.168.15.161:9200/_aliases
{
"actions": [
{
"add": {
"alias": "外部对接",
"index": "datadocking"
}
},
{
"add": {
"alias": "微商城活动",
"index": "activity-provider"
}
}
]
}
  • 进入项目的索引模式

    输入刚刚设置的中文名,就可以直接对应到英文的索引名,然后按照步骤创建索引模式

    在搜索界面就可以使用中文的索引名来搜索