diff --git a/projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/SnapshotMonitor.groovy b/projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/SnapshotMonitor.groovy new file mode 100644 index 000000000..fecb21729 --- /dev/null +++ b/projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/SnapshotMonitor.groovy @@ -0,0 +1,75 @@ +package com.fangjia.script.temp.shenjian + +import com.alibaba.fastjson.JSONObject +import com.fangjia.core.util.DateUtil +import com.fangjia.script.BaseBatch +import com.fangjia.script.BatchOwner +import com.fangjia.script.Watcher +import com.fangjia.script.util.DateUtils +import com.fangjia.script.yinjihaun.util.DingDingMessageUtil + +/** + * 快照监控 + * + * @author shenjian + * @since 2021/8/27 + */ +@BatchOwner(Watcher.ShenJian) +class SnapshotMonitor extends BaseBatch { + + // 运维组 运维警报 + static final access_token = "2307d2fec8314b3e8b4b83dd1d42da2e2051638e3e668f21e77ca01258bf682c" + + static void main(String[] args) { + runBatch(new SnapshotMonitor(), args) + } + + + @Override + void process(Object args) { + // /_snapshot/es_backup/weekly-snap-2021.05* + // 监控两个月的快照 + String[] months = [DateUtils.formatDateByPattern(new Date(), "yyyy.MM"), DateUtils.formatDateByPattern(DateUtil.addMonthToDate(new Date(), -1), "yyyy.MM")] + String urlPrefix = "http://192.168.0.206:9200/_snapshot/es_backup/weekly-snap-" + + String msg = ""; + for (String month : months) { + // 必须分开写,不能有空格 + String[] cmds = ["curl", "-XGET", urlPrefix + month + "*", "--user", "elastic:goojia123456"] + String result = execCurl(cmds) + JSONObject jsonObject = JSONObject.parse(result); + + if (jsonObject.get("snapshots") != null) { + com.alibaba.fastjson.JSONArray jsonArray = jsonObject.get("snapshots") + for (JSONObject obj : jsonArray) { + String state = obj.get("state") + if (!"SUCCESS".equals(state)) { + msg = "【ES集群快照异常】\n" + obj.get("failures").toString() + DingDingMessageUtil.sendTextMessage(access_token, msg) + return ; + } + } + } + } + } + + static String execCurl(String[] cmds) { + ProcessBuilder process = new ProcessBuilder(cmds); + Process p; + try { + p = process.start(); + BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); + StringBuilder builder = new StringBuilder(); + String line = null; + while ((line = reader.readLine()) != null) { + builder.append(line); + builder.append(System.getProperty("line.separator")); + } + return builder.toString(); + + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } +}