1. 程式人生 > >使用groovy指令碼自動清理過多Elasticsearch索引

使用groovy指令碼自動清理過多Elasticsearch索引

之前搭建了一套ELK方案分佈日誌採集系統,一直執行比較穩定,每日採集日誌量600萬+,存放es索引的磁碟只有150G,一般1個月左右就滿了,所以必須定期清理一下舊索引,以免爆了引起連鎖反應,導致整個應用鏈都掛了。

手工清晰比較麻煩,花了點時間,使用groovy寫了個指令碼,配合linux cron定時每天執行,效果理想。

@Grab('com.squareup.okhttp:okhttp:2.5.0')
@Grab('org.slf4j:slf4j-log4j12:1.7.12')


import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;


Properties pro = new Properties();
pro.put("log4j.rootLogger", "DEBUG,file"); 
pro.put("log4j.appender.file", "org.apache.log4j.RollingFileAppender"); 
pro.put("log4j.appender.file.File", "/home/apps/mamaner_index_shell/logs/info.txt"); 
pro.put("log4j.appender.file.Threshold", "info");
pro.put("log4j.appender.file.MaxFileSize", "1024kb");
pro.put("log4j.appender.file.MaxBackupIndex", "100");
pro.put("log4j.appender.file.encoding", "UTF-8");
pro.put("log4j.appender.file.layout", "org.apache.log4j.PatternLayout");
pro.put("log4j.appender.file.layout.ConversionPattern", "[%d] [%-5p] [%c.%M:%L] - %m%n");
PropertyConfigurator.configure(pro);

Logger logger = Logger.getLogger(this.class);
logger.info("Start to check index....");

//獲取某個區間磁碟剩餘百分比
def getDiskFreePercent = {
        String info -> 
        def a = info.split("%")[0]
        def p = a.substring(a.lastIndexOf(" ")).trim()
}
//獲取整個磁碟的資訊
def getDiskinfo=
{
        try {
            Process process = Runtime.getRuntime().exec("df -m");
            process.waitFor();
            InputStreamReader ir = new InputStreamReader(process.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            
            String line;
            while ((line = input.readLine()) != null) {
             if (line.indexOf("/home")>=0)
                return line;
            }
        } catch (java.io.IOException e) {
            System.err.println("IOException " + e.getMessage());
        }
}
//http刪除
def httpDelete={
    String url->
    
    logger.info("DELETE URL:"+url);

    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(url).delete(null).build();
    Response response = client.newCall(request).execute();  
    if (response.isSuccessful()) {     
       println response.body().string();
    }
    else {  
          println response;
    }
}

//獲取當前索引目錄並排序
def getIndexFolders={

    def indexFoldList = new ArrayList<String>();
    def File targetFile = new File("/home/apps/log-watcher/elasticsearch-2.1.1/data/elasticsearch/nodes/0/indices")
    targetFile.eachDir {
        File indFold->
        if (indFold.getName().indexOf("logstash")>=0) indexFoldList.add(indFold.getName())
    }
    Collections.sort(indexFoldList,new Comparator<String>(){
                public int compare(String arg0, String arg1) {
                    return arg0.compareTo(arg1);
                }
            });
    return indexFoldList;
}

//超過預定磁碟佔用,開始從最早日期刪除索引,知道容量滿足
def captain = 70;
for(;getDiskFreePercent(getDiskinfo()).toFloat()>captain;)
{
     httpDelete("http://localhost:9200/"+getIndexFolders()[0])
}

logger.info("Finish checked index!");