查看所有的索引文件:
curl -XGET http://localhost:9200/_cat/indices?v
刪除索引文件以釋放空間:
curl -XDELETE http://localhost:9200/filebeat-2016.12.28
單節點的elk可在索引目錄刪除索引文件:集群環境刪除某節點的索引文件,會導致集群服務不可用.集群環境需要使用API的方式進行刪除.
索引文件保留在服務器中,大大減小服務器的性能,占用硬盤空間,
因此使用腳本自動刪除elk中兩個月以前的索引以釋放空間:
--#!/bin/bash
find '/data/elasticsearch/data/elks/nodes/0/indices/' -name 'filebeat-*' -ctime +60 > index.txt
cd ~
cat index.txt | while read line
do
curl -XDELETE "http://localhost:9200/"$(basename $line)""
done
添加計划任務:
$crontab -e
0 0 * * * cd /root && ./elk_index_remove.sh >>/dev/null
ps:shell中單引號&雙引號:
單引號里的任何字符都會原樣輸出,單引號字符串中的變量是無效的.
單引號字符串中不能出現單引號(對單引號使用轉義符后也不可以).
雙引號里可以有變量.
雙引號里可以出現轉移字符.
使用數組循環刪除.
index=(curl -XGET 'http://localhost:9200/_cat/indices/*?v'|awk '{print $3}'|sed '1d'
)
for i in ${index[*]}:do
curl -XDELETE "http://localhost:9200/$i"
done